2025-简单点-ultralytics.models.yolo.detect.predict.py解析

代码

# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license

from ultralytics.engine.predictor import BasePredictor
from ultralytics.engine.results import Results
from ultralytics.utils import ops


class DetectionPredictor(BasePredictor):
    """
    A class extending the BasePredictor class for prediction based on a detection model.

    Example:
        ```python
        from ultralytics.utils import ASSETS
        from ultralytics.models.yolo.detect import DetectionPredictor

        args = dict(model="yolo11n.pt", source=ASSETS)
        predictor = DetectionPredictor(overrides=args)
        predictor.predict_cli()
        ```
    """

    def postprocess(self, preds, img, orig_imgs, **kwargs):
        """Post-processes predictions and returns a list of Results objects."""
        preds = ops.non_max_suppression(
            preds,
            self.args.conf,
            self.args.iou,
            self.args.classes,
            self.args.agnostic_nms,
            max_det=self.args.max_det,
            nc=len(self.model.names),
            end2end=getattr(self.model, "end2end", False),
            rotated=self.args.task == "obb",
        )

        if not isinstance(orig_imgs, list):  # input images are a torch.Tensor, not a list
            orig_imgs = ops.convert_torch2numpy_batch(orig_imgs)

        return self.construct_results(preds, img, orig_imgs, **kwargs)

    def construct_results(self, preds, img, orig_imgs):
        """
        Constructs a list of result objects from the predictions.

        Args:
            preds (List[torch.Tensor]): List of predicted bounding boxes and scores.
            img (torch.Tensor): The image after preprocessing.
            orig_imgs (List[np.ndarray]): List of original images before preprocessing.

        Returns:
            (list): List of result objects containing the original images, image paths, class names, and bounding boxes.
        """
        return [
            self.construct_result(pred, img, orig_img, img_path)
            for pred, orig_img, img_path in zip(preds, orig_imgs, self.batch[0])
        ]

    def construct_result(self, pred, img, orig_img, img_path):
        """
        Constructs the result object from the prediction.

        Args:
            pred (torch.Tensor): The predicted bounding boxes and scores.
            img (torch.Tensor): The image after preprocessing.
            orig_img (np.ndarray): The original image before preprocessing.
            img_path (str): The path to the original image.

        Returns:
            (Results): The result object containing the original image, image path, class names, and bounding boxes.
        """
        pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], orig_img.shape)
        return Results(orig_img, path=img_path, names=self.model.names, boxes=pred[:, :6])

继承BasePredictor: 有preprocess和pre_transform和stream_inference等方法

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

在这里插入图片描述
自己重写了 postprocess:

主要关注nms和construct_results
在这里插入图片描述
construct_results就是构造成独有的Result对象

non_max_pression就是极大值抑制,具体代码如下:

non_max_suppression

