Theano conv pool example
import theano
from theano import tensor as T
from theano.tensor.nnet import conv2d,sigmoid
from theano.tensor.signal.pool import pool_2d
import numpy
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
rng = numpy.random.RandomState(23455)
input = T.tensor4(name='input')
w_shp = (2, 3, 9, 9)
w_bound = numpy.sqrt(3 * 9 * 9)
W = theano.shared( numpy.asarray(
rng.uniform(
low=-1.0 / w_bound,
high=1.0 / w_bound,
size=w_shp),
dtype=input.dtype), name ='W')
b_shp = (2,)
b = theano.shared(numpy.asarray(
rng.uniform(low=-.5, high=.5, size=b_shp),
dtype=input.dtype), name ='b')
conv_out = conv2d(input, W)
poolsize=(2,2)
pooled_out = pool_2d( input=conv_out, ws=poolsize, ignore_border=True)
conv_activations = sigmoid(conv_out + b.dimshuffle('x', 0, 'x', 'x'))
f = theano.function([input], conv_activations)
pooled_activations = sigmoid(pooled_out + b.dimshuffle('x', 0, 'x', 'x'))
f2 = theano.function([input], pooled_activations)
img = Image.open(open('./3wolfmoon.jpg'))
img = numpy.asarray(img, dtype=theano.config.floatX) / 256.
input_img_ = img.transpose(2, 0, 1).reshape(1, 3, 639, 516)
filtered_img = f(input_img_)
pooled_img = f2(input_img_)
print filtered_img.shape
print pooled_img.shape
fig = plt.figure(figsize=(16,8))
plt.subplot(2, 3, 1); plt.axis('off'); plt.imshow(img)
plt.gray();
plt.subplot(2, 3, 2); plt.axis('off'); plt.imshow(filtered_img[0, 0, :, :])
plt.subplot(2, 3, 3); plt.axis('off'); plt.imshow(filtered_img[0, 1, :, :])
plt.subplot(2, 3, 4); plt.axis('off'); plt.imshow(img)
plt.gray();
plt.subplot(2, 3, 5); plt.axis('off'); plt.imshow(pooled_img[0, 0, :, :])
plt.subplot(2, 3, 6); plt.axis('off'); plt.imshow(pooled_img[0, 1, :, :])
plt.show()
(1, 2, 631, 508)
(1, 2, 315, 254)
Pool example
from theano.tensor.signal import pool
input = T.dtensor4('input')
maxpool_shape = (2, 2)
pool_out = pool.pool_2d(input, maxpool_shape, ignore_border=True)
f = theano.function([input],pool_out)
invals = np.arange(50).reshape(1, 2, 5, 5)
print 'With ignore_border set to True:'
print 'invals[0, 0, :, :] =\n', invals[0, 0, :, :]
print 'output[0, 0, :, :] =\n', f(invals)[0, 0, :, :]
pool_out = pool.pool_2d(input, maxpool_shape, ignore_border=False)
f = theano.function([input],pool_out)
print
print 'With ignore_border set to False:'
print 'invals[1, 0, :, :] =\n ', invals[0, 0, :, :]
print 'output[1, 0, :, :] =\n ', f(invals)[0, 0, :, :]
With ignore_border set to True:
invals[0, 0, :, :] =
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
output[0, 0, :, :] =
[[ 6. 8.]
[ 16. 18.]]
With ignore_border set to False:
invals[1, 0, :, :] =
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
output[1, 0, :, :] =
[[ 6. 8. 9.]
[ 16. 18. 19.]
[ 21. 23. 24.]]
Reference
History