-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathmodels.py
113 lines (89 loc) · 3.75 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
import torch
from torch import nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.utils.data import TensorDataset, DataLoader
class GeneratorCNN(nn.Module):
def __init__(self, input_channel, output_channel, conv_dims, deconv_dims, num_gpu):
super(GeneratorCNN, self).__init__()
self.num_gpu = num_gpu
self.layers = []
prev_dim = conv_dims[0]
self.layers.append(nn.Conv2d(input_channel, prev_dim, 4, 2, 1, bias=False))
self.layers.append(nn.LeakyReLU(0.2, inplace=True))
for out_dim in conv_dims[1:]:
self.layers.append(nn.Conv2d(prev_dim, out_dim, 4, 2, 1, bias=False))
self.layers.append(nn.BatchNorm2d(out_dim))
self.layers.append(nn.LeakyReLU(0.2, inplace=True))
prev_dim = out_dim
for out_dim in deconv_dims:
self.layers.append(nn.ConvTranspose2d(prev_dim, out_dim, 4, 2, 1, bias=False))
self.layers.append(nn.BatchNorm2d(out_dim))
self.layers.append(nn.ReLU(True))
prev_dim = out_dim
self.layers.append(nn.ConvTranspose2d(prev_dim, output_channel, 4, 2, 1, bias=False))
self.layers.append(nn.Tanh())
self.layer_module = nn.ModuleList(self.layers)
def main(self, x):
out = x
for layer in self.layer_module:
out = layer(out)
return out
def forward(self, x):
return self.main(x)
class DiscriminatorCNN(nn.Module):
def __init__(self, input_channel, output_channel, hidden_dims, num_gpu):
super(DiscriminatorCNN, self).__init__()
self.num_gpu = num_gpu
self.layers = []
prev_dim = hidden_dims[0]
self.layers.append(nn.Conv2d(input_channel, prev_dim, 4, 2, 1, bias=False))
self.layers.append(nn.LeakyReLU(0.2, inplace=True))
for out_dim in hidden_dims[1:]:
self.layers.append(nn.Conv2d(prev_dim, out_dim, 4, 2, 1, bias=False))
self.layers.append(nn.BatchNorm2d(out_dim))
self.layers.append(nn.LeakyReLU(0.2, inplace=True))
prev_dim = out_dim
self.layers.append(nn.Conv2d(prev_dim, output_channel, 4, 1, 0, bias=False))
self.layers.append(nn.Sigmoid())
self.layer_module = nn.ModuleList(self.layers)
def main(self, x):
out = x
for layer in self.layer_module:
out = layer(out)
return out.view(out.size(0), -1)
def forward(self, x):
return self.main(x)
class GeneratorFC(nn.Module):
def __init__(self, input_size, output_size, hidden_dims):
super(GeneratorFC, self).__init__()
self.layers = []
prev_dim = input_size
for hidden_dim in hidden_dims:
self.layers.append(nn.Linear(prev_dim, hidden_dim))
self.layers.append(nn.ReLU(True))
prev_dim = hidden_dim
self.layers.append(nn.Linear(prev_dim, output_size))
self.layer_module = nn.ModuleList(self.layers)
def forward(self, x):
out = x
for layer in self.layer_module:
out = layer(out)
return out
class DiscriminatorFC(nn.Module):
def __init__(self, input_size, output_size, hidden_dims):
super(DiscriminatorFC, self).__init__()
self.layers = []
prev_dim = input_size
for idx, hidden_dim in enumerate(hidden_dims):
self.layers.append(nn.Linear(prev_dim, hidden_dim))
self.layers.append(nn.ReLU(True))
prev_dim = hidden_dim
self.layers.append(nn.Linear(prev_dim, output_size))
self.layers.append(nn.Sigmoid())
self.layer_module = nn.ModuleList(self.layers)
def forward(self, x):
out = x
for layer in self.layer_module:
out = layer(out)
return out.view(-1, 1)