替换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
相关推荐
微学AI2 小时前
遥感影像岩性分类:基于CNN与CNN-EL集成学习的深度学习方法
深度学习·分类·cnn
IT古董2 小时前
【第三章:神经网络原理详解与Pytorch入门】02.深度学习框架PyTorch入门-(5)PyTorch 实战——使用 RNN 进行人名分类
pytorch·深度学习·神经网络
视觉语言导航4 小时前
ICCV-2025 | 复杂场景的精准可控生成新突破!基于场景图的可控 3D 户外场景生成
人工智能·深度学习·具身智能
AI街潜水的八角6 小时前
深度学习图像分类数据集—濒危动物识别分类
人工智能·深度学习
安思派Anspire7 小时前
LangGraph + MCP + Ollama:构建强大代理 AI 的关键(一)
前端·深度学习·架构
FF-Studio7 小时前
大语言模型(LLM)课程学习(Curriculum Learning)、数据课程(data curriculum)指南:从原理到实践
人工智能·python·深度学习·神经网络·机器学习·语言模型·自然语言处理
CoovallyAIHub8 小时前
YOLO模型优化全攻略:从“准”到“快”,全靠这些招!
深度学习·算法·计算机视觉
G.E.N.9 小时前
开源!RAG竞技场(2):标准RAG算法
大数据·人工智能·深度学习·神经网络·算法·llm·rag
zm-v-1593043398611 小时前
ArcGIS 水文分析升级:基于深度学习的流域洪水演进过程模拟
人工智能·深度学习·arcgis
SHIPKING39313 小时前
【机器学习&深度学习】什么是下游任务模型?
人工智能·深度学习·机器学习