-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
200 lines (154 loc) · 6.3 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
def delete_zero(tensor):
zero = torch.zeros_like(tensor)
index = tensor == zero
return tensor[~index].unsqueeze(0)
def mean_activation(tensor):
tensor_delete = delete_zero(tensor)
mean_value = tensor_delete.mean(dim=1, keepdim=True)
tensor_hat = tensor - mean_value
active_tensor = torch.nn.Sigmoid()(tensor_hat)
return active_tensor
class SRMLayer(nn.Module):
def __init__(self, channel):
super(SRMLayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel // 16, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channel // 16, channel, bias=False),
nn.Sigmoid()
)
self.conv1d = nn.Conv1d(in_channels=channel,out_channels=channel,kernel_size=2,stride=1)
self.activation = nn.Sigmoid()
def channel_attention(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y.expand_as(x)
def _style_pooling(self, x, eps=1e-5):
N, C, _, _ = x.size()
channel_mean = x.view(N, C, -1).mean(dim=2, keepdim=True) # N x C x 1
channel_var = x.view(N, C, -1).var(dim=2, keepdim=True) + eps # N x C x 1
channel_std = channel_var.sqrt()
t = torch.cat((channel_mean, channel_std), dim=2) # N x C x 2
return t
def _style_integration(self, t):
z = self.conv1d(t) # N x C x 1
z_hat = z.unsqueeze(2) # N x C x 1 x 1
g = self.activation(z_hat) # N x C x 1 x 1
return g
def forward(self, x):
# N x C x H x W
se = self.channel_attention(x)
# N x C x 2
t = self._style_pooling(se)
# N x C x 1 x 1
g = self._style_integration(t)
return x * g
class SRMConvBlock(nn.Module):
def __init__(self, in_features):
super(SRMConvBlock, self).__init__()
conv_block = [nn.ReflectionPad2d(1),
nn.Conv2d(in_features, in_features, 3),
nn.InstanceNorm2d(in_features),
nn.ReLU(inplace=True),
nn.ReflectionPad2d(1),
nn.Conv2d(in_features, in_features, 3),
nn.InstanceNorm2d(in_features)]
self.conv_block = nn.Sequential(*conv_block)
self.srm_layer = SRMLayer(in_features)
def forward(self, x):
t = x
t_conv = self.conv_block(t)
r = t_conv
r_srm = self.srm_layer(r)
return x + r_srm
class SRMResidualLayer(nn.Module):
def __init__(self, in_features):
super(SRMResidualLayer, self).__init__()
self.srm_layer = SRMLayer(in_features)
def forward(self, x):
t = x
r_srm = self.srm_layer(t)
return x + r_srm
class Generator(nn.Module):
def __init__(self, input_nc, output_nc, n_residual_blocks=9):
super(Generator, self).__init__()
# Initial convolution block
model = [ nn.ReflectionPad2d(3),
nn.Conv2d(input_nc, 64, 7),
nn.InstanceNorm2d(64),
nn.ReLU(inplace=True) ]
# Downsampling
in_features = 64
out_features = in_features*2
for _ in range(2):
model += [ nn.Conv2d(in_features, out_features, 3, stride=2, padding=1),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True) ]
in_features = out_features
out_features = in_features*2
# Residual blocks
for _ in range(n_residual_blocks):
model += [SRMConvBlock(in_features)]
# Upsampling
out_features = in_features//2
for _ in range(2):
model += [ nn.ConvTranspose2d(in_features, out_features, 3, stride=2, padding=1, output_padding=1),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True) ]
in_features = out_features
out_features = in_features//2
# Output layer
model += [ nn.ReflectionPad2d(3),
nn.Conv2d(64, output_nc, 7),
nn.Tanh() ]
self.model = nn.Sequential(*model)
def forward(self, x):
return self.model(x)
class StyleDiscriminator(nn.Module):
def __init__(self, input_nc):
super(StyleDiscriminator, self).__init__()
model = [ nn.Conv2d(input_nc, 64, 4, stride=2, padding=1),
SRMResidualLayer(64),
nn.LeakyReLU(0.2, inplace=True) ]
model += [ nn.Conv2d(64, 128, 4, stride=2, padding=1),
SRMResidualLayer(128),
nn.InstanceNorm2d(128),
nn.LeakyReLU(0.2, inplace=True) ]
model += [ nn.Conv2d(128, 256, 4, stride=2, padding=1),
SRMResidualLayer(256),
nn.InstanceNorm2d(256),
nn.LeakyReLU(0.2, inplace=True) ]
self.encoder_layers_SD = nn.Sequential(*model)
model += [nn.Conv2d(256, 1, kernel_size=1, stride=1, padding=0, bias=False)]
self.encoder_layers_D = nn.Sequential(*model)
self.avg_pool1 = nn.AdaptiveAvgPool2d(8)
self.avg_pool2 = nn.AdaptiveAvgPool2d(1)
self.maxpool1 = nn.AdaptiveMaxPool2d(8)
self.maxpool2 = nn.AdaptiveMaxPool2d(1)
def forward(self, x):
# For Decision
x_copy = x
x_copy = self.encoder_layers_D(x_copy)
scalar_D = F.avg_pool2d(x_copy, x_copy.size()[2:]).view(x_copy.size()[0], -1)
# For Style Vector
x = self.encoder_layers_SD(x)
y = x
x1 = self.avg_pool1(x)
y1 = self.maxpool1(y)
combine1 = x1 + y1
x2 = self.avg_pool2(combine1)
y2 = self.maxpool2(combine1)
x3 = x2.view(x.size()[0], -1)
y3 = y2.view(y.size()[0], -1)
z = x3 + y3
z = torch.mm(z.t(),z) # VT*V
z = torch.triu(z, diagonal=1)
z = z.view(1, -1)
z = mean_activation(z)
return scalar_D.squeeze(-1), z