-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmodels.py
169 lines (146 loc) · 6.11 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
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
165
166
167
168
169
# coding: utf-8
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.init as init
# see: _netD in https://github.com/pytorch/examples/blob/master/dcgan/main.py
class Discriminator_I(nn.Module):
def __init__(self, nc=3, ndf=64, ngpu=1):
super(Discriminator_I, self).__init__()
self.ngpu = ngpu
self.main = nn.Sequential(
# input is (nc) x 96 x 96
nn.Conv2d(nc, ndf, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf) x 48 x 48
nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*2) x 24 x 24
nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*4) x 12 x 12
nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*8) x 6 x 6
nn.Conv2d(ndf * 8, 1, 6, 1, 0, bias=False),
nn.Sigmoid()
)
def forward(self, input):
if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu > 1:
output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))
else:
output = self.main(input)
return output.view(-1, 1).squeeze(1)
class Discriminator_V(nn.Module):
def __init__(self, nc=3, ndf=64, T=16, ngpu=1):
super(Discriminator_V, self).__init__()
self.ngpu = ngpu
self.main = nn.Sequential(
# input is (nc) x T x 96 x 96
nn.Conv3d(nc, ndf, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf) x T/2 x 48 x 48
nn.Conv3d(ndf, ndf * 2, 4, 2, 1, bias=False),
nn.BatchNorm3d(ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*2) x T/4 x 24 x 24
nn.Conv3d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
nn.BatchNorm3d(ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*4) x T/8 x 12 x 12
nn.Conv3d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
nn.BatchNorm3d(ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*8) x T/16 x 6 x 6
Flatten(),
nn.Linear(int((ndf*8)*(T/16)*6*6), 1),
nn.Sigmoid()
)
def forward(self, input):
if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu > 1:
output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))
else:
output = self.main(input)
return output.view(-1, 1).squeeze(1)
# see: _netG in https://github.com/pytorch/examples/blob/master/dcgan/main.py
class Generator_I(nn.Module):
def __init__(self, nc=3, ngf=64, nz=60, ngpu=1):
super(Generator_I, self).__init__()
self.ngpu = ngpu
self.main = nn.Sequential(
# input is Z, going into a convolution
nn.ConvTranspose2d( nz, ngf * 8, 6, 1, 0, bias=False),
nn.BatchNorm2d(ngf * 8),
nn.ReLU(True),
# state size. (ngf*8) x 6 x 6
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 4),
nn.ReLU(True),
# state size. (ngf*4) x 12 x 12
nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 2),
nn.ReLU(True),
# state size. (ngf*2) x 24 x 24
nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf),
nn.ReLU(True),
# state size. (ngf) x 48 x 48
nn.ConvTranspose2d( ngf, nc, 4, 2, 1, bias=False),
nn.Tanh()
# state size. (nc) x 96 x 96
)
def forward(self, input):
if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu > 1:
output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))
else:
output = self.main(input)
return output
class GRU(nn.Module):
def __init__(self, input_size, hidden_size, dropout=0, gpu=True):
super(GRU, self).__init__()
output_size = input_size
self._gpu = gpu
self.hidden_size = hidden_size
# define layers
self.gru = nn.GRUCell(input_size, hidden_size)
self.drop = nn.Dropout(p=dropout)
self.linear = nn.Linear(hidden_size, output_size)
self.bn = nn.BatchNorm1d(output_size, affine=False)
def forward(self, inputs, n_frames):
'''
inputs.shape() => (batch_size, input_size)
outputs.shape() => (seq_len, batch_size, output_size)
'''
outputs = []
for i in range(n_frames):
self.hidden = self.gru(inputs, self.hidden)
inputs = self.linear(self.hidden)
outputs.append(inputs)
outputs = [ self.bn(elm) for elm in outputs ]
outputs = torch.stack(outputs)
return outputs
def initWeight(self, init_forget_bias=1):
# See details in https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/rnn.py
for name, params in self.named_parameters():
if 'weight' in name:
init.xavier_uniform(params)
# initialize forget gate bias
elif 'gru.bias_ih_l' in name:
b_ir, b_iz, b_in = params.chunk(3, 0)
init.constant(b_iz, init_forget_bias)
elif 'gru.bias_hh_l' in name:
b_hr, b_hz, b_hn = params.chunk(3, 0)
init.constant(b_hz, init_forget_bias)
else:
init.constant(params, 0)
def initHidden(self, batch_size):
self.hidden = Variable(torch.zeros(batch_size, self.hidden_size))
if self._gpu == True:
self.hidden = self.hidden.cuda()
''' utils '''
class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)