Exercise:Convolution and Pooling 代码示例

Exercise:Convolution and Pooling 代码示例

练习参考Convolution and Pooling

 

       这个练习用于处理大型图像,需要编写代码实现卷积特征提取和池化(采样)两个过程。在上一个练习中,通过小尺寸图像样本训练线性编码器得到的权重矩阵W、偏差向量b以及预处理的ZCA白化矩阵ZCAWhite、均值向量meanPatch存为文件STL10Features.mat。此练习利用STL10Features.mat中的特征与大图作卷积生成卷积特征矩阵。

       卷积计算在cnnConvolve.m中实现。对每张图像的每个特征(隐藏单元)的每个RGB分量(三层循环),从W中提取对应的卷积核,将其与大图做卷积。这里计算大图与卷积核的卷积与数学中的矩阵卷积不同,是两矩阵的对应项直接相乘再求和,具体过程如下图:

       每个RGB分量计算的卷积累加起来,其结果加上特征的偏置后取sigmoid就得到了一张图像的一个特征的卷积矩阵。三层循环结束后就得到了全部图像的卷积特征矩阵族。

cnnConvolve.m

 

[plain] view plaincopy
 
  1. function convolvedFeatures = cnnConvolve(patchDim, numFeatures, images, W, b, ZCAWhite, meanPatch)  
  2. %cnnConvolve Returns the convolution of the features given by W and b with  
  3. %the given images  
  4. %  
  5. % Parameters:  
  6. %  patchDim - patch (feature) dimension  
  7. %  numFeatures - number of features  
  8. %  images - large images to convolve with, matrix in the form  
  9. %           images(r, c, channel, image number)  
  10. %  W, b - W, b for features from the sparse autoencoder  
  11. %  ZCAWhite, meanPatch - ZCAWhitening and meanPatch matrices used for  
  12. %                        preprocessing  
  13. %  
  14. % Returns:  
  15. %  convolvedFeatures - matrix of convolved features in the form  
  16. %                      convolvedFeatures(featureNum, imageNum, imageRow, imageCol)  
  17.   
  18. numImages = size(images, 4);  
  19. imageDim = size(images, 1);  
  20. imageChannels = size(images, 3);  
  21.   
  22. % Instructions:  
  23. %   Convolve every feature with every large image here to produce the   
  24. %   numFeatures x numImages x (imageDim - patchDim + 1) x (imageDim - patchDim + 1)   
  25. %   matrix convolvedFeatures, such that   
  26. %   convolvedFeatures(featureNum, imageNum, imageRow, imageCol) is the  
  27. %   value of the convolved featureNum feature for the imageNum image over  
  28. %   the region (imageRow, imageCol) to (imageRow + patchDim - 1, imageCol + patchDim - 1)  
  29. %  
  30. % Expected running times:   
  31. %   Convolving with 100 images should take less than 3 minutes   
  32. %   Convolving with 5000 images should take around an hour  
  33. %   (So to save time when testing, you should convolve with less images, as  
  34. %   described earlier)  
  35.   
  36. % -------------------- YOUR CODE HERE --------------------  
  37. % Precompute the matrices that will be used during the convolution. Recall  
  38. % that you need to take into account the whitening and mean subtraction  
  39. % steps  
  40.   
  41. % patchDim    8  
  42. % numFeatures 400; is hiddenSize  
  43. % images      images(r, c, channel, image number)  
  44. % W           hiddenSize X visibleSize  
  45. % b           hiddenSize X 1  
  46. % ZCAWhite    visibleSize X visibleSize  
  47. % meanPatch   visibleSize X 1  
  48. WT = W * ZCAWhite;  
  49. bias = b - WT * meanPatch;  
  50. patchSize = patchDim * patchDim;  
  51.   
  52. % --------------------------------------------------------  
  53.   
  54. convolvedFeatures = zeros(numFeatures, numImages, imageDim - patchDim + 1, imageDim - patchDim + 1);  
  55. for imageNum = 1:numImages  
  56.   for featureNum = 1:numFeatures  
  57.   
  58.     % convolution of image with feature matrix for each channel  
  59.     convolvedImage = zeros(imageDim - patchDim + 1, imageDim - patchDim + 1);  
  60.     for channel = 1:imageChannels  
  61.   
  62.       % Obtain the feature (patchDim x patchDim) needed during the convolution  
  63.       % ---- YOUR CODE HERE ----  
  64.       feature = reshape(WT(featureNum,(channel-1)*patchSize+1:channel*patchSize), patchDim, patchDim);       
  65.       % ------------------------  
  66.   
  67.       % Flip the feature matrix because of the definition of convolution, as explained later  
  68.       feature = rot90(squeeze(feature),2);  
  69.         
  70.       % Obtain the image  
  71.       im = squeeze(images(:, :, channel, imageNum));  
  72.   
  73.       % Convolve "feature" with "im", adding the result to convolvedImage  
  74.       % be sure to do a 'valid' convolution  
  75.       % ---- YOUR CODE HERE ----  
  76.       convolvedImage = convolvedImage + conv2(im, feature, 'valid');   
  77.       % ------------------------  
  78.   
  79.     end  
  80.       
  81.     % Subtract the bias unit (correcting for the mean subtraction as well)  
  82.     % Then, apply the sigmoid function to get the hidden activation  
  83.     % ---- YOUR CODE HERE ----  
  84.     convolvedImage = sigmoid(convolvedImage + bias(featureNum));  
  85.     % ------------------------  
  86.       
  87.     % The convolved feature is the sum of the convolved values for all channels  
  88.     convolvedFeatures(featureNum, imageNum, :, :) = convolvedImage;  
  89.   end  
  90. end  
  91.   
  92.   
  93. end  
  94.   
  95. function sigm = sigmoid(x)  
  96.     
  97.     sigm = 1 ./ (1 + exp(-x));  
  98. end  

 

       池化采用平均采样。对每个卷积特征矩阵划分为若干个池化区域,每个区域取特征均值作为一个采样特征。在采样特征上做Softmax分类及测试。

cnnPool.m

 

[plain] view plaincopy
 
    1. for imageNum = 1:numImages  
    2.   for featureNum = 1:numFeatures  
    3.       temp = conv2(squeeze(convolvedFeatures(featureNum,imageNum,:,:)),ones(poolDim)/poolDim/poolDim,'valid');  
    4.       pooledFeatures(featureNum,imageNum,:,:) = temp(1:poolDim:end,1:poolDim:end);  
    5.   end  
    6. end  
posted @ 2015-11-18 15:47  菜鸡一枚  阅读(374)  评论(0)    收藏  举报