-
Notifications
You must be signed in to change notification settings - Fork 4
/
edsr.py
54 lines (39 loc) · 1.63 KB
/
edsr.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
from typing import Any
import torch.nn as nn
from .common import DefaultConv2d, MeanShift, ResBlock, UpscaleBlock
from .srmodel import SRModel
class EDSR(SRModel):
"""
LightningModule for EDSR, https://openaccess.thecvf.com/content_cvpr_2017_workshops/w12/papers/Lim_Enhanced_Deep_Residual_CVPR_2017_paper.pdf.
"""
def __init__(self, n_feats: int=64, n_resblocks: int=16, res_scale: int=1, **kwargs: dict[str, Any]):
super(EDSR, self).__init__(**kwargs)
kernel_size = 3
if self._channels == 3:
self.sub_mean = MeanShift()
self.add_mean = MeanShift(sign=1)
m_head = [DefaultConv2d(in_channels=self._channels,
out_channels=n_feats, kernel_size=kernel_size)]
m_body = [
ResBlock(n_feats=n_feats, kernel_size=kernel_size, res_scale=res_scale) for _ in range(n_resblocks)
]
m_body.append(DefaultConv2d(in_channels=n_feats,
out_channels=n_feats, kernel_size=kernel_size))
m_tail = [
UpscaleBlock(self._scale_factor, n_feats),
DefaultConv2d(in_channels=n_feats, out_channels=self._channels,
kernel_size=kernel_size)
]
self.head = nn.Sequential(*m_head)
self.body = nn.Sequential(*m_body)
self.tail = nn.Sequential(*m_tail)
def forward(self, x):
if self._channels == 3:
x = self.sub_mean(x)
x = self.head(x)
res = self.body(x)
res += x
x = self.tail(res)
if self._channels == 3:
x = self.add_mean(x)
return x