-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsodeep_model.py
308 lines (227 loc) · 9.55 KB
/
sodeep_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
"""
****************** COPYRIGHT AND CONFIDENTIALITY INFORMATION ******************
Copyright (c) 2019 [Thomson Licensing]
All Rights Reserved
This program contains proprietary information which is a trade secret/business \
secret of [Thomson Licensing] and is protected, even if unpublished, under \
applicable Copyright laws (including French droit d'auteur) and/or may be \
subject to one or more patent(s).
Recipient is to retain this program in confidence and is not permitted to use \
or make copies thereof other than as permitted in a written agreement with \
[Thomson Licensing] unless otherwise expressly allowed by applicable laws or \
by [Thomson Licensing] under express agreement.
Thomson Licensing is a company of the group TECHNICOLOR
*******************************************************************************
This scripts permits one to reproduce training and experiments of:
Engilberge, M., Chevallier, L., Pérez, P., & Cord, M. (2019, June).
SoDeep: A Sorting Deep Net to Learn Ranking Loss Surrogates.
In Proceedings of CVPR
Author: Martin Engilberge
"""
import torch
import torch.nn as nn
from sodeep_utils import get_rank
def model_loader(model_type, seq_len, pretrained_state_dict=None):
if model_type == "lstm":
model = lstm_baseline(seq_len)
elif model_type == "grus":
model = gru_sum(seq_len)
elif model_type == "gruc":
model = gru_constrained(seq_len)
elif model_type == "grup":
model = gru_proj(seq_len)
elif model_type == "exa":
model = sorter_exact()
elif model_type == "lstmla":
model = lstm_large(seq_len)
elif model_type == "lstme":
model = lstm_end(seq_len)
elif model_type == "mlp":
model = mlp(seq_len)
elif model_type == "cnn":
return cnn(seq_len)
else:
raise Exception("Model type unknown", model_type)
if pretrained_state_dict is not None:
model.load_state_dict(pretrained_state_dict)
return model
class UpdatingWrapper(nn.Module):
""" Wrapper to store the data forwarded throught the sorter and use them later to finetune the sorter on real data
Once enough data have been colected a call to the method update_sorter will perform the finetuning of the sorter.
"""
def __init__(self, sorter, lr_sorter=0.00001):
super(UpdatingWrapper, self).__init__()
self.sorter = sorter
self.opti = torch.optim.Adam(self.sorter.parameters(), lr=lr_sorter, betas=(0.9, 0.999))
self.criterion = nn.L1Loss()
self.average_loss = list()
self.collected_data = list()
self.nb_update = 10
def forward(self, input_):
out = self.sorter(input_)
self.collected_data.append(input_.detach().cpu())
return out
def update_sorter(self):
for input_opti in self.collected_data:
self.opti.zero_grad()
input_opti = input_opti.cuda()
input_opti.requires_grad = True
rank_gt = get_rank(input_opti)
out_opti = self.sorter(input_opti)
loss = self.criterion(out_opti, rank_gt)
loss.backward()
self.opti.step()
self.average_loss.append(loss.item())
# Empty collected data
self.collected_data = list()
def save_data(self, path):
torch.save(self.collected_data, path)
def get_loss_average(self, windows=50):
return sum(self.average_loss[-windows:]) / min(len(self.average_loss), windows)
class lstm_baseline(nn.Module):
def __init__(self, seq_len):
super(lstm_baseline, self).__init__()
self.lstm = nn.LSTM(1, 128, 2, batch_first=True, bidirectional=True)
self.conv1 = nn.Conv1d(seq_len, seq_len, 256)
def forward(self, input_):
input_ = input_.reshape(input_.size(0), -1, 1)
out, _ = self.lstm(input_)
out = self.conv1(out)
return out.view(input_.size(0), -1)
class gru_constrained(nn.Module):
def __init__(self, seq_len):
super(gru_constrained, self).__init__()
self.rnn = nn.GRU(1, 32, 6, batch_first=True, bidirectional=True)
self.sig = torch.nn.Sigmoid()
def forward(self, input_):
input_ = (input_.reshape(input_.size(0), -1, 1) / 2.0) + 1
input_ = self.sig(input_)
x, hn = self.rnn(input_)
out = x.sum(dim=2)
out = self.sig(out)
return out.view(input_.size(0), -1)
class gru_proj(nn.Module):
def __init__(self, seq_len):
super(gru_proj, self).__init__()
self.rnn = nn.GRU(1, 128, 6, batch_first=True, bidirectional=True)
self.conv1 = nn.Conv1d(seq_len, seq_len, 256)
self.sig = torch.nn.Sigmoid()
def forward(self, input_):
input_ = (input_.reshape(input_.size(0), -1, 1) / 2.0) + 1
input_ = self.sig(input_)
out, _ = self.rnn(input_)
out = self.conv1(out)
out = self.sig(out)
return out.view(input_.size(0), -1)
class cnn(nn.Module):
def __init__(self, seq_len):
super(cnn, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv1d(1, 8, 2),
nn.PReLU())
self.layer2 = nn.Sequential(
nn.Conv1d(8, 16, 3),
nn.BatchNorm1d(16),
nn.PReLU())
self.layer3 = nn.Sequential(
nn.Conv1d(16, 32, 5),
nn.PReLU())
self.layer4 = nn.Sequential(
nn.Conv1d(32, 64, 7),
nn.BatchNorm1d(64),
nn.PReLU())
self.layer5 = nn.Sequential(
nn.Conv1d(64, 96, 10),
nn.PReLU())
self.layer6 = nn.Sequential(
nn.Conv1d(96, 128, 7),
nn.BatchNorm1d(128),
nn.PReLU())
self.layer7 = nn.Sequential(
nn.Conv1d(128, 256, 5),
nn.PReLU())
self.layer8 = nn.Sequential(
nn.Conv1d(256, 256, 3),
nn.BatchNorm1d(256),
nn.PReLU())
self.layer9 = nn.Sequential(
nn.Conv1d(256, 128, 3),
nn.PReLU())
self.layer10 = nn.Conv1d(128, seq_len, 64)
def forward(self, input_):
out = input_.unsqueeze(1)
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = self.layer5(out)
out = self.layer6(out)
out = self.layer7(out)
out = self.layer8(out)
out = self.layer9(out)
out = self.layer10(out).view(input_.size(0), -1)
out = torch.sigmoid(out)
out = out
return out
class mlp(nn.Module):
def __init__(self, seq_len):
super(mlp, self).__init__()
self.lin1 = nn.Linear(seq_len, 2048)
self.lin2 = nn.Linear(2048, 2048)
self.lin3 = nn.Linear(2048, seq_len)
self.relu = nn.ReLU()
def forward(self, input_):
input_ = input_.reshape(input_.size(0), -1)
out = self.lin1(input_)
out = self.lin2(self.relu(out))
out = self.lin3(self.relu(out))
return out.view(input_.size(0), -1)
class gru_sum(nn.Module):
def __init__(self, seq_len):
super(gru_sum, self).__init__()
self.lstm = nn.GRU(1, 4, 1, batch_first=True, bidirectional=True)
def forward(self, input_):
input_ = input_.reshape(input_.size(0), -1, 1)
out, _ = self.lstm(input_)
out = out.sum(dim=2)
return out.view(input_.size(0), -1)
class lstm_end(nn.Module):
def __init__(self, seq_len):
super(lstm_end, self).__init__()
self.seq_len = seq_len
self.lstm = nn.GRU(self.seq_len, 5 * self.seq_len, batch_first=True, bidirectional=False)
def forward(self, input_):
input_ = input_.reshape(input_.size(0), -1, 1).repeat(1, input_.size(1), 1).view(input_.size(0), input_.size(1), -1)
_, out = self.lstm(input_)
out = out.view(input_.size(0), self.seq_len, -1) # .view(input_.size(0), -1)[:,:self.seq_len]
out = out.sum(dim=2)
return out
class sorter_exact(nn.Module):
def __init__(self):
super(sorter_exact, self).__init__()
def comp(self, inpu):
in_mat1 = torch.triu(inpu.repeat(inpu.size(0), 1), diagonal=1)
in_mat2 = torch.triu(inpu.repeat(inpu.size(0), 1).t(), diagonal=1)
comp_first = (in_mat1 - in_mat2)
comp_second = (in_mat2 - in_mat1)
std1 = torch.std(comp_first).item()
std2 = torch.std(comp_second).item()
comp_first = torch.sigmoid(comp_first * (6.8 / std1))
comp_second = torch.sigmoid(comp_second * (6.8 / std2))
comp_first = torch.triu(comp_first, diagonal=1)
comp_second = torch.triu(comp_second, diagonal=1)
return (torch.sum(comp_first, 1) + torch.sum(comp_second, 0) + 1) / inpu.size(0)
def forward(self, input_):
out = [self.comp(input_[d]) for d in range(input_.size(0))]
out = torch.stack(out)
return out.view(input_.size(0), -1)
class lstm_large(nn.Module):
def __init__(self, seq_len):
super(lstm_large, self).__init__()
self.lstm = nn.LSTM(1, 512, 2, batch_first=True, bidirectional=True)
self.conv1 = nn.Conv1d(seq_len, seq_len, 1024)
def forward(self, input_):
input_ = input_.reshape(input_.size(0), -1, 1)
out, _ = self.lstm(input_)
out = self.conv1(out)
return out.view(input_.size(0), -1)