def non_max_suppression(
    prediction,
    conf_thres=0.25,
    iou_thres=0.45,
    classes=None,
    agnostic=False,
    multi_label=False,
    labels=(),
    max_det=300,
    nc=0,  # number of classes (optional)
    max_time_img=0.05,
    max_nms=30000,
    max_wh=7680,
    in_place=True,
    rotated=False,
    end2end=False,
):
    """
    Perform non-maximum suppression (NMS) on a set of boxes, with support for masks and multiple labels per box.

    Args:
        prediction (torch.Tensor): A tensor of shape (batch_size, num_classes + 4 + num_masks, num_boxes)
            containing the predicted boxes, classes, and masks. The tensor should be in the format
            output by a model, such as YOLO.
        conf_thres (float): The confidence threshold below which boxes will be filtered out.
            Valid values are between 0.0 and 1.0.
        iou_thres (float): The IoU threshold below which boxes will be filtered out during NMS.
            Valid values are between 0.0 and 1.0.
        classes (List[int]): A list of class indices to consider. If None, all classes will be considered.
        agnostic (bool): If True, the model is agnostic to the number of classes, and all
            classes will be considered as one.
        multi_label (bool): If True, each box may have multiple labels.
        labels (List[List[Union[int, float, torch.Tensor]]]): A list of lists, where each inner
            list contains the apriori labels for a given image. The list should be in the format
            output by a dataloader, with each label being a tuple of (class_index, x1, y1, x2, y2).
        max_det (int): The maximum number of boxes to keep after NMS.
        nc (int, optional): The number of classes output by the model. Any indices after this will be considered masks.
        max_time_img (float): The maximum time (seconds) for processing one image.
        max_nms (int): The maximum number of boxes into torchvision.ops.nms().
        max_wh (int): The maximum box width and height in pixels.
        in_place (bool): If True, the input prediction tensor will be modified in place.
        rotated (bool): If Oriented Bounding Boxes (OBB) are being passed for NMS.
        end2end (bool): If the model doesn't require NMS.

    Returns:
        (List[torch.Tensor]): A list of length batch_size, where each element is a tensor of
            shape (num_boxes, 6 + num_masks) containing the kept boxes, with columns
            (x1, y1, x2, y2, confidence, class, mask1, mask2, ...).
    """
    import torchvision  # scope for faster 'import ultralytics'

    # Checks
    assert 0 <= conf_thres <= 1, f"Invalid Confidence threshold {conf_thres}, valid values are between 0.0 and 1.0"
    assert 0 <= iou_thres <= 1, f"Invalid IoU {iou_thres}, valid values are between 0.0 and 1.0"
    if isinstance(prediction, (list, tuple)):  # YOLOv8 model in validation model, output = (inference_out, loss_out)
        prediction = prediction[0]  # select only inference output
    if classes is not None:
        classes = torch.tensor(classes, device=prediction.device)

    if prediction.shape[-1] == 6 or end2end:  # end-to-end model (BNC, i.e. 1,300,6)
        output = [pred[pred[:, 4] > conf_thres][:max_det] for pred in prediction]
        if classes is not None:
            output = [pred[(pred[:, 5:6] == classes).any(1)] for pred in output]
        return output

    bs = prediction.shape[0]  # batch size (BCN, i.e. 1,84,6300)
    nc = nc or (prediction.shape[1] - 4)  # number of classes
    nm = prediction.shape[1] - nc - 4  # number of masks
    mi = 4 + nc  # mask start index
    xc = prediction[:, 4:mi].amax(1) > conf_thres  # candidates

    # Settings
    # min_wh = 2  # (pixels) minimum box width and height
    time_limit = 2.0 + max_time_img * bs  # seconds to quit after
    multi_label &= nc > 1  # multiple labels per box (adds 0.5ms/img)

    prediction = prediction.transpose(-1, -2)  # shape(1,84,6300) to shape(1,6300,84)
    if not rotated:
        if in_place:
            prediction[..., :4] = xywh2xyxy(prediction[..., :4])  # xywh to xyxy
        else:
            prediction = torch.cat((xywh2xyxy(prediction[..., :4]), prediction[..., 4:]), dim=-1)  # xywh to xyxy

    t = time.time()
    output = [torch.zeros((0, 6 + nm), device=prediction.device)] * bs
    for xi, x in enumerate(prediction):  # image index, image inference
        # Apply constraints
        # x[((x[:, 2:4] < min_wh) | (x[:, 2:4] > max_wh)).any(1), 4] = 0  # width-height
        x = x[xc[xi]]  # confidence

        # Cat apriori labels if autolabelling
        if labels and len(labels[xi]) and not rotated:
            lb = labels[xi]
            v = torch.zeros((len(lb), nc + nm + 4), device=x.device)
            v[:, :4] = xywh2xyxy(lb[:, 1:5])  # box
            v[range(len(lb)), lb[:, 0].long() + 4] = 1.0  # cls
            x = torch.cat((x, v), 0)

        # If none remain process next image
        if not x.shape[0]:
            continue

        # Detections matrix nx6 (xyxy, conf, cls)
        box, cls, mask = x.split((4, nc, nm), 1)

        if multi_label:
            i, j = torch.where(cls > conf_thres)
            x = torch.cat((box[i], x[i, 4 + j, None], j[:, None].float(), mask[i]), 1)
        else:  # best class only
            conf, j = cls.max(1, keepdim=True)
            x = torch.cat((box, conf, j.float(), mask), 1)[conf.view(-1) > conf_thres]

        # Filter by class
        if classes is not None:
            x = x[(x[:, 5:6] == classes).any(1)]

        # Check shape
        n = x.shape[0]  # number of boxes
        if not n:  # no boxes
            continue
        if n > max_nms:  # excess boxes
            x = x[x[:, 4].argsort(descending=True)[:max_nms]]  # sort by confidence and remove excess boxes

        # Batched NMS
        c = x[:, 5:6] * (0 if agnostic else max_wh)  # classes
        scores = x[:, 4]  # scores
        if rotated:
            boxes = torch.cat((x[:, :2] + c, x[:, 2:4], x[:, -1:]), dim=-1)  # xywhr
            i = nms_rotated(boxes, scores, iou_thres)
        else:
            boxes = x[:, :4] + c  # boxes (offset by class)
            i = torchvision.ops.nms(boxes, scores, iou_thres)  # NMS
        i = i[:max_det]  # limit detections

        # # Experimental
        # merge = False  # use merge-NMS
        # if merge and (1 < n < 3E3):  # Merge NMS (boxes merged using weighted mean)
        #     # Update boxes as boxes(i,4) = weights(i,n) * boxes(n,4)
        #     from .metrics import box_iou
        #     iou = box_iou(boxes[i], boxes) > iou_thres  # IoU matrix
        #     weights = iou * scores[None]  # box weights
        #     x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True)  # merged boxes
        #     redundant = True  # require redundant detections
        #     if redundant:
        #         i = i[iou.sum(1) > 1]  # require redundancy

        output[xi] = x[i]
        if (time.time() - t) > time_limit:
            LOGGER.warning(f"WARNING ⚠️ NMS time limit {time_limit:.3f}s exceeded")
            break  # time limit exceeded

    return output

核心是一个这个:

 i = torchvision.ops.nms(boxes, scores, iou_thres)  # NMS

ops.scale_boxes

