前面学习了如何训练模型,也学了如何用测试集测试模型。但是好像还不会拿一张自己的图片去模型中进行测试。这篇文章就主要聊聊如何使用训练好的模型对自己的一张图片进行测试!(前面提到了做的项目是藏文识别,本篇文章就以藏文MNIST数据集模型作为举例,请各位重点关注不同的地方)
基本步骤如下:
1.准备测试数据
2.准备deploy文件
3.准备标签文件
4.准备均值文件
5.准备分类器文件
6.使用命令进行测试
1.准备测试数据:
这里的数据可以是网上下载的可以是自己手写的,格式的话.jpg和.png的都是可以滴。
有两点需要注意:
1) 调整图片大小为28*28是训练藏文mnist数据集的指定格式。(此处根据你的实际需要进行调整,与训练集保持统一)
2)在藏文mnist数据集中数字是黑底白字。在进行一张检测之前,还需要提前对其进行二值化操作。把它变成黑底白字的图片。(二值化转换黑底白字后面会有一篇博客给出代码并详细介绍过程)
2.准备deploy文件:
deploy文件可从MNIST训练模型中test_train.prototxt衍生,将训练和测试集的输入输出层删除,增加以下层:
layer {
name: “data”
type: “Input”
top: “data”
input_param { shape: { dim: 1 dim: 3 dim: 28 dim: 28 } }
}
deploy文件全文如下:
name: "LeNet"
layer {
name: "data"
type: "Input"
top: "data"
input_param { shape: { dim: 1 dim: 3 dim: 28 dim: 28 } }
}
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 20
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "conv2"
type: "Convolution"
bottom: "pool1"
top: "conv2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 50
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "ip1"
type: "InnerProduct"
bottom: "pool2"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 500
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "ip1"
top: "ip1"
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "prob"
type: "Softmax"
bottom: "ip2"
top: "prob"
}
注意:shape:里面根据实际情况决定,mnist数据集是1,1,28,28,藏文MNIST是1,3,28,28。这个根据你模型建立的数据集大小可调整.
3.准备标签文件
命名synset_words.txt(其实名字不重要啦!),里面的内容就是总的种类。对于藏文MNIST数据集是0到9,每个数字一行
如下所示:
0
1
2
3
4
5
6
7
8
9
4.准备均值文件:
均值文件caffe有自带的,使用和Lenet相同的,命令如下(在caffe目录下):
sudo build/tools/compute_image_mean examples/TibetanMNIST/TibetanMNIST_train_lmdb examples/TibetanMNIST/mean.binaryproto
5.准备分类器文件:
在藏文数字识别中,使用默认分类器。这里没有命令
6.使用命令进行测试(在caffe目录下执行):
./build/examples/cpp_classification/classification.bin
examples/TibetanMNIST/TibetanMNIST_deploy.prototxt
examples/TibetanMNIST/TibetanMNIST_train_model_iter_10000.caffemodel
examples/TibetanMNIST/mean.binaryproto
examples/TibetanMNIST/synset_words.txt
/home/user/桌面/6_27_177.jpg
第一行是分类器,使用的是caffe自带的分类器
第二行是deploy文件的位置
第三行是已经训练好模型的位置
第四行是均值文件的位置
第五行是标签文件的位置
第六行是待测试图片的位置
注意:为了更好的进行注释,将命令放在不同行中。执行时两个语句间留一个空格即可。
下面来个结果展示,我选择了一张藏文数字6进行测试。因为藏文MNIST数据 集下载的时候就已经变成黑底白字了,我就省了二值化的步骤了。从结果看,从训练集挑选一张作测试不想我们想象那样惊心动魄。6号以100%的预测率碾压。所以以后一张测试的时候,尽量要自己制作。不要向我一样手懒哦!
参考学习博客:https://blog.csdn.net/xunan003/article/details/73126425link
因作者水平有限,如有不足之处,请各位下方评论区指出,谢谢!