MNN学习笔记(五):caffe物体检测模型部署

2023-12-23 10:48

本文主要是介绍MNN学习笔记(五):caffe物体检测模型部署,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.模型转换

首先下载caffe模型,下载地址为:

https://github.com/C-Aniruddh/realtime_object_recognition

然后将caffe模型转换成mnn模型:

./MNNConvert -f CAFFE --modelFile MobileNetSSD_deploy.caffemodel --prototxt MobileNetSSD_deploy.prototxt --MNNModel mobilenetssd.mnn --bizCode MNN

2.模型部署

首先,进行初始化:模型载入并创建解释器,设置调度参数,设置后端参数,创建会话和图像处理参数配置

int MobilenetSSD::Init(const char * root_path) {std::cout << "start Init." << std::endl;std::string model_file = std::string(root_path) + "/mobilenetssd.mnn";mobilenetssd_interpreter_ = std::unique_ptr<MNN::Interpreter>(MNN::Interpreter::createFromFile(model_file.c_str()));if (nullptr == mobilenetssd_interpreter_) {std::cout << "load model failed." << std::endl;return 10000;}MNN::ScheduleConfig schedule_config;schedule_config.type = MNN_FORWARD_CPU;schedule_config.numThread = 4;MNN::BackendConfig backend_config;backend_config.precision = MNN::BackendConfig::Precision_High;backend_config.power = MNN::BackendConfig::Power_High;schedule_config.backendConfig = &backend_config;mobilenetssd_sess_ = mobilenetssd_interpreter_->createSession(schedule_config);// image processerMNN::CV::Matrix trans;trans.setScale(1.0f, 1.0f);MNN::CV::ImageProcess::Config img_config;img_config.filterType = MNN::CV::BICUBIC;::memcpy(img_config.mean, meanVals_, sizeof(meanVals_));::memcpy(img_config.normal, normVals_, sizeof(normVals_));img_config.sourceFormat = MNN::CV::RGBA;img_config.destFormat = MNN::CV::RGB;pretreat_data_ = std::shared_ptr<MNN::CV::ImageProcess>(MNN::CV::ImageProcess::create(img_config));pretreat_data_->setMatrix(trans);std::string input_name = "data";input_tensor_ = mobilenetssd_interpreter_->getSessionInput(mobilenetssd_sess_, input_name.c_str());mobilenetssd_interpreter_->resizeTensor(input_tensor_, dims_);mobilenetssd_interpreter_->resizeSession(mobilenetssd_sess_);initialized_ = true;std::cout << "end Init." << std::endl;return 0;
}

然后,进行数据读入、模型推理和输出结果后处理

这里数据读入参考了资料[3],这里详细介绍了如何使用opencv读入数据,当然不止这一种,还有很多种读取方式

int MobilenetSSD::Detect(const cv::Mat & img_src, std::vector<ObjectInfo>* objects) {std::cout << "start detect." << std::endl;if (!initialized_) {std::cout << "model uninitialized." << std::endl;return 10000;}if (img_src.empty()) {std::cout << "input empty." << std::endl;return 10001;}int width = img_src.cols;int height = img_src.rows;// preprocesscv::Mat img_resized;cv::resize(img_src, img_resized, inputSize_);uint8_t* data_ptr = GetImage(img_resized);pretreat_data_->convert(data_ptr, inputSize_.width, inputSize_.height, 0, input_tensor_);mobilenetssd_interpreter_->runSession(mobilenetssd_sess_);std::string output_name = "detection_out";MNN::Tensor* output_tensor = mobilenetssd_interpreter_->getSessionOutput(mobilenetssd_sess_, output_name.c_str());// copy to hostMNN::Tensor output_host(output_tensor, output_tensor->getDimensionType());output_tensor->copyToHostTensor(&output_host);auto output_ptr = output_host.host<float>();for (int i = 0; i < output_host.height(); ++i) {int index = i * output_host.width();ObjectInfo object;object.name_ = class_names[int(output_ptr[index + 0])];object.score_ = output_ptr[index + 1];object.location_.x = output_ptr[index + 2] * width;object.location_.y = output_ptr[index + 3] * height;object.location_.width = output_ptr[index + 4] * width - object.location_.x;object.location_.height = output_ptr[index + 5] * height - object.location_.y;objects->push_back(object);}std::cout << "end detect." << std::endl;return 0;
}

具体代码已经上传到github:

https://github.com/MirrorYuChen/mnn_example/tree/master/src/object/mobilenetssd

觉得有用的点个star,不许白嫖哈~

参考资料:

[1] https://github.com/alibaba/MNN

[2] https://github.com/lqian/light-LPR

[3] https://blog.csdn.net/abcd740181246/article/details/90143848

这篇关于MNN学习笔记(五):caffe物体检测模型部署的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/527760

相关文章

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

大数据spark3.5安装部署之local模式详解

《大数据spark3.5安装部署之local模式详解》本文介绍了如何在本地模式下安装和配置Spark,并展示了如何使用SparkShell进行基本的数据处理操作,同时,还介绍了如何通过Spark-su... 目录下载上传解压配置jdk解压配置环境变量启动查看交互操作命令行提交应用spark,一个数据处理框架

如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件

《如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件》本文介绍了如何使用Docker部署FTP服务器和Nginx,并通过HTTP访问FTP中的文件,通过将FTP数据目录挂载到N... 目录docker部署FTP和Nginx并通过HTTP访问FTP里的文件1. 部署 FTP 服务器 (

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

Ubuntu 22.04 服务器安装部署(nginx+postgresql)

《Ubuntu22.04服务器安装部署(nginx+postgresql)》Ubuntu22.04LTS是迄今为止最好的Ubuntu版本之一,很多linux的应用服务器都是选择的这个版本... 目录是什么让 Ubuntu 22.04 LTS 变得安全?更新了安全包linux 内核改进一、部署环境二、安装系统

JAVA集成本地部署的DeepSeek的图文教程

《JAVA集成本地部署的DeepSeek的图文教程》本文主要介绍了JAVA集成本地部署的DeepSeek的图文教程,包含配置环境变量及下载DeepSeek-R1模型并启动,具有一定的参考价值,感兴趣的... 目录一、下载部署DeepSeek1.下载ollama2.下载DeepSeek-R1模型并启动 二、J

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

Docker部署Jenkins持续集成(CI)工具的实现

《Docker部署Jenkins持续集成(CI)工具的实现》Jenkins是一个流行的开源自动化工具,广泛应用于持续集成(CI)和持续交付(CD)的环境中,本文介绍了使用Docker部署Jenkins... 目录前言一、准备工作二、设置变量和目录结构三、配置 docker 权限和网络四、启动 Jenkins

SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程

《SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程》本文详细介绍了如何在虚拟机和宝塔面板中安装RabbitMQ,并使用Java代码实现消息的发送和接收,通过异步通讯,可以优化... 目录一、RabbitMQ安装二、启动RabbitMQ三、javascript编写Java代码1、引入

ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法

《ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法》本文介绍了Elasticsearch的基本概念,包括文档和字段、索引和映射,还详细描述了如何通过Docker... 目录1、ElasticSearch概念2、ElasticSearch、Kibana和IK分词器部署