caffe data层_caffe训练CNN的各层介绍(转)

创建caffe模型,首先要在protocol buffer 定义文件(prototxt)中定义结构。

在caffe环境中,图像的明显特征是其空间结构。

主要layers

主要功能

主要类型

其他

卷积层

提取特征

CONVOLUTION

学习率、数据维度

池化层

特征池化

POOLING

池化方法,数据维度

局部响应归一化层

临近抑制

LRN

损失计算层

loss计算

SOFTMAX_LOSS

EUCLIDEAN_LOSS

HINGE_LOSS

ACCURACY正确率

选择合适的loss

范数可选

激励层

非线性函数

ReLU

SIGMOID

TANH

ABSVAL

POWER

BNLL

ReLU收敛更快

数据层

数据源

Level-DB

LMDB

HDF5_DATA

HDF5_OUTPUT

IMAGE_DATA

Level-DB和LMDB更加高效

一般层

INNER_PRODUCT全连接层

SPLIT

FLATTEN类似shape方法

CONCAT

ARGMAX

MVN

一、卷积层

Convolution:

Documents:注意维度变化与参数选择

1 Parameters (ConvolutionParameter convolution_param)2

3 Required4 num_output (c_o): 输出数(filter数)5 kernel_size (or kernel_h andkernel_w): 指定卷积核6

7 Strongly Recommended8 weight_filler [default type: 'constant'value: 0]9

10 Optional11 bias_term [default true]: 指定是否提供偏置1012 pad (or pad_h andpad_w) [default 0]: 指定输入图片的两侧像素填充量13 stride (or stride_h and stride_w) [default 1]: 过滤器步长14 group (g) [default 1]: 如果 g > 1, 我们限制每一个filter之间的连通性 对于输入的子集. 指定输入和输出被分为 g 组,第i输出组只会和第i输入组相连接.15

16 Input17

18 n * c_i * h_i *w_i19

20 Output21

22 n * c_o * h_o * w_o, where h_o = (h_i + 2 * pad_h - kernel_h) / stride_h + 1 and w_o likewise.

example:

1 layers {2 name: "conv1"

3 type: CONVOLUTION4 bottom: "data"

5 top: "conv1"

6 blobs_lr: 1 #learning rate multiplier for the filters

7 blobs_lr: 2 #learning rate multiplier for the biases

8 weight_decay: 1 #weight decay multiplier for the filters

9 weight_decay: 0 #weight decay multiplier for the biases

10 convolution_param {11 num_output: 96 #learn 96 filters

12 kernel_size: 11 #each filter is 11x11

13 stride: 4 #step 4 pixels between each filter application

14 weight_filler {15 type: "gaussian" #initialize the filters from a Gaussian

16 std: 0.01 #distribution with stdev 0.01 (default mean: 0)

17 }18 bias_filler {19 type: "constant" #initialize the biases to zero (0)

20 value: 021 }22 }23 }

二、池化层 Pooling:

参考链接

deeplearning.stanford.edu/wiki/index.php/池化

池化: 概述

在通过卷积获得了特征 (features)

之后,下一步我们希望利用这些特征去做分类。理论上讲,人们可以用所有提取得到的特征去训练分类器,例如 softmax

分类器,但这样做面临计算量的挑战。例如:对于一个 96X96

像素的图像,假设我们已经学习得到了400个定义在8X8输入上的特征,每一个特征和图像卷积都会得到一个 (96 − 8 + 1) *

(96 − 8 + 1) = 7921 维的卷积特征,由于有 400 个特征,所以每个样例 (example) 都会得到一个 892

* 400 = 3,168,400 维的卷积特征向量。学习一个拥有超过 3 百万特征输入的分类器十分不便,并且容易出现过拟合

(over-fitting)。

为了解决这个问题,首先回忆一下,我们之所以决定使用卷积后的特征是因为图像具有一种“静态性”的属性,这也就意味着在一个图像区域有用的特征极有可能在

