当前位置: 首页 > news >正文

TensorFlow系列:第五讲:移动端部署模型

项目地址:https://github.com/LionJackson/imageClassification
Flutter项目地址:https://github.com/LionJackson/flutter_image

一. 模型转换

编写tflite模型工具类:

import osimport PIL
import tensorflow as tf
import keras
import numpy as np
from PIL.Image import Image
from matplotlib import pyplot as pltfrom utils.dataset_loader import DatasetLoader
from utils.utils import Utils"""
tflite模型工具类
"""class TFLiteUtil:def __init__(self, saved_model_dir, path_url):self.save_model_dir = saved_model_dirself.path_url = path_url# 训练的模型生成标签列表def get_folder_names(self):folder_names = []for root, dirs, files in os.walk(self.path_url + '/train'):for dir_name in dirs:folder_names.append(dir_name)with open(self.save_model_dir + '.label', 'w') as file:for name in folder_names:file.write(name + '\n')return folder_names# 模型转成tflite格式def convert_tflite(self):self.get_folder_names()converter = tf.lite.TFLiteConverter.from_saved_model(self.save_model_dir)tflite_model = converter.convert()# 将转换后的 TFLite 模型保存为文件with open(self.save_model_dir + '.tflite', 'wb') as f:f.write(tflite_model)print("转换成功,已保存为 tflite")# 加载keras并转成tflitedef convert_model_tflite(self):self.get_folder_names()model = keras.models.load_model(self.save_model_dir + ".keras")converter = tf.lite.TFLiteConverter.from_keras_model(model)converter.optimizations = [tf.lite.Optimize.DEFAULT]converter.target_spec.supported_types = [tf.float16]tflite_model = converter.convert()# 将转换后的 TFLite 模型保存为文件with open(self.save_model_dir + '.tflite', 'wb') as f:f.write(tflite_model)print("转换成功(model),已保存为 tflite")# 批量识别 进行可视化显示def batch_evaluation(self, class_mode='categorical', image_size=(224, 224), num_images=25):dataset_loader = DatasetLoader(self.path_url, image_size=image_size, class_mode=class_mode)train_ds, val_ds, test_ds, class_names = dataset_loader.load_data()interpreter = tf.lite.Interpreter(self.save_model_dir + '.tflite')interpreter.allocate_tensors()# 获取输入和输出张量的信息input_details = interpreter.get_input_details()output_details = interpreter.get_output_details()plt.figure(figsize=(10, 10))for images, labels in test_ds.take(1):outputs = []for img in images:img_expanded = np.expand_dims(img, axis=0)interpreter.set_tensor(input_details[0]['index'], img_expanded)interpreter.invoke()output = interpreter.get_tensor(output_details[0]['index'])outputs.append(output)for i in range(num_images):plt.subplot(5, 5, i + 1)image = np.array(images[i]).astype("uint8")plt.imshow(image)index = int(np.argmax(outputs[i]))prediction = outputs[i][0][index]percentage_str = "{:.2f}%".format(prediction * 100)plt.title(f"{class_names[index]}: {percentage_str}")plt.axis("off")plt.subplots_adjust(hspace=0.5, wspace=0.5)plt.show()# 查看tflite模型信息def tflite_analyzer(self):# 加载 TFLite 模型interpreter = tf.lite.Interpreter(model_path=self.save_model_dir + '.tflite')interpreter.allocate_tensors()# 获取输入和输出的详细信息input_details = interpreter.get_input_details()output_details = interpreter.get_output_details()# 打印输入和输出的详细信息print("Input Details:")for detail in input_details:print(detail)print("\nOutput Details:")for detail in output_details:print(detail)# 列出所有使用的算子tensor_details = interpreter.get_tensor_details()print("\nTensor Details:")for tensor_detail in tensor_details:print("Index:", tensor_detail['index'])print("Name:", tensor_detail['name'])print("Shape:", tensor_detail['shape'])print("Shape Signature:", tensor_detail['shape_signature'])print("dtype:", tensor_detail['dtype'])print("Quantization:", tensor_detail['quantization'])print("Quantization Parameters:", tensor_detail['quantization_parameters'])print("Sparsity Parameters:", tensor_detail['sparsity_parameters'])print()

