MNN框架学习(四):tensorflow图像分类模型部署

2023-12-23 10:48

本文主要是介绍MNN框架学习(四):tensorflow图像分类模型部署,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.模型转换

先下载tensorflow的模型,下载地址为:

https://github.com/tensorflow/models/tree/master/research/slim

然后,使用编译好的MNN工具转换模型:

./MNNConvert -f TF --modelFile mobilenet_v1_1.0_224_frozen.pb --MNNModel mobilenet.mnn --bizCode MNN

2、模型部署

主要分为两个步骤:

第一步,初始化步骤,包括读取模型创建解释器,配置调度参数、配置后端参数和创建会话

int Classifier::Init(const char* root_path) {std::cout << "start Init." << std::endl;std::string model_file = std::string(root_path) + "/mobilenet.mnn";// 创建解释器classifier_interpreter_ = std::shared_ptr<MNN::Interpreter>(MNN::Interpreter::createFromFile(model_file.c_str()));if (!classifier_interpreter_ || LoadLabels(root_path) != 0) {std::cout << "load model failed." << std::endl;return 10000;}    // 配置调度MNN::ScheduleConfig schedule_config;schedule_config.type = MNN_FORWARD_CPU;schedule_config.numThread = 1;// 配置后端MNN::BackendConfig backend_config;backend_config.precision = MNN::BackendConfig::Precision_Normal;schedule_config.backendConfig = &backend_config;// 创建会话classifier_sess_ = classifier_interpreter_->createSession(schedule_config);input_tensor_ = classifier_interpreter_->getSessionInput(classifier_sess_, nullptr);classifier_interpreter_->resizeTensor(input_tensor_, {1, 3, inputSize_.height, inputSize_.width});classifier_interpreter_->resizeSession(classifier_sess_);std::cout << "End Init." << std::endl; initialized_ = true;return 0;
}

第二步:数据读入、模型推理和后处理输出

int Classifier::Classify(const cv::Mat& img_src, std::vector<ImageInfo>* images) {std::cout << "start classify." << std::endl;images->clear();if (!initialized_) {std::cout << "model uninitialized." << std::endl;return 10000;}if (img_src.empty()) {std::cout << "input empty." << std::endl;return 10001;}cv::Mat img_resized;cv::resize(img_src.clone(), img_resized, inputSize_);std::shared_ptr<MNN::CV::ImageProcess> pretreat(MNN::CV::ImageProcess::create(MNN::CV::BGR, MNN::CV::RGB, meanVals, 3, normVals, 3));pretreat->convert((uint8_t*)img_resized.data, inputSize_.width, inputSize_.height, img_resized.step[0], input_tensor_);// forwardclassifier_interpreter_->runSession(classifier_sess_);// get output// mobilenet: "classifierV1/Predictions/Reshape_1"MNN::Tensor* output_score = classifier_interpreter_->getSessionOutput(classifier_sess_, nullptr);// copy to hostMNN::Tensor score_host(output_score, output_score->getDimensionType());output_score->copyToHostTensor(&score_host);auto score_ptr = score_host.host<float>();std::vector<std::pair<float, int>> scores;for (int i = 0; i < 1000; ++i) {float score = score_ptr[i];scores.push_back(std::make_pair(score, i));}std::partial_sort(scores.begin(), scores.begin() + topk_, scores.end(), std::greater< std::pair<float, int> >());for (int i = 0; i < topk_; ++i) {ImageInfo image_info;image_info.label_ = labels_[scores[i].second];image_info.score_ = scores[i].first;images->push_back(image_info);}std::cout << "end classify." << std::endl;return 0;
}

具体代码已经上传到github:https://github.com/MirrorYuChen/mnn_example/tree/master/src/classifier

大家觉得有用就给个star,不许白嫖哦~

参考资料:

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

[2] https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB/tree/master/MNN

 

这篇关于MNN框架学习(四):tensorflow图像分类模型部署的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

MySQL 主从复制部署及验证(示例详解)

《MySQL主从复制部署及验证(示例详解)》本文介绍MySQL主从复制部署步骤及学校管理数据库创建脚本,包含表结构设计、示例数据插入和查询语句,用于验证主从同步功能,感兴趣的朋友一起看看吧... 目录mysql 主从复制部署指南部署步骤1.环境准备2. 主服务器配置3. 创建复制用户4. 获取主服务器状态5

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

如何在Ubuntu 24.04上部署Zabbix 7.0对服务器进行监控

《如何在Ubuntu24.04上部署Zabbix7.0对服务器进行监控》在Ubuntu24.04上部署Zabbix7.0监控阿里云ECS服务器,需配置MariaDB数据库、开放10050/1005... 目录软硬件信息部署步骤步骤 1:安装并配置mariadb步骤 2:安装Zabbix 7.0 Server

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

Python中Tensorflow无法调用GPU问题的解决方法

《Python中Tensorflow无法调用GPU问题的解决方法》文章详解如何解决TensorFlow在Windows无法识别GPU的问题,需降级至2.10版本,安装匹配CUDA11.2和cuDNN... 当用以下代码查看GPU数量时,gpuspython返回的是一个空列表,说明tensorflow没有找到

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2

Python的端到端测试框架SeleniumBase使用解读

《Python的端到端测试框架SeleniumBase使用解读》:本文主要介绍Python的端到端测试框架SeleniumBase使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录SeleniumBase详细介绍及用法指南什么是 SeleniumBase?SeleniumBase

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio