-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
175 lines (159 loc) · 7.07 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*- coding: utf-8 -*-
# Author: Molia Chen
import torch
import torch.nn as nn
from collections import OrderedDict
class Encoder(nn.Module):
def __init__(self, cr):
super().__init__()
self.input_ = nn.Sequential(
nn.Conv2d(2, 2, kernel_size=(7, 7), stride=1, padding=3, bias=False),
nn.BatchNorm2d(2),
nn.LeakyReLU(0.1),
nn.Conv2d(2, 2, kernel_size=(7, 7), stride=1, padding=3, bias=False),
nn.BatchNorm2d(2),
nn.LeakyReLU(0.1)
)
self.output = nn.Sequential(
nn.Linear(896, int(2048/cr))
)
def forward(self, data):
out = self.input_(data)
out = out.flatten(start_dim=1)
out = self.output(out)
return out
class BRMBlock(nn.Module):
def __init__(self, cr, padding_2, last_block=False):
super().__init__()
self.last_block = last_block
if padding_2:
self.up_sample_block = nn.Sequential(OrderedDict([
('conv_transpose1', nn.ConvTranspose2d(64, 64, kernel_size=3, stride=2, padding=2, output_padding=1))
]))
elif cr==4:
self.up_sample_block = nn.Sequential(OrderedDict([
('conv_transpose1', nn.ConvTranspose2d(64, 64, kernel_size=1, stride=1))
]))
else:
self.up_sample_block = nn.Sequential(OrderedDict([
('conv_transpose1', nn.ConvTranspose2d(64, 64, kernel_size=3, stride=2, output_padding=1))
]))
self.SR_flow = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, bias=False)),
('bn1', nn.BatchNorm2d(64)),
('prelu1', nn.PReLU()),
('conv2', nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, bias=False)),
('bn1', nn.BatchNorm2d(64)),
('prelu2', nn.PReLU()),
('conv3', nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, bias=False)),
]))
if not last_block:
if padding_2:
self.down_sample = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=2, bias=False))
]))
elif cr==4:
self.down_sample = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(64, 64, kernel_size=1, stride=1, bias=False))
]))
else:
self.down_sample = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(64, 64, kernel_size=3, stride=2, bias=False))
]))
self.back_projection = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, bias=False)),
('bn1', nn.BatchNorm2d(64)),
('prelu1', nn.PReLU()),
('conv2', nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, bias=False)),
('bn2', nn.BatchNorm2d(64)),
('prelu2', nn.PReLU()),
('conv3', nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, bias=False)),
]))
def forward(self, data):
up_sample = self.up_sample_block(data)
sr_flow_output = self.SR_flow(up_sample)
if not self.last_block:
down_sample = self.down_sample(sr_flow_output)
subtraction = data - down_sample
back_projection_output = self.back_projection(subtraction)
back_projection_output += subtraction
return sr_flow_output, back_projection_output
return sr_flow_output
class EBRM(nn.Module):
def __init__(self, cr, block_num, padding_2):
super().__init__()
self.block_num = block_num
self.ebrm = nn.ModuleList([BRMBlock(cr, padding_2) for _ in range(block_num-1)])
self.ebrm.append(BRMBlock(cr, padding_2, last_block=True))
self.fusion_function = nn.ModuleList([nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, bias=False)
for _ in range(block_num-1)])
def forward(self, data):
out = data
sr_flow = []
for m in self.ebrm[:-1]:
sr_flow_out, out = m(out)
sr_flow.append(sr_flow_out)
sr_flow.append(self.ebrm[-1](out))
sr_flow = sr_flow[::-1]
fusion_out = sr_flow[0]
sr_flow_out_hat = []
sr_flow_out_hat.append(fusion_out)
for d, m in zip(sr_flow[1:], self.fusion_function):
fusion_out = m(d + fusion_out)
sr_flow_out_hat.append(fusion_out)
output = torch.cat(sr_flow_out_hat[::-1], dim=1)
return output
class Decoder(nn.Module):
def __init__(self, cr, low_resolution, padding_2=False, EBRM_block=4):
super().__init__()
self.low_resolution = low_resolution
self.linear = nn.Linear(int(2048/cr), int(low_resolution[0]*low_resolution[1]*low_resolution[2]))
self.features = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(low_resolution[0], 64, kernel_size=3, stride=1, padding=1, bias=False)),
]))
self.ebrm = EBRM(cr=cr, block_num=EBRM_block, padding_2=padding_2)
self.concatmodule = nn.Conv2d(64*EBRM_block, 64, kernel_size=3, stride=1, padding=1, bias=False)
self.middle_path = nn.Sequential(
nn.Conv2d(64, 32, kernel_size=3, stride=1, padding=1, bias=False),
nn.PReLU(),
nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1, bias=False),
nn.PReLU(),
nn.Conv2d(32, 2, kernel_size=3, stride=1, padding=1, bias=False),
)
self.out = nn.Sequential(
nn.Conv2d(2, 2, kernel_size=1, stride=1, bias=False),
nn.Sigmoid()
)
def forward(self, data):
data = self.linear(data)
lr_input = data.reshape(-1, *self.low_resolution)
feature = self.features(lr_input)
sr_flow_output = self.ebrm(feature)
concat_out = self.concatmodule(sr_flow_output)
middle_out = self.middle_path(concat_out)
output = self.out(middle_out)
return output
class AutoEncoder(nn.Module):
def __init__(self, cr, EBRM_block):
super().__init__()
padding_2 = False
if cr >= 32:
low_resolution = (1, 15, 6)
elif cr == 16:
low_resolution = (2, 15, 6)
elif cr==4:
low_resolution = (2, 32, 14)
else:
low_resolution = (2, 17, 8)
padding_2 = True
self.encoder = Encoder(cr=cr)
self.decoder = Decoder(cr=cr, low_resolution=low_resolution, padding_2=padding_2, EBRM_block=EBRM_block)
def forward(self, data):
out = self.encoder(data)
out = self.decoder(out)
return out
def load_network(scenario, cr):
assert scenario in ['indoor', 'outdoor'], "The scenario is not exist!"
assert cr in [4, 8, 16, 32, 64], "The compression ratio is not exist!"
model = AutoEncoder(cr, 6)
return model