Caffe
data、examples、include、matlab、models、python、src、tools
examples:官网提供的基本的模型
include:框架源码头文件
src:caffe框架源码
models:存放训练完的模型以及网络结构文档
tools:用于可执行的文件
分类小例子
用已有的参数文件和训练好的模型对现有的图片进行分类,这个用官网的例子作为测试
import numpy as np
import matplotlib.pyplot as plt
caffe_root= "../"
import sys
sys.path.insert(0,caffe_root+'python')
import caffe
MODEL_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 os
if 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 image
input_image = caffe.io.load_image(IMAGE_FILE)
plt.imshow(input_image)
plt.show()
prediction = net.predict([input_image]) # 进行网络的预测
print 'prediction class:',prediction[0].shape
plt.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)
数据结果:
原图
分类结果:
分类数据:
其中每一层的数据都可以分类出来进行分析