首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >创建包含元数据的Tflite模型(用于对象检测)中的问题

创建包含元数据的Tflite模型(用于对象检测)中的问题
EN

Stack Overflow用户
提问于 2020-09-27 22:38:21
回答 1查看 4.6K关注 0票数 1

我试图在Android上运行一个tflite模型来检测对象。同样的,

  1. 我已经成功地用我的图像集训练了模型如下:

(a)培训:

代码语言:javascript
运行
AI代码解释
复制
!python3 object_detection/model_main.py \
--pipeline_config_path=/content/drive/My\ Drive/Detecto\ Tutorial/models/research/object_detection/samples/configs/ssd_mobilenet_v2_coco.config \
--model_dir=training/

(修改配置文件以指向提到我的特定TFrecords的位置)

(b)导出推理图

代码语言:javascript
运行
AI代码解释
复制
!python /content/drive/'My Drive'/'Detecto Tutorial'/models/research/object_detection/export_inference_graph.py \
--input_type=image_tensor \
--pipeline_config_path=/content/drive/My\ Drive/Detecto\ Tutorial/models/research/object_detection/samples/configs/ssd_mobilenet_v2_coco.config \
--output_directory={output_directory} \
--trained_checkpoint_prefix={last_model_path}

(c)创建tflite就绪图

代码语言:javascript
运行
AI代码解释
复制
!python /content/drive/'My Drive'/'Detecto Tutorial'/models/research/object_detection/export_tflite_ssd_graph.py \
  --pipeline_config_path=/content/drive/My\ Drive/Detecto\ Tutorial/models/research/object_detection/samples/configs/ssd_mobilenet_v2_coco.config \
  --output_directory={output_directory} \
  --trained_checkpoint_prefix={last_model_path} \
  --add_postprocessing_op=true
  1. 我从图形文件中使用tflite_convert创建了一个tflite模型,如下所示 !tflite_convert -图_def_file=/content/驱动器/My\ Drive/Detecto\ Tutorial/models/research/fine_tuned_model/tflite_graph.pb -输出_file=/content/驱动器/My\ Drive/Detecto\ Tutorial/models/research/fine_tuned_model/detect3.tflite -输出格式=TFLITE -输入形状=1,300,300,3 -输入阵列=归一化输入图像张量 --output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3'--inference_type=FLOAT--allow_custom_ops

上面的tflite模型是独立验证的,运行良好(在Android之外)。

现在需要用元数据填充tflite模型,以便可以在下面提供的示例Android代码中处理tflite模型(否则我会收到错误:不是一个有效的Zip文件,并且在Android上运行时没有关联文件)。

检测/android/README.md

作为同一链接的一部分提供的示例.TFlite使用元数据填充,运行良好。

当我尝试使用以下链接时:示例

代码语言:javascript
运行
AI代码解释
复制
populator = _metadata.MetadataPopulator.with_model_file('/content/drive/My Drive/Detecto Tutorial/models/research/fine_tuned_model/detect3.tflite')
populator.load_metadata_buffer(metadata_buf)
populator.load_associated_files(['/content/drive/My Drive/Detecto Tutorial/models/research/fine_tuned_model/labelmap.txt'])
populator.populate()

要添加元数据(代码的其余部分实际上与将元描述更改为对象检测(而不是图像分类和指定labelmap.txt的位置)相同,它会给出以下错误:

代码语言:javascript
运行
AI代码解释
复制
<ipython-input-6-173fc798ea6e> in <module>()
  1 populator = _metadata.MetadataPopulator.with_model_file('/content/drive/My Drive/Detecto Tutorial/models/research/fine_tuned_model/detect3.tflite')
  ----> 2 populator.load_metadata_buffer(metadata_buf)
        3 populator.load_associated_files(['/content/drive/My Drive/Detecto Tutorial/models/research/fine_tuned_model/labelmap.txt'])
        4 populator.populate()

1 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_lite_support/metadata/metadata.py in _validate_metadata(self, metadata_buf)
    540           "The number of output tensors ({0}) should match the number of "
    541           "output tensor metadata ({1})".format(num_output_tensors,
--> 542                                                 num_output_meta))
    543 
    544 

ValueError: The number of output tensors (4) should match the number of output tensor metadata (1)

这4个输出张量是步骤2中提到的output_arrays中提到的那些张量(可能有人会在那里纠正我)。我不知道如何相应地更新输出张量元数据。

最近使用自定义模型(然后在Android上应用)使用对象检测的人能帮忙吗?或者帮助理解如何将张量元数据更新为4而不是1。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-22 19:36:41

