Skip to content

A little c++ class for getting the feature maps image in any layer of the neural network by loading the trained caffe model.

Notifications You must be signed in to change notification settings

somone23412/FeatureMapGenerator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 

Repository files navigation

FeatureMapGenerator

A little c++ class for getting the feature maps in any layer of the neural network by loading the trained caffe model.

By using this class you can see the feature maps of each layer in the neural network.

Here's an example running on G_Model:

featureMap

This example is mainly used to show what the generated feature map looks like. If you are interested in what this neural network has done, you can take a look at the model author's matlab code.

Getting start

You need caffe and opencv environments to use this class.

  • using example:
#include "FeatureMapGenerator.h"

	...
	//init generator, loading pre-trained caffe model.
	FeatureMapGenerator *featureMapGenerator = new FeatureMapGenerator("model/Model_G.prototxt", "model/Model_G.caffemodel");
	
	//Layers which you want to generate feature maps from.
	std::vector<std::string> layerNames = {"data","conv0_1", "conv1_1_new", "conv_decode1_1_new", "reconstruction_new"};
	//load test image.
	cv::Mat faceImg = cv::imread("faceimg/182701.png");
	
	//forward pass network, generate feature maps.
	featureMapGenerator->generateFeatureMaps(faceImg, layerNames);
	
	//get feature maps, which saved as "HashMap<string, Mat[]>".
	auto genImgs = featureMapGenerator->getFeatureMaps(); //"auto" here = std::unordered_map<std::string, std::vector<cv::Mat>>
	
	//if feature channels == 3, you can get a three-channel RGB Mat object in "hashMap[name][3]".
	cv::imshow("genImg", genImgs["data"][3]);
	
	//destroy generator.
	delete featureMapGenerator;
	...
	
	

Documentation

FeatureMapGenerator class Documentation.

Contents

FeatureMapGenerator Class

The FeatureMapGenerator class is used to getting the feature maps in any layer of the neural network by loading the trained caffe model.

Header: #include "FeatureMapGenerator.h"

Included Header: <opencv2\opencv.hpp>, <caffe\caffe.hpp>, <caffe\proto\caffe.pb.h>, <caffe\data_transformer.hpp>, <vector>, <string>, <unordered_map>

Inherits: None

PublicTypes

None

PublicFunctions

returns functions
FeatureMapGenerator(std::string modelFile, std::string trainedFile)
~FeatureMapGenerator()
void setMeanValue(std::vector<float> &meanValue)
void setScale(float scale)
std::vector<float> getMeanValue() const
float getScale() const
std::unordered_map<std::string, std::vector<cv::Mat>> getFeatureMaps() const
std::unordered_map<std::string, std::vector<cv::Mat>> generateFeatureMaps(cv::Mat img, std::vector<std::string> &layerNames)
std::unordered_map<std::string, std::vector<cv::Mat>> generateFeatureMaps(std::string imgPath, std::vector<std::string> &layerNames)

Member Function Documentation


func_1

FeatureMapGenerator::FeatureMapGenerator(std::string modelFile, std::string trainedFile)

Constructs a FeatureMapGenerator and loads the trained caffe model.


func_2

FeatureMapGenerator::~FeatureMapGenerator()

Destroys the generator.


func_3

void FeatureMapGenerator::setMeanValue(std::vector &meanValue)

Sets the meanValue to meanValue.

See also getMeanValue().


func_4

void FeatureMapGenerator::setScale(float scale)

Sets the scale to scale.

See also getScale().


func_5

std::vector FeatureMapGenerator::getMeanValue() const

returns the meanValue.

See also setMeanValue().


func_6

float FeatureMapGenerator::getScale() const

returns the scale.

See also setScale().


func_7

std::unordered_map<std::string, std::vector<cv::Mat>> FeatureMapGenerator::getFeatureMaps() const

returns the featureMaps.


func_8

std::unordered_map<std::string, std::vector<cv::Mat>> FeatureMapGenerator::generateFeatureMaps(cv::Mat img, std::vector<std::string> &layerNames)

+1 overloads.

Get the img from cv::Mat and forward the neural network, save the feature map of each layer to featureMaps.

returns the featureMaps.

See also generateFeatureMaps().

func_9

std::unordered_map<std::string, std::vector<cv::Mat>> FeatureMapGenerator::generateFeatureMaps(std::string imgPath, std::vector<std::string> &layerNames)

+1 overloads.

Get the img from file and forward the neural network, save the feature map of each layer to featureMaps.

returns the featureMaps.

See also generateFeatureMaps().

About

A little c++ class for getting the feature maps image in any layer of the neural network by loading the trained caffe model.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages