标签:deep learning ufldl 机器学习 matlab 图像识别
ufldl学习笔记与编程作业:Feature Extraction Using Convolution,Pooling(卷积和池化抽取特征)
ufldl出了新教程,感觉比之前的好,从基础讲起,系统清晰,又有编程实践。
在deep learning高质量群里面听一些前辈说,不必深究其他机器学习的算法,可以直接来学dl。
于是最近就开始搞这个了,教程加上matlab编程,就是完美啊。
新教程的地址是:http://ufldl.stanford.edu/tutorial/
function convolvedFeatures = cnnConvolve(filterDim, numFilters, images, W, b) %cnnConvolve Returns the convolution of the features given by W and b with %the given images % % Parameters: % filterDim - filter (feature) dimension % numFilters - number of feature maps % images - large images to convolve with, matrix in the form % images(r, c, image number) % -------------注意维度的位置 % W, b - W, b for features from the sparse autoencoder % W is of shape (filterDim,filterDim,numFilters) % b is of shape (numFilters,1) % % Returns: % convolvedFeatures - matrix of convolved features in the form % convolvedFeatures(imageRow, imageCol, featureNum, imageNum) % ----------注意维度的位置 numImages = size(images, 3); imageDim = size(images, 1); %行数,即是高度。 这里没算宽度,貌似默认高宽相等。 convDim = imageDim - filterDim + 1; % 卷积后,特征的高度 convolvedFeatures = zeros(convDim, convDim, numFilters, numImages); % Instructions: % Convolve every filter with every image here to produce the % (imageDim - filterDim + 1) x (imageDim - filterDim + 1) x numFeatures x numImages % matrix convolvedFeatures, such that % convolvedFeatures(imageRow, imageCol, featureNum, imageNum) is the % value of the convolved featureNum feature for the imageNum image over % the region (imageRow, imageCol) to (imageRow + filterDim - 1, imageCol + filterDim - 1) % % Expected running times: % Convolving with 100 images should take less than 30 seconds % Convolving with 5000 images should take around 2 minutes % (So to save time when testing, you should convolve with less images, as % described earlier) for imageNum = 1:numImages for filterNum = 1:numFilters % convolution of image with feature matrix convolvedImage = zeros(convDim, convDim); % Obtain the feature (filterDim x filterDim) needed during the convolution %%% YOUR CODE HERE %%% filter = W(:,:,filterNum); % Flip the feature matrix because of the definition of convolution, as explained later filter = rot90(squeeze(filter),2); %squeeze是把只有一个维度的那一维给去掉。这里就是把第三维给去掉,三维变二维。 % Obtain the image im = squeeze(images(:, :, imageNum)); % Convolve "filter" with "im", adding the result to convolvedImage % be sure to do a 'valid' convolution %%% YOUR CODE HERE %%% convolvedImage =conv2(im, filter,"valid");%加上valid参数,下面代码不要了。 %conv2Dim = size(convolvedImage,1); %im_start = (conv2Dim - convDim+2)/2; %im_end = im_start+convDim-1; %convolvedImage = convolvedImage(im_start:im_end,im_start:im_end);%取中间部分 % Add the bias unit % Then, apply the sigmoid function to get the hidden activation %%% YOUR CODE HERE %%% convolvedImage = convolvedImage.+b(filterNum); convolvedImage = sigmoid(convolvedImage); convolvedImage = reshape(convolvedImage,convDim, convDim, 1, 1);%2维变维4维 convolvedFeatures(:, :, filterNum, imageNum) = convolvedImage; end end end
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(imageRow, imageCol, featureNum, imageNum) % % Returns: % pooledFeatures - matrix of pooled features in the form % pooledFeatures(poolRow, poolCol, featureNum, imageNum) % numImages = size(convolvedFeatures, 4); numFilters = size(convolvedFeatures, 3); convolvedDim = size(convolvedFeatures, 1); pooledFeatures = zeros(convolvedDim / poolDim, ... convolvedDim / poolDim, numFilters, numImages); % Instructions: % Now pool the convolved features in regions of poolDim x poolDim, % to obtain the % (convolvedDim/poolDim) x (convolvedDim/poolDim) x numFeatures x numImages % matrix pooledFeatures, such that % pooledFeatures(poolRow, poolCol, featureNum, imageNum) is the % value of the featureNum feature for the imageNum image pooled over the % corresponding (poolRow, poolCol) pooling region. % % Use mean pooling here. %%% YOUR CODE HERE %%% filter = ones(poolDim); for imageNum=1:numImages for filterNum=1:numFilters im = squeeze(squeeze(convolvedFeatures(:, :,filterNum,imageNum)));%貌似squeeze不要也可以 pooledImage =conv2(im, filter,"valid"); pooledImage = pooledImage(1:poolDim:end,1:poolDim:end);%取中间部分 pooledImage = pooledImage./(poolDim*poolDim); %pooledImage = sigmoid(pooledImage); %不需要sigmoid pooledImage = reshape(pooledImage,convolvedDim / poolDim, convolvedDim / poolDim, 1, 1);%2维变维4维 pooledFeatures(:, :, filterNum, imageNum) = pooledImage; end end end
ufldl学习笔记与编程作业:Feature Extraction Using Convolution,Pooling(卷积和池化抽取特征),布布扣,bubuko.com
ufldl学习笔记与编程作业:Feature Extraction Using Convolution,Pooling(卷积和池化抽取特征)
标签:deep learning ufldl 机器学习 matlab 图像识别
原文地址:http://blog.csdn.net/lingerlanlan/article/details/38502627