【基于yolo转onnx 量化测试】

1、 训练模型转onnx 和量化

from ultralytics import YOLOmodel_path = "yolov10/runs/train8/weights/best.pt"
model = YOLO(model_path)    # 载入官方模型
# 导出模型
model.export(format='onnx',half=True)

2、量化,减少了三分之一的存储空间从100M到30M

from onnxruntime.quantization import quantize_dynamic, QuantType# 动态量化模型
quantize_dynamic("/train8/weights/best.onnx", "best_quantized.onnx", weight_type=QuantType.QUInt8)

3、利用onnx 模型推理

import math
import time
import cv2
import numpy as np
import onnxruntimeclass_names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light','fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow','elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee','skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard','tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple','sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch','potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard','cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase','scissors', 'teddy bear', 'hair drier', 'toothbrush']# Create a list of colors for each class where each color is a tuple of 3 integer values
rng = np.random.default_rng(3)
colors = rng.uniform(0, 255, size=(len(class_names), 3))def nms(boxes, scores, iou_threshold):# Sort by scoresorted_indices = np.argsort(scores)[::-1]keep_boxes = []while sorted_indices.size > 0:# Pick the last boxbox_id = sorted_indices[0]keep_boxes.append(box_id)# Compute IoU of the picked box with the restious = compute_iou(boxes[box_id, :], boxes[sorted_indices[1:], :])# Remove boxes with IoU over the thresholdkeep_indices = np.where(ious < iou_threshold)[0]# print(keep_indices.shape, sorted_indices.shape)sorted_indices = sorted_indices[keep_indices + 1]return keep_boxesdef compute_iou(box, boxes):# Compute xmin, ymin, xmax, ymax for both boxesxmin = np.maximum(box[0], boxes[:, 0])ymin = np.maximum(box[1], boxes[:, 1])xmax = np.minimum(box[2], boxes[:, 2])ymax = np.minimum(box[3], boxes[:, 3])# Compute intersection areaintersection_area = np.maximum(0, xmax - xmin) * np.maximum(0, ymax - ymin)# Compute union areabox_area = (box[2] - box[0]) * (box[3] - box[1])boxes_area = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])union_area = box_area + boxes_area - intersection_area# Compute IoUiou = intersection_area / union_areareturn ioudef xywh2xyxy(x):# Convert bounding box (x, y, w, h) to bounding box (x1, y1, x2, y2)y = np.copy(x)y[..., 0] = x[..., 0] - x[..., 2] / 2y[..., 1] = x[..., 1] - x[..., 3] / 2y[..., 2] = x[..., 0] + x[..., 2] / 2y[..., 3] = x[..., 1] + x[..., 3] / 2return ydef sigmoid(x):return 1 / (1 + np.exp(-x))def draw_detections(image, boxes, scores, class_ids, mask_alpha=0.3, mask_maps=None):img_height, img_width = image.shape[:2]size = min([img_height, img_width]) * 0.0006text_thickness = int(min([img_height, img_width]) * 0.001)mask_img = draw_masks(image, boxes, class_ids, mask_alpha, mask_maps)# Draw bounding boxes and labels of detectionsfor box, score, class_id in zip(boxes, scores, class_ids):color = colors[class_id]x1, y1, x2, y2 = box.astype(int)# Draw rectanglecv2.rectangle(mask_img, (x1, y1), (x2, y2), color, 2)label = class_names[class_id]caption = f'{label} {int(score * 100)}%'(tw, th), _ = cv2.getTextSize(text=caption, fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale=size, thickness=text_thickness)th = int(th * 1.2)cv2.rectangle(mask_img, (x1, y1),(x1 + tw, y1 - th), color, -1)cv2.putText(mask_img, caption, (x1, y1),cv2.FONT_HERSHEY_SIMPLEX, size, (255, 255, 255), text_thickness, cv2.LINE_AA)return mask_imgdef draw_masks(image, boxes, class_ids, mask_alpha=0.3, mask_maps=None):mask_img = image.copy()# Draw bounding boxes and labels of detectionsfor i, (box, class_id) in enumerate(zip(boxes, class_ids)):color = colors[class_id]x1, y1, x2, y2 = box.astype(int)# Draw fill mask imageif mask_maps is None:cv2.rectangle(mask_img, (x1, y1), (x2, y2), color, -1)else:crop_mask = mask_maps[i][y1:y2, x1:x2, np.newaxis]crop_mask_img = mask_img[y1:y2, x1:x2]crop_mask_img = crop_mask_img * (1 - crop_mask) + crop_mask * colormask_img[y1:y2, x1:x2] = crop_mask_imgreturn cv2.addWeighted(mask_img, mask_alpha, image, 1 - mask_alpha, 0)def draw_comparison(img1, img2, name1, name2, fontsize=2.6, text_thickness=3):(tw, th), _ = cv2.getTextSize(text=name1, fontFace=cv2.FONT_HERSHEY_DUPLEX,fontScale=fontsize, thickness=text_thickness)x1 = img1.shape[1] // 3y1 = thoffset = th // 5cv2.rectangle(img1, (x1 - offset * 2, y1 + offset),(x1 + tw + offset * 2, y1 - th - offset), (0, 115, 255), -1)cv2.putText(img1, name1,(x1, y1),cv2.FONT_HERSHEY_DUPLEX, fontsize,(255, 255, 255), text_thickness)(tw, th), _ = cv2.getTextSize(text=name2, fontFace=cv2.FONT_HERSHEY_DUPLEX,fontScale=fontsize, thickness=text_thickness)x1 = img2.shape[1] // 3y1 = thoffset = th // 5cv2.rectangle(img2, (x1 - offset * 2, y1 + offset),(x1 + tw + offset * 2, y1 - th - offset), (94, 23, 235), -1)cv2.putText(img2, name2,(x1, y1),cv2.FONT_HERSHEY_DUPLEX, fontsize,(255, 255, 255), text_thickness)combined_img = cv2.hconcat([img1, img2])if combined_img.shape[1] > 3840:combined_img = cv2.resize(combined_img, (3840, 2160))return combined_imgclass YOLOSeg:def __init__(self, path, conf_thres=0.7, iou_thres=0.5, num_masks=32):self.conf_threshold = conf_thresself.iou_threshold = iou_thresself.num_masks = num_masks# Initialize modelself.initialize_model(path)def __call__(self, image):return self.segment_objects(image)def initialize_model(self, path):self.session = onnxruntime.InferenceSession(path,providers=['CUDAExecutionProvider','CPUExecutionProvider'])# Get model infoself.get_input_details()self.get_output_details()def segment_objects(self, image):input_tensor = self.prepare_input(image)# Perform inference on the image   # 注意用dfl回归bbox的话,bbox的维度就是80+16*4+32outputs = self.inference(input_tensor)  # output[0] (1,4+80+32=116,80*80+40*40+20*20=8400)  //  output[1] (1,32,160,160)# 对输出0进行处理,返回bbox和对应的mask_featureself.boxes, self.scores, self.class_ids, mask_pred = self.process_box_output(outputs[0])self.mask_maps = self.process_mask_output(mask_pred, outputs[1])return self.boxes, self.scores, self.class_ids, self.mask_mapsdef prepare_input(self, image):self.img_height, self.img_width = image.shape[:2]input_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)# Resize input imageinput_img = cv2.resize(input_img, (self.input_width, self.input_height))# Scale input pixel values to 0 to 1input_img = input_img / 255.0input_img = input_img.transpose(2, 0, 1)input_tensor = input_img[np.newaxis, :, :, :].astype(np.float32)return input_tensordef inference(self, input_tensor):start = time.perf_counter()outputs = self.session.run(self.output_names, {self.input_names[0]: input_tensor})# print(f"Inference time: {(time.perf_counter() - start)*1000:.2f} ms")return outputsdef process_box_output(self, box_output):predictions = np.squeeze(box_output).Tnum_classes = box_output.shape[1] - self.num_masks - 4# Filter out object confidence scores below thresholdscores = np.max(predictions[:, 4:4+num_classes], axis=1)predictions = predictions[scores > self.conf_threshold, :]scores = scores[scores > self.conf_threshold]if len(scores) == 0:return [], [], [], np.array([])box_predictions = predictions[..., :num_classes+4]mask_predictions = predictions[..., num_classes+4:]# Get the class with the highest confidenceclass_ids = np.argmax(box_predictions[:, 4:], axis=1)# Get bounding boxes for each objectboxes = self.extract_boxes(box_predictions)# Apply non-maxima suppression to suppress weak, overlapping bounding boxesindices = nms(boxes, scores, self.iou_threshold)return boxes[indices], scores[indices], class_ids[indices], mask_predictions[indices]def process_mask_output(self, mask_predictions, mask_output):if mask_predictions.shape[0] == 0:return []mask_output = np.squeeze(mask_output)# Calculate the mask maps for each boxnum_mask, mask_height, mask_width = mask_output.shape  # CHWmasks = sigmoid(mask_predictions @ mask_output.reshape((num_mask, -1)))masks = masks.reshape((-1, mask_height, mask_width))# Downscale the boxes to match the mask sizescale_boxes = self.rescale_boxes(self.boxes,(self.img_height, self.img_width),(mask_height, mask_width))# For every box/mask pair, get the mask mapmask_maps = np.zeros((len(scale_boxes), self.img_height, self.img_width))blur_size = (int(self.img_width / mask_width), int(self.img_height / mask_height))for i in range(len(scale_boxes)):scale_x1 = int(math.floor(scale_boxes[i][0]))scale_y1 = int(math.floor(scale_boxes[i][1]))scale_x2 = int(math.ceil(scale_boxes[i][2]))scale_y2 = int(math.ceil(scale_boxes[i][3]))x1 = int(math.floor(self.boxes[i][0]))y1 = int(math.floor(self.boxes[i][1]))x2 = int(math.ceil(self.boxes[i][2]))y2 = int(math.ceil(self.boxes[i][3]))scale_crop_mask = masks[i][scale_y1:scale_y2, scale_x1:scale_x2]crop_mask = cv2.resize(scale_crop_mask,(x2 - x1, y2 - y1),interpolation=cv2.INTER_CUBIC)crop_mask = cv2.blur(crop_mask, blur_size)crop_mask = (crop_mask > 0.5).astype(np.uint8)mask_maps[i, y1:y2, x1:x2] = crop_maskreturn mask_mapsdef extract_boxes(self, box_predictions):# Extract boxes from predictionsboxes = box_predictions[:, :4]# Scale boxes to original image dimensionsboxes = self.rescale_boxes(boxes,(self.input_height, self.input_width),(self.img_height, self.img_width))# Convert boxes to xyxy formatboxes = xywh2xyxy(boxes)# Check the boxes are within the imageboxes[:, 0] = np.clip(boxes[:, 0], 0, self.img_width)boxes[:, 1] = np.clip(boxes[:, 1], 0, self.img_height)boxes[:, 2] = np.clip(boxes[:, 2], 0, self.img_width)boxes[:, 3] = np.clip(boxes[:, 3], 0, self.img_height)return boxesdef draw_detections(self, image, draw_scores=True, mask_alpha=0.4):return draw_detections(image, self.boxes, self.scores,self.class_ids, mask_alpha)def draw_masks(self, image, draw_scores=True, mask_alpha=0.5):return draw_detections(image, self.boxes, self.scores,self.class_ids, mask_alpha, mask_maps=self.mask_maps)def get_input_details(self):model_inputs = self.session.get_inputs()self.input_names = [model_inputs[i].name for i in range(len(model_inputs))]self.input_shape = model_inputs[0].shapeself.input_height = self.input_shape[2]self.input_width = self.input_shape[3]def get_output_details(self):model_outputs = self.session.get_outputs()self.output_names = [model_outputs[i].name for i in range(len(model_outputs))]@staticmethoddef rescale_boxes(boxes, input_shape, image_shape):# Rescale boxes to original image dimensionsinput_shape = np.array([input_shape[1], input_shape[0], input_shape[1], input_shape[0]])boxes = np.divide(boxes, input_shape, dtype=np.float32)boxes *= np.array([image_shape[1], image_shape[0], image_shape[1], image_shape[0]])return boxesif __name__ == '__main__':model_path = "yolov8n-seg.onnx"model_path = "best_quantized.onnx"# model_path = "train8/weights/best.onnx"# Initialize YOLOv8 Instance Segmentatoryoloseg = YOLOSeg(model_path, conf_thres=0.3, iou_thres=0.5)img = cv2.imread('bus.jpg')img = cv2.imread('street.jpeg')# Detect Objectsyoloseg(img)# Draw detectionscombined_img = yoloseg.draw_masks(img)# cv2.imwrite("Output_yolov10_seg_quantized.jpg", combined_img)cv2.imwrite("street_Output_yolov10_seg_quantized.jpg", combined_img)# cv2.namedWindow("Output", cv2.WINDOW_NORMAL)# cv2.imshow("Output", combined_img)# cv2.waitKey(0)

