-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
388 lines (318 loc) · 13.2 KB
/
models.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision.transforms import transforms
# No need for my proj
# from r3m import load_r3m
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Utilities for defining neural nets
def weight_init(m):
"""Custom weight init for Conv2D and Linear layers."""
if isinstance(m, nn.Linear):
nn.init.orthogonal_(m.weight.data)
if hasattr(m.bias, "data"):
m.bias.data.fill_(0.0)
class MLP(nn.Module):
def __init__(
self, input_dim, hidden_dim, output_dim, hidden_depth, output_mod=None
):
super().__init__()
self.trunk = mlp(input_dim, hidden_dim, output_dim, hidden_depth, output_mod)
self.apply(weight_init)
def forward(self, x):
return self.trunk(x)
def mlp(input_dim: int, hidden_dim, output_dim, hidden_depth, output_mod=None, do_regularization=False):
if hidden_depth == 0:
mods = [nn.Linear(input_dim, output_dim)]
else:
if do_regularization:
mods = [nn.Linear(input_dim, hidden_dim), nn.BatchNorm1d(hidden_dim), nn.ReLU(inplace=True)]
else:
mods = [nn.Linear(input_dim, hidden_dim), nn.ReLU(inplace=True)]
for i in range(hidden_depth - 1):
if do_regularization:
mods += [nn.Linear(hidden_dim, hidden_dim), nn.BatchNorm1d(hidden_dim), nn.ReLU(inplace=True)]
else:
mods += [nn.Linear(hidden_dim, hidden_dim), nn.ReLU(inplace=True)]
mods.append(nn.Linear(hidden_dim, output_dim))
if output_mod is not None:
mods.append(output_mod)
trunk = nn.Sequential(*mods)
return trunk
# Define the forward model
class Policy(nn.Module):
def __init__(self, obs_dim, action_dim, hidden_dim, hidden_depth, output_mod=None, do_regularization=False):
super().__init__()
self.trunk = mlp(obs_dim, hidden_dim, action_dim, hidden_depth, output_mod=output_mod, do_regularization=do_regularization)
def forward(self, obs):
next_pred = self.trunk(obs)
return next_pred
class Critic(nn.Module):
def __init__(self, obs_dim, action_dim, hidden_dim, hidden_depth, output_mod=None, do_regularization=False):
super().__init__()
# critic should just output a single value!
self.trunk = mlp(obs_dim + action_dim, hidden_dim, 1, hidden_depth, output_mod=output_mod, do_regularization=do_regularization)
def forward(self, state, action):
x = torch.cat([state, action], dim=1)
x = self.trunk(x)
return x
class ConvNet(nn.Module):
def __init__(self, h, w, hidden_fc=128, outputs=2, in_channels=3):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(in_channels, 16, kernel_size=3, stride=1)
self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1)
self.conv3 = nn.Conv2d(32, 32, kernel_size=3, stride=1)
def conv2d_size_out(size, kernel_size = 3, stride = 1):
return (size - (kernel_size - 1) - 1) // stride + 1
convw = conv2d_size_out(conv2d_size_out(conv2d_size_out(w)))
convh = conv2d_size_out(conv2d_size_out(conv2d_size_out(h)))
linear_input_size = convw * convh * 32
self.head1 = nn.Linear(linear_input_size, hidden_fc)
self.head2 = nn.Linear(hidden_fc, outputs)
self.h = h
self.w = w
self.hidden_fc = hidden_fc
self.outputs = outputs
def forward(self, x):
x = x.to(device)
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
# import ipdb; ipdb.set_trace()
x = x.reshape(x.size(0), -1)
x = F.relu(self.head1(x))
x = self.head2(x)
return x
class Net(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
# self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(92416, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 32)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = torch.flatten(x, 1) # flatten all dimensions except batch
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
# net = Net()
# Define the forward model
class ConvPolicy(nn.Module):
def __init__(self, output_dim=2, coordconv=False):
super().__init__()
self.obs_trunk = ConvNet(h=84, w=84, hidden_fc=64, outputs=32)
self.after_trunk = mlp(32 + 3, 128, output_dim, 1, output_mod=None)
def forward(self, obs, goal):
img_embedding = self.obs_trunk(obs)
img_embedding = torch.cat([img_embedding, goal], dim=-1)
next_pred = self.after_trunk(img_embedding)
return next_pred
def get_resnet_preprocess_transforms():
return transforms.Compose([
# transforms.ToTensor(),
# transforms.Resize(224),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
# transforms.Resize(256),
# transforms.CenterCrop(224),
])
def get_unnormalize_transform():
return transforms.Compose([
transforms.Normalize(mean=[0.0, 0.0, 0.0], std=[1/0.229, 1/0.224, 1/0.225]),
transforms.Normalize(mean=[-0.485, -0.456, -0.406], std=[1.0, 1.0, 1.0]),
])
class ResnetPolicy(nn.Module):
def __init__(
self,
output_size: int = 1,
pretrained: bool = True,
freeze_backbone: bool = True,
do_preprocess: bool = True,
):
super(ResnetPolicy, self).__init__()
# get the backbone
self.resnet = torchvision.models.resnet18(pretrained=pretrained)
fc_in_size = self.resnet.fc.in_features # 512
self.resnet = torch.nn.Sequential(*(list(self.resnet.children())[:-1]))
if freeze_backbone:
assert pretrained
for param in self.resnet.parameters():
param.requires_grad = False
# add our head to it
self.output_size = output_size
self.fc_head = nn.Sequential(
nn.Linear(fc_in_size + 3, 256), # + 3 because of the goal
nn.PReLU(),
nn.Linear(256, output_size)
)
self.do_preprocess = do_preprocess
self.preprocess_transforms = get_resnet_preprocess_transforms()
def forward(self, x: torch.Tensor, goal: torch.Tensor):
if self.do_preprocess:
x = self.preprocess_transforms(x)
x = self.resnet(x).squeeze(dim=2).squeeze(dim=2)
x = torch.cat([x, goal], dim=1)
x = self.fc_head(x)
return x
def save_model(self, model_path: str):
print(f"saved frame classification model to {model_path}")
torch.save(self.state_dict(), model_path)
def load_model(self, model_path: str):
print(f"loaded frame classification model from {model_path}")
self.load_state_dict(torch.load(model_path))
# class R3MFeatureExtractor(nn.Module):
# def __init__(
# self,
# do_multiply_255: bool = True,
# freeze_backbone: bool = True,
# ):
# super(R3MFeatureExtractor, self).__init__()
# # get the backbone
# self.r3m = load_r3m("resnet18") # resnet18, resnet34, resnet50
# self.r3m.to(device)
# self.freeze_r3m = freeze_backbone
# if self.freeze_r3m:
# self.r3m.eval()
# for param in self.r3m.parameters():
# param.requires_grad = False
# self.r3m_embedding_dim = 512
# self.do_multiply_255 = do_multiply_255
# def forward(self, x: torch.Tensor):
# # r3m expects things to be [0-255] instead of [0-1]!!!
# if self.do_multiply_255:
# x = x * 255.0
# # some computational savings
# if self.freeze_r3m:
# with torch.no_grad():
# x = self.r3m(x)
# else:
# x = self.r3m(x)
# return x
# class R3MPolicy(nn.Module):
# def __init__(
# self,
# output_size: int = 1,
# do_multiply_255: bool = True,
# freeze_backbone: bool = False,
# film_layer_goal: bool = False,
# state_only: bool = False
# ):
# super(R3MPolicy, self).__init__()
# self.state_only = state_only
# # get the backbone
# self.r3m = load_r3m("resnet18") # resnet18, resnet34, resnet50
# self.r3m.to(device)
# self.freeze_r3m = freeze_backbone
# if self.freeze_r3m:
# self.r3m.eval()
# for param in self.r3m.parameters():
# param.requires_grad = False
# self.r3m_embedding_dim = 512 # for resnet 18 - 512, for resnet50 - 2048
# # film layer the goal
# self.film_layer_goal = film_layer_goal
# self.goal_dim = 3
# if self.film_layer_goal:
# self.film_layer = nn.Sequential(
# nn.Linear(self.goal_dim, 4 * self.r3m_embedding_dim),
# nn.LeakyReLU(),
# nn.Linear(4 * self.r3m_embedding_dim, 2 * self.r3m_embedding_dim)
# )
# # add our head to the r3m output
# if self.film_layer_goal:
# fc_head_in = self.r3m_embedding_dim
# elif self.state_only:
# fc_head_in = self.r3m_embedding_dim
# else:
# fc_head_in = self.r3m_embedding_dim + 3
# self.output_size = output_size
# self.fc_head = nn.Sequential(
# nn.Linear(fc_head_in, 256),
# nn.LeakyReLU(),
# nn.Linear(256, output_size)
# )
# self.do_multiply_255 = do_multiply_255
# def forward(self, x: torch.Tensor, goal: torch.Tensor = None):
# # r3m expects things to be [0-255] instead of [0-1]!!!
# if self.do_multiply_255:
# x = x * 255.0
# # some computational savings
# if self.freeze_r3m:
# with torch.no_grad():
# x = self.r3m(x)
# else:
# x = self.r3m(x)
# if not self.state_only:
# # film layer
# if self.film_layer_goal:
# gammabeta = self.film_layer(goal)
# gamma, beta = torch.split(gammabeta, self.r3m_embedding_dim, dim=1)
# x = x * gamma + beta
# else:
# # mix and run through head
# x = torch.cat([x, goal], dim=1)
# x = self.fc_head(x)
# return x
# def save_model(self, model_path: str):
# print(f"saved frame classification model to {model_path}")
# torch.save(self.state_dict(), model_path)
# def load_model(self, model_path: str):
# print(f"loaded frame classification model from {model_path}")
# self.load_state_dict(torch.load(model_path))
# class R3MImageGoalPolicy(nn.Module):
# def __init__(
# self,
# output_size: int = 1,
# do_multiply_255: bool = True,
# freeze_backbone: bool = False,
# film_layer_goal: bool = False, # kitchen goals are all the same, might make more sense with realworld videos
# ):
# super(R3MImageGoalPolicy, self).__init__()
# # get the backbone
# self.r3m = load_r3m("resnet18") # resnet18, resnet34, resnet50
# self.r3m.to(device)
# self.freeze_r3m = freeze_backbone
# if self.freeze_r3m:
# self.r3m.eval()
# for param in self.r3m.parameters():
# param.requires_grad = False
# self.r3m_embedding_dim = 512 # for resnet 18 - 512, for resnet50 - 2048
# # add our head to the r3m output
# fc_head_in = 2 * self.r3m_embedding_dim
# self.output_size = output_size
# self.fc_head = nn.Sequential(
# nn.Linear(fc_head_in, 256),
# nn.LeakyReLU(),
# nn.Linear(256, output_size)
# )
# self.do_multiply_255 = do_multiply_255
# def forward(self, x: torch.Tensor, goal: torch.Tensor):
# # r3m expects things to be [0-255] instead of [0-1]!!!
# if self.do_multiply_255:
# x = x * 255.0
# goal = goal * 255.0
# # some computational savings
# if self.freeze_r3m:
# with torch.no_grad():
# x = self.r3m(x)
# goal = self.r3m(goal)
# else:
# x = self.r3m(x)
# goal = self.r3m(goal)
# # film layer
# x = torch.cat([x, goal], dim=1)
# x = self.fc_head(x)
# return x
# def save_model(self, model_path: str):
# print(f"saved frame classification model to {model_path}")
# torch.save(self.state_dict(), model_path)
# def load_model(self, model_path: str):
# print(f"loaded frame classification model from {model_path}")
# self.load_state_dict(torch.load(model_path))
if __name__ == "__main__":
test = R3MPolicy()
import pdb; pdb.set_trace()