-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathtensor.cc
256 lines (227 loc) · 8.39 KB
/
tensor.cc
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "caffe2/util/tensor.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include "cvplot/cvplot.h"
namespace caffe2 {
const auto screen_width = 1600;
const auto window_padding = 4;
template <typename T>
cv::Mat to_image(const Tensor<CPUContext> &tensor, int index, float scale,
float mean, int type) {
CAFFE_ENFORCE_EQ(tensor.ndim(), 4);
auto count = tensor.dim(0), depth = tensor.dim(1), height = tensor.dim(2),
width = tensor.dim(3);
CAFFE_ENFORCE_LT(index, count);
auto data = tensor.data<T>() + (index * width * height * depth);
vector<cv::Mat> channels(depth);
for (auto &j : channels) {
j = cv::Mat(height, width, type, (void *)data);
if (scale != 1.0 || mean != 0.0) {
cv::Mat k;
j.convertTo(k, type, scale, mean);
j = k;
}
data += (width * height);
}
cv::Mat image;
cv::merge(channels, image);
if (depth == 1) {
cvtColor(image, image, CV_GRAY2RGB);
}
return image;
}
cv::Mat to_image(const Tensor<CPUContext> &tensor, int index, float scale,
float mean) {
if (tensor.IsType<float>()) {
return to_image<float>(tensor, index, scale, mean, CV_32F);
}
if (tensor.IsType<uchar>()) {
return to_image<uchar>(tensor, index, scale, mean, CV_8UC1);
}
LOG(FATAL) << "tensor to image for type " << tensor.meta().name()
<< " not implemented";
}
void TensorUtil::ShowImage(const std::string &title, int index, float scale,
float mean, bool flush) {
auto image = to_image(tensor_, index, scale, mean);
cvplot::Window::current().view(title).drawImage(&image);
cvplot::Window::current().view(title).finish();
if (flush) {
cvplot::Window::current().view(title).flush();
}
}
void TensorUtil::ShowImages(const std::string &name, float scale, float mean,
bool flush) {
for (auto i = 0, e = (int)tensor_.dim(0); i != e; i++) {
ShowImage(name + "-" + std::to_string(i), i, scale, mean,
flush && (i + 1 == e));
}
}
void TensorUtil::WriteImages(const std::string &name, float mean, bool lossy,
int index) {
auto count = tensor_.dim(0);
for (int i = 0; i < count; i++) {
auto suffix = index >= 0 ? "_" + std::to_string(i + index) : "";
WriteImage(name + suffix, i, mean, lossy);
}
}
void TensorUtil::WriteImage(const std::string &name, int index, float mean,
bool lossy) {
auto image = to_image(tensor_, index, 1.0, mean);
auto filename = name + (lossy ? ".jpg" : ".png");
vector<int> params({CV_IMWRITE_JPEG_QUALITY, 90});
CAFFE_ENFORCE(cv::imwrite(filename, image, params),
"unable to write to " + filename);
}
TensorCPU TensorUtil::ScaleImageTensor(int width, int height) {
auto count = tensor_.dim(0), dim_c = tensor_.dim(1), dim_h = tensor_.dim(2),
dim_w = tensor_.dim(3);
std::vector<float> output;
output.reserve(count * dim_c * height * width);
auto input = tensor_.data<float>();
vector<cv::Mat> channels(dim_c);
for (int i = 0; i < count; i++) {
for (auto &j : channels) {
j = cv::Mat(dim_h, dim_w, CV_32F, (void *)input);
input += (dim_w * dim_h);
}
cv::Mat image;
cv::merge(channels, image);
// image.convertTo(image, CV_8UC3, 1.0, mean);
cv::resize(image, image, cv::Size(width, height));
// image.convertTo(image, CV_32FC3, 1.0, -mean);
cv::split(image, channels);
for (auto &c : channels) {
output.insert(output.end(), (float *)c.datastart, (float *)c.dataend);
}
}
std::vector<TIndex> dims({count, dim_c, height, width});
return TensorCPU(dims, output, NULL);
}
template <typename T>
void image_to_tensor(TensorCPU &tensor, cv::Mat &image, float mean = 128) {
std::vector<T> data;
image.convertTo(image, CV_32FC3, 1.0, -mean);
vector<cv::Mat> channels(3);
cv::split(image, channels);
for (auto &c : channels) {
data.insert(data.end(), (T *)c.datastart, (T *)c.dataend);
}
std::vector<TIndex> dims({1, 3, image.rows, image.cols});
TensorCPU t(dims, data, NULL);
tensor.ResizeLike(t);
tensor.ShareData(t);
}
template <typename T>
void read_image_tensor(TensorCPU &tensor,
const std::vector<std::string> &filenames, int width,
int height, std::vector<int> &indices, float mean,
TensorProto::DataType type) {
std::vector<T> data;
data.reserve(filenames.size() * 3 * width * height);
auto count = 0;
for (auto &filename : filenames) {
// load image
auto image = cv::imread(filename); // CV_8UC3 uchar
// std::cout << "image size: " << image.size() << std::endl;
if (!image.cols || !image.rows) {
count++;
continue;
}
if (image.cols != width || image.rows != height) {
// scale image to fit
cv::Size scaled(std::max(height * image.cols / image.rows, width),
std::max(height, width * image.rows / image.cols));
cv::resize(image, image, scaled);
// std::cout << "scaled size: " << image.size() << std::endl;
// crop image to fit
cv::Rect crop((image.cols - width) / 2, (image.rows - height) / 2, width,
height);
image = image(crop);
// std::cout << "cropped size: " << image.size() << std::endl;
}
switch (type) {
case TensorProto_DataType_FLOAT:
image.convertTo(image, CV_32FC3, 1.0, -mean);
break;
case TensorProto_DataType_INT8:
image.convertTo(image, CV_8SC3, 1.0, -mean);
break;
default:
break;
}
// std::cout << "value range: (" << *std::min_element((T *)image.datastart,
// (T *)image.dataend) << ", " << *std::max_element((T *)image.datastart, (T
// *)image.dataend) << ")" << std::endl;
CAFFE_ENFORCE_EQ(image.channels(), 3);
CAFFE_ENFORCE_EQ(image.rows, height);
CAFFE_ENFORCE_EQ(image.cols, width);
// convert NHWC to NCHW
vector<cv::Mat> channels(3);
cv::split(image, channels);
for (auto &c : channels) {
data.insert(data.end(), (T *)c.datastart, (T *)c.dataend);
}
indices.push_back(count++);
}
// create tensor
std::vector<TIndex> dims({(TIndex)indices.size(), 3, height, width});
TensorCPU t(dims, data, NULL);
tensor.ResizeLike(t);
tensor.ShareData(t);
}
void TensorUtil::ReadImages(const std::vector<std::string> &filenames,
int width, int height, std::vector<int> &indices,
float mean, TensorProto::DataType type) {
switch (type) {
case TensorProto_DataType_FLOAT:
read_image_tensor<float>(tensor_, filenames, width, height, indices, mean,
type);
break;
case TensorProto_DataType_INT8:
read_image_tensor<int8_t>(tensor_, filenames, width, height, indices,
mean, type);
break;
case TensorProto_DataType_UINT8:
read_image_tensor<uint8_t>(tensor_, filenames, width, height, indices,
mean, type);
break;
default:
LOG(FATAL) << "datatype " << type << " not implemented" << std::endl;
}
}
void TensorUtil::ReadImage(const std::string &filename, int width, int height) {
std::vector<int> indices;
ReadImages({filename}, width, height, indices);
}
template <typename T, typename C>
void tensor_print_type(const Tensor<C> &tensor, const std::string &name,
int max) {
const auto &data = tensor.template data<T>();
if (name.length() > 0) std::cout << name << "(" << tensor.dims() << "): ";
for (auto i = 0; i < (tensor.size() > max ? max : tensor.size()); ++i) {
std::cout << (float)data[i] << ' ';
}
if (tensor.size() > max) {
std::cout << "... (" << *std::min_element(data, data + tensor.size()) << ","
<< *std::max_element(data, data + tensor.size()) << ")";
}
if (name.length() > 0) std::cout << std::endl;
}
void TensorUtil::Print(const std::string &name, int max) {
if (tensor_.template IsType<float>()) {
return tensor_print_type<float>(tensor_, name, max);
}
if (tensor_.template IsType<int>()) {
return tensor_print_type<int>(tensor_, name, max);
}
if (tensor_.template IsType<uint8_t>()) {
return tensor_print_type<uint8_t>(tensor_, name, max);
}
if (tensor_.template IsType<int8_t>()) {
return tensor_print_type<int8_t>(tensor_, name, max);
}
std::cout << name << "?" << std::endl;
}
} // namespace caffe2