-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy patharchitecture.py
132 lines (100 loc) · 4.65 KB
/
architecture.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import torch.nn as nn
from . import block as B
import torch
# For any upscale factors
class IMDN_AS(nn.Module):
def __init__(self, in_nc=3, nf=64, num_modules=6, out_nc=3, upscale=4):
super(IMDN_AS, self).__init__()
self.fea_conv = nn.Sequential(B.conv_layer(in_nc, nf, kernel_size=3, stride=2),
nn.LeakyReLU(0.05),
B.conv_layer(nf, nf, kernel_size=3, stride=2))
# IMDBs
self.IMDB1 = B.IMDModule(in_channels=nf)
self.IMDB2 = B.IMDModule(in_channels=nf)
self.IMDB3 = B.IMDModule(in_channels=nf)
self.IMDB4 = B.IMDModule(in_channels=nf)
self.IMDB5 = B.IMDModule(in_channels=nf)
self.IMDB6 = B.IMDModule(in_channels=nf)
self.c = B.conv_block(nf * num_modules, nf, kernel_size=1, act_type='lrelu')
self.LR_conv = B.conv_layer(nf, nf, kernel_size=3)
upsample_block = B.pixelshuffle_block
self.upsampler = upsample_block(nf, out_nc, upscale_factor=upscale)
def forward(self, input):
out_fea = self.fea_conv(input)
out_B1 = self.IMDB1(out_fea)
out_B2 = self.IMDB2(out_B1)
out_B3 = self.IMDB3(out_B2)
out_B4 = self.IMDB4(out_B3)
out_B5 = self.IMDB5(out_B4)
out_B6 = self.IMDB6(out_B5)
out_B = self.c(torch.cat([out_B1, out_B2, out_B3, out_B4, out_B5, out_B6], dim=1))
out_lr = self.LR_conv(out_B) + out_fea
output = self.upsampler(out_lr)
return output
class IMDN(nn.Module):
def __init__(self, in_nc=3, nf=64, num_modules=6, out_nc=3, upscale=4):
super(IMDN, self).__init__()
self.fea_conv = B.conv_layer(in_nc, nf, kernel_size=3)
# IMDBs
self.IMDB1 = B.IMDModule(in_channels=nf)
self.IMDB2 = B.IMDModule(in_channels=nf)
self.IMDB3 = B.IMDModule(in_channels=nf)
self.IMDB4 = B.IMDModule(in_channels=nf)
self.IMDB5 = B.IMDModule(in_channels=nf)
self.IMDB6 = B.IMDModule(in_channels=nf)
self.c = B.conv_block(nf * num_modules, nf, kernel_size=1, act_type='lrelu')
self.LR_conv = B.conv_layer(nf, nf, kernel_size=3)
upsample_block = B.pixelshuffle_block
self.upsampler = upsample_block(nf, out_nc, upscale_factor=upscale)
def forward(self, input):
out_fea = self.fea_conv(input)
out_B1 = self.IMDB1(out_fea)
out_B2 = self.IMDB2(out_B1)
out_B3 = self.IMDB3(out_B2)
out_B4 = self.IMDB4(out_B3)
out_B5 = self.IMDB5(out_B4)
out_B6 = self.IMDB6(out_B5)
out_B = self.c(torch.cat([out_B1, out_B2, out_B3, out_B4, out_B5, out_B6], dim=1))
out_lr = self.LR_conv(out_B) + out_fea
output = self.upsampler(out_lr)
return output
# AI in RTC Image Super-Resolution Algorithm Performance Comparison Challenge (Winner solution)
class IMDN_RTC(nn.Module):
def __init__(self, in_nc=3, nf=12, num_modules=5, out_nc=3, upscale=2):
super(IMDN_RTC, self).__init__()
fea_conv = [B.conv_layer(in_nc, nf, kernel_size=3)]
rb_blocks = [B.IMDModule_speed(in_channels=nf) for _ in range(num_modules)]
LR_conv = B.conv_layer(nf, nf, kernel_size=1)
upsample_block = B.pixelshuffle_block
upsampler = upsample_block(nf, out_nc, upscale_factor=upscale)
self.model = B.sequential(*fea_conv, B.ShortcutBlock(B.sequential(*rb_blocks, LR_conv)),
*upsampler)
def forward(self, input):
output = self.model(input)
return output
class IMDN_RTE(nn.Module):
def __init__(self, upscale=2, in_nc=3, nf=20, out_nc=3):
super(IMDN_RTE, self).__init__()
self.upscale = upscale
self.fea_conv = nn.Sequential(B.conv_layer(in_nc, nf, 3),
nn.ReLU(inplace=True),
B.conv_layer(nf, nf, 3, stride=2, bias=False))
self.block1 = IMDModule_Large(nf)
self.block2 = IMDModule_Large(nf)
self.block3 = IMDModule_Large(nf)
self.block4 = IMDModule_Large(nf)
self.block5 = IMDModule_Large(nf)
self.block6 = IMDModule_Large(nf)
self.LR_conv = B.conv_layer(nf, nf, 1, bias=False)
self.upsampler = B.pixelshuffle_block(nf, out_nc, upscale_factor=upscale**2)
def forward(self, input):
fea = self.fea_conv(input)
out_b1 = self.block1(fea)
out_b2 = self.block2(out_b1)
out_b3 = self.block3(out_b2)
out_b4 = self.block4(out_b3)
out_b5 = self.block5(out_b4)
out_b6 = self.block6(out_b5)
out_lr = self.LR_conv(out_b6) + fea
output = self.upsampler(out_lr)
return output