另一个区域同样适用。因此,为了描述大的图像,一个很自然的想法就是对不同位置的特征进行聚合统计,例如,人们可以计算图像一个区域上的某个特定特征的平

均值 (或最大值)。这些概要统计特征不仅具有低得多的维度

(相比使用所有提取得到的特征),同时还会改善结果(不容易过拟合)。这种聚合的操作就叫做池化

(pooling),有时也称为平均池化或者最大池化 (取决于计算池化的方法)。

参数解释:

1 Required2 kernel_size (or kernel_h andkernel_w):池化核3 Optional4 pool [default MAX]:指定池化方法. MAX, AVE, orSTOCHASTIC(按照概率值大小随机选择,数值大的被选中的概率大)5 pad (or pad_h andpad_w) [default 0]: 指定输入图片的两侧像素填充量6 stride (or stride_h and stride_w) [default 1]:过滤器步长7 Input8 n * c * h_i *w_i9 Output10 n * c * h_o * w_o,where h_o = (h_i + 2 * pad_h - kernel_h) / stride_h + 1 and w_o likewise..

示例:

1 layers {2 name: "pool1"

3 type: POOLING4 bottom: "conv1"

5 top: "pool1"

6 pooling_param {7 pool: MAX8 kernel_size: 3 #3*3 区域池化

9 stride: 2 #(in the bottom blob) between pooling regions

10 }11 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
clear all; % TODO: Edit this to point to the folder your caffe mex file is in. % path_to_matcaffe = '/data/jkrause/cs231b/caffe-rc2/matlab/caffe'; path_to_matcaffe = 'C:/Users/DELL/Downloads/caffe-master/windows'; addpath(path_to_matcaffe) % Load up the image im = imread('peppers.png'); % Get some random image regions (format of each row is [x1 y1 x2 y2]) % Note: If you want to change the number of regions you extract features from, % then you need to change the first input_dim in cnn_deploy.prototxt. regions = [ 1 1 100 100; 100 50 400 250; 1 1 512 284; 200 200 230 220 100 100 300 200]; % Convert image from RGB to BGR and single, which caffe requires. im = single(im(:,:,[3 2 1])); % Get the image mean and crop it to the center mean_data = load('ilsvrc_2012_mean.mat'); image_mean = mean_data.image_mean; cnn_input_size = 227; % Input size to the cnn we trained. off = floor((size(image_mean,1) - cnn_input_size)/2)+1; image_mean = image_mean(off:off+cnn_input_size-1, off:off+cnn_input_size-1, :); % Extract each region ims = zeros(cnn_input_size, cnn_input_size, 3, size(regions, 1), 'single'); for i = 1:size(regions, 1) r = regions(i,:); reg = im(r(2):r(4), r(1):r(3), :); % Resize to input CNN size and subtract mean reg = imresize(reg, [cnn_input_size, cnn_input_size], 'bilinear', 'antialiasing', false); reg = reg - image_mean; % Swap dims 1 and 2 to work with caffe ims(:,:,:,i) = permute(reg, [2 1 3]); end % Initialize caffe with our network. % -cnn_deploy.prototxt gives the structure of the network we're using for % extracting features and is how we specify we want fc6 features. % -cnn512.caffemodel is the binary network containing all the learned weights. % -'test' indicates that we're only going to be extracting features and not % training anything init_key = caffe('init', 'cnn_deploy.prototxt', 'cnn512.caffemodel', 'test'); caffe('set_device', 0); % Specify which gpu we want to use. In this case, let's use the first gpu. caffe('set_mode_gpu'); %caffe('set_mode_cpu'); % Use if you want to use a cpu for whatever reason % Run the CNN f = caffe('forward', {ims}); % Convert the features to (num. dims) x (num. regions) feat = single(reshape(f{1}(:), [], size(ims, 4)));
05-22

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值