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

相关文章

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

详解如何使用Python从零开始构建文本统计模型

《详解如何使用Python从零开始构建文本统计模型》在自然语言处理领域,词汇表构建是文本预处理的关键环节,本文通过Python代码实践,演示如何从原始文本中提取多尺度特征,并通过动态调整机制构建更精确... 目录一、项目背景与核心思想二、核心代码解析1. 数据加载与预处理2. 多尺度字符统计3. 统计结果可

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

C/C++的OpenCV 进行图像梯度提取的几种实现

《C/C++的OpenCV进行图像梯度提取的几种实现》本文主要介绍了C/C++的OpenCV进行图像梯度提取的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录预www.chinasem.cn备知识1. 图像加载与预处理2. Sobel 算子计算 X 和 Y

c/c++的opencv图像金字塔缩放实现

《c/c++的opencv图像金字塔缩放实现》本文主要介绍了c/c++的opencv图像金字塔缩放实现,通过对原始图像进行连续的下采样或上采样操作,生成一系列不同分辨率的图像,具有一定的参考价值,感兴... 目录图像金字塔简介图像下采样 (cv::pyrDown)图像上采样 (cv::pyrUp)C++ O