-
Notifications
You must be signed in to change notification settings - Fork 5
/
train.py
207 lines (184 loc) · 8.43 KB
/
train.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
import os
import json
import torch
import argparse
import numpy as np
import torch.nn as nn
import MinkowskiEngine as ME
import matplotlib.pyplot as plt
import torch.distributed as dist
from tqdm import tqdm
from copy import deepcopy
from easydict import EasyDict as edict
from diffusers.optimization import get_cosine_schedule_with_warmup
from policy import RISE
from dataset.realworld import RealWorldDataset, collate_fn
from utils.training import set_seed, plot_history, sync_loss
default_args = edict({
"data_path": "data/collect_pens",
"aug": False,
"aug_jitter": False,
"num_action": 20,
"voxel_size": 0.005,
"obs_feature_dim": 512,
"hidden_dim": 512,
"nheads": 8,
"num_encoder_layers": 4,
"num_decoder_layers": 1,
"dim_feedforward": 2048,
"dropout": 0.1,
"ckpt_dir": "logs/collect_pens",
"resume_ckpt": None,
"resume_epoch": -1,
"lr": 3e-4,
"batch_size": 240,
"num_epochs": 1000,
"save_epochs": 50,
"num_workers": 24,
"seed": 233
})
def train(args_override):
# load default arguments
args = deepcopy(default_args)
for key, value in args_override.items():
args[key] = value
# prepare distributed training
torch.multiprocessing.set_sharing_strategy('file_system')
WORLD_SIZE = int(os.environ['WORLD_SIZE'])
RANK = int(os.environ['RANK'])
LOCAL_RANK = int(os.environ['LOCAL_RANK'])
os.environ['NCCL_P2P_DISABLE'] = '1'
dist.init_process_group(backend = 'nccl', init_method = 'env://', world_size = WORLD_SIZE, rank = RANK)
# set up device
set_seed(args.seed)
torch.cuda.set_device(LOCAL_RANK)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# dataset & dataloader
if RANK == 0: print("Loading dataset ...")
dataset = RealWorldDataset(
path = args.data_path,
split = 'train',
num_obs = 1,
num_action = args.num_action,
voxel_size = args.voxel_size,
aug = args.aug,
aug_jitter = args.aug_jitter,
with_cloud = False
)
sampler = torch.utils.data.distributed.DistributedSampler(
dataset,
num_replicas = WORLD_SIZE,
rank = RANK,
shuffle = True
)
dataloader = torch.utils.data.DataLoader(
dataset,
batch_size = args.batch_size // WORLD_SIZE,
num_workers = args.num_workers,
collate_fn = collate_fn,
sampler = sampler
)
# policy
if RANK == 0: print("Loading policy ...")
policy = RISE(
num_action = args.num_action,
input_dim = 6,
obs_feature_dim = args.obs_feature_dim,
action_dim = 10,
hidden_dim = args.hidden_dim,
nheads = args.nheads,
num_encoder_layers = args.num_encoder_layers,
num_decoder_layers = args.num_decoder_layers,
dropout = args.dropout
).to(device)
if RANK == 0:
n_parameters = sum(p.numel() for p in policy.parameters() if p.requires_grad)
print("Number of parameters: {:.2f}M".format(n_parameters / 1e6))
policy = nn.parallel.DistributedDataParallel(
policy,
device_ids = [LOCAL_RANK],
output_device = LOCAL_RANK,
find_unused_parameters = True
)
# load checkpoint
if args.resume_ckpt is not None:
policy.module.load_state_dict(torch.load(args.resume_ckpt, map_location = device), strict = False)
if RANK == 0:
print("Checkpoint {} loaded.".format(args.resume_ckpt))
# ckpt path
if RANK == 0 and not os.path.exists(args.ckpt_dir):
os.makedirs(args.ckpt_dir)
# optimizer and lr scheduler
if RANK == 0: print("Loading optimizer and scheduler ...")
optimizer = torch.optim.AdamW(policy.parameters(), lr = args.lr, betas = [0.95, 0.999], weight_decay = 1e-6)
lr_scheduler = get_cosine_schedule_with_warmup(
optimizer = optimizer,
num_warmup_steps = 2000,
num_training_steps = len(dataloader) * args.num_epochs
)
lr_scheduler.last_epoch = len(dataloader) * (args.resume_epoch + 1) - 1
# training
train_history = []
policy.train()
for epoch in range(args.resume_epoch + 1, args.num_epochs):
if RANK == 0: print("Epoch {}".format(epoch))
sampler.set_epoch(epoch)
optimizer.zero_grad()
num_steps = len(dataloader)
pbar = tqdm(dataloader) if RANK == 0 else dataloader
avg_loss = 0
for data in pbar:
# cloud data processing
cloud_coords = data['input_coords_list']
cloud_feats = data['input_feats_list']
action_data = data['action_normalized']
cloud_feats, cloud_coords, action_data = cloud_feats.to(device), cloud_coords.to(device), action_data.to(device)
cloud_data = ME.SparseTensor(cloud_feats, cloud_coords)
# forward
loss = policy(cloud_data, action_data, batch_size = action_data.shape[0])
# backward
loss.backward()
optimizer.step()
optimizer.zero_grad()
lr_scheduler.step()
avg_loss += loss.item()
avg_loss = avg_loss / num_steps
sync_loss(avg_loss, device)
train_history.append(avg_loss)
if RANK == 0:
print("Train loss: {:.6f}".format(avg_loss))
if (epoch + 1) % args.save_epochs == 0:
torch.save(
policy.module.state_dict(),
os.path.join(args.ckpt_dir, "policy_epoch_{}_seed_{}.ckpt".format(epoch + 1, args.seed))
)
plot_history(train_history, epoch, args.ckpt_dir, args.seed)
if RANK == 0:
torch.save(
policy.module.state_dict(),
os.path.join(args.ckpt_dir, "policy_last.ckpt")
)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data_path', action = 'store', type = str, help = 'data path', required = True)
parser.add_argument('--aug', action = 'store_true', help = 'whether to add 3D data augmentation')
parser.add_argument('--aug_jitter', action = 'store_true', help = 'whether to add color jitter augmentation')
parser.add_argument('--num_action', action = 'store', type = int, help = 'number of action steps', required = False, default = 20)
parser.add_argument('--voxel_size', action = 'store', type = float, help = 'voxel size', required = False, default = 0.005)
parser.add_argument('--obs_feature_dim', action = 'store', type = int, help = 'observation feature dimension', required = False, default = 512)
parser.add_argument('--hidden_dim', action = 'store', type = int, help = 'hidden dimension', required = False, default = 512)
parser.add_argument('--nheads', action = 'store', type = int, help = 'number of heads', required = False, default = 8)
parser.add_argument('--num_encoder_layers', action = 'store', type = int, help = 'number of encoder layers', required = False, default = 4)
parser.add_argument('--num_decoder_layers', action = 'store', type = int, help = 'number of decoder layers', required = False, default = 1)
parser.add_argument('--dim_feedforward', action = 'store', type = int, help = 'feedforward dimension', required = False, default = 2048)
parser.add_argument('--dropout', action = 'store', type = float, help = 'dropout ratio', required = False, default = 0.1)
parser.add_argument('--ckpt_dir', action = 'store', type = str, help = 'checkpoint directory', required = True)
parser.add_argument('--resume_ckpt', action = 'store', type = str, help = 'resume checkpoint file', required = False, default = None)
parser.add_argument('--resume_epoch', action = 'store', type = int, help = 'resume from which epoch', required = False, default = -1)
parser.add_argument('--lr', action = 'store', type = float, help = 'learning rate', required = False, default = 3e-4)
parser.add_argument('--batch_size', action = 'store', type = int, help = 'batch size', required = False, default = 240)
parser.add_argument('--num_epochs', action = 'store', type = int, help = 'training epochs', required = False, default = 1000)
parser.add_argument('--save_epochs', action = 'store', type = int, help = 'saving epochs', required = False, default = 50)
parser.add_argument('--num_workers', action = 'store', type = int, help = 'number of workers', required = False, default = 24)
parser.add_argument('--seed', action = 'store', type = int, help = 'seed', required = False, default = 233)
train(vars(parser.parse_args()))