更新于2021年6月10日:

请参阅关于元数据写入器库的最新教程 on tensorflow.org。

更新

元数据写入库已经发布。它目前支持图像分类器和目标检测器,更多支持的任务正在进行中。

下面是一个为对象检测器模型编写元数据的示例:

  1. 安装TFLite支持夜间Pypi包:
代码语言:javascript
运行
AI代码解释
复制
pip install tflite_support_nightly
  1. 使用以下脚本将元数据写入模型:
代码语言:javascript
运行
AI代码解释
复制
from tflite_support.metadata_writers import object_detector
from tflite_support.metadata_writers import writer_utils
from tflite_support import metadata

ObjectDetectorWriter = object_detector.MetadataWriter
_MODEL_PATH = "ssd_mobilenet_v1_1_default_1.tflite"
_LABEL_FILE = "labelmap.txt"
_SAVE_TO_PATH = "ssd_mobilenet_v1_1_default_1_metadata.tflite"

writer = ObjectDetectorWriter.create_for_inference(
    writer_utils.load_file(_MODEL_PATH), [127.5], [127.5], [_LABEL_FILE])
writer_utils.save_file(writer.populate(), _SAVE_TO_PATH)

# Verify the populated metadata and associated files.
displayer = metadata.MetadataDisplayer.with_model_file(_SAVE_TO_PATH)
print("Metadata populated:")
print(displayer.get_metadata_json())
print("Associated file(s) populated:")
print(displayer.get_packed_associated_file_list())

这里有一个代码片段,您可以使用它来填充对象检测模型的元数据,它与TFLite安卓应用程序兼容。

代码语言:javascript
运行
AI代码解释
复制
model_meta = _metadata_fb.ModelMetadataT()
model_meta.name = "SSD_Detector"
model_meta.description = (
    "Identify which of a known set of objects might be present and provide "
    "information about their positions within the given image or a video "
    "stream.")

# Creates input info.
input_meta = _metadata_fb.TensorMetadataT()
input_meta.name = "image"
input_meta.content = _metadata_fb.ContentT()
input_meta.content.contentProperties = _metadata_fb.ImagePropertiesT()
input_meta.content.contentProperties.colorSpace = (
    _metadata_fb.ColorSpaceType.RGB)
input_meta.content.contentPropertiesType = (
    _metadata_fb.ContentProperties.ImageProperties)
input_normalization = _metadata_fb.ProcessUnitT()
input_normalization.optionsType = (
    _metadata_fb.ProcessUnitOptions.NormalizationOptions)
input_normalization.options = _metadata_fb.NormalizationOptionsT()
input_normalization.options.mean = [127.5]
input_normalization.options.std = [127.5]
input_meta.processUnits = [input_normalization]
input_stats = _metadata_fb.StatsT()
input_stats.max = [255]
input_stats.min = [0]
input_meta.stats = input_stats

# Creates outputs info.
output_location_meta = _metadata_fb.TensorMetadataT()
output_location_meta.name = "location"
output_location_meta.description = "The locations of the detected boxes."
output_location_meta.content = _metadata_fb.ContentT()
output_location_meta.content.contentPropertiesType = (
    _metadata_fb.ContentProperties.BoundingBoxProperties)
output_location_meta.content.contentProperties = (
    _metadata_fb.BoundingBoxPropertiesT())
output_location_meta.content.contentProperties.index = [1, 0, 3, 2]
output_location_meta.content.contentProperties.type = (
    _metadata_fb.BoundingBoxType.BOUNDARIES)
output_location_meta.content.contentProperties.coordinateType = (
    _metadata_fb.CoordinateType.RATIO)
output_location_meta.content.range = _metadata_fb.ValueRangeT()
output_location_meta.content.range.min = 2
output_location_meta.content.range.max = 2

output_class_meta = _metadata_fb.TensorMetadataT()
output_class_meta.name = "category"
output_class_meta.description = "The categories of the detected boxes."
output_class_meta.content = _metadata_fb.ContentT()
output_class_meta.content.contentPropertiesType = (
    _metadata_fb.ContentProperties.FeatureProperties)
output_class_meta.content.contentProperties = (
    _metadata_fb.FeaturePropertiesT())
output_class_meta.content.range = _metadata_fb.ValueRangeT()
output_class_meta.content.range.min = 2
output_class_meta.content.range.max = 2
label_file = _metadata_fb.AssociatedFileT()
label_file.name = os.path.basename("label.txt")
label_file.description = "Label of objects that this model can recognize."
label_file.type = _metadata_fb.AssociatedFileType.TENSOR_VALUE_LABELS
output_class_meta.associatedFiles = [label_file]

