forked from stupel/Preprocessing
-
Notifications
You must be signed in to change notification settings - Fork 2
/
preprocessing_caffenetwork.h
executable file
·104 lines (82 loc) · 3.1 KB
/
preprocessing_caffenetwork.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef PREPROCESSING_CAFFENETWORK_H
#define PREPROCESSING_CAFFENETWORK_H
#define GLOG_NO_ABBREVIATED_SEVERITIES
#include "preprocessing_config.h"
//Caffe
#include <caffe/caffe.hpp>
//OpenCV
#include <opencv2/opencv.hpp>
#include <algorithm>
#include <fstream> // NOLINT(readability/streams)
#include <string>
#include <utility>
#include <vector>
#include "boost/algorithm/string.hpp"
#include "boost/scoped_ptr.hpp"
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/db.hpp"
#include "caffe/util/format.hpp"
#include "caffe/util/io.hpp"
#include "caffe/util/rng.hpp"
#include "caffe/util/signal_handler.h"
using std::pair;
using boost::scoped_ptr;
using caffe::Blob;
using caffe::Caffe;
using caffe::Net;
using caffe::Layer;
using caffe::Solver;
using caffe::shared_ptr;
using caffe::string;
using caffe::Timer;
using caffe::vector;
using std::ostringstream;
using namespace caffe;
/* Pair (label, confidence) representing a prediction. */
typedef std::pair<string, float> Prediction;
class PreprocessingCaffeNetwork : public QObject
{
Q_OBJECT
public:
PreprocessingCaffeNetwork();
~PreprocessingCaffeNetwork();
std::vector<Prediction> classify(const cv::Mat img);
std::vector<vector<Prediction>> classifyBatch(const vector<cv::Mat> imgs, int num_classes);
bool getNetworkLoaded() const;
void loadModel(const QString& model_file,
const QString& trained_file,
const QString& mean_file,
const QString& label_file);
private:
QObject *parent;
std::shared_ptr<Net<float> > net_;
cv::Size input_geometry_;
int num_channels;
cv::Mat mean_;
std::vector<std::string> labels_;
int batchSize;
bool networkLoaded;
void setMean(const std::string& mean_file);
std::vector<float> predict(const cv::Mat& img);
std::vector<float> predictBatch(const vector< cv::Mat > imgs);
void wrapInputLayer(std::vector<cv::Mat>* input_channels);
void wrapBatchInputLayer(std::vector<std::vector<cv::Mat> > *input_batch);
void preprocess(const cv::Mat& img, std::vector<cv::Mat>* input_channels);
void preprocessBatch(const vector<cv::Mat> imgs, std::vector< std::vector<cv::Mat> >* input_batch);
void computeImageMean(const QString &outputDir, QString setName, int totalCount);
void trainNetwork();
void trainNetworkWithExe(const QString &outputDir);
static std::vector<int> caffeArgmax(const std::vector<float>& v, int N);
static bool caffePairCompare(const std::pair<float, int>& lhs, const std::pair<float, int>& rhs);
static void get_gpus(vector<int>* gpus);
std::vector<string> get_stages_from_flags();
caffe::SolverAction::Enum GetRequestedAction(const std::string& flag_value);
void CopyLayers(caffe::Solver<float>* solver, const std::string& model_list);
signals:
void logSignal(QString, QString);
void updateProgressBarSignal(QString, int, QString);
void trainingDoneSignal();
};
#endif // PREPROCESSING_CAFFENETWORK_H