Add a new option to add a labels file and a means file.

This commit is contained in:
2017-10-09 13:00:26 +02:00
parent 1e901507fa
commit e70c5f22ce
5 changed files with 92 additions and 34 deletions

View File

@@ -1,5 +1,5 @@
#include <cassert>
#include <iostream>
#include <iterator>
#include <vector>
#include "Simulator.hpp"
@@ -8,7 +8,7 @@ using namespace caffe;
using namespace std;
using namespace fmri;
Simulator::Simulator(const string& model_file, const string& weights_file) :
Simulator::Simulator(const string& model_file, const string& weights_file, const string& means_file) :
net(model_file, TEST)
{
net.CopyTrainedLayersFrom(weights_file);
@@ -21,16 +21,18 @@ Simulator::Simulator(const string& model_file, const string& weights_file) :
input_geometry.height, input_geometry.width);
/* Forward dimension change to all layers. */
net.Reshape();
if (means_file != "") {
means = processMeans(means_file);
}
}
void Simulator::simulate(const string& image_file)
vector<Simulator::DType> Simulator::simulate(const string& image_file)
{
cv::Mat im = cv::imread(image_file, -1);
if (im.empty()) {
cerr << "Unable to read " << image_file << endl;
return;
}
assert(!im.empty());
auto input = preprocess(im);
auto channels = getWrappedInputLayer();
@@ -44,10 +46,7 @@ void Simulator::simulate(const string& image_file)
const DType *end = begin + output_layer->channels();
vector<DType> result(begin, end);
// TODO: visualize, rather than just print.
for (auto v : result) {
cout << v << endl;
}
return result;
}
vector<cv::Mat> Simulator::getWrappedInputLayer()
@@ -111,8 +110,36 @@ cv::Mat Simulator::preprocess(cv::Mat original) const
cv::Mat sample_float;
resized.convertTo(sample_float, num_channels == 3 ? CV_32FC3 : CV_32FC1);
// TODO: substract means.
// Don't know if necessary yet.
if (means.empty()) {
return sample_float;
}
cv::Mat normalized;
cv::subtract(sample_float, means, normalized);
return normalized;
return sample_float;
}
cv::Mat Simulator::processMeans(const string &means_file) const
{
BlobProto proto;
ReadProtoFromBinaryFileOrDie(means_file, &proto);
Blob<DType> mean_blob;
mean_blob.FromProto(proto);
assert(mean_blob.channels() == num_channels);
vector<cv::Mat> channels;
float* data = mean_blob.mutable_cpu_data();
for (unsigned int i = 0; i < 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);
return cv::Mat(input_geometry, mean.type(), cv::mean(mean));
}