深度学习笔记7 Working with Large Images 卷积特征提取

来源:互联网 发布:淘宝名字大全2016款女 编辑:程序博客网 时间:2024/06/08 04:41

动机

在用稀疏自编码器对8*8 或28*28等小图像提取特征时是可行的,但是若对大图像学习整幅图像的特征,将会非常耗时。因此需要把这种“全连接”的设计改为“部分联通”的网络。

卷积

自然图像有其固有特性——图像的一部分统计特征与其他部分是一样的,这也意味着我们在这一部分上学习的特征也能用在另一部分上,所以对这个图像上的所以位置,我们都能使用同样的学习特征。
比如,可以8*8的样本中学习到一些特征,并把这个特征应用到图像的任意地方去。特别是,我们可以从8*8的样本中学习的特征跟原来的大尺寸图像做卷积。

池化

得到的卷积特征就可以去训练分类器了,但是由于卷积特征的维数很高,除了计算慢之外,还容易出现过拟合。因此把每一个的卷积特征进行池化。如卷积特征是89^2*400,表示400个特征,每个特征有89^2维。池化就是对每一特征89^2,平均分成若干固定大小不相干的块,可以用块内的平均或最大值代表,这样若分成了10块,则89^2维就变成了10维。
池化具有平移性

练习

这个部分感觉做了练习才理解的清楚了。
step1:从大图像中随机提取8*8的小块–> ZCA白化–>用SparseEncoder提取出特征。
step2:实现卷积

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%  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);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% stepsWT=W*ZCAWhite;b_mean = 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:imageChannels      % Obtain the feature (patchDim x patchDim) needed during the convolution      % ---- YOUR CODE HERE ----      feature = zeros(8,8); % You should replace this      offset=(channel-1)*patchDim*patchDim;      fea=WT(featureNum,offset+1:offset+patchDim*patchDim);      feature=reshape(fea,patchDim,patchDim);      % ------------------------      % Flip the feature matrix because of the definition of convolution, as explained later      feature = flipud(fliplr(squeeze(feature)));      % Obtain the image      im = squeeze(images(:, :, channel, imageNum));      % Convolve "feature" with "im", adding the result to convolvedImage      % be sure to do a 'valid' convolution      % ---- YOUR CODE HERE ----      convolvedImage=convolvedImage+conv2( im,feature,'valid' );      % ------------------------    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+b_mean(featureNum));    % ------------------------    % The convolved feature is the sum of the convolved values for all channels    convolvedFeatures(featureNum, imageNum, :, :) = convolvedImage;  endendendfunction sigm=sigmoid(x)    sigm=1./(1+exp(-x));end

step3:池化

function pooledFeatures = 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:numFeatures    for imageNum=1:numImages        for row=1:numRegin            for col=1:numRegin                regin=convolvedFeatures(featureNum, imageNum,(row-1)*poolDim+1:row*poolDim,(col-1)*poolDim+1:col*poolDim);                pooledFeatures(featureNum,imageNum,row,col)=mean(regin(:));            end        end    endendend
0 0
原创粉丝点击