-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generator.py
152 lines (134 loc) · 5.72 KB
/
Generator.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
import torch
import torch.nn as nn
from Layer import DSC2_PLUS as DP
from Layer import UP_DSC_PLUS as UpDP
from Layer import C_DSC_PLUS as CDP
from torch.nn.utils.parametrizations import spectral_norm
class Generator(nn.Module):
def __init__(
self,
in_step: int=4,
):
super().__init__()
self.in_step = in_step
self.DP0 = nn.ModuleList([DP(4, 64) for _ in range(2)])
self.DP1 = nn.ModuleList([DP(64,128) for _ in range(2)])
self.DP2 = nn.ModuleList([DP(128,256) for _ in range(2)])
self.DP3 = nn.ModuleList([DP(256,512) for _ in range(2)])
self.DP4 = DP(512,512)
self.conv0 = nn.Sequential(
# spectral_norm(nn.Conv2d(192, 64, 3, 1, 1)),
spectral_norm(nn.Conv2d(128, 128, 3, 1, 1, groups=128)),
spectral_norm(nn.Conv2d(128, 64, 1, 1, 0)),
nn.LeakyReLU(0.2,inplace=True)
)
self.conv1 = nn.Sequential(
# spectral_norm(nn.Conv2d(384, 128, 3, 1, 1)),
spectral_norm(nn.Conv2d(256, 256, 3, 1, 1, groups=256)),
spectral_norm(nn.Conv2d(256, 128, 1, 1, 0)),
nn.LeakyReLU(0.2,inplace=True)
)
self.conv2 = nn.Sequential(
# spectral_norm(nn.Conv2d(768, 256, 3, 1, 1)),
spectral_norm(nn.Conv2d(512, 512, 3, 1, 1, groups=512)),
spectral_norm(nn.Conv2d(512, 256, 1, 1, 0)),
nn.LeakyReLU(0.2,inplace=True)
)
self.conv3 = nn.Sequential(
# spectral_norm(nn.Conv2d(1536, 512, 3, 1, 1)),
spectral_norm(nn.Conv2d(1024, 1024, 3, 1, 1, groups=1024)),
spectral_norm(nn.Conv2d(1024, 512, 1, 1, 0)),
nn.LeakyReLU(0.2,inplace=True)
)
self.scaling = nn.AvgPool2d(2)
#0layer:64x128x128 / 1layer:128x64x64 / 2layer:256x32x32 / 3layer:512x16x16 / 4layer:1024x8x8
self.trans23 = nn.Sequential(
nn.AvgPool2d(2),
spectral_norm(nn.Conv2d(256,256,3,1,1,groups=256)),
spectral_norm(nn.Conv2d(256,128,1,1,0)),
nn.LeakyReLU(0.2,inplace=True)
)
self.trans33 = nn.Sequential(
spectral_norm(nn.Conv2d(512,512,3,1,1,groups=512)),
spectral_norm(nn.Conv2d(512,128,1,1,0)),
nn.LeakyReLU(0.2,inplace=True)
)
self.trans12 = nn.Sequential(
nn.AvgPool2d(2),
spectral_norm(nn.Conv2d(128,128,3,1,1,groups=128)),
spectral_norm(nn.Conv2d(128,128,1,1,0)),
nn.LeakyReLU(0.2,inplace=True)
)
self.trans22 = nn.Sequential(
spectral_norm(nn.Conv2d(256,256,3,1,1,groups=256)),
spectral_norm(nn.Conv2d(256,128,1,1,0)),
nn.LeakyReLU(0.2,inplace=True)
)
self.trans01 = nn.Sequential(
nn.AvgPool2d(2),
spectral_norm(nn.Conv2d(64,64,3,1,1,groups=64)),
spectral_norm(nn.Conv2d(64,128,1,1,0)),
nn.LeakyReLU(0.2,inplace=True)
)
self.trans11 = nn.Sequential(
spectral_norm(nn.Conv2d(128,128,3,1,1,groups=128)),
spectral_norm(nn.Conv2d(128,128,1,1,0)),
nn.LeakyReLU(0.2,inplace=True)
)
self.trans001 = DP(64,128)
self.trans002 = DP(1,128)
self.CDP4 = CDP(512, 128)
self.CDPa = CDP(384, 128)
self.CDPb = CDP(128, 128)
self.CDP0 = CDP(384, 64)
self.CDP_up = UpDP(128, 128)
self.conv_out = nn.Sequential(
nn.Conv2d(64,64,1,1,0),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 1, 1, 1, 0)
)
def forward(self, input):
temp_x0, temp_x1, temp_x2, temp_x3= [], [], [], []
for block0, block1, block2, block3 in zip(self.DP0, self.DP1, self.DP2, self.DP3):
d0 = block0(input)
d1 = block1(self.scaling(d0))
d2 = block2(self.scaling(d1))
d3 = block3(self.scaling(d2))
temp_x0.append(d0)
temp_x1.append(d1)
temp_x2.append(d2)
temp_x3.append(d3)
x0 = torch.cat(temp_x0, dim=1)
x1 = torch.cat(temp_x1, dim=1)
x2 = torch.cat(temp_x2, dim=1)
x3 = torch.cat(temp_x3, dim=1)
del temp_x0, temp_x1, temp_x2, temp_x3
f0 = self.conv0(x0) # 64x128x128
f1 = self.conv1(x1) # 128x64x64
f2 = self.conv2(x2) # 256x32x32
f3 = self.conv3(x3) # 512x16x16
f4 = self.DP4(self.scaling(f3)) # 512x8x8
f23 = self.trans23(f2)
f33 = self.trans33(f3)
f12 = self.trans12(f1)
f22 = self.trans22(f2)
f01 = self.trans01(f0)
f11 = self.trans11(f1)
f001 = self.trans001(f0)
f002 = self.trans002(input[:,-1].unsqueeze(1))
post_f4 = self.CDPb(self.CDP4(f4))
f43 = self.CDP_up(post_f4)
concat_f3 = torch.cat((f33,f43,f23),dim=1)
post_f3 = self.CDPb(self.CDPa(concat_f3))
f32 = self.CDP_up(post_f3)
concat_f2 = torch.cat((f22,f32,f12),dim=1)
post_f2 = self.CDPb(self.CDPa(concat_f2))
f21 = self.CDP_up(post_f2)
concat_f1 = torch.cat((f11,f21,f01),dim=1)
post_f1 = self.CDPb(self.CDPa(concat_f1))
f10 = self.CDP_up(post_f1)
concat_f0 = torch.cat((f001,f10,f002),dim=1)
post_f0 = self.CDP0(concat_f0)
last = self.conv_out(post_f0)
return last