-
Notifications
You must be signed in to change notification settings - Fork 52
/
transformerlayer_test.py
73 lines (57 loc) · 1.76 KB
/
transformerlayer_test.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
import os
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
import lasagne
import numpy as np
import theano.tensor as T
from transformerlayer import TransformerLayer
from PIL import Image
import theano
theano.config.profile=True
height = 640
width = 480
I = Image.open("cat.jpg")
I = I.resize((height, width))
#I = np.asarray(I).reshape((height, width)).astype('float32')
I = np.asarray(I) / 255.
I = I.astype('float32')
cmap = None
num_batch = 10
downsample = 1.3
# repeat Image 3 times to simulate batches.
X_in = np.concatenate([[I.copy() for _ in range(num_batch)]])
X_sym = T.tensor4()
# setup network
l_in = lasagne.layers.InputLayer(X_in.shape)
# add channels dimension
l_dim = lasagne.layers.DimshuffleLayer(l_in, (0, 3, 1, 2))
# create bias that is identity transform
b = np.zeros((2, 3), dtype='float32')
b[0, 0] = 0.5
b[1, 1] = 0.5
b[0, 1] = 0.5
b = b.flatten()[:6]
l_loc1 = lasagne.layers.DenseLayer(
l_dim,
num_units=20, nonlinearity=None, W=lasagne.init.Constant(0.01))
l_loc2 = lasagne.layers.DenseLayer(
l_loc1, num_units=6, b=b, W=lasagne.init.Constant(0.0))
l_trans = TransformerLayer([l_dim, l_loc2],
downsample_factor=downsample)
l_dim = lasagne.layers.DimshuffleLayer(l_trans, (0, 2, 3, 1))
out, theta = lasagne.layers.get_output([l_dim, l_loc2], X_sym, deterministic=True)
grads = T.grad(T.sum(out), lasagne.layers.get_all_params(l_trans))
print grads
f = theano.function([X_sym], [out, theta])
output, A = f(X_in)
print "Expected output_train shape", l_dim.output_shape
print "Actual output_train shape", output.shape
print "Transformation matrix:"
print A
print "-"*20
print "Output dtype:", output.dtype
import pylab
pylab.figure()
pylab.gray()
pylab.imshow(output[4],
interpolation='none', cmap=cmap)
pylab.show()