-
Notifications
You must be signed in to change notification settings - Fork 23
/
model.py
172 lines (138 loc) · 5.72 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
import megengine as mge
import megengine.module as nn
import megengine.functional as F
def conv3x3(in_chn, out_chn, bias=True):
layer = nn.Conv2d(in_chn, out_chn, kernel_size=3, stride=1, padding=1, bias=bias)
return layer
def conv_down(in_chn, out_chn, bias=False):
layer = nn.Conv2d(in_chn, out_chn, kernel_size=4, stride=2, padding=1, bias=bias)
return layer
class UNetD(nn.Module):
def __init__(self, in_chn, wf=32, depth=5, relu_slope=0.2, subspace_dim=16):
super(UNetD, self).__init__()
self.depth = depth
self.down_path = []
prev_channels = self.get_input_chn(in_chn)
for i in range(depth):
downsample = True if (i+1) < depth else False
self.down_path.append(UNetConvBlock(prev_channels, (2**i)*wf, downsample, relu_slope))
prev_channels = (2**i) * wf
# self.ema = EMAU(prev_channels, prev_channels//8)
self.up_path = []
subnet_repeat_num = 1
for i in reversed(range(depth - 1)):
self.up_path.append(UNetUpBlock(prev_channels, (2**i)*wf, relu_slope, subnet_repeat_num, subspace_dim))
prev_channels = (2**i)*wf
subnet_repeat_num += 1
self.last = conv3x3(prev_channels, in_chn, bias=True)
#self._initialize()
def forward(self, x1):
blocks = []
for i, down in enumerate(self.down_path):
# print(x1.shape)
if (i+1) < self.depth:
x1, x1_up = down(x1)
blocks.append(x1_up)
else:
x1 = down(x1)
# print(x1.shape)
# x1 = self.ema(x1)
for i, up in enumerate(self.up_path):
# print(x1.shape, blocks[-i-1].shape)
x1 = up(x1, blocks[-i-1])
pred = self.last(x1)
return pred
def get_input_chn(self, in_chn):
return in_chn
def _initialize(self):
gain = nn.init.calculate_gain('leaky_relu', 0.20)
for m in self.modules():
if isinstance(m, nn.Conv2d):
print("weight")
nn.init.xavier_uniform_(m.weight)
if m.bias is not None:
print("bias")
nn.init.zeros_(m.bias)
class UNetConvBlock(nn.Module):
def __init__(self, in_size, out_size, downsample, relu_slope):
super(UNetConvBlock, self).__init__()
self.block = nn.Sequential(
nn.Conv2d(in_size, out_size, kernel_size=3, padding=1, bias=True),
nn.LeakyReLU(relu_slope),
nn.Conv2d(out_size, out_size, kernel_size=3, padding=1, bias=True),
nn.LeakyReLU(relu_slope))
self.downsample = downsample
if downsample:
self.downsample = conv_down(out_size, out_size, bias=False)
self.shortcut = nn.Conv2d(in_size, out_size, kernel_size=1, bias=True)
def forward(self, x):
out = self.block(x)
sc = self.shortcut(x)
out = out + sc
if self.downsample:
out_down = self.downsample(out)
return out_down, out
else:
return out
class UNetUpBlock(nn.Module):
def __init__(self, in_size, out_size, relu_slope, subnet_repeat_num, subspace_dim=16):
super(UNetUpBlock, self).__init__()
self.up = nn.ConvTranspose2d(in_size, out_size, kernel_size=2, stride=2, bias=True)
self.conv_block = UNetConvBlock(in_size, out_size, False, relu_slope)
self.num_subspace = subspace_dim
print(self.num_subspace, subnet_repeat_num)
self.subnet = Subspace(in_size, self.num_subspace)
self.skip_m = skip_blocks(out_size, out_size, subnet_repeat_num)
def forward(self, x, bridge):
up = self.up(x)
bridge = self.skip_m(bridge)
out = F.concat([up, bridge], 1)
if self.subnet:
b_, c_, h_, w_ = bridge.shape
sub = self.subnet(out)
V_t = sub.reshape(b_, self.num_subspace, h_*w_)
V_t = V_t / (1e-6 + F.abs(V_t).sum(axis=2, keepdims=True))
V = V_t.transpose(0, 2, 1)
mat = F.matmul(V_t, V)
mat_inv = F.matinv(mat)
project_mat = F.matmul(mat_inv, V_t)
bridge_ = bridge.reshape(b_, c_, h_*w_)
project_feature = F.matmul(project_mat, bridge_.transpose(0, 2, 1))
bridge = F.matmul(V, project_feature).transpose(0, 2, 1).reshape(b_, c_, h_, w_)
out = F.concat([up, bridge], 1)
out = self.conv_block(out)
return out
class Subspace(nn.Module):
def __init__(self, in_size, out_size):
super(Subspace, self).__init__()
self.blocks = []
self.blocks.append(UNetConvBlock(in_size, out_size, False, 0.2))
self.shortcut = nn.Conv2d(in_size, out_size, kernel_size=1, bias=True)
def forward(self, x):
sc = self.shortcut(x)
for i in range(len(self.blocks)):
x = self.blocks[i](x)
return x + sc
class skip_blocks(nn.Module):
def __init__(self, in_size, out_size, repeat_num=1):
super(skip_blocks, self).__init__()
self.blocks = []
self.re_num = repeat_num
mid_c = 128
self.blocks.append(UNetConvBlock(in_size, mid_c, False, 0.2))
for i in range(self.re_num - 2):
self.blocks.append(UNetConvBlock(mid_c, mid_c, False, 0.2))
self.blocks.append(UNetConvBlock(mid_c, out_size, False, 0.2))
self.shortcut = nn.Conv2d(in_size, out_size, kernel_size=1, bias=True)
def forward(self, x):
sc = self.shortcut(x)
for m in self.blocks:
x = m(x)
return x + sc
if __name__ == "__main__":
import numpy as np
a = UNetD(3)
#print(a)
im = mge.tensor(np.random.randn(1, 3, 128, 128).astype(np.float32))
print(a(im))