-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffusion_unet_hybrid_image_policy.py
380 lines (314 loc) · 13.6 KB
/
diffusion_unet_hybrid_image_policy.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
from typing import Dict
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange, reduce
from diffusers.schedulers.scheduling_ddpm import DDPMScheduler
from diffusion_policy.model.common.normalizer import LinearNormalizer
from diffusion_policy.policy.base_image_policy import BaseImagePolicy
from diffusion_policy.model.diffusion.conditional_unet1d import ConditionalUnet1D
from diffusion_policy.model.diffusion.mask_generator import LowdimMaskGenerator
from diffusion_policy.common.robomimic_config_util import get_robomimic_config
from robomimic.algo import algo_factory
from robomimic.algo.algo import PolicyAlgo
import robomimic.utils.obs_utils as ObsUtils
import robomimic.models.base_nets as rmbn
import diffusion_policy.model.vision.crop_randomizer as dmvc
from diffusion_policy.common.pytorch_util import dict_apply, replace_submodules
import numpy as np
import matplotlib.pyplot as plt
class DiffusionUnetHybridImagePolicy(BaseImagePolicy):
def __init__(self,
shape_meta: dict,
noise_scheduler: DDPMScheduler,
horizon,
n_action_steps,
n_obs_steps,
num_inference_steps=None,
obs_as_global_cond=True,
crop_shape=(76, 76),
diffusion_step_embed_dim=256,
down_dims=(256,512,1024),
kernel_size=5,
n_groups=8,
cond_predict_scale=True,
obs_encoder_group_norm=False,
eval_fixed_crop=False,
# parameters passed to step
**kwargs):
super().__init__()
# parse shape_meta
action_shape = shape_meta['action']['shape']
assert len(action_shape) == 1
action_dim = action_shape[0]
obs_shape_meta = shape_meta['obs']
obs_config = {
'low_dim': [],
'rgb': [],
'depth': [],
'scan': []
}
obs_key_shapes = dict()
for key, attr in obs_shape_meta.items():
shape = attr['shape']
obs_key_shapes[key] = list(shape)
type = attr.get('type', 'low_dim')
if type == 'rgb':
obs_config['rgb'].append(key)
elif type == 'low_dim':
obs_config['low_dim'].append(key)
else:
raise RuntimeError(f"Unsupported obs type: {type}")
# get raw robomimic config
config = get_robomimic_config(
algo_name='bc_rnn',
hdf5_type='image',
task_name='square',
dataset_type='ph')
with config.unlocked():
# set config with shape_meta
config.observation.modalities.obs = obs_config
if crop_shape is None:
for key, modality in config.observation.encoder.items():
if modality.obs_randomizer_class == 'CropRandomizer':
modality['obs_randomizer_class'] = None
else:
# set random crop parameter
ch, cw = crop_shape
for key, modality in config.observation.encoder.items():
if modality.obs_randomizer_class == 'CropRandomizer':
modality.obs_randomizer_kwargs.crop_height = ch
modality.obs_randomizer_kwargs.crop_width = cw
# init global state
ObsUtils.initialize_obs_utils_with_config(config)
# load model
policy: PolicyAlgo = algo_factory(
algo_name=config.algo_name,
config=config,
obs_key_shapes=obs_key_shapes,
ac_dim=action_dim,
device='cpu',
)
obs_encoder = policy.nets['policy'].nets['encoder'].nets['obs']
if obs_encoder_group_norm:
# replace batch norm with group norm
replace_submodules(
root_module=obs_encoder,
predicate=lambda x: isinstance(x, nn.BatchNorm2d),
func=lambda x: nn.GroupNorm(
num_groups=x.num_features//16,
num_channels=x.num_features)
)
# obs_encoder.obs_nets['agentview_image'].nets[0].nets
# obs_encoder.obs_randomizers['agentview_image']
if eval_fixed_crop:
replace_submodules(
root_module=obs_encoder,
predicate=lambda x: isinstance(x, rmbn.CropRandomizer),
func=lambda x: dmvc.CropRandomizer(
input_shape=x.input_shape,
crop_height=x.crop_height,
crop_width=x.crop_width,
num_crops=x.num_crops,
pos_enc=x.pos_enc
)
)
# create diffusion model
obs_feature_dim = obs_encoder.output_shape()[0]
input_dim = action_dim + obs_feature_dim
global_cond_dim = None
if obs_as_global_cond:
input_dim = action_dim
global_cond_dim = obs_feature_dim * n_obs_steps
# import ipdb; ipdb.set_trace()
model = ConditionalUnet1D(
input_dim=input_dim,
local_cond_dim=None,
global_cond_dim=global_cond_dim,
diffusion_step_embed_dim=diffusion_step_embed_dim,
down_dims=down_dims,
kernel_size=kernel_size,
n_groups=n_groups,
cond_predict_scale=cond_predict_scale
)
# import ipdb; ipdb.set_trace()
print(input_dim, global_cond_dim, diffusion_step_embed_dim)
self.obs_encoder = obs_encoder
self.model = model
self.noise_scheduler = noise_scheduler
self.mask_generator = LowdimMaskGenerator(
action_dim=action_dim,
obs_dim=0 if obs_as_global_cond else obs_feature_dim,
max_n_obs_steps=n_obs_steps,
fix_obs_steps=True,
action_visible=False
)
self.normalizer = LinearNormalizer()
self.horizon = horizon
self.obs_feature_dim = obs_feature_dim
self.action_dim = action_dim
self.n_action_steps = n_action_steps
self.n_obs_steps = n_obs_steps
self.obs_as_global_cond = obs_as_global_cond
self.kwargs = kwargs
if num_inference_steps is None:
num_inference_steps = noise_scheduler.config.num_train_timesteps
self.num_inference_steps = num_inference_steps
print("Diffusion params: %e" % sum(p.numel() for p in self.model.parameters()))
print("Vision params: %e" % sum(p.numel() for p in self.obs_encoder.parameters()))
# ========= inference ============
def conditional_sample(self,
condition_data, condition_mask,
local_cond=None, global_cond=None,
generator=None,
# keyword arguments to scheduler.step
**kwargs
):
# import ipdb; ipdb.set_trace()
# condition_data = rotate_1(10, condition_data)
condition_data = torch.randn(condition_data.shape[0], condition_data.shape[1], 2).to(condition_data)
model = self.model
# import ipdb; ipdb.set_trace()
scheduler = self.noise_scheduler
trajectory = torch.randn(
size=condition_data.shape,
dtype=condition_data.dtype,
device=condition_data.device,
generator=generator)
# trajectory[:] = 0
# set step values
scheduler.set_timesteps(self.num_inference_steps)
# for t in torch.flip(scheduler.timesteps, dims=[0]):
# model_output = model(trajectory, t,
# local_cond=local_cond, global_cond=global_cond)
# trajectory = trajectory.detach().clone() + model_output * (1 / len(scheduler.timesteps))
# trajectory = torch.randn(batch_size, dim)
t = torch.ones(trajectory.shape[0]) * (torch.pi/2)
t = t.cuda()
# dt = (torch.pi/2) / 2
# for i in range(2):
# model_output = model(trajectory, t*100,
# local_cond=local_cond, global_cond=global_cond)
# trajectory = trajectory + model_output * dt
# t = t - dt
step = 1
if step == 1:
trajectory = -model(trajectory, t*100,
local_cond=local_cond, global_cond=global_cond)
else:
trajectory = torch.cos(torch.tensor(1.5)) * trajectory - torch.sin(torch.tensor(1.5)) * model(trajectory, 1.5*100, local_cond=local_cond, global_cond=global_cond)
trajectory = torch.cos(torch.tensor(1.1)) * trajectory - torch.sin(torch.tensor(1.1)) * model(trajectory, 1.1*100, local_cond=local_cond, global_cond=global_cond)
return trajectory
def predict_action(self, obs_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
"""
obs_dict: must include "obs" key
result: must include "action" key
"""
assert 'past_action' not in obs_dict # not implemented yet
# normalize input
nobs = self.normalizer.normalize(obs_dict)
value = next(iter(nobs.values()))
B, To = value.shape[:2]
T = self.horizon
Da = self.action_dim
Do = self.obs_feature_dim
To = self.n_obs_steps
# build input
device = self.device
dtype = self.dtype
# handle different ways of passing observation
local_cond = None
global_cond = None
# import ipdb; ipdb.set_trace()
if self.obs_as_global_cond:
# condition through global feature
this_nobs = dict_apply(nobs, lambda x: x[:,:To,...].reshape(-1,*x.shape[2:]))
nobs_features = self.obs_encoder(this_nobs)
# reshape back to B, Do
global_cond = nobs_features.reshape(B, -1)
# empty data for action
cond_data = torch.zeros(size=(B, T, Da), device=device, dtype=dtype)
cond_mask = torch.zeros_like(cond_data, dtype=torch.bool)
else:
# condition through impainting
this_nobs = dict_apply(nobs, lambda x: x[:,:To,...].reshape(-1,*x.shape[2:]))
nobs_features = self.obs_encoder(this_nobs)
# reshape back to B, To, Do
nobs_features = nobs_features.reshape(B, To, -1)
cond_data = torch.zeros(size=(B, T, Da+Do), device=device, dtype=dtype)
cond_mask = torch.zeros_like(cond_data, dtype=torch.bool)
cond_data[:,:To,Da:] = nobs_features
cond_mask[:,:To,Da:] = True
# run sampling
# import ipdb; ipdb.set_trace()
nsample = self.conditional_sample(
cond_data,
cond_mask,
local_cond=local_cond,
global_cond=global_cond,
**self.kwargs)
# unnormalize prediction
naction_pred = nsample[...,:Da]
action_pred = self.normalizer['action'].unnormalize(naction_pred)
# get action
start = To - 1
end = start + self.n_action_steps
action = action_pred[:,start:end]
result = {
'action': action,
'action_pred': action_pred
}
return result
# ========= training ============
def set_normalizer(self, normalizer: LinearNormalizer):
self.normalizer.load_state_dict(normalizer.state_dict())
def compute_loss(self, batch):
# normalize input
assert 'valid_mask' not in batch
nobs = self.normalizer.normalize(batch['obs'])
nactions = self.normalizer['action'].normalize(batch['action'])
batch_size = nactions.shape[0]
horizon = nactions.shape[1]
# handle different ways of passing observation
local_cond = None
global_cond = None
trajectory = nactions
cond_data = trajectory
if self.obs_as_global_cond:
# reshape B, T, ... to B*T
this_nobs = dict_apply(nobs,
lambda x: x[:,:self.n_obs_steps,...].reshape(-1,*x.shape[2:]))
nobs_features = self.obs_encoder(this_nobs)
# reshape back to B, Do
global_cond = nobs_features.reshape(batch_size, -1)
else:
# reshape B, T, ... to B*T
this_nobs = dict_apply(nobs, lambda x: x.reshape(-1, *x.shape[2:]))
nobs_features = self.obs_encoder(this_nobs)
# reshape back to B, T, Do
nobs_features = nobs_features.reshape(batch_size, horizon, -1)
cond_data = torch.cat([nactions, nobs_features], dim=-1)
trajectory = cond_data.detach()
# generate impainting mask
condition_mask = self.mask_generator(trajectory.shape)
# Sample noise that we'll add to the images
noise = torch.randn(trajectory.shape, device=trajectory.device)
# import ipdb; ipdb.set_trace()
t = torch.rand(noise.shape[0], 1, 1).cuda()* (torch.pi/2)
x_noise = torch.randn(trajectory.shape, device=trajectory.device)
x_real = trajectory
x_t = torch.cos(t) * x_real + torch.sin(t) * x_noise
noisy_trajectory = x_t
target = torch.cos(t) * x_noise - torch.sin(t) * x_real
timesteps = t.squeeze() * 100
# Predict the noise residual
# import ipdb; ipdb.set_trace()
pred = self.model(noisy_trajectory, timesteps,
local_cond=local_cond, global_cond=global_cond)
loss = F.mse_loss(pred, target, reduction='none')
# loss = loss * loss_mask.type(loss.dtype)
loss = reduce(loss, 'b ... -> b (...)', 'mean')
loss = loss.mean()
return loss
diffusion_unet_hybrid_image_policy.py