-
Notifications
You must be signed in to change notification settings - Fork 1
/
model3.py
81 lines (66 loc) · 2.27 KB
/
model3.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
# -*- coding: utf-8 -*-
import torch.nn as nn
from utils.nn import Maxout
from utils import initialize_weights
# model parameters
nz = 74 # number of visual units
# Network architecture is exactly same as in InfoGAN
# Refer to pytorch-generative-model-collections
# https://github.com/znxlwm/pytorch-generative-model-collections/blob/master/GAN.py
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.L1 = nn.Sequential(
nn.Linear(nz, 1024),
nn.BatchNorm1d(1024), # num_features - from an expected input of size batch_size x num_feafures
nn.ReLU()
)
self.L2 = nn.Sequential(
nn.Linear(1024, 128 * 7 * 7), # 128 will be used as channel size
nn.BatchNorm1d(128 * 7 * 7),
nn.ReLU()
)
self.L3 = nn.Sequential(
nn.ConvTranspose2d(128, 64, 4, 2, 1), # in channels, out channels, kernel size, stride, padding
nn.BatchNorm2d(64), # batch_size x `num_features` x height x width
nn.ReLU(),
)
self.L4 = nn.Sequential(
nn.ConvTranspose2d(64, 1, 4, 2, 1),
nn.Sigmoid()
)
def forward(self, x):
x = self.L1(x)
x = self.L2(x)
x = x.view(-1, 128, 7, 7) # reshape to batch_size x channel_size x height x width
x = self.L3(x)
x = self.L4(x)
return x
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.L1 = nn.Sequential(
nn.Conv2d(1, 64, 4, 2, 1), # in channels, out channels, kernel size, stride, padding
nn.LeakyReLU(0.1)
)
self.L2 = nn.Sequential(
nn.Conv2d(64, 128, 4, 2, 1),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.1)
)
self.L3 = nn.Sequential(
nn.Linear(128 * 7 * 7, 1024),
nn.BatchNorm1d(1024),
nn.LeakyReLU(0.1)
)
self.L4 = nn.Sequential(
nn.Linear(1024, 1),
nn.Sigmoid()
)
def forward(self, x):
x = self.L1(x)
x = self.L2(x)
x = x.view(-1, 128 * 7 * 7) # reshape as batch_size x 128*7*7
x = self.L3(x)
x = self.L4(x)
return x