-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
executable file
·99 lines (76 loc) · 2.91 KB
/
model.py
File metadata and controls
executable file
·99 lines (76 loc) · 2.91 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
# Copyright 2020 Dakewe Biotech Corporation. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import math
import torch
from torch import nn, Tensor
__all__ = [
"ESPCN",
"espcn_x2", "espcn_x3", "espcn_x4", "espcn_x8",
]
class ESPCN(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
channels: int,
upscale_factor: int,
) -> None:
super(ESPCN, self).__init__()
hidden_channels = channels // 2
out_channels = int(out_channels * (upscale_factor ** 2))
# Feature mapping
self.feature_maps = nn.Sequential(
nn.Conv2d(in_channels, channels, (5, 5), (1, 1), (2, 2)),
nn.Tanh(),
nn.Conv2d(channels, hidden_channels, (3, 3), (1, 1), (1, 1)),
nn.Tanh(),
)
# Sub-pixel convolution layer
self.sub_pixel = nn.Sequential(
nn.Conv2d(hidden_channels, out_channels, (3, 3), (1, 1), (1, 1)),
nn.PixelShuffle(upscale_factor),
)
# Initial model weights
for module in self.modules():
if isinstance(module, nn.Conv2d):
if module.in_channels == 32:
nn.init.normal_(module.weight.data,
0.0,
0.001)
nn.init.zeros_(module.bias.data)
else:
nn.init.normal_(module.weight.data,
0.0,
math.sqrt(2 / (module.out_channels * module.weight.data[0][0].numel())))
nn.init.zeros_(module.bias.data)
def forward(self, x: Tensor) -> Tensor:
return self._forward_impl(x)
# Support torch.script function.
def _forward_impl(self, x: Tensor) -> Tensor:
x = self.feature_maps(x)
x = self.sub_pixel(x)
x = torch.clamp_(x, 0.0, 1.0)
return x
def espcn_x2(**kwargs) -> ESPCN:
model = ESPCN(upscale_factor=2, **kwargs)
return model
def espcn_x3(**kwargs) -> ESPCN:
model = ESPCN(upscale_factor=3, **kwargs)
return model
def espcn_x4(**kwargs) -> ESPCN:
model = ESPCN(upscale_factor=4, **kwargs)
return model
def espcn_x8(**kwargs) -> ESPCN:
model = ESPCN(upscale_factor=8, **kwargs)
return model