部署已经训练好的caffe网络用于图片分类

博主最近在学习caffe,所以会将每一个关键节点都记录下来,方便自己查看,也希望能够帮到有需要的人!

首先,这篇文章是基于你已经能够用自己的数据集训练caffe中现成的深度网络,如letnet、caffenet等。如果这一步你还没有实现,建议查看博主之前的博客,有关于这一块的过程。

在你成功的进行了训练之后,在你指定的路径下,一般是你的solver.prototxt配置文件所在的路径下,caffe会自动生成一个caffemodel,这个caffemodel中记录了你训练完成的网络参数。有了这些参数,才能够将这个训练好的网络用于图片分类。

如上图所示,其中的500,5000代笔你训练的次数,在这里建议如果你的数据集很小的话,训练次数可以少一些,不然很容易出问题,比如你的测试accuracy=1。这样是不现实的,肯定是训练时出了问题,而且很大概率和你的数据集的size和训练次数有关。

另外,deploy.prototxt文件也要注意,这个在caffe的models下点开任意一个网络都能找到,这个是一个简洁版的深度网络,不包含数据集的读入和测试部分,只是纯粹的接收单张图片输入,然后进过卷积、池化等一系列操作直接输出最终结果,也即是一个概率向量。然后去这个向量中最大元素的下标也即是图像所对应的类别。

整体的思路就是,要用到这个deploy.prototxt配置文件,重构网络的前向计算,不用考虑反馈,权值更新之类的问题。因为权值已经训练好了并且保存在caffemodel文件中,我们只需要读出来就行。

博主是在jupyter notebook 环境下实现这一过程的,至于这个notebook怎么用,可以百度也可以参考博主之前的文章,博主本人一直都是用的这个,很是方便和直观。

下面粘代码:


# coding: utf-8

# In[19]:


import numpy as np
import matplotlib.pyplot as plt
#%matplotlib inline

plt.rcParams['figure.figsize']=(10,10)
plt.rcParams['image.interpolation']='nearest'
plt.rcParams['image.cmap']='gray'


import sys
caffe_root='/home/mysj/caffe/'
sys.path.insert(0,caffe_root+'python')
import caffe


import os
if os.path.isfile(caffe_root+'examples/myfile/solver_iter_500.caffemodel'):
    print 'CaffeNet found'
#else:
   # print 'Downloading pre-trained caffeNet model....'
   # !../scripts/download_model_binary.py ../models/bvlc_reference_caffenet

#timeit net.forward()
caffe.set_device(0)
caffe.set_mode_gpu()

model_def=caffe_root+'examples/myfile/deploy.prototxt'
model_weights=caffe_root+'examples/myfile/solver_iter_500.caffemodel'

net=caffe.Net(model_def,               # defines the structure of the model
             model_weights,            # contains the trained weights
             caffe.TEST)               # use test mode (e.g., don't perform dropout)
#print net
mu=np.load(caffe_root+'python/caffe/imagenet/ilsvrc_2012_mean.npy')
mu=mu.mean(1).mean(1)
print 'mean-subtracted values:',zip('BGR',mu)


transformer=caffe.io.Transformer({'data':net.blobs['data'].data.shape})

transformer.set_transpose('data',(2,0,1))#0,1,2 represents three channels
transformer.set_mean('data',mu)
transformer.set_raw_scale('data',255)
transformer.set_channel_swap('data',(2,1,0))#change RGB to BGR-210

net.blobs['data'].reshape(50,
                         3,
                         227,227)
########################################################################
######                 load image for detection            ############
######################################################################
#image=caffe.io.load_image(caffe_root+'examples/images/cat.jpg')
image=caffe.io.load_image(caffe_root+'examples/images/507.jpg')
transformed_image=transformer.preprocess('data',image)#transform image with transformer RGB to BGR-210
plt.imshow(image)


net.blobs['data'].data[...]=transformed_image
output=net.forward()
output_prob=output['prob'][0]# the output probability vector for the first image in the batch
                             ###输出当前batch中的第一副图像的可能类别向量。######################
print 'predicted class is:',output_prob.argmax()
labels_file=caffe_root+'data/re/labels_words.txt'
labels=np.loadtxt(labels_file,str,delimiter='\t')
print 'output label:',labels[output_prob.argmax()]
#net.forward()
#%timeit net.forward()
################################################################################
#print 'predicted class is:',output_prob
#print output_prob
    


    

运行,会输出你所给的图片最有可能的类别。注意其中的labels_words.txt是根据自己的数据集生成的标签集,里面一次存放每一类的标签。如:

class0: dog
class1: cat
class2: fish
.....
.....

至此,基于你自己的数据集训练好的网络就能够用于实际的应用了,虽然并没有什么实际应用。

2333333333

不明白的欢迎随时交流。

猜你喜欢

转载自blog.csdn.net/zshluckydogs/article/details/80004739
今日推荐