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

相关推荐
小陈工3 分钟前
FastAPI性能优化实战:从每秒100请求到1000的踩坑记录
python·性能优化·django·flask·numpy·pandas·fastapi
知我Deja_Vu8 分钟前
【避坑指南】ConcurrentHashMap 并发计数优化实战
java·开发语言·python
njidf12 分钟前
用Python制作一个文字冒险游戏
jvm·数据库·python
呆呆小孩23 分钟前
Anaconda 被误删抢救手册:从绝望到重生
python·conda
liliangcsdn24 分钟前
LLM复杂数值的提取计算场景示例
人工智能·python
AI视觉网奇34 分钟前
2d 数字人解决方案-待机动作
人工智能·计算机视觉
人工智能AI酱1 小时前
【AI深究】逻辑回归(Logistic Regression)全网最详细全流程详解与案例(附大量Python代码演示)| 数学原理、案例流程、代码演示及结果解读 | 决策边界、正则化、优缺点及工程建议
人工智能·python·算法·机器学习·ai·逻辑回归·正则化
WangLanguager1 小时前
逻辑回归(Logistic Regression)的详细介绍及Python代码示例
python·算法·逻辑回归
wefly20171 小时前
m3u8live.cn 在线M3U8播放器,免安装高效验流排错
前端·后端·python·音视频·前端开发工具
ZTLJQ1 小时前
深入理解逻辑回归:从数学原理到实战应用
开发语言·python·机器学习