forked from elijahcole/sinr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlosses.py
291 lines (218 loc) · 10.3 KB
/
losses.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
import torch
from torch.nn import functional as F
import utils
def get_loss_function(params):
if params['loss'] == 'an_full':
return an_full
elif params['loss'] == 'an_slds':
return an_slds
elif params['loss'] == 'an_ssdl':
return an_ssdl
elif params['loss'] == 'an_full_me':
return an_full_me
elif params['loss'] == 'an_slds_me':
return an_slds_me
elif params['loss'] == 'an_ssdl_me':
return an_ssdl_me
elif params['loss'] == 'neg_log_loss':
return neg_log_loss
elif params['loss'] == 'bce':
return bce
elif params['loss'] == 'neg_log_dl_an':
return neg_log_dl_an
elif params['loss'] == 'bce_dl_an':
return bce_dl_an
def neg_log(x):
return -torch.log(x + 1e-5)
def bernoulli_entropy(p):
entropy = p * neg_log(p) + (1-p) * neg_log(1-p)
return entropy
def neg_log_loss(batch, model, params, loc_to_feats):
"""
Calculates negative log loss of prediction at a location targeted to the corresponding annotation.
Parameters:
- batch: the annotation batch that supplies: locational features, locations (unused), class ids, annotation types (0 | 1)
- model: the model used to calculate loss for
- params: training | annotation parameters used for fine tuning
- loc_to_feats (unused): location encoder
Returns:
- neg_log_loss: A torch Loss object that you would use to calculate backwards on.
"""
inds = torch.arange(params['batch_size'])
loc_feat, _, class_id, types = batch
loc_feat = loc_feat.to(params['device'])
class_id = class_id.to(params['device'])
types = types.to(params['device'])
assert model.inc_bias == False
batch_size = loc_feat.shape[0]
loc_emb = model(loc_feat, return_feats=True)
loc_pred = torch.sigmoid(model.class_emb(loc_emb))
falsy = torch.ones_like(loc_pred) - loc_pred
truthy = loc_pred
loc_pred = torch.where(types.unsqueeze(1) == 0, falsy, truthy)
nl_loss = neg_log(loc_pred)
return nl_loss.mean()
def bce(batch, model, params, loc_to_feats):
"""
Calculates binary cross entropy loss of prediction at a location targeted to the corresponding annotation.
Parameters:
- batch: the annotation batch that supplies: locational features, locations (unused), class ids, annotation types (0 | 1)
- model: the model used to calculate loss for
- params: training | annotation parameters used for fine tuning
- loc_to_feats (unused): location encoder
Returns:
- bce_loss: A torch Loss object that you would use to calculate backwards on.
"""
inds = torch.arange(params['batch_size'])
loc_feat, _, class_id, types = batch
loc_feat = loc_feat.to(params['device'])
class_id = class_id.to(params['device'])
types = types.to(params['device'])
assert model.inc_bias == False
batch_size = loc_feat.shape[0]
loc_emb = model(loc_feat, return_feats=True)
loc_pred = torch.sigmoid(model.class_emb(loc_emb))
# weights = torch.where(types == 0, torch.tensor(100.0).to(params['device']), torch.tensor(1.0).to(params['device']))
# weights = weights.to(params['device'])
bce_loss = F.binary_cross_entropy(loc_pred[inds[:batch_size], class_id], types.float())
return bce_loss
def an_ssdl(batch, model, params, loc_to_feats, neg_type='hard'):
inds = torch.arange(params['batch_size'])
loc_feat, _, class_id = batch
loc_feat = loc_feat.to(params['device'])
class_id = class_id.to(params['device'])
assert model.inc_bias == False
batch_size = loc_feat.shape[0]
# create random background samples and extract features
rand_loc = utils.rand_samples(batch_size, params['device'], rand_type='spherical')
rand_feat = loc_to_feats(rand_loc, normalize=False)
# get location embeddings
loc_cat = torch.cat((loc_feat, rand_feat), 0) # stack vertically
loc_emb_cat = model(loc_cat, return_feats=True)
loc_emb = loc_emb_cat[:batch_size, :]
loc_emb_rand = loc_emb_cat[batch_size:, :]
loc_pred = torch.sigmoid(model.class_emb(loc_emb))
loc_pred_rand = torch.sigmoid(model.class_emb(loc_emb_rand))
# data loss
loss_pos = neg_log(loc_pred[inds[:batch_size], class_id])
if neg_type == 'hard':
loss_bg = neg_log(1.0 - loc_pred_rand[inds[:batch_size], class_id]) # assume negative
elif neg_type == 'entropy':
loss_bg = -1 * bernoulli_entropy(1.0 - loc_pred_rand[inds[:batch_size], class_id]) # entropy
else:
raise NotImplementedError
# total loss
loss = loss_pos.mean() + loss_bg.mean()
return loss
def an_slds(batch, model, params, loc_to_feats, neg_type='hard'):
inds = torch.arange(params['batch_size'])
loc_feat, _, class_id = batch
loc_feat = loc_feat.to(params['device'])
class_id = class_id.to(params['device'])
assert model.inc_bias == False
batch_size = loc_feat.shape[0]
loc_emb = model(loc_feat, return_feats=True)
loc_pred = torch.sigmoid(model.class_emb(loc_emb))
num_classes = loc_pred.shape[1]
bg_class = torch.randint(low=0, high=num_classes-1, size=(batch_size,), device=params['device'])
bg_class[bg_class >= class_id[:batch_size]] += 1
# data loss
loss_pos = neg_log(loc_pred[inds[:batch_size], class_id])
if neg_type == 'hard':
loss_bg = neg_log(1.0 - loc_pred[inds[:batch_size], bg_class]) # assume negative
elif neg_type == 'entropy':
loss_bg = -1 * bernoulli_entropy(1.0 - loc_pred[inds[:batch_size], bg_class]) # entropy
else:
raise NotImplementedError
# total loss
loss = loss_pos.mean() + loss_bg.mean()
return loss
def an_full(batch, model, params, loc_to_feats, neg_type='hard'):
inds = torch.arange(params['batch_size'])
loc_feat, _, class_id = batch
loc_feat = loc_feat.to(params['device'])
class_id = class_id.to(params['device'])
assert model.inc_bias == False
batch_size = loc_feat.shape[0]
# create random background samples and extract features
rand_loc = utils.rand_samples(batch_size, params['device'], rand_type='spherical')
rand_feat = loc_to_feats(rand_loc, normalize=False)
# get location embeddings
loc_cat = torch.cat((loc_feat, rand_feat), 0) # stack vertically
loc_emb_cat = model(loc_cat, return_feats=True)
loc_emb = loc_emb_cat[:batch_size, :]
loc_emb_rand = loc_emb_cat[batch_size:, :]
# get predictions for locations and background locations
loc_pred = torch.sigmoid(model.class_emb(loc_emb))
loc_pred_rand = torch.sigmoid(model.class_emb(loc_emb_rand))
# data loss
if neg_type == 'hard':
loss_pos = neg_log(1.0 - loc_pred) # assume negative
loss_bg = neg_log(1.0 - loc_pred_rand) # assume negative
elif neg_type == 'entropy':
loss_pos = -1 * bernoulli_entropy(1.0 - loc_pred) # entropy
loss_bg = -1 * bernoulli_entropy(1.0 - loc_pred_rand) # entropy
else:
raise NotImplementedError
loss_pos[inds[:batch_size], class_id] = params['pos_weight'] * neg_log(loc_pred[inds[:batch_size], class_id])
# total loss
loss = loss_pos.mean() + loss_bg.mean()
return loss
def an_full_me(batch, model, params, loc_to_feats):
return an_full(batch, model, params, loc_to_feats, neg_type='entropy')
def an_ssdl_me(batch, model, params, loc_to_feats):
return an_ssdl(batch, model, params, loc_to_feats, neg_type='entropy')
def an_slds_me(batch, model, params, loc_to_feats):
return an_slds(batch, model, params, loc_to_feats, neg_type='entropy')
def neg_log_dl_an(batch, model, params, loc_to_feats, neg_type='hard'):
"""
Loss function for fine tuning, combines neg_log loss and ssdl loss. Further discussed in fine tuning report.
Parameters:
- batch: the annotation batch that supplies: locational features, locations (unused), class ids, annotation types (0 | 1)
- model: the model used to calculate loss for
- params: training | annotation parameters used for fine tuning
- loc_to_feats (unused): location encoder
Returns:
- loss: weighted sum of the two losses
"""
nl_loss = neg_log_loss(batch, model, params, loc_to_feats)
inds = torch.arange(params['batch_size'])
loc_feat, _, class_id, _ = batch
loc_feat = loc_feat.to(params['device'])
class_id = class_id.to(params['device'])
assert model.inc_bias == False
batch_size = loc_feat.shape[0]
# create random background samples and extract features
rand_loc = utils.rand_samples(batch_size, params['device'], rand_type='spherical')
rand_feat = loc_to_feats(rand_loc, normalize=False)
loc_emb = model(rand_feat, return_feats=True)
loc_pred = torch.sigmoid(model.class_emb(loc_emb))
loc_pred_input = loc_pred[inds[:batch_size], class_id]
dl_an = F.binary_cross_entropy(loc_pred_input, torch.zeros_like(loc_pred_input))
return nl_loss * params['prior_weight'] + dl_an * params['dl_an_weight']
def bce_dl_an(batch, model, params, loc_to_feats, neg_type='hard'):
"""
Loss function for fine tuning, combines bce loss and ssdl loss. Further discussed in fine tuning report.
Parameters:
- batch: the annotation batch that supplies: locational features, locations (unused), class ids, annotation types (0 | 1)
- model: the model used to calculate loss for
- params: training | annotation parameters used for fine tuning
- loc_to_feats (unused): location encoder
Returns:
- loss: weighted sum of the two losses
"""
bce_loss = bce(batch, model, params, loc_to_feats)
inds = torch.arange(params['batch_size'])
loc_feat, _, class_id, _ = batch
loc_feat = loc_feat.to(params['device'])
class_id = class_id.to(params['device'])
assert model.inc_bias == False
batch_size = loc_feat.shape[0]
# create random background samples and extract features
rand_loc = utils.rand_samples(batch_size, params['device'], rand_type='spherical')
rand_feat = loc_to_feats(rand_loc, normalize=False)
loc_emb = model(rand_feat, return_feats=True)
loc_pred = torch.sigmoid(model.class_emb(loc_emb))
loc_pred_input = loc_pred[inds[:batch_size], class_id]
dl_an = F.binary_cross_entropy(loc_pred_input, torch.zeros_like(loc_pred_input))
return bce_loss * params['prior_weight'] + dl_an * params['dl_an_weight']