-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsrcnn_ir_model.py
87 lines (72 loc) · 2.99 KB
/
fsrcnn_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
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
# Copyright 2021 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.
# ============================================================================
"""Realize the model definition function."""
from math import sqrt
import torch
from torch import nn
class FSRCNN(nn.Module):
"""
Args:
upscale_factor (int): Image magnification factor.
"""
def __init__(self, upscale_factor: int) -> None:
super(FSRCNN, self).__init__()
# Feature extraction layer.
self.feature_extraction = nn.Sequential(
nn.Conv2d(1, 56, (5, 5), (1, 1), (2, 2)),
nn.PReLU(56)
)
# Shrinking layer.
self.shrink = nn.Sequential(
nn.Conv2d(56, 12, (1, 1), (1, 1), (0, 0)),
nn.PReLU(12)
)
# Mapping layer.
self.map = nn.Sequential(
nn.Conv2d(12, 12, (3, 3), (1, 1), (1, 1)),
nn.PReLU(12),
nn.Conv2d(12, 12, (3, 3), (1, 1), (1, 1)),
nn.PReLU(12),
nn.Conv2d(12, 12, (3, 3), (1, 1), (1, 1)),
nn.PReLU(12),
nn.Conv2d(12, 12, (3, 3), (1, 1), (1, 1)),
nn.PReLU(12)
)
# Expanding layer.
self.expand = nn.Sequential(
nn.Conv2d(12, 56, (1, 1), (1, 1), (0, 0)),
nn.PReLU(56)
)
# Deconvolution layer.
self.deconv = nn.ConvTranspose2d(56, 1, (9, 9), (upscale_factor, upscale_factor), (4, 4), (upscale_factor - 1, upscale_factor - 1))
# Initialize model weights.
self._initialize_weights()
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self._forward_impl(x)
# Support torch.script function.
def _forward_impl(self, x: torch.Tensor) -> torch.Tensor:
out = self.feature_extraction(x)
out = self.shrink(out)
out = self.map(out)
out = self.expand(out)
out = self.deconv(out)
return out
# The filter weight of each layer is a Gaussian distribution with zero mean and standard deviation initialized by random extraction 0.001 (deviation is 0).
def _initialize_weights(self) -> None:
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.normal_(m.weight.data, mean=0.0, std=sqrt(2 / (m.out_channels * m.weight.data[0][0].numel())))
nn.init.zeros_(m.bias.data)
nn.init.normal_(self.deconv.weight.data, mean=0.0, std=0.001)
nn.init.zeros_(self.deconv.bias.data)