-
Notifications
You must be signed in to change notification settings - Fork 20
/
sagan.py
137 lines (124 loc) · 4.48 KB
/
sagan.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
# Copyright (C) 2019 Elvis Yu-Jing Lin <elvisyjlin@gmail.com>
#
# This work is licensed under the MIT License. To view a copy of this license,
# visit https://opensource.org/licenses/MIT.
"""Models of Spatial Attention Generative Adversarial Network"""
import torch
import torch.nn as nn
def get_norm(name, nc):
if name == 'batchnorm':
return nn.BatchNorm2d(nc)
if name == 'instancenorm':
return nn.InstanceNorm2d(nc)
raise ValueError('Unsupported normalization layer: {:s}'.format(name))
def get_nonlinear(name):
if name == 'relu':
return nn.ReLU(inplace=True)
if name == 'lrelu':
return nn.LeakyReLU(0.2, inplace=True)
if name == 'sigmoid':
return nn.Sigmoid()
if name == 'tanh':
return nn.Tanh()
raise ValueError('Unsupported activation layer: {:s}'.format(name))
class ResBlk(nn.Module):
def __init__(self, n_in, n_out):
super(ResBlk, self).__init__()
self.layers = nn.Sequential(
nn.Conv2d(n_in, n_out, 3, 1, 1),
get_norm('batchnorm', n_out),
nn.ReLU(inplace=True),
nn.Conv2d(n_out, n_out, 3, 1, 1),
get_norm('batchnorm', n_out),
)
def forward(self, x):
return self.layers(x) + x
class _Generator(nn.Module):
def __init__(self, input_channels, output_channels, last_nonlinear):
super(_Generator, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(input_channels, 32, 7, 1, 3), # n_in, n_out, kernel_size, stride, padding
get_norm('instancenorm', 32),
get_nonlinear('relu'),
nn.Conv2d(32, 64, 4, 2, 1),
get_norm('instancenorm', 64),
get_nonlinear('relu'),
nn.Conv2d(64, 128, 4, 2, 1),
get_norm('instancenorm', 128),
get_nonlinear('relu'),
nn.Conv2d(128, 256, 4, 2, 1),
get_norm('instancenorm', 256),
get_nonlinear('relu'),
)
self.resblk = nn.Sequential(
ResBlk(256, 256),
ResBlk(256, 256),
ResBlk(256, 256),
ResBlk(256, 256),
)
self.deconv = nn.Sequential(
nn.ConvTranspose2d(256, 128, 4, 2, 1),
get_norm('instancenorm', 128),
get_nonlinear('relu'),
nn.ConvTranspose2d(128, 64, 4, 2, 1),
get_norm('instancenorm', 64),
get_nonlinear('relu'),
nn.ConvTranspose2d(64, 32, 4, 2, 1),
get_norm('instancenorm', 32),
get_nonlinear('relu'),
nn.ConvTranspose2d(32, output_channels, 7, 1, 3),
get_nonlinear(last_nonlinear),
)
def forward(self, x, a=None):
if a is not None:
assert a.dim() == 2 and x.size(0) == a.size(0)
a = a.type(x.dtype)
a = a.unsqueeze(2).unsqueeze(3).repeat(1, 1, x.size(2), x.size(3))
x = torch.cat((x, a), dim=1)
h = self.conv(x)
h = self.resblk(h)
y = self.deconv(h)
return y
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.AMN = _Generator(4, 3, 'tanh')
self.SAN = _Generator(3, 1, 'sigmoid')
def forward(self, x, a):
y = self.AMN(x, a)
m = self.SAN(x)
y_ = y * m + x * (1-m)
return y_, m
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(3, 32, 4, 2, 1),
get_nonlinear('lrelu'),
nn.Conv2d(32, 64, 4, 2, 1),
get_nonlinear('lrelu'),
nn.Conv2d(64, 128, 4, 2, 1),
get_nonlinear('lrelu'),
nn.Conv2d(128, 256, 4, 2, 1),
get_nonlinear('lrelu'),
nn.Conv2d(256, 512, 4, 2, 1),
get_nonlinear('lrelu'),
nn.Conv2d(512, 1024, 4, 2, 1),
get_nonlinear('lrelu'),
)
self.src = nn.Conv2d(1024, 1, 3, 1, 1)
self.cls = nn.Sequential(
nn.Conv2d(1024, 1, 2, 1, 0),
get_nonlinear('sigmoid'),
)
def forward(self, x):
h = self.conv(x)
return self.src(h), self.cls(h).squeeze().unsqueeze(1)
if __name__ == '__main__':
from torchsummary import summary
AMN = Generator(4, 3, 'tanh')
summary(AMN, (4, 128, 128), device='cpu')
SAN = Generator(3, 1, 'sigmoid')
summary(SAN, (3, 128, 128), device='cpu')
D = Discriminator(3)
summary(D, (3, 128, 128), device='cpu')