hans

hans

【Python】【Caffe】四、classification检测模型《python调用caffe模块》


GitHub 代码地址: https://github.com/HansRen1024/Use-Python-to-call-Caffe-module

前言#

mnist 训练集是单通道的,所以有两个版本。有一些细节不同,但功能相同。思路是将图片丢进网络进行一次前向传播,通过最后 softmax 层得到对应每一类别的概率,取最大概率类。

caffe/python/classify.py 是调用 caffe 模块中 Classifier 类,其实这个类内容和上面方法思路是一样的。两种方法殊途同归。

一、适用于 mnist 代码:#

还是要着重强调一件事:deploy.prototxt 文件,强烈建议直接从 train.prototxt 改,最后一层名字是 “prob”,输入维度是:1, 1,
28, 28

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 30 20:41:03 2017

@author: hans

"""

import caffe
import numpy as np


deploy='doc/deploy_lenet.prototxt' # 需要修改inout_dim: 1, 1, 28, 28
caffe_model='models/lenet_iter_10000.caffemodel'
img='doc/7.jpg'
labels_filename='doc/words.txt'
labels = np.loadtxt(labels_filename, str, delimiter='\t')
mean_file='doc/mnist_mean.npy'

net = caffe.Net(deploy, caffe_model, caffe.TEST)

transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) #data blob 结构(n, k, h, w)
transformer.set_transpose('data', (2, 0, 1)) #改变图片维度顺序,(h, w, k) -> (k, h, w)
transformer.set_mean('data', np.load(mean_file).mean(1).mean(1))
transformer.set_raw_scale('data', 255) #将像素范围改成缩放到[0,1]
# transformer.set_channel_swap('data', (2, 1, 0)) # mnist单通道不需要转换

im = caffe.io.load_image(img) #加载图片
im = caffe.io.resize_image(im,(28,28,1)) # 修改图片尺寸维度

caffe_in = transformer.preprocess('data', im) #将处理好的数据放入caffe_in
out = net.forward(**{'data': caffe_in}) #将数据放入网络中进行一次前向传播
prob = out['prob'].reshape(10,) # 可以看出网络中blob都是以字典形式存储数据的。

# net.blobs['data'].data[...] = transformer.preprocess('data', im)#与上面功能相同
# net.forward()
# prob = net.blobs['prob'].data[0].flatten()

print prob

# print 'the class is:', labels[prob.argmax()], 'accuracy: ', prob[prob.argmax()] #跟下面两句话功能相同

order = prob.argsort()[-1]
print 'the class is:', labels[order], 'accuracy: ', prob[order]

二、使用于三通道图片代码:#

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 30 20:41:03 2017

@author: hans

"""

import caffe
import numpy as np
deploy='.prototxt'
caffe_model='.caffemodel'
img='.jpg'
labels_filename='.txt'
mean_file='.npy'

net = caffe.Net(deploy, caffe_model, caffe.TEST)

transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) #data blob 结构(n, k, w, h)
transformer.set_transpose('data', (2, 0, 1)) #改变图片维度顺序,(w, h, k) -> (k, w, h)
transformer.set_mean('data', np.load(mean_file).mean(1).mean(1))
transformer.set_raw_scale('data', 255)
transformer.set_channel_swap('data', (2, 1, 0)) # RGB -> BGR

im = caffe.io.load_image(img)
# 将处理好的数据放入网络中名为'data'的bolb内,就是放入net预分配的内存中。
net.blobs['data'].data[...] = transformer.preprocess('data', im)

out = net.forward() # 网络结构,模型和数据都已经准备好,无需加参数

labels = np.loadtxt(labels_filename, str, delimiter='\t')
prob = net.blobs['prob'].data[0].flatten()
print prob

# print 'the class is:', labels[prob.argmax()], 'accuracy: ', prob[prob.argmax()] #跟下面两句话功能相同

order = prob.argsort()[-1]
print 'the class is:', labels[order], 'accuracy: ', prob[order]

以上部分内容参考自: http://www.cnblogs.com/denny402/p/5685909.html

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.