Sample Code for caffe feature Visualization
Many readers read the previous two articles
Summarize the research process of using caffe to run image data.
Summary of deep learning practical experience 2-accuracy improved again, reaching 0.8.
Then, I want to know how to implement feature visualization.
To put it simply, it is to let the neural network spread forward once, then extract the feature values of a layer, and convert them to images for storage.
Below is a demo that you can modify as needed.
Let's take a look at the usage of my demo.
Visualize_features.bin net_proto pretrained_net_proto iterations [CPU/GPU] img_list_file dstdir laydepth
Visualize_features.bin is an executable file compiled by CPP.
Let's take a look at the meaning of each parameter:
1 net_proto: A file format defined by caffe that defines the network structure. The suffix is ". prototxt ". This file defines the network input, related parameters, and the overall network structure.
2 pretrained_net_proto: This is a trained model.
3 iterations: number of iterations
4 [CPU/GPU]: CPU or GPU Mode
5 img_list_file: List of file names to be tested. Here I need this mainly to get the Class Name of the image.
6 dstdir: the output folder of the image.
7 laydepth: the layer of features to be output
The following is an example:
. /Visualize_features.bin/home/linger/Caffe-action/Caffe-master/examples/cifar10/examples/home/linger/Caffe-action/Caffe-master/examples/cifar10 /cifar10_full_iter_60000 20 GPU/home/linger/testfile/skirt_test_attachment/image_filename/home/linger/testfile/innerproduct/7
The source code is as follows:
// 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> # inclu De <algorithm> # include <vector> # include <utility> # include "caffe/Caffe. HPP "# include <opencv2/highgui. HPP> # include <opencv2/highgui/highgui_c.h> # include <opencv2/imgproc. HPP> Using STD: make_pair; Using STD: pair; using namespace caffe; // nolint (build/namespaces) using namespace STD; vector <string> filenames; char * filelist; /** the content format of the file to be read is similar to this: global ID category _ category id.jpg 0 One-Step skirt _0.jpg 1 one-step skirt _1.jpg 2 One-step podium _10.jpg */void readfile () {If (filenames. empty () {ifstream read (filelist ); // "/home/linger/testfile/test_attachment/image_filename"/"/home/linger/imdata/test_files_collar.txt" // "/home/linger/testfile/ testfilename "If (read. is_open () {While (! Read. EOF () {string name; int ID; read >>> ID >>> name; filenames. push_back (name) ;}}}/ ** obtain the class name */string getclassnamebyid (int id) {readfile (); int Index = filenames [ID] based on the image 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); // high width vector <float> VEC; Vec. resize (height); float max =-1; float min = 999999; For (int row = 0; row