输入图片:在这里插入图片描述
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/3269309.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

当镜像地址出错的时候下载selenium的处理办法

当镜像地址出错的时候下载selenium的处理办法 一、原因 显示出错&#xff1a; C:\Users\xiaodaidai>pip install selenium3.4.0 Looking in indexes: Simple Index WARNING: Retrying (Retry(total4, connectNone, readNone, redirectNone, statusNone)) after connection …

学语言,看这里,如何快速掌握JavaScript?

本篇文章是基于会点c语言和会点python基础的&#xff0c;去更容易上手javascript 学习笔记分享✨&#x1f308;&#x1f44f;&#x1f44f;&#x1f451;&#x1f451; javascript目录 1.安装node.js&#xff1a;2.配置环境变量——创建NODE_HOME :3.变量与常量4.原生数据类型5…

C++ —— STL简介

1. 什么是STL STL(standard template libaray-标准模板库)&#xff1a;是C标准库的重要组成部分&#xff0c;不仅是一个可复用的 组件库&#xff0c;而且是一个包罗数据结构与算法的软件框架 2.STL的版本 原始版本 Alexander Stepanov、Meng Lee 在惠普实验室完成的原始版本…

Java之父官宣退休

今年不用说大家都知道环境真的很差很差&#xff0c;裁员降薪已经是家常便饭&#xff0c;在这种严峻环境下&#xff0c;我们只能提升自己内功来抗风险&#xff0c;下面分享一本java之父推荐的优秀书籍。 刚过完自己 69 岁生日的两个月后&#xff0c;Java 之父 James Gosling&…

