本文参考如下:
Instant Recognition with Caffe
Extracting Features
Caffe Python特征提取
caffe 练习4 —-利用python批量抽取caffe计算得到的特征——by 香蕉麦乐迪
caffe 练习3 用caffe提供的C++函数批量抽取图像特征——by 香蕉麦乐迪
caffe python批量抽取图像特征
caffe python 批量抽取图像特征—续篇
caffe c++ 抽取图片特征
shicai C++ Caffe提取特征
caffe源码修改:抽取任意一张图片的特征
matlab 批量提取CNN特征
关于如何批量提取特征,本文的框架如下:
1. 准备数据及相应准备工作
2. 初始化网络
3.读取图像列表
4.提取图像特征,并保存为特定格式
Python方法一
主要有三个函数:
initialize () 初始化网络的相关
readlist() 读取抽取图像列表
extractFeatre() 抽取图像的特征,保存为指定的格式
其中在transformer那里需要根据自己的需求设定
#encoding:utf-8
#详情请查看http://www.cnblogs.com/louyihang-loves-baiyan/p/5078746.html
import numpy as np
import matplotlib.pyplot as plt
import os
import caffe
import sys
import pickle
import struct
import sys,cv2
caffe_root = '../'
# 运行模型的prototxt
deployPrototxt = '/home/bids/caffe/caffe-master/changmiao/model/deploy.prototxt'
# 相应载入的modelfile
modelFile = '/home/bids/caffe/caffe-master/changmiao/model/bvlc_reference_caffenet.caffemodel'
# meanfile 也可以用自己生成的
meanFile = 'python/caffe/imagenet/ilsvrc_2012_mean.npy'
# 需要提取的图像列表
imageListFile = '/home/bids/caffe/caffe-master/changmiao/data/temp.txt'
imageBasePath = '/home/bids/caffe/caffe-master/changmiao/data/cat'
#gpuID = 4 #根据你自己电脑的GPU情况而定
postfix = '.classify_allCar1716_fc6'# 初始化函数的相关操作
def initilize():print 'initilize ... 'sys.path.insert(0, caffe_root + 'python')caffe.set_mode_gpu()
# caffe.set_device(gpuID)net = caffe.Net(deployPrototxt, modelFile,caffe.TEST)return net
# 提取特征并保存为相应地文件
def extractFeature(imageList, net):# 对输入数据做相应地调整如通道、尺寸等等transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})transformer.set_transpose('data', (2,0,1))transformer.set_mean('data', np.load(caffe_root + meanFile).mean(1).mean(1)) # mean pixeltransformer.set_raw_scale('data', 255) transformer.set_channel_swap('data', (2,1,0)) # set net to batch size of 1 如果图片较多就设置合适的batchsize net.blobs['data'].reshape(1,3,227,227) #这里根据需要设定,如果网络中不一致,需要调整num=0#imageList = os.listdir(imageBasePath)for imagefile in imageList:imagefile_abs = os.path.join(imageBasePath, imagefile)print imagefile_absnet.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image(imagefile_abs))out = net.forward()fea_file = imagefile_abs.replace('.jpg',postfix)num +=1print 'Num ',num,' extract feature ',fea_filewith open(fea_file,'wb') as f:for x in xrange(0, net.blobs['fc6'].data.shape[0]):for y in xrange(0, net.blobs['fc6'].data.shape[1]):f.write(struct.pack('f', net.blobs['fc6'].data[x,y]))# 读取文件列表
def readImageList(imageListFile):imageList = []with open(imageListFile,'r') as fi:while(True):line = fi.readline().strip().split()# every line is a image file nameif not line:breakimageList.append(line[0]) print 'read imageList done image num ', len(imageList)return imageListif __name__ == "__main__":net = initilize()imageList = readImageList(imageListFile) extractFeature(imageList, net)