forked from Lornatang/VDSR-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
73 lines (57 loc) · 2.32 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
# 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 ConvReLU(nn.Module):
def __init__(self, channels: int) -> None:
super(ConvReLU, self).__init__()
self.conv = nn.Conv2d(channels, channels, (3, 3), (1, 1), (1, 1), bias=False)
self.relu = nn.ReLU(True)
def forward(self, x: torch.Tensor) -> torch.Tensor:
out = self.conv(x)
out = self.relu(out)
return out
class VDSR(nn.Module):
def __init__(self) -> None:
super(VDSR, self).__init__()
# Input layer
self.conv1 = nn.Sequential(
nn.Conv2d(1, 64, (3, 3), (1, 1), (1, 1), bias=False),
nn.ReLU(True),
)
# Features trunk blocks
trunk = []
for _ in range(18):
trunk.append(ConvReLU(64))
self.trunk = nn.Sequential(*trunk)
# Output layer
self.conv2 = nn.Conv2d(64, 1, (3, 3), (1, 1), (1, 1), bias=False)
# 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:
identity = x
out = self.conv1(x)
out = self.trunk(out)
out = self.conv2(out)
out = torch.add(out, identity)
return out
def _initialize_weights(self) -> None:
for module in self.modules():
if isinstance(module, nn.Conv2d):
module.weight.data.normal_(0.0, sqrt(2 / (module.kernel_size[0] * module.kernel_size[1] * module.out_channels)))