def scale_boxes(img1_shape, boxes, img0_shape, ratio_pad=None, padding=True, xywh=False):
    """
    Rescales bounding boxes (in the format of xyxy by default) from the shape of the image they were originally
    specified in (img1_shape) to the shape of a different image (img0_shape).

    Args:
        img1_shape (tuple): The shape of the image that the bounding boxes are for, in the format of (height, width).
        boxes (torch.Tensor): the bounding boxes of the objects in the image, in the format of (x1, y1, x2, y2)
        img0_shape (tuple): the shape of the target image, in the format of (height, width).
        ratio_pad (tuple): a tuple of (ratio, pad) for scaling the boxes. If not provided, the ratio and pad will be
            calculated based on the size difference between the two images.
        padding (bool): If True, assuming the boxes is based on image augmented by yolo style. If False then do regular
            rescaling.
        xywh (bool): The box format is xywh or not, default=False.

    Returns:
        boxes (torch.Tensor): The scaled bounding boxes, in the format of (x1, y1, x2, y2)
    """
    if ratio_pad is None:  # calculate from img0_shape
        gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1])  # gain  = old / new
        pad = (
            round((img1_shape[1] - img0_shape[1] * gain) / 2 - 0.1),
            round((img1_shape[0] - img0_shape[0] * gain) / 2 - 0.1),
        )  # wh padding
    else:
        gain = ratio_pad[0][0]
        pad = ratio_pad[1]

    if padding:
        boxes[..., 0] -= pad[0]  # x padding
        boxes[..., 1] -= pad[1]  # y padding
        if not xywh:
            boxes[..., 2] -= pad[0]  # x padding
            boxes[..., 3] -= pad[1]  # y padding
    boxes[..., :4] /= gain
    return clip_boxes(boxes, img0_shape)

老东西了,扣除padding后,除以gain,返回到最初的大小。这里有个clip_boxes:

def clip_boxes(boxes, shape):
    """
    Takes a list of bounding boxes and a shape (height, width) and clips the bounding boxes to the shape.

    Args:
        boxes (torch.Tensor): The bounding boxes to clip.
        shape (tuple): The shape of the image.

    Returns:
        (torch.Tensor | numpy.ndarray): The clipped boxes.
    """
    if isinstance(boxes, torch.Tensor):  # faster individually (WARNING: inplace .clamp_() Apple MPS bug)
        boxes[..., 0] = boxes[..., 0].clamp(0, shape[1])  # x1
        boxes[..., 1] = boxes[..., 1].clamp(0, shape[0])  # y1
        boxes[..., 2] = boxes[..., 2].clamp(0, shape[1])  # x2
        boxes[..., 3] = boxes[..., 3].clamp(0, shape[0])  # y2
    else:  # np.array (faster grouped)
        boxes[..., [0, 2]] = boxes[..., [0, 2]].clip(0, shape[1])  # x1, x2
        boxes[..., [1, 3]] = boxes[..., [1, 3]].clip(0, shape[0])  # y1, y2
    return boxes

就是个控制上下界,避免计算错误

