Search code examples
pythontheanotransposeconv-neural-network

What is the difference made by using Transpose?


I was learning how convolution works then I encountered this..when I try this

rng = numpy.random.RandomState(23455)
input = T.tensor4(name='input')
w_shp = (2, 3, 9, 9)
w_bound = numpy.sqrt(3 * 9 * 9)
print w_bound
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,subsample=(2,2))
pooled=downsample.max_pool_2d(conv_out,(2,2),ignore_border=True)
output = T.nnet.sigmoid(pooled + b.dimshuffle('x', 0, 'x', 'x'))
f = theano.function([input], output)
img = Image.open('2.jpg')
img = numpy.asarray(img, dtype='float64') / 256.
l,w,r=img.shape
img_ = img.transpose(2, 0, 1).reshape(1, 3, l, w)
print img_.shape
filtered_img = f(img_)
pylab.subplot(1, 3, 1); pylab.axis('off'); pylab.imshow(img)
pylab.gray();
pylab.subplot(1, 3, 2); pylab.axis('off'); pylab.imshow(filtered_img[0, 1, :, :])
pylab.subplot(1, 3, 3); pylab.axis('off'); pylab.imshow(filtered_img[0, 0, :, :])

After convolution

When I don't Transpose input image i.e ...

img_ = img.reshape(1, 3, l, w)

Without Tanspose

Can someone explain what is the difference?


Solution

  • The original image is a 3d array, with dimensions (pixel rows, pixel columns, channels). Channels are the color channels (e.g. RGB). img.transpose(2, 0, 1) re-orders the dimensions of the array to be (channels, pixel rows, pixel columns). The rest of your code requires this initial ordering. It ensures that convolutions happen on the space of pixels, for each channel. If you omit the transpose, pixel coordinates and channels will be mixed up with each other.