-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
258 lines (191 loc) · 7.82 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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
from torch.nn import Parameter
class ConvInputModel(nn.Module):
def __init__(self):
super(ConvInputModel, self).__init__()
self.conv1 = nn.Conv2d(3, 24, 3, stride=2, padding=1)
self.batchNorm1 = nn.BatchNorm2d(24)
self.conv2 = nn.Conv2d(24, 24, 3, stride=2, padding=1)
self.batchNorm2 = nn.BatchNorm2d(24)
self.conv3 = nn.Conv2d(24, 24, 3, stride=2, padding=1)
self.batchNorm3 = nn.BatchNorm2d(24)
self.conv4 = nn.Conv2d(24, 24, 3, stride=2, padding=1)
self.batchNorm4 = nn.BatchNorm2d(24)
# This is now 24 channels in a 5x5 grid
def forward(self, img):
"""convolution"""
x = self.conv1(img)
x = F.relu(x)
x = self.batchNorm1(x)
x = self.conv2(x)
x = F.relu(x)
x = self.batchNorm2(x)
x = self.conv3(x)
x = F.relu(x)
x = self.batchNorm3(x)
x = self.conv4(x)
x = F.relu(x)
x = self.batchNorm4(x)
return x
class FCOutputModel(nn.Module):
def __init__(self):
super(FCOutputModel, self).__init__()
self.fc2 = nn.Linear(256, 256)
self.fc3 = nn.Linear(256, 10)
def forward(self, x):
x = self.fc2(x)
x = F.relu(x)
x = F.dropout(x)
x = self.fc3(x)
return F.log_softmax(x)
class BasicModel(nn.Module):
def __init__(self, args, name):
super(BasicModel, self).__init__()
self.name=name
def train_regular(self, input_img, input_qst, label):
self.train()
self.optimizer.zero_grad()
output = self(input_img, input_qst)
loss = F.nll_loss(output, label)
loss.backward()
self.optimizer.step()
pred = output.data.max(1)[1]
correct = pred.eq(label.data).cpu().sum()
accuracy = correct * 100. / len(label)
return accuracy
def train_SCST(self, input_img, input_qst, label):
#self.eval()
#output_greedy = self(input_img, input_qst)
#loss_greedy = F.nll_loss(output_greedy, label)
self.train()
self.optimizer.zero_grad()
output = self(input_img, input_qst)
loss = F.nll_loss(output, label)
## loss_adjusted = loss - Variable(loss_greedy, requires_grad=False)
#loss_adjusted = 2.0 * loss - 1.0 * loss_greedy
loss_adjusted = loss
loss_adjusted.backward()
self.optimizer.step()
pred = output.data.max(1)[1]
correct = pred.eq(label.data).cpu().sum()
accuracy = correct * 100. / len(label)
return accuracy
def train_(self, input_img, input_qst, label):
#return self.train_regular(input_img, input_qst, label)
return self.train_SCST(input_img, input_qst, label)
def test_(self, input_img, input_qst, label):
self.eval()
output = self(input_img, input_qst)
pred = output.data.max(1)[1]
correct = pred.eq(label.data).cpu().sum()
accuracy = correct * 100. / len(label)
return accuracy
def save_model(self, save_template, epoch):
#torch.save(self.state_dict(), 'model/epoch_{}_{:02d}.pth'.format(self.name, epoch))
torch.save(self.state_dict(), save_template.format(self.name, epoch))
class RN(BasicModel):
def __init__(self, args):
super(RN, self).__init__(args, 'RN')
self.conv = ConvInputModel()
##(number of filters per object+coordinate of object)*2+question vector
self.g_fc1 = nn.Linear((24+2)*2+11, 256)
self.g_fc2 = nn.Linear(256, 256)
self.g_fc3 = nn.Linear(256, 256)
self.g_fc4 = nn.Linear(256, 256)
self.f_fc1 = nn.Linear(256, 256)
# prepare coord tensor
def cvt_coord(i):
return [(i/5-2)/2., (i%5-2)/2.]
np_coord_tensor = np.zeros((args.batch_size, 25, 2))
for i in range(25):
np_coord_tensor[:,i,:] = np.array( cvt_coord(i) )
coord_tensor = torch.FloatTensor(args.batch_size, 25, 2)
if args.cuda:
coord_tensor = coord_tensor.cuda()
self.coord_tensor = Variable(coord_tensor)
self.coord_tensor.data.copy_(torch.from_numpy(np_coord_tensor))
self.fcout = FCOutputModel()
self.optimizer = optim.Adam(self.parameters(), lr=args.lr)
def forward(self, img, qst):
x = self.conv(img) ## x = (64 x 24 x 5 x 5)
"""g"""
mb = x.size()[0]
n_channels = x.size()[1]
d = x.size()[2]
x_flat = x.view(mb,n_channels,d*d).permute(0,2,1)
# x_flat = (64 x 25 x 24)
# add coordinates
x_flat = torch.cat([x_flat, self.coord_tensor],2)
# add question everywhere
qst = torch.unsqueeze(qst, 1)
qst = qst.repeat(1,25,1)
qst = torch.unsqueeze(qst, 2)
# cast all pairs against each other
x_i = torch.unsqueeze(x_flat,1) # (64x1x25x26+11)
x_i = x_i.repeat(1,25,1,1) # (64x25x25x26+11)
x_j = torch.unsqueeze(x_flat,2) # (64x25x1x26+11)
x_j = torch.cat([x_j,qst],3)
x_j = x_j.repeat(1,1,25,1) # (64x25x25x26+11)
# concatenate all together
x_full = torch.cat([x_i,x_j],3) # (64x25x25x2*26+11)
# reshape for passing through network
x_ = x_full.view(mb*d*d*d*d,63)
x_ = self.g_fc1(x_)
x_ = F.relu(x_)
x_ = self.g_fc2(x_)
x_ = F.relu(x_)
x_ = self.g_fc3(x_)
x_ = F.relu(x_)
x_ = self.g_fc4(x_)
x_ = F.relu(x_)
# reshape again and sum
x_g = x_.view(mb,d*d*d*d,256)
x_g = x_g.sum(1).squeeze()
"""f"""
x_f = self.f_fc1(x_g)
x_f = F.relu(x_f)
return self.fcout(x_f)
class CNN_MLP(BasicModel):
def __init__(self, args):
super(CNN_MLP, self).__init__(args, 'CNNMLP')
self.conv = ConvInputModel()
self.fc1 = nn.Linear(5*5*24 + 11, 256) # question concatenated to all
self.fcout = FCOutputModel()
self.optimizer = optim.Adam(self.parameters(), lr=args.lr)
#print([ a for a in self.parameters() ] )
def forward(self, img, qst):
x = self.conv(img) ## x = (64 x 24 x 5 x 5)
"""fully connected layers"""
x = x.view(x.size(0), -1)
x_ = torch.cat((x, qst), 1) # Concat question
x_ = self.fc1(x_)
x_ = F.relu(x_)
return self.fcout(x_)
# https://github.com/kefirski/pytorch_Highway (MIT license)
class Highway(nn.Module):
def __init__(self, size, num_layers, f):
super(Highway, self).__init__()
self.num_layers = num_layers
self.nonlinear = nn.ModuleList([nn.Linear(size, size) for _ in range(num_layers)])
self.linear = nn.ModuleList([nn.Linear(size, size) for _ in range(num_layers)])
self.gate = nn.ModuleList([nn.Linear(size, size) for _ in range(num_layers)])
self.f = f
def forward(self, x):
"""
:param x: tensor with shape of [batch_size, size]
:return: tensor with shape of [batch_size, size]
applies σ(x) ⨀ (f(G(x))) + (1 - σ(x)) ⨀ (Q(x)) transformation | G and Q is affine transformation,
f is non-linear transformation, σ(x) is affine transformation with sigmoid non-linearition
and ⨀ is element-wise multiplication
"""
for layer in range(self.num_layers):
gate = F.sigmoid(self.gate[layer](x))
nonlinear = self.f(self.nonlinear[layer](x))
linear = self.linear[layer](x)
x = gate * nonlinear + (1 - gate) * linear
return x