forked from arnold-benchmark/arnold
-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_bc_lang.py
195 lines (159 loc) · 6.25 KB
/
train_bc_lang.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
"""
For example, run:
Single-task:
python train_bc_lang.py task=pickup_object model=bc_lang_vit \
mode=train batch_size=8 steps=100000
Multi-task:
python train_bc_lang.py task=multi model=bc_lang_vit \
mode=train batch_size=8 steps=200000
"""
import os
import time
import hydra
import torch
import numpy as np
from math import floor
from torch.utils.tensorboard import SummaryWriter
from dataset import ArnoldDataset, ArnoldMultiTaskDataset, InstructionEmbedding
from peract.utils import normalize_quaternion
from bc_z.blocks import SiameseNet, CNNLangAndFcsNet, ViT, ViTLangAndFcsNet
from bc_z.agent import CLIP_encoder, BCLangAgent
def create_lang_encoder(cfg, device):
return CLIP_encoder(device)
def create_agent(cfg, device: str):
if 'cnn' in cfg.model:
siamese_net = SiameseNet(
input_channels=[3, 3],
filters=[16],
kernel_sizes=[5],
strides=[1],
activation=cfg.bc_lang.activation,
norm=None,
)
siamese_net.build()
actor_net = CNNLangAndFcsNet(
siamese_net=siamese_net,
input_resolution=cfg.bc_lang.image_resolution,
filters=[32, 64, 64],
kernel_sizes=[3, 3, 3],
strides=[2, 2, 2],
norm=None,
activation=cfg.bc_lang.activation,
fc_layers=[128, 64, 3 + 4 + 1],
low_dim_state_len=4
)
elif 'vit' in cfg.model:
vit = ViT(
image_size=128,
patch_size=8,
num_classes=16,
dim=64,
depth=6,
heads=8,
mlp_dim=64,
dropout=0.1,
emb_dropout=0.1,
channels=6,
)
actor_net = ViTLangAndFcsNet(
vit=vit,
input_resolution=cfg.bc_lang.image_resolution,
filters=[64, 96, 128],
kernel_sizes=[1, 1, 1],
strides=[1, 1, 1],
norm=None,
activation=cfg.bc_lang.activation,
fc_layers=[128, 64, 3 + 4 + 1],
low_dim_state_len=4
)
actor_net.build()
bc_agent = BCLangAgent(
actor_network=actor_net,
camera_name=cfg.bc_lang.camera_name,
lr=cfg.bc_lang.lr,
weight_decay=cfg.bc_lang.weight_decay,
grad_clip=cfg.bc_lang.grad_clip)
bc_agent.build(training=(cfg.mode == 'train'), device=device)
return bc_agent
def prepare_batch(batch_data, cfg, lang_embed_cache, device):
obs_dict = {}
language_instructions = []
target_points = []
gripper_open = []
low_dim_state = []
for data in batch_data:
for k, v in data['obs_dict'].items():
if k not in obs_dict:
obs_dict[k] = [v]
else:
obs_dict[k].append(v)
target_points.append(data['target_points'])
gripper_open.append(data['target_gripper'])
language_instructions.append(data['language'])
low_dim_state.append(data['low_dim_state'])
for k, v in obs_dict.items():
v = np.stack(v, axis=0)
obs_dict[k] = v.transpose(0, 3, 1, 2) # peract requires input as [C, H, W]
bs = len(language_instructions)
target_points = np.stack(target_points, axis=0)
gripper_open = np.array(gripper_open).reshape(bs, 1)
low_dim_state = np.stack(low_dim_state, axis=0)
trans_action_coords = target_points[:, :3]
rot_action_quat = target_points[:, 3:]
rot_action_quat = normalize_quaternion(rot_action_quat)
lang_goal_embs = lang_embed_cache.get_lang_embed(language_instructions)
inp = {}
inp.update(obs_dict)
inp.update({
'action': np.concatenate([trans_action_coords, rot_action_quat, gripper_open], axis=-1),
'lang_goal_embs': lang_goal_embs,
'low_dim_state': low_dim_state,
'bound': np.array(cfg.offset_bound).reshape(2, 3)
})
for k, v in inp.items():
if v is not None:
if not isinstance(v, torch.Tensor):
v = torch.from_numpy(v)
inp[k] = v.to(device)
return inp
@hydra.main(config_path='./configs', config_name='default')
def main(cfg):
os.makedirs(cfg.checkpoint_dir, exist_ok=True)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
if cfg.task != 'multi':
train_dataset = ArnoldDataset(data_path=os.path.join(cfg.data_root, cfg.task, 'train'), task=cfg.task, obs_type=cfg.obs_type)
else:
train_dataset = ArnoldMultiTaskDataset(data_root=cfg.data_root, obs_type=cfg.obs_type)
writer = SummaryWriter(log_dir=os.path.join(cfg.exp_dir, 'tb_logs'))
agent = create_agent(cfg, device=device)
lang_encoder = create_lang_encoder(cfg, device)
lang_embed_cache = InstructionEmbedding(lang_encoder)
start_step = 0
if cfg.resume:
if os.path.isfile(cfg.resume):
print(f'=> loading checkpoint {cfg.resume}')
start_step = agent.load_weights(cfg.resume)
print(f'=> loaded checkpoint {cfg.resume} (step {start_step})')
else:
print(f'=> no checkpoint found at {cfg.resume}')
cfg.save_interval = floor(cfg.steps/10)
print(f'Training {cfg.steps} steps, {len(train_dataset)} demos, {cfg.batch_size} batch_size')
start_time = time.time()
for iteration in range(start_step, cfg.steps):
# train
batch_data = train_dataset.sample(cfg.batch_size)
inp = prepare_batch(batch_data, cfg, lang_embed_cache, device)
update_dict = agent.update(iteration, inp)
running_loss = update_dict['total_loss']
if iteration % cfg.log_interval == 0:
elapsed_time = (time.time() - start_time) / 60.0
print(f'Iteration: {iteration} | Total Loss: {running_loss} | Elapsed Time: {elapsed_time} mins')
writer.add_scalar('total_loss', running_loss, iteration)
if (iteration+1) % cfg.save_interval == 0:
# save for model selection
ckpt_path = os.path.join(cfg.checkpoint_dir, f'{cfg.model}_{cfg.task}_{cfg.obs_type}_{cfg.lang_encoder}_{iteration+1}.pth')
print('Saving checkpoint')
agent.save_weights(ckpt_path, iteration)
writer.close()
if __name__ == '__main__':
main()