-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunet.py
122 lines (105 loc) · 4.32 KB
/
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
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import torch
from torch import nn
from torch.nn import functional as F
class ConvBlock(nn.Module):
"""
A Convolutional Block that consists of two convolution layers each followed by
instance normalization, relu activation and dropout.
"""
def __init__(self, in_chans, out_chans, drop_prob):
"""
Args:
in_chans (int): Number of channels in the input.
out_chans (int): Number of channels in the output.
drop_prob (float): Dropout probability.
"""
super().__init__()
self.in_chans = in_chans
self.out_chans = out_chans
self.drop_prob = drop_prob
self.layers = nn.Sequential(
nn.Conv2d(in_chans, out_chans, kernel_size=3, padding=1),
nn.InstanceNorm2d(out_chans),
nn.ReLU(),
nn.Dropout2d(drop_prob),
nn.Conv2d(out_chans, out_chans, kernel_size=3, padding=1),
nn.InstanceNorm2d(out_chans),
nn.ReLU(),
nn.Dropout2d(drop_prob)
)
def forward(self, input):
"""
Args:
input (torch.Tensor): Input tensor of shape [batch_size, self.in_chans, height, width]
Returns:
(torch.Tensor): Output tensor of shape [batch_size, self.out_chans, height, width]
"""
return self.layers(input)
def __repr__(self):
return f'ConvBlock(in_chans={self.in_chans}, out_chans={self.out_chans}, ' \
f'drop_prob={self.drop_prob})'
class UnetModel(nn.Module):
"""
PyTorch implementation of a U-Net model.
This is based on:
Olaf Ronneberger, Philipp Fischer, and Thomas Brox. U-net: Convolutional networks
for biomedical image segmentation. In International Conference on Medical image
computing and computer-assisted intervention, pages 234–241. Springer, 2015.
"""
def __init__(self, in_chans, out_chans, chans, num_pool_layers, drop_prob):
"""
Args:
in_chans (int): Number of channels in the input to the U-Net model.
out_chans (int): Number of channels in the output to the U-Net model.
chans (int): Number of output channels of the first convolution layer.
num_pool_layers (int): Number of down-sampling and up-sampling layers.
drop_prob (float): Dropout probability.
"""
super().__init__()
self.in_chans = in_chans
self.out_chans = out_chans
self.chans = chans
self.num_pool_layers = num_pool_layers
self.drop_prob = drop_prob
self.down_sample_layers = nn.ModuleList([ConvBlock(in_chans, chans, drop_prob)])
ch = chans
for i in range(num_pool_layers - 1):
self.down_sample_layers += [ConvBlock(ch, ch * 2, drop_prob)]
ch *= 2
self.conv = ConvBlock(ch, ch, drop_prob)
self.up_sample_layers = nn.ModuleList()
for i in range(num_pool_layers - 1):
self.up_sample_layers += [ConvBlock(ch * 2, ch // 2, drop_prob)]
ch //= 2
self.up_sample_layers += [ConvBlock(ch * 2, ch, drop_prob)]
self.conv2 = nn.Sequential(
nn.Conv2d(ch, ch // 2, kernel_size=1),
nn.Conv2d(ch // 2, out_chans, kernel_size=1),
nn.Conv2d(out_chans, out_chans, kernel_size=1),
)
def forward(self, input):
"""
Args:
input (torch.Tensor): Input tensor of shape [batch_size, self.in_chans, height, width]
Returns:
(torch.Tensor): Output tensor of shape [batch_size, self.out_chans, height, width]
"""
stack = []
output = input
# Apply down-sampling layers
for layer in self.down_sample_layers:
output = layer(output)
stack.append(output)
output = F.max_pool2d(output, kernel_size=2)
output = self.conv(output)
# Apply up-sampling layers
for layer in self.up_sample_layers:
output = F.interpolate(output, scale_factor=2, mode='bilinear', align_corners=False)
output = torch.cat([output, stack.pop()], dim=1)
output = layer(output)
return self.conv2(output)