-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodel.py
More file actions
179 lines (134 loc) · 6.16 KB
/
model.py
File metadata and controls
179 lines (134 loc) · 6.16 KB
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
172
173
174
175
176
177
178
179
import torch
import torch.nn as nn
import torch.nn.utils.spectral_norm as SN
class DenseBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3):
super().__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, padding=(kernel_size-1)//2),
nn.ReLU(inplace=True)
)
def forward(self, x):
out = self.conv(x)
out = torch.cat((x, out), 1)
return out
class RDBlock(nn.Module):
def __init__(self, in_channels, num_dense, dense_out):
super().__init__()
out_channels = in_channels
layers = []
for i in range(num_dense):
layers.append(DenseBlock(out_channels, dense_out))
out_channels += dense_out
self.dense = nn.Sequential(*layers)
self.conv_fusion = nn.Conv2d(out_channels, in_channels, kernel_size=1, padding=0)
def forward(self, x):
out = self.dense(x)
out = self.conv_fusion(out)
return x + (0.1*out)
class NSRNet(nn.Module):
def __init__(self, c_img=3, n_feat=64, num_dense=5, dense_out=64, scale=4):
super().__init__()
self.conv1 = nn.Conv2d(c_img, n_feat, kernel_size=3, padding=1)
self.block1 = RDBlock(n_feat, num_dense, dense_out)
self.block2 = RDBlock(n_feat, num_dense, dense_out)
self.block3 = RDBlock(n_feat, num_dense, dense_out)
self.block4 = RDBlock(n_feat, num_dense, dense_out)
self.block5 = RDBlock(n_feat, num_dense, dense_out)
self.block6 = RDBlock(n_feat, num_dense, dense_out)
self.block7 = RDBlock(n_feat, num_dense, dense_out)
self.block8 = RDBlock(n_feat, num_dense, dense_out)
self.conv2 = nn.Conv2d(n_feat, n_feat, kernel_size=3, padding=1)
if scale == 4:
self.conv_up = nn.Sequential(
nn.Conv2d(n_feat, n_feat*scale//2*scale//2, kernel_size=3, padding=1),
nn.PixelShuffle(scale//2),
nn.Conv2d(n_feat, n_feat*scale//2*scale//2, kernel_size=3, padding=1),
nn.PixelShuffle(scale//2)
)
else:
self.conv_up = nn.Sequential(
nn.Conv2d(n_feat, n_feat*scale*scale, kernel_size=3, padding=1),
nn.PixelShuffle(scale)
)
self.conv_out = nn.Conv2d(n_feat, c_img, kernel_size=3, padding=1)
def forward(self, x):
out_1 = self.conv1(x)
rdb_out_1 = self.block1(out_1)
rdb_out_1 = self.block2(rdb_out_1)
rdb_out_1 = rdb_out_1 + out_1
rdb_out_2 = self.block3(rdb_out_1)
rdb_out_2 = self.block4(rdb_out_2)
rdb_out_2 = rdb_out_2 + rdb_out_1 + out_1
rdb_out_3 = self.block5(rdb_out_2)
rdb_out_3 = self.block6(rdb_out_3)
rdb_out_3 = rdb_out_3 + rdb_out_2
rdb_out_4 = self.block7(rdb_out_3)
rdb_out_4 = self.block8(rdb_out_4)
rdb_out_4 = rdb_out_4 + rdb_out_3 + rdb_out_2 + out_1
out_2 = self.conv2(rdb_out_4)
out_2 = out_2 + out_1
up_out = self.conv_up(out_2)
out = self.conv_out(up_out)
return out
class Discriminator(nn.Module):
def __init__(self, c_img=3, n_feat=64):
super().__init__()
self.discriminator = nn.Sequential(
SN(nn.Conv2d(c_img, n_feat, kernel_size=3, padding=1)),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
SN(nn.Conv2d(n_feat, n_feat, kernel_size=3, stride=2, padding=1)),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
SN(nn.Conv2d(n_feat, n_feat*2, kernel_size=3, padding=1)),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
SN(nn.Conv2d(n_feat*2, n_feat*2, kernel_size=3, stride=2, padding=1)),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
SN(nn.Conv2d(n_feat*2, n_feat*4, kernel_size=3, padding=1)),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
SN(nn.Conv2d(n_feat*4, n_feat*4, kernel_size=3, stride=2, padding=1)),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
SN(nn.Conv2d(n_feat*4, n_feat*8, kernel_size=3, padding=1)),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
SN(nn.Conv2d(n_feat*8, n_feat*8, kernel_size=3, stride=2, padding=1)),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
SN(nn.Conv2d(n_feat*8, n_feat*16, kernel_size=3, padding=1)),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
SN(nn.Conv2d(n_feat*16, n_feat*16, kernel_size=3, stride=2, padding=1)),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
SN(nn.Conv2d(n_feat*16, 1, kernel_size=3, padding=1)),
nn.AdaptiveAvgPool2d(1)
)
def forward(self, x):
out = self.discriminator(x)
return out
class NMDiscriminator(nn.Module):
def __init__(self, c_img=3, n_feat=64):
super().__init__()
self.discriminator = nn.Sequential(
nn.Conv2d(c_img, n_feat, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(n_feat, n_feat, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(n_feat, n_feat*2, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(n_feat*2, n_feat*2, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(n_feat*2, n_feat*4, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(n_feat*4, n_feat*4, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(n_feat*4, n_feat*8, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(n_feat*8, n_feat*8, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(n_feat*8, 1, kernel_size=3, padding=1),
nn.AdaptiveAvgPool2d(1),
nn.Sigmoid()
)
def forward(self, x):
out = self.discriminator(x)
return out