forked from StanfordVL/causal_induction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
F_models.py
218 lines (171 loc) · 6.72 KB
/
F_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
from __future__ import print_function, division, absolute_import
import torch as th
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
import numpy as np
import math
import cv2
class ImageEncoder(nn.Module):
"""
IMage Encoder
"""
def __init__(self, num):
super(ImageEncoder, self).__init__()
self.encoder_conv = nn.Sequential(
# 224x224xN_CHANNELS -> 112x112x64
nn.Conv2d(3, 8, kernel_size=3, stride=1, padding=1),
nn.MaxPool2d(kernel_size=2, stride=2), # 56x56x64
nn.ReLU(inplace=True),
)
self.encoder_conv2 = nn.Sequential(
nn.Conv2d(8, 16, kernel_size=3, stride=1, padding=1),
nn.MaxPool2d(kernel_size=2, stride=2), # 27x27x64
nn.ReLU(inplace=True),
)
self.encoder_conv3 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1),
nn.MaxPool2d(kernel_size=2, stride=2), # 6x6x64
nn.ReLU(inplace=True),
)
self.fc = nn.Linear(4 * 4 * 32, num)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
e1 = self.encoder_conv(x)
e2 = self.encoder_conv2(e1)
e3 = self.encoder_conv3(e2)
e3 = e3.view(e3.size(0), -1)
encoding = self.fc(e3)
return encoding
class IterativeModelAttention(nn.Module):
def __init__(self, horizon,num=5, ms=False, images=False):
super(IterativeModelAttention, self).__init__()
self.horizon = horizon
self.ms = ms
if self.ms:
self.attnsize = num + 1
self.outsize = num
else:
self.attnsize = num
self.outsize = num
self.images = images
self.num = num
self.ie = ImageEncoder(num)
self.fc1 = nn.Linear(2*num+1, 1024)
self.fc2 = nn.Linear(1024, 512)
self.fc3 = nn.Linear(512, self.attnsize + num)
self.fc4 = nn.Linear(self.attnsize*self.outsize, self.attnsize + num)
self.dp = nn.Dropout(0.3)
self.relu = nn.ReLU()
self.softmax = nn.Softmax(dim=1)
self.sigmoid = nn.Sigmoid()
def forward(self, s, a):
if self.images:
s_im = s.view(-1, 32, 32, 3).permute(0,3,1,2)
senc = self.ie(s_im)
sp = senc.view(-1, self.horizon, self.num)
else:
sp = s.view(-1, self.horizon, self.num)
sp[:,:-1] = sp[:,1:] - sp[:,:-1]
a = a.view(-1, self.horizon, self.num+1)
e = th.cat([sp, a], 2)
p = th.zeros((sp.size(0), self.attnsize, self.outsize)).cuda()
for i in range(self.horizon):
inn = e[:,i,:]
e1 = self.relu(self.dp(self.fc1(inn)))
e2 = self.relu(self.dp(self.fc2(e1)))
e3 = self.fc3(e2)
atn = self.softmax(e3[:, :self.attnsize]).unsqueeze(-1)
e3 = self.sigmoid(e3[:, self.attnsize:].unsqueeze(1).repeat(1, self.attnsize, 1))
r = atn * e3
p = p + r
e3 = self.fc4(p.view(-1, self.attnsize*self.num))
atn = self.softmax(e3[:, :self.attnsize]).unsqueeze(-1)
e3 = self.sigmoid(e3[:, self.attnsize:].unsqueeze(1).repeat(1, self.attnsize, 1))
r = atn * e3
p = p + r
p = p.view(-1, self.attnsize*self.num)
return p
class IterativeModel(nn.Module):
def __init__(self, horizon,num=5, ms=False, images=False):
super(IterativeModel, self).__init__()
self.images = images
self.ie = ImageEncoder(num)
self.horizon = horizon
self.ms = ms
if self.ms:
self.outsize = num**2 + num
else:
self.outsize = num**2
self.num = num
self.fc1 = nn.Linear(2*num+1, 1024)
self.fc2 = nn.Linear(1024, 512)
self.fc3 = nn.Linear(512, self.outsize)
self.fc4 = nn.Linear(self.outsize, self.outsize)
self.cnn1 = nn.Conv1d(2*num+1, 256, kernel_size=3, padding=1)
self.cnn2 = nn.Conv1d(256, 128, kernel_size=3, padding=1)
self.cnn3 = nn.Conv1d(128, 128, kernel_size=3, padding=1)
self.dp = nn.Dropout(0.3)
self.relu = nn.ReLU()
self.softmax = nn.Softmax()
self.sigmoid = nn.Sigmoid()
def forward(self, s, a):
if self.images:
s_im = s.view(-1, 32, 32, 3).permute(0,3,1,2)
senc = self.ie(s_im)
sp = senc.view(-1, self.horizon, self.num)
else:
sp = s.view(-1, self.horizon, self.num)
sp[:,1:] = sp[:,1:] - sp[:,:-1]
a = a.view(-1, self.horizon, self.num+1)
e = th.cat([sp, a], 2)
e = e.permute(0,2,1)
c2 = e
p = th.zeros((sp.size(0), self.outsize)).cuda()
for i in range(self.horizon):
e1 = self.relu(self.dp(self.fc1(c2[:,:,i])))
e2 = self.relu(self.dp(self.fc2(e1)))
e3 = self.sigmoid(self.fc3(e2))
p = p + e3
p = self.sigmoid(self.fc4(p))
return p
class SupervisedModelCNN(nn.Module):
def __init__(self, horizon,num=5, ms=False, images=False):
super(SupervisedModelCNN, self).__init__()
self.images = images
self.ie = ImageEncoder(num)
self.horizon = horizon
self.ms = ms
if self.ms:
self.outsize = num**2 + num
else:
self.outsize = num**2
self.num = num
self.cnn1 = nn.Conv1d(2*num+1, 256, kernel_size=3, padding=1)
self.cnn2 = nn.Conv1d(256, 128, kernel_size=3, padding=1)
self.cnn3 = nn.Conv1d(128, 128, kernel_size=3, padding=1)
self.fc1 = nn.Linear(self.horizon*128, 1024)
self.fc2 = nn.Linear(1024, 512)
self.fc3 = nn.Linear(512, self.outsize)
self.dp = nn.Dropout(0.3)
self.relu = nn.ReLU()
self.softmax = nn.Softmax()
self.sigmoid = nn.Sigmoid()
def forward(self, s, a):
if self.images:
s_im = s.view(-1, 32, 32, 3).permute(0,3,1,2)
senc = self.ie(s_im)
sp = senc.view(-1, self.horizon, self.num)
else:
sp = s.view(-1, self.horizon, self.num)
a = a.view(-1, self.horizon, self.num+1)
e = th.cat([sp, a], 2)
e = e.permute(0,2,1)
c1 = self.relu(self.cnn1(e))
c2 = self.relu(self.cnn2(c1))
c2 = self.relu(self.cnn3(c2))
c2 = c2.view(-1, self.horizon*128)
e1 = self.relu(self.dp(self.fc1(c2)))
e2 = self.relu(self.dp(self.fc2(e1)))
rec = self.sigmoid(self.fc3(e2))
return rec