日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

批量提取 caffe 特征 (python, C++, Matlab)(待续)

發布時間:2023/12/13 python 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 批量提取 caffe 特征 (python, C++, Matlab)(待续) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文參考如下

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)

總結

以上是生活随笔為你收集整理的批量提取 caffe 特征 (python, C++, Matlab)(待续)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。