在http://caffe.berkeleyvision.org/gathered/examples/mnist.html之后,我成功地在mnist数据库上训练了Caffe网
现在我想用Matlab包装器用我自己的图像测试网络。
因此,在"matcaffe.m“中,im加载"lenet.prototxt”文件,该文件不用于培训,但似乎适合测试。它引用的输入大小为28x28像素:
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);
%-------------------------------------------------------------这将输入图像转换为1x1x28x28,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网络的经验吗?
发布于 2015-05-21 14:08:48
最后,我找到了完整的解决方案:这是如何使用matcaffe.m (Matlab包装器)来预测自己输入图像的数字。
input_dim: 1(输入可以是任意大小的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);发布于 2015-05-19 16:47:48
您出现此错误的原因(输入大小不匹配)是因为网络原型需要一批64幅图像。线
input_dim: 64
input_dim: 1
input_dim: 28
input_dim: 28这意味着该网络预计将有一批64张灰度图,28张x28张图片。如果您保持所有MATLAB代码相同,并将第一行更改为
input_dim: 1你的问题应该解决了。
https://stackoverflow.com/questions/30331476
复制相似问题