[caffe]extract feature

本文介绍了如何使用Caffe从预训练模型中提取特征。最初,作者试图通过C++实现,但遇到问题。后来转向Python,参照教程和代码示例,成功实现了从txt文件批量提取多张图片的特征。提到了data层在deploy.prototxt中的作用,并提供了相关资源链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记得最开始看caffe的时候就在纠结train_val.prototxtdeploy.prototxt到底有啥区别, 今天要用别人的一个模型提特征, 网站上(https://staff.fnwi.uva.nl/p.s.m.mettes/index.html#data)提供的是caffe model 和 deploy.prototxt, 查了一下午到底怎么提…

deploy.prototxt

name: "CaffeNet"
input: "data"
input_shape {
  dim: 10
  dim: 3
  dim: 227
  dim: 227
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  convolution_param {
    num_output: 96
    kernel_size: 11
    stride: 4
  }
}

看,deploy.prototxt里没有data层!那怎么输入图像???

网上说train_val.prototxt是用来训练的,
deploy.prototxt是用来提特征/分类的.

1. c++

caffe官方的提特征是用的train_val.prototxt
http://caffe.berkeleyvision.org/gathered/examples/feature_extraction.html
我照着这个把deploy.prototxt加了一层data层然后出错了..

好像大家都是拿pyhton+deploy.prototxt去提取特征.

2. python

tutorial

http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/01-learning-lenet.ipynb
http://stackoverflow.com/questions/32379878/cheat-sheet-for-caffe-pycaffe
http://christopher5106.github.io/deep/learning/2015/09/04/Deep-learning-tutorial-on-Caffe-Technology.html

code

#Load an image (that comes with Caffe) and perform the preprocessing we've set up.
image = caffe.io.load_image(caffe_root + 'examples/images/cat.jpg')
transformed_image = transformer.preprocess('data', image)
#copy the image data into the memory allocated for the net
net.blobs['data'].data[ ...] = transformed_image 

我修改了下,可以读入一个txt,提取多张图的特征
https://github.com/apsvvfb/caffe_extract_features/tree/master

image = caffe.io.load_image(caffe_root + 'examples/images/cat.jpg')
transformed_image = transformer.preprocess('data', image)
net.blobs['data'].data[0, ...] = transformed_image
image = caffe.io.load_image(caffe_root + 'examples/images/fish-bike.jpg')
transformed_image = transformer.preprocess('data', image)
net.blobs['data'].data[1, ...] = transformed_image

最后读数据的时候

# The first layer output of first image, conv1 (rectified responses of the filters above, first 36 only)
feat = net.blobs['conv1'].data[0, :36]
# The first layer output of second image
feat = net.blobs['conv1'].data[1, :36]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值