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
| #include <opencv2/core.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/opencv.hpp> #include <opencv2/highgui.hpp> #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc_c.h> #include <opencv2/dnn.hpp> #include <iostream> #include <onnxruntime_cxx_api.h> #include <assert.h> #include <vector> #include <fstream>
using namespace cv; using namespace std; using namespace Ort; using namespace cv::dnn;
String labels_txt_file = "F:\\Pycharm\\PyCharm_Study\\Others\\c++_learning\\C++_Master\\Onnx\\classification\\classification_classes_ILSVRC2012.txt"; vector<String> readClassNames();
void PreProcess(const Mat& image, Mat& image_blob) { Mat input; image.copyTo(input);
std::vector<Mat> channels, channel_p; split(input, channels); Mat R, G, B; B = channels.at(0); G = channels.at(1); R = channels.at(2);
B = (B / 255. - 0.406) / 0.225; G = (G / 255. - 0.456) / 0.224; R = (R / 255. - 0.485) / 0.229;
channel_p.push_back(R); channel_p.push_back(G); channel_p.push_back(B);
Mat outt; merge(channel_p, outt); image_blob = outt; }
std::vector<String> readClassNames() { std::vector<String> classNames;
std::ifstream fp(labels_txt_file); if (!fp.is_open()) { printf("could not open file...\n"); exit(-1); } std::string name; while (!fp.eof()) { std::getline(fp, name); if (name.length()) classNames.push_back(name); } fp.close(); return classNames; }
int main() {
Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "OnnxModel"); Ort::SessionOptions session_options; session_options.SetIntraOpNumThreads(1); CUDA加速开启(由于onnxruntime的版本太高,无cuda_provider_factory.h的头文件,加速可以使用onnxruntime V1.8的版本) session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL);
#ifdef _WIN32 const wchar_t* model_path = L"F:\\Pycharm\\PyCharm_Study\\Others\\c++_learning\\C++_Master\\Onnx\\classification\\vgg16.onnx"; #else const char* model_path = "F:\\Pycharm\\PyCharm_Study\\Others\\c++_learning\\C++_Master\\Onnx\\classification\\vgg16.onnx"; #endif
printf("Using Onnxruntime C++ API\n"); Ort::Session session(env, model_path, session_options); Ort::AllocatorWithDefaultOptions allocator;
size_t num_input_nodes = session.GetInputCount(); size_t num_output_nodes = session.GetOutputCount(); printf("Number of inputs = %zu\n", num_input_nodes); printf("Number of output = %zu\n", num_output_nodes); const char* input_name = session.GetInputName(0, allocator); std::cout << "input_name:" << input_name << std::endl; const char* output_name = session.GetOutputName(0, allocator); std::cout << "output_name: " << output_name << std::endl; auto input_dims = session.GetInputTypeInfo(0).GetTensorTypeAndShapeInfo().GetShape(); auto output_dims = session.GetOutputTypeInfo(0).GetTensorTypeAndShapeInfo().GetShape(); std::cout << "input_dims:" << input_dims[0] << std::endl; std::cout << "output_dims:" << output_dims[0] << std::endl; std::vector<const char*> input_names{ input_name }; std::vector<const char*> output_names = { output_name }; std::vector<const char*> input_node_names = { "input.1" }; std::vector<const char*> output_node_names = { "70"};
Mat img = imread("F:\\Pycharm\\PyCharm_Study\\Others\\c++_learning\\C++_Master\\Onnx\\classification\\dog.jpg"); Mat det1, det2; resize(img, det1, Size(256, 256), INTER_AREA); det1.convertTo(det1, CV_32FC3); PreProcess(det1, det2); Mat blob = dnn::blobFromImage(det2, 1., Size(224, 224), Scalar(0, 0, 0), false, true); printf("Load success!\n");
clock_t startTime, endTime; auto memory_info = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator, OrtMemType::OrtMemTypeDefault); std::vector<Ort::Value> input_tensors; input_tensors.emplace_back(Ort::Value::CreateTensor<float>(memory_info, blob.ptr<float>(), blob.total(), input_dims.data(), input_dims.size())); startTime = clock();
推理(score model & input tensor, get back output tensor) auto output_tensors = session.Run(Ort::RunOptions{ nullptr }, input_node_names.data(), input_tensors.data(), input_names.size(), output_node_names.data(), output_node_names.size()); endTime = clock(); assert(output_tensors.size() == 1 && output_tensors.front().IsTensor()); 获取输出(Get pointer to output tensor float values) float* floatarr = output_tensors[0].GetTensorMutableData<float>(); Mat newarr = Mat_<double>(1, 1000); for (int i = 0; i < newarr.rows; i++) { for (int j = 0; j < newarr.cols; j++) { newarr.at<double>(i, j) = floatarr[j]; } }
vector<String> labels = readClassNames(); for (int n = 0; n < newarr.rows; n++) { Point classNumber; double classProb; Mat probMat = newarr(Rect(0, n, 1000, 1)).clone(); Mat result = probMat.reshape(1, 1); minMaxLoc(result, NULL, &classProb, NULL, &classNumber); int classidx = classNumber.x; printf("\n current image classification : %s, possible : %.2f\n", labels.at(classidx).c_str(), classProb);
putText(img, labels.at(classidx), Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.6, Scalar(0, 0, 255), 1, 1); imshow("Image Classification", img); waitKey(0); }
计算运行时间 std::cout << "The run time is:" << (double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << std::endl; printf("Done!\n"); system("pause"); return 0; }
|