forked from pclucas14/pixel-cnn-pp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
178 lines (140 loc) · 7.33 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
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
170
171
172
173
174
175
176
177
178
import pdb
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from layers import *
from utils import *
import numpy as np
class PixelCNNLayer_up(nn.Module):
def __init__(self, nr_resnet, nr_filters, resnet_nonlinearity):
super(PixelCNNLayer_up, self).__init__()
self.nr_resnet = nr_resnet
# stream from pixels above
self.u_stream = nn.ModuleList([gated_resnet(nr_filters, down_shifted_conv2d,
resnet_nonlinearity, skip_connection=0)
for _ in range(nr_resnet)])
# stream from pixels above and to thes left
self.ul_stream = nn.ModuleList([gated_resnet(nr_filters, down_right_shifted_conv2d,
resnet_nonlinearity, skip_connection=1)
for _ in range(nr_resnet)])
def forward(self, u, ul):
u_list, ul_list = [], []
for i in range(self.nr_resnet):
u = self.u_stream[i](u)
ul = self.ul_stream[i](ul, a=u)
u_list += [u]
ul_list += [ul]
return u_list, ul_list
class PixelCNNLayer_down(nn.Module):
def __init__(self, nr_resnet, nr_filters, resnet_nonlinearity):
super(PixelCNNLayer_down, self).__init__()
self.nr_resnet = nr_resnet
# stream from pixels above
self.u_stream = nn.ModuleList([gated_resnet(nr_filters, down_shifted_conv2d,
resnet_nonlinearity, skip_connection=1)
for _ in range(nr_resnet)])
# stream from pixels above and to thes left
self.ul_stream = nn.ModuleList([gated_resnet(nr_filters, down_right_shifted_conv2d,
resnet_nonlinearity, skip_connection=2)
for _ in range(nr_resnet)])
def forward(self, u, ul, u_list, ul_list):
for i in range(self.nr_resnet):
u = self.u_stream[i](u, a=u_list.pop())
ul = self.ul_stream[i](ul, a=torch.cat((u, ul_list.pop()), 1))
return u, ul
class PixelCNN(nn.Module):
def __init__(self, nr_resnet=5, nr_filters=80, nr_logistic_mix=10,
resnet_nonlinearity='concat_elu', input_channels=3, gauss_out=False):
super(PixelCNN, self).__init__()
if resnet_nonlinearity == 'concat_elu' :
self.resnet_nonlinearity = lambda x : concat_elu(x)
else :
raise Exception('right now only concat elu is supported as resnet nonlinearity.')
self.nr_filters = nr_filters
self.input_channels = input_channels
self.nr_logistic_mix = nr_logistic_mix
self.right_shift_pad = nn.ZeroPad2d((1, 0, 0, 0))
self.down_shift_pad = nn.ZeroPad2d((0, 0, 1, 0))
down_nr_resnet = [nr_resnet] + [nr_resnet + 1] * 2
self.down_layers = nn.ModuleList([PixelCNNLayer_down(down_nr_resnet[i], nr_filters,
self.resnet_nonlinearity) for i in range(3)])
self.up_layers = nn.ModuleList([PixelCNNLayer_up(nr_resnet, nr_filters,
self.resnet_nonlinearity) for _ in range(3)])
self.downsize_u_stream = nn.ModuleList([down_shifted_conv2d(nr_filters, nr_filters,
stride=(2,2)) for _ in range(2)])
self.downsize_ul_stream = nn.ModuleList([down_right_shifted_conv2d(nr_filters,
nr_filters, stride=(2,2)) for _ in range(2)])
self.upsize_u_stream = nn.ModuleList([down_shifted_deconv2d(nr_filters, nr_filters,
stride=(2,2)) for _ in range(2)])
self.upsize_ul_stream = nn.ModuleList([down_right_shifted_deconv2d(nr_filters,
nr_filters, stride=(2,2)) for _ in range(2)])
self.u_init = down_shifted_conv2d(input_channels + 1, nr_filters, filter_size=(2,3),
shift_output_down=True)
self.ul_init = nn.ModuleList([down_shifted_conv2d(input_channels + 1, nr_filters,
filter_size=(1,3), shift_output_down=True),
down_right_shifted_conv2d(input_channels + 1, nr_filters,
filter_size=(2,1), shift_output_right=True)])
num_mix = 3 if self.input_channels == 1 else 10
# gaussian pixelcnn...
if gauss_out:
self.nin_out = nin(nr_filters, 2 * input_channels)
else:
self.nin_out = nin(nr_filters, num_mix * nr_logistic_mix)
self.init_padding = None
def forward(self, x, sample=False):
# similar as done in the tf repo :
if self.init_padding is None and not sample:
xs = [int(y) for y in x.size()]
padding = Variable(torch.ones(xs[0], 1, xs[2], xs[3]), requires_grad=False)
self.init_padding = padding.cuda() if x.is_cuda else padding
if sample :
xs = [int(y) for y in x.size()]
padding = Variable(torch.ones(xs[0], 1, xs[2], xs[3]), requires_grad=False)
padding = padding.cuda() if x.is_cuda else padding
x = torch.cat((x, padding), 1)
### UP PASS ###
x = x if sample else torch.cat((x, self.init_padding), 1)
u_list = [self.u_init(x)]
ul_list = [self.ul_init[0](x) + self.ul_init[1](x)]
for i in range(3):
# resnet block
u_out, ul_out = self.up_layers[i](u_list[-1], ul_list[-1])
u_list += u_out
ul_list += ul_out
if i != 2:
# downscale (only twice)
u_list += [self.downsize_u_stream[i](u_list[-1])]
ul_list += [self.downsize_ul_stream[i](ul_list[-1])]
### DOWN PASS ###
u = u_list.pop()
ul = ul_list.pop()
for i in range(3):
# resnet block
u, ul = self.down_layers[i](u, ul, u_list, ul_list)
# upscale (only twice)
if i != 2 :
u = self.upsize_u_stream[i](u)
ul = self.upsize_ul_stream[i](ul)
x_out = self.nin_out(F.elu(ul))
assert len(u_list) == len(ul_list) == 0, pdb.set_trace()
return x_out
if __name__ == '__main__':
''' testing loss with tf version '''
np.random.seed(1)
xx_t = (np.random.rand(15, 32, 32, 100) * 3).astype('float32')
yy_t = np.random.uniform(-1, 1, size=(15, 32, 32, 3)).astype('float32')
x_t = Variable(torch.from_numpy(xx_t)).cuda()
y_t = Variable(torch.from_numpy(yy_t)).cuda()
loss = discretized_mix_logistic_loss(y_t, x_t)
''' testing model and deconv dimensions '''
x = torch.cuda.FloatTensor(32, 3, 32, 32).uniform_(-1., 1.)
xv = Variable(x).cpu()
ds = down_shifted_deconv2d(3, 40, stride=(2,2))
x_v = Variable(x)
''' testing loss compatibility '''
model = PixelCNN(nr_resnet=3, nr_filters=100, input_channels=x.size(1))
model = model.cuda()
out = model(x_v)
loss = discretized_mix_logistic_loss(x_v, out)
print('loss : %s' % loss.data[0])