Caffe学习(二):Windows训练与测试Caffe mnist

参考以下两篇文章来做训练(任选一):

1. windows下caffe训练mnist数据集https://blog.csdn.net/panhong1992/article/details/84593439

2. Windows caffe (一) MNIST手写体跑起来https://blog.csdn.net/renyhui/article/details/60871888

第1篇文章中只写了训练mnist,可参考第2篇文章多了测试的步骤,即:

模型训练好后,在examples\mnist\mnist_data\ 目录下多了caffemodel模型文件,因此在caffe-master目录下创建test_mnist.bat文件,并运行即可

.\Build\x64\Release\caffe.exe test -model=examples\mnist\lenet_train_test.prototxt -weights=examples\mnist\mnist_data\lenet_iter_10000.caffemodel -iterations=100
pause

注意这里使用到的是lenet_train_test.prototxt文件,因此这里的测试是对训练样本中的验证集数据进行测试。

运行结果如下:

测试单张图片

创建一张28x28, 256位黑底白字的图片,如下,命名为num.bmp, 放到examples/images/路径下

在examples目录下创建python文件test_mnist_num.py,代码如下:

注意这里的网络文件为lenet.prototxt,为真正的测试prototxt文件,即deploy部署文件

import os.path as osp
import caffe

this_dir = osp.dirname(__file__)
model_file = osp.join(this_dir, 'mnist/lenet.prototxt')
pretrained = osp.join(this_dir, 'mnist/mnist_data/lenet_iter_10000.caffemodel')
image_file = osp.join(this_dir, 'images/num.bmp')

input_image = caffe.io.load_image(image_file, color=False)
net = caffe.Classifier(model_file, pretrained)
prediction = net.predict([input_image], oversample=False)
caffe.set_mode_gpu()

print 'predicted_class:', prediction[0].argmax()

运行后,结果如下:

发布了40 篇原创文章 · 获赞 51 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/zh8706/article/details/94722788
今日推荐