替换SlowFast中Detectron2为Yolov8

一 需求

FaceBookReserch中SlowFast源码中检测框是用Detectron2进行目标检测,本文想实现用yolov8替换detectron2

二 实施方案

首先,yolov8 支持有自定义库ultralytics(仅支持yolov8),安装对应库

bash 复制代码
pip install ultralytics

源码中slowfast/visualization.py 43行中

python 复制代码
if cfg.DETECTION.ENABLE:
       self.object_detector = Detectron2Predictor(cfg, gpu_id=self.gpu_id)

根据ultralytics文档进行定义

创建对应YOLOPredictor类(加入了检测框及其标签,具体见前一篇文章)

python 复制代码
class YOLOPredictor:

    def __init__(self, cfg, gpu_id=None):
        # 加载预训练的 YOLOv8n 模型
        self.model = YOLO('/root/autodl-tmp/data/runs/detect/train/weights/best.pt')
        self.detect_names, _, _ = get_class_names(cfg.DEMO.Detect_File_Path, None, None)

    def __call__(self, task):
        """
        Return bounding boxes predictions as a tensor.
        Args:
            task (TaskInfo object): task object that contain
                the necessary information for action prediction. (e.g. frames)
        Returns:
            task (TaskInfo object): the same task info object but filled with
                prediction values (a tensor) and the corresponding boxes for
                action detection task.
        """
        # """得到预测置信度"""
        # scores = outputs["instances"].scores[mask].tolist()
        # """获取类别标签"""
        # pred_labels = outputs["instances"].pred_classes[mask]
        # pred_labels = pred_labels.tolist()
        # """进行标签匹配"""
        # for i in range(len(pred_labels)):
        #     pred_labels[i] = self.detect_names[pred_labels[i]]
        # preds = [
        #     "[{:.4f}] {}".format(s, labels) for s, labels in zip(scores, pred_labels)
        # ]
        # """加入预测标签"""
        # task.add_detect_preds(preds)
        # task.add_bboxes(pred_boxes)
        middle_frame = task.frames[len(task.frames) // 2]
        outputs = self.model(middle_frame)
        boxes = outputs[0].boxes
        mask = boxes.conf >= 0.5
        pred_boxes = boxes.xyxy[mask]
        scores = boxes.conf[mask].tolist()
        pred_labels = boxes.cls[mask].to(torch.int)
        pred_labels = pred_labels.tolist()
        for i in range(len(pred_labels)):
            pred_labels[i] = self.detect_names[pred_labels[i]]
        preds = [
            "[{:.4f}] {}".format(s, labels) for s, labels in zip(scores, pred_labels)
        ]
        """加入预测标签"""
        task.add_detect_preds(preds)
        task.add_bboxes(pred_boxes)

        return task
相关推荐
静心问道5 小时前
WGAN算法
深度学习·算法·机器学习
清纯世纪7 小时前
基于深度学习的图像分类或识别系统(含全套项目+PyQt5界面)
开发语言·python·深度学习
AIPaPerPass写论文7 小时前
写论文去哪个网站?2024最佳五款AI毕业论文学术网站
人工智能·深度学习·chatgpt·powerpoint·ai写作
5pace8 小时前
PyTorch深度学习快速入门教程【土堆】基础知识篇
人工智能·pytorch·深度学习
从懒虫到爬虫8 小时前
YOLOv5/v8 + 双目相机测距
yolo·qq767172261·双目测距
FL16238631299 小时前
[数据集][目标检测]不同颜色的安全帽检测数据集VOC+YOLO格式7574张5类别
人工智能·yolo·目标检测
QuantumYou9 小时前
计算机视觉 对比学习 串烧二
人工智能·学习·计算机视觉
Terry Cao 漕河泾10 小时前
SRT3D: A Sparse Region-Based 3D Object Tracking Approach for the Real World
人工智能·计算机视觉·3d·目标跟踪
AI完全体10 小时前
AI小项目4-用Pytorch从头实现Transformer(详细注解)
人工智能·pytorch·深度学习·机器学习·语言模型·transformer·注意力机制
AI知识分享官10 小时前
智能绘画Midjourney AIGC在设计领域中的应用
人工智能·深度学习·语言模型·chatgpt·aigc·midjourney·llama