-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathtest_unet.py
147 lines (131 loc) · 3.7 KB
/
test_unet.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
import pytest
import torch
from unet import (
ActivationFunction,
ConvMode,
Dimensions,
NormalizationLayer,
UNet,
UpMode,
)
def test_unet_2d():
batch_size = 1
in_channels = 1
out_channels = 2
height = 256
width = 256
unet = UNet(
in_channels=in_channels,
out_channels=out_channels,
n_blocks=4,
start_filters=32,
activation=ActivationFunction.RELU,
normalization=NormalizationLayer.BATCH,
conv_mode=ConvMode.SAME,
dim=Dimensions.TWO,
up_mode=UpMode.TRANSPOSED,
)
inp = torch.rand(size=(batch_size, in_channels, height, width), dtype=torch.float32)
out = unet(inp)
assert out.shape == (batch_size, out_channels, height, width)
def test_unet_3d():
batch_size = 1
in_channels = 1
out_channels = 2
height = 64
width = 64
depth = 64
unet = UNet(
in_channels=in_channels,
out_channels=out_channels,
n_blocks=4,
start_filters=32,
activation=ActivationFunction.RELU,
normalization=NormalizationLayer.BATCH,
conv_mode=ConvMode.SAME,
dim=Dimensions.THREE,
up_mode=UpMode.TRANSPOSED,
)
inp = torch.rand(
size=(batch_size, in_channels, depth, height, width), dtype=torch.float32
)
out = unet(inp)
assert out.shape == (batch_size, out_channels, depth, height, width)
def test_unet_valid():
"""
Same settings & input as in U-Net: Convolutional Networks for Biomedical Image Segmentation: https://arxiv.org/abs/1505.04597
"""
batch_size = 1
in_channels = 1
out_channels = 2
input_spatial_dim = 572
expected_spatial_dim = 388
unet = UNet(
in_channels=in_channels,
out_channels=out_channels,
n_blocks=5,
start_filters=32,
activation=ActivationFunction.RELU,
normalization=NormalizationLayer.BATCH,
conv_mode=ConvMode.VALID,
dim=Dimensions.TWO,
up_mode=UpMode.TRANSPOSED,
)
inp = torch.rand(
size=(batch_size, in_channels, input_spatial_dim, input_spatial_dim),
dtype=torch.float32,
)
out = unet(inp)
assert out.shape == (
batch_size,
out_channels,
expected_spatial_dim,
expected_spatial_dim,
)
@pytest.mark.parametrize(
argnames="up_mode", argvalues=[UpMode.BILINEAR, UpMode.BICUBIC]
)
def test_unet_2d_up_modes(up_mode):
batch_size = 1
in_channels = 1
out_channels = 2
height = 256
width = 256
unet = UNet(
in_channels=in_channels,
out_channels=out_channels,
n_blocks=4,
start_filters=32,
activation=ActivationFunction.RELU,
normalization=NormalizationLayer.BATCH,
conv_mode=ConvMode.SAME,
dim=Dimensions.TWO,
up_mode=up_mode,
)
inp = torch.rand(size=(batch_size, in_channels, height, width), dtype=torch.float32)
out = unet(inp)
assert out.shape == (batch_size, out_channels, height, width)
@pytest.mark.parametrize(argnames="up_mode", argvalues=[UpMode.TRILINEAR])
def test_unet_3d_up_modes(up_mode):
batch_size = 1
in_channels = 1
out_channels = 2
height = 64
width = 64
depth = 64
unet = UNet(
in_channels=in_channels,
out_channels=out_channels,
n_blocks=4,
start_filters=32,
activation=ActivationFunction.RELU,
normalization=NormalizationLayer.BATCH,
conv_mode=ConvMode.SAME,
dim=Dimensions.THREE,
up_mode=up_mode,
)
inp = torch.rand(
size=(batch_size, in_channels, depth, height, width), dtype=torch.float32
)
out = unet(inp)
assert out.shape == (batch_size, out_channels, depth, height, width)