1、目的
在上述训练模型的基础上,测试自己手写的图片,完成手写图片的生成、图片转caffe输入格式文件生成以及模型测试。
2、手写图片、caffe输入格式转换
windows画图软件,属性设置图片像素28*28图片黑底白字,
用matlab将图片转化为mnist二进制格式文件,
clc;
clear;
for i=1:10
imgOrigin=imread([num2str(i,'%d'),'.png']);
imgBinaryImg(i,:,:)= imgOrigin(:,:,1).';
end
imgMnist = fopen('test_img_ubyte', 'wb');
magic = 2051;
numImages = i;
numRows = 28;
numCols = 28;
fwrite(imgMnist, magic, 'int32', 0, 'ieee-be');
fwrite(imgMnist, numImages, 'int32', 0, 'ieee-be');
fwrite(imgMnist, numRows, 'int32', 0, 'ieee-be');
fwrite(imgMnist, numCols, 'int32', 0, 'ieee-be');
for i=1:10
imgBinaryImg1(numCols* numRows*(i-1)+1:numCols* numRows*i)= reshape(imgBinaryImg(i,:,:), 1, numCols* numRows);
end
fwrite(imgMnist, imgBinaryImg1, 'unsigned char');
fclose(imgMnist);
flabel = fopen('test_lable_ubyte', 'wb');
magic = 2049;
numLabels = i;
labels = [5 6 1 4 7 0 9 1 3 5];
fwrite(flabel, magic, 'int32', 0, 'ieee-be');
fwrite(flabel, numLabels, 'int32', 0, 'ieee-be');
fwrite(flabel, labels, 'unsigned char');
fclose(flabel);
运行后产生文件将其放于\caffe\data\mnist文件夹。
git下运行/mnist/create_mnist_byourownselves.sh ,产生caffe读入文件
#!/usr/bin/env sh
# This script converts the mnist data into lmdb/leveldb format,
# depending on the value assigned to $BACKEND.
set -e
EXAMPLE=examples/mnist
DATA=data/mnist
BUILD=Build/x64/Release
BACKEND="lmdb"
echo "Creating ${BACKEND}..."
rm -rf $EXAMPLE/mnist_train_${BACKEND}
rm -rf $EXAMPLE/mnist_test_${BACKEND}
$BUILD/convert_mnist_data.exe $DATA/train-images-idx3-ubyte \
$DATA/train-labels-idx1-ubyte $EXAMPLE/mnist_train_${BACKEND} --backend=${BACKEND}
$BUILD/convert_mnist_data.exe $DATA/test_img_ubyte \
$DATA/test_lable_ubyte $EXAMPLE/mnist_test_${BACKEND} --backend=${BACKEND}
echo "Done."
产生测试图片为10张,修改lenet_train_test.prototxtbatch_size为10,
git bash下运行,
./Build/x64/Release/caffe.exe test -model examples/mnist/lenet_train_test_byourself.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -iterations 1
测试结果为:所有代码以及资料地址:
https://github.com/Alix1992/Caffe
参考博客:
https://blog.csdn.net/woyaopojie1990/article/details/50820526`