function convolvedFeatures = cnnConvolve(patchDim, numFeatures, images, W, b, ZCAWhite, meanPatch)
%cnnConvolve Returns the convolution ofthe features givenby W and b with
%thegiven images
%
% Parameters:
% patchDim - patch (feature) dimension
% numFeatures - numberof features
% images - large images to convolve with, matrix inthe form
% images(r, c, channel, image number)
% W, b - W, b for features fromthe sparse autoencoder
% ZCAWhite, meanPatch - ZCAWhitening and meanPatch matrices used for
% preprocessing
%
% Returns:
% convolvedFeatures - matrix of convolved features inthe form
% convolvedFeatures(featureNum, imageNum, imageRow, imageCol)numImages = size(images, 4);
imageDim = size(images, 1);
imageChannels = size(images, 3);%convolvedFeatures = zeros(numFeatures, numImages, imageDim - patchDim + 1, imageDim - patchDim + 1);% Instructions:
% Convolve every feature withevery 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) isthe
% value ofthe convolved featureNum feature forthe imageNum image over
% the region (imageRow, imageCol) to (imageRow + patchDim - 1, imageCol + patchDim - 1)
%
% Expected runningtimes:
% Convolving with100 images should take less than3 minutes
% Convolving with5000 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
% stepsWT=W*ZCAWhite;
b_mean = b - WT*meanPatch;% --------------------------------------------------------convolvedFeatures = zeros(numFeatures, numImages, imageDim - patchDim + 1, imageDim - patchDim + 1);
for imageNum = 1:numImagesfor featureNum = 1:numFeatures% convolution of image with feature matrix for each channelconvolvedImage = zeros(imageDim - patchDim + 1, imageDim - patchDim + 1);for channel = 1:imageChannels% Obtain the feature (patchDim x patchDim) needed during the convolution% ---- YOUR CODE HERE ----feature = zeros(8,8); % You should replace thisoffset=(channel-1)*patchDim*patchDim;fea=WT(featureNum,offset+1:offset+patchDim*patchDim);feature=reshape(fea,patchDim,patchDim);% ------------------------% Flip the feature matrix because ofthe definition of convolution, as explained laterfeature = flipud(fliplr(squeeze(feature)));% Obtain the imageim = squeeze(images(:, :, channel, imageNum));% Convolve "feature"with"im", adding theresultto convolvedImage% be sure to do a 'valid' convolution% ---- YOUR CODE HERE ----convolvedImage=convolvedImage+conv2( im,feature,'valid' );% ------------------------end% Subtract the bias unit (correcting forthe mean subtraction as well)% Then, apply the sigmoid function togetthe hidden activation% ---- YOUR CODE HERE ----convolvedImage=sigmoid(convolvedImage+b_mean(featureNum));% ------------------------% The convolved feature isthe sum ofthe convolved values for all channelsconvolvedFeatures(featureNum, imageNum, :, :) = convolvedImage;endendendfunction sigm=sigmoid(x)sigm=1./(1+exp(-x));
end
step3:池化
functionpooledFeatures = cnnPool(poolDim, convolvedFeatures)%cnnPool Pools the given convolved features%% Parameters:% poolDim - dimension of pooling region% convolvedFeatures - convolved features to pool (as given by cnnConvolve)% convolvedFeatures(featureNum, imageNum, imageRow, imageCol)%% Returns:% pooledFeatures - matrix of pooled features in the form% pooledFeatures(featureNum, imageNum, poolRow, poolCol)% numImages = size(convolvedFeatures, 2);
numFeatures = size(convolvedFeatures, 1);
convolvedDim = size(convolvedFeatures, 3);pooledFeatures = zeros(numFeatures, numImages, floor(convolvedDim / poolDim), floor(convolvedDim / poolDim));numRegin=floor(convolvedDim / poolDim);
for featureNum=1:numFeaturesfor imageNum=1:numImagesfor row=1:numReginfor col=1:numReginregin=convolvedFeatures(featureNum, imageNum,(row-1)*poolDim+1:row*poolDim,(col-1)*poolDim+1:col*poolDim);pooledFeatures(featureNum,imageNum,row,col)=mean(regin(:));endendendendend