Convolution Neural Network (CNN) 原理与实现



  本文结合Deep learning的一个应用,Convolution Neural Network 进行一些基本应用,参考Lecun的Document 0.1进行部分拓展,与结果展示(inPython)。

分为以下几部分:

1. Convolution(卷积)

2. Pooling(降采样过程)

3. CNN结构

4.  跑实验

下面分别介绍。


PS:本篇blog为ese机器学习短期班参考资料(20140516课程),本文只是简要讲最naive最simple的思想,重在实践部分,原理课上详述。


1. Convolution(卷积)

类似于高斯卷积,对imagebatch中的所有image进行卷积。对于一张图,其所有feature map用一个filter卷成一张feature map。 如下面的代码,对一个imagebatch(含两张图)进行操作,每个图初始有3张feature map(R,G,B), 用两个9*9的filter进行卷积,结果是,每张图得到两个feature map。

卷积操作由theano的conv.conv2d实现,这里我们用随机参数W,b。结果有点像edge detector是不是?

Code: (详见注释)


[python]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. # -*- coding: utf-8 -*-  
  2. """ 
  3. Created on Sat May 10 18:55:26 2014 
  4.  
  5. @author: rachel 
  6.  
  7. Function: convolution option of two pictures with same size (width,height) 
  8. input: 3 feature maps (3 channels <RGB> of a picture) 
  9. convolution: two 9*9 convolutional filters 
  10. """  
  11.   
  12. from theano.tensor.nnet import conv  
  13. import theano.tensor as T  
  14. import numpy, theano  
  15.   
  16.   
  17. rng = numpy.random.RandomState(23455)  
  18.   
  19. # symbol variable  
  20. input = T.tensor4(name = 'input')  
  21.   
  22. # initial weights  
  23. w_shape = (2,3,9,9#2 convolutional filters, 3 channels, filter shape: 9*9  
  24. w_bound = numpy.sqrt(3*9*9)  
  25. W = theano.shared(numpy.asarray(rng.uniform(low = -1.0/w_bound, high = 1.0/w_bound,size = w_shape),  
  26.                                 dtype = input.dtype),name = 'W')  
  27.   
  28. b_shape = (2,)  
  29. b = theano.shared(numpy.asarray(rng.uniform(low = -.5, high = .5, size = b_shape),  
  30.                                 dtype = input.dtype),name = 'b')  
  31.                                   
  32. conv_out = conv.conv2d(input,W)  
  33.   
  34. #T.TensorVariable.dimshuffle() can reshape or broadcast (add dimension)  
  35. #dimshuffle(self,*pattern)  
  36. # >>>b1 = b.dimshuffle('x',0,'x','x')  
  37. # >>>b1.shape.eval()  
  38. # array([1,2,1,1])  
  39. output = T.nnet.sigmoid(conv_out + b.dimshuffle('x',0,'x','x'))  
  40. f = theano.function([input],output)  
  41.   
  42.   
  43.   
  44.   
  45.   
  46. # demo  
  47. import pylab  
  48. from PIL import Image  
  49. #minibatch_img = T.tensor4(name = 'minibatch_img')  
  50.   
  51. #-------------img1---------------  
  52. img1 = Image.open(open('//home//rachel//Documents//ZJU_Projects//DL//Dataset//rachel.jpg'))  
  53. width1,height1 = img1.size  
  54. img1 = numpy.asarray(img1, dtype = 'float32')/256. # (height, width, 3)  
  55.   
  56. # put image in 4D tensor of shape (1,3,height,width)  
  57. img1_rgb = img1.swapaxes(0,2).swapaxes(1,2).reshape(1,3,height1,width1) #(3,height,width)  
  58.   
  59.   
  60. #-------------img2---------------  
  61. img2 = Image.open(open('//home//rachel//Documents//ZJU_Projects//DL//Dataset//rachel1.jpg'))  
  62. width2,height2 = img2.size  
  63. img2 = numpy.asarray(img2,dtype = 'float32')/256.  
  64. img2_rgb = img2.swapaxes(0,2).swapaxes(1,2).reshape(1,3,height2,width2) #(3,height,width)  
  65.   
  66.   
  67.   
  68. #minibatch_img = T.join(0,img1_rgb,img2_rgb)  
  69. minibatch_img = numpy.concatenate((img1_rgb,img2_rgb),axis = 0)  
  70. filtered_img = f(minibatch_img)  
  71.   
  72.   
  73. # plot original image and two convoluted results  
  74. pylab.subplot(2,3,1);pylab.axis('off');  
  75. pylab.imshow(img1)  
  76.   
  77. pylab.subplot(2,3,4);pylab.axis('off');  
  78. pylab.imshow(img2)  
  79.   
  80. pylab.gray()  
  81. pylab.subplot(2,3,2); pylab.axis("off")  
  82. pylab.imshow(filtered_img[0,0,:,:]) #0:minibatch_index; 0:1-st filter  
  83.   
  84. pylab.subplot(2,3,3); pylab.axis("off")  
  85. pylab.imshow(filtered_img[0,1,:,:]) #0:minibatch_index; 1:1-st filter  
  86.   
  87. pylab.subplot(2,3,5); pylab.axis("off")  
  88. pylab.imshow(filtered_img[1,0,:,:]) #0:minibatch_index; 0:1-st filter  
  89.   
  90. pylab.subplot(2,3,6); pylab.axis("off")  
  91. pylab.imshow(filtered_img[1,1,:,:]) #0:minibatch_index; 1:1-st filter  
  92. pylab.show()  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值