-
Notifications
You must be signed in to change notification settings - Fork 17
/
convolution.py
89 lines (57 loc) · 2.06 KB
/
convolution.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
# -*- coding: utf-8 -*-
"""
Created on Thu May 15 09:46:15 2014
@author: Terry
Function: convolution
"""
from theano.tensor.nnet import conv
import theano.tensor as T
import numpy, theano
rng = numpy.random.RandomState(23455)
input = T.tensor4(name = 'input')
w_shape = (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_shape),
dtype = input.dtype),name = 'W')
b_shape = (2,)
b = theano.shared(numpy.asarray(rng.uniform(low = -.5, high = .5, size = b_shape),
dtype = input.dtype),name = 'b')
conv_out = conv.conv2d(input,W)
output = T.nnet.sigmoid(conv_out + b.dimshuffle('x',0,'x','x'))
f = theano.function([input],output)
import pylab
from PIL import Image
from matplotlib.pyplot import *
img = Image.open('C://Users//Terry//Desktop//img2.jpg')
width,height = img.size
img = numpy.asarray(img, dtype = 'float32')/256. # (height, width, 3)
img_rgb = img.swapaxes(0,2).swapaxes(1,2)
minibatch_img = img_rgb.reshape(1,3,height,width)
filtered_img = f(minibatch_img)
pylab.figure(1)
pylab.subplot(1,3,1);pylab.axis('off');
pylab.imshow(img)
title('origin image')
pylab.gray()
pylab.subplot(2,3,2); pylab.axis("off")
pylab.imshow(filtered_img[0,0,:,:]) #0:minibatch_index; 0:1-st filter
title('convolution 1')
pylab.subplot(2,3,3); pylab.axis("off")
pylab.imshow(filtered_img[0,1,:,:]) #0:minibatch_index; 1:1-st filter
title('convolution 2')
# maxpooling
from theano.tensor.signal import downsample
input = T.tensor4('input')
maxpool_shape = (2,2)
pooled_img = downsample.max_pool_2d(input,maxpool_shape,ignore_border = False)
maxpool = theano.function(inputs = [input],
outputs = [pooled_img])
pooled_res = numpy.squeeze(maxpool(filtered_img))
#pylab.figure(2)
pylab.subplot(235);pylab.axis('off');
pylab.imshow(pooled_res[0,:,:])
title('down sampled 1')
pylab.subplot(236);pylab.axis('off');
pylab.imshow(pooled_res[1,:,:])
title('down sampled 2')
pylab.show()