如何使用MatConvNet使输出回归值在[0,1]范围内?

时间:2016-03-23 01:46:58

标签: matlab image-processing computer-vision deep-learning

我正在尝试使用CNN实现回归,其中我输入图像(RGB,32x32x3),模型可以告诉我一个标准化值,例如0.711,表示图像中对象区域的比率到图像大小。在MatConvNet中,我实现了回归损失函数,如下所示:

function Y = vl_l2normloss(X,c,dzdy)
assert(numel(X) == numel(c));
% n = sizer(X,1) * size(X,2);
n = size(X,4);
if nargin <= 2
  Y = sum((X(:) - c(:)).^2) ./ (2*n);
else
  assert(numel(dzdy) == 1);
  Y = reshape((dzdy / n) * (X(:) - c(:)), size(X));
end

我还修改了Cifar-10网络结构的第二层,如下所示:

lr = [.1 2] ;

% Define network CIFAR10-quick
net.layers = {} ;

% Block 1
net.layers{end+1} = struct('type', 'conv', ...
                           'weights', {{0.01*randn(5,5,3,32, 'single'), zeros(1, 32, 'single')}}, ...
                           'learningRate', lr, ...
                           'stride', 1, ...
                           'pad', 2) ;
net.layers{end+1} = struct('type', 'pool', ...
                           'method', 'max', ...
                           'pool', [3 3], ...
                           'stride', 2, ...
                           'pad', [0 1 0 1]) ;
net.layers{end+1} = struct('type', 'relu') ;

% Block 2
net.layers{end+1} = struct('type', 'conv', ...
                           'weights', {{0.05*randn(5,5,32,32, 'single'), zeros(1,32,'single')}}, ...
                           'learningRate', lr, ...
                           'stride', 1, ...
                           'pad', 2) ;
net.layers{end+1} = struct('type', 'relu') ;
net.layers{end+1} = struct('type', 'pool', ...
                           'method', 'avg', ...
                           'pool', [3 3], ...
                           'stride', 2, ...
                           'pad', [0 1 0 1]) ; % Emulate caffe

% Block 3
net.layers{end+1} = struct('type', 'conv', ...
                           'weights', {{0.05*randn(5,5,32,64, 'single'), zeros(1,64,'single')}}, ...
                           'learningRate', lr, ...
                           'stride', 1, ...
                           'pad', 2) ;
net.layers{end+1} = struct('type', 'relu') ;
net.layers{end+1} = struct('type', 'pool', ...
                           'method', 'avg', ...
                           'pool', [3 3], ...
                           'stride', 2, ...
                           'pad', [0 1 0 1]) ; % Emulate caffe

% Block 4
net.layers{end+1} = struct('type', 'conv', ...
                           'weights', {{0.05*randn(4,4,64,64, 'single'), zeros(1,64,'single')}}, ...
                           'learningRate', lr, ...
                           'stride', 1, ...
                           'pad', 0) ;
net.layers{end+1} = struct('type', 'relu') ;

% Block 5
net.layers{end+1} = struct('type', 'conv', ...
                           'weights', {{0.05*randn(1,1,64,1, 'single'), zeros(1,1,'single')}}, ...
                           'learningRate', .1*lr, ...
                           'stride', 1, ...
                           'pad', 0) ;
% Loss layer
net.layers{end+1} = struct('type', 'l2normloss') ;

但输出回归值(测试时)不在[0,1]范围内。我不知道为什么会这样?我的设置有什么问题吗?谢谢!

1 个答案:

答案 0 :(得分:1)

您可以在最后一层使用sigmoid函数或在训练后放置sigmoid函数。

喜欢net.layers{end}.type='sigmoid';(我不确定这是否写得正确)

或者您可以手动定义。它为您提供0到1之间的数字。