UFLDL cnnConvolve.m

主要功能为对于图片进行卷积运算,这个并不难。但是慢成狗。500张图片的时间为:

时间已过 81.044622 秒。

代码中卖了一个小关子,就是为什么要filter = rot90(squeeze(filter),2);% 180 degree。这个其实就是一块“面包屑”。

其实matlab有自己conv和conv2函数,从数学上来说,严格规范的conv是要首先flip filter的。调用matlab的库函数能够更加高效,但是就是要首先flip filter成为错误的之后,调用的时候,matlab再flip一遍,就是正确的了。500张图片时间

时间已过 3.091949 秒。

高下立判。

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);% 180 degree

    % 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');

    % Add the bias unit
    % Then, apply the sigmoid function to get the hidden activation

    %%% YOUR CODE HERE %%%
    convolvedImage = convolvedImage + b(filterNum);
    convolvedImage = 1./(1+exp(-convolvedImage));


    convolvedFeatures(:, :, filterNum, imageNum) = convolvedImage;
  end
end


end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pooling(池化)是深度学习中一种用于减少特征图大小的操作。在卷积神经网络(CNN)中,通过卷积层的操作,可以提取出输入图像的局部特征。然而,特征图的大小通常会随着卷积操作的进行而增加。而池化层可以通过缩小特征图的空间维度来降低数据的复杂性。 在池化操作中,通常有两种常用的方式:最大池化(Max Pooling)和平均池化(Average Pooling)。最大池化是在每个区域中选择最大的值作为该区域的表示,而平均池化是计算区域内所有值的平均值作为该区域的表示。这两种方式都能有效地减少特征图的尺寸,并保留一定程度上的空间信息。 池化操作可以带来以下几个好处: 1. 减少参数数量:通过降低特征图的尺寸,从而减少网络中需要学习的参数数量,进而可以减小模型的复杂性和计算量。 2. 缓解 overfitting:特征图的尺寸减小后,提取的特征更加局部且抽象,可以减少模型对细节的过拟合,从而提高模型的泛化能力。 3. 提高计算效率:特征图尺寸的减小,意味着后续层次的计算量也会减少,可以加快训练过程和推理阶段的速度。 4. 提取关键信息:通过池化操作,可以保留图像的重要特征,并且丢弃冗余的信息,从而使得后续层次可以更加关注关键的特征。 总而言之,池化操作是深度学习中非常重要的一个操作,可以通过减小特征图的尺寸提高计算效率、减少参数数量以及提取关键信息,从而改善模型的性能和泛化能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值