-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUtils.h
69 lines (55 loc) · 2.43 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
#pragma once
#include"NvInfer.h"
#include"NvOnnxParser.h"
#include<iostream>
//#include<string>
#include"NvInferRuntime.h"
#include<vector>
#include<map>
#include<fstream>
#include<iomanip>
#include<assert.h>
#include<numeric>
#include<opencv/highgui.h>
#include<opencv2/opencv.hpp>
//#include<opencv2/gpu/gpu.hpp>
#define CUDA_CHECK(callstr) \
{ \
cudaError_t error_code = callstr; \
if (error_code != cudaSuccess) { \
std::cerr << "CUDA error " << error_code << " at " << __FILE__ << ":" << __LINE__; \
assert(0); \
} \
}
class Logger : public nvinfer1::ILogger
{
public:
Logger(Severity severity = Severity::kWARNING)
: reportableSeverity(severity)
{
}
void log(Severity severity, const char* msg) override
{
// suppress messages with severity enum value greater than the reportable
if (severity > reportableSeverity)
return;
switch (severity)
{
case Severity::kINTERNAL_ERROR: std::cerr << "INTERNAL_ERROR: "; break;
case Severity::kERROR: std::cerr << "ERROR: "; break;
case Severity::kWARNING: std::cerr << "WARNING: "; break;
case Severity::kINFO: std::cout << "INFO: "; break;
default: std::cout << "UNKNOWN: "; break;
}
std::cout << msg << std::endl;
}
Severity reportableSeverity;
};
nvinfer1::ICudaEngine* engineFromFiles(std::string onnxFile,std::string trtFile,nvinfer1::IRuntime* runtime,int batchSize,Logger& logger,bool useInt8,bool markOutput,nvinfer1::IInt8EntropyCalibrator* calibrator);
void prepareImage(cv::Mat& img,float* data,int w,int h,int c,bool cvtColor = true,bool padCenter = true,bool pad = true,bool normalize =true );
float* prepareImage2(cv::Mat& img,cv::Mat& img_pad,float* data,int w,int h,int c,bool cvtColor=true,bool normalize = true);
float* calculate_padding(int orig_width,int orig_height,int new_width,int new_height);
inline int64_t volume(const nvinfer1::Dims& d )
{
return std::accumulate(d.d,d.d+d.nbDims,1,std::multiplies<int64_t>());
}