forked from jolibrain/deepdetect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtfinputconns.h
193 lines (167 loc) · 4.83 KB
/
tfinputconns.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*copyright (c) 2016 Emmanuel Benazera
* Author: Emmanuel Benazera <[email protected]>
*
* This file is part of deepdetect.
*
* deepdetect is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* deepdetect is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with deepdetect. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TFINPUTCONNS_H
#define TFINPUTCONNS_H
#include "imginputfileconn.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/framework/tensor.h"
#include <opencv2/opencv.hpp>
#include "tensorflow/cc/ops/const_op.h"
#include "tensorflow/cc/ops/image_ops.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/graph/graph_def_builder.h"
#include "inputconnectorstrategy.h"
#include "ext/base64/base64.h"
#include <glog/logging.h>
namespace dd
{
class TFInputInterface
{
public:
TFInputInterface() {}
TFInputInterface(const TFInputInterface &tii)
:_dv(tii._dv),_ids(tii._ids){}
~TFInputInterface() {}
public:
// parameters common to all TF input connectors
std::vector<tensorflow::Tensor> _dv; // main tensor for prediction.
std::vector<tensorflow::Tensor> _dv_test;
std::vector<std::string> _ids; // input ids (eg. Image Ids).
};
class ImgTFInputFileConn : public ImgInputFileConn, public TFInputInterface
{
public:
ImgTFInputFileConn()
:ImgInputFileConn()
{
reset_dv();
}
ImgTFInputFileConn(const ImgTFInputFileConn &i)
:ImgInputFileConn(i),TFInputInterface(i),_mean(i._mean),_std(i._std) {}
~ImgTFInputFileConn() {}
int channels() const
{
if (_bw) return 1;
else return 3; // RGB
}
int height() const
{
return _height;
}
int width() const
{
return _width;
}
int batch_size() const
{
if (!_dv.empty())
return _dv.size();
else return ImgInputFileConn::batch_size();
}
int test_batch_size() const
{
if (!_dv_test.empty())
return _dv_test.size();
else return ImgInputFileConn::test_batch_size();
}
void init(const APIData &ad)
{
ImgInputFileConn::init(ad);
if (ad.has("mean"))
_mean = ad.get("mean").get<double>();
if (ad.has("std"))
_std = ad.get("std").get<double>();
}
void transform(const APIData &ad)
{
try
{
ImgInputFileConn::transform(ad);
}
catch (InputConnectorBadParamException &e)
{
throw;
}
APIData ad_param = ad.getobj("parameters");
if (ad_param.has("input"))
{
APIData ad_input = ad_param.getobj("input");
if (ad_input.has("mean"))
_mean = ad_input.get("mean").get<double>();
if (ad_input.has("std"))
_std = ad_input.get("std").get<double>();
}
for (size_t i=0;i<_images.size();i++)
{
tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({1,_height,_width,channels()}));
auto input_tensor_mapped = input_tensor.tensor<float, 4>();
cv::Mat CImage = std::move(this->_images.at(i));
cv::Mat Image;
CImage.convertTo(Image, CV_32FC1);
cv::Mat Image2;
cv::cvtColor(Image,Image2,CV_BGR2RGB); // because OpenCV defaults to BGR
Image = (Image2 - _mean) / _std;
const float * source_data = (float*) Image.data;
// copying the data into the corresponding tensor
for (int y = 0; y < height(); ++y) {
const float* source_row = source_data + (y * width() * channels());
for (int x = 0; x < width(); ++x) {
const float* source_pixel = source_row + (x * channels());
for (int c = 0; c < channels(); ++c) {
const float* source_value = source_pixel + c;
input_tensor_mapped(0, y, x, c) = *source_value;
}
}
}
_dv.push_back(input_tensor);
_ids.push_back(_uris.at(i));
}
_images.clear();
}
std::vector<tensorflow::Tensor> get_dv(const int &num)
{
if (!_train)
{
int i = 0;
std::vector<tensorflow::Tensor> dv;
while(_dt_vit!=_dv.end()
&& i < num)
{
dv.push_back((*_dt_vit));
++i;
++_dt_vit;
}
return dv;
}
return std::vector<tensorflow::Tensor>(); // unused
}
void reset_dv()
{
_dt_vit = _dv.begin();
}
public:
int _mean = 128;
int _std = 128;
std::vector<tensorflow::Tensor>::const_iterator _dt_vit;
};
}
#endif