More explicit use of "Optional".

This commit is contained in:
2017-10-18 13:00:16 +02:00
parent a151f0f5e3
commit 754874dcf3
4 changed files with 30 additions and 23 deletions

View File

@@ -3,6 +3,7 @@
#include <iostream>
#include <unistd.h>
#include "Options.hpp"
#include "utils.hpp"
using namespace fmri;
using namespace std;
@@ -127,12 +128,20 @@ const string& Options::means() const
return meansPath;
}
const string& Options::labels() const
optional<vector<string>> Options::labels() const
{
return labelsPath;
if (labelsPath.empty()) {
return nullopt;
} else {
return read_vector<string>(labelsPath);
}
}
const string &Options::imageDump() const
std::optional<PNGDumper> Options::imageDumper() const
{
return dumpPath;
if (dumpPath.empty()) {
return nullopt;
} else {
return move(PNGDumper(dumpPath));
}
}

View File

@@ -1,7 +1,10 @@
#pragma once
#include <vector>
#include <optional>
#include <string>
#include <vector>
#include "PNGDumper.hpp"
namespace fmri {
@@ -15,8 +18,8 @@ namespace fmri {
const string& model() const;
const string& weights() const;
const string& means() const;
const string& labels() const;
const string& imageDump() const;
std::optional<vector<string>> labels() const;
std::optional<fmri::PNGDumper> imageDumper() const;
const vector<string>& inputs() const;

View File

@@ -15,6 +15,8 @@ namespace fmri
{
public:
PNGDumper(string_view baseDir);
PNGDumper(PNGDumper&&) = default;
PNGDumper(const PNGDumper&) = default;
void dump(const LayerData& layerData);

View File

@@ -1,11 +1,9 @@
#include <algorithm>
#include <iostream>
#include <memory>
#include <glog/logging.h>
#include "Options.hpp"
#include "Simulator.hpp"
#include "PNGDumper.hpp"
using namespace std;
using namespace fmri;
@@ -14,15 +12,8 @@ int main(int argc, char *const argv[]) {
google::InitGoogleLogging(argv[0]);
Options options = Options::parse(argc, argv);
vector<string> labels;
if (!options.labels().empty()) {
labels = read_vector<string>(options.labels());
}
unique_ptr<PNGDumper> pngDumper;
if (!options.imageDump().empty()) {
pngDumper.reset(new PNGDumper(options.imageDump()));
}
auto labels = options.labels();
auto dumper = options.imageDumper();
Simulator simulator(options.model(), options.weights(), options.means());
@@ -31,9 +22,9 @@ int main(int argc, char *const argv[]) {
LOG(INFO) << "Result for " << image << ":" << endl;
const auto& resultRow = res[res.size() - 1];
if (!labels.empty()) {
if (labels) {
vector<DType> weights(resultRow.data(), resultRow.data() + resultRow.numEntries());
auto scores = combine(weights, labels);
auto scores = combine(weights, *labels);
sort(scores.begin(), scores.end(), greater<>());
for (unsigned int i = 0; i < scores.size() && i < 5; ++i) {
LOG(INFO) << scores[i].first << " " << scores[i].second << endl;
@@ -42,9 +33,11 @@ int main(int argc, char *const argv[]) {
LOG(INFO) << "Best result: " << *(resultRow.data(), resultRow.data() + resultRow.numEntries()) << endl;
}
for (auto& layer : res) {
pngDumper->dump(layer);
}
if (dumper) {
for (const auto& layer : res) {
dumper->dump(layer);
}
}
}
google::ShutdownGoogleLogging();