-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels_file.py
164 lines (143 loc) · 6.31 KB
/
models_file.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
import torch
cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if cuda else "cpu")
from torch.utils.data import DataLoader
from torch.nn.utils.parametrizations import spectral_norm
from torch.utils.data.sampler import SubsetRandomSampler
from torchvision.datasets import MNIST
from torchvision.transforms import ToTensor, Normalize, Compose
from functools import reduce
from scipy.linalg import sqrtm
from torch import nn
from scipy.linalg import sqrtm
from torchvision import transforms
def get_gan_models(modelname,dataset,config):
latent_dim = config["latent_dim"]
nch = config["nch"]
def identity_model(model):
return model
def layer_to_batchnorm(dims):
return nn.BatchNorm2d(dims[0])
def initialize_weights(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
nn.init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find('BatchNorm') != -1:
nn.init.normal_(m.weight.data, 1.0, 0.02)
nn.init.constant_(m.bias.data, 0)
if modelname=="spec":
norm = spectral_norm
bn = nn.LayerNorm
sm = torch.nn.Identity()
elif modelname =="weight":
norm = identity_model
bn = layer_to_batchnorm
sm = torch.nn.Identity()
elif modelname == 'GP':
norm = identity_model
bn = layer_to_batchnorm
sm = torch.nn.Identity()
else:
norm = identity_model
bn = layer_to_batchnorm
sm = nn.Sigmoid()
if dataset=="mnist":
generator = nn.Sequential(
# nn.ConvTranspose2d can be seen as the inverse operation
# of Conv2d, where after convolution we arrive at an
# upscaled image.
nn.ConvTranspose2d(latent_dim, 4*nch, kernel_size=4, stride=2,padding=0,bias=False),
nn.BatchNorm2d(4*nch),
nn.ReLU(True),
nn.ConvTranspose2d(4*nch, 2*nch, kernel_size=3, stride=2,padding=1,bias=False),
nn.BatchNorm2d(2*nch),
nn.ReLU(True),
nn.ConvTranspose2d(2*nch, nch, kernel_size=4, stride=2,padding=1,bias=False),
nn.BatchNorm2d(nch),
nn.ReLU(True),
nn.ConvTranspose2d(nch, 1, kernel_size=4, stride=2,padding=1),
nn.Tanh()
).to(device)
# The discriminator takes an image (real or fake)
# and decides whether it is generated or not.
discriminator = nn.Sequential(
norm(nn.Conv2d(1, nch, kernel_size=4, stride=2,padding=1)),
nn.ReLU(True),
norm(nn.Conv2d(nch, 2*nch, kernel_size=4, stride=2,padding=1,bias=False)),
bn([2*nch,7,7]),
nn.ReLU(True),
norm(nn.Conv2d(2*nch, 2*nch, kernel_size=3, stride=2,padding=1,bias=False)),
bn([2*nch,4,4]),
nn.ReLU(True),
norm(nn.Conv2d(2*nch, 1, kernel_size=4, stride=2,padding=0)),
sm
).to(device)
elif dataset=='CIFAR10':
generator = nn.Sequential(
# nn.ConvTranspose2d can be seen as the inverse operation
# of Conv2d, where after convolution we arrive at an upscaled image.
nn.ConvTranspose2d(latent_dim, 16*nch, kernel_size=4, stride=1,padding=0,bias=False),
nn.BatchNorm2d(16*nch),
nn.ReLU(True),
nn.ConvTranspose2d(16*nch, 8*nch, kernel_size=4, stride=2,padding=1,bias=False),
nn.BatchNorm2d(8*nch),
nn.ReLU(True),
nn.ConvTranspose2d(8*nch, 4*nch, kernel_size=4, stride=2,padding=1,bias=False),
nn.BatchNorm2d(4*nch),
nn.ReLU(True),
nn.ConvTranspose2d(4*nch, 3, kernel_size=4, stride=2,padding=1,bias=False),
nn.Tanh()
).to(device)
# The discriminator takes an image (real or fake)
# and decides whether it is generated or not.
discriminator = nn.Sequential(
norm(nn.Conv2d(3, 4*nch, kernel_size=4, stride=2,padding=1,bias=False)),
nn.ReLU(True),
norm(nn.Conv2d(4*nch, 8*nch, kernel_size=4, stride=2,padding=1,bias=False)),
bn([8*nch,8,8]),
nn.ReLU(True),
#nn.Dropout(0.4),
norm(nn.Conv2d(8*nch, 8*nch, kernel_size=4, stride=2,padding=1,bias=False)),
bn([8*nch,4,4]),
nn.ReLU(True),
norm(nn.Conv2d(8*nch,1, kernel_size=4, stride=1,padding=0,bias=True)),
sm
).to(device)
elif dataset=='celeba':
generator = nn.Sequential(
# nn.ConvTranspose2d can be seen as the inverse operation
# of Conv2d, where after convolution we arrive at an upscaled image.
nn.ConvTranspose2d(latent_dim, 16*nch, kernel_size=4, stride=1,padding=0,bias=False),
nn.BatchNorm2d(16*nch),
nn.ReLU(True),
nn.ConvTranspose2d(16*nch, 8*nch, kernel_size=4, stride=2,padding=1,bias=False),
nn.BatchNorm2d(8*nch),
nn.ReLU(True),
nn.ConvTranspose2d(8*nch, 4*nch, kernel_size=4, stride=2,padding=1,bias=False),
nn.BatchNorm2d(4*nch),
nn.ReLU(True),
nn.ConvTranspose2d(4*nch, 2*nch, kernel_size=4, stride=2,padding=1,bias=False),
nn.BatchNorm2d(2*nch),
nn.ReLU(True),
nn.ConvTranspose2d(2*nch, 3, kernel_size=4, stride=2,padding=1,bias=True),
nn.Tanh()
).to(device)
# The discriminator takes an image (real or fake)
# and decides whether it is generated or not.
discriminator = nn.Sequential(
norm(nn.Conv2d(3, 2*nch, kernel_size=4, stride=2,padding=1,bias=False)),
nn.ReLU(True),
norm(nn.Conv2d(2*nch, 4*nch, kernel_size=4, stride=2,padding=1,bias=False)),
bn([4*nch,16,16]),
nn.ReLU(True),
#nn.Dropout(0.4),
norm(nn.Conv2d(4*nch, 8*nch, kernel_size=4, stride=2,padding=1,bias=False)),
bn([8*nch,8,8]),
nn.ReLU(True),
norm(nn.Conv2d(8*nch,8*nch, kernel_size=4, stride=2,padding=1,bias=False)),
bn([8*nch,4,4]),
nn.ReLU(True),
norm(nn.Conv2d(8*nch,1, kernel_size=4, stride=1,padding=0,bias=True)),
sm
).to(device)
return generator,discriminator