In Theano, how to do Reverse-MaxPooling in the Convolutional MaxPooling Auto-Encoder
MaxPooling Layer always follows Convolutional Layer. But in the Convolutional MaxPooling Auto-Encoder, how to do reverse or Un-MaxPooling?
Here is the method:
1 import numpy as np 2 import theano 3 import theano.tensor as T 4 import theano.sandbox.neighbours as nbr 5 6 poolsize = (2, 2) 7 print 'neighbor box:' 8 print poolsize 9 # Defining batch images 10 images = T.tensor4('images') 11 #conver images to neighbors matrix 12 neibs = nbr.images2neibs(images, neib_shape=poolsize) 13 14 # Constructing theano function 15 window_function = theano.function([images], neibs) 16 17 # Input tensor image width and height are 4 18 im_val = np.random.randint(0, 100, (2, 1, 4, 4)) 19 im_val = im_val.astype(np.float32) 20 # Function application 21 neibs_val = window_function(im_val) 22 23 print 'original images:' 24 print im_val 25 print 'neighbor flatten:' 26 print neibs_val 27 28 29 neibs0 = T.zeros_like(neibs) 30 #get maximum of neighbors 31 am = T.argmax(neibs, axis=1) 32 neibs1 = T.set_subtensor(neibs0[T.arange(neibs.shape[0]), am], 1) 33 neibs *= neibs1 #suppress the non-maximum 34 35 #convert neighbors matrix to images 36 im_new = nbr.neibs2images(neibs, poolsize, im_val.shape) 37 # Theano function definition 38 inv_window = theano.function([images], im_new) 39 # Function application 40 im_new_val = inv_window(im_val) 41 42 print 'non maximum suppression result:' 43 print im_new_val
The following is the output:
1 neighbor box: 2 (2, 2) 3 original images: 4 [[[[ 15. 46. 46. 37.] 5 [ 73. 93. 2. 91.] 6 [ 97. 93. 78. 7.] 7 [ 0. 24. 34. 26.]]] 8 9 10 [[[ 70. 46. 2. 93.] 11 [ 11. 54. 29. 83.] 12 [ 0. 48. 69. 95.] 13 [ 49. 48. 10. 19.]]]] 14 neighbor flatten: 15 [[ 15. 46. 73. 93.] 16 [ 46. 37. 2. 91.] 17 [ 97. 93. 0. 24.] 18 [ 78. 7. 34. 26.] 19 [ 70. 46. 11. 54.] 20 [ 2. 93. 29. 83.] 21 [ 0. 48. 49. 48.] 22 [ 69. 95. 10. 19.]] 23 non maximum suppression result: 24 [[[[ 0. 0. 0. 0.] 25 [ 0. 93. 0. 91.] 26 [ 97. 0. 78. 0.] 27 [ 0. 0. 0. 0.]]] 28 29 30 [[[ 70. 0. 0. 93.] 31 [ 0. 0. 0. 0.] 32 [ 0. 0. 0. 95.] 33 [ 49. 0. 0. 0.]]]]