modelscope实时手机检测-通用 模型导出Onnx、修改模型输出维度

目录

说明

代码

其他

参考


说明

本模型为高性能热门应用系列检测模型中的 实时手机检测模型,基于面向工业落地的高性能检测框架DAMOYOLO,其精度和速度超越当前经典的YOLO系列方法。用户使用的时候,仅需要输入一张图像,便可以获得图像中所有手机的坐标信息,并可用于打电话检测等后续应用场景。

本模型为实时手机检测模型,基于检测框架DAMOYOLO-S模型,DAMO-YOLO是一个面向工业落地的目标检测框架,兼顾模型速度与精度,其训练的模型效果超越了目前的一众YOLO系列方法,并且仍然保持极高的推理速度。

代码

复制代码
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks

model_id = 'damo/cv_tinynas_object-detection_damoyolo_phone'
input_location = 'image_phone.jpg'

phone_detection = pipeline(Tasks.domain_specific_object_detection, model=model_id)
result = phone_detection(input_location)
print("result is : ", result)

#================模型导出============================
from modelscope.models import Model
import torch

model_id = 'damo/cv_tinynas_object-detection_damoyolo_phone'
model = Model.from_pretrained(model_id)
model.eval()
input = torch.randn((1,3,640,640)).float()
type(model).__call__ = type(model).forward
torch.onnx.export(model, input,'damoyolo_phone.onnx',input_names=["input"],output_names=["output"],opset_version=13)


#================修改模型输出维度=======================
import onnx
from onnx import helper, TensorProto,shape_inference

# 加载原始模型
model = onnx.load("damoyolo_phone.onnx")

# 获取原始输出节点信息
original_output = model.graph.output[0]
print(f"Original output shape: {original_output.type.tensor_type.shape}")

# 创建Transpose节点
transpose_node = helper.make_node(
        "Transpose",
        inputs=[original_output.name],  # 使用原始输出作为输入
        outputs=["transposed_output"],
        perm=[0, 2, 1]  # 交换第1和第2维度
)

# 创建新的输出Tensor
new_output = helper.make_tensor_value_info(
        "transposed_output",
        TensorProto.FLOAT,
        [1, 5, 8400]  # 新的形状
)

# 添加新节点到计算图
model.graph.node.append(transpose_node)

# 替换输出
model.graph.output.remove(original_output)
model.graph.output.insert(0, new_output)

# 检查并修复模型结构
model = shape_inference.infer_shapes(model)

# 保存修改后的模型
onnx.save(model, "model_modified.onnx")
print("Model modified and saved successfully!")
复制代码
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks

model_id = 'damo/cv_tinynas_object-detection_damoyolo_phone'
input_location = 'image_phone.jpg'

phone_detection = pipeline(Tasks.domain_specific_object_detection, model=model_id)
result = phone_detection(input_location)
print("result is : ", result)

#================模型导出============================
from modelscope.models import Model
import torch

model_id = 'damo/cv_tinynas_object-detection_damoyolo_phone'
model = Model.from_pretrained(model_id)
model.eval()
input = torch.randn((1,3,640,640)).float()
type(model).__call__ = type(model).forward
torch.onnx.export(model, input,'damoyolo_phone.onnx',input_names=["input"],output_names=["output"],opset_version=13)


#================修改模型输出维度=======================
import onnx
from onnx import helper, TensorProto,shape_inference

# 加载原始模型
model = onnx.load("damoyolo_phone.onnx")

# 获取原始输出节点信息
original_output = model.graph.output[0]
print(f"Original output shape: {original_output.type.tensor_type.shape}")

# 创建Transpose节点
transpose_node = helper.make_node(
        "Transpose",
        inputs=[original_output.name],  # 使用原始输出作为输入
        outputs=["transposed_output"],
        perm=[0, 2, 1]  # 交换第1和第2维度
)

# 创建新的输出Tensor
new_output = helper.make_tensor_value_info(
        "transposed_output",
        TensorProto.FLOAT,
        [1, 5, 8400]  # 新的形状
)

# 添加新节点到计算图
model.graph.node.append(transpose_node)

# 替换输出
model.graph.output.remove(original_output)
model.graph.output.insert(0, new_output)

# 检查并修复模型结构
model = shape_inference.infer_shapes(model)

# 保存修改后的模型
onnx.save(model, "model_modified.onnx")
print("Model modified and saved successfully!")

其他

模型修改后推理实现C# OnnxRuntime DAMO-YOLO 手机检测

参考

https://modelscope.cn/models/iic/cv_tinynas_object-detection_damoyolo_phone/summary

相关推荐
全栈练习生12 小时前
AI Agent 沙箱
python·ai
SEO_juper13 小时前
用 Python 写一个 GEO 可见性检查脚本:你的网站现在能被 AI 引用吗
开发语言·人工智能·爬虫·python·seo·外贸独立站
数智工坊14 小时前
视觉SLAM第12讲|地图构建:单目稠密重建、RGB-D点云与八叉树地图全解析
人工智能·深度学习·矩阵·机器人
GPU实战笔记14 小时前
云端 GPU 训练的账户余额提醒:它能说明什么,不能说明什么?
深度学习·算法·成本管理·gpu云计算·云端训练
用户83562907805115 小时前
使用 Python 对 Excel 工作表进行排序
后端·python
for_ever_love__15 小时前
MySQL 全文索引实战:FULLTEXT、ngram 中文分词与 MATCH AGAINST 到底该怎么用
java·python·mysql·全文检索·分词·索引·ngram
用户83562907805115 小时前
使用 Python 合并 PowerPoint 演示文稿
后端·python
weixin_4617694015 小时前
VS Code 中创建 Jupyter 文件(.ipynb)
ide·python·jupyter
论文复现现场15 小时前
8卡4090能跑70B吗?Llama-2显存预算、QLoRA与通信瓶颈
人工智能·深度学习·分布式训练·llama·显存·qlora·算家云
2601_9669496516 小时前
多市场量化策略的数据接口应该如何设计:从数据层架构到策略接入
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash