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:
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.
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;
...
FeatureMapGenerator class Documentation.
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
None
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) |
FeatureMapGenerator::FeatureMapGenerator(std::string modelFile, std::string trainedFile)
Constructs a FeatureMapGenerator and loads the trained caffe model.
FeatureMapGenerator::~FeatureMapGenerator()
Destroys the generator.
void FeatureMapGenerator::setMeanValue(std::vector &meanValue)
Sets the meanValue to meanValue.
See also getMeanValue().
void FeatureMapGenerator::setScale(float scale)
Sets the scale to scale.
See also getScale().
std::vector FeatureMapGenerator::getMeanValue() const
returns the meanValue.
See also setMeanValue().
float FeatureMapGenerator::getScale() const
returns the scale.
See also setScale().
std::unordered_map<std::string, std::vector<cv::Mat>> FeatureMapGenerator::getFeatureMaps() const
returns the featureMaps.
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().
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().