--------------------------------------------------------------------------- ImportError Traceback (most recent call last) Cell In[1], line 1 ----> 1 from ultralytics import YOLO File c:\Users\lenovo\miniconda3\envs\yolov8\lib\site-packages\ultralytics\__init__.py:11 8 if not os.environ.get("OMP_NUM_THREADS"): 9 os.environ["OMP_NUM_THREADS"] = "1" # default for reduced CPU utilization during training ---> 11 from ultralytics.models import NAS, RTDETR, SAM, YOLO, YOLOE, FastSAM, YOLOWorld 12 from ultralytics.utils import ASSETS, SETTINGS 13 from ultralytics.utils.checks import check_yolo as checks File c:\Users\lenovo\miniconda3\envs\yolov8\lib\site-packages\ultralytics\models\__init__.py:3 1 # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license ----> 3 from .fastsam import FastSAM 4 from .nas import NAS 5 from .rtdetr import RTDETR File c:\Users\lenovo\miniconda3\envs\yolov8\lib\site-packages\ultralytics\models\fastsam\__init__.py:3 1 # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license ----> 3 from .model import FastSAM 4 from .predict import FastSAMPredictor 5 from .val import FastSAMValidator File c:\Users\lenovo\miniconda3\envs\yolov8\lib\site-packages\ultralytics\models\fastsam\model.py:5 1 # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 3 from pathlib import Path ----> 5 from ultralytics.engine.model import Model 7 from .predict import FastSAMPredictor 8 from .val import FastSAMValidator File c:\Users\lenovo\miniconda3\envs\yolov8\lib\site-packages\ultralytics\engine\model.py:12 9 from PIL import Image 11 from ultralytics.cfg import TASK2DATA, get_cfg, get_save_dir ---> 12 from ultralytics.engine.results import Results 13 from ultralytics.nn.tasks import attempt_load_one_weight, guess_model_task, yaml_model_load 14 from ultralytics.utils import ( 15 ARGV, 16 ASSETS, (...) 23 checks, 24 ) File c:\Users\lenovo\miniconda3\envs\yolov8\lib\site-packages\ultralytics\engine\results.py:15 12 import numpy as np 13 import torch ---> 15 from ultralytics.data.augment import LetterBox 16 from ultralytics.utils import LOGGER, SimpleClass, ops 17 from ultralytics.utils.checks import check_requirements File c:\Users\lenovo\miniconda3\envs\yolov8\lib\site-packages\ultralytics\data\__init__.py:3 1 # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license ----> 3 from .base import BaseDataset 4 from .build import build_dataloader, build_grounding, build_yolo_dataset, load_inference_source 5 from .dataset import ( 6 ClassificationDataset, 7 GroundingDataset, (...) 11 YOLOMultiModalDataset, 12 ) File c:\Users\lenovo\miniconda3\envs\yolov8\lib\site-packages\ultralytics\data\base.py:16 13 import numpy as np 14 from torch.utils.data import Dataset ---> 16 from ultralytics.data.utils import FORMATS_HELP_MSG, HELP_URL, IMG_FORMATS, check_file_speeds 17 from ultralytics.utils import DEFAULT_CFG, LOCAL_RANK, LOGGER, NUM_THREADS, TQDM 18 from ultralytics.utils.patches import imread File c:\Users\lenovo\miniconda3\envs\yolov8\lib\site-packages\ultralytics\data\utils.py:17 14 import numpy as np 15 from PIL import Image, ImageOps ---> 17 from ultralytics.nn.autobackend import check_class_names 18 from ultralytics.utils import ( 19 DATASETS_DIR, 20 LOGGER, (...) 30 is_dir_writeable, 31 ) 32 from ultralytics.utils.checks import check_file, check_font, is_ascii File c:\Users\lenovo\miniconda3\envs\yolov8\lib\site-packages\ultralytics\nn\__init__.py:3 1 # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license ----> 3 from .tasks import ( 4 BaseModel, 5 ClassificationModel, 6 DetectionModel, 7 SegmentationModel, 8 attempt_load_one_weight, 9 attempt_load_weights, 10 guess_model_scale, 11 guess_model_task, 12 parse_model, 13 torch_safe_load, 14 yaml_model_load, 15 ) 17 __all__ = ( 18 "attempt_load_one_weight", 19 "attempt_load_weights", (...) 28 "BaseModel", 29 ) File c:\Users\lenovo\miniconda3\envs\yolov8\lib\site-packages\ultralytics\nn\tasks.py:70 15 from ultralytics.nn.modules import ( 16 AIFI, 17 C1, (...) 67 v10Detect, 68 ) 69 from ultralytics.nn.MHSA import MHSA ---> 70 from ultralytics.utils import DEFAULT_CFG_DICT, DEFAULT_CFG_KEYS, LOGGER, colorstr, emojis, yaml_load 71 from ultralytics.utils.checks import check_requirements, check_suffix, check_yaml 72 from ultralytics.utils.loss import ( 73 E2EDetectLoss, 74 v8ClassificationLoss, (...) 78 v8SegmentationLoss, 79 ) ImportError: cannot import name 'yaml_load' from 'ultralytics.utils' (c:\Users\lenovo\miniconda3\envs\yolov8\lib\site-packages\ultralytics\utils\__init__.py)
05-15
(yolov5_new) PS E:\YOLO\ultralytics-yolo11-main> python train.py WARNING ⚠️ no model scale passed. Assuming scale='n'. New https://pypi.org/project/ultralytics/8.3.225 available 😃 Update with 'pip install -U ultralytics' Ultralytics 8.3.9 🚀 Python-3.8.18 torch-2.0.1+cu117 CUDA:0 (NVIDIA GeForce RTX 4060 Laptop GPU, 8188MiB) engine\trainer: task=detect, mode=train, model=ultralytics/cfg/models/11/yolo11-HGNetV2_1.yaml, data=datase t/data.yaml, epochs=2, time=None, patience=100, batch=32, imgsz=640, save=True, save_period=-1, cache=False , device=None, workers=0, project=runs/train, name=exp3, exist_ok=False, pretrained=True, optimizer=SGD, ve rbose=True, seed=0, deterministic=True, single_cls=False, rect=False, cos_lr=False, close_mosaic=0, resume= False, amp=True, fraction=1.0, profile=False, freeze=None, multi_scale=False, overlap_mask=True, mask_ratio =4, dropout=0.0, val=True, split=val, save_json=False, save_hybrid=False, conf=None, iou=0.7, max_det=300, half=False, dnn=False, plots=True, source=None, vid_stride=1, stream_buffer=False, visualize=False, augment =False, agnostic_nms=False, classes=None, retina_masks=False, embed=None, show=False, save_frames=False, sa ve_txt=False, save_conf=False, save_crop=False, show_labels=True, show_conf=True, show_boxes=True, line_wid th=None, format=torchscript, keras=False, optimize=False, int8=False, dynamic=False, simplify=True, opset=N one, workspace=4, nms=False, lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, wa rmup_momentum=0.8, warmup_bias_lr=0.1, box=7.5, cls=0.5, dfl=1.5, pose=12.0, kobj=1.0, label_smoothing=0.0, nbs=64, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0 .0, flipud=0.0, fliplr=0.5, bgr=0.0, mosaic=1.0, mixup=0.0, copy_paste=0.0, copy_paste_mode=flip, auto_augment=randaugment, erasing=0.4, crop_fraction=1.0, cfg=None, tracker=botsort.yaml, save_dir=runs\train\exp3 Overriding model.yaml nc=80 with nc=3 WARNING ⚠️ no model scale passed. Assuming scale='n'. from n params module arguments 0 -1 1 1840 ultralytics.nn.modules.block.HGStem [3, 8, 16] 1 -1 3 5784 ultralytics.nn.modules.Light_HGNet.Light_HGBlock[16, 16, 32, 3, 3] 2 -1 1 352 ultralytics.nn.modules.conv.DWConv [32, 32, 3, 2, 1, False] 3 -1 3 24916 ultralytics.nn.modules.Light_HGNet.Light_HGBlock[32, 24, 128, 3, 3] 4 -1 1 1408 ultralytics.nn.modules.conv.DWConv [128, 128, 3, 2, 1, False] 5 -1 3 80640 ultralytics.nn.modules.Light_HGNet.Light_HGBlock[128, 48, 256, 5, 3, 3, False] 6 -1 3 106624 ultralytics.nn.modules.Light_HGNet.Light_HGBlock[256, 48, 256, 5, 3, 3, True] 7 -1 3 106624 ultralytics.nn.modules.Light_HGNet.Light_HGBlock[256, 48, 256, 5, 3, 3, True] 8 -1 1 2816 ultralytics.nn.modules.conv.DWConv [256, 256, 3, 2, 1, False] 9 -1 3 127744 ultralytics.nn.modules.Light_HGNet.Light_HGBlock[256, 96, 256, 5, 3, 3, False] 10 -1 1 164608 ultralytics.nn.modules.block.SPPF [256, 256, 5] 11 -1 1 249728 ultralytics.nn.modules.block.C2PSA [256, 256, 1] 12 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 13 [-1, 7] 1 0 ultralytics.nn.modules.conv.Concat [1] 14 -1 1 127680 ultralytics.nn.modules.block.C3k2 [512, 128, 1, False] 15 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 16 [-1, 3] 1 0 ultralytics.nn.modules.conv.Concat [1] 17 -1 1 32096 ultralytics.nn.modules.block.C3k2 [256, 64, 1, False] 18 -1 1 36992 ultralytics.nn.modules.conv.Conv [64, 64, 3, 2] 19 [-1, 14] 1 0 ultralytics.nn.modules.conv.Concat [1] 20 -1 1 86720 ultralytics.nn.modules.block.C3k2 [192, 128, 1, False] 21 -1 1 147712 ultralytics.nn.modules.conv.Conv [128, 128, 3, 2] 22 [-1, 11] 1 0 ultralytics.nn.modules.conv.Concat [1] 23 -1 1 378880 ultralytics.nn.modules.block.C3k2 [384, 256, 1, True] 24 [17, 20, 23] 1 431257 ultralytics.nn.modules.head.Detect [3, [64, 128, 256]] YOLO11-HGNetV2_1 summary: 368 layers, 2,114,421 parameters, 2,114,405 gradients, 5.6 GFLOPs TensorBoard: Start with 'tensorboard --logdir runs\train\exp3', view at http://localhost:6006/ Freezing layer 'model.24.dfl.conv.weight' AMP: running Automatic Mixed Precision (AMP) checks with YOLO11n... Traceback (most recent call last): File "train.py", line 32, in <module> model.train(data='dataset/data.yaml', File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\model.py", line 802, in train self.trainer.train() File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\trainer.py", line 208, in train self._do_train(world_size) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\trainer.py", line 328, in _do_train self._setup_train(world_size) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\trainer.py", line 265, in _setup_train self.amp = torch.tensor(check_amp(self.model), device=self.device) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\utils\checks.py", line 695, in check_amp assert amp_allclose(YOLO("yolo11n.pt"), im) return self.predict(source, stream, **kwargs) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\model.py", line 554, in predict return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\predictor.py", line 168, in __call__ return list(self.stream_inference(source, model, *args, **kwargs)) # merge list of Result into one File "C:\Users\JJCHAN\.conda\envs\yolov5_new\lib\site-packages\torch\utils\_contextlib.py", line 35, in generator_context response = gen.send(None) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\predictor.py", line 261, in stream_inference self.results = self.postprocess(preds, im, im0s) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\models\yolo\detect\predict.py", line 25, in postprocess preds = ops.non_max_suppression( File "E:\YOLO\ultralytics-yolo11-main\ultralytics\utils\ops.py", line 378, in non_max_suppression x = x[xc[xi]] # confidence IndexError: The shape of the mask [32, 24] at index 0 does not match the shape of the indexed tensor [64, 24, 32] at index 0 (yolov5_new) PS E:\YOLO\ultralytics-yolo11-main> python train.py WARNING ⚠️ no model scale passed. Assuming scale='n'. New https://pypi.org/project/ultralytics/8.3.225 available 😃 Update with 'pip install -U ultralytics' Ultralytics 8.3.9 🚀 Python-3.8.18 torch-2.0.1+cu117 CUDA:0 (NVIDIA GeForce RTX 4060 Laptop GPU, 8188MiB) engine\trainer: task=detect, mode=train, model=ultralytics/cfg/models/11/yolo11-HGNetV2_1.yaml, data=dataset/data.yaml, epochs=2, time=None, patience=100, batch=32, imgsz=640, save=True, save_period=-1, cache=False, device=None, workers=0, project=runs/train, name=exp4, exist_ok=False, pretrained=True, opti mizer=SGD, verbose=True, seed=0, deterministic=True, single_cls=False, rect=False, cos_lr=False, close_mosaic=0, resume=False, amp=True, fraction=1.0, profile=False, freeze=None, multi_scale=False, overlap_mask=True, mask_ratio=4, dropout=0.0, val=True, split=val, save_json=False, save_hybrid=False, conf=No ne, iou=0.7, max_det=300, half=False, dnn=False, plots=True, source=None, vid_stride=1, stream_buffer=False, visualize=False, augment=False, agnostic_nms=False, classes=None, retina_masks=False, embed=None, show=False, save_frames=False, save_txt=False, save_conf=False, save_crop=False, show_labels=True, sh ow_conf=True, show_boxes=True, line_width=None, format=torchscript, keras=False, optimize=False, int8=False, dynamic=False, simplify=True, opset=None, workspace=4, nms=False, lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=7.5, cls=0.5 , dfl=1.5, pose=12.0, kobj=1.0, label_smoothing=0.0, nbs=64, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, bgr=0.0, mosaic=1.0, mixup=0.0, copy_paste=0.0, copy_paste_mode=flip, auto_augment=randaugment, erasing=0.4, crop_fraction=1.0, cfg=None, tracker=botsort.yaml, save_dir=runs\train\exp4 Overriding model.yaml nc=80 with nc=3 WARNING ⚠️ no model scale passed. Assuming scale='n'. from n params module arguments 0 -1 1 1840 ultralytics.nn.modules.block.HGStem [3, 8, 16] 1 -1 3 5784 ultralytics.nn.modules.Light_HGNet.Light_HGBlock[16, 16, 32, 3, 3] 2 -1 1 352 ultralytics.nn.modules.conv.DWConv [32, 32, 3, 2, 1, False] 3 -1 3 24916 ultralytics.nn.modules.Light_HGNet.Light_HGBlock[32, 24, 128, 3, 3] 4 -1 1 1408 ultralytics.nn.modules.conv.DWConv [128, 128, 3, 2, 1, False] 5 -1 3 80640 ultralytics.nn.modules.Light_HGNet.Light_HGBlock[128, 48, 256, 5, 3, 3, False] 6 -1 3 106624 ultralytics.nn.modules.Light_HGNet.Light_HGBlock[256, 48, 256, 5, 3, 3, True] 7 -1 3 106624 ultralytics.nn.modules.Light_HGNet.Light_HGBlock[256, 48, 256, 5, 3, 3, True] 8 -1 1 2816 ultralytics.nn.modules.conv.DWConv [256, 256, 3, 2, 1, False] 9 -1 3 127744 ultralytics.nn.modules.Light_HGNet.Light_HGBlock[256, 96, 256, 5, 3, 3, False] 10 -1 1 164608 ultralytics.nn.modules.block.SPPF [256, 256, 5] 11 -1 1 249728 ultralytics.nn.modules.block.C2PSA [256, 256, 1] 12 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 13 [-1, 7] 1 0 ultralytics.nn.modules.conv.Concat [1] 14 -1 1 127680 ultralytics.nn.modules.block.C3k2 [512, 128, 1, False] 15 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 16 [-1, 3] 1 0 ultralytics.nn.modules.conv.Concat [1] 17 -1 1 32096 ultralytics.nn.modules.block.C3k2 [256, 64, 1, False] 18 -1 1 36992 ultralytics.nn.modules.conv.Conv [64, 64, 3, 2] 19 [-1, 14] 1 0 ultralytics.nn.modules.conv.Concat [1] 20 -1 1 86720 ultralytics.nn.modules.block.C3k2 [192, 128, 1, False] 21 -1 1 147712 ultralytics.nn.modules.conv.Conv [128, 128, 3, 2] 22 [-1, 11] 1 0 ultralytics.nn.modules.conv.Concat [1] 23 -1 1 378880 ultralytics.nn.modules.block.C3k2 [384, 256, 1, True] 24 [17, 20, 23] 1 431257 ultralytics.nn.modules.head.Detect [3, [64, 128, 256]] YOLO11-HGNetV2_1 summary: 368 layers, 2,114,421 parameters, 2,114,405 gradients, 5.6 GFLOPs TensorBoard: Start with 'tensorboard --logdir runs\train\exp4', view at http://localhost:6006/ Freezing layer 'model.24.dfl.conv.weight' AMP: running Automatic Mixed Precision (AMP) checks with YOLO11n... Traceback (most recent call last): File "train.py", line 32, in <module> model.train(data='dataset/data.yaml', File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\model.py", line 802, in train self.trainer.train() File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\trainer.py", line 208, in train self._do_train(world_size) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\trainer.py", line 328, in _do_train self._setup_train(world_size) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\trainer.py", line 265, in _setup_train self.amp = torch.tensor(check_amp(self.model), device=self.device) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\utils\checks.py", line 695, in check_amp assert amp_allclose(YOLO("yolo11n.pt"), im) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\utils\checks.py", line 682, in amp_allclose a = m(batch, imgsz=imgsz, device=device, verbose=False)[0].boxes.data # FP32 inference File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\model.py", line 176, in __call__ return self.predict(source, stream, **kwargs) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\model.py", line 554, in predict return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\predictor.py", line 168, in __call__ return list(self.stream_inference(source, model, *args, **kwargs)) # merge list of Result into one File "C:\Users\JJCHAN\.conda\envs\yolov5_new\lib\site-packages\torch\utils\_contextlib.py", line 35, in generator_context response = gen.send(None) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\engine\predictor.py", line 261, in stream_inference self.results = self.postprocess(preds, im, im0s) File "E:\YOLO\ultralytics-yolo11-main\ultralytics\models\yolo\detect\predict.py", line 25, in postprocess preds = ops.non_max_suppression( File "E:\YOLO\ultralytics-yolo11-main\ultralytics\utils\ops.py", line 378, in non_max_suppression x = x[xc[xi]] # confidence IndexError: The shape of the mask [32, 24] at index 0 does not match the shape of the indexed tensor [64, 24, 32] at index 0
11-07
D:\miniconda\envs\yolo8\python.exe C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\train_v8.py WARNING no model scale passed. Assuming scale='n'. from n params module arguments 0 -1 1 464 ultralytics.nn.modules.conv.Conv [3, 16, 3, 2] 1 -1 1 2470 ultralytics.nn.modules.AKConv.AKConv [16, 32, 3, 2] 2 -1 1 7360 ultralytics.nn.modules.block.C2f [32, 32, 1, True] 3 -1 1 8006 ultralytics.nn.modules.AKConv.AKConv [32, 64, 3, 2] 4 -1 2 49664 ultralytics.nn.modules.block.C2f [64, 64, 2, True] 5 -1 1 28294 ultralytics.nn.modules.AKConv.AKConv [64, 128, 3, 2] 6 -1 2 197632 ultralytics.nn.modules.block.C2f [128, 128, 2, True] 7 -1 1 105734 ultralytics.nn.modules.AKConv.AKConv [128, 256, 3, 2] 8 -1 1 460288 ultralytics.nn.modules.block.C2f [256, 256, 1, True] 9 -1 1 164608 ultralytics.nn.modules.block.SPPF [256, 256, 5] 10 -1 1 0 ultralytics.nn.SimAM.SimAM [1024] 11 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 12 [-1, 6] 1 0 ultralytics.nn.modules.conv.Concat [1] 13 -1 1 148224 ultralytics.nn.modules.block.C2f [384, 128, 1] 14 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 15 [-1, 4] 1 0 ultralytics.nn.modules.conv.Concat [1] 16 -1 1 37248 ultralytics.nn.modules.block.C2f [192, 64, 1] 17 -1 1 15878 ultralytics.nn.modules.AKConv.AKConv [64, 64, 3, 2] 18 [-1, 12] 1 0 ultralytics.nn.modules.conv.Concat [1] 19 -1 1 156416 ultralytics.nn.modules.block.C2f [448, 128, 1] 20 -1 1 56326 ultralytics.nn.modules.AKConv.AKConv [128, 128, 3, 2] 21 [-1, 9] 1 0 ultralytics.nn.modules.conv.Concat [1] 22 -1 1 493056 ultralytics.nn.modules.block.C2f [384, 256, 1] 23 [15, 18, 21] 1 3481363 ultralytics.nn.modules.head.Detect [1, [192, 448, 384]] D:\miniconda\envs\yolo8\lib\site-packages\torch\functional.py:534: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\aten\src\ATen\native\TensorShape.cpp:3596.) return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined] AKn summary: 245 layers, 5413031 parameters, 5413015 gradients AKn summary: 245 layers, 5413031 parameters, 5413015 gradients New https://pypi.org/project/ultralytics/8.3.203 available Update with 'pip install -U ultralytics' Ultralytics YOLOv8.0.151 Python-3.10.0 torch-2.5.1+cu118 CUDA:0 (NVIDIA GeForce RTX 3080 Laptop GPU, 16383MiB) engine\trainer: task=detect, mode=train, model=ultralytics/cfg/models/v8/AKn.yaml, data=mosquito.yaml, epochs=500, patience=50, batch=64, imgsz=640, save=True, save_period=-1, cache=False, device=None, workers=8, project=runs, name=AKConv+simam, exist_ok=False, pretrained=True, optimizer=auto, verbose=True, seed=0, deterministic=True, single_cls=False, rect=False, cos_lr=False, close_mosaic=10, resume=False, amp=True, fraction=1.0, profile=False, overlap_mask=True, mask_ratio=4, dropout=0.0, val=True, split=val, save_json=False, save_hybrid=False, conf=None, iou=0.7, max_det=300, half=False, dnn=False, plots=True, source=None, show=False, save_txt=False, save_conf=False, save_crop=False, show_labels=True, show_conf=True, vid_stride=1, line_width=None, visualize=False, augment=False, agnostic_nms=False, classes=None, retina_masks=False, boxes=True, format=rknn, keras=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=None, workspace=4, nms=False, lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=7.5, cls=0.5, dfl=1.5, pose=12.0, kobj=1.0, label_smoothing=0.0, nbs=64, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0, cfg=None, tracker=botsort.yaml, save_dir=runs\AKConv+simam3 WARNING no model scale passed. Assuming scale='n'. from n params module arguments 0 -1 1 464 ultralytics.nn.modules.conv.Conv [3, 16, 3, 2] 1 -1 1 2470 ultralytics.nn.modules.AKConv.AKConv [16, 32, 3, 2] 2 -1 1 7360 ultralytics.nn.modules.block.C2f [32, 32, 1, True] 3 -1 1 8006 ultralytics.nn.modules.AKConv.AKConv [32, 64, 3, 2] 4 -1 2 49664 ultralytics.nn.modules.block.C2f [64, 64, 2, True] 5 -1 1 28294 ultralytics.nn.modules.AKConv.AKConv [64, 128, 3, 2] 6 -1 2 197632 ultralytics.nn.modules.block.C2f [128, 128, 2, True] 7 -1 1 105734 ultralytics.nn.modules.AKConv.AKConv [128, 256, 3, 2] 8 -1 1 460288 ultralytics.nn.modules.block.C2f [256, 256, 1, True] 9 -1 1 164608 ultralytics.nn.modules.block.SPPF [256, 256, 5] 10 -1 1 0 ultralytics.nn.SimAM.SimAM [1024] 11 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 12 [-1, 6] 1 0 ultralytics.nn.modules.conv.Concat [1] 13 -1 1 148224 ultralytics.nn.modules.block.C2f [384, 128, 1] 14 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 15 [-1, 4] 1 0 ultralytics.nn.modules.conv.Concat [1] 16 -1 1 37248 ultralytics.nn.modules.block.C2f [192, 64, 1] 17 -1 1 15878 ultralytics.nn.modules.AKConv.AKConv [64, 64, 3, 2] 18 [-1, 12] 1 0 ultralytics.nn.modules.conv.Concat [1] 19 -1 1 156416 ultralytics.nn.modules.block.C2f [448, 128, 1] 20 -1 1 56326 ultralytics.nn.modules.AKConv.AKConv [128, 128, 3, 2] 21 [-1, 9] 1 0 ultralytics.nn.modules.conv.Concat [1] 22 -1 1 493056 ultralytics.nn.modules.block.C2f [384, 256, 1] 23 [15, 18, 21] 1 3481363 ultralytics.nn.modules.head.Detect [1, [192, 448, 384]] AKn summary: 245 layers, 5413031 parameters, 5413015 gradients AMP: running Automatic Mixed Precision (AMP) checks with YOLOv8n... C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\ultralytics\nn\tasks.py:565: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature. return torch.load(file, map_location='cpu'), file # load [ WARN:0@10.900] global loadsave.cpp:268 cv::findDecoder imread_('C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\ultralytics\assets\bus.jpg'): can't open/read file: check file path/integrity Traceback (most recent call last): File "C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\train_v8.py", line 36, in <module> main(opt) File "C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\train_v8.py", line 17, in main results = model.train(data='mosquito.yaml', # 训练参数均可以重新设置 File "C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\ultralytics\engine\model.py", line 377, in train self.trainer.train() File "C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\ultralytics\engine\trainer.py", line 192, in train self._do_train(world_size) File "C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\ultralytics\engine\trainer.py", line 276, in _do_train self._setup_train(world_size) File "C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\ultralytics\engine\trainer.py", line 219, in _setup_train self.amp = torch.tensor(check_amp(self.model), device=self.device) File "C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\ultralytics\utils\checks.py", line 465, in check_amp assert amp_allclose(YOLO('yolov8n.pt'), im) File "C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\ultralytics\utils\checks.py", line 452, in amp_allclose a = m(im, device=device, verbose=False)[0].boxes.data # FP32 inference File "C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\ultralytics\engine\model.py", line 98, in __call__ return self.predict(source, stream, **kwargs) File "D:\miniconda\envs\yolo8\lib\site-packages\torch\utils\_contextlib.py", line 116, in decorate_context return func(*args, **kwargs) File "C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\ultralytics\engine\model.py", line 246, in predict return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream) File "C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\ultralytics\engine\predictor.py", line 197, in __call__ return list(self.stream_inference(source, model, *args, **kwargs)) # merge list of Result into one File "D:\miniconda\envs\yolo8\lib\site-packages\torch\utils\_contextlib.py", line 36, in generator_context response = gen.send(None) File "C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\ultralytics\engine\predictor.py", line 242, in stream_inference for batch in self.dataset: File "C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\ultralytics\data\loaders.py", line 240, in __next__ raise FileNotFoundError(f'Image Not Found {path}') FileNotFoundError: Image Not Found C:\Users\Administrator\Desktop\正原子rk3588\2.pt转onnx\ultralytics_yolov8-rk_opt_v1.6\ultralytics\assets\bus.jpg 进程已结束,退出代码为 1
09-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万物琴弦光锥之外

给个0.1,恭喜老板发财

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值