Actually run the network.
This commit is contained in:
@@ -1,15 +1,118 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <iterator>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "Simulator.hpp"
|
#include "Simulator.hpp"
|
||||||
|
|
||||||
using namespace caffe;
|
using namespace caffe;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace fmri;
|
using namespace fmri;
|
||||||
|
|
||||||
Simulator::Simulator(const string &model_file, const string &weights_file) :
|
Simulator::Simulator(const string& model_file, const string& weights_file) :
|
||||||
net(model_file, TEST) {
|
net(model_file, TEST)
|
||||||
net.CopyTrainedLayersFrom(weights_file);
|
{
|
||||||
|
net.CopyTrainedLayersFrom(weights_file);
|
||||||
|
|
||||||
|
Blob<DType>* 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Simulator::simulate(const string &image_file) {
|
void Simulator::simulate(const string& image_file)
|
||||||
cerr << "This is not implemented yet." << endl;
|
{
|
||||||
|
cv::Mat im = cv::imread(image_file, -1);
|
||||||
|
|
||||||
|
if (im.empty()) {
|
||||||
|
cerr << "Unable to read " << image_file << endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto input = preprocess(im);
|
||||||
|
auto channels = getWrappedInputLayer();
|
||||||
|
|
||||||
|
cv::split(input, channels);
|
||||||
|
|
||||||
|
net.Forward();
|
||||||
|
|
||||||
|
Blob<DType> *output_layer = net.output_blobs()[0];
|
||||||
|
const DType *begin = output_layer->cpu_data();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<cv::Mat> Simulator::getWrappedInputLayer()
|
||||||
|
{
|
||||||
|
vector<cv::Mat> channels;
|
||||||
|
Blob<DType>* input_layer = net.input_blobs()[0];
|
||||||
|
|
||||||
|
const int width = input_geometry.width;
|
||||||
|
const int height = input_geometry.height;
|
||||||
|
|
||||||
|
DType* input_data = input_layer->mutable_cpu_data();
|
||||||
|
for (unsigned int i = 0; i < num_channels; i++) {
|
||||||
|
channels.emplace_back(height, width, CV_32FC1, input_data);
|
||||||
|
input_data += width * height;
|
||||||
|
}
|
||||||
|
|
||||||
|
return channels;
|
||||||
|
}
|
||||||
|
|
||||||
|
static cv::Mat fix_channels(const int num_channels, cv::Mat original) {
|
||||||
|
if (num_channels == original.channels()) {
|
||||||
|
return original;
|
||||||
|
}
|
||||||
|
|
||||||
|
cv::Mat converted;
|
||||||
|
|
||||||
|
if (num_channels == 1 && original.channels() == 3) {
|
||||||
|
cv::cvtColor(original, converted, cv::COLOR_BGR2GRAY);
|
||||||
|
} else if (num_channels == 1 && original.channels() == 4) {
|
||||||
|
cv::cvtColor(original, converted, cv::COLOR_BGRA2GRAY);
|
||||||
|
} else if (num_channels == 3 && original.channels() == 1) {
|
||||||
|
cv::cvtColor(original, converted, cv::COLOR_GRAY2BGR);
|
||||||
|
} else if (num_channels == 3 && original.channels() == 4) {
|
||||||
|
cv::cvtColor(original, converted, cv::COLOR_BGRA2BGR);
|
||||||
|
} else {
|
||||||
|
// Don't know how to convert.
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
return converted;
|
||||||
|
}
|
||||||
|
|
||||||
|
static cv::Mat resize(const cv::Size& targetSize, cv::Mat original)
|
||||||
|
{
|
||||||
|
if (targetSize != original.size()) {
|
||||||
|
cv::Mat resized;
|
||||||
|
cv::resize(original, resized, targetSize);
|
||||||
|
|
||||||
|
return resized;
|
||||||
|
}
|
||||||
|
|
||||||
|
return original;
|
||||||
|
}
|
||||||
|
|
||||||
|
cv::Mat Simulator::preprocess(cv::Mat original) const
|
||||||
|
{
|
||||||
|
auto converted = fix_channels(num_channels, original);
|
||||||
|
|
||||||
|
auto resized = resize(input_geometry, converted);
|
||||||
|
|
||||||
|
cv::Mat sample_float;
|
||||||
|
resized.convertTo(sample_float, num_channels == 3 ? CV_32FC3 : CV_32FC1);
|
||||||
|
|
||||||
|
// TODO: substract means.
|
||||||
|
// Don't know if necessary yet.
|
||||||
|
|
||||||
|
return sample_float;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,16 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <caffe/caffe.hpp>
|
#include <caffe/caffe.hpp>
|
||||||
|
#include <opencv2/core/core.hpp>
|
||||||
|
#include <opencv2/highgui/highgui.hpp>
|
||||||
|
#include <opencv2/imgproc/imgproc.hpp>
|
||||||
|
|
||||||
namespace fmri {
|
namespace fmri {
|
||||||
using std::string;
|
using std::string;
|
||||||
|
using std::vector;
|
||||||
|
|
||||||
class Simulator {
|
class Simulator {
|
||||||
public:
|
public:
|
||||||
@@ -18,5 +23,10 @@ namespace fmri {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
caffe::Net<DType> net;
|
caffe::Net<DType> net;
|
||||||
|
cv::Size input_geometry;
|
||||||
|
unsigned int num_channels;
|
||||||
|
|
||||||
|
vector<cv::Mat> getWrappedInputLayer();
|
||||||
|
cv::Mat preprocess(cv::Mat original) const;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,5 +16,7 @@ int main(int argc, char *const argv[]) {
|
|||||||
simulator.simulate(image);
|
simulator.simulate(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
::google::ShutdownGoogleLogging();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user