-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
78 lines (62 loc) · 2.21 KB
/
model.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
import torch.nn as nn
import torch.nn.functional as F
class LinearModelGenerator(nn.Module):
def __init__(self, latent_dim):
super(LinearModelGenerator, self).__init__()
self.latent_dim = latent_dim
self.linear = nn.Sequential(
nn.Linear(latent_dim, 256),
nn.BatchNorm1d(256),
nn.ReLU(),
nn.Linear(256, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Linear(512, 32 * 32),
nn.Tanh()
)
def forward(self, x):
x = self.linear(x)
return x.view(-1, 1, 32, 32)
class DCGANModelGenerator(nn.Module):
def __init__(self, latent_dim):
super(DCGANModelGenerator, self).__init__()
self.latent_dim = latent_dim
self.project = nn.Linear(latent_dim, 4 * 4 * 256)
# self.bn = nn.BatchNorm1d(4 * 4 * 256)
self.conv = nn.Sequential(
nn.ConvTranspose2d(256, 128, kernel_size=2, stride=2, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.ConvTranspose2d(128, 64, kernel_size=2, stride=2, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.ConvTranspose2d(64, 1, kernel_size=2, stride=2),
nn.Tanh()
)
def forward(self, x):
x = self.project(x)
x = F.relu(x)
x = self.conv(x.view(-1, 256, 4, 4))
return x
class DCGANModelDiscriminator(nn.Module):
def __init__(self):
super(DCGANModelDiscriminator, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(1, 64, 3, stride=2, padding=2),
# nn.BatchNorm2d(64),
nn.LeakyReLU(negative_slope=0.2),
#nn.Dropout2d(0.5),
nn.Conv2d(64, 128, 3, stride=2, padding=2, bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(negative_slope=0.2),
#nn.Dropout2d(0.5),
nn.Conv2d(128, 256, 3, stride=2, padding=2, bias=False),
nn.BatchNorm2d(256),
nn.LeakyReLU(negative_slope=0.2),
#nn.Dropout2d(0.5),
nn.Conv2d(256, 1, 4, stride=4),
# nn.Sigmoid()
)
def forward(self, x):
x = self.conv(x)
return x.squeeze()