-
Notifications
You must be signed in to change notification settings - Fork 2
/
fractallayer_nosquare.py
executable file
·281 lines (220 loc) · 11.5 KB
/
fractallayer_nosquare.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import numpy as np
from pylearn2.models.mlp import max_pool
import theano.tensor as T
import theano.sandbox
import theano.gof
import theano
from theano.sandbox.neighbours import images2neibs
from layerbase import Layer, Param, VisLayer, LayerbasedDropOut
from theano.tensor.nnet import conv
INF = 1e10
b3tensor = T.TensorType(dtype = theano.config.floatX, broadcastable = [])
def dtypeX(val):
return val + 0.0
class StacksampleFractal(Layer):
def __init__(self, input, input_shape = None, feedval = 0.0):
if isinstance(input, Layer):
self.input = input.output
if input_shape == None:
input_shape = input.output_shape
else:
self.input = input
self.input_shape = input_shape
##Only square image allowed
#assert input_shape[2]==input_shape[3]
#Extend one pixel at each direction
shapeext = input_shape[0], input_shape[1], input_shape[2]+2, input_shape[3]+2
inputext = T.alloc(dtypeX(-INF), *shapeext)
inputext = T.set_subtensor(inputext[:,:,1:input_shape[2]+1,1:input_shape[3]+1], self.input)
output_cmb = max_pool(inputext, (3,3), (1,1), shapeext[2:])
self.output_cmb = output_cmb
#Separate output to 4 channels
c00 = output_cmb[:,:,::2,::2]
c01 = output_cmb[:,:,::2,1::2]
c10 = output_cmb[:,:,1::2,::2]
c11 = output_cmb[:,:,1::2,1::2]
self.one_channel = input_shape[0], input_shape[1], (input_shape[2]+1)/2, (input_shape[3]+1)/2
#Combine, 2 conditions: even/odd
odd2 = (input_shape[2]&1)==1
odd3 = (input_shape[3]&1)==1
joined = T.alloc(dtypeX(feedval), *((input_shape[0]*4,)+self.one_channel[1:4]))
joined = T.set_subtensor(joined[0:self.one_channel[0],:,:,:], c00)
joined = T.set_subtensor(joined[self.one_channel[0]:self.one_channel[0]*2,:,:,:-1] if odd3 else joined[self.one_channel[0]:self.one_channel[0]*2], c01)
joined = T.set_subtensor(joined[self.one_channel[0]*2:self.one_channel[0]*3,:,:-1,:] if odd2 else joined[self.one_channel[0]*2:self.one_channel[0]*3], c10)
if odd2:
if odd3:
joined = T.set_subtensor(joined[self.one_channel[0]*3:self.one_channel[0]*4,:,:-1,:-1], c11)
else:
joined = T.set_subtensor(joined[self.one_channel[0]*3:self.one_channel[0]*4,:,:-1,:], c11)
else:
if odd3:
joined = T.set_subtensor(joined[self.one_channel[0]*3:self.one_channel[0]*4,:,:,:-1], c11)
else:
joined = T.set_subtensor(joined[self.one_channel[0]*3:self.one_channel[0]*4,:,:,:], c11)
self.output = joined
self.output_shape = input_shape[0]*4, self.one_channel[1], self.one_channel[2], self.one_channel[3]
class DestacksampleFractal(Layer):
def __init__(self, input, stacksamplelayer, input_shape = None):
if isinstance(input, Layer):
self.input = input.output
if input_shape == None:
input_shape = input.output_shape
else:
self.input = input
self.input_shape = input_shape
assert isinstance(stacksamplelayer, StacksampleFractal)
#Insert back by shape
odd2 = (stacksamplelayer.input_shape[2]&1)==1
odd3 = (stacksamplelayer.input_shape[3]&1)==1
self.output_shape = stacksamplelayer.input_shape[0], input_shape[1], (input_shape[2]*2-1) if odd2 else (input_shape[2]*2), (input_shape[3]*2-1) if odd3 else (input_shape[3]*2)
self.output = T.alloc(dtypeX(0.0), *self.output_shape)
c00 = self.input[0:stacksamplelayer.one_channel[0]]
c01 = self.input[stacksamplelayer.one_channel[0]:stacksamplelayer.one_channel[0]*2]
c10 = self.input[stacksamplelayer.one_channel[0]*2:stacksamplelayer.one_channel[0]*3]
c11 = self.input[stacksamplelayer.one_channel[0]*3:stacksamplelayer.one_channel[0]*4]
self.output = T.set_subtensor(self.output[:,:,::2,::2], c00)
self.output = T.set_subtensor(self.output[:,:,::2,1::2], c01[:,:,:,:-1] if odd3 else c01)
self.output = T.set_subtensor(self.output[:,:,1::2,::2], c10[:,:,:-1,:] if odd2 else c10)
if odd2:
if odd3:
self.output = T.set_subtensor(self.output[:,:,1::2,1::2], c11[:,:,:-1,:-1])
else:
self.output = T.set_subtensor(self.output[:,:,1::2,1::2], c11[:,:,:-1,:])
else:
if odd3:
self.output = T.set_subtensor(self.output[:,:,1::2,1::2], c11[:,:,:,:-1])
else:
self.output = T.set_subtensor(self.output[:,:,1::2,1::2], c11[:,:,:,:])
class ShrinkshapeMeanFractal(Layer):
def __init__(self,input,input_shape = None):
if isinstance(input, Layer):
self.input = input.output
if input_shape == None:
input_shape = input.output_shape
else:
self.input = input
self.input_shape = input_shape
##Only square image allowed
#assert input_shape[2]==input_shape[3]
#Extend one pixel at each direction
shapeext = input_shape[0], input_shape[1], input_shape[2]+2, input_shape[3]+2
inputext = T.alloc(dtypeX(-INF), *shapeext)
inputext = T.set_subtensor(inputext[:,:,1:input_shape[2]+1,1:input_shape[3]+1], self.input)
self.output_shape = input_shape[0], input_shape[1], (input_shape[2]+1)/2, (input_shape[3]+1)/2
self.output = images2neibs(inputext, (3,3), (2,2), 'ignore_borders').mean(axis=-1)
self.output = self.output.reshape(self.output_shape)
class ShrinkshapeFractal(Layer):
def __init__(self,input,input_shape = None):
if isinstance(input, Layer):
self.input = input.output
if input_shape == None:
input_shape = input.output_shape
else:
self.input = input
self.input_shape = input_shape
##Only square image allowed
#assert input_shape[2]==input_shape[3]
#Extend one pixel at each direction
shapeext = input_shape[0], input_shape[1], input_shape[2]+1, input_shape[3]+1
inputext = T.alloc(dtypeX(-INF), *shapeext)
inputext = T.set_subtensor(inputext[:,:,1:input_shape[2]+1,1:input_shape[3]+1], self.input)
self.output = max_pool(inputext, (3,3), (2,2), shapeext[2:])
self.output_shape = input_shape[0], input_shape[1], (input_shape[2]+1)/2, (input_shape[3]+1)/2
print self.output_shape
class ExpandshapeFractal(Layer):
def __init__(self, input, shrinksamplelayer, input_shape=None, calibrate = True):
if isinstance(input, Layer):
self.input = input.output
if input_shape == None:
input_shape = input.output_shape
else:
self.input = input
self.input_shape = input_shape
assert isinstance(shrinksamplelayer, (ShrinkshapeFractal, ShrinkshapeMeanFractal))
if calibrate:
cali=2
else:
cali=0
self.output_shape = input_shape[0], input_shape[1]+cali, shrinksamplelayer.input_shape[2], shrinksamplelayer.input_shape[3]
print self.output_shape
output = T.alloc(dtypeX(0.0), *self.output_shape)
odd2 = (shrinksamplelayer.input_shape[2]&1)==1
odd3 = (shrinksamplelayer.input_shape[3]&1)==1
#Expand data 4 fold
output = T.set_subtensor(output[:,:input_shape[1],::2,::2], self.input)
output = T.set_subtensor(output[:,:input_shape[1],::2,1::2], self.input[:,:,:,:-1] if odd3 else self.input)
output = T.set_subtensor(output[:,:input_shape[1],1::2,::2], self.input[:,:,:-1,:] if odd2 else self.input)
t = self.input[:,:,:-1,:] if odd2 else self.input
t = t[:,:,:,:-1] if odd3 else t
output = T.set_subtensor(output[:,:input_shape[1],1::2,1::2],t)
#Feed calibrate data
if calibrate:
#HACK, strange
dval = T.alloc(dtypeX(1.0), input_shape[0], shrinksamplelayer.input_shape[2], (shrinksamplelayer.input_shape[3])/2)
output = T.set_subtensor(output[:,input_shape[1],:,1::2], dval)
dval = T.alloc(dtypeX(1.0), input_shape[0], (shrinksamplelayer.input_shape[2])/2, shrinksamplelayer.input_shape[3])
output = T.set_subtensor(output[:,input_shape[1]+1,1::2], dval)
self.output = output
class AggregationLayer(Layer):
def __init__(self, *layers):
channels = 0
for i in layers:
assert isinstance(i, Layer)
channels += i.output_shape[1]
self.output_shape = layers[0].output_shape[0], channels, layers[0].output_shape[2], layers[0].output_shape[3]
self.output = T.alloc(dtypeX(0.0), *self.output_shape)
channels = 0
for i in layers:
self.output = T.set_subtensor(self.output[:,channels:channels+i.output_shape[1]], i.output)
channels += i.output_shape[1]
class ConvKeepLayer(Layer, Param, VisLayer):
def __init__(self, rng, input, filter_shape, image_shape = None, Nonlinear = "tanh", zeroone = False, inc=[0], dropout = False, dropoutrnd = None):
if isinstance(input, Layer):
self.input = input.output
if image_shape==None:
image_shape = input.output_shape
else:
self.input = input
print image_shape[1]
#assert image_shape[1] == filter_shape[1]
assert filter_shape[2]%2 == 1
assert filter_shape[3]%2 == 1
med = (filter_shape[2]-1)/2,(filter_shape[3]-1)/2
fan_in = np.prod(filter_shape[1:])
W_values = np.asarray(rng.uniform(
low=-np.sqrt(0.5/fan_in),
high=np.sqrt(0.5/fan_in),
size=filter_shape), dtype=theano.config.floatX)
self.W = theano.shared(value=W_values, name='W_%s'%inc[0])
b_values = np.zeros((filter_shape[0],), dtype=theano.config.floatX)
self.b = theano.shared(value=b_values, name='b_%s'%inc[0])
conv_out = conv.conv2d(self.input, self.W,
filter_shape=filter_shape, image_shape=image_shape, border_mode="full")
#Get middle area
conv_out = conv_out[:,:,med[0]:-med[0],med[1]:-med[1]]
if Nonlinear:
self.output = T.tanh(conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
if zeroone:
self.output = (self.output+1) * 0.5
else:
self.output = conv_out + self.b.dimshuffle('x', 0, 'x', 'x')
self.output_shape = (image_shape[0], filter_shape[0], image_shape[2], image_shape[3])
#dropout = False
if not (dropout is False): #Embed a layerwise dropout layer
self.output = LayerbasedDropOut(self, dropoutrnd, dropout).output
self.params = [self.W, self.b]
inc[0] = inc[0]+1
if __name__=="__main__":
import numpy as np
import theano
a=np.array([[[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]]]],'f')
#Make subsample fractal test layer
inp = T.tensor4("inp")
l1 = StacksampleFractal(inp, a.shape)
l2 = DestacksampleFractal(l1, l1)
f = theano.function([inp], [l1.output, l2.output])
print f(a)
l1 = ShrinkshapeFractal(inp, a.shape)
l2 = ExpandshapeFractal(l1, l1)
f = theano.function([inp], [l1.output, l2.output])
print f(a)