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

相关推荐
小猴子爱上树11 分钟前
跨境电商翻译工具推荐:批量图片翻译、视频字幕翻译、智能抠图一站式搞定
人工智能·python·音视频
林澈在路上35 分钟前
AI翻唱软件哪个好 2026国产AI写歌工具对比推荐
大数据·人工智能·深度学习·github·aigc·音视频·音频
咖啡星人k39 分钟前
2026 MCP 模型上下文协议:让 AI 自己动手接工具,告别手写接口(MonkeyCode 实战)
人工智能·深度学习·机器学习·语言模型·自然语言处理
丈剑走天涯42 分钟前
基于 Python 3 语法的 PyCharm 练习示例,涵盖列表操作、类型判断及数据清洗等核心知识点
windows·python·pycharm
————A1 小时前
Agent 接收用户上传文件
人工智能·笔记·python·状态模式
可乐鸡翅yeah_1 小时前
hls.js 内存泄漏实战排查,直播 M3U8 长时间播放异常定位方案
开发语言·javascript·python·django·ecmascript·m3u8·m3u8在线
可乐鸡翅yeah_1 小时前
第三方接口对接 M3U8 流媒体排坑实战,解决外部服务商流兼容难题
前端·javascript·python·django·html·m3u8·m3u8在线
金融小师妹1 小时前
多因子智能推演:黄金震荡回升,杰克逊霍尔“沃什首秀”政策如何重塑金价路径的AI预测框架
大数据·人工智能·python·线性回归
阿童木写作1 小时前
跨马翻译:AI批量图片翻译与视频字幕翻译工具推荐
人工智能·python·音视频
阿kun要赚马内2 小时前
MCP(Model Context Protocol,模型上下文协议)
人工智能·python