caffe提取每一层中的特征,在matlab或python查看
参考博客:
http://blog.csdn.net/abc8730866/article/details/52522843
http://blog.csdn.net/lijiancheng0614/article/details/48180331
编译出extract_features.exe模块
在×64、Release模式下编译生成extract_features.exe
将某一层的特征向量生成lmdb文件
在caffe工程的examples下新建一个文件夹,命名为_temp
将examples\images下的图片写成一个文本文档,命名为file_list.txt,放在_temp文件夹下
将examples/eature_extraction/imagenet_val.prototxt复制到之前新建的_temp文件夹。
打开imagenet_val.prototxt,修改以下file_list.txt的路径,对应准确即可:
在models\bvlc_reference_caffenet目录中,下载bvlc_reference_caffenet.caffemodel文件
在caffe根目录下,新建bat脚本,
Build\x64\Release\extract_features.exe models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel examples/_temp/imagenet_val.prototxt fc7 examples/_temp/features 10 lmdb
pause
可以在examples/_temp/features中生成提取的lmdb文件
将lmdb文件转化为mat文件
feat_helper_pb2.py
# Generated by the protocol buffer compiler. DO NOT EDIT!from google.protobuf import descriptor from google.protobuf import message from google.protobuf import reflection from google.protobuf import descriptor_pb2# @@protoc_insertion_point(imports) DESCRIPTOR = descriptor.FileDescriptor(name='datum.proto',package='feat_extract',serialized_pb='\n\x0b\x64\x61tum.proto\x12\x0c\x66\x65\x61t_extract\"i\n\x05\x44\x61tum\x12\x10\n\x08\x63hannels\x18\x01 \x01(\x05\x12\x0e\n\x06height\x18\x02 \x01(\x05\x12\r\n\x05width\x18\x03 \x01(\x05\x12\x0c\n\x04\x64\x61ta\x18\x04 \x01(\x0c\x12\r\n\x05label\x18\x05 \x01(\x05\x12\x12\n\nfloat_data\x18\x06 \x03(\x02')_DATUM = descriptor.Descriptor(name='Datum',full_name='feat_extract.Datum',filename=None,file=DESCRIPTOR,containing_type=None,fields=[descriptor.FieldDescriptor(name='channels', full_name='feat_extract.Datum.channels', index=0,number=1, type=5, cpp_type=1, label=1,has_default_value=False, default_value=0,message_type=None, enum_type=None, containing_type=None,is_extension=False, extension_scope=None,options=None),descriptor.FieldDescriptor(name='height', full_name='feat_extract.Datum.height', index=1,number=2, type=5, cpp_type=1, label=1,has_default_value=False, default_value=0,message_type=None, enum_type=None, containing_type=None,is_extension=False, extension_scope=None,options=None),descriptor.FieldDescriptor(name='width', full_name='feat_extract.Datum.width', index=2,number=3, type=5, cpp_type=1, label=1,has_default_value=False, default_value=0,message_type=None, enum_type=None, containing_type=None,is_extension=False, extension_scope=None,options=None),descriptor.FieldDescriptor(name='data', full_name='feat_extract.Datum.data', index=3,number=4, type=12, cpp_type=9, label=1,has_default_value=False, default_value="",message_type=None, enum_type=None, containing_type=None,is_extension=False, extension_scope=None,options=None),descriptor.FieldDescriptor(name='label', full_name='feat_extract.Datum.label', index=4,number=5, type=5, cpp_type=1, label=1,has_default_value=False, default_value=0,message_type=None, enum_type=None, containing_type=None,is_extension=False, extension_scope=None,options=None),descriptor.FieldDescriptor(name='float_data', full_name='feat_extract.Datum.float_data', index=5,number=6, type=2, cpp_type=6, label=3,has_default_value=False, default_value=[],message_type=None, enum_type=None, containing_type=None,is_extension=False, extension_scope=None,options=None),],extensions=[],nested_types=[],enum_types=[],options=None,is_extendable=False,extension_ranges=[],serialized_start=29,serialized_end=134, )DESCRIPTOR.message_types_by_name['Datum'] = _DATUMclass Datum(message.Message):__metaclass__ = reflection.GeneratedProtocolMessageTypeDESCRIPTOR = _DATUM# @@protoc_insertion_point(class_scope:feat_extract.Datum)# @@protoc_insertion_point(module_scope)
View Code
lmdb2mat.py
import lmdb import feat_helper_pb2 import numpy as np import scipy.io as sio import timedef main(argv):lmdb_name = sys.argv[1]print "%s" % sys.argv[1]batch_num = int(sys.argv[2]);batch_size = int(sys.argv[3]);window_num = batch_num*batch_size;start = time.time()if 'db' not in locals().keys():db = lmdb.open(lmdb_name)txn= db.begin()cursor = txn.cursor()cursor.iternext()datum = feat_helper_pb2.Datum()keys = []values = []for key, value in enumerate( cursor.iternext_nodup()):keys.append(key)values.append(cursor.value())ft = np.zeros((window_num, int(sys.argv[4])))for im_idx in range(window_num):datum.ParseFromString(values[im_idx])ft[im_idx, :] = datum.float_dataprint 'time 1: %f' %(time.time() - start)sio.savemat(sys.argv[5], {'feats':ft})print 'time 2: %f' %(time.time() - start)print 'done!'if __name__ == '__main__':import sysmain(sys.argv)
View Code
将这个两个文件放在_temp文件夹中(位置可随意放置),cmd打开python,进入到该目录,
python lmdb2mat.py features 1 10 4096 features_fc7.mat
在ubantu下命令的格式的参考,
LMDB=./examples/_temp/features_fc7 # lmdb文件路径 BATCHNUM=1 BATCHSIZE=10# DIM=290400 # feature长度,conv1# DIM=43264 # conv5 DIM=4096 OUT=./examples/_temp/features_fc7.mat #mat文件保存路径 python ./lmdb2mat.py $LMDB $BATCHNUM $BATCHSIZE $DIM $OUT
可生成一个mat文件,
新建matlab函数及脚本,对该mat文件进行可视化
display_network.m
function [h, array] = display_network(A, opt_normalize, opt_graycolor, cols, opt_colmajor) % This function visualizes filters in matrix A. Each column of A is a % filter. We will reshape each column into a square image and visualizes % on each cell of the visualization panel. % All other parameters are optional, usually you do not need to worry % about it. % opt_normalize: whether we need to normalize the filter so that all of % them can have similar contrast. Default value is true. % opt_graycolor: whether we use gray as the heat map. Default is true. % cols: how many columns are there in the display. Default value is the % squareroot of the number of columns in A. % opt_colmajor: you can switch convention to row major for A. In that % case, each row of A is a filter. Default value is false. warning off allif ~exist('opt_normalize', 'var') || isempty(opt_normalize)opt_normalize= true; endif ~exist('opt_graycolor', 'var') || isempty(opt_graycolor)opt_graycolor= true; endif ~exist('opt_colmajor', 'var') || isempty(opt_colmajor)opt_colmajor = false; end% rescale A = A - mean(A(:));if opt_graycolor, colormap(gray); end% compute rows, cols [L M]=size(A); sz=sqrt(L); buf=1; if ~exist('cols', 'var')if floor(sqrt(M))^2 ~= Mn=ceil(sqrt(M));while mod(M, n)~=0 && n<1.2*sqrt(M), n=n+1; endm=ceil(M/n);elsen=sqrt(M);m=n;end elsen = cols;m = ceil(M/n); endarray=-ones(buf+m*(sz+buf),buf+n*(sz+buf));if ~opt_graycolorarray = 0.1.* array; endif ~opt_colmajork=1;for i=1:mfor j=1:nif k>M, continue; endclim=max(abs(A(:,k)));if opt_normalizearray(buf+(i-1)*(sz+buf)+(1:sz),buf+(j-1)*(sz+buf)+(1:sz))=reshape(A(:,k),sz,sz)'/clim;elsearray(buf+(i-1)*(sz+buf)+(1:sz),buf+(j-1)*(sz+buf)+(1:sz))=reshape(A(:,k),sz,sz)'/max(abs(A(:))); endk=k+1;endend elsek=1;for j=1:nfor i=1:mif k>M, continue; endclim=max(abs(A(:,k)));if opt_normalizearray(buf+(i-1)*(sz+buf)+(1:sz),buf+(j-1)*(sz+buf)+(1:sz))=reshape(A(:,k),sz,sz)'/clim;elsearray(buf+(i-1)*(sz+buf)+(1:sz),buf+(j-1)*(sz+buf)+(1:sz))=reshape(A(:,k),sz,sz)'; endk=k+1;endend endif opt_graycolorh=imagesc(array); elseh=imagesc(array,'EraseMode','none',[-1 1]); end axis image offdrawnow;warning on all
View Code
在该目录下,新建脚本,调用该函数
nsample = 2; % num_output = 96; % conv1 % num_output = 256; % conv5 num_output = 4096; % fc7load features_fc7.mat width = size(feats, 2); nmap = width / num_output;for i = 1 : nsamplefeat = feats(i, :);feat = reshape(feat, [nmap num_output]);figure('name', sprintf('image #%d', i));display_network(feat); end
View Code
在python中读取该mat文件
import scipy.io matfile = 'features_fc7.mat' data = scipy.io.loadmat(matfile)
转载于:https://www.cnblogs.com/k7k8k91/p/8560362.html
caffe提取每一层中的特征,在matlab或python查看相关推荐
- 批量跨帧提取人脸视频中的脸部区域帧图像python
代码实现功能 1.所有人脸视频在一个文件夹中. 2.跨帧提取帧图像. 3.截取帧图像的脸部区域. 3.将截取到的所有脸部图像保存到新文件夹中. 代码语言:python3.6 #这个代码实现了批量跨帧提 ...
- caffe 提取特征并可视化(已测试可执行)及在线可视化
网络结构在线可视化工具 http://ethereon.github.io/netscope/#/editor 参考主页: caffe 可视化的资料可在百度云盘下载 链接: http://pan.ba ...
- 基于多阈值的形态提取遥感图像中的沿海线的特征方法(Qu Jishuang)
论文阅读笔记 Title:A multi-threshold based morphological approach for extracting coastal line feature in r ...
- 神经网络一键可视化!这个AI神器可以放大网络中的任何一层,显示特征图!...
点击下方卡片,关注"CVer"公众号 AI/CV重磅干货,第一时间送达 行早 发自 凹非寺 来源:量子位(QbitAI) 神经网络在工作的时候,里面到底是什么样? 为了能透视这个& ...
- 目标检测 | 盘点目标检测中的特征融合技巧(根据YOLO v4总结)
点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 特征融合分类 在深度学习的很多工作中(例如目标检测.图像分割),融 ...
- 目标检测中的特征冲突与不对齐问题
点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 本文转自|深度学习这件小事 前言 昨天看到一篇商汤的刷榜文< ...
- 浅谈微视推荐系统中的特征工程
本文作者:hannahguo,腾讯 PCG 推荐算法工程师 在推荐系统中,特征工程扮演着重要的角色.俗话说数据和特征决定了机器学习算法的上限,而模型.算法的选择和优化只是在不断逼近这个上限.特征工程的 ...
- 深度CTR预估模型中的特征自动组合机制演化简史
文 | 杨旭东 源 | 知乎 众所周知,深度学习在计算机视觉.语音识别.自然语言处理等领域最先取得突破并成为主流方法.但是,深度学习为什么是在这些领域而不是其他领域最先成功呢?我想一个原因就是图像.语 ...
- 浅谈目标检测中的特征冲突与不对齐问题
点击上方"AI算法修炼营",选择加星标或"置顶" 标题以下,全是干货 作者:黄飘 知乎链接:https://zhuanlan.zhihu.com/p/11470 ...
最新文章
- vmstart的用法
- 谷歌称居家办公影响工作效率!2021 年将回归线下办公
- 三面百度AI岗,炸了
- Python 数据库连接
- mysql linux32_Linux 配置 mysql 5.7.32 实操记录
- AI 闯入法律界,第一步是当律师的得力助手
- java复制和上传_java文件上传复制等功能
- php 关键词回复 图片,php微信开发之关键词回复功能
- linux系统中各颜色的代表
- 这几天心里颇不宁静,采的不是信号,而是寂寞
- ROS2编程基础课程--库
- ArcGIS 切片/瓦片的发布与加载
- 品牌公关与传统媒体有什么关联和作用
- c++ 经典服务器开源项目 Tinywebserver的使用与配置(百度智能云服务器安装ubuntu18.04可用公网ip访问)
- 磁盘阵列RAID技术超详细解读
- 2020总结:软件工程,由分析需求到立项到测试再到?
- 网技·实验·3.29
- [源码阅读]VDO-SLAM笔记[1] Track()中动态obj部分
- 腾讯手游助手弹窗提示检测到兼容的Hyper-v正在运行,请关闭后重试
- 加油吧,707!——立体几何篇