-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpick_train.py
162 lines (124 loc) · 5.97 KB
/
pick_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
import os
from edf.pc_utils import draw_geometry, create_o3d_points
from edf.data import PointCloud, SE3, TargetPoseDemo, DemoSequence, DemoSeqDataset, gzip_save
from edf.preprocess import Rescale, NormalizeColor, Downsample, PointJitter, ColorJitter
from edf.agent import PickAgent
import numpy as np
import yaml
import plotly as pl
import plotly.express as ple
import open3d as o3d
import torch
from torch.utils.data import DataLoader
from torchvision.transforms import Compose
torch.set_printoptions(precision= 3, sci_mode=False, linewidth=120)
agent_config_dir = "config/agent_config/pick_agent.yaml"
train_config_dir = "config/train_config/train_pick.yaml"
agent_param_dir = "checkpoint/mug_10_demo/pick"
with open(train_config_dir) as file:
config = yaml.load(file, Loader=yaml.FullLoader)
device = config['device']
unit_len = config['characteristic_length']
temperature = config['temperature']
max_epochs = config['max_epochs']
N_transform_init = config['N_transform_init']
mh_iter_init = config['mh_iter_init']
langevin_iter_init = config['langevin_iter_init']
langevin_begin_epoch = config['langevin_begin_epoch']
report_freq = config['report_freq']
init_CD_ratio = config['init_CD_ratio']
end_CD_ratio = config['end_CD_ratio']
CD_end_iter = config['CD_end_iter']
lr_se3T = config['lr_se3T']
lr_energy_fast = config['lr_energy_fast']
lr_energy_slow = config['lr_energy_slow']
lr_query_fast = config['lr_query_fast']
lr_query_slow = config['lr_query_slow']
std_theta_perturb = config['std_theta_degree_perturb'] / 180 * np.pi
std_X_perturb = config['std_X_perturb']
edf_norm_std = config['edf_norm_std']
langevin_dt = config['langevin_dt']
load_transforms = Compose([Rescale(rescale_factor=1/unit_len),
])
trainset = DemoSeqDataset(dataset_dir="demo/mug_demo", annotation_file="data.yaml", load_transforms = load_transforms, device=device)
train_dataloader = DataLoader(trainset, shuffle=True, collate_fn=lambda x:x)
scene_voxel_size = 1.7
grasp_voxel_size = 1.4
scene_points_jitter = scene_voxel_size * 0.1
grasp_points_jitter = grasp_voxel_size * 0.1
scene_color_jitter = 0.03
grasp_color_jitter = 0.03
scene_proc_fn = Compose([Downsample(voxel_size=1.7, coord_reduction="average"),
NormalizeColor(color_mean = torch.tensor([0.5, 0.5, 0.5]), color_std = torch.tensor([0.5, 0.5, 0.5])),
PointJitter(jitter_std=scene_points_jitter),
ColorJitter(jitter_std=scene_color_jitter)
])
grasp_proc_fn = Compose([Downsample(voxel_size=1.4, coord_reduction="average"),
NormalizeColor(color_mean = torch.tensor([0.5, 0.5, 0.5]), color_std = torch.tensor([0.5, 0.5, 0.5])),
PointJitter(jitter_std=grasp_points_jitter),
ColorJitter(jitter_std=grasp_color_jitter)
])
pick_agent = PickAgent(config_dir=agent_config_dir, device=device, lr_se3T=lr_se3T, lr_energy_fast = lr_energy_fast,
lr_energy_slow = lr_energy_slow, lr_query_fast = lr_query_fast, lr_query_slow = lr_query_slow,
std_theta_perturb=std_theta_perturb, std_X_perturb=std_X_perturb, langevin_dt=langevin_dt)
###### Train ######
save_checkpoint = True
visualize = False
max_iter = len(trainset) * max_epochs
iter = 0
for epoch in range(1, max_epochs+1):
for train_batch in train_dataloader:
iter += 1
assert len(train_batch) == 1, "Batch training is not supported yet."
data = train_batch[0]
demo_seq: DemoSequence = data.to(device)
pick_demo: TargetPoseDemo = demo_seq[0]
scene_raw: PointCloud = pick_demo.scene_pc
grasp_raw: PointCloud = pick_demo.grasp_pc
target_poses: SE3 = pick_demo.target_poses
scene_proc = scene_proc_fn(scene_raw)
grasp_proc = grasp_proc_fn(grasp_raw)
################################################# Train #########################################################
N_transforms = N_transform_init
mh_iter = mh_iter_init
if epoch >= langevin_begin_epoch:
langevin_iter = int( langevin_iter_init * (1+ iter / max_iter) )
else:
langevin_iter = 0
if iter % report_freq == 0 or iter == 1:
pbar = True
verbose = True
if visualize:
raise NotImplementedError
else:
pass
else:
pbar = False
verbose = False
visual_info = None
if iter > CD_end_iter:
CD_ratio = end_CD_ratio
else:
p_CD = 1 - (iter-1)/CD_end_iter
CD_ratio = p_CD*init_CD_ratio + (1-p_CD)*end_CD_ratio
if iter == int(max_iter * 0.9):
print("Lower lr rate")
pick_agent.rescale_lr(factor=0.2)
if verbose:
print(f"=========Iter {iter}=========", flush=True)
train_logs = pick_agent.train_once(scene=scene_proc, target_T = target_poses, N_transforms = N_transforms,
mh_iter = mh_iter, langevin_iter = langevin_iter, temperature = temperature,
pbar = pbar, verbose = verbose, grasp=None,
CD_ratio = CD_ratio, edf_norm_std=edf_norm_std)
train_logs['scene_raw'] = scene_raw.to('cpu')
train_logs['grasp_raw'] = grasp_raw.to('cpu')
train_logs['scene_proc'] = scene_proc.to('cpu')
train_logs['grasp_proc'] = grasp_proc.to('cpu')
if iter % report_freq == 0 or iter == 1:
if save_checkpoint:
filename = f'model_iter_{iter}.pt'
pick_agent.save(agent_param_dir, filename)
log_filename = f'trainlog_iter_{iter}.gzip'
gzip_save(train_logs, path=os.path.join(agent_param_dir, log_filename))
if verbose:
print("===============================", flush=True)