【问题标题】:How to adapt Caffe Matlab wrapper for a network trained on Mnist?如何使 Caffe Matlab 包装器适应在 Mnist 上训练的网络?
【发布时间】:2015-07-31 15:02:15
【问题描述】:

我在 http://caffe.berkeleyvision.org/gathered/examples/mnist.html 之后在 mnist 数据库上成功训练了我的 Caffe 网络

现在我想使用 Matlab 包装器用我自己的图像测试网络。

因此,在“matcaffe.m”中,我加载了文件“lenet.prototxt”,该文件不用于训练,但似乎适合测试。它引用 28 x 28 像素的输入大小:

name: "LeNet"
input: "data"
input_dim: 64
input_dim: 1
input_dim: 28
input_dim: 28
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"

因此,我相应地调整了“matcaffe.m”中的“prepare_image”函数。现在看起来像这样:

% ------------------------------------------------------------------------
function images = prepare_image(im)
IMAGE_DIM = 28;
% resize to fixed input size
im = rgb2gray(im);
im = imresize(im, [IMAGE_DIM IMAGE_DIM], 'bilinear');
im = single(im);
images = zeros(1,1,IMAGE_DIM,IMAGE_DIM);
images(1,1,:,:) = im;
images = single(images);
%-------------------------------------------------------------

这会将输入图像转换为 [1 x 1 x 28 x 28]、4dim、灰度图像。但 Matlab 仍然在抱怨:

Error using caffe
MatCaffe input size does not match the input size of the
network
Error in matcaffe_myModel_mnist (line 76)
scores = caffe('forward', input_data);

有人有使用自己的数据测试训练有素的 mnist 网络的经验吗?

【问题讨论】:

    标签: matlab caffe matcaffe


    【解决方案1】:

    最后我找到了完整的解决方案: 这是如何使用 Caffe 的 matcaffe.m(Matlab 包装器)预测您自己的输入图像的数字

    1. 在“matcaffe.m”中:必须引用文件“caffe-master/examples/mnist/lenet.prototxt”
    2. 修改文件“lenet.prototxt”,正如mprat指出的那样:将条目input_dim更改为input_dim: 1
    3. 对 matcaffe.m 中的子函数“prepare_image”使用以下适配:

    (输入可以是任意大小的rgb图片)

    function image = prepare_image(im)
    
    IMAGE_DIM = 28;
    
    % If input image is too big , is rgb and of type uint8:
    % -> resize to fixed input size, single channel, type float
    
    im = rgb2gray(im);
    im = imresize(im, [IMAGE_DIM IMAGE_DIM], 'bilinear');
    im = single(im);
    
    % Caffe needs a 4D input matrix which has single precision
    % Data has to be scaled by 1/256 = 0.00390625 (like during training)
    % In the second last line the image is beeing transposed!
    images = zeros(1,1,IMAGE_DIM,IMAGE_DIM);
    images(1,1,:,:) = 0.00390625*im';
    images = single(images);
    

    【讨论】:

    • 您需要将“图像”更正为“图像”并更改“im = rgb2gray(im)”的选项,因为当前代码仅适用于彩色图像。
    • 它确实在我的电脑上运行,我必须将格式 image(1,1,:, :) 更改为 image(:, :, 1, 1) 才能使其运行。但是,使用真实数据集(我在 Internet 上收集)得到的结果非常差。你遇到过这个问题吗?
    【解决方案2】:

    您遇到该错误(输入大小不匹配)的原因是网络 prototxt 需要一批 64 个图像。线条

    input_dim: 64
    input_dim: 1
    input_dim: 28
    input_dim: 28
    

    意味着网络需要一批 64 灰度、28 x 28 的图像。如果您保持所有 MATLAB 代码相同并将第一行更改为

    input_dim: 1
    

    您的问题应该会消失。

    【讨论】:

    • 我现在那个教程。我已经成功地训练了 mnist 数据库。我的问题是如何结合我自己的图像数据和matlab包装器“matcaffe.m”使用训练好的网络
    • 啊,对不起,我误会了。我会更新我的答案。
    • 但是我输入数据的方式似乎仍然存在错误,因为 mnist 测试数据的准确率约为 10%(这是随机猜测)。我错过了更多的预处理吗?有没有人试过这个? (我尝试将输入数据缩放 256,但也无济于事
    • 这仅适用于 imagenet 示例。然而,在 mnist 的情况下,没有平均减法,图像只有 1 个通道。正如我所说,数据仅在训练期间缩放(1/256)。但是使用这种缩放并没有帮助
    • 知道了。我忘了转置输入图像。现在一切正常。为了更好地概述,我将在单独的答案中编写解决方案。仍然非常感谢您的帮助
    猜你喜欢
    • 2016-09-28
    • 2017-09-07
    • 1970-01-01
    • 2017-02-18
    • 2016-03-31
    • 2016-02-19
    • 2010-11-20
    • 1970-01-01
    • 2016-02-25
    相关资源
    最近更新 更多