-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
293 lines (215 loc) · 7.87 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import torch
from torch import Tensor
from torch.nn import (
Module,
Sequential,
Conv2d,
Upsample,
PixelShuffle,
SiLU,
BatchNorm2d,
MaxPool2d,
AdaptiveAvgPool2d,
Identity,
Flatten,
Linear,
)
from torch.nn.utils.parametrizations import weight_norm
from torch.nn.utils.parametrize import remove_parametrizations
from huggingface_hub import PyTorchModelHubMixin
class SuperCool(Module, PyTorchModelHubMixin):
"""
A fast single-image super-resolution model with a deep low-resolution encoder network
and high-resolution sub-pixel convolutional decoder head with global residual pathway.
"""
def __init__(
self,
base_upscaler: str,
upscale_ratio: int,
num_channels: int,
hidden_ratio: int,
num_layers: int,
):
super().__init__()
if base_upscaler not in {"bilinear", "bicubic"}:
raise ValueError(
f"Base upscaler must be bilinear or bicubic, {base_upscaler} given."
)
if upscale_ratio not in {2, 4, 8}:
raise ValueError(
f"Upscale ratio must be either 2, 4, or 8, {upscale_ratio} given."
)
if num_channels < 1:
raise ValueError(
f"Num channels must be greater than 0, {num_channels} given."
)
if hidden_ratio not in {1, 2, 4}:
raise ValueError(
f"Hidden ratio must be either 1, 2, or 4, {hidden_ratio} given."
)
if num_layers < 1:
raise ValueError(f"Num layers must be greater than 0, {num_layers} given.")
self.input = weight_norm(Conv2d(3, num_channels, kernel_size=5, padding=2))
self.skip = Upsample(scale_factor=upscale_ratio, mode=base_upscaler)
self.encoder = Sequential(
*[EncoderBlock(num_channels, hidden_ratio) for _ in range(num_layers)]
)
self.decoder = SubpixelConv2d(
num_channels, upscale_ratio, kernel_size=3, padding=1
)
self.shuffle = PixelShuffle(upscale_ratio)
@property
def num_trainable_params(self) -> int:
return sum(param.numel() for param in self.parameters() if param.requires_grad)
def remove_weight_norms(self) -> None:
for module in self.modules():
if hasattr(module, "parametrizations"):
params = [name for name in module.parametrizations.keys()]
for name in params:
remove_parametrizations(module, name, leave_parametrized=True)
def forward(self, x: Tensor) -> Tensor:
z = self.input(x)
z = self.encoder(z)
z = self.decoder(z)
z = self.shuffle(z)
s = self.skip(x)
z += s # Global residual connection
z = torch.clamp(z, 0, 1)
return z
class EncoderBlock(Module):
"""A low-resolution encoder block with {num_channels} feature maps and wide activations."""
def __init__(self, num_channels: int, hidden_ratio: int):
super().__init__()
if hidden_ratio not in {1, 2, 4}:
raise ValueError(
f"Hidden ratio must be either 1, 2, or 4, {hidden_ratio} given."
)
hidden_channels = num_channels * hidden_ratio
conv1 = Conv2d(num_channels, hidden_channels, kernel_size=3, padding=1)
conv2 = Conv2d(hidden_channels, num_channels, kernel_size=3, padding=1)
self.conv1 = weight_norm(conv1)
self.conv2 = weight_norm(conv2)
self.silu = SiLU()
def forward(self, x: Tensor) -> Tensor:
z = self.conv1(x)
z = self.silu(z)
z = self.conv2(z)
z += x # Local residual connection
return z
class SubpixelConv2d(Module):
"""A sub-pixel (1 / upscale_ratio) convolution layer with weight normalization."""
def __init__(
self, num_channels: int, upscale_ratio: int, kernel_size: int, padding: int
):
super().__init__()
channels_out = 3 * upscale_ratio**2
conv = Conv2d(
num_channels, channels_out, kernel_size=kernel_size, padding=padding
)
self.conv = weight_norm(conv)
def forward(self, x: Tensor) -> Tensor:
return self.conv(x)
class Bouncer(Module):
"""A residual-style discriminator network for adversarial training."""
def __init__(self, model_size: str):
super().__init__()
if model_size not in {"small", "medium", "large"}:
raise ValueError(
f"Model size must be small, medium, or large, {model_size} given."
)
num_primary_layers = 3
num_quaternary_layers = 3
match model_size:
case "small":
num_secondary_layers = 4
num_tertiary_layers = 6
case "medium":
num_secondary_layers = 4
num_tertiary_layers = 23
case "large":
num_secondary_layers = 8
num_tertiary_layers = 36
self.input = Sequential(
Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False),
BatchNorm2d(64),
SiLU(),
MaxPool2d(3, stride=2, padding=1),
)
self.detector = Sequential(DetectorBlock(64, 64, 256))
self.detector.extend(
[DetectorBlock(256, 64, 256) for _ in range(num_primary_layers - 1)]
)
self.detector.append(DetectorBlock(256, 128, 512, stride=2))
self.detector.extend(
[DetectorBlock(512, 128, 512) for _ in range(num_secondary_layers - 1)]
)
self.detector.append(DetectorBlock(512, 256, 1024, stride=2))
self.detector.extend(
[DetectorBlock(1024, 256, 1024) for _ in range(num_tertiary_layers - 1)]
)
self.detector.append(DetectorBlock(1024, 512, 2048, stride=2))
self.detector.extend(
[DetectorBlock(2048, 512, 2048) for _ in range(num_quaternary_layers - 1)]
)
self.pool = AdaptiveAvgPool2d(1)
self.flatten = Flatten(start_dim=1)
self.linear = Linear(2048, 1)
@property
def num_trainable_params(self) -> int:
return sum(param.numel() for param in self.parameters() if param.requires_grad)
def forward(self, x: Tensor) -> Tensor:
x = self.input(x)
x = self.detector(x)
x = self.pool(x)
x = self.flatten(x)
x = self.linear(x)
return x
class DetectorBlock(Module):
"""A residual bottleneck block with 3x3 convolutions and batch normalization."""
def __init__(
self, channels_in: int, hidden_channels: int, channels_out: int, stride: int = 1
):
super().__init__()
self.residual = Sequential(
Conv2d(
in_channels=channels_in,
out_channels=hidden_channels,
kernel_size=1,
bias=False,
),
BatchNorm2d(hidden_channels),
SiLU(),
Conv2d(
in_channels=hidden_channels,
out_channels=hidden_channels,
kernel_size=3,
stride=stride,
padding=1,
),
SiLU(),
Conv2d(
in_channels=hidden_channels,
out_channels=channels_out,
kernel_size=1,
),
)
if channels_in == channels_out:
skip = Sequential(Identity())
else:
skip = Sequential(
Conv2d(
in_channels=channels_in,
out_channels=channels_out,
kernel_size=1,
stride=stride,
bias=False,
)
)
self.skip = skip
self.silu = SiLU()
def forward(self, x: Tensor) -> Tensor:
z = self.residual(x)
s = self.skip(x)
z += s
z = self.silu(z)
return z