forked from NVIDIA-AI-IOT/tf_to_trt_image_classification
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.h
119 lines (100 loc) · 2.95 KB
/
utils.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
/**
* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
* Full license terms provided in LICENSE.md file.
*/
#ifndef TRT_IMAGE_CLASSIFICATION_UTILS_H
#define TRT_IMAGE_CLASSIFICATION_UTILS_H
#include <opencv2/opencv.hpp>
#include <vector>
#include <algorithm>
#include <NvInfer.h>
void cvImageToTensor(const cv::Mat & image, float *tensor, nvinfer1::Dims dimensions)
{
const size_t channels = dimensions.d[0];
const size_t height = dimensions.d[1];
const size_t width = dimensions.d[2];
// TODO: validate dimensions match
const size_t stridesCv[3] = { width * channels, channels, 1 };
const size_t strides[3] = { height * width, width, 1 };
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
for (int k = 0; k < channels; k++)
{
const size_t offsetCv = i * stridesCv[0] + j * stridesCv[1] + k * stridesCv[2];
const size_t offset = k * strides[0] + i * strides[1] + j * strides[2];
tensor[offset] = (float) image.data[offsetCv];
}
}
}
}
void preprocessVgg(float *tensor, nvinfer1::Dims dimensions)
{
size_t channels = dimensions.d[0];
size_t height = dimensions.d[1];
size_t width = dimensions.d[2];
const size_t strides[3] = { height * width, width, 1 };
const float mean[3] = { 123.68, 116.78, 103.94 }; // values from TensorFlow slim models code
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
for (int k = 0; k < channels; k++)
{
const size_t offset = k * strides[0] + i * strides[1] + j * strides[2];
tensor[offset] -= mean[k];
}
}
}
}
void preprocessInception(float *tensor, nvinfer1::Dims dimensions)
{
size_t channels = dimensions.d[0];
size_t height = dimensions.d[1];
size_t width = dimensions.d[2];
const size_t numel = channels * height * width;
for (int i = 0; i < numel; i++)
tensor[i] = 2.0 * (tensor[i] / 255.0 - 0.5); // values from TensorFlow slim models code
}
int argmax(float *tensor, nvinfer1::Dims dimensions)
{
size_t channels = dimensions.d[0];
size_t height = dimensions.d[1];
size_t width = dimensions.d[2];
size_t numel = channels * height * width;
if (numel <= 0)
return 0;
size_t maxIndex = 0;
float max = tensor[0];
for (int i = 0; i < numel; i++)
{
if (tensor[i] > max)
{
maxIndex = i;
max = tensor[i];
}
}
return maxIndex;
}
size_t numTensorElements(nvinfer1::Dims dimensions)
{
if (dimensions.nbDims == 0)
return 0;
size_t size = 1;
for (int i = 0; i < dimensions.nbDims; i++)
size *= dimensions.d[i];
return size;
}
std::vector<size_t> argsort(float *tensor, nvinfer1::Dims dimensions)
{
size_t numel = numTensorElements(dimensions);
std::vector<size_t> indices(numel);
for (int i = 0; i < numel; i++)
indices[i] = i;
std::sort(indices.begin(), indices.begin() + numel, [tensor](size_t idx1, size_t idx2) {
return tensor[idx1] > tensor[idx2];
});
return indices;
}
#endif