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
| 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()
|