-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseline_models.py
271 lines (237 loc) · 7.79 KB
/
baseline_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
"""
This file defines simple baseline models for scene comparison.
"""
import torch
import torch.nn.functional as F
from torch.nn import Linear, Sequential, ReLU
class MLPBaseline(torch.nn.Module):
def __init__(self):
super().__init__()
class RNNBaseline(torch.nn.Module):
def __init__(self):
super().__init__()
# simple models
class NaiveMLP(MLPBaseline):
"""
The simplest, most unstructured model.
"""
def __init__(self,
n_objects,
f_obj,
layers,
**kwargs):
super().__init__()
self.layer_list = []
f_in = n_objects * f_obj
for f_out in layers:
self.layer_list.append(Linear(f_in, f_out))
self.layer_list.append(ReLU())
f_in = f_out
self.layer_list.append(Linear(f_in, 2))
self.mlp = Sequential(*self.layer_list)
def forward(self, *inputs):
x, _, _, _ = inputs
return self.mlp(x)
class NaiveLSTM(RNNBaseline):
"""
LSTM Baseline.
"""
def __init__(self,
f_obj,
h,
layers,
n_layers=1,
**kwargs):
super().__init__()
self.lstm = torch.nn.LSTM(f_obj, h, n_layers)
self.layer_list = []
f_in = h
for f_out in layers:
self.layer_list.append(Linear(f_in, f_out))
self.layer_list.append(ReLU())
f_in = f_out
self.layer_list.append(Linear(f_in, 2))
self.mlp = Sequential(*self.layer_list)
def forward(self, *inputs):
"""
Forward pass. Expects the data to be have as size :
[seq_len, b_size, f_obj]
We use the last hidden state as the latent vector we then decode using
am mlp.
"""
x1, _, _, _ = inputs
out = self.lstm(x1)[0][-1]
return self.mlp(out)
# double models
class DoubleNaiveMLP(MLPBaseline):
"""
The simplest, most unstructured model.
"""
def __init__(self,
n_objects,
f_obj,
layers,
**kwargs):
super().__init__()
self.layer_list = []
f_in = 2 * n_objects * f_obj
for f_out in layers:
self.layer_list.append(Linear(f_in, f_out))
self.layer_list.append(ReLU())
f_in = f_out
self.layer_list.append(Linear(f_in, 2))
self.mlp = Sequential(*self.layer_list)
def forward(self, *inputs):
x1, x2, _, _ = inputs
return self.mlp(torch.cat([x1, x2], 1))
class SceneMLP(MLPBaseline):
"""
A model that is a bit more structured than NaiveMLP.
"""
def __init__(self,
n_objects,
f_obj,
layers_scene,
f_scene,
layers_merge,
**kwargs):
super().__init__()
# scene mlp
self.layer_list = []
f_in = f_obj * n_objects
for f_out in layers_scene:
self.layer_list.append(Linear(f_in, f_out))
self.layer_list.append(ReLU())
f_in = f_out
self.layer_list.append(Linear(f_in, f_scene))
self.scene_mlp = Sequential(*self.layer_list)
# merge mlp
f_in = 2 * f_scene # two scenes as input to merge
self.layer_list = []
for f_out in layers_merge:
self.layer_list.append(Linear(f_in, f_out))
self.layer_list.append(ReLU())
f_in = f_out
self.layer_list.append(Linear(f_in, 2))
self.merge_mlp = Sequential(*self.layer_list)
def forward(self, *inputs):
x1, x2, _, _ = inputs
scene1 = self.scene_mlp(x1)
scene2 = self.scene_mlp(x2)
return self.merge_mlp(torch.cat([scene1, scene2], 1))
class DoubleNaiveLSTM(RNNBaseline):
"""
LSTM Baseline for double setting.
"""
def __init__(self,
f_obj,
h,
layers,
n_layers=1,
**kwargs):
super().__init__()
self.lstm = torch.nn.LSTM(f_obj, h, n_layers)
self.layer_list = []
f_in = h
for f_out in layers:
self.layer_list.append(Linear(f_in, f_out))
self.layer_list.append(ReLU())
f_in = f_out
self.layer_list.append(Linear(f_in, 2))
self.mlp = Sequential(*self.layer_list)
def forward(self, *inputs):
x1, x2, _, _ = inputs
out = self.lstm(torch.cat([x1, x2], 0))[0][-1]
return self.mlp(out)
class SceneLSTM(RNNBaseline):
"""
LSTM baseline, with scene separation.
"""
def __init__(self,
f_obj,
h,
layers,
f_out=2,
n_layers=1,
**kwargs):
super().__init__()
self.lstm = torch.nn.LSTM(f_obj, h, n_layers)
self.layer_list = []
f_in = 2 * h
for f_out in layers:
self.layer_list.append(Linear(f_in, f_out))
self.layer_list.append(ReLU())
f_in = f_out
self.layer_list.append(Linear(f_in, 2))
self.mlp = Sequential(*self.layer_list)
def forward(self, *inputs):
x1, x2, _, _ = inputs
h1 = self.lstm(x1)[0][-1]
h2 = self.lstm(x2)[0][-1]
return self.mlp(torch.cat([h1, h2], 1))
###############################################################################
# #
# Image-based Baselines #
# #
###############################################################################
class CNNBaseline(object):
"""docstring for CNNBaseline"""
def __init__(self, arg):
super(CNNBaseline, self).__init__()
self.arg = arg
class BetaVAE(torch.nn.Module):
"""
Beta-VAE used to learn embeddings of the scenes, to be used subsequently
for defining a distance between two scenes in embedding space.
"""
def __init__(self,
image_size,
f_z,
layers):
"""
Initializes the BetaVAE.
"""
super(BetaVAE, self).__init__()
pass
def forward(self, image):
pass
class EmbeddingComparison(torch.nn.Module):
"""
This class uses the difference in embedding space to compare the two
scenes.
"""
def __init__(self,
embedding,
f_embed,
layers):
"""
Initializes the EmbeddingComparison.
The model works in the following way : the two scenes are embedded by
the provided embedding, their difference in this space is computed, and
this difference is then processes by an MLP.
Arguments :
- embedding (nn model) : the embedding model.
- f_embed (int) : the number of features in the output of the
embedding
- layers (iterable of ints) : number of hidden units of the
mlp layers, excluding the output.
"""
super(EmbeddingComparison, self).__init__()
self.embedding = embedding
self.layer_list = []
f_in = f_embed
for f_out in layers:
self.layer_list.append(Linear(f_in, f_out))
self.layer_list.append(ReLU())
f_in = f_out
self.layer_list.append(Linear(f_in, 2))
self.mlp = Sequential(*self.layer_list)
def forward(self, image1, image2):
"""
Gives a score for each class : "different configuration" and "same
configuration"
"""
z1 = self.embedding(image1)
z2 = self.embedding(image2)
z = z1 - z2
return self.mlp(z)