引用工具类:

if __name__ == '__main__':# train()# model_util = ModelUtil(SAVED_MODEL_DIR, PATH_URL)# model_util.batch_evaluation()tflite_util = TFLiteUtil(SAVED_MODEL_DIR, PATH_URL)tflite_util.convert_tflite()tflite_util.tflite_analyzer()tflite_util.batch_evaluation()

此时会生成tflite模型文件:

在这里插入图片描述

二. 使用模型

创建flutter项目,引入以下库:

  image: ^4.0.17path: ^1.8.3path_provider: ^2.0.15image_picker: ^0.8.8tflite_flutter: ^0.10.4camera: ^0.10.5+2

把模型文件拷贝到项目中:

在这里插入图片描述
核心代码:

import 'dart:developer';
import 'dart:io';
import 'dart:isolate';import 'package:camera/camera.dart';
import 'package:flutter/services.dart';
import 'package:image/image.dart';
import 'package:tflite_flutter/tflite_flutter.dart';import 'isolate_inference.dart';class ImageClassificationHelper {static const modelPath = 'assets/models/fruits.tflite';static const labelsPath = 'assets/models/fruits.label';late final Interpreter interpreter;late final List<String> labels;late final IsolateInference isolateInference;late Tensor inputTensor;late Tensor outputTensor;// Load modelFuture<void> _loadModel() async {final options = InterpreterOptions();// Use XNNPACK Delegateif (Platform.isAndroid) {options.addDelegate(XNNPackDelegate());}// Use GPU Delegate// doesn't work on emulator// if (Platform.isAndroid) {//   options.addDelegate(GpuDelegateV2());// }// Use Metal Delegateif (Platform.isIOS) {options.addDelegate(GpuDelegate());}// Load model from assetsinterpreter = await Interpreter.fromAsset(modelPath, options: options);// Get tensor input shape [1, 224, 224, 3]inputTensor = interpreter.getInputTensors().first;// Get tensor output shape [1, 1001]outputTensor = interpreter.getOutputTensors().first;log('Interpreter loaded successfully');}// Load labels from assetsFuture<void> _loadLabels() async {final labelTxt = await rootBundle.loadString(labelsPath);labels = labelTxt.split('\n');}Future<void> initHelper() async {_loadLabels();_loadModel();isolateInference = IsolateInference();await isolateInference.start();}Future<Map<String, double>> _inference(InferenceModel inferenceModel) async {ReceivePort responsePort = ReceivePort();isolateInference.sendPort.send(inferenceModel..responsePort = responsePort.sendPort);// get inference result.var results = await responsePort.first;return results;}// inference camera frameFuture<Map<String, double>> inferenceCameraFrame(CameraImage cameraImage) async {var isolateModel = InferenceModel(cameraImage, null, interpreter.address,labels, inputTensor.shape, outputTensor.shape);return _inference(isolateModel);}// inference still imageFuture<Map<String, double>> inferenceImage(Image image) async {var isolateModel = InferenceModel(null, image, interpreter.address, labels,inputTensor.shape, outputTensor.shape);return _inference(isolateModel);}Future<void> close() async {isolateInference.close();}
}

页面部分:

在这里插入图片描述

相关文章:

TensorFlow系列:第五讲:移动端部署模型

项目地址&#xff1a;https://github.com/LionJackson/imageClassification Flutter项目地址&#xff1a;https://github.com/LionJackson/flutter_image 一. 模型转换 编写tflite模型工具类&#xff1a; import osimport PIL import tensorflow as tf import keras import …...

深度学习DeepLearning二元分类 学习笔记

文章目录 类别区分变量与概念逻辑回归Sigmoid函数公式决策边逻辑损失函数和代价函数逻辑回归的梯度下降泛化过拟合的解决方案正则化 类别区分 变量与概念 决策边置信度阈值threshold过拟合欠拟合正则化高偏差lambda&#xff08;λ&#xff09; 线性回归受个别极端值影响&…...

