神经网络:caffe特征可视化的代码例子
caffe特征可视化的代码例子
不少读者看了我前面两篇文章
deep learning实践经验总结2--准确率再次提升,到达0.8。再来总结一下
之后。想知道我是怎么实现特征可视化的。
简单来说,事实上就是让神经网络正向传播一次。然后把某层的特征值给取出来。然后转换为图片保存。
以下我提供一个demo,大家能够依据自己的需求改动。
先看看我的demo的用法。
visualize_features.bin net_proto pretrained_net_proto iterations [CPU/GPU] img_list_file dstdir laydepth
visualize_features.bin是cpp编译出来的可运行文件
以下看看各參数的意义:
1 net_proto:caffe规定的一种定义网络结构的文件格式,后缀名为".prototxt"。
这个文件定义了网络的输入,已经相关參数,还有就是总体的网络结构。
2 pretrained_net_proto:这个是已经训练好了的模型
3 iterations:迭代次数
4 [CPU/GPU]:cpu还是gpu模式
5 img_list_file:待測试的文件名称列表。我这里须要这个主要是为了得到图片的类名。
6 dstdir:图片输出的目录
7 laydepth:须要输出哪一层的特征
以下是一个实例样例:
./visualize_features.bin /home/linger/linger/caffe-action/caffe-master/examples/cifar10/cifar10_full_test.prototxt /home/linger/linger/caffe-action/caffe-master/examples/cifar10/cifar10_full_iter_60000
20 GPU /home/linger/linger/testfile/skirt_test_attachment/image_filename /home/linger/linger/testfile/innerproduct/ 7
以下是源码:
// Copyright 2013 Yangqing Jia
//
// This is a simple script that allows one to quickly test a network whose
// structure is specified by text format protocol buffers, and whose parameter
// are loaded from a pre-trained network.
// Usage:
// test_net net_proto pretrained_net_proto iterations [CPU/GPU] #include <cuda_runtime.h>
#include <fstream>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <utility>
#include "caffe/caffe.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/imgproc/imgproc.hpp> using std::make_pair;
using std::pair;
using namespace caffe; // NOLINT(build/namespaces)
using namespace std; vector<string> fileNames;
char * filelist; /*
* 读入的文件的内容格式相似这样子的:全局id 类名_所在类的id.jpg
0 一步裙_0.jpg
1 一步裙_1.jpg
2 一步裙_10.jpg
*/
void readFile()
{
if(fileNames.empty())
{
ifstream read(filelist);
//"/home/linger/linger/testfile/test_attachment/image_filename"
// "/home/linger/imdata/test_files_collar.txt"
// "/home/linger/linger/testfile/testfilename"
if(read.is_open())
{
while(!read.eof())
{
string name;
int id;
read>>id>>name;
fileNames.push_back(name);
}
}
}
} /*
* 依据图片id获取类名
*/
string getClassNameById(int id)
{
readFile();
int index = fileNames[id].find_last_of('_') ;
return fileNames[id].substr(0, index);
} void writeBatch(const float* data,int num,int channels,int width,int height,int startID,const char*dir)
{
for(int id = 0;id<num;id++)
{
for(int channel=0;channel<channels;channel++)
{
cv::Mat mat(height,width, CV_8UC1);//高宽
vector<vector<float> > vec;
vec.resize(height);
float max = -1;
float min = 999999;
for(int row=0;row<height;row++)
{
vec[row].resize(width);
for(int col=0;col<width;col++)
{
vec[row][col] =
data[id*channels*width*height+channel*width*height+row*width+col];
if(max<vec[row][col])
{
max = vec[row][col];
}
if(min>vec[row][col])
{
min = vec[row][col];
} }
} for(int row=0;row<height;row++)
{
for(int col=0;col<width;col++)
{
vec[row][col] = 255*((float)(vec[row][col]-min))/(max-min);
uchar& img = mat.at<uchar>(row,col);
img= vec[row][col]; }
}
char filename[100];
string label = getClassNameById(startID+id);
string file_reg =dir;
file_reg+="%s%05d_%05d.png";
snprintf(filename, 100, file_reg.c_str(), label.c_str(),startID+id,channel);
//printf("%s\n",filename);
cv::imwrite(filename, mat);
} }
} int main(int argc, char** argv)
{
if (argc < 4)
{
LOG(ERROR) << "visualize_features.bin net_proto pretrained_net_proto iterations "
<< "[CPU/GPU] img_list_file dstdir laydepth";
return 0;
}
/* ./visualize_features.bin /home/linger/linger/caffe-action/caffee-ext/Caffe_MM/prototxt/triplet/triplet_test_simple.prototxt /home/linger/linger/caffe-action/caffee-ext/Caffe_MM/snapshorts/_iter_100000 8 GPU /home/linger/linger/testfile/test_attachment/image_filename /home/linger/linger/testfile/innerproduct/ 6 */ filelist = argv[5];
cudaSetDevice(0);
Caffe::set_phase(Caffe::TEST); if (argc == 5 && strcmp(argv[4], "GPU") == 0)
{
LOG(ERROR) << "Using GPU";
Caffe::set_mode(Caffe::GPU);
}
else
{
LOG(ERROR) << "Using CPU";
Caffe::set_mode(Caffe::CPU);
} NetParameter test_net_param;
ReadProtoFromTextFile(argv[1], &test_net_param);
Net<float> caffe_test_net(test_net_param);
NetParameter trained_net_param;
ReadProtoFromBinaryFile(argv[2], &trained_net_param);
caffe_test_net.CopyTrainedLayersFrom(trained_net_param); int total_iter = atoi(argv[3]);
LOG(ERROR) << "Running " << total_iter << " Iterations."; double test_accuracy = 0;
vector<Blob<float>*> dummy_blob_input_vec; int startID = 0;
int nums;
int dims;
int batchsize = test_net_param.layers(0).layer().batchsize(); int laynum = caffe_test_net.bottom_vecs().size();
printf("num of layers:%d\n",laynum); for (int i = 0; i < total_iter; ++i)
{
const vector<Blob<float>*>& result =
caffe_test_net.Forward(dummy_blob_input_vec); int laydepth = atoi(argv[7]); Blob<float>* features = (*(caffe_test_net.bottom_vecs().begin()+laydepth))[0];//调整第几层就可以 nums = features->num();
dims= features->count()/features->num(); int num = features->num();
int channels = features->channels();
int width = features->width();
int height = features->height();
printf("channels:%d,width:%d,height:%d\n",channels,width,height);
writeBatch(features->cpu_data(),num,channels,width,height,startID,argv[6]);
startID += nums; } return 0;
}
神经网络:caffe特征可视化的代码例子的更多相关文章
- caffe net 可视化工具,,层特征可视化
1.只用网络在线结构绘制可视化网络模型 http://ethereon.github.io/netscope/#/editor 将对应的网络输入到里面,然后按shift+enter即可查看对应的网络结 ...
- 深度学习之卷积神经网络(CNN)详解与代码实现(一)
卷积神经网络(CNN)详解与代码实现 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/10430073.html 目 ...
- 《神经网络的梯度推导与代码验证》之CNN的前向传播和反向梯度推导
在FNN(DNN)的前向传播,反向梯度推导以及代码验证中,我们不仅总结了FNN(DNN)这种神经网络结构的前向传播和反向梯度求导公式,还通过tensorflow的自动求微分工具验证了其准确性.在本篇章 ...
- HOG特征原理及代码实现
HOG特征原理 HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子. 它通过计算和统计 ...
- 《神经网络的梯度推导与代码验证》之FNN(DNN)前向和反向过程的代码验证
在<神经网络的梯度推导与代码验证>之FNN(DNN)的前向传播和反向梯度推导中,我们学习了FNN(DNN)的前向传播和反向梯度求导,但知识仍停留在纸面.本篇章将基于深度学习框架tensor ...
- 《神经网络的梯度推导与代码验证》之CNN前向和反向传播过程的代码验证
在<神经网络的梯度推导与代码验证>之CNN的前向传播和反向梯度推导 中,我们学习了CNN的前向传播和反向梯度求导,但知识仍停留在纸面.本篇章将基于深度学习框架tensorflow验证我们所 ...
- 《神经网络的梯度推导与代码验证》之vanilla RNN的前向传播和反向梯度推导
在本篇章,我们将专门针对vanilla RNN,也就是所谓的原始RNN这种网络结构进行前向传播介绍和反向梯度推导.更多相关内容请见<神经网络的梯度推导与代码验证>系列介绍. 注意: 本系列 ...
- 30个php操作redis常用方法代码例子
From: http://www.jb51.net/article/51884.htm 这篇文章主要介绍了30个php操作redis常用方法代码例子,本文其实不止30个方法,可以操作string类型. ...
- 30 个 php 操作 redis 常用方法代码例子
这篇文章主要介绍了 30 个 php 操作 redis 常用方法代码例子 , 本文其实不止 30 个方法 , 可以操作 string 类型. list 类型和 set 类型的数据 , 需要的朋友可以参 ...
随机推荐
- Eclipse断点调试(DBG)Android应用
1.添加断点 双击左侧边框便可添加断点,右击也能添加断点. 2.进入调试模式 点击虫子,然后选择工程运行,快捷键为单击F11 ,如果是正常运行就是Ctrl+F11 3.单步调试+跳到下一个断点 运行到 ...
- [转]MySQL事务学习-->隔离级别
From : http://blog.csdn.net/mchdba/article/details/12837427 6 事务的隔离级别 设置的目的 在数据库操作中,为了有效保证并发读取数据的正确性 ...
- Windows Server 2012上安装.NET Framework 3.5(不需要安装光盘)
因为在windows2012里,安装数据库,IIS部分组件都需要.NET3.5,而默认windows2012安装时,并不会把此组件复制到电脑里 导致,后期要安装.NET3.5还需要安装盘.但是,很多人 ...
- JAVA中String.format的用法 格式化字符串,格式化数字,日期时间格式化,
1.对整数进行格式化:%[index$][标识][最小宽度]转换方式 我们可以看到,格式化字符串由4部分组成,其中%[index$]的含义我们上面已经讲过,[最小宽度]的含义也很好理解, ...
- java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
错误信息 查看Console标签页: 这儿提示找不到HttpServletRequest类. 解决办法 规则文件更新的时候需要调用servlet-api.jar相关的类,如果您的系统环境下无法找到这个 ...
- DatabaseMirroring搭建
1. 概述 数据库镜像维护一个数据库的两个副本,这两个副本必须驻留在不同的 SQL Server 数据库引擎 服务器实例上.通常,这些服务器实例驻留在不同位置的计算机上.启动数据库上的数据库镜像 ...
- N体运动的程序模拟
这依然是与<三体>有关的一篇文章.空间中三个星体在万有引力作用下的运动被称之为三体问题,参见我的上一篇文章:三体运动的程序模拟.而这一节,对三体问题进行了扩展,实现了空间中N个星体在万有引 ...
- 很不错的python 机器学习博客
http://www.cuijiahua.com/resource.html 曾看过的书,感觉一些很有用的学习资料,推荐给大家! Python基础: 网络教程推荐: 系统学习python3可以看廖雪峰 ...
- 一款基于TweenMax跟随鼠标单击移动的div
今天给大家分享一款基于TweenMax跟随鼠标单击移动的div.在这款实例中你可以单击任意位置,div会移动到你单击的位置.效果图如下: 在线预览 源码下载 实现的代码. html代码: < ...
- 利用blob对象实现粘贴图片
blob的一个常用应用场景,就是获取剪切板上的数据来进行粘贴的操作.例如通过QQ截图后,需要在网页上进行粘贴操作. 粘贴图片我们需要解决下面几个问题 1.监听用户的粘贴操作 2.获取到剪切板上的数据 ...