output_score_meta = _metadata_fb.TensorMetadataT()
output_score_meta.name = "score"
output_score_meta.description = "The scores of the detected boxes."
output_score_meta.content = _metadata_fb.ContentT()
output_score_meta.content.contentPropertiesType = (
    _metadata_fb.ContentProperties.FeatureProperties)
output_score_meta.content.contentProperties = (
    _metadata_fb.FeaturePropertiesT())
output_score_meta.content.range = _metadata_fb.ValueRangeT()
output_score_meta.content.range.min = 2
output_score_meta.content.range.max = 2

output_number_meta = _metadata_fb.TensorMetadataT()
output_number_meta.name = "number of detections"
output_number_meta.description = "The number of the detected boxes."
output_number_meta.content = _metadata_fb.ContentT()
output_number_meta.content.contentPropertiesType = (
    _metadata_fb.ContentProperties.FeatureProperties)
output_number_meta.content.contentProperties = (
    _metadata_fb.FeaturePropertiesT())

# Creates subgraph info.
group = _metadata_fb.TensorGroupT()
group.name = "detection result"
group.tensorNames = [
    output_location_meta.name, output_class_meta.name,
    output_score_meta.name
]
subgraph = _metadata_fb.SubGraphMetadataT()
subgraph.inputTensorMetadata = [input_meta]
subgraph.outputTensorMetadata = [
    output_location_meta, output_class_meta, output_score_meta,
    output_number_meta
]
subgraph.outputTensorGroups = [group]
model_meta.subgraphMetadata = [subgraph]

b = flatbuffers.Builder(0)
b.Finish(
    model_meta.Pack(b),
    _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER)
self.metadata_buf = b.Output()
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64097085

