-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_resnet.py
164 lines (132 loc) · 5.66 KB
/
model_resnet.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
# ResNet generator and discriminator
from torch import nn
import torch.nn.functional as F
from spectral_normalization import SpectralNorm
import numpy as np
channels = 3
class ResBlockGenerator(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super(ResBlockGenerator, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, 3, 1, padding=1)
self.conv2 = nn.Conv2d(out_channels, out_channels, 3, 1, padding=1)
nn.init.xavier_uniform(self.conv1.weight.data, 1.)
nn.init.xavier_uniform(self.conv2.weight.data, 1.)
self.model = nn.Sequential(
nn.BatchNorm2d(in_channels),
nn.ReLU(),
nn.Upsample(scale_factor=2),
self.conv1,
nn.BatchNorm2d(out_channels),
nn.ReLU(),
self.conv2
)
self.bypass = nn.Sequential()
if stride != 1:
self.bypass = nn.Upsample(scale_factor=2)
def forward(self, x):
return self.model(x) + self.bypass(x)
class ResBlockDiscriminator(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super(ResBlockDiscriminator, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, 3, 1, padding=1)
self.conv2 = nn.Conv2d(out_channels, out_channels, 3, 1, padding=1)
nn.init.xavier_uniform(self.conv1.weight.data, 1.)
nn.init.xavier_uniform(self.conv2.weight.data, 1.)
if stride == 1:
self.model = nn.Sequential(
nn.ReLU(),
SpectralNorm(self.conv1),
nn.ReLU(),
SpectralNorm(self.conv2)
)
else:
self.model = nn.Sequential(
nn.ReLU(),
SpectralNorm(self.conv1),
nn.ReLU(),
SpectralNorm(self.conv2),
nn.AvgPool2d(2, stride=stride, padding=0)
)
self.bypass = nn.Sequential()
if stride != 1:
self.bypass_conv = nn.Conv2d(in_channels,out_channels, 1, 1, padding=0)
nn.init.xavier_uniform(self.bypass_conv.weight.data, np.sqrt(2))
self.bypass = nn.Sequential(
SpectralNorm(self.bypass_conv),
nn.AvgPool2d(2, stride=stride, padding=0)
)
# if in_channels == out_channels:
# self.bypass = nn.AvgPool2d(2, stride=stride, padding=0)
# else:
# self.bypass = nn.Sequential(
# SpectralNorm(nn.Conv2d(in_channels,out_channels, 1, 1, padding=0)),
# nn.AvgPool2d(2, stride=stride, padding=0)
# )
def forward(self, x):
return self.model(x) + self.bypass(x)
# special ResBlock just for the first layer of the discriminator
class FirstResBlockDiscriminator(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super(FirstResBlockDiscriminator, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, 3, 1, padding=1)
self.conv2 = nn.Conv2d(out_channels, out_channels, 3, 1, padding=1)
self.bypass_conv = nn.Conv2d(in_channels, out_channels, 1, 1, padding=0)
nn.init.xavier_uniform(self.conv1.weight.data, 1.)
nn.init.xavier_uniform(self.conv2.weight.data, 1.)
nn.init.xavier_uniform(self.bypass_conv.weight.data, np.sqrt(2))
# we don't want to apply ReLU activation to raw image before convolution transformation.
self.model = nn.Sequential(
SpectralNorm(self.conv1),
nn.ReLU(),
SpectralNorm(self.conv2),
nn.AvgPool2d(2)
)
self.bypass = nn.Sequential(
nn.AvgPool2d(2),
SpectralNorm(self.bypass_conv),
)
def forward(self, x):
return self.model(x) + self.bypass(x)
GEN_SIZE=128
DISC_SIZE=128
class Generator(nn.Module):
def __init__(self, z_dim):
super(Generator, self).__init__()
self.z_dim = z_dim
self.dense = nn.Linear(self.z_dim, 4 * 4 * GEN_SIZE)
self.final = nn.Conv2d(GEN_SIZE, channels, 3, stride=1, padding=1)
nn.init.xavier_uniform(self.dense.weight.data, 1.)
nn.init.xavier_uniform(self.final.weight.data, 1.)
self.model = nn.Sequential(
ResBlockGenerator(GEN_SIZE, GEN_SIZE, stride=2),
ResBlockGenerator(GEN_SIZE, GEN_SIZE, stride=2),
ResBlockGenerator(GEN_SIZE, GEN_SIZE, stride=2),
nn.BatchNorm2d(GEN_SIZE),
nn.ReLU(),
self.final,
nn.Tanh())
def forward(self, z):
return self.model(self.dense(z).view(-1, GEN_SIZE, 4, 4))
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.model = nn.Sequential(
FirstResBlockDiscriminator(channels, DISC_SIZE, stride=2),
ResBlockDiscriminator(DISC_SIZE, DISC_SIZE, stride=2),
ResBlockDiscriminator(DISC_SIZE, DISC_SIZE),
ResBlockDiscriminator(DISC_SIZE, DISC_SIZE),
nn.ReLU(),
nn.AvgPool2d(8),
)
self.fc = nn.Linear(DISC_SIZE, 1)
nn.init.xavier_uniform(self.fc.weight.data, 1.)
self.fc = SpectralNorm(self.fc)
self.rotate_fc = nn.Linear(DISC_SIZE, 4)
nn.init.xavier_uniform(self.rotate_fc.weight.data, 1.)
self.rotate_fc = SpectralNorm(self.rotate_fc)
def forward(self, x):
feat = self.model(x).view(-1, DISC_SIZE)
disc_score = self.fc(feat)
rotate_score = self.rotate_fc(feat)
return disc_score, rotate_score
#return self.fc(self.model(x).view(-1,DISC_SIZE))