教程地址:http://deeplearning.stanford.edu/wiki/index.php/UFLDL_Tutorial
Exercise地址:http://deeplearning.stanford.edu/wiki/index.php/Exercise:Convolution_and_Pooling
代码
Step 1: Load learned features ——cnnExercise.m
% --------------------------- YOUR CODE HERE --------------------------
% Train the sparse autoencoder and fill the following variables with
% the optimal parameters:
optTheta = zeros(2*hiddenSize*visibleSize+hiddenSize+visibleSize, 1);
ZCAWhite = zeros(visibleSize, visibleSize);
meanPatch = zeros(visibleSize, 1);
load STL10Features.mat;
% --------------------------------------------------------------------
Step 2: Implement and test convolution and pooling
Step 2a: Implement convolution——cnnConvolve.m
function convolvedFeatures = cnnConvolve(patchDim, numFeatures, images, W, b, ZCAWhite, meanPatch)
%cnnConvolve Returns the convolution of the features given by W and b with
%the given images
%
% Parameters:
% patchDim - patch (feature) dimension
% numFeatures - number of features特征数 hiddenSize隐藏层单元数
% images - large images to convolve with, matrix in the form
% images(r, c, channel, image number) 需要被卷积的大尺寸图像
% W, b - W, b for features from the sparse autoencoder
% ZCAWhite, meanPatch - ZCAWhitening and meanPatch matrices used for
% preprocessing
%
% Returns:
% convolvedFeatures - matrix of convolved features in the form
% convolvedFeatures(featureNum, imageNum, imageRow, imageCol)
numImages = size(images, 4); %图像数量
imageDim = size(images, 1); %图像patch的row
imageChannels = size(images, 3); %图像通道数
convolvedFeatures = zeros(numFeatures, numImages, imageDim - patchDim + 1, imageDim - patchDim + 1);
% Instructions:
% Convolve every feature with every large image here to produce the
% numFeatures x numImages x (imageDim - patchDim + 1) x (imageDim - patchDim + 1)
% matrix convolvedFeatures, such that
% convolvedFeatures(featureNum, imageNum, imageRow, imageCol) is the
% value of the convolved featureNum feature for the imageNum image over
% the region (imageRow, imageCol) to (imageRow + patchDim - 1, imageCol + patchDim - 1)
%
% Expected running times:
% Convolving with 100 images should take less than 3 minutes
% Convolving with 5000 images should take around an hour
% (So to save time when testing, you should convolve with less images, as
% described earlier)
% -------------------- YOUR CODE HERE --------------------
% Precompute the matrices that will be used during the convolution. Recall
% that you need to take into account the whitening and mean subtraction
% steps
WT = W * ZCAWhite; %ZCA白化并减去均值
bT = b - WT * meanPatch;
% --------------------------------------------------------
convolvedFeatures = zeros(numFeatures, numImages, imageDim - patchDim + 1, imageDim - patchDim + 1);
for imageNum = 1:numImages
for featureNum = 1:numFeatures
% convolution of image with feature matrix for each channel
convolvedImage = zeros(imageDim - patchDim + 1, imageDim - patchDim + 1);
for channel = 1:3
% Obtain the feature (patchDim x patchDim) needed during the convolution
% ---- YOUR CODE HERE ----
feature = zeros(8,8); % You should replace this
feature = reshape(WT(featureNum,patchDim*patchDim*(channel-1)+1:patchDim*patchDim*channel),[patchDim,patchDim]);
%WT(featureNum(hiddenSize)行,(channel*patchDim*patchDim)列)
%取WT的对应第featureNum幅图像的第channel通道的图像信息,patchDim*patchDim个元素
% ------------------------
% Flip the feature matrix because of the definition of convolution, as explained later
feature = flipud(fliplr(squeeze(feature))); %翻转feature
%squeeze函数用于删除矩阵中的单一维(Remove singleton dimensions)
% Obtain the image
im = squeeze(images(:, :, channel, imageNum)); %大尺寸图像,一幅,单通道的图像信息,r*c矩阵
% Convolve "feature" with "im", adding the result to convolvedImage
% be sure to do a 'valid' convolution
% ---- YOUR CODE HERE ----
convolvedSingleChannel = conv2( im, feature, 'valid'); %对一幅图像,单通道进行卷积操作
convolvedImage = convolvedImage + convolvedSingleChannel; %将三通道的卷积值相加,作为最后的卷积结果
% ------------------------
end
% Subtract the bias unit (correcting for the mean subtraction as well)
% Then, apply the sigmoid function to get the hidden activation
% ---- YOUR CODE HERE ----
convolvedImage = sigmoid(convolvedImage + bT(featureNum)); %加上偏置项bias,用sigmoid函数计算激活值
% ------------------------
% The convolved feature is the sum of the convolved values for all channels
convolvedFeatures(featureNum, imageNum, :, :) = convolvedImage;
end
end
end
function sigm = sigmoid(x)
sigm = 1./(1+exp(-x));
end
Step 2b: Check your convolution——代码已给
Step 2c: Pooling——cnnPool.m
% -------------------- YOUR CODE HERE -------------------- resultDim = floor(convolvedDim / poolDim); %池化后图像的维数 for imageNum = 1:numImages for featureNum = 1:numFeatures for poolRow = 1 : resultDim %池化后图像的row for poolCol = 1 : resultDim %%池化后图像的col patchRow = (poolRow - 1) * poolDim + 1; %池化图像块的row起点 patchCol = (poolCol - 1) * poolDim + 1; %池化图像块的col起点 poolPatch = convolvedFeatures(featureNum, imageNum, patchRow:(patchRow + poolDim - 1), patchCol:(patchCol + poolDim - 1)); pooledFeatures(featureNum, imageNum, poolRow, poolCol) = mean(poolPatch(:)); end end end end %---------------------------------------------------------------
Step 2d: Check your pooling——代码已给
Step 3: Convolve and pool with the dataset——代码已给
Step 4: Use pooled features for classification——代码已给
Step 5: Test classifier——代码已给