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

@@ -10,14 +10,14 @@ using namespace std;
static void show_help(const char *progname, int exitcode) {
cerr << "Usage: " << progname << " -m MODEL -w WEIGHTS INPUTS..." << endl
<< endl
<< R"END(
Simulate the specified network on the specified inputs.
<< R"END(Simulate the specified network on the specified inputs.
Options:
-h show this message
-m (required) the model file to simulate
-n (required) the model file to simulate
-w (required) the trained weights
)END" << endl;
-m means file. Will be substracted from input if available.
-l labels file. Will be used to print prediction labels if available.)END" << endl;
exit(exitcode);
}
@@ -32,10 +32,12 @@ static void check_file(const char *filename) {
Options Options::parse(const int argc, char *const argv[]) {
string model;
string weights;
string means;
string labels;
char c;
while ((c = getopt(argc, argv, "hm:w:")) != -1) {
while ((c = getopt(argc, argv, "hm:w:n:l:")) != -1) {
switch (c) {
case 'h':
show_help(argv[0], 0);
@@ -46,16 +48,27 @@ Options Options::parse(const int argc, char *const argv[]) {
weights = optarg;
break;
case 'm':
case 'n':
check_file(optarg);
model = optarg;
break;
case 'm':
check_file(optarg);
means = optarg;
break;
case 'l':
check_file(optarg);
labels = optarg;
break;
case '?':
show_help(argv[0], 1);
break;
default:
cerr << "Unhandled option: " << c << endl;
abort();
}
}
@@ -78,23 +91,36 @@ Options Options::parse(const int argc, char *const argv[]) {
show_help(argv[0], 1);
}
return Options(move(model), move(weights), move(inputs));
return Options(move(model), move(weights), move(means), move(labels), move(inputs));
}
Options::Options(string &&model, string &&weights, vector<string> &&inputs) noexcept:
Options::Options(string &&model, string &&weights, string&& means, string&& labels, vector<string> &&inputs) noexcept:
modelPath(move(model)),
weightsPath(move(weights)),
inputPaths(move(inputs)) {
meansPath(means),
labelsPath(labels),
inputPaths(move(inputs))
{
}
const string &Options::model() const {
const string& Options::model() const {
return modelPath;
}
const string &Options::weights() const {
const string& Options::weights() const {
return weightsPath;
}
const vector<string> &Options::inputs() const {
const vector<string>& Options::inputs() const {
return inputPaths;
}
const string& Options::means() const
{
return meansPath;
}
const string& Options::labels() const
{
return labelsPath;
}

View File

@@ -12,17 +12,20 @@ namespace fmri {
public:
static Options parse(const int argc, char *const argv[]);
const string &model() const;
const string& model() const;
const string& weights() const;
const string& means() const;
const string& labels() const;
const string &weights() const;
const vector<string> &inputs() const;
const vector<string>& inputs() const;
private:
const string modelPath;
const string weightsPath;
const string meansPath;
const string labelsPath;
const vector<string> inputPaths;
Options(string &&, string &&, vector<string> &&) noexcept;
Options(string &&, string &&, string&&, string&&, vector<string> &&) noexcept;
};
}

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;
}
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));
}

View File

@@ -17,16 +17,18 @@ namespace fmri {
public:
typedef float DType;
Simulator(const string &model_file, const string &weights_file);
Simulator(const string &model_file, const string &weights_file, const string &means_file = "");
void simulate(const string &input_file);
vector<DType> simulate(const string &input_file);
private:
caffe::Net<DType> net;
cv::Size input_geometry;
cv::Mat means;
unsigned int num_channels;
vector<cv::Mat> getWrappedInputLayer();
cv::Mat preprocess(cv::Mat original) const;
cv::Mat processMeans(const string &means_file) const;
};
}

View File

@@ -10,7 +10,7 @@ int main(int argc, char *const argv[]) {
Options options = Options::parse(argc, argv);
Simulator simulator(options.model(), options.weights());
Simulator simulator(options.model(), options.weights(), options.means());
for (const auto &image : options.inputs()) {
simulator.simulate(image);