-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_dp.py
More file actions
252 lines (213 loc) · 8.02 KB
/
Copy pathtrain_dp.py
File metadata and controls
252 lines (213 loc) · 8.02 KB
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
from typing import Tuple, Sequence, Dict, Union, Optional
import numpy as np
import math
import torch
import torch.nn as nn
import collections
import zarr
import pickle
import gzip
import numpy as np
import os
import diffusion_policy
import json
from datetime import datetime
from models.base_models.ConditionalUnet1D import ConditionalUnet1D
from models.datasets.image_dataset import ImageDataset
from diffusers.schedulers.scheduling_ddpm import DDPMScheduler
from diffusers.training_utils import EMAModel
from diffusers.optimization import get_scheduler
from tqdm.auto import tqdm
from models.base_models.vision_encoder import get_resnet, replace_bn_with_gn
from models.datasets.maniskill_dataset import ManiSkillTrajectoryDataset
# TODO: add main function with arguments
digest_trajectory_data = True
# download demonstration data from Google Drive
dataset_path = "demos/TurnFaucet-v0/5001.pointcloud.pd_joint_pos.h5"
zarr_path = "demos/TurnFaucet-v0/5001.pointcloud.1024.pd_joint_pos.qpos_tcp.demos.10.zarr"
if digest_trajectory_data:
trajectory_data = ManiSkillTrajectoryDataset(dataset_path, load_count=10, success_only=True, device=None, zarr_path=zarr_path, state_method="qpos_tcp")
# parameters
pred_horizon = 16
obs_horizon = 2
action_horizon = 8
#|o|o| observations: 2
#| |a|a|a|a|a|a|a|a| actions executed: 8
#|p|p|p|p|p|p|p|p|p|p|p|p|p|p|p|p| actions predicted: 16
# create dataset from file
dataset = ImageDataset(
dataset_path=zarr_path,
pred_horizon=pred_horizon,
obs_horizon=obs_horizon,
action_horizon=action_horizon
)
stats = dataset.stats
if digest_trajectory_data:
#save training data statistics (min, max) for each dim
print("Saving stats data...")
path = "demos/TurnFaucet-v0"
data_file = os.path.join(path, 'stats.gzip')
f = gzip.open(data_file,'wb')
pickle.dump(stats, f)
f.close()
print("Stats data saved!")
print(stats)
# create dataloader
dataloader = torch.utils.data.DataLoader(
dataset,
batch_size=64,
num_workers=4,
shuffle=True,
# accelerate cpu-gpu transfer
pin_memory=True,
# don't kill worker process afte each epoch
persistent_workers=True
)
# visualize data in batch
batch = next(iter(dataloader))
print("batch['image'].shape:", batch['image'].shape)
print("batch['agent_pos'].shape:", batch['agent_pos'].shape)
print("batch['action'].shape", batch['action'].shape)
# construct ResNet18 encoder
# if you have multiple camera views, use seperate encoder weights for each view.
vision_encoder = get_resnet('resnet18')
# IMPORTANT!
# replace all BatchNorm with GroupNorm to work with EMA
# performance will tank if you forget to do this!
vision_encoder = replace_bn_with_gn(vision_encoder)
# ResNet18 has output dim of 512
vision_feature_dim = 512
# agent_pos is 2 dimensional
lowdim_obs_dim = 18
# observation feature has 514 dims in total per step
obs_dim = vision_feature_dim + lowdim_obs_dim
action_dim = 8
# create network object
noise_pred_net = ConditionalUnet1D(
input_dim=action_dim,
global_cond_dim=obs_dim*obs_horizon
)
# the final arch has 2 parts
nets = nn.ModuleDict({
'vision_encoder': vision_encoder,
'noise_pred_net': noise_pred_net
})
# demo
with torch.no_grad():
# example inputs
image = torch.zeros((1, obs_horizon, 3, 128, 128))
agent_pos = torch.zeros((1, obs_horizon, 18))
# vision encoder
image_features = nets['vision_encoder'](
image.flatten(end_dim=1))
# (2,512)
image_features = image_features.reshape(*image.shape[:2],-1)
# (1,2,512)
obs = torch.cat([image_features, agent_pos],dim=-1)
# (1,2,514)
noised_action = torch.randn((1, pred_horizon, action_dim))
diffusion_iter = torch.zeros((1,))
# the noise prediction network
# takes noisy action, diffusion iteration and observation as input
# predicts the noise added to action
noise = nets['noise_pred_net'](
sample=noised_action,
timestep=diffusion_iter,
global_cond=obs.flatten(start_dim=1))
# illustration of removing noise
# the actual noise removal is performed by NoiseScheduler
# and is dependent on the diffusion noise schedule
denoised_action = noised_action - noise
# for this demo, we use DDPMScheduler with 100 diffusion iterations
num_diffusion_iters = 100
noise_scheduler = DDPMScheduler(
num_train_timesteps=num_diffusion_iters,
# the choise of beta schedule has big impact on performance
# we found squared cosine works the best
beta_schedule='squaredcos_cap_v2',
# clip output to [-1,1] to improve stability
clip_sample=True,
# our network predicts noise (instead of denoised action)
prediction_type='epsilon'
)
# device transfer
device = torch.device('cuda')
_ = nets.to(device)
num_epochs = 100
# Exponential Moving Average
# accelerates training and improves stability
# holds a copy of the model weights
ema = EMAModel(
parameters=nets.parameters(),
power=0.75)
# Standard ADAM optimizer
# Note that EMA parametesr are not optimized
optimizer = torch.optim.AdamW(
params=nets.parameters(),
lr=1e-4, weight_decay=1e-6)
# Cosine LR schedule with linear warmup
lr_scheduler = get_scheduler(
name='cosine',
optimizer=optimizer,
num_warmup_steps=500,
num_training_steps=len(dataloader) * num_epochs
)
with tqdm(range(num_epochs), desc='Epoch') as tglobal:
# epoch loop
for epoch_idx in tglobal:
epoch_loss = list()
# batch loop
with tqdm(dataloader, desc='Batch', leave=False) as tepoch:
for nbatch in tepoch:
# data normalized in dataset
# device transfer
nimage = nbatch['image'][:,:obs_horizon].to(device)
nagent_pos = nbatch['agent_pos'][:,:obs_horizon].to(device)
naction = nbatch['action'].to(device)
B = nagent_pos.shape[0]
# encoder vision features
image_features = nets['vision_encoder'](
nimage.flatten(end_dim=1))
image_features = image_features.reshape(
*nimage.shape[:2],-1)
# (B,obs_horizon,D)
# concatenate vision feature and low-dim obs
obs_features = torch.cat([image_features, nagent_pos], dim=-1)
obs_cond = obs_features.flatten(start_dim=1)
# (B, obs_horizon * obs_dim)
# sample noise to add to actions
noise = torch.randn(naction.shape, device=device)
# sample a diffusion iteration for each data point
timesteps = torch.randint(
0, noise_scheduler.config.num_train_timesteps,
(B,), device=device
).long()
# add noise to the clean images according to the noise magnitude at each diffusion iteration
# (this is the forward diffusion process)
noisy_actions = noise_scheduler.add_noise(
naction, noise, timesteps)
# predict the noise residual
noise_pred = noise_pred_net(
noisy_actions, timesteps, global_cond=obs_cond)
# L2 loss
loss = nn.functional.mse_loss(noise_pred, noise)
# optimize
loss.backward()
optimizer.step()
optimizer.zero_grad()
# step lr scheduler every batch
# this is different from standard pytorch behavior
lr_scheduler.step()
# update Exponential Moving Average of the model weights
ema.step(nets.parameters())
# logging
loss_cpu = loss.item()
epoch_loss.append(loss_cpu)
tepoch.set_postfix(loss=loss_cpu)
tglobal.set_postfix(loss=np.mean(epoch_loss))
# Weights of the EMA model
# is used for inference
ema_nets = nets
ema.copy_to(ema_nets.parameters())
torch.save(ema_nets.state_dict(),
f'models/checkpoints/ema_nets_{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}.pth')