替换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
相关推荐
wanzhong23331 天前
CUDA学习5-矩阵乘法(共享内存版)
深度学习·学习·算法·cuda·高性能计算
Hcoco_me1 天前
大模型面试题19:梯度消失&梯度爆炸 纯白话文版
人工智能·rnn·深度学习·自然语言处理·word2vec
是店小二呀1 天前
在 AtomGit 昇腾 Atlas 800T上解锁 SGLang:零成本打造高性能推理服务
人工智能·pytorch·深度学习·npu
万事可爱^1 天前
GitCode+昇腾部署Rnj-1模型实践教程
人工智能·深度学习·语言模型·gitcode·本地部署·昇腾npu
高洁011 天前
图神经网络初探(2)
人工智能·深度学习·算法·机器学习·transformer
祝余Eleanor1 天前
Day 51 神经网络调参指南
深度学习·神经网络·机器学习
算法熔炉1 天前
深度学习面试八股文(4)—— transformer专题
深度学习·面试·transformer
大学生毕业题目1 天前
毕业项目推荐:90-基于yolov8/yolov5/yolo11的工程车辆检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·目标检测·cnn·pyqt·工程车辆检测
软件算法开发1 天前
基于山羚羊优化的LSTM深度学习网络模型(MGO-LSTM)的一维时间序列预测算法matlab仿真
深度学习·matlab·lstm·一维时间序列预测·山羚羊优化·mgo-lstm
LaughingZhu1 天前
Product Hunt 每日热榜 | 2025-12-26
人工智能·经验分享·深度学习·神经网络·产品运营