caffe學習筆記2:使用pycaffe提取CNN特徵

本文記錄如何基於pycaffe提取pretrained model某一層的特徵。

import numpy as np
import matplotlib.pyplot as plt
import scipy.io

# 設置plt畫圖顯示結果
# 圖片尺寸
plt.rcParams['figure.figsize'] = (12,12)     
plt.rcParams['figure.dpi'] = 150   
# 不插值
plt.rcParams['image.interpolation'] = 'nearest'  
# 熱圖
plt.rcParams['image.cmap'] = 'jet'

# 添加工具包的python路徑
import sys
import os
tool_root = '/mnt/SSAP/Util/'  
tool_pakg = ['caffe', 'libsvm', 'liblinear']

for item in tool_pakg:
    sys.path.append(tool_root + item + '/python')

# pycaffe模塊
import caffe

caffe_root = '/mnt/SSAP/Util/caffe/' # caffe根目錄
model_root = './models/'
opt = {
    "debug": False,
    "caffeMode": "gpu",
    "batchSize": 1,
    "inputSize":  227,
    "net": "alexNetPlaces", 
    "layer": "single", # multi, single 
    "dataset": "MITIndoor67" 
    }
if opt["caffeMode"] == "cpu":
    caffe.set_mode_cpu()
else:
    caffe.set_device(0)  
    caffe.set_mode_gpu()

if opt["net"] == "googleNet":
    model_def = model_root + 'deploy_googlenet.prototxt'
    model_weights = model_root + 'imagenet_googlelet_train_iter_120000.caffemodel'
elif opt["net"] == "alexNetPlaces":
    model_def = model_root + 'alexnet_places/places205CNN_deploy_upgraded.prototxt'
    model_weights = model_root + 'alexnet_places/places205CNN_iter_300000_upgraded.caffemodel'
    layer_names = ["fc7"]
else:
    print "[Error]no model exist."
    exit()
net = caffe.Net(model_def,      # 定義模型結構
                model_weights,  # 預訓練的網絡
                caffe.TEST)     #  測試模式

net.blobs['data'].reshape( opt["batchSize"], # batch size
                          3,                 # BGR
                          opt["inputSize"], opt["inputSize"] ) # image size

transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})

transformer.set_transpose('data', (2,0,1))  # 變換image矩陣,把channel放到最後一維
transformer.set_raw_scale('data', 255)      # 從[0,1]rescale到[0,255]
transformer.set_channel_swap('data', (2,1,0))  # 調整 channels from RGB to BGR  

下面是對一幅圖提取CNN特徵

featAll = [] # 每一行存一幅圖的CNN特徵
image = caffe.io.load_image('test.jpg') # 讀入圖片
transformed_image = transformer.preprocess('data', image)
net.blobs['data'].data[...] = transformed_image
output = net.forward() # 這裏output是CNN最後一層的輸出向量
feature = net.blobs['fc7'].data[0] # 讀取fc7層的特徵
# 打印feature維度看看,這裏是blob
if opt["debug"]:
    print feature.shape
feature_standarlized = (feature - min(feature)) / (max(feature) - min(feature)) # 歸一化
# 把ndarray轉爲list
tmpf = feature_standarlized.reshape(1,feature_standarlized.size)
s = tmpf.tolist()
fe = reduce(lambda x,y: x+y,s)
# 驗證list維度
if opt["debug"]:
    print len(fe)

featAll.append(fe)                  

循環執行上述代碼,可對整個數據集提取fc7層的CNN特徵到featAll中。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章