caffe分类小例子

来源:互联网 发布:电脑设计软件 编辑:程序博客网 时间:2024/06/08 08:21

Caffe

data、examples、include、matlab、models、python、src、tools

examples:官网提供的基本的模型

include:框架源码头文件

src:caffe框架源码

models:存放训练完的模型以及网络结构文档

tools:用于可执行的文件

分类小例子

用已有的参数文件和训练好的模型对现有的图片进行分类,这个用官网的例子作为测试

import numpy as npimport matplotlib.pyplot as pltcaffe_root= "../"import syssys.path.insert(0,caffe_root+'python')import caffeMODEL_FILE = '../models/bvlc_reference_caffenet/deploy.prototxt'#模型文件PRETRAINED = '../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'# already trained model # 已经训练好的模型IMAGE_FILE = '../examples/images/cat.jpg' # 待测试的图片import osif not os.path.isfile(PRETRAINED):    print("Downloading pre-trained CaffeNet model")caffe.set_mode_cpu()net = caffe.Classifier(MODEL_FILE,PRETRAINED,mean = np.load('caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1),channel_swap=(2,1,0),raw_scale=255,image_dims=(256,256))#limit martix of the input imageinput_image = caffe.io.load_image(IMAGE_FILE)plt.imshow(input_image)plt.show()prediction = net.predict([input_image]) # 进行网络的预测print 'prediction class:',prediction[0].shapeplt.plot(prediction[0]) # 训练后类值,这里就能看出测试图片的预测值plt.show() print 'predicted class',prediction[0].argmax()print net.predict([input_image])input_oversampled = caffe.io.oversample([caffe.io.resize_image(input_image,net.image_dims)],net.crop_dims)caffe_input = np.asarray([net.transformer.preprocess('data',in_) for in_ in input_oversampled])print net.forward(data = caffe_input)

数据结果:

​ 原图

分类结果:

分类数据:

​ 其中每一层的数据都可以分类出来进行分析

原创粉丝点击