-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathtversky_loss_test.py
51 lines (43 loc) · 1.31 KB
/
tversky_loss_test.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
import torch
import torch.nn as nn
import torch.optim as optim
from seg_loss.multitverskyloss import MultiTverskyLoss
class ConvNet(nn.Module):
def __init__(self, in_channels, out_channels):
super(ConvNet, self).__init__()
self.Conv1 = nn.Sequential(
nn.Conv3d(in_channels, 32, 3, padding=1),
nn.BatchNorm3d(32),
nn.ReLU()
)
self.Conv2 = nn.Sequential(
nn.Conv3d(32, 64, 3, padding=1),
nn.BatchNorm3d(64),
nn.ReLU()
)
self.Conv3 = nn.Sequential(
nn.Conv3d(64, out_channels, 3, padding=1),
nn.Softmax(dim=1)
)
def forward(self, input):
out = self.Conv1(input)
out = self.Conv2(out)
out = self.Conv3(out)
return out
def test():
in_channels = 1
n_classes = 5
data = torch.rand(4, in_channels, 16, 64, 64)
target = torch.randint(0, n_classes, size=(4, 1, 16, 64, 64)).long()
net = ConvNet(in_channels, n_classes)
opt = optim.Adam(net.parameters(), lr=0.001)
for i in range(100):
opt.zero_grad()
out = net(data)
criterion = MultiTverskyLoss(0.7, 0.3)
loss = criterion(out, target)
print(loss)
loss.backward()
opt.step()
if __name__ == '__main__':
test()