1、环境:win10+cuda11.4+onnxruntime-gpu1.10+opencv3.4.4+vs2019 c++

GPU:本人使用的NVIDIA GeForce GTX 1650  4GB显存。

cifar-10图像分类onnx模型C++调用_onnx模型

2、模型的训练参考:

https://blog.51cto.com/u_8681773/6163656

3、C++调用测试:

cifar-10图像分类onnx模型C++调用_cifar-10_02

接口形式:

cifar-10图像分类onnx模型C++调用_onnx模型_03

#pragma once
#ifndef _IMAGECLASSIFY_ONNX_INTERFACE_H_
#define _IMAGECLASSIFY_ONNX_INTERFACE_H_
#ifndef  IMAGECLASSIFY_ONNX_EXPORTS
#define IMAGECLASSIFY_ONNX_API  __declspec(dllimport)
#else
#define IMAGECLASSIFY_ONNX_API  __declspec(dllexport)
#endif
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <vector>
#include <stdio.h>
#include <map>
#include <algorithm>
#include<string>
using namespace std;
#ifdef __cplusplus
extern "C" {
#endif
	//初始化图像分类模型
	IMAGECLASSIFY_ONNX_API int Init_ImageClassify(void*& modelHandles, std::string modelpath, const int gpu_id);
	//图像分类
	IMAGECLASSIFY_ONNX_API int ImageClassify(void*& modelHandles, cv::Mat img, int& ClassifyResult, float& score);
	//图像分类,batch,需要模型支持batch识别
	IMAGECLASSIFY_ONNX_API int ImageClassify_Batch(void*& modelHandles, const std::vector<cv::Mat>& imgs, std::vector<int>& ClassifyResult, std::vector<float>& scores);
	//模型释放
	IMAGECLASSIFY_ONNX_API int ImageClassify_Free(void*& modelHandles);
#ifdef __cplusplus
#endif
#endif //_IMAGECLASSIFY_ONNX_INTERFACE_H_

cifar-10图像分类onnx模型C++调用_onnx模型_04

测试结果:(lenet网络效果不太好)

cifar-10图像分类onnx模型C++调用_onnx模型_05

cifar-10图像分类onnx模型C++调用_cifar-10_06

#include "image_classify_interface.h"
#include <Windows.h>
#include <iostream>
#include <ctime>
#define batch_test 0
double getCurrentTime() {
	return (static_cast<double>(cv::getTickCount())) / cv::getTickFrequency() * 1000;//单位毫秒
int main(int argc, char** argv)
	string gpu_id_str = argv[3];
	int gpu_id = atoi(gpu_id_str.c_str());
	void* modelHandles=NULL;
	std::string modelpath = argv[1];
	//初始化CRNN模型
	int initflag = Init_ImageClassify(modelHandles, modelpath, gpu_id);
	std::string image_dir = argv[2];
	cv::String pattern = image_dir + "/*.jpg";
	vector<cv::String> image_list;
	cv::glob(pattern, image_list, false);
	if (image_list.empty())
		cout << "images list is null,please check your image dir !!!" << endl;
		return -1;
#if batch_test
	for (int i = 0; i < image_list.size()-32; i++)
		std::vector<cv::Mat> inputImage;
		for (int j = 0; j < 32; j++)
			std::cout << "[" << i + j << "/" << image_list.size() << "]" << image_list[i + j] << endl;
			cv::Mat inputImage1 = cv::imread(image_list[i + j]);
			inputImage.push_back(inputImage1);
		std::vector<int> recResult;
		std::vector<float> scores;
		double startCrnnTime = getCurrentTime();
		//识别文本
		initflag = ImageClassify_Batch(modelHandles, inputImage, recResult, scores);
		double endCrnnTime = getCurrentTime();
		cout << "recog time=" << endCrnnTime - startCrnnTime << "ms" << endl;
		for (auto& rlt: recResult)
			cout << rlt << endl;
		//Sleep(100);
#else
	for (int i = 0; i < image_list.size(); i++)
		std::cout << "[" << i + 1 << "/" << image_list.size() << "]" << image_list[i] << endl;
		cv::Mat inputImage = cv::imread(image_list[i]);
		int recResult;
		float score;
		double startCrnnTime = getCurrentTime();
		//识别文本
		initflag = ImageClassify(modelHandles, inputImage, recResult, score);
		double endCrnnTime = getCurrentTime();
		cout << "recog time = " << endCrnnTime - startCrnnTime << "ms,recResult = " << recResult<<",score = "<< score << endl;
		//Sleep(100);
#endif
	//模型释放
 	initflag = ImageClassify_Free(modelHandles);
	return 0;
}

图像分类 C++ onnx使用示例工程分享:

链接: https://pan.baidu.com/s/1kjsRhruiEhUpl1CmuMr4zg

提取码:g5kr

自己要学习Onnx的C++推导,可学习以下课程:

https://edu.51cto.com/course/30388.html