-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_loss.py
50 lines (41 loc) · 1.29 KB
/
test_loss.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
import unittest
import torch
from torch import nn
import numpy as np
from loss import SegmentationLoss
class LossTest(unittest.TestCase):
def test_segmentation(self):
pred1 = torch.tensor([
[1, 0, 1],
[1, 1, 1],
[1, 0, 1]
])
target1 = torch.tensor([
[1, 0, 1],
[1, 0, 1],
[1, 0, 1]
])
iou1 = 6 / 7
pred2 = torch.tensor([
[1, 0, 1],
[0, 0, 0],
[0, 0, 0]
])
target2 = torch.tensor([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
])
iou2 = 0
preds = torch.stack((pred1, pred2))
targets = torch.stack((target1, target2))
pred_batch = torch.stack((preds, preds))
targets_batch = torch.stack((targets, targets))
# Since we calculate the mean of the IOU loss for each sample, and both samples here are duplicates of each other,
# mean_iou_loss will not change
expected_loss = 1 - 6/7
loss = SegmentationLoss()
print(loss(pred_batch.to(torch.float32), targets_batch.to(torch.float32)))
assert(expected_loss == loss(pred_batch.to(torch.float32), targets_batch.to(torch.float32)))
if __name__ == '__main__':
unittest.main()