-
Notifications
You must be signed in to change notification settings - Fork 18
/
train_smoother.py
229 lines (165 loc) · 7.24 KB
/
train_smoother.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
import torch
import numpy as np
from config import set_random_seed
from tqdm import tqdm as tqdm
from tensorboardX import SummaryWriter
from eval_gnn import explore
from smoother import joint_smoother_ratio
from torch_geometric.utils import add_self_loops
from str2name import str2name
from copy import deepcopy
class DotDict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
def obs_data(config_size, obstacles, free, collided):
if not len(free):
free.append([0. for _ in range(config_size)])
if not len(collided):
collided.append([0. for _ in range(config_size)])
data = DotDict({
'free': free[:500],
'collided': collided[:500],
'obstacles': obstacles,
})
return data
def train(env, replay, model, optimizer, batch_idx=None):
if len(replay) <= 8:
return 0.
optimizer.zero_grad()
loss = 0.
if batch_idx is None:
batch_idx = np.random.choice(len(replay), size=8, replace=False)
for idx in batch_idx:
env_id, path_origin, path_smooth, obstacles, free, collided = replay[idx]
data = obs_data(model.config_size, obstacles, free, collided)
data = DotDict({k: torch.FloatTensor(v).to(device) for k, v in data.items()})
data.path = torch.FloatTensor(path_origin).to(device)
data.edge_index = torch.cat((torch.arange(1, len(path_origin)).reshape(1, -1),
torch.arange(0, len(path_origin)-1).reshape(1, -1)), dim=0)
data.edge_index = torch.cat((data.edge_index, data.edge_index.flip(0)), dim=-1)
data.edge_index, _ = add_self_loops(data.edge_index, num_nodes=len(data.path))
data.edge_index = data.edge_index.to(device)
path_pred = model(**data, loop=np.random.randint(1, 10))
loss += torch.nn.MSELoss()(torch.FloatTensor(path_smooth).to(device)[1:-1], path_pred[1:-1])
optimizer.zero_grad()
(loss/len(batch_idx)).backward()
optimizer.step()
return loss
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def train_smoother(epoch, model_explore, model, model_path, env, data_iter=3):
_, model_explore, model_explore_path, _, _ = str2name(str=env.__str__())
model_explore.load_state_dict(torch.load(model_explore_path, map_location=torch.device("cpu")))
model_explore.to(device)
writer = SummaryWriter()
INFINITY = float('inf')
# env = KukaEnv(kuka_file="kuka_iiwa/model_3.urdf", map_file="maze_files/kukas_13_3000.pkl")
set_random_seed(1234)
train_iter=20
replay = []
model.train()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3, momentum=0.9, weight_decay=1e-4)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=0)
optimizer.zero_grad()
for iter_i in range(data_iter):
indexes = np.random.permutation(epoch)
pbar = tqdm(indexes)
for index in pbar:
env.init_new_problem(index)
if iter_i != 0:
env.set_random_init_goal()
try:
path, free, collided = explore(env, model_explore, model, smooth=False)
if len(path) > 2:
path_smooth = joint_smoother_ratio([tuple(node) for node in path], env, iter=5)
replay.append((index, path, path_smooth, deepcopy(env.obstacles), free, collided))
except Exception:
continue
# torch.save(replay, 'data/pkl/smooth_14.p')
for iter_i in range(train_iter):
indexes = np.random.permutation(len(replay))
pbar = tqdm(np.arange(len(replay)))
losses = []
for index in pbar:
if index % 8 != 0:
continue
try:
loss = train(env, replay, model, optimizer, batch_idx=indexes[index:(index+8)])
except:
print(indexes[index:(index+8)])
losses.append(float(loss.detach().cpu()))
pbar.set_description("loss: %.5f" % np.mean(losses))
writer.add_scalar('loss', loss)
torch.save(model.state_dict(), model_path)
scheduler.step(np.mean(losses))
torch.save(model.state_dict(), model_path)
writer.close()
return
def train_env(str_):
import os
os.environ['CUDA_LAUNCH_BLOCKING']='1'
from train_explorer import train_explorer
from importlib import reload
import torch
from environment import MazeEnv, KukaEnv, SnakeEnv, UR5Env, Kuka2Env
from str2name import str2name
from copy import deepcopy
epoch = 2000
env, model_explore, model_explore_path, model_smooth, model_smooth_path = str2name(str=str_)
model_explore.load_state_dict(torch.load(model_explore_path, map_location=torch.device("cpu")))
model_explore.to(device)
model_smooth_path = model_smooth_path.replace('.pt', 'v3.pt')
model = model_smooth
model_path = model_smooth_path
# model.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
model.to(device)
writer = SummaryWriter()
INFINITY = float('inf')
set_random_seed(1234)
replay = []
for iter_i in range(3):
indexes = np.random.permutation(epoch)
pbar = tqdm(indexes)
for index in pbar:
env.init_new_problem(index)
if iter_i != 0:
env.set_random_init_goal()
try:
path, free, collided = explore(env, model_explore, model, smooth=False)
if len(path) > 2:
path_smooth = joint_smoother_ratio([tuple(node) for node in path], env, iter=5)
replay.append((index, path, path_smooth, deepcopy(env.obstacles), free, collided))
except Exception as e:
continue
import pickle
pickle.dump([(r[0], r[1], r[2]) for r in replay], open("data/oracle_{0:s}.p".format(str_), "wb"))
from model_smoother import ModelSmoother
model = ModelSmoother(workspace_size=env.dim, config_size=env.config_dim, embed_size=128, obs_size=6, scale=np.max(env.bound)).to(device)
_ = model.to(device)
model.train()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3, momentum=0.9, weight_decay=1e-4)
# optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=0)
optimizer.zero_grad()
train_iter=20
loss_min = float('inf')
for iter_i in range(train_iter):
indexes = np.random.permutation(len(replay))
pbar = tqdm(np.arange(len(replay)))
losses = []
for index in pbar:
if index % 8 != 0:
continue
loss = train(env, replay, model, optimizer, batch_idx=indexes[index:(index+8)])
losses.append(float(loss))
pbar.set_description("loss: %.5f" % np.mean(losses))
writer.add_scalar('loss', loss)
scheduler.step(np.mean(losses))
if np.mean(losses) < loss_min:
loss_min = np.mean(losses)
torch.save(model.state_dict(), model_path)
writer.close()
if __name__ == '__main__':
for str_ in ['snake7', 'kuka13']:
train_env(str_)