-
Notifications
You must be signed in to change notification settings - Fork 30
/
unet_fzh.py
134 lines (115 loc) · 4.96 KB
/
unet_fzh.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
class UNet(nn.Module):
"""Custom U-Net architecture for Noise2Noise (see Appendix, Table 2)."""
def __init__(self, in_channels=3, out_channels=3):
"""Initializes U-Net."""
super(UNet, self).__init__()
# Layers: enc_conv0, enc_conv1, pool1
self._block1 = nn.Sequential(
nn.Conv2d(in_channels, 48, 3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(48, 48, 3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(2))
# Layers: enc_conv(i), pool(i); i=2..5
self._block2 = nn.Sequential(
nn.Conv2d(48, 48, 3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(2))
#增大一层感受野
self._block2_2 = nn.Sequential(
nn.Conv2d(48, 48, 3, stride=1, padding=6, dilation=6),
nn.ReLU(inplace=True),
nn.MaxPool2d(2))
# Layers: enc_conv6, upsample5
self._block3 = nn.Sequential(
nn.Conv2d(48, 48, 3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(48, 48, 3, stride=2, padding=1, output_padding=1))
#nn.Upsample(scale_factor=2, mode='nearest'))
# Layers: dec_conv5a, dec_conv5b, upsample4
self._block4_1 = nn.Sequential(
nn.Conv2d(96, 96, 3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(96, 96, 3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(96, 96, 3, stride=2, padding=1, output_padding=1))
# Layers: dec_conv5a, dec_conv5b, upsample4
self._block4 = nn.Sequential(
nn.Conv2d(144, 96, 3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(96, 96, 3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(96, 96, 3, stride=2, padding=1, output_padding=1))
#nn.Upsample(scale_factor=2, mode='nearest'))
# Layers: dec_deconv(i)a, dec_deconv(i)b, upsample(i-1); i=4..2
self._block5 = nn.Sequential(
nn.Conv2d(144, 96, 3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(96, 96, 3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(96, 96, 3, stride=2, padding=1, output_padding=1))
#nn.Upsample(scale_factor=2, mode='nearest'))
# Layers: dec_conv1a, dec_conv1b, dec_conv1c,
self._block6 = nn.Sequential(
nn.Conv2d(96 + in_channels, 64, 3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(64, 32, 3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(32, out_channels, 3, stride=1, padding=1),
nn.LeakyReLU(0.1))
# Initialize weights
self._init_weights()
def _init_weights(self):
"""Initializes weights using He et al. (2015)."""
for m in self.modules():
if isinstance(m, nn.ConvTranspose2d) or isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight.data)
m.bias.data.zero_()
def forward(self, x):
"""Through encoder, then decoder by adding U-skip connections. """
# Encoder
pool1 = self._block1(x)
# print('==pool1.shape:', pool1.shape)
pool2 = self._block2(pool1)
# print('==pool2.shape:', pool2.shape)
pool3 = self._block2(pool2)
# print('==pool3.shape:', pool3.shape)
pool4 = self._block2(pool3)
# print('==pool4.shape:', pool4.shape)
pool5 = self._block2_2(pool4)
# print('==pool5.shape:', pool5.shape)
pool6 = self._block2_2(pool5)
# print('==pool6.shape:', pool6.shape)
# Decoder
upsample6 = self._block3(pool6)
# print('==upsample6.shape:', upsample6.shape)
concat6 = torch.cat((upsample6, pool5), dim=1)
# print('==concat6.shape', concat6.shape)
upsample5 = self._block4_1(concat6)
# print('==upsample5.shape:', upsample5.shape)
concat5 = torch.cat((upsample5, pool4), dim=1)
upsample4 = self._block4(concat5)
# print('==upsample4.shape:', upsample4.shape)
concat4 = torch.cat((upsample4, pool3), dim=1)
upsample3 = self._block5(concat4)
# print('==upsample3.shape:', upsample3.shape)
concat3 = torch.cat((upsample3, pool2), dim=1)
upsample2 = self._block5(concat3)
# print('==upsample2.shape:', upsample2.shape)
concat2 = torch.cat((upsample2, pool1), dim=1)
upsample1 = self._block5(concat2)
# print('==upsample1.shape:', upsample1.shape)
concat1 = torch.cat((upsample1, x), dim=1)
# Final activation
return self._block6(concat1)
def debug_unet():
model = UNet()
x = torch.rand((32, 3, 640, 640))
y = model(x)
print('==y.shape:', y.shape)
if __name__ == '__main__':
debug_unet()