caffe python批量抽取图像特征
2015-07-27 21:41
741 查看
caffe在[1]讲到如何看一个图片的特征和分类结果,但是如何批量抽取特征呢?可以使用c++的版本,这里我们谈下如何用python批量抽取特征。
首先,我们要注意caffe filter_visualization.ipynb的程序中deploy.prototxt中网络每一轮的图片batch是10, 这个数刚好和oversample=true的crop数量是一样的,也就是net一轮forward 刚好是一张图片的10个crop。
第一种,oversample = true的情况, 也就是每张图片会产生10张crop的图片: center, 4 corner, 和mirror
假如我们要抽取两张图片, 每张图片有10个crop
首先是修改deploy.prototxt: input_dim : 20
然后:将imagelist 放入predict参数。
[python] view
plaincopy
scores = net.predict([caffe.io.load_image(caffe_root + "building.jpg"), caffe.io.load_image(caffe_root + "thumb.jpg")])
最后用
[python] view
plaincopy
net.blobs['fc7'].data[4]
net.blobs['fc7'].data[14]
[python] view
plaincopy
import numpy as np
import scipy
caffe_root = '/home/hduser/Project/caffe/'
import sys
sys.path.insert(0,caffe_root + 'python/')
import caffe
net = caffe.Classifier(caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt',
caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel',image_dims=(256, 256))
net.set_phase_test()
net.set_mode_cpu()
net.set_mean('data', np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy'))
net.set_raw_scale('data', 255)
net.set_channel_swap('data', (2,1, 0))
#in fact, you can input a list of images.
scores = net.predict([caffe.io.load_image(caffe_root + "building.jpg"), caffe.io.load_image(caffe_root + "thumb.jpg")])
output = open("feature.txt", "w")
#the fc6 is the fc6 layer feature, data[4] means the five crop images, because each image will be crop to 10 sub-images.
feat = net.blobs['fc6'].data[4]
feat2 = net.blobs['fc6'].data[14]
这样不好的地方是需要修改deploy.prototxt, 另一种方法[2]:modify predict() in python/caffe/classifier.py to store them before the blobs in net are overwritten by the features of a subsequent batch. 该种方法我还没尝试,改天试下。
[python] view
plaincopy
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">第二种情况, 如果图片不需要crop成10张子图片的话,可以用oversample=False,如果设置image_dims=(256, 256), 由于bvlc_reference_caffenet trained model 是227*227的图片大小,所以python/caffe/classifier.py的代码会take center crop 227*227 from 256*256.</span>
input_dim : 2
[python] view
plaincopy
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">第一张图片:net.blobs['fc7'].data[0],</span>
[python] view
plaincopy
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">第二张图片:net.blobs['fc7'].data[1]</span>
后记:还需要熟悉下caffe的python接口函数,可惜貌似看不到这方面的api,只有自己先琢磨琢磨。
参考文章:
1. http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/filter_visualization.ipynb
2. https://groups.google.com/forum/#!searchin/caffe-users/python$20batch$20feature$20extraction/caffe-users/wIgLYMF54AI/iuDf3fZ0_K0J
首先,我们要注意caffe filter_visualization.ipynb的程序中deploy.prototxt中网络每一轮的图片batch是10, 这个数刚好和oversample=true的crop数量是一样的,也就是net一轮forward 刚好是一张图片的10个crop。
第一种,oversample = true的情况, 也就是每张图片会产生10张crop的图片: center, 4 corner, 和mirror
假如我们要抽取两张图片, 每张图片有10个crop
首先是修改deploy.prototxt: input_dim : 20
然后:将imagelist 放入predict参数。
[python] view
plaincopy
scores = net.predict([caffe.io.load_image(caffe_root + "building.jpg"), caffe.io.load_image(caffe_root + "thumb.jpg")])
最后用
[python] view
plaincopy
net.blobs['fc7'].data[4]
net.blobs['fc7'].data[14]
[python] view
plaincopy
import numpy as np
import scipy
caffe_root = '/home/hduser/Project/caffe/'
import sys
sys.path.insert(0,caffe_root + 'python/')
import caffe
net = caffe.Classifier(caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt',
caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel',image_dims=(256, 256))
net.set_phase_test()
net.set_mode_cpu()
net.set_mean('data', np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy'))
net.set_raw_scale('data', 255)
net.set_channel_swap('data', (2,1, 0))
#in fact, you can input a list of images.
scores = net.predict([caffe.io.load_image(caffe_root + "building.jpg"), caffe.io.load_image(caffe_root + "thumb.jpg")])
output = open("feature.txt", "w")
#the fc6 is the fc6 layer feature, data[4] means the five crop images, because each image will be crop to 10 sub-images.
feat = net.blobs['fc6'].data[4]
feat2 = net.blobs['fc6'].data[14]
这样不好的地方是需要修改deploy.prototxt, 另一种方法[2]:modify predict() in python/caffe/classifier.py to store them before the blobs in net are overwritten by the features of a subsequent batch. 该种方法我还没尝试,改天试下。
[python] view
plaincopy
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">第二种情况, 如果图片不需要crop成10张子图片的话,可以用oversample=False,如果设置image_dims=(256, 256), 由于bvlc_reference_caffenet trained model 是227*227的图片大小,所以python/caffe/classifier.py的代码会take center crop 227*227 from 256*256.</span>
input_dim : 2
[python] view
plaincopy
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">第一张图片:net.blobs['fc7'].data[0],</span>
[python] view
plaincopy
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">第二张图片:net.blobs['fc7'].data[1]</span>
后记:还需要熟悉下caffe的python接口函数,可惜貌似看不到这方面的api,只有自己先琢磨琢磨。
参考文章:
1. http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/filter_visualization.ipynb
2. https://groups.google.com/forum/#!searchin/caffe-users/python$20batch$20feature$20extraction/caffe-users/wIgLYMF54AI/iuDf3fZ0_K0J
相关文章推荐
- 助手系列之python的FTP服务器
- python变量、运算符、表达式、数据类型
- Python脚本调试方法
- python 用pyftplib快速建设ftp
- Vim的Python编辑器详细配置过程 (Based on Ubuntu 12.04 LTS)
- python中如何表示多维数组(即矩阵形式)
- Python 从 mat 文件读写 到 h5py 文件包
- numpy函数库中一些常用函数的记录
- python学习笔记(一)
- 如何调用另一个python文件中的代码
- [python]小技巧集锦
- Python程序的执行原理
- python中出现IndentationError:unindent does not match any outer indentation level错误
- Django+python实现网页数据的excel导出
- python学习 - yield
- Alternating Least Squares(ASL) for Implicit Feedback Datasets的数学推导以及用Python实现
- ubuntu python 安装
- python tab补全
- python数组过滤实现方法
- Python实现删除当前目录下除当前脚本以外的文件和文件夹实例