From 1945f3c361445ba0018f2fcbb288a814e9e9999a Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 13 Oct 2017 16:09:00 +0200 Subject: [PATCH] Restructure include files. This way main.o is much faster to compile, leaving Simulator.o the only file that really takes time. --- src/Simulator.cpp | 30 ++++++++++++++++++++---------- src/Simulator.hpp | 15 ++++++++++----- src/main.cpp | 2 ++ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/Simulator.cpp b/src/Simulator.cpp index 58eb352..e112f17 100644 --- a/src/Simulator.cpp +++ b/src/Simulator.cpp @@ -3,24 +3,29 @@ #include #include "Simulator.hpp" +#include + +#include +#include +#include using namespace caffe; using namespace std; using namespace fmri; Simulator::Simulator(const string& model_file, const string& weights_file, const string& means_file) : - net(model_file, TEST) + net(new Net(model_file, TEST)) { - net.CopyTrainedLayersFrom(weights_file); + net->CopyTrainedLayersFrom(weights_file); - Blob* input_layer = net.input_blobs()[0]; + Blob* input_layer = net->input_blobs()[0]; input_geometry = cv::Size(input_layer->width(), input_layer->height()); num_channels = input_layer->channels(); input_layer->Reshape(1, num_channels, input_geometry.height, input_geometry.width); /* Forward dimension change to all layers. */ - net.Reshape(); + net->Reshape(); if (means_file != "") { means = processMeans(means_file); @@ -41,15 +46,15 @@ vector Simulator::simulate(const string& image_file) cv::split(input, channels); - net.Forward(); + net->Forward(); vector result; - Blob* input_layer = net.input_blobs()[0]; + Blob* input_layer = net->input_blobs()[0]; - const auto& names = net.layer_names(); - const auto& results = net.top_vecs(); - const auto& layers = net.layers(); + const auto& names = net->layer_names(); + const auto& results = net->top_vecs(); + const auto& layers = net->layers(); for (unsigned int i = 0; i < names.size(); ++i) { CHECK_EQ(results[i].size(), 1) << "Multiple outputs per layer are not supported!" << endl; @@ -64,7 +69,7 @@ vector Simulator::simulate(const string& image_file) vector Simulator::getWrappedInputLayer() { vector channels; - Blob* input_layer = net.input_blobs()[0]; + Blob* input_layer = net->input_blobs()[0]; const int width = input_geometry.width; const int height = input_geometry.height; @@ -151,3 +156,8 @@ cv::Mat Simulator::processMeans(const string &means_file) const return cv::Mat(input_geometry, mean.type(), cv::mean(mean)); } + +Simulator::~Simulator() +{ + // Empty but defined constructor. +} diff --git a/src/Simulator.hpp b/src/Simulator.hpp index 9762d66..867f8d7 100644 --- a/src/Simulator.hpp +++ b/src/Simulator.hpp @@ -4,14 +4,18 @@ #include #include -#include -#include -#include -#include +#include +#include #include "utils.hpp" #include "LayerData.hpp" +namespace caffe +{ + template + class Net; +} + namespace fmri { using std::string; using std::vector; @@ -19,11 +23,12 @@ namespace fmri { class Simulator { public: Simulator(const string &model_file, const string &weights_file, const string &means_file = ""); + ~Simulator(); vector simulate(const string &input_file); private: - caffe::Net net; + std::unique_ptr> net; cv::Size input_geometry; cv::Mat means; unsigned int num_channels; diff --git a/src/main.cpp b/src/main.cpp index 9667390..f1b3154 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ #include #include #include +#include + #include "Options.hpp" #include "Simulator.hpp" #include "PNGDumper.hpp"