-
Notifications
You must be signed in to change notification settings - Fork 141
/
model.py
executable file
·452 lines (352 loc) · 16.6 KB
/
model.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import torch
import torch.nn as nn
from torch.nn import functional as F
import torchvision.models as models
import numpy as np
from utils.distributions import Categorical, DiagGaussian
from utils.model import get_grid, ChannelPool, Flatten, NNBase
# Global Policy model code
class Global_Policy(NNBase):
def __init__(self, input_shape, recurrent=False, hidden_size=512,
downscaling=1):
super(Global_Policy, self).__init__(recurrent, hidden_size,
hidden_size)
out_size = int(input_shape[1] / 16. * input_shape[2] / 16.)
self.main = nn.Sequential(
nn.MaxPool2d(2),
nn.Conv2d(8, 32, 3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(64, 128, 3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(128, 64, 3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(64, 32, 3, stride=1, padding=1),
nn.ReLU(),
Flatten()
)
self.linear1 = nn.Linear(out_size * 32 + 8, hidden_size)
self.linear2 = nn.Linear(hidden_size, 256)
self.critic_linear = nn.Linear(256, 1)
self.orientation_emb = nn.Embedding(72, 8)
self.train()
def forward(self, inputs, rnn_hxs, masks, extras):
x = self.main(inputs)
orientation_emb = self.orientation_emb(extras).squeeze(1)
x = torch.cat((x, orientation_emb), 1)
x = nn.ReLU()(self.linear1(x))
if self.is_recurrent:
x, rnn_hxs = self._forward_gru(x, rnn_hxs, masks)
x = nn.ReLU()(self.linear2(x))
return self.critic_linear(x).squeeze(-1), x, rnn_hxs
# Neural SLAM Module code
class Neural_SLAM_Module(nn.Module):
"""
"""
def __init__(self, args):
super(Neural_SLAM_Module, self).__init__()
self.device = args.device
self.screen_h = args.frame_height
self.screen_w = args.frame_width
self.resolution = args.map_resolution
self.map_size_cm = args.map_size_cm // args.global_downscaling
self.n_channels = 3
self.vision_range = args.vision_range
self.dropout = 0.5
self.use_pe = args.use_pose_estimation
# Visual Encoding
resnet = models.resnet18(pretrained=args.pretrained_resnet)
self.resnet_l5 = nn.Sequential(*list(resnet.children())[0:8])
self.conv = nn.Sequential(*filter(bool, [
nn.Conv2d(512, 64, (1, 1), stride=(1, 1)),
nn.ReLU()
]))
# convolution output size
input_test = torch.randn(1,
self.n_channels,
self.screen_h,
self.screen_w)
conv_output = self.conv(self.resnet_l5(input_test))
self.pool = ChannelPool(1)
self.conv_output_size = conv_output.view(-1).size(0)
# projection layer
self.proj1 = nn.Linear(self.conv_output_size, 1024)
self.proj2 = nn.Linear(1024, 4096)
if self.dropout > 0:
self.dropout1 = nn.Dropout(self.dropout)
self.dropout2 = nn.Dropout(self.dropout)
# Deconv layers to predict map
self.deconv = nn.Sequential(*filter(bool, [
nn.ConvTranspose2d(64, 32, (4, 4), stride=(2, 2), padding=(1, 1)),
nn.ReLU(),
nn.ConvTranspose2d(32, 16, (4, 4), stride=(2, 2), padding=(1, 1)),
nn.ReLU(),
nn.ConvTranspose2d(16, 2, (4, 4), stride=(2, 2), padding=(1, 1)),
]))
# Pose Estimator
self.pose_conv = nn.Sequential(*filter(bool, [
nn.Conv2d(4, 64, (4, 4), stride=(2, 2)),
nn.ReLU(),
nn.Conv2d(64, 32, (4, 4), stride=(2, 2)),
nn.ReLU(),
nn.Conv2d(32, 16, (3, 3), stride=(1, 1)),
nn.ReLU()
]))
pose_conv_output = self.pose_conv(torch.randn(1, 4,
self.vision_range,
self.vision_range))
self.pose_conv_output_size = pose_conv_output.view(-1).size(0)
# projection layer
self.pose_proj1 = nn.Linear(self.pose_conv_output_size, 1024)
self.pose_proj2_x = nn.Linear(1024, 128)
self.pose_proj2_y = nn.Linear(1024, 128)
self.pose_proj2_o = nn.Linear(1024, 128)
self.pose_proj3_x = nn.Linear(128, 1)
self.pose_proj3_y = nn.Linear(128, 1)
self.pose_proj3_o = nn.Linear(128, 1)
if self.dropout > 0:
self.pose_dropout1 = nn.Dropout(self.dropout)
self.st_poses_eval = torch.zeros(args.num_processes,
3).to(self.device)
self.st_poses_train = torch.zeros(args.slam_batch_size,
3).to(self.device)
grid_size = self.vision_range * 2
self.grid_map_eval = torch.zeros(args.num_processes, 2,
grid_size, grid_size
).float().to(self.device)
self.grid_map_train = torch.zeros(args.slam_batch_size, 2,
grid_size, grid_size
).float().to(self.device)
self.agent_view = torch.zeros(args.num_processes, 2,
self.map_size_cm // self.resolution,
self.map_size_cm // self.resolution
).float().to(self.device)
def forward(self, obs_last, obs, poses, maps, explored, current_poses,
build_maps=True):
# Get egocentric map prediction for the current obs
bs, c, h, w = obs.size()
resnet_output = self.resnet_l5(obs[:, :3, :, :])
conv_output = self.conv(resnet_output)
proj1 = nn.ReLU()(self.proj1(
conv_output.view(-1, self.conv_output_size)))
if self.dropout > 0:
proj1 = self.dropout1(proj1)
proj3 = nn.ReLU()(self.proj2(proj1))
deconv_input = proj3.view(bs, 64, 8, 8)
deconv_output = self.deconv(deconv_input)
pred = torch.sigmoid(deconv_output)
proj_pred = pred[:, :1, :, :]
fp_exp_pred = pred[:, 1:, :, :]
with torch.no_grad():
# Get egocentric map prediction for the last obs
bs, c, h, w = obs_last.size()
resnet_output = self.resnet_l5(obs_last[:, :3, :, :])
conv_output = self.conv(resnet_output)
proj1 = nn.ReLU()(self.proj1(
conv_output.view(-1, self.conv_output_size)))
if self.dropout > 0:
proj1 = self.dropout1(proj1)
proj3 = nn.ReLU()(self.proj2(proj1))
deconv_input = proj3.view(bs, 64, 8, 8)
deconv_output = self.deconv(deconv_input)
pred_last = torch.sigmoid(deconv_output)
# ST of proj
vr = self.vision_range
grid_size = vr * 2
if build_maps:
st_poses = self.st_poses_eval.detach_()
grid_map = self.grid_map_eval.detach_()
else:
st_poses = self.st_poses_train.detach_()
grid_map = self.grid_map_train.detach_()
st_poses.fill_(0.)
st_poses[:, 0] = poses[:, 1] * 200. / self.resolution / grid_size
st_poses[:, 1] = poses[:, 0] * 200. / self.resolution / grid_size
st_poses[:, 2] = poses[:, 2] * 57.29577951308232
rot_mat, trans_mat = get_grid(st_poses,
(bs, 2, grid_size, grid_size),
self.device)
grid_map.fill_(0.)
grid_map[:, :, vr:, int(vr / 2):int(vr / 2 + vr)] = pred_last
translated = F.grid_sample(grid_map, trans_mat)
rotated = F.grid_sample(translated, rot_mat)
rotated = rotated[:, :, vr:, int(vr / 2):int(vr / 2 + vr)]
pred_last_st = rotated
# Pose estimator
pose_est_input = torch.cat((pred.detach(), pred_last_st.detach()),
dim=1)
pose_conv_output = self.pose_conv(pose_est_input)
pose_conv_output = pose_conv_output.view(-1,
self.pose_conv_output_size)
proj1 = nn.ReLU()(self.pose_proj1(pose_conv_output))
if self.dropout > 0:
proj1 = self.pose_dropout1(proj1)
proj2_x = nn.ReLU()(self.pose_proj2_x(proj1))
pred_dx = self.pose_proj3_x(proj2_x)
proj2_y = nn.ReLU()(self.pose_proj2_y(proj1))
pred_dy = self.pose_proj3_y(proj2_y)
proj2_o = nn.ReLU()(self.pose_proj2_o(proj1))
pred_do = self.pose_proj3_o(proj2_o)
pose_pred = torch.cat((pred_dx, pred_dy, pred_do), dim=1)
if self.use_pe == 0:
pose_pred = pose_pred * self.use_pe
if build_maps:
# Aggregate egocentric map prediction in the geocentric map
# using the predicted pose
with torch.no_grad():
agent_view = self.agent_view.detach_()
agent_view.fill_(0.)
x1 = self.map_size_cm // (self.resolution * 2) \
- self.vision_range // 2
x2 = x1 + self.vision_range
y1 = self.map_size_cm // (self.resolution * 2)
y2 = y1 + self.vision_range
agent_view[:, :, y1:y2, x1:x2] = pred
corrected_pose = poses + pose_pred
def get_new_pose_batch(pose, rel_pose_change):
pose[:, 1] += rel_pose_change[:, 0] * \
torch.sin(pose[:, 2] / 57.29577951308232) \
+ rel_pose_change[:, 1] * \
torch.cos(pose[:, 2] / 57.29577951308232)
pose[:, 0] += rel_pose_change[:, 0] * \
torch.cos(pose[:, 2] / 57.29577951308232) \
- rel_pose_change[:, 1] * \
torch.sin(pose[:, 2] / 57.29577951308232)
pose[:, 2] += rel_pose_change[:, 2] * 57.29577951308232
pose[:, 2] = torch.fmod(pose[:, 2] - 180.0, 360.0) + 180.0
pose[:, 2] = torch.fmod(pose[:, 2] + 180.0, 360.0) - 180.0
return pose
current_poses = get_new_pose_batch(current_poses,
corrected_pose)
st_pose = current_poses.clone().detach()
st_pose[:, :2] = - (st_pose[:, :2] * 100.0 / self.resolution
- self.map_size_cm \
// (self.resolution * 2)) \
/ (self.map_size_cm // (self.resolution * 2))
st_pose[:, 2] = 90. - (st_pose[:, 2])
rot_mat, trans_mat = get_grid(st_pose, agent_view.size(),
self.device)
rotated = F.grid_sample(agent_view, rot_mat)
translated = F.grid_sample(rotated, trans_mat)
maps2 = torch.cat((maps.unsqueeze(1),
translated[:, :1, :, :]), 1)
explored2 = torch.cat((explored.unsqueeze(1),
translated[:, 1:, :, :]), 1)
map_pred = self.pool(maps2).squeeze(1)
exp_pred = self.pool(explored2).squeeze(1)
else:
map_pred = None
exp_pred = None
current_poses = None
return proj_pred, fp_exp_pred, map_pred, exp_pred,\
pose_pred, current_poses
# Local Policy model code
class Local_IL_Policy(NNBase):
def __init__(self, input_shape, num_actions, recurrent=False,
hidden_size=512, deterministic=False):
super(Local_IL_Policy, self).__init__(recurrent, hidden_size,
hidden_size)
self.deterministic = deterministic
self.dropout = 0.5
resnet = models.resnet18(pretrained=True)
self.resnet_l5 = nn.Sequential(*list(resnet.children())[0:8])
# Extra convolution layer
self.conv = nn.Sequential(*filter(bool, [
nn.Conv2d(512, 64, (1, 1), stride=(1, 1)),
nn.ReLU()
]))
# convolution output size
input_test = torch.randn(1, 3, input_shape[1], input_shape[2])
conv_output = self.conv(self.resnet_l5(input_test))
self.conv_output_size = conv_output.view(-1).size(0)
# projection layers
self.proj1 = nn.Linear(self.conv_output_size, hidden_size - 16)
if self.dropout > 0:
self.dropout1 = nn.Dropout(self.dropout)
self.linear = nn.Linear(hidden_size, hidden_size)
# Short-term goal embedding layers
self.embedding_angle = nn.Embedding(72, 8)
self.embedding_dist = nn.Embedding(24, 8)
# Policy linear layer
self.policy_linear = nn.Linear(hidden_size, num_actions)
self.train()
def forward(self, rgb, rnn_hxs, masks, extras):
if self.deterministic:
x = torch.zeros(extras.size(0), 3)
for i, stg in enumerate(extras):
if stg[0] < 3 or stg[0] > 68:
x[i] = torch.tensor([0.0, 0.0, 1.0])
elif stg[0] < 36:
x[i] = torch.tensor([0.0, 1.0, 0.0])
else:
x[i] = torch.tensor([1.0, 0.0, 0.0])
else:
resnet_output = self.resnet_l5(rgb[:, :3, :, :])
conv_output = self.conv(resnet_output)
proj1 = nn.ReLU()(self.proj1(conv_output.view(
-1, self.conv_output_size)))
if self.dropout > 0:
proj1 = self.dropout1(proj1)
angle_emb = self.embedding_angle(extras[:, 0]).view(-1, 8)
dist_emb = self.embedding_dist(extras[:, 1]).view(-1, 8)
x = torch.cat((proj1, angle_emb, dist_emb), 1)
x = nn.ReLU()(self.linear(x))
if self.is_recurrent:
x, rnn_hxs = self._forward_gru(x, rnn_hxs, masks)
x = nn.Softmax(dim=1)(self.policy_linear(x))
action = torch.argmax(x, dim=1)
return action, x, rnn_hxs
# https://github.com/ikostrikov/pytorch-a2c-ppo-acktr-gail/blob/master/a2c_ppo_acktr/model.py#L15
class RL_Policy(nn.Module):
def __init__(self, obs_shape, action_space, model_type=0,
base_kwargs=None):
super(RL_Policy, self).__init__()
if base_kwargs is None:
base_kwargs = {}
if model_type == 0:
self.network = Global_Policy(obs_shape, **base_kwargs)
else:
raise NotImplementedError
if action_space.__class__.__name__ == "Discrete":
num_outputs = action_space.n
self.dist = Categorical(self.network.output_size, num_outputs)
elif action_space.__class__.__name__ == "Box":
num_outputs = action_space.shape[0]
self.dist = DiagGaussian(self.network.output_size, num_outputs)
else:
raise NotImplementedError
self.model_type = model_type
@property
def is_recurrent(self):
return self.network.is_recurrent
@property
def rec_state_size(self):
"""Size of rnn_hx."""
return self.network.rec_state_size
def forward(self, inputs, rnn_hxs, masks, extras):
if extras is None:
return self.network(inputs, rnn_hxs, masks)
else:
return self.network(inputs, rnn_hxs, masks, extras)
def act(self, inputs, rnn_hxs, masks, extras=None, deterministic=False):
value, actor_features, rnn_hxs = self(inputs, rnn_hxs, masks, extras)
dist = self.dist(actor_features)
if deterministic:
action = dist.mode()
else:
action = dist.sample()
action_log_probs = dist.log_probs(action)
return value, action, action_log_probs, rnn_hxs
def get_value(self, inputs, rnn_hxs, masks, extras=None):
value, _, _ = self(inputs, rnn_hxs, masks, extras)
return value
def evaluate_actions(self, inputs, rnn_hxs, masks, action, extras=None):
value, actor_features, rnn_hxs = self(inputs, rnn_hxs, masks, extras)
dist = self.dist(actor_features)
action_log_probs = dist.log_probs(action)
dist_entropy = dist.entropy().mean()
return value, action_log_probs, dist_entropy, rnn_hxs