-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers.py
645 lines (586 loc) · 26.3 KB
/
layers.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
"""Library for capsule layers.
This has the layer implementation for routing and
capsule layers.
Tensorflow source: https://github.com/Sarasra/models/tree/master/research/capsules
"""
from __future__ import absolute_import, division, print_function
import torch
import torch.nn.functional as F
from torch import nn
def _squash(input_tensor, dim=2):
"""
Applies norm nonlinearity (squash) to a capsule layer.
Args:
input_tensor: Input tensor. Shape is [batch, num_channels, num_atoms] for a
fully connected capsule layer or
[batch, num_channels, num_atoms, height, width] or
[batch, num_channels, num_atoms, height, width, depth] for a convolutional
capsule layer.
Returns:
A tensor with same shape as input for output of this layer.
"""
epsilon = 1e-12
norm = torch.linalg.norm(input_tensor, dim=dim, keepdim=True)
norm_squared = norm * norm
return (input_tensor / (norm + epsilon)) * (norm_squared / (1 + norm_squared))
def _update_routing(votes, biases, num_routing):
"""
Sums over scaled votes and applies squash to compute the activations.
Iteratively updates routing logits (scales) based on the similarity between
the activation of this layer and the votes of the layer below.
Args:
votes: tensor, The transformed outputs of the layer below.
biases: tensor, Bias variable.
num_dims: scalar, number of dimmensions in votes. For fully connected
capsule it is 4, for convolutional 2D it is 6, for convolutional 3D it is 7.
num_routing: scalar, Number of routing iterations.
Returns:
The activation tensor of the output layer after num_routing iterations.
"""
votes_shape = votes.size()
logits_shape = list(votes_shape)
logits_shape[3] = 1
logits = torch.zeros(logits_shape, requires_grad=False, device=votes.device)
for i in range(num_routing):
route = F.softmax(logits, dim=2)
preactivate = torch.sum(votes * route, dim=1) + biases[None, ...]
if i + 1 < num_routing:
distances = F.cosine_similarity(preactivate[:, None, ...], votes, dim=3)
logits = logits + distances[:, :, :, None, ...]
else:
activation = _squash(preactivate)
return activation
class DepthwiseConv3d(nn.Module):
"""
Performs 2D convolution given a 5D input tensor.
This layer given an input tensor of shape
`[batch, input_dim, input_atoms, input_height, input_width]` squeezes the
first two dimmensions to get a 4D tensor as the input of torch.nn.Conv2d. Then
splits the first dimmension and the second dimmension and returns the 6D
convolution output.
Args:
kernel_size: scalar or tuple, convolutional kernels are [kernel_size, kernel_size].
input_dim: scalar, number of capsules in the layer below.
output_dim: scalar, number of capsules in this layer.
input_atoms: scalar, number of units in each capsule of input layer.
output_atoms: scalar, number of units in each capsule of output layer.
stride: scalar or tuple, stride of the convolutional kernel.
padding: scalar or tuple, zero-padding added to both sides of the input
dilation: scalar or tuple, spacing between kernel elements
share_weight: share transformation weight matrices between capsules in lower layer or not
Returns:
6D Tensor output of a 2D convolution with shape
`[batch, input_dim, output_dim, output_atoms, out_height, out_width]`.
"""
def __init__(
self,
kernel_size,
input_dim,
output_dim,
input_atoms=8,
output_atoms=8,
stride=2,
dilation=1,
padding=0,
share_weight=True,
):
super().__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.input_atoms = input_atoms
self.output_atoms = output_atoms
self.share_weight = share_weight
if self.share_weight:
self.conv2d = nn.Conv2d(
input_atoms, output_dim * output_atoms, kernel_size, stride=stride, dilation=dilation, padding=padding
)
else:
self.conv2d = nn.Conv2d(
input_dim * input_atoms,
input_dim * output_dim * output_atoms,
kernel_size=kernel_size,
stride=stride,
dilation=dilation,
padding=padding,
groups=input_dim,
)
torch.nn.init.normal_(self.conv2d.weight, std=0.1)
def forward(self, input_tensor):
input_shape = input_tensor.size()
if self.share_weight:
input_tensor_reshaped = input_tensor.view(
input_shape[0] * self.input_dim, self.input_atoms, input_shape[-2], input_shape[-1]
)
else:
input_tensor_reshaped = input_tensor.view(
input_shape[0], self.input_dim * self.input_atoms, input_shape[-2], input_shape[-1]
)
conv = self.conv2d(input_tensor_reshaped)
conv_shape = conv.size()
conv_reshaped = conv.view(
input_shape[0], self.input_dim, self.output_dim, self.output_atoms, conv_shape[-2], conv_shape[-1]
)
return conv_reshaped
class ConvSlimCapsule2D(nn.Module):
"""
Builds a slim convolutional capsule layer.
This layer performs 2D convolution given 5D input tensor of shape
`[batch, input_dim, input_atoms, input_height, input_width]`. Then refines
the votes with routing and applies Squash non linearity for each capsule.
Each capsule in this layer is a convolutional unit and shares its kernel over
the position grid and different capsules of layer below. Therefore, number
of trainable variables in this layer is:
kernel: [kernel_size, kernel_size, input_atoms, output_dim * output_atoms]
bias: [output_dim, output_atoms]
Output of a conv2d layer is a single capsule with channel number of atoms.
Therefore conv_slim_capsule is suitable to be added on top of a conv2d layer
with num_routing=1, input_dim=1 and input_atoms=conv_channels.
Args:
kernel_size: scalar or tuple, convolutional kernels are [kernel_size, kernel_size].
input_dim: scalar, number of capsules in the layer below.
output_dim: scalar, number of capsules in this layer.
input_atoms: scalar, number of units in each capsule of input layer.
output_atoms: scalar, number of units in each capsule of output layer.
stride: scalar or tuple, stride of the convolutional kernel.
padding: scalar or tuple, zero-padding added to both sides of the input
dilation: scalar or tuple, spacing between kernel elements
num_routing: scalar, number of routing iterations.
share_weight: share transformation weight matrices between capsules in lower layer or not
Returns:
Tensor of activations for this layer of shape
`[batch, output_dim, output_atoms, out_height, out_width]`
"""
def __init__(
self,
kernel_size,
input_dim,
output_dim,
input_atoms=8,
output_atoms=8,
stride=2,
dilation=1,
padding=0,
num_routing=3,
share_weight=True,
):
super().__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.output_atoms = output_atoms
self.num_routing = num_routing
self.biases = nn.Parameter(torch.nn.init.constant_(torch.empty(output_dim, output_atoms, 1, 1), 0.1))
self.depthwise_conv3d = DepthwiseConv3d(
kernel_size=kernel_size,
input_dim=input_dim,
output_dim=output_dim,
input_atoms=input_atoms,
output_atoms=output_atoms,
stride=stride,
dilation=dilation,
padding=padding,
share_weight=share_weight,
)
def forward(self, input_tensor):
votes = self.depthwise_conv3d(input_tensor)
return _update_routing(votes, self.biases, self.num_routing)
class DepthwiseDeconv3d(nn.Module):
"""
Performs 2D deconvolution given a 5D input tensor.
This layer given an input tensor of shape
`[batch, input_dim, input_atoms, input_height, input_width]` squeezes the
first two dimmensions to get a 4D tensor as the input of torch.nn.ConvTranspose2d. Then
splits the first dimmension and the second dimmension and returns the 6D
convolution output.
Args:
kernel_size: scalar or tuple, deconvolutional kernels are [kernel_size, kernel_size].
input_dim: scalar, number of capsules in the layer below.
output_dim: scalar, number of capsules in this layer.
input_atoms: scalar, number of units in each capsule of input layer.
output_atoms: scalar, number of units in each capsule of output layer.
stride: scalar or tuple, controls the stride for the cross-correlation.
padding: scalar or tuple, controls the amount of implicit zero-paddings on both sides for dilation * (kernel_size - 1) - padding number of points
share_weight: share transformation weight matrices between capsules in lower layer or not
Returns:
6D Tensor output of a 2D deconvolution with shape
`[batch, input_dim, output_dim, output_atoms, out_height, out_width]`.
"""
def __init__(
self, kernel_size, input_dim, output_dim, input_atoms=8, output_atoms=8, stride=2, padding=0, share_weight=True
):
super().__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.input_atoms = input_atoms
self.output_atoms = output_atoms
self.share_weight = share_weight
if self.share_weight:
self.deconv2d = nn.ConvTranspose2d(input_atoms, output_dim * output_atoms, kernel_size, stride, padding)
else:
self.deconv2d = nn.ConvTranspose2d(
input_dim * input_atoms,
input_dim * output_dim * output_atoms,
kernel_size,
stride,
padding,
groups=input_dim,
)
torch.nn.init.normal_(self.deconv2d.weight, std=0.1)
def forward(self, input_tensor):
input_shape = input_tensor.size()
if self.share_weight:
input_tensor_reshaped = input_tensor.view(
input_shape[0] * self.input_dim, self.input_atoms, input_shape[-2], input_shape[-1]
)
else:
input_tensor_reshaped = input_tensor.view(
input_shape[0], self.input_dim * self.input_atoms, input_shape[-2], input_shape[-1]
)
deconv = self.deconv2d(input_tensor_reshaped)
deconv_shape = deconv.size()
deconv_reshaped = deconv.view(
input_shape[0], self.input_dim, self.output_dim, self.output_atoms, deconv_shape[-2], deconv_shape[-1]
)
return deconv_reshaped
class DeconvSlimCapsule2D(nn.Module):
"""
Builds a slim deconvolutional capsule layer.
This layer performs 2D deconvolution given 5D input tensor of shape
`[batch, input_dim, input_atoms, input_height, input_width]`. Then refines
the votes with routing and applies Squash non linearity for each capsule.
Each capsule in this layer is a deconvolutional unit and shares its kernel over
the position grid and different capsules of layer below. Therefore, number
of trainable variables in this layer is:
kernel: [kernel_size, kernel_size, input_atoms, output_dim * output_atoms]
bias: [output_dim, output_atoms]
Args:
kernel_size: scalar or tuple, deconvolutional kernels are [kernel_size, kernel_size, kernel_size].
input_dim: scalar, number of capsules in the layer below.
output_dim: scalar, number of capsules in this layer.
input_atoms: scalar, number of units in each capsule of input layer.
output_atoms: scalar, number of units in each capsule of output layer.
stride: scalar or tuple, controls the stride for the cross-correlation.
padding: scalar or tuple, controls the amount of implicit zero-paddings on both sides for dilation * (kernel_size - 1) - padding number of points
num_routing: scalar, number of routing iterations.
share_weight: share transformation weight matrices between capsules in lower layer or not
Returns:
Tensor of activations for this layer of shape
`[batch, output_dim, output_atoms, out_height, out_width]`
"""
def __init__(
self,
kernel_size,
input_dim,
output_dim,
input_atoms=8,
output_atoms=8,
stride=2,
padding=0,
num_routing=3,
share_weight=True,
):
super().__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.num_routing = num_routing
self.biases = nn.Parameter(torch.nn.init.constant_(torch.empty(output_dim, output_atoms, 1, 1), 0.1))
self.depthwise_deconv3d = DepthwiseDeconv3d(
kernel_size, input_dim, output_dim, input_atoms, output_atoms, stride, padding, share_weight=share_weight
)
def forward(self, input_tensor):
votes = self.depthwise_deconv3d(input_tensor)
return _update_routing(votes, self.biases, self.num_routing)
class DepthwiseConv4d(nn.Module):
"""
Performs 3D convolution given a 6D input tensor.
This layer given an input tensor of shape
`[batch, input_dim, input_atoms, input_height, input_width, input_depth]` squeezes the
first two dimmensions to get a 5D tensor as the input of torch.nn.Conv3d. Then
splits the first dimmension and the second dimmension and returns the 7D
convolution output.
Args:
kernel_size: scalar or tuple, convolutional kernels are [kernel_size, kernel_size, kernel_size].
input_dim: scalar, number of capsules in the layer below.
output_dim: scalar, number of capsules in this layer.
input_atoms: scalar, number of units in each capsule of input layer.
output_atoms: scalar, number of units in each capsule of output layer.
stride: scalar or tuple, stride of the convolutional kernel.
padding: scalar or tuple, zero-padding added to both sides of the input
dilation: scalar or tuple, spacing between kernel elements
share_weight: share transformation weight matrices between capsules in lower layer or not
Returns:
7D Tensor output of a 3D convolution with shape
`[batch, input_dim, output_dim, output_atoms, out_height, out_width, out_depth]`.
"""
def __init__(
self,
kernel_size,
input_dim,
output_dim,
input_atoms=8,
output_atoms=8,
stride=2,
dilation=1,
padding=0,
share_weight=True,
):
super().__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.input_atoms = input_atoms
self.output_atoms = output_atoms
self.share_weight = share_weight
if self.share_weight:
self.conv3d = nn.Conv3d(
input_atoms,
output_dim * output_atoms,
kernel_size=kernel_size,
stride=stride,
dilation=dilation,
padding=padding,
)
else:
self.conv3d = nn.Conv3d(
input_dim * input_atoms,
input_dim * output_dim * output_atoms,
kernel_size=kernel_size,
stride=stride,
dilation=dilation,
padding=padding,
groups=input_dim,
)
torch.nn.init.normal_(self.conv3d.weight, std=0.1)
def forward(self, input_tensor):
input_shape = input_tensor.size()
if self.share_weight:
input_tensor_reshaped = input_tensor.view(
input_shape[0] * self.input_dim, self.input_atoms, input_shape[-3], input_shape[-2], input_shape[-1]
)
else:
input_tensor_reshaped = input_tensor.view(
input_shape[0], self.input_dim * self.input_atoms, input_shape[-3], input_shape[-2], input_shape[-1]
)
conv = self.conv3d(input_tensor_reshaped)
conv_shape = conv.size()
conv_reshaped = conv.view(
input_shape[0],
self.input_dim,
self.output_dim,
self.output_atoms,
conv_shape[-3],
conv_shape[-2],
conv_shape[-1],
)
return conv_reshaped
class ConvSlimCapsule3D(nn.Module):
"""
Builds a slim convolutional capsule layer.
This layer performs 3D convolution given 6D input tensor of shape
`[batch, input_dim, input_atoms, input_height, input_width, input_depth]`. Then refines
the votes with routing and applies Squash non linearity for each capsule.
Each capsule in this layer is a convolutional unit and shares its kernel over
the position grid and different capsules of layer below. Therefore, number
of trainable variables in this layer is:
kernel: [kernel_size, kernel_size, kernel_size, input_atoms, output_dim * output_atoms]
bias: [output_dim, output_atoms]
Output of a conv3d layer is a single capsule with channel number of atoms.
Therefore conv_slim_capsule_3d is suitable to be added on top of a conv3d layer
with num_routing=1, input_dim=1 and input_atoms=conv_channels.
Args:
kernel_size: scalar or tuple, convolutional kernels are [kernel_size, kernel_size, kernel_size].
input_dim: scalar, number of capsules in the layer below.
output_dim: scalar, number of capsules in this layer.
input_atoms: scalar, number of units in each capsule of input layer.
output_atoms: scalar, number of units in each capsule of output layer.
stride: scalar or tuple, stride of the convolutional kernel.
padding: scalar or tuple, zero-padding added to both sides of the input
dilation: scalar or tuple, spacing between kernel elements
num_routing: scalar, number of routing iterations.
share_weight: share transformation weight matrices between capsules in lower layer or not
Returns:
Tensor of activations for this layer of shape
`[batch, output_dim, output_atoms, out_height, out_width, out_depth]`
"""
def __init__(
self,
kernel_size,
input_dim,
output_dim,
input_atoms=8,
output_atoms=8,
stride=2,
padding=0,
dilation=1,
num_routing=3,
share_weight=True,
):
super().__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.output_atoms = output_atoms
self.num_routing = num_routing
self.biases = nn.Parameter(torch.nn.init.constant_(torch.empty(output_dim, output_atoms, 1, 1, 1), 0.1))
self.depthwise_conv4d = DepthwiseConv4d(
kernel_size=kernel_size,
input_dim=input_dim,
output_dim=output_dim,
input_atoms=input_atoms,
output_atoms=output_atoms,
stride=stride,
padding=padding,
dilation=dilation,
share_weight=share_weight,
)
def forward(self, input_tensor):
votes = self.depthwise_conv4d(input_tensor)
return _update_routing(votes, self.biases, self.num_routing)
class DepthwiseDeconv4d(nn.Module):
"""
Performs 3D deconvolution given a 6D input tensor.
This layer given an input tensor of shape
`[batch, input_dim, input_atoms, input_height, input_width, input_depth]` squeezes the
first two dimmensions to get a 5D tensor as the input of torch.nn.ConvTranspose3d. Then
splits the first dimmension and the second dimmension and returns the 7D
convolution output.
Args:
kernel_size: scalar or tuple, deconvolutional kernels are [kernel_size, kernel_size, kernel_size].
input_dim: scalar, number of capsules in the layer below.
output_dim: scalar, number of capsules in this layer.
input_atoms: scalar, number of units in each capsule of input layer.
output_atoms: scalar, number of units in each capsule of output layer.
stride: scalar or tuple, controls the stride for the cross-correlation.
padding: scalar or tuple, controls the amount of implicit zero-paddings on both sides for dilation * (kernel_size - 1) - padding number of points
share_weight: share transformation weight matrices between capsules in lower layer or not
Returns:
7D Tensor output of a 3D deconvolution with shape
`[batch, input_dim, output_dim, output_atoms, out_height, out_width, out_depth]`.
"""
def __init__(
self, kernel_size, input_dim, output_dim, input_atoms=8, output_atoms=8, stride=2, padding=0, share_weight=True
):
super().__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.input_atoms = input_atoms
self.output_atoms = output_atoms
self.share_weight = share_weight
if self.share_weight:
self.deconv3d = nn.ConvTranspose3d(input_atoms, output_dim * output_atoms, kernel_size, stride, padding)
else:
self.deconv3d = nn.ConvTranspose3d(
input_dim * input_atoms,
input_dim * output_dim * output_atoms,
kernel_size,
stride,
padding,
groups=input_dim,
)
torch.nn.init.normal_(self.deconv3d.weight, std=0.1)
def forward(self, input_tensor):
input_shape = input_tensor.size()
if self.share_weight:
input_tensor_reshaped = input_tensor.view(
input_shape[0] * self.input_dim, self.input_atoms, input_shape[-3], input_shape[-2], input_shape[-1]
)
else:
input_tensor_reshaped = input_tensor.view(
input_shape[0], self.input_dim * self.input_atoms, input_shape[-3], input_shape[-2], input_shape[-1]
)
deconv = self.deconv3d(input_tensor_reshaped)
deconv_shape = deconv.size()
deconv_reshaped = deconv.view(
input_shape[0],
self.input_dim,
self.output_dim,
self.output_atoms,
deconv_shape[-3],
deconv_shape[-2],
deconv_shape[-1],
)
return deconv_reshaped
class DeconvSlimCapsule3D(nn.Module):
"""
Builds a slim deconvolutional capsule layer.
This layer performs 3D deconvolution given 6D input tensor of shape
`[batch, input_dim, input_atoms, input_height, input_width, input_depth]`. Then refines
the votes with routing and applies Squash non linearity for each capsule.
Each capsule in this layer is a deconvolutional unit and shares its kernel over
the position grid and different capsules of layer below. Therefore, number
of trainable variables in this layer is:
kernel: [kernel_size, kernel_size, kernel_size, input_atoms, output_dim * output_atoms]
bias: [output_dim, output_atoms]
Args:
kernel_size: scalar or tuple, deconvolutional kernels are [kernel_size, kernel_size, kernel_size].
input_dim: scalar, number of capsules in the layer below.
output_dim: scalar, number of capsules in this layer.
input_atoms: scalar, number of units in each capsule of input layer.
output_atoms: scalar, number of units in each capsule of output layer.
stride: scalar or tuple, controls the stride for the cross-correlation.
padding: scalar or tuple, controls the amount of implicit zero-paddings on both sides for dilation * (kernel_size - 1) - padding number of points
num_routing: scalar, number of routing iterations.
share_weight: share transformation weight matrices between capsules in lower layer or not
Returns:
Tensor of activations for this layer of shape
`[batch, output_dim, output_atoms, out_height, out_width, out_depth]`
"""
def __init__(
self,
kernel_size,
input_dim,
output_dim,
input_atoms=8,
output_atoms=8,
stride=2,
padding=0,
num_routing=3,
share_weight=True,
):
super().__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.num_routing = num_routing
self.biases = nn.Parameter(torch.nn.init.constant_(torch.empty(output_dim, output_atoms, 1, 1, 1), 0.1))
self.depthwise_deconv4d = DepthwiseDeconv4d(
kernel_size, input_dim, output_dim, input_atoms, output_atoms, stride, padding, share_weight=share_weight
)
def forward(self, input_tensor):
votes = self.depthwise_deconv4d(input_tensor)
return _update_routing(votes, self.biases, self.num_routing)
class MarginLoss(nn.Module):
def __init__(self, margin=0.4, downweight=0.5, class_weight=None, reduction="mean"):
super(MarginLoss, self).__init__()
self.margin = margin
self.downweight = downweight
if class_weight is not None:
self.register_buffer("class_weight", class_weight)
else:
self.class_weight = class_weight
self.reduction = reduction
def forward(self, raw_logits, labels):
raw_logits_shape = raw_logits.size()
num_dims = len(raw_logits_shape)
if num_dims > 2:
raw_logits = raw_logits.view(raw_logits_shape[0], raw_logits_shape[1], -1)
labels = labels.view(raw_logits_shape[0], raw_logits_shape[1], -1)
logits = raw_logits - 0.5
positive_cost = labels * F.relu(self.margin - logits) ** 2
negative_cost = (1 - labels) * F.relu(logits + self.margin) ** 2
if self.class_weight is not None:
if num_dims > 2:
loss = (
torch.sum(
self.class_weight[None, :, None]
* (0.5 * positive_cost + self.downweight * 0.5 * negative_cost),
dim=1,
)
/ torch.sum(self.class_weight)
)
else:
loss = torch.sum(
self.class_weight[None, :] * (0.5 * positive_cost + self.downweight * 0.5 * negative_cost), dim=1
) / torch.sum(self.class_weight)
else:
loss = torch.sum(0.5 * positive_cost + self.downweight * 0.5 * negative_cost, dim=1)
if self.reduction == "mean":
return torch.mean(loss)
else:
pass