-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodels.py
199 lines (173 loc) · 6.42 KB
/
models.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
import torch.nn as nn
import torch.nn.functional as F
import torch
from activations import *
from torchvision import models
def get_resnet18(num_classes):
model = models.resnet18(pretrained=True)
n_inputs = model.fc.in_features
model.fc = nn.Sequential(
nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2),
nn.Linear(256, num_classes))#, nn.LogSoftmax(dim=1))
#instead normalization prepend batchnorm
model = nn.Sequential(nn.BatchNorm2d(num_features=3, affine=False), model)
return model
# Target Model definition
class MNIST_target_net(nn.Module):
def __init__(self):
super(MNIST_target_net, self).__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=3)
self.conv2 = nn.Conv2d(32, 32, kernel_size=3)
self.conv3 = nn.Conv2d(32, 64, kernel_size=3)
self.conv4 = nn.Conv2d(64, 64, kernel_size=3)
self.fc1 = nn.Linear(64*4*4, 200)
self.fc2 = nn.Linear(200, 200)
self.logits = nn.Linear(200, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv3(x))
x = F.relu(self.conv4(x))
x = F.max_pool2d(x, 2)
x = x.view(-1, 64*4*4)
x = F.relu(self.fc1(x))
x = F.dropout(x, 0.5)
x = F.relu(self.fc2(x))
x = self.logits(x)
return x
class Discriminator(nn.Module):
def __init__(self, image_nc):
super(Discriminator, self).__init__()
model = [
#c8
nn.Conv2d(image_nc, 8, kernel_size=4, stride=2, padding=0, bias=True),
nn.LeakyReLU(0.2, inplace=True),
# AReLU(),
# Rational(),
# Swish(),
# c16
nn.Conv2d(8, 16, kernel_size=4, stride=2, padding=0, bias=False),
nn.InstanceNorm2d(16),
nn.LeakyReLU(0.2, inplace=True),
# AReLU(),
# Rational(),
# Swish(),
# c32
nn.Conv2d(16, 32, kernel_size=4, stride=2, padding=0, bias=False),
nn.InstanceNorm2d(32),
nn.LeakyReLU(0.2, inplace=True),
# AReLU(),
# Rational(),
# Swish(),
nn.Conv2d(32, 1, 1, bias=True),
]
self.model = nn.Sequential(*model)
self.prob = nn.Sigmoid()
def forward(self, x):
output = self.model(x).squeeze()
probs = self.prob(output)
return output, probs
class Generator(nn.Module):
def __init__(self,
gen_input_nc,
image_nc,
):
super(Generator, self).__init__()
encoder_lis = [
# MNIST:1*28*28
nn.Conv2d(gen_input_nc, 8, kernel_size=3, stride=1, padding=0, bias=True),
nn.InstanceNorm2d(8),
nn.ReLU(),
# nn.LeakyReLU(0.2, inplace=True),
# AReLU(),
# Rational(),
# Swish(),
# 8*26*26
nn.Conv2d(8, 16, kernel_size=3, stride=2, padding=0, bias=True),
nn.InstanceNorm2d(16),
nn.ReLU(),
# nn.LeakyReLU(0.2, inplace=True),
# AReLU(),
# Rational(),
# Swish(),
# 16*12*12
nn.Conv2d(16, 32, kernel_size=3, stride=2, padding=0, bias=True),
nn.InstanceNorm2d(32),
nn.ReLU(),
# nn.LeakyReLU(0.2, inplace=True),
# AReLU(),
# Rational(),
# Swish(),
# 32*5*5
]
bottle_neck_lis = [ResnetBlock(32),
ResnetBlock(32),
ResnetBlock(32),
ResnetBlock(32),]
decoder_lis = [
nn.ConvTranspose2d(32, 16, kernel_size=3, stride=2, padding=0, bias=False),
nn.InstanceNorm2d(16),
nn.ReLU(),
# nn.LeakyReLU(0.2, inplace=True),
# AReLU(),
# Rational(),
# Swish(),
# state size. 16 x 11 x 11
nn.ConvTranspose2d(16, 8, kernel_size=3, stride=2, padding=0, bias=False),
nn.InstanceNorm2d(8),
nn.ReLU(),
# nn.LeakyReLU(0.2, inplace=True),
# AReLU(),
# Rational(),
# Swish(),
# state size. 8 x 23 x 23
nn.ConvTranspose2d(8, image_nc, kernel_size=6, stride=1, padding=0, bias=False),
nn.Tanh()
# state size. image_nc x 28 x 28
]
self.encoder = nn.Sequential(*encoder_lis)
self.bottle_neck = nn.Sequential(*bottle_neck_lis)
self.decoder = nn.Sequential(*decoder_lis)
def forward(self, x):
x = self.encoder(x)
x = self.bottle_neck(x)
x = self.decoder(x)
return x
# Define a resnet block
# modified from https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/models/networks.py
class ResnetBlock(nn.Module):
def __init__(self, dim, padding_type='reflect', norm_layer=nn.BatchNorm2d, use_dropout=False, use_bias=False):
super(ResnetBlock, self).__init__()
self.conv_block = self.build_conv_block(dim, padding_type, norm_layer, use_dropout, use_bias)
def build_conv_block(self, dim, padding_type, norm_layer, use_dropout, use_bias):
conv_block = []
p = 0
if padding_type == 'reflect':
conv_block += [nn.ReflectionPad2d(1)]
elif padding_type == 'replicate':
conv_block += [nn.ReplicationPad2d(1)]
elif padding_type == 'zero':
p = 1
else:
raise NotImplementedError('padding [%s] is not implemented' % padding_type)
conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias=use_bias),
norm_layer(dim),
nn.ReLU(True)]
if use_dropout:
conv_block += [nn.Dropout(0.5)]
p = 0
if padding_type == 'reflect':
conv_block += [nn.ReflectionPad2d(1)]
elif padding_type == 'replicate':
conv_block += [nn.ReplicationPad2d(1)]
elif padding_type == 'zero':
p = 1
else:
raise NotImplementedError('padding [%s] is not implemented' % padding_type)
conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias=use_bias),
norm_layer(dim)]
return nn.Sequential(*conv_block)
def forward(self, x):
out = x + self.conv_block(x)
return out