论文阅读:Deep_Generic_Dynamic_Object_Detection_Based_on_Dynamic_Grid_Maps

目录 概要 Motivation 整体框架流程 技术细节 小结 不足 论文地址&#xff1a;Deep Generic Dynamic Object Detection Based on Dynamic Grid Maps | IEEE Conference Publication | IEEE Xplore 概要 该文章提出了一种基于动态网格图&#xff08;Dynamic Grid Maps&a…

Golang高效合并(拼接)多个gzip压缩文件

有时我们可能会遇到需要把多个 gzip 文件合并成单个 gzip 文件的场景&#xff0c;最简单最容易的方式是把每个gzip文件都先解压&#xff0c;然后合并成一个文件后再次进行压缩&#xff0c;最终得到我们想要的结果&#xff0c;但这种先解压后压缩的方式显然效率不高&#xff0c;…

监控Windows文件夹下面的文件(C#和C++实现)

最近在做虚拟打印机时&#xff0c;需要实时监控打印文件的到达&#xff0c;并移动文件到另外的位置。一开始我使用了线程&#xff0c;在线程里去检测新文件的到达。实际上Windows提供了一个文件监控接口函数ReadDIrectoryChangesW。这个函数可以对所有文件操作进行监控。 ReadD…

1 深度学习网络DNN

代码来自B站up爆肝杰哥 测试版本 import torch import torchvisiondef print_hi(name):print(fHi, {name}) if __name__ __main__:print_hi(陀思妥耶夫斯基)print("HELLO pytorch {}".format(torch.__version__))print("torchvision.version:", torchvi…

2024后端开发面试题总结

一、前言 上一篇离职贴发布之后仿佛登上了热门&#xff0c;就连曾经阿里的师兄都看到了我的分享&#xff0c;这波流量真是受宠若惊&#xff01; 回到正题&#xff0c;文章火之后&#xff0c;一些同学急切想要让我分享一下面试内容&#xff0c;回忆了几个晚上顺便总结一下&#…

mybatis查询数据字段返回空值

1.描述 数据苦衷实际存储字段全不为空 查询后brand_name/company_name为空 2.原因分析 带下划线的字段&#xff0c;都会返回空值&#xff0c;应该是字段映射出了问题 3.解决方案 在配置文件中添加下划线自动映射为驼峰 <configuration><settings><sett…

【计算机网络】OSPF单区域实验

一&#xff1a;实验目的 1&#xff1a;掌握在路由器上配置OSPF单区域。 2&#xff1a;学习OSPF协议的原理&#xff0c;及其网络拓扑结构改变后的变化。 二&#xff1a;实验仪器设备及软件 硬件&#xff1a;RCMS交换机、网线、内网网卡接口、Windows 2019操作系统的计算机等。…

STM32+ESP8266-连接阿里云-物联网通用Android app(2)

前言 接着上一篇的文章创建好了设备&#xff0c;云产品转发&#xff0c;让STM32连接上阿里云&#xff0c;发布和订阅了相关主题。本篇文章来编写一个Android app来进行控制STM32和接收传感器数据显示在屏幕上。基于Android studio。 演示视频 实现一个简单的app来控制stm32开…

Django-3.3创建模型

创建模型&#xff08;models&#xff09;的时候&#xff0c; 1&#xff1a;我们需要这个模型是哪个文件下面的模型&#xff08;models&#xff09;&#xff0c;我们需要在配置文件中吧应用安装上&#xff08;安装应用&#xff1a;INSTALLED_APPS&#xff09; 2&#xff1a;找对…

【机器学习】不同操作系统下如何安装Jupyter Notebook和Anaconda

引言 Jupyter Notebook 是一个非常流行的开源Web应用程序&#xff0c;允许你创建和共享包含代码、方程、可视化和解释性文本的文档 文章目录 引言一、如何安装Jupyter Notebook1.1 对于Windows用户1.2 对于macOS用户1.3 对于Linux用户&#xff1a; 二、如何安装Anaconda2.1 对于…

《python程序语言设计》第6章13题 数列求和编写一个函数计算

正确代码 def sumNumber(integer_num):print(" i || m(i)")print("-"*30)a 0for i in range(1, integer_num 1):a i / (i 1)print("{:4d} || {:.4f}".format(i, a))sumNumber(20)结果如下

使用 leanback 库 GridView 管理AnroidTV的焦点

一、前情提要 我当前需要开发一个TV应用&#xff0c;但是之前处理过的焦点问题的很少&#xff0c;现在空下来了&#xff0c;对过往的工作做一个总结分享。在手机APP开发中常用的 RecycleView 在 TV 中开发时&#xff0c;无法解决大量的焦点问题&#xff0c;所以使用leanback进…

ElasticSearch核心之DSL查询语句实战

什么是DSL&#xff1f; Elasticsearch提供丰富且灵活的查询语言叫做DSL查询(Query DSL),它允许你构建更加复杂、强大的查询。 DSL(Domain Specific Language特定领域语言)以JSON请求体的形式出现。目前常用的框架查询方法什么的底层都是构建DSL语句实现的&#xff0c;所以你必…

git 学习总结

文章目录 一、 git 基础操作1、工作区2、暂存区3、本地仓库4、远程仓库 二、git 的本质三、分支git 命令总结 作者: baron 一、 git 基础操作 如图所示 git 总共有几个区域 工作区, 暂存区, 本地仓库, 远程仓库. 1、工作区 存放项目代码的地方&#xff0c;他有两种状态 Unm…

2024新版 黑马程序员《C++零基础入门》笔记——第一章24 三元运算符

1.三元运算符 2.代码实践 #include "iostream" using namespace std;int main() {// 表达式? v1 : v2;int num1, num2;cout << "请输入num1的值" << endl;cin >> num1;cout << "请输入num2的值" << endl;cin >…

Flink-CDC解析(第47天)

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言1. CDC 概述1.1 什么是CDC&#xff1f;1.2 CDC的实现机制1) 基于主动查询的 CDC&#xff1a;2) 基于事件接收CDC&#xff1a; 前言 本文主要概述了Flink-CDC. …