-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedsr_ir_model.py
110 lines (86 loc) · 3.66 KB
/
edsr_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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
class ResidualConvBlock(nn.Module):
def __init__(self, channels: int) -> None:
super(ResidualConvBlock, self).__init__()
self.rcb = nn.Sequential(
nn.Conv2d(channels, channels, (3, 3), (1, 1), (1, 1)),
nn.ReLU(True),
nn.Conv2d(channels, channels, (3, 3), (1, 1), (1, 1)),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
identity = x
out = self.rcb(x)
out = torch.mul(out, 0.1)
out = torch.add(out, identity)
return out
class UpsampleBlock(nn.Module):
def __init__(self, channels: int, upscale_factor: int) -> None:
super(UpsampleBlock, self).__init__()
self.upsample_block = nn.Sequential(
nn.Conv2d(channels, channels * upscale_factor * upscale_factor, (3, 3), (1, 1), (1, 1)),
nn.PixelShuffle(upscale_factor),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
out = self.upsample_block(x)
return out
class EDSR(nn.Module):
def __init__(self, upscale_factor: int) -> None:
super(EDSR, self).__init__()
# First layer
self.conv1 = nn.Conv2d(1, 64, (3, 3), (1, 1), (1, 1))
# Residual blocks
trunk = []
for _ in range(16):
trunk.append(ResidualConvBlock(64))
self.trunk = nn.Sequential(*trunk)
# Second layer
self.conv2 = nn.Conv2d(64, 64, (3, 3), (1, 1), (1, 1))
# Upsampling layers
upsampling = []
if upscale_factor == 2 or upscale_factor == 4:
for _ in range(int(math.log(upscale_factor, 2))):
upsampling.append(UpsampleBlock(64, 2))
elif upscale_factor == 3:
upsampling.append(UpsampleBlock(64, 3))
self.upsampling = nn.Sequential(*upsampling)
# Final output layer
self.conv3 = nn.Conv2d(64, 1, (3, 3), (1, 1), (1, 1))
# self.register_buffer("mean", torch.Tensor([0.4488, 0.4371, 0.4040]).view(1, 3, 1, 1))
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:
# # The images by subtracting the mean RGB value of the DIV2K dataset.
# out = x.sub_(self.mean).mul_(255.)
# out1 = self.conv1(out)
out1 = self.conv1(x)
out = self.trunk(out1)
out = self.conv2(out)
out = torch.add(out, out1)
out = self.upsampling(out)
out = self.conv3(out)
# out = out.div_(255.).add_(self.mean)
return out
def _initialize_weights(self) -> None:
for module in self.modules():
if isinstance(module, nn.Conv2d):
nn.init.kaiming_normal_(module.weight)
if module.bias is not None:
nn.init.constant_(module.bias, 0)
elif isinstance(module, nn.BatchNorm2d):
nn.init.constant_(module.weight, 1)