-
Notifications
You must be signed in to change notification settings - Fork 25
/
model.py
161 lines (123 loc) · 6.63 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
import torch.nn as nn
from layers import *
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):
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
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 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
class random_classifier(nn.Module):
def __init__(self, NUM_CLASSES):
super(random_classifier, self).__init__()
self.NUM_CLASSES = NUM_CLASSES
self.fc = nn.Linear(3, NUM_CLASSES)
print("Random classifier initialized")
# create a folder
if 'models' not in os.listdir():
os.mkdir('models')
torch.save(self.state_dict(), 'models/conditional_pixelcnn.pth')
def forward(self, x, device):
return torch.randint(0, self.NUM_CLASSES, (x.shape[0],)).to(device)