Eureka 介绍与使用

Eureka 是一个开源的服务发现框架&#xff0c;它主要用于在分布式系统中管理和发现服务实例。它由 Netflix 开发并开源&#xff0c;是 Netflix OSS 中的一部分。 使用 Eureka 可以方便地将新的服务实例注册到 Eureka 服务器&#xff0c;并且让其他服务通过 Eureka 服务器来发现…...

Java异常体系、UncaughtExceptionHandler、Spring MVC统一异常处理、Spring Boot统一异常处理

概述 所有异常都是继承自java.lang.Throwable类&#xff0c;Throwable有两个直接子类&#xff0c;Error和Exception。 Error用来表示程序底层或硬件有关的错误&#xff0c;这种错误和程序本身无关&#xff0c;如常见的NoClassDefFoundError。这种异常和程序本身无关&#xff0…...

bash终端快捷键

快捷键作用ShiftCtrlC复制ShiftCtrlV粘贴CtrlAltT新建终端ShiftPgUp/PgDn终端上下翻页滚动CtrlC终止命令CtrlD关闭终端CtrlA光标移动到最开始为止CtrlE光标移动到最末尾CtrlK删除此处到末尾的所有内容CtrlU删除此处至开始的所有内容CtrlD删除当前字符CtrlH删除当前字符的前一个…...

【Visual Studio】Visual Studio报错合集及解决办法

目录 Visual Studio报错:error LNK2001 Visual Studio报错:error C2061 Visual Studio报错:error C1075 Visual Studio报错:error C4430 Visual Studio报错error C3867 概述 持续更细Visual Studio报错及解决方法 Visual Studio报错:error LNK2001 问题 : error LNK2001…...

【微信小程序知识点】转发功能的实现

转发功能&#xff0c;主要帮助用户更流畅地与好友分享内容与服务。 想实现转发功能&#xff0c;有两种方式&#xff1a; 1.页面js文件必须声明onShareAppMessage事件监听函数&#xff0c;并自定义转发内容。只有定义了此事件处理函数&#xff0c;右上角菜单才会显示“转发”按…...

用python识别二维码(python实例二十三)

目录 1.认识Python 2.环境与工具 2.1 python环境 2.2 Visual Studio Code编译 3.识别二维码 3.1 代码构思 3.2 代码实例 3.3 运行结果 4.总结 1.认识Python Python 是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。 Python 的设计具有很强的可读性&…...

电脑文件夹怎么设置密码?让你的文件更安全!

在日常使用电脑的过程中&#xff0c;我们常常会有一些需要保护的个人文件或资料。为了防止这些文件被他人未经授权访问&#xff0c;对重要文件夹设置密码是一种有效的保护措施&#xff0c;可是电脑文件夹怎么设置密码呢&#xff1f;本文将介绍2种简单有效的方法帮助您为电脑文件…...

paddla模型转gguf

在使用ollama配置本地模型时&#xff0c;只支持gguf格式的模型&#xff0c;所以我们首先需要把自己的模型转化为bin格式&#xff0c;本文为paddle&#xff0c;onnx&#xff0c;pytorch格式的模型提供说明&#xff0c;safetensors格式比较简单请参考官方文档&#xff0c;或其它教…...

Memcached vs Redis——Java项目缓存选择

在Java项目开发中&#xff0c;缓存系统作为提升性能、优化资源利用的关键技术之一&#xff0c;扮演着至关重要的角色。Memcached和Redis作为两种流行的缓存解决方案&#xff0c;各有其独特的优势和应用场景。本文旨在通过分析项目大小、用户访问量、业务复杂度以及服务器部署情…...

大模型最新黑书:基于GPT-3、ChatGPT、GPT-4等Transformer架构的自然语言处理 PDF

