-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgan_models.py
executable file
·75 lines (67 loc) · 2.6 KB
/
gan_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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from spectral import SpectralNorm
import numpy as np
# G(z)
class Generator_MLP(nn.Module):
# initializers
def __init__(self, batch_size=64, image_size=64, z_dim=100, mlp_dim=64, rgb_channel=3):
self.size = [image_size, rgb_channel]
super(Generator_MLP, self).__init__()
def block(in_feat, out_feat, normalize=True):
layers = [SpectralNorm(nn.Linear(in_feat, out_feat))]
if normalize:
layers.append(nn.BatchNorm1d(out_feat, 0.8))
layers.append(nn.LeakyReLU(0.2, inplace=True))
return layers
self.model = nn.Sequential(
*block(z_dim, 128, normalize=False),
*block(128, mlp_dim*4),
*block(mlp_dim*4, mlp_dim*12),
*block(mlp_dim*12, mlp_dim*48),
nn.Linear(mlp_dim*48, image_size*image_size*rgb_channel),
nn.Tanh()
)
# forward method
def forward(self, z):
# 64 x 128
x = self.model(z)
x = x.view(x.size(0), self.size[1], self.size[0], self.size[0])
return x, None, None
class Discriminator_MLP(nn.Module):
# initializers
def __init__(self, batch_size=64, image_size=64, mlp_dim=64, rgb_channel=3):
super(Discriminator_MLP, self).__init__()
self.model = nn.Sequential(
SpectralNorm(nn.Linear(image_size*image_size*rgb_channel, mlp_dim * 48)),
#nn.BatchNorm1d(mlp_dim * 48),
nn.LeakyReLU(0.2, inplace=True),
SpectralNorm(nn.Linear(mlp_dim*48, mlp_dim*12)),
#nn.BatchNorm1d(mlp_dim * 12),
nn.LeakyReLU(0.2, inplace=True),
SpectralNorm(nn.Linear(mlp_dim*12, mlp_dim*4)),
#nn.BatchNorm1d(mlp_dim * 4),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(mlp_dim*4, 1),
)
#self.model = nn.Sequential(
# nn.Linear(image_size*image_size*rgb_channel, mlp_dim * 48),
# #nn.BatchNorm1d(mlp_dim * 48),
# nn.LeakyReLU(0.2, inplace=True),
# nn.Linear(mlp_dim*48, mlp_dim*12),
# #nn.BatchNorm1d(mlp_dim * 12),
# nn.LeakyReLU(0.2, inplace=True),
# nn.Linear(mlp_dim*12, mlp_dim*4),
# #nn.BatchNorm1d(mlp_dim * 4),
# nn.LeakyReLU(0.2, inplace=True),
# nn.Linear(mlp_dim*4, 1),
#)
# forward method
def forward(self, input):
# 64 x 3 x 64 x 64
x = input.view(input.size(0), -1)
x = self.model(x)
# 64 x 1
return x.squeeze(), None, None