forked from calum-green/OpenLSR-X
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmodels.py
More file actions
145 lines (121 loc) · 3.9 KB
/
submodels.py
File metadata and controls
145 lines (121 loc) · 3.9 KB
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
"""
This model contains all of submodels such
as the Generator and Discriminator
"""
import pytorch_lightning as pl
from torch import nn
from blocks import ConvBlock, UpsampleBlock, ResidualBlock
import math
from loss import custom_sigmoid
class Generator(pl.LightningModule):
"""
Describes the whole Generator Network as described in SRGAN paper
https://arxiv.org/abs/1609.04802v5
in_channels = 1 (grayscale)
out_channels = 64 (from paper)
res_blocks = 16 (from paper)
"""
def __init__(
self,
in_channels=1,
out_channels=64,
num_blocks=10,
super_res_factor=4,
sigmoid_k=1.0,
):
super().__init__()
self.sigmoid_k = float(sigmoid_k)
self.initial = ConvBlock(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=9,
stride=1,
padding=4,
padding_mode="reflect",
use_bn=False,
)
self.res_blocks = nn.Sequential(
*[ResidualBlock(out_channels) for block in range(num_blocks)]
)
# code above creates a list of length num_blocks of ResidualBlocks
# these are then sequentially executed
self.last_res = ConvBlock(
in_channels=out_channels,
out_channels=out_channels,
kernel_size=3,
stride=1,
padding=1,
padding_mode="reflect",
use_act=False,
)
self.num_upsamples = [i for i in range(int(math.log2(super_res_factor)))]
self.upsample = nn.Sequential(
*[
UpsampleBlock(in_channels=out_channels, scale_factor=2)
for factor in self.num_upsamples
]
)
self.final_conv = ConvBlock(
in_channels=out_channels,
out_channels=in_channels,
kernel_size=9,
stride=1,
padding=4,
padding_mode="reflect",
use_bn=False,
use_act=False,
)
def forward(self, x):
initial = self.initial(x)
out = self.res_blocks(initial)
out = self.last_res(out)
out = out + initial
out = self.upsample(out)
out = self.final_conv(out)
# return torch.sigmoid(out)
return custom_sigmoid(out, k=self.sigmoid_k)
class Discriminator(pl.LightningModule):
"""
This class describes the Discriminator architecture as
described in paper https://arxiv.org/abs/1609.04802v5
"""
def __init__(
self,
in_channels=1,
features=[64, 64, 128, 128, 256, 256, 512, 512],
adaptive_size=6,
):
super().__init__()
disc_blocks = []
for idx, feature in enumerate(features):
disc_blocks.append(
ConvBlock(
in_channels=in_channels,
out_channels=feature,
discriminator=True,
kernel_size=3,
stride=1 + idx % 2,
use_act=True,
use_bn=False if idx == 0 else True,
)
)
in_channels = feature
self.disc_blocks = nn.Sequential(*disc_blocks)
self.highest_feature = features[-1]
self.adaptive_size = int(adaptive_size)
self.dense = nn.Sequential(
nn.AdaptiveAvgPool2d((self.adaptive_size, self.adaptive_size)),
nn.Flatten(),
nn.Linear(
self.highest_feature * self.adaptive_size * self.adaptive_size,
self.highest_feature * 2,
),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(self.highest_feature * 2, 1),
)
def forward(self, x):
out = self.disc_blocks(x)
out = self.dense(out)
# note: BCEWithLogitsLoss includes a sigmoid layer
# so do not apply sigmoid here
return out