今天给大家推荐一本丹尼斯罗斯曼(Denis Rothman)编写的关于大语言模型&#xff08;LLM&#xff09;权威教程<<大模型应用解决方案> 基于GPT-3、ChatGPT、GPT-4等Transformer架构的自然语言处理>&#xff01;Google工程总监Antonio Gulli作序&#xff0c;这含金量不…...

【电子数据取证】电子数据司法鉴定

文章关键词&#xff1a;电子数据取证、司法鉴定服务、司法鉴定流程 一、定义 什么是司法鉴定&#xff1f; 在诉讼活动中鉴定人运用科学技术或者专业知识对诉讼涉及的专门性问题进行鉴别和判断并提供鉴定意见的活动。 电子数据司法鉴定 那么电子数据司法鉴定&#xff0c;就…...

使用 OpenCV 的 inRange 函数进行颜色分割

使用 OpenCV 的 inRange 函数进行颜色分割 在图像处理领域&#xff0c;颜色分割是一个常见的任务&#xff0c;常用于识别和提取图像中的特定颜色区域。OpenCV 提供了一个非常方便的函数 inRange 来实现这一功能。在这篇博客中&#xff0c;我们将详细介绍 inRange 函数的用法&a…...

OpenAI终止对中国提供API服务,对国内AI市场产生重大冲击?

6月25日&#xff0c;OpenAI突然宣布终止向包括中国在内的国家地区提供API服务&#xff0c;本月9日这一政策已经正式生效了&#xff01; 有人说&#xff0c;这个事件给中国AI行业带来很大冲击&#xff01;是这样吗&#xff1f;在展开讨论前&#xff0c;我们先来看看什么是API服务…...

JavaDS —— 栈 Stack 和 队列 Queue

栈的概念 栈是一种先进后出的线性表&#xff0c;只允许在固定的一端进行插入和删除操作。 进行插入和删除操作的一端被称为栈顶&#xff0c;另一端被称为栈底 栈的插入操作叫做进栈/压栈/入栈 栈的删除操作叫做出栈 现实生活中栈的例子&#xff1a; 栈的模拟实现 下面是Jav…...

C++进阶:继承和多态

文章目录 ❤️继承&#x1fa77;继承与友元&#x1f9e1;继承和静态成员&#x1f49b;菱形继承及菱形虚拟继承&#x1f49a;继承和组合 ❤️多态&#x1fa77;什么是多态&#xff1f;&#x1f9e1;多态的定义以及实现&#x1f49b;虚函数&#x1f49a;虚函数的重写&#x1f499…...

【八大排序】java版(上)(冒泡、快排、堆排、选择排序)

文章目录 一、冒泡排序(重点)思路代码 二、快排(面试重点)思路代码 三、堆排序(面试重点)思路代码 四、选择排序思路代码 一、冒泡排序(重点) 思路 前后两两数据进行比较&#xff0c;小的数据往前走&#xff0c;大的数据往后走&#xff0c;每一轮结束之后&#xff0c;最大的数…...

.Net Core 微服务之Consul(二)-集群搭建

引言: 集合上一期.Net Core 微服务之Consul(一)(.Net Core 微服务之Consul(一)-CSDN博客) 。 目录 一、 Consul集群搭建 1. 高可用 1.1 高可用性概念 1.2 高可用集群的基本原理 1.3 高可用集群的架构设计 1.3.1 主从复制架构 1.3.2 共享存储架构 1.3.3 负载均衡…...

C++ --> 类和对象(二)

前言 在前面简单的介绍了OOP&#xff0c;什么是类&#xff0c;在类中的this指针。接下来就深入理解类和对象。 默认成员函数 默认构造函数&#xff1a;用于在创建对象时初始化对象的成员变量。默认拷贝构造函数&#xff1a;用于使用已存在的对象来初始化新创建的对象。默认析构…...

linux之kylin系统nginx的安装

一、nginx的作用 1.可做高性能的web服务器 直接处理静态资源&#xff08;HTML/CSS/图片等&#xff09;&#xff0c;响应速度远超传统服务器类似apache支持高并发连接 2.反向代理服务器 隐藏后端服务器IP地址&#xff0c;提高安全性 3.负载均衡服务器 支持多种策略分发流量…...

