From 470399aabf9af471606d997cc55213875066b815 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Tue, 2 Jan 2018 16:17:07 +0100 Subject: [PATCH] Implement a new layer data type. Contrary to layerdata, this describes the layer itself rather than the layer contents. --- src/LayerInfo.cpp | 33 ++++++++++++++++++++++++++ src/LayerInfo.hpp | 35 ++++++++++++++++++++++++++++ src/Simulator.cpp | 59 ++++++++++++++++++++++++++++++++++++----------- src/Simulator.hpp | 2 ++ 4 files changed, 116 insertions(+), 13 deletions(-) create mode 100644 src/LayerInfo.cpp create mode 100644 src/LayerInfo.hpp diff --git a/src/LayerInfo.cpp b/src/LayerInfo.cpp new file mode 100644 index 0000000..8ef9a2d --- /dev/null +++ b/src/LayerInfo.cpp @@ -0,0 +1,33 @@ +#include "LayerInfo.hpp" + +using namespace std; +using namespace fmri; + + +LayerInfo::Type LayerInfo::typeByName(string_view name) +{ + if (name == "Input") { + return Type::Input; + } else if (name == "Convolution") { + return Type::Convolutional; + } else if (name == "ReLU") { + return Type::ReLU; + } else if (name == "Pooling") { + return Type::Pooling; + } else { + LOG(INFO) << "Received unknown layer type: " << name << endl; + return Type::Other; + } +} + +LayerInfo::LayerInfo(string_view name, string_view type, + const vector>> ¶meters) +: parameters_(parameters), name_(name), type_(typeByName(type)) +{ + +} + +const std::string &LayerInfo::name() const +{ + return name_; +} diff --git a/src/LayerInfo.hpp b/src/LayerInfo.hpp new file mode 100644 index 0000000..d0019b8 --- /dev/null +++ b/src/LayerInfo.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include +#include "utils.hpp" + +namespace fmri +{ + class LayerInfo + { + public: + enum class Type + { + Input, + Convolutional, + ReLU, + Pooling, + Output, + Other + }; + + LayerInfo(std::string_view name, std::string_view type, + const std::vector>> ¶meters); + + const std::string& name() const; + + static Type typeByName(std::string_view name); + + private: + std::vector>> parameters_; + Type type_; + std::string name_; + }; +} diff --git a/src/Simulator.cpp b/src/Simulator.cpp index bec91b0..9b5dbcd 100644 --- a/src/Simulator.cpp +++ b/src/Simulator.cpp @@ -21,12 +21,18 @@ struct Simulator::Impl cv::Size input_geometry; optional means; unsigned int num_channels; + map layerInfo_; Impl(const string& model_file, const string& weights_file, const string& means_file); vector getWrappedInputLayer(); cv::Mat preprocess(cv::Mat original) const; vector simulate(const string &input_file); + const map& layerInfo() const; + + void computeLayerInfo(); + + void loadMeans(const string &means_file); }; // Create simple forwarding functions. @@ -55,27 +61,33 @@ Simulator::Impl::Impl(const string& model_file, const string& weights_file, cons net.Reshape(); if (!means_file.empty()) { - // Read in the means file - BlobProto proto; - ReadProtoFromBinaryFileOrDie(means_file, &proto); + loadMeans(means_file); + } - Blob mean_blob; - mean_blob.FromProto(proto); + computeLayerInfo(); +} - CHECK_EQ(mean_blob.channels(), num_channels) << "Number of channels should match!" << endl; +void Simulator::Impl::loadMeans(const string &means_file) +{// Read in the means file + BlobProto proto; + ReadProtoFromBinaryFileOrDie(means_file, &proto); - vector channels; - float* data = mean_blob.mutable_cpu_data(); - for (unsigned int i = 0; i < num_channels; ++i) { + Blob mean_blob; + mean_blob.FromProto(proto); + + CHECK_EQ(mean_blob.channels(), num_channels) << "Number of channels should match!" << endl; + + vector channels; + float* data = mean_blob.mutable_cpu_data(); + for (unsigned int i = 0; i < this->num_channels; ++i) { channels.emplace_back(mean_blob.height(), mean_blob.width(), CV_32FC1, data); data += mean_blob.height() * mean_blob.width(); } - cv::Mat mean; - cv::merge(channels, mean); + cv::Mat mean; + merge(channels, mean); - means = cv::Mat(input_geometry, mean.type(), cv::mean(mean)); - } + this->means = cv::Mat(this->input_geometry, mean.type(), cv::mean(mean)); } vector Simulator::Impl::simulate(const string& image_file) @@ -177,7 +189,28 @@ cv::Mat Simulator::Impl::preprocess(cv::Mat original) const } +const map &Simulator::Impl::layerInfo() const +{ + return layerInfo_; +} + +void Simulator::Impl::computeLayerInfo() +{ + const auto& names = net.layer_names(); + const auto& layers = net.layers(); + + for (auto i = 0u; i < names.size() && i < layers.size(); ++i) { + auto& layer = layers[i]; + layerInfo_.emplace(names[i], LayerInfo(names[i], layer->type(), layer->blobs())); + } +} + Simulator::~Simulator() { // Empty but defined constructor. } + +const map & Simulator::layerInfo() const +{ + return pImpl->layerInfo(); +} diff --git a/src/Simulator.hpp b/src/Simulator.hpp index 4353b8c..48dfc52 100644 --- a/src/Simulator.hpp +++ b/src/Simulator.hpp @@ -4,6 +4,7 @@ #include #include #include "LayerData.hpp" +#include "LayerInfo.hpp" namespace fmri { using std::string; @@ -15,6 +16,7 @@ namespace fmri { ~Simulator(); vector simulate(const string &input_file); + const std::map& layerInfo() const; private: struct Impl;