-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathrt1_inference_example.py
181 lines (147 loc) · 5.22 KB
/
rt1_inference_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""Runs inference with a RT-1 model."""
import copy
from absl import app
from absl import flags
from flax.training import checkpoints
import jax
import jax.numpy as jnp
import numpy as np
import tensorflow as tf
import rt1
_CHECKPOINT_PATH = flags.DEFINE_string(
'checkpoint_path', None, 'Path to checkpoint.'
)
flags.mark_flag_as_required('checkpoint_path')
class RT1Policy:
"""Runs inference with a RT-1 policy."""
def __init__(
self,
checkpoint_path=None,
model=rt1.RT1(),
variables=None,
seqlen=15,
rng=None,
):
"""Initializes the policy.
Args:
checkpoint_path: A checkpoint point from which to load variables. Either
this or variables must be provided.
model: A nn.Module to use for the policy. Must match with the variables
provided by checkpoint_path or variables.
variables: If provided, will use variables instead of loading from
checkpoint_path.
seqlen: The history length to use for observations.
rng: a jax.random.PRNGKey to use for the random number generator.
"""
if not variables and not checkpoint_path:
raise ValueError(
'At least one of `variables` or `checkpoint_path` must be defined.'
)
self.model = model
self._checkpoint_path = checkpoint_path
self.seqlen = seqlen
self._run_action_inference_jit = jax.jit(self._run_action_inference)
if rng is None:
self.rng = jax.random.PRNGKey(0)
else:
self.rng = rng
if variables:
self.variables = variables
else:
state_dict = checkpoints.restore_checkpoint(checkpoint_path, None)
variables = {
'params': state_dict['params'],
'batch_stats': state_dict['batch_stats'],
}
self.variables = variables
def _run_action_inference(self, observation, rng):
"""A jittable function for running inference."""
# We add zero action tokens so that the shape is (seqlen, 11).
# Note that in the vanilla RT-1 setup, where
# `include_prev_timesteps_actions=False`, the network will not use the
# input tokens and instead uses zero action tokens, thereby not using the
# action history. We still pass it in for simplicity.
act_tokens = jnp.zeros((1, 6, 11))
# Add a batch dim to the observation.
batch_obs = jax.tree_map(lambda x: jnp.expand_dims(x, 0), observation)
_, random_rng = jax.random.split(rng)
output_logits = self.model.apply(
self.variables,
batch_obs,
act=None,
act_tokens=act_tokens,
train=False,
rngs={'random': random_rng},
)
time_step_tokens = (
self.model.num_image_tokens + self.model.num_action_tokens
)
output_logits = jnp.reshape(
output_logits, (1, self.seqlen, time_step_tokens, -1)
)
action_logits = output_logits[:, -1, ...]
action_logits = action_logits[:, self.model.num_image_tokens - 1 : -1]
action_logp = jax.nn.softmax(action_logits)
action_token = jnp.argmax(action_logp, axis=-1)
# Detokenize the full action sequence.
detokenized = rt1.detokenize_action(
action_token, self.model.vocab_size, self.model.world_vector_range
)
detokenized = jax.tree_map(lambda x: x[0], detokenized)
return detokenized
def action(self, observation):
"""Outputs the action given observation from the env."""
# Assume obs has no batch dimensions.
observation = copy.deepcopy(observation)
# Jax does not support string types, so remove it from the dict if it
# exists.
if 'natural_language_instruction' in observation:
del observation['natural_language_instruction']
image = observation['image']
# Resize using TF image resize to avoid any issues with using different
# resize implementation, since we also use tf.image.resize in the data
# pipeline. Also scale image to [0, 1].
image = tf.image.resize(image, (300, 300)).numpy()
image /= 255.0
observation['image'] = image
self.rng, rng = jax.random.split(self.rng)
action = self._run_action_inference_jit(
observation, rng
)
action = jax.device_get(action)
# Use the base pose mode if the episode if the network outputs an invalid
# `terminate_episode` action.
if np.sum(action['terminate_episode']) == 0:
action['terminate_episode'] = np.zeros_like(action['terminate_episode'])
action['terminate_episode'][-1] = 1
return action
def main(argv):
del argv
sequence_length = 15
num_action_tokens = 11
layer_size = 256
vocab_size = 512
num_image_tokens = 81
rt1x_model = rt1.RT1(
num_image_tokens=num_image_tokens,
num_action_tokens=num_action_tokens,
layer_size=layer_size,
vocab_size=vocab_size,
# Use token learner to reduce tokens per image to 81.
use_token_learner=True,
# RT-1-X uses (-2.0, 2.0) instead of (-1.0, 1.0).
world_vector_range=(-2.0, 2.0),
)
policy = RT1Policy(
checkpoint_path=_CHECKPOINT_PATH.value,
model=rt1x_model,
seqlen=sequence_length,
)
# Create a fake observation and run the policy.
obs = {
'image': jnp.ones((15, 300, 300, 3)),
'natural_language_embedding': jnp.ones((15, 512)),
}
print(policy.action(obs))
if __name__ == '__main__':
app.run(main)