-
Notifications
You must be signed in to change notification settings - Fork 18
/
test_loss_wrapper.py
154 lines (128 loc) · 5.05 KB
/
test_loss_wrapper.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
import unittest
import torch
class TestLossWrapper(unittest.TestCase):
def test_ApplyAndRemove_grad_masking(self):
from torch_em.loss import ( ApplyAndRemoveMask,
ApplyMask,
DiceLoss,
LossWrapper)
shape = (1, 1, 128, 128)
for masking_func in ApplyMask.MASKING_FUNCS:
transform = ApplyAndRemoveMask(
masking_method=masking_func
)
loss = LossWrapper(DiceLoss(), transform=transform)
x = torch.rand(*shape)
x.requires_grad = True
x.retain_grad = True
y = torch.rand(*shape)
mask = torch.rand(*shape) > .5
y = torch.cat([
y, mask.to(dtype=y.dtype)
], dim=1)
lval = loss(x, y)
self.assertTrue(0. < lval.item() < 1.)
lval.backward()
grad = x.grad.numpy()
mask = mask.numpy()
# print((grad[mask] == 0).sum())
self.assertFalse((grad[mask] == 0).all())
# print((grad[~mask] == 0).sum())
self.assertTrue((grad[~mask] == 0).all())
def test_MaskIgnoreLabel_grad_masking(self):
from torch_em.loss import ( MaskIgnoreLabel,
ApplyMask,
DiceLoss,
LossWrapper)
shape = (1, 1, 128, 128)
ignore_label = -1
for masking_func in ApplyMask.MASKING_FUNCS:
transform = MaskIgnoreLabel(
masking_method=masking_func,
ignore_label=ignore_label
)
loss = LossWrapper(DiceLoss(), transform=transform)
x = torch.rand(*shape)
x.requires_grad = True
x.retain_grad = True
y = torch.rand(*shape)
mask = torch.rand(*shape) > .5
y[mask] = ignore_label
lval = loss(x, y)
self.assertTrue(0. < lval.item() < 1.)
lval.backward()
grad = x.grad.numpy()
mask = mask.numpy()
self.assertFalse((grad[~mask] == 0).all())
self.assertTrue((grad[mask] == 0).all())
def test_ApplyMask_grad_masking(self):
from torch_em.loss import ( ApplyMask,
DiceLoss,
LossWrapper)
shape = (1, 1, 128, 128)
for masking_func in ApplyMask.MASKING_FUNCS:
transform = ApplyMask(
masking_method=masking_func
)
loss = LossWrapper(DiceLoss(), transform=transform)
x = torch.rand(*shape)
x.requires_grad = True
x.retain_grad = True
y = torch.rand(*shape)
mask = torch.rand(*shape) > .5
lval = loss(x, y, mask=mask)
self.assertTrue(0. < lval.item() < 1.)
lval.backward()
grad = x.grad.numpy()
mask = mask.numpy()
self.assertFalse((grad[mask] == 0).all())
self.assertTrue((grad[~mask] == 0).all())
def test_ApplyMask_output_shape_crop(self):
from torch_em.loss import ApplyMask
# _crop batch_size=1
shape = (1, 1, 10, 128, 128)
p = torch.rand(*shape)
t = torch.rand(*shape)
m = torch.rand(*shape) > .5
p_masked, t_masked = ApplyMask()(p, t, m)
out_shape = (m.sum(), shape[1])
self.assertTrue(p_masked.shape == out_shape)
self.assertTrue(t_masked.shape == out_shape)
# _crop batch_size>1
shape = (5, 1, 10, 128, 128)
p = torch.rand(*shape)
t = torch.rand(*shape)
m = torch.rand(*shape) > .5
p_masked, t_masked = ApplyMask()(p, t, m)
out_shape = (m.sum(), shape[1])
self.assertTrue(p_masked.shape == out_shape)
self.assertTrue(t_masked.shape == out_shape)
# _crop n_channels>1
shape = (1, 2, 10, 128, 128)
p = torch.rand(*shape)
t = torch.rand(*shape)
m = torch.rand(*shape) > .5
with self.assertRaises(ValueError):
p_masked, t_masked = ApplyMask()(p, t, m)
# _crop different shapes
shape_pt = (5, 2, 10, 128, 128)
p = torch.rand(*shape_pt)
t = torch.rand(*shape_pt)
shape_m = (5, 1, 10, 128, 128)
m = torch.rand(*shape_m) > .5
p_masked, t_masked = ApplyMask()(p, t, m)
out_shape = (m.sum(), shape_pt[1])
self.assertTrue(p_masked.shape == out_shape)
self.assertTrue(t_masked.shape == out_shape)
def test_ApplyMask_output_shape_multiply(self):
from torch_em.loss import ApplyMask
# _multiply
shape = (2, 5, 10, 128, 128)
p = torch.rand(*shape)
t = torch.rand(*shape)
m = torch.rand(*shape) > .5
p_masked, t_masked = ApplyMask(masking_method="multiply")(p, t, m)
self.assertTrue(p_masked.shape == shape)
self.assertTrue(t_masked.shape == shape)
if __name__ == '__main__':
unittest.main()