本文參考如下:
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)