-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
151 lines (106 loc) · 5.95 KB
/
model.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
148
149
150
import torch
import torch.nn as nn
from torch.nn import init
from torch.optim import lr_scheduler
class UNet(nn.Module):
def __init__(self, nch_in, nch_out, nch_ker=64, norm='bnorm'):
super(UNet, self).__init__()
self.nch_in = nch_in
self.nch_out = nch_out
self.nch_ker = nch_ker
self.norm = norm
if norm == 'bnorm':
self.bias = False
else:
self.bias = True
"""
Encoder part
"""
self.enc1_1 = CNR2d(1 * self.nch_in, 1 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.enc1_2 = CNR2d(1 * self.nch_ker, 1 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.pool1 = Pooling2d(pool=2, type='avg')
self.enc2_1 = CNR2d(1 * self.nch_ker, 2 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.enc2_2 = CNR2d(2 * self.nch_ker, 2 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.pool2 = Pooling2d(pool=2, type='avg')
self.enc3_1 = CNR2d(2 * self.nch_ker, 4 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.enc3_2 = CNR2d(4 * self.nch_ker, 4 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.pool3 = Pooling2d(pool=2, type='avg')
self.enc4_1 = CNR2d(4 * self.nch_ker, 8 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.enc4_2 = CNR2d(8 * self.nch_ker, 8 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.pool4 = Pooling2d(pool=2, type='avg')
self.enc5_1 = CNR2d(8 * self.nch_ker, 2 * 8 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
"""
Decoder part
"""
self.dec5_1 = DECNR2d(2 * 8 * self.nch_ker, 8 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.unpool4 = UnPooling2d(pool=2, type='nearest')
self.dec4_2 = DECNR2d(2 * 8 * self.nch_ker, 8 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.dec4_1 = DECNR2d(8 * self.nch_ker, 4 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.unpool3 = UnPooling2d(pool=2, type='nearest')
self.dec3_2 = DECNR2d(2 * 4 * self.nch_ker, 4 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.dec3_1 = DECNR2d(4 * self.nch_ker, 2 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.unpool2 = UnPooling2d(pool=2, type='nearest')
self.dec2_2 = DECNR2d(2 * 2 * self.nch_ker, 2 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.dec2_1 = DECNR2d(2 * self.nch_ker, 1 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.unpool1 = UnPooling2d(pool=2, type='nearest')
self.dec1_2 = DECNR2d(2 * 1 * self.nch_ker, 1 * self.nch_ker, kernel_size=3, stride=1, norm=self.norm, relu=0.0, drop=[])
self.dec1_1 = DECNR2d(1 * self.nch_ker, 1 * self.nch_out, kernel_size=3, stride=1, norm=[], relu=[], drop=[], bias=False)
def forward(self, x):
"""
Encoder part
"""
enc1 = self.enc1_2(self.enc1_1(x))
pool1 = self.pool1(enc1)
enc2 = self.enc2_2(self.enc2_1(pool1))
pool2 = self.pool2(enc2)
enc3 = self.enc3_2(self.enc3_1(pool2))
pool3 = self.pool3(enc3)
enc4 = self.enc4_2(self.enc4_1(pool3))
pool4 = self.pool4(enc4)
enc5 = self.enc5_1(pool4)
"""
Decoder part
"""
dec5 = self.dec5_1(enc5)
unpool4 = self.unpool4(dec5)
cat4 = torch.cat([enc4, unpool4], dim=1)
dec4 = self.dec4_1(self.dec4_2(cat4))
unpool3 = self.unpool3(dec4)
cat3 = torch.cat([enc3, unpool3], dim=1)
dec3 = self.dec3_1(self.dec3_2(cat3))
unpool2 = self.unpool2(dec3)
cat2 = torch.cat([enc2, unpool2], dim=1)
dec2 = self.dec2_1(self.dec2_2(cat2))
unpool1 = self.unpool1(dec2)
cat1 = torch.cat([enc1, unpool1], dim=1)
dec1 = self.dec1_1(self.dec1_2(cat1))
x = dec1
return x
def init_weights(net, init_type='normal', init_gain=0.02):
def init_func(m):
classname = m.__class__.__name__
if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
if init_type == 'normal':
init.normal_(m.weight.data, 0.0, init_gain)
elif init_type == 'xavier':
init.xavier_normal_(m.weight.data, gain=init_gain)
elif init_type == 'kaiming':
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
elif init_type == 'orthogonal':
init.orthogonal_(m.weight.data, gain=init_gain)
else:
raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
if hasattr(m, 'bias') and m.bias is not None:
init.constant_(m.bias.data, 0.0)
elif classname.find('BatchNorm2d') != -1: # BatchNorm Layer's weight is not a matrix; only normal distribution applies.
init.normal_(m.weight.data, 1.0, init_gain)
init.constant_(m.bias.data, 0.0)
print('initialize network with %s' % init_type)
net.apply(init_func) # apply the initialization function <init_func>
def init_net(net, init_type='normal', init_gain=0.02, gpu_ids=[]):
if gpu_ids:
assert(torch.cuda.is_available())
net.to(gpu_ids[0])
net = torch.nn.DataParallel(net, gpu_ids) # multi-GPUs
init_weights(net, init_type, init_gain=init_gain)
return net