复制
相关文章
DiffusionDet:用于对象检测的扩散模型
Shoufa Chen1, Peize Sun1, Yibing Song2, Ping Luo1 1The University of Hong Kong 2Tencent AI Lab {sfchen, pzsun, pluo}@cs.hku.hk yibingsong.cv@gmail.com
3D视觉工坊
2023/04/29
1.1K0
DiffusionDet:用于对象检测的扩散模型
干货 | 详解对象检测模型中的Anchors
今天,我将讨论在物体检测器中引入的一个优雅的概念 —— Anchors,它是如何帮助检测图像中的物体,以及它们与传统的两阶段检测器中的Anchor有何不同。
小白学视觉
2022/02/12
6760
干货 | 详解对象检测模型中的Anchors
Spring用于创建对象的注解@Autowired @Qualifier @Resource
先通过数据类型IAccountDao圈定出来匹配的几个对象 再通过变量名称作为bean的id accountDao查找和他一样的 都不一样就报错
韦恩少爷的背
2020/03/09
1.2K0
构建对象检测模型
我喜欢深度学习。坦率地说,这是一个有大量技术和框架可供倾注和学习的广阔领域。当我看到现实世界中的应用程序,如面部识别和板球跟踪等时,建立深度学习和计算机视觉模型的真正兴奋就来了。
磐创AI
2020/07/24
1.2K0
构建对象检测模型
使用Tensorflow进行实时移动视频对象检测
随着对计算机视觉的用例日益增长的兴趣,例如无人驾驶汽车,面部识别,智能交通系统等,人们希望建立定制的机器学习模型以检测和识别特定对象。
代码医生工作室
2019/10/10
2.2K0
使用Tensorflow进行实时移动视频对象检测
如何创建对象以及jQuery中创建对象的方式(推荐)
在实际使用当中,字面量创建对象虽然很有用,但是它并不能满足我们的所有需求,我们希望能够能够和其他后台语言一样创建一个类,然后声明类的实例就能够多次使用,而不用每次使用的时候都要重新创建它,于是,便有了工厂模式的出现。
晓歌
2018/08/15
5.1K0
如何创建对象以及jQuery中创建对象的方式(推荐)
Java中创建对象的方式
Class类的Class.newInstance使用的是类的public的无参数构造方法。
算法与编程之美
2023/01/03
1.7K0
Java中创建对象的方式
作为Java开发者,我们每天创建很多对象,但我们通常使用依赖管理系统,比如Spring去创建对象。然而这里有很多创建对象的方法,我们会在这篇文章中学到。 Java中有5种创建对象的方式,下面给出它们的
程序你好
2018/07/20
1K0
汇总 | OpenCV DNN支持的对象检测模型
OpenCV DNN不光支持图像分类,对象检测作为计算机视觉主要任务之一,OpenCV DNN支持多种对象检测模型,可以快速实现基于COCO数据集与Pascal VOC数据集的对象检测。此外基于自定义数据集,通过tensorflow对象检测框架或者pytorch的ONNX格式还可以支持自定义对象检测模型训练导出与部署。本文总结了OpenCV DNN支持的各种对象检测模型与它们的输入输出。
OpenCV学堂
2020/09/08
1.4K0
汇总 | OpenCV DNN支持的对象检测模型
简单问题的有限元模型
如图所示,杆一端固定,另一端距离刚性墙为, 杆中间位置作用一个F,当时,求杆两端的反力。 当时,杆右端已经与刚性墙接触。有限元模型如下图所示,平衡方程为 考虑边界条件,于是 解得 代入平衡方程可得,支座反力 杆系结构有限元分析有以下3个层次: (1)单元分析。将结构离散为若干有限单元,研究典型单元的力学特性,确定单元坐标系中的单元刚度矩阵。此外,还要将单元坐标系中的刚度矩阵,节点力转化成为整体坐标系中的。  (2)整体分析。在单元分析的基础上,形成整体刚度矩阵,整体节点力向量,进一步形成刚度方程。并求解得
fem178
2022/04/18
4780
简单问题的有限元模型
Oracle创建数据对象时加双引号存在的问题
一位开发的同事在Oracle中创建表空间A,然后创建用户user_a并指定表空间为A时,提示表空间不存在。
星哥玩云
2022/08/18
8470
Oracle获取数据库中的对象创建语句
实验环境:Oracle 11.2.0.4 以获取jingyu用户下的T1表为例:
Alfred Zhao
2019/05/24
1.7K0
Orange:用于创建机器学习模型的便捷开源工具
在本教程中,我将演示Orange,一种用于机器学习的工具。Orange是一款极易使用,轻巧的拖放式工具。更重要的是,它是开源的!如果您是Anaconda用户,那么您可以在控制台中找到它,如下图所示 - 一个带着微笑的纯橙色太阳镜。
February
2018/11/16
3.3K0
[WPF 自定义控件]创建包含CheckBox的ListBoxItem
不过它用起来不怎么样,与其这样还不如参考UWP的ListView实现,而且动画效果也很好看:
dino.c
2020/02/21
2.9K0
[WPF 自定义控件]创建包含CheckBox的ListBoxItem
脑电分析系列[MNE-Python-14]| Epoch对象中的元数据(metadata)
脑电分析系列[MNE-Python-2]| MNE中数据结构Epoch及其创建方法
脑机接口社区
2022/08/17
6380
脑电分析系列[MNE-Python-14]| Epoch对象中的元数据(metadata)
解密 Python 中的对象模型
关于 Python,你肯定听过这么一句话:"Python中一切皆对象"。没错,在 Python 的世界里,一切都是对象。
Python编程与实战
2021/01/25
1.6K0
JavaScript中创建对象的几种模式
代码如下: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>创建对象的模式</title> 6 <meta name="viewport" content="width=device-width, initial-scale=1"> 7 <!--<link rel="stylesheet" type="text/css" href="ma
用户1149564
2018/07/31
1.2K0
JavaScript中创建对象的几种模式
TensorFlow:使用Cloud TPU在30分钟内训练出实时移动对象检测器
是否能够更快地训练和提供对象检测模型?我们已经听到了这种的反馈,在今天我们很高兴地宣布支持训练Cloud TPU上的对象检测模型,模型量化以及并添加了包括RetinaNet和MobileNet改编的RetinaNet在内的新模型。本文将引导你使用迁移学习在Cloud TPU上训练量化的宠物品种检测器。
AiTechYun
2018/07/27
4K0
TensorFlow:使用Cloud TPU在30分钟内训练出实时移动对象检测器
Qt核心:元对象系统(1)- 元对象和元数据
P.S.(该系列文章是个人学习总结,拿出来和大家讨论,水平有限,如有错误,特别、非常、极其欢迎批评和指正!)
登山客
2022/10/17
2.5K0
JavaScript面试卷(二) -- 复杂的创建对象模型
接着上一篇文章说,上一篇创建的对象没有向外部提供直接设置属性值的入口。都是在new 创建对象时,给定默认值。
用户7293182
2022/01/17
6150
JavaScript面试卷(二) -- 复杂的创建对象模型

相似问题

使用元数据创建用于对象检测的tflite模型

123

不适用于TFlite的标准MLKit对象检测模型

211

Tensorflow对象检测api模型到tflite

126

TFLite对象检测模型的Sagemaker Neo编译失败

157

如何从tflite模型中提取元数据

140
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文