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

发布于:2025-03-05 ⋅ 阅读:(17) ⋅ 点赞:(0)

目录

说明

代码

其他

参考


说明

本模型为高性能热门应用系列检测模型中的 实时手机检测模型,基于面向工业落地的高性能检测框架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