-
Notifications
You must be signed in to change notification settings - Fork 0
/
espcn_ir_model.py
50 lines (41 loc) · 1.68 KB
/
espcn_ir_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
import math
import torch
from torch import nn, Tensor
class ESPCN(nn.Module):
def __init__(self, upscale_factor, in_channels = 1, out_channels = 1, channels = 64):
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