#coding=utf-8
import caffe
import numpy as np
caffe.set_mode_cpu()
model_def = './deploy.prototxt'
model_weights = './bvlc_alexnet.caffemodel'
labels_file = './labels.txt'
with open(labels_file,'r') as f:
lines = f.readlines()
labels = []
for i in lines:
labels.append(i.strip('\n').split(',')[1:])
net = caffe.Net(model_def, model_weights, caffe.TEST)
mu = np.load('./ilsvrc_2012_mean.npy')#3*256*256
mu = mu.mean(1)#3*256
mu = mu.mean(1)#3*1 BGR
transforms = caffe.io.Transformer({'data': net.blobs['data'].data.shape})#设定图片的shape格式(1,3,227,227),大小由deploy 文件指定
transforms.set_transpose('data', (2, 0, 1))#改变维度的顺序,由原始图片(227,227,3)变为(3,227,227)
transforms.set_mean('data', mu)#减去均值
transforms.set_raw_scale('data', 255)#放缩到255
transforms.set_channel_swap('data', (2, 1, 0)) #用于将输入图片通道进行重排,将RGB格式转换为BGR
net.blobs['data'].reshape(1, 3, 227, 227)
image = caffe.io.load_image('../cat.jpg')
image_pre = transforms.preprocess('data', image)
net.blobs['data'].data[...] = image_pre
output = net.forward()
output_pro = output['prob'][0]
print(labels[output_pro.argmax()-1])
注意:caffe 里面的图像格式都是BGR而不是RGB,图像维度是(batch,C,H,W) 不是平时的(H,W,C)
caffe.io.load_image读取的图片都是RGB格式,且像素范围都是0-1之间
所有用到的文件https://download.csdn.net/download/daixiangzi/10703475
注意:transforms.preprocess中,最后才做的减均值操作,以免误导,在RGB和BGR格式不对应的情况下进行了减均值操作