[caffe(二)]Python加载训练caffe模型并进行测试1

#coding=utf-8

import caffe
import numpy as np
import struct
import matplotlib.pyplot as plt

#读取mnist数据集
filename = 'train-images.idx3-ubyte'
binfile = open(filename, 'rb')
buf = binfile.read()

#提取第1张图片进行测试
index = 0   #0代表第一张图,784*(n-1)代表第n张图片
magic, numImages, numRows, numColumns = struct.unpack_from('>IIII', buf, index)
index += struct.calcsize('>IIII')
im = struct.unpack_from('>784B', buf, index)
index += struct.calcsize('>784B')

#模型和部署文件的加载
deploy='lenet_deploy.prototxt'    #deploy文件
caffe_model= 'lenet_iter_10000.caffemodel'   #训练好的 caffemodel

#将向量展开为28*28的图片
im = np.array(im)
im = im.reshape(28, 28)

#显示图片
fig = plt.figure()
plotwindow = fig.add_subplot(111)
plt.imshow(im, cmap='gray')
plt.show()

#将图片reshape为神经网络的输入
im = im.reshape(28, 28,1)
im=im.astype(np.float32)   #数据转换
print "The shape of im:", im.shape

gender_net = caffe.Classifier(deploy, caffe_model)
output =gender_net.predict([im],oversample = False)
caffe.set_mode_cpu()
print 'predicted class:',output[0].argmax()

 

转载于:https://www.cnblogs.com/youngsea/p/9549443.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值