FastAPI 教程:从入门到实践

FastAPI 是一个现代、快速&#xff08;高性能&#xff09;的 Web 框架&#xff0c;用于构建 API&#xff0c;支持 Python 3.6。它基于标准 Python 类型提示&#xff0c;易于学习且功能强大。以下是一个完整的 FastAPI 入门教程&#xff0c;涵盖从环境搭建到创建并运行一个简单的…...

STM32F4基本定时器使用和原理详解

STM32F4基本定时器使用和原理详解 前言如何确定定时器挂载在哪条时钟线上配置及使用方法参数配置PrescalerCounter ModeCounter Periodauto-reload preloadTrigger Event Selection 中断配置生成的代码及使用方法初始化代码基本定时器触发DCA或者ADC的代码讲解中断代码定时启动…...

镜像里切换为普通用户

如果你登录远程虚拟机默认就是 root 用户&#xff0c;但你不希望用 root 权限运行 ns-3&#xff08;这是对的&#xff0c;ns3 工具会拒绝 root&#xff09;&#xff0c;你可以按以下方法创建一个 非 root 用户账号 并切换到它运行 ns-3。 一次性解决方案&#xff1a;创建非 roo…...

2025 后端自学UNIAPP【项目实战:旅游项目】6、我的收藏页面

代码框架视图 1、先添加一个获取收藏景点的列表请求 【在文件my_api.js文件中添加】 // 引入公共的请求封装 import http from ./my_http.js// 登录接口&#xff08;适配服务端返回 Token&#xff09; export const login async (code, avatar) > {const res await http…...

【Web 进阶篇】优雅的接口设计:统一响应、全局异常处理与参数校验

系列回顾&#xff1a; 在上一篇中&#xff0c;我们成功地为应用集成了数据库&#xff0c;并使用 Spring Data JPA 实现了基本的 CRUD API。我们的应用现在能“记忆”数据了&#xff01;但是&#xff0c;如果你仔细审视那些 API&#xff0c;会发现它们还很“粗糙”&#xff1a;有…...

BCS 2025|百度副总裁陈洋:智能体在安全领域的应用实践

6月5日&#xff0c;2025全球数字经济大会数字安全主论坛暨北京网络安全大会在国家会议中心隆重开幕。百度副总裁陈洋受邀出席&#xff0c;并作《智能体在安全领域的应用实践》主题演讲&#xff0c;分享了在智能体在安全领域的突破性实践。他指出&#xff0c;百度通过将安全能力…...

学校时钟系统,标准考场时钟系统,AI亮相2025高考,赛思时钟系统为教育公平筑起“精准防线”

2025年#高考 将在近日拉开帷幕&#xff0c;#AI 监考一度冲上热搜。当AI深度融入高考&#xff0c;#时间同步 不再是辅助功能&#xff0c;而是决定AI监考系统成败的“生命线”。 AI亮相2025高考&#xff0c;40种异常行为0.5秒精准识别 2025年高考即将拉开帷幕&#xff0c;江西、…...

C#中的CLR属性、依赖属性与附加属性

CLR属性的主要特征 封装性&#xff1a; 隐藏字段的实现细节 提供对字段的受控访问 访问控制&#xff1a; 可单独设置get/set访问器的可见性 可创建只读或只写属性 计算属性&#xff1a; 可以在getter中执行计算逻辑 不需要直接对应一个字段 验证逻辑&#xff1a; 可以…...

MySQL 索引底层结构揭秘:B-Tree 与 B+Tree 的区别与应用

文章目录 一、背景知识&#xff1a;什么是 B-Tree 和 BTree&#xff1f; B-Tree&#xff08;平衡多路查找树&#xff09; BTree&#xff08;B-Tree 的变种&#xff09; 二、结构对比&#xff1a;一张图看懂 三、为什么 MySQL InnoDB 选择 BTree&#xff1f; 1. 范围查询更快 2…...