• Yolov5 batch 推理


    前言

    想要就有了

    代码

    1. import shutil
    2. import time
    3. import traceback
    4. import torch
    5. import os
    6. import cv2
    7. class PeopleDetect(object):
    8. def __init__(self, repo_or_dir, weight_path, confidence) -> None:
    9. self.model = torch.hub.load(repo_or_dir, "custom", path=weight_path, source='local',force_reload=True)
    10. self.confidence = confidence
    11. def start(self, img) -> list:
    12. mode_result = self.model(img,size=640)
    13. assert mode_result
    14. detect_result = mode_result.pandas().xyxy[0].to_dict(orient='index')
    15. if (len(detect_result)) > 0:
    16. box_list = self._get_box(detect_result)
    17. return box_list
    18. else:
    19. return []
    20. def _get_box(self, detect_result:list) -> list:
    21. box_list = []
    22. for i in range(len(detect_result)):
    23. conf = detect_result[i]['confidence']
    24. xmin = int(detect_result[i]['xmin'])
    25. ymin = int(detect_result[i]['ymin'])
    26. xmax = int(detect_result[i]['xmax'])
    27. ymax = int(detect_result[i]['ymax'])
    28. if conf > self.confidence:
    29. box_list.append([xmin, ymin, xmax, ymax,conf])
    30. return box_list
    31. detect=PeopleDetect(repo_or_dir="yolov5", weight_path='weight/best.pt', confidence=0.01)
    32. images_path="/home/save_images"
    33. save_path="/home/errors"
    34. Filelist = []
    35. for home, dirs, files in os.walk(images_path):
    36. for filename in files:
    37. Filelist.append(os.path.join(home, filename))
    38. start_time=time.time()
    39. image_list=[]
    40. count=0
    41. for ind,i in enumerate( Filelist):
    42. try:
    43. image_ori=cv2.imread(i)
    44. if len(image_list)<128:
    45. image_list.append(image_ori[:,:,-1])
    46. else:
    47. res=detect.start(image_list)
    48. image_list=[]
    49. print(int(time.time() - start_time) / int(ind))
    50. except (Exception,BaseException)as e:
    51. print(traceback.format_exc())

    测试结果

    以下结果使用RTX3080 测试得出,imgsize为640,model为5s 

    batch

    GPU显存/MB

    单帧耗时/sFPS
    115000.0333
    816000.0250
    1617000.02150
    3228000.02150
    6436000.018950
    10046000.021750
    12854000.021850
    20074000.02250

  • 相关阅读:
    mybatis
    YOLO 系列论文精读 & YOLOv4
    FFmpeg 命令:从入门到精通 | ffplay 命令播放媒体
    Amazon EC2 部署Ollama + webUI
    如何在C#中使用Channels进行异步排队
    STARKs with small finite field:小域带来的迷人性能
    HTML分类面试题
    npm 常用命令
    Linux Shell脚本的10个有用的“面试问题和解答”
    Vue3简单项目流程分享——工作室主页
  • 原文地址:https://blog.csdn.net/qq_55542491/article/details/134264211