diff --git a/examples/RTDETR-ONNXRuntime-Python/main.py b/examples/RTDETR-ONNXRuntime-Python/main.py index 34e67fce..36882371 100644 --- a/examples/RTDETR-ONNXRuntime-Python/main.py +++ b/examples/RTDETR-ONNXRuntime-Python/main.py @@ -49,7 +49,7 @@ class RTDETR: model_input (list): Model input metadata. input_width (int): Width dimension required by the model. input_height (int): Height dimension required by the model. - classes (List[str]): List of class names from COCO dataset. + classes (list[str]): List of class names from COCO dataset. color_palette (np.ndarray): Random color palette for visualization. img (np.ndarray): Loaded input image. img_height (int): Height of the input image. @@ -221,7 +221,7 @@ class RTDETR: dimensions, and draws detection annotations. Args: - model_output (List[np.ndarray]): Output tensors from the model inference. + model_output (list[np.ndarray]): Output tensors from the model inference. Returns: (np.ndarray): Annotated image with detection bounding boxes and labels. diff --git a/examples/YOLO-Interactive-Tracking-UI/interactive_tracker.py b/examples/YOLO-Interactive-Tracking-UI/interactive_tracker.py index f82ecf33..72293d85 100644 --- a/examples/YOLO-Interactive-Tracking-UI/interactive_tracker.py +++ b/examples/YOLO-Interactive-Tracking-UI/interactive_tracker.py @@ -79,7 +79,7 @@ def extend_line_from_edge(mid_x: int, mid_y: int, direction: str, img_shape: tup mid_x (int): X-coordinate of the midpoint. mid_y (int): Y-coordinate of the midpoint. direction (str): Direction to extend ('left', 'right', 'up', 'down'). - img_shape (Tuple[int, int, int]): Image shape in (height, width, channels). + img_shape (tuple[int, int, int]): Image shape in (height, width, channels). Returns: end_x (int): X-coordinate of the endpoint. diff --git a/examples/YOLOv8-Action-Recognition/action_recognition.py b/examples/YOLOv8-Action-Recognition/action_recognition.py index 705d46e0..ba232b20 100644 --- a/examples/YOLOv8-Action-Recognition/action_recognition.py +++ b/examples/YOLOv8-Action-Recognition/action_recognition.py @@ -91,7 +91,7 @@ class TorchVisionVideoClassifier: Get the list of available model names. Returns: - (List[str]): List of available model names that can be used with this classifier. + (list[str]): List of available model names that can be used with this classifier. """ return list(TorchVisionVideoClassifier.model_name_to_model_and_weights.keys()) @@ -100,8 +100,8 @@ class TorchVisionVideoClassifier: Preprocess a list of crops for video classification. Args: - crops (List[np.ndarray]): List of crops to preprocess. Each crop should have dimensions (H, W, C). - input_size (List[int], optional): The target input size for the model. + crops (list[np.ndarray]): List of crops to preprocess. Each crop should have dimensions (H, W, C). + input_size (list[int], optional): The target input size for the model. Returns: (torch.Tensor): Preprocessed crops as a tensor with dimensions (1, T, C, H, W). @@ -143,8 +143,8 @@ class TorchVisionVideoClassifier: outputs (torch.Tensor): The model's output logits. Returns: - pred_labels (List[str]): The predicted labels. - pred_confs (List[float]): The predicted confidences. + pred_labels (list[str]): The predicted labels. + pred_confs (list[float]): The predicted confidences. """ pred_labels = [] pred_confs = [] @@ -167,7 +167,7 @@ class HuggingFaceVideoClassifier: Attributes: fp16 (bool): Whether to use FP16 for inference. - labels (List[str]): List of labels for zero-shot classification. + labels (list[str]): List of labels for zero-shot classification. device (torch.device): The device on which the model is loaded. processor (transformers.AutoProcessor): The processor for the model. model (transformers.AutoModel): The loaded Hugging Face model. @@ -197,7 +197,7 @@ class HuggingFaceVideoClassifier: Initialize the HuggingFaceVideoClassifier with the specified model name. Args: - labels (List[str]): List of labels for zero-shot classification. + labels (list[str]): List of labels for zero-shot classification. model_name (str): The name of the model to use. device (str | torch.device): The device to run the model on. fp16 (bool): Whether to use FP16 for inference. @@ -216,8 +216,8 @@ class HuggingFaceVideoClassifier: Preprocess a list of crops for video classification. Args: - crops (List[np.ndarray]): List of crops to preprocess. Each crop should have dimensions (H, W, C). - input_size (List[int], optional): The target input size for the model. + crops (list[np.ndarray]): List of crops to preprocess. Each crop should have dimensions (H, W, C). + input_size (list[int], optional): The target input size for the model. Returns: (torch.Tensor): Preprocessed crops as a tensor with dimensions (1, T, C, H, W). @@ -269,8 +269,8 @@ class HuggingFaceVideoClassifier: outputs (torch.Tensor): The model's output logits. Returns: - pred_labels (List[List[str]]): The predicted top2 labels for each sample. - pred_confs (List[List[float]]): The predicted top2 confidences for each sample. + pred_labels (list[list[str]]): The predicted top2 labels for each sample. + pred_confs (list[list[float]]): The predicted top2 confidences for each sample. """ pred_labels = [] pred_confs = [] @@ -295,7 +295,7 @@ def crop_and_pad(frame: np.ndarray, box: list[float], margin_percent: int) -> np Args: frame (np.ndarray): The input frame to crop from. - box (List[float]): The bounding box coordinates [x1, y1, x2, y2]. + box (list[float]): The bounding box coordinates [x1, y1, x2, y2]. margin_percent (int): The percentage of margin to add around the box. Returns: @@ -348,7 +348,7 @@ def run( video_cls_overlap_ratio (float): Overlap ratio between video sequences. fp16 (bool): Whether to use half-precision floating point. video_classifier_model (str): Name or path of the video classifier model. - labels (List[str], optional): List of labels for zero-shot classification. + labels (list[str], optional): List of labels for zero-shot classification. """ if labels is None: labels = [ diff --git a/examples/YOLOv8-ONNXRuntime/main.py b/examples/YOLOv8-ONNXRuntime/main.py index fc367a5c..3669474a 100644 --- a/examples/YOLOv8-ONNXRuntime/main.py +++ b/examples/YOLOv8-ONNXRuntime/main.py @@ -25,7 +25,7 @@ class YOLOv8: input_image (str): Path to the input image file. confidence_thres (float): Confidence threshold for filtering detections. iou_thres (float): IoU threshold for non-maximum suppression. - classes (List[str]): List of class names from the COCO dataset. + classes (list[str]): List of class names from the COCO dataset. color_palette (np.ndarray): Random color palette for visualizing different classes. input_width (int): Width dimension of the model input. input_height (int): Height dimension of the model input. @@ -73,11 +73,11 @@ class YOLOv8: Args: img (np.ndarray): Input image to be resized. - new_shape (Tuple[int, int]): Target shape (height, width) for the image. + new_shape (tuple[int, int]): Target shape (height, width) for the image. Returns: img (np.ndarray): Resized and padded image. - pad (Tuple[int, int]): Padding values (top, left) applied to the image. + pad (tuple[int, int]): Padding values (top, left) applied to the image. """ shape = img.shape[:2] # current shape [height, width] @@ -134,7 +134,7 @@ class YOLOv8: Returns: image_data (np.ndarray): Preprocessed image data ready for inference with shape (1, 3, height, width). - pad (Tuple[int, int]): Padding values (top, left) applied during letterboxing. + pad (tuple[int, int]): Padding values (top, left) applied during letterboxing. """ # Read the input image using OpenCV self.img = cv2.imread(self.input_image) @@ -168,8 +168,8 @@ class YOLOv8: Args: input_image (np.ndarray): The input image. - output (List[np.ndarray]): The output arrays from the model. - pad (Tuple[int, int]): Padding values (top, left) used during letterboxing. + output (list[np.ndarray]): The output arrays from the model. + pad (tuple[int, int]): Padding values (top, left) used during letterboxing. Returns: (np.ndarray): The input image with detections drawn on it. diff --git a/examples/YOLOv8-OpenCV-ONNX-Python/main.py b/examples/YOLOv8-OpenCV-ONNX-Python/main.py index 13916841..fe6df996 100644 --- a/examples/YOLOv8-OpenCV-ONNX-Python/main.py +++ b/examples/YOLOv8-OpenCV-ONNX-Python/main.py @@ -45,7 +45,7 @@ def main(onnx_model: str, input_image: str) -> list[dict[str, Any]]: input_image (str): Path to the input image. Returns: - (List[Dict[str, Any]]): List of dictionaries containing detection information such as class_id, class_name, + (list[dict[str, Any]]): List of dictionaries containing detection information such as class_id, class_name, confidence, box coordinates, and scale factor. """ # Load the ONNX model diff --git a/examples/YOLOv8-Region-Counter/yolov8_region_counter.py b/examples/YOLOv8-Region-Counter/yolov8_region_counter.py index e441da6d..48b612cd 100644 --- a/examples/YOLOv8-Region-Counter/yolov8_region_counter.py +++ b/examples/YOLOv8-Region-Counter/yolov8_region_counter.py @@ -111,7 +111,7 @@ def run( view_img (bool): Display results in a live window. save_img (bool): Save processed video to file. exist_ok (bool): Overwrite existing output files without incrementing. - classes (List[int], optional): Specific class IDs to detect and track. + classes (list[int], optional): Specific class IDs to detect and track. line_thickness (int): Thickness of bounding box lines. track_thickness (int): Thickness of object tracking lines. region_thickness (int): Thickness of counting region boundaries. diff --git a/examples/YOLOv8-Segmentation-ONNXRuntime-Python/main.py b/examples/YOLOv8-Segmentation-ONNXRuntime-Python/main.py index edab4b92..7d9212a4 100644 --- a/examples/YOLOv8-Segmentation-ONNXRuntime-Python/main.py +++ b/examples/YOLOv8-Segmentation-ONNXRuntime-Python/main.py @@ -24,7 +24,7 @@ class YOLOv8Seg: Attributes: session (ort.InferenceSession): ONNX Runtime inference session for model execution. - imgsz (Tuple[int, int]): Input image size as (height, width) for the model. + imgsz (tuple[int, int]): Input image size as (height, width) for the model. classes (dict): Dictionary mapping class indices to class names from the dataset. conf (float): Confidence threshold for filtering detections. iou (float): IoU threshold used by non-maximum suppression. @@ -50,7 +50,7 @@ class YOLOv8Seg: onnx_model (str): Path to the ONNX model file. conf (float, optional): Confidence threshold for filtering detections. iou (float, optional): IoU threshold for non-maximum suppression. - imgsz (int | Tuple[int, int], optional): Input image size of the model. Can be an integer for square + imgsz (int | tuple[int, int], optional): Input image size of the model. Can be an integer for square input or a tuple for rectangular input. """ self.session = ort.InferenceSession( @@ -73,7 +73,7 @@ class YOLOv8Seg: img (np.ndarray): The original input image in BGR format. Returns: - (List[Results]): Processed detection results after post-processing, containing bounding boxes and + (list[Results]): Processed detection results after post-processing, containing bounding boxes and segmentation masks. """ prep_img = self.preprocess(img, self.imgsz) @@ -86,7 +86,7 @@ class YOLOv8Seg: Args: img (np.ndarray): Input image in BGR format. - new_shape (Tuple[int, int], optional): Target shape as (height, width). + new_shape (tuple[int, int], optional): Target shape as (height, width). Returns: (np.ndarray): Resized and padded image. @@ -114,7 +114,7 @@ class YOLOv8Seg: Args: img (np.ndarray): The input image in BGR format. - new_shape (Tuple[int, int]): The target shape for resizing as (height, width). + new_shape (tuple[int, int]): The target shape for resizing as (height, width). Returns: (np.ndarray): Preprocessed image ready for model inference, with shape (1, 3, height, width) and @@ -133,10 +133,10 @@ class YOLOv8Seg: Args: img (np.ndarray): The original input image. prep_img (np.ndarray): The preprocessed image used for inference. - outs (List): Model outputs containing predictions and prototype masks. + outs (list): Model outputs containing predictions and prototype masks. Returns: - (List[Results]): Processed detection results containing bounding boxes and segmentation masks. + (list[Results]): Processed detection results containing bounding boxes and segmentation masks. """ preds, protos = (torch.from_numpy(p) for p in outs) preds = nms.non_max_suppression(preds, self.conf, self.iou, nc=len(self.classes)) @@ -160,7 +160,7 @@ class YOLOv8Seg: masks_in (torch.Tensor): Predicted mask coefficients with shape (N, mask_dim), where N is number of detections. bboxes (torch.Tensor): Bounding boxes with shape (N, 4), where N is number of detections. - shape (Tuple[int, int]): The size of the input image as (height, width). + shape (tuple[int, int]): The size of the input image as (height, width). Returns: (torch.Tensor): Binary segmentation masks with shape (N, height, width). diff --git a/examples/YOLOv8-TFLite-Python/main.py b/examples/YOLOv8-TFLite-Python/main.py index 1d22424e..c8265220 100644 --- a/examples/YOLOv8-TFLite-Python/main.py +++ b/examples/YOLOv8-TFLite-Python/main.py @@ -99,11 +99,11 @@ class YOLOv8TFLite: Args: img (np.ndarray): Input image with shape (H, W, C). - new_shape (Tuple[int, int]): Target shape (height, width). + new_shape (tuple[int, int]): Target shape (height, width). Returns: (np.ndarray): Resized and padded image. - (Tuple[float, float]): Padding ratios (top/height, left/width) for coordinate adjustment. + (tuple[float, float]): Padding ratios (top/height, left/width) for coordinate adjustment. """ shape = img.shape[:2] # Current shape [height, width] @@ -169,7 +169,7 @@ class YOLOv8TFLite: Returns: (np.ndarray): Preprocessed image ready for model input. - (Tuple[float, float]): Padding ratios for coordinate adjustment. + (tuple[float, float]): Padding ratios for coordinate adjustment. """ img, pad = self.letterbox(img, (self.in_width, self.in_height)) img = img[..., ::-1][None] # BGR to RGB and add batch dimension (N, H, W, C) for TFLite @@ -184,7 +184,7 @@ class YOLOv8TFLite: Args: img (np.ndarray): The original input image. outputs (np.ndarray): Raw model outputs. - pad (Tuple[float, float]): Padding ratios from preprocessing. + pad (tuple[float, float]): Padding ratios from preprocessing. Returns: (np.ndarray): The input image with detections drawn on it. diff --git a/tests/test_python.py b/tests/test_python.py index 732b55b4..dc1900b7 100644 --- a/tests/test_python.py +++ b/tests/test_python.py @@ -270,7 +270,7 @@ def test_predict_callback_and_setup(): path, im0s, _ = predictor.batch im0s = im0s if isinstance(im0s, list) else [im0s] bs = [predictor.dataset.bs for _ in range(len(path))] - predictor.results = zip(predictor.results, im0s, bs) # results is List[batch_size] + predictor.results = zip(predictor.results, im0s, bs) # results is list[batch_size] model = YOLO(MODEL) model.add_callback("on_predict_batch_end", on_predict_batch_end) diff --git a/ultralytics/cfg/__init__.py b/ultralytics/cfg/__init__.py index 1c50c3f2..0cc1cb14 100644 --- a/ultralytics/cfg/__init__.py +++ b/ultralytics/cfg/__init__.py @@ -246,7 +246,7 @@ def cfg2dict(cfg: str | Path | dict | SimpleNamespace) -> dict: Convert a configuration object to a dictionary. Args: - cfg (str | Path | Dict | SimpleNamespace): Configuration object to be converted. Can be a file path, + cfg (str | Path | dict | SimpleNamespace): Configuration object to be converted. Can be a file path, a string, a dictionary, or a SimpleNamespace object. Returns: @@ -281,9 +281,9 @@ def get_cfg(cfg: str | Path | dict | SimpleNamespace = DEFAULT_CFG_DICT, overrid Load and merge configuration data from a file or dictionary, with optional overrides. Args: - cfg (str | Path | Dict | SimpleNamespace): Configuration data source. Can be a file path, dictionary, or + cfg (str | Path | dict | SimpleNamespace): Configuration data source. Can be a file path, dictionary, or SimpleNamespace object. - overrides (Dict | None): Dictionary containing key-value pairs to override the base configuration. + overrides (dict | None): Dictionary containing key-value pairs to override the base configuration. Returns: (SimpleNamespace): Namespace containing the merged configuration arguments. @@ -513,10 +513,10 @@ def merge_equals_args(args: list[str]) -> list[str]: 4. Joins fragments with brackets, e.g., ['imgsz=[3,', '640,', '640]'] becomes ['imgsz=[3,640,640]'] Args: - args (List[str]): A list of strings where each element represents an argument or fragment. + args (list[str]): A list of strings where each element represents an argument or fragment. Returns: - (List[str]): A list of strings where the arguments around isolated '=' are merged and fragments with brackets are joined. + (list[str]): A list of strings where the arguments around isolated '=' are merged and fragments with brackets are joined. Examples: >>> args = ["arg1", "=", "value", "arg2=", "value2", "arg3", "=value3", "imgsz=[3,", "640,", "640]"] @@ -569,7 +569,7 @@ def handle_yolo_hub(args: list[str]) -> None: script with arguments related to HUB authentication. Args: - args (List[str]): A list of command line arguments. The first argument should be either 'login' + args (list[str]): A list of command line arguments. The first argument should be either 'login' or 'logout'. For 'login', an optional second argument can be the API key. Examples: @@ -599,7 +599,7 @@ def handle_yolo_settings(args: list[str]) -> None: called when executing a script with arguments related to YOLO settings management. Args: - args (List[str]): A list of command line arguments for YOLO settings management. + args (list[str]): A list of command line arguments for YOLO settings management. Examples: >>> handle_yolo_settings(["reset"]) # Reset YOLO settings @@ -639,7 +639,7 @@ def handle_yolo_solutions(args: list[str]) -> None: Process YOLO solutions arguments and run the specified computer vision solutions pipeline. Args: - args (List[str]): Command-line arguments for configuring and running the Ultralytics YOLO + args (list[str]): Command-line arguments for configuring and running the Ultralytics YOLO solutions: https://docs.ultralytics.com/solutions/, It can include solution name, source, and other configuration parameters. diff --git a/ultralytics/data/annotator.py b/ultralytics/data/annotator.py index ece9395f..5600ee3d 100644 --- a/ultralytics/data/annotator.py +++ b/ultralytics/data/annotator.py @@ -34,7 +34,7 @@ def auto_annotate( iou (float): IoU threshold for filtering overlapping boxes in detection results. imgsz (int): Input image resize dimension. max_det (int): Maximum number of detections per image. - classes (List[int], optional): Filter predictions to specified class IDs, returning only relevant detections. + classes (list[int], optional): Filter predictions to specified class IDs, returning only relevant detections. output_dir (str | Path, optional): Directory to save the annotated results. If None, creates a default directory based on the input data path. diff --git a/ultralytics/data/augment.py b/ultralytics/data/augment.py index bf92c4af..48d9e2ba 100644 --- a/ultralytics/data/augment.py +++ b/ultralytics/data/augment.py @@ -150,7 +150,7 @@ class Compose: A class for composing multiple image transformations. Attributes: - transforms (List[Callable]): A list of transformation functions to be applied sequentially. + transforms (list[Callable]): A list of transformation functions to be applied sequentially. Methods: __call__: Apply a series of transformations to input data. @@ -173,7 +173,7 @@ class Compose: Initialize the Compose object with a list of transforms. Args: - transforms (List[Callable]): A list of callable transform objects to be applied sequentially. + transforms (list[Callable]): A list of callable transform objects to be applied sequentially. Examples: >>> from ultralytics.data.augment import Compose, RandomHSV, RandomFlip @@ -238,7 +238,7 @@ class Compose: Retrieve a specific transform or a set of transforms using indexing. Args: - index (int | List[int]): Index or list of indices of the transforms to retrieve. + index (int | list[int]): Index or list of indices of the transforms to retrieve. Returns: (Compose): A new Compose object containing the selected transform(s). @@ -260,8 +260,8 @@ class Compose: Set one or more transforms in the composition using indexing. Args: - index (int | List[int]): Index or list of indices to set transforms at. - value (Any | List[Any]): Transform or list of transforms to set at the specified index(es). + index (int | list[int]): Index or list of indices to set transforms at. + value (Any | list[Any]): Transform or list of transforms to set at the specified index(es). Raises: AssertionError: If index type is invalid, value type doesn't match index type, or index is out of range. @@ -376,10 +376,10 @@ class BaseMixTransform: selects additional images, applies pre-transforms if specified, and then performs the mix transform. Args: - labels (Dict[str, Any]): A dictionary containing label data for an image. + labels (dict[str, Any]): A dictionary containing label data for an image. Returns: - (Dict[str, Any]): The transformed labels dictionary, which may include mixed data from other images. + (dict[str, Any]): The transformed labels dictionary, which may include mixed data from other images. Examples: >>> transform = BaseMixTransform(dataset, pre_transform=None, p=0.5) @@ -416,11 +416,11 @@ class BaseMixTransform: Mosaic. It modifies the input label dictionary in-place with the augmented data. Args: - labels (Dict[str, Any]): A dictionary containing image and label data. Expected to have a 'mix_labels' key + labels (dict[str, Any]): A dictionary containing image and label data. Expected to have a 'mix_labels' key with a list of additional image and label data for mixing. Returns: - (Dict[str, Any]): The modified labels dictionary with augmented data after applying the mix transform. + (dict[str, Any]): The modified labels dictionary with augmented data after applying the mix transform. Examples: >>> transform = BaseMixTransform(dataset) @@ -434,7 +434,7 @@ class BaseMixTransform: Get a list of shuffled indexes for mosaic augmentation. Returns: - (List[int]): A list of shuffled indexes from the dataset. + (list[int]): A list of shuffled indexes from the dataset. Examples: >>> transform = BaseMixTransform(dataset) @@ -452,11 +452,11 @@ class BaseMixTransform: creating a unified set of text labels and updating class IDs accordingly. Args: - labels (Dict[str, Any]): A dictionary containing label information, including 'texts' and 'cls' fields, + labels (dict[str, Any]): A dictionary containing label information, including 'texts' and 'cls' fields, and optionally a 'mix_labels' field with additional label dictionaries. Returns: - (Dict[str, Any]): The updated labels dictionary with unified text labels and updated class IDs. + (dict[str, Any]): The updated labels dictionary with unified text labels and updated class IDs. Examples: >>> labels = { @@ -501,7 +501,7 @@ class Mosaic(BaseMixTransform): imgsz (int): Image size (height and width) after mosaic pipeline of a single image. p (float): Probability of applying the mosaic augmentation. Must be in the range 0-1. n (int): The grid size, either 4 (for 2x2) or 9 (for 3x3). - border (Tuple[int, int]): Border size for width and height. + border (tuple[int, int]): Border size for width and height. Methods: get_indexes: Return a list of random indexes from the dataset. @@ -553,7 +553,7 @@ class Mosaic(BaseMixTransform): the 'buffer' parameter. It is used to choose images for creating mosaic augmentations. Returns: - (List[int]): A list of random image indexes. The length of the list is n-1, where n is the number + (list[int]): A list of random image indexes. The length of the list is n-1, where n is the number of images used in the mosaic (either 3 or 8, depending on whether n is 4 or 9). Examples: @@ -575,12 +575,12 @@ class Mosaic(BaseMixTransform): mosaic augmentation. Args: - labels (Dict[str, Any]): A dictionary containing image data and annotations. Expected keys include: + labels (dict[str, Any]): A dictionary containing image data and annotations. Expected keys include: - 'rect_shape': Should be None as rect and mosaic are mutually exclusive. - 'mix_labels': A list of dictionaries containing data for other images to be used in the mosaic. Returns: - (Dict[str, Any]): A dictionary containing the mosaic-augmented image and updated annotations. + (dict[str, Any]): A dictionary containing the mosaic-augmented image and updated annotations. Raises: AssertionError: If 'rect_shape' is not None or if 'mix_labels' is empty. @@ -603,12 +603,12 @@ class Mosaic(BaseMixTransform): additional images on either side. It's part of the Mosaic augmentation technique used in object detection. Args: - labels (Dict[str, Any]): A dictionary containing image and label information for the main (center) image. + labels (dict[str, Any]): A dictionary containing image and label information for the main (center) image. Must include 'img' key with the image array, and 'mix_labels' key with a list of two dictionaries containing information for the side images. Returns: - (Dict[str, Any]): A dictionary with the mosaic image and updated labels. Keys include: + (dict[str, Any]): A dictionary with the mosaic image and updated labels. Keys include: - 'img' (np.ndarray): The mosaic image array with shape (H, W, C). - Other keys from the input labels, updated to reflect the new image dimensions. @@ -662,11 +662,11 @@ class Mosaic(BaseMixTransform): updates the corresponding labels for each image in the mosaic. Args: - labels (Dict[str, Any]): A dictionary containing image data and labels for the base image (index 0) and three + labels (dict[str, Any]): A dictionary containing image data and labels for the base image (index 0) and three additional images (indices 1-3) in the 'mix_labels' key. Returns: - (Dict[str, Any]): A dictionary containing the mosaic image and updated labels. The 'img' key contains the mosaic + (dict[str, Any]): A dictionary containing the mosaic image and updated labels. The 'img' key contains the mosaic image as a numpy array, and other keys contain the combined and adjusted labels for all four images. Examples: @@ -720,15 +720,15 @@ class Mosaic(BaseMixTransform): and eight additional images from the dataset are placed around it in a 3x3 grid pattern. Args: - labels (Dict[str, Any]): A dictionary containing the input image and its associated labels. It should have + labels (dict[str, Any]): A dictionary containing the input image and its associated labels. It should have the following keys: - 'img' (np.ndarray): The input image. - - 'resized_shape' (Tuple[int, int]): The shape of the resized image (height, width). - - 'mix_labels' (List[Dict]): A list of dictionaries containing information for the additional + - 'resized_shape' (tuple[int, int]): The shape of the resized image (height, width). + - 'mix_labels' (list[dict]): A list of dictionaries containing information for the additional eight images, each with the same structure as the input labels. Returns: - (Dict[str, Any]): A dictionary containing the mosaic image and updated labels. It includes the following keys: + (dict[str, Any]): A dictionary containing the mosaic image and updated labels. It includes the following keys: - 'img' (np.ndarray): The final mosaic image. - Other keys from the input labels, updated to reflect the new mosaic arrangement. @@ -793,7 +793,7 @@ class Mosaic(BaseMixTransform): values. It also denormalizes the coordinates if they were previously normalized. Args: - labels (Dict[str, Any]): A dictionary containing image and instance information. + labels (dict[str, Any]): A dictionary containing image and instance information. padw (int): Padding width to be added to the x-coordinates. padh (int): Padding height to be added to the y-coordinates. @@ -819,17 +819,17 @@ class Mosaic(BaseMixTransform): mosaic border, and removes zero-area boxes. Args: - mosaic_labels (List[Dict[str, Any]]): A list of label dictionaries for each image in the mosaic. + mosaic_labels (list[dict[str, Any]]): A list of label dictionaries for each image in the mosaic. Returns: - (Dict[str, Any]): A dictionary containing concatenated and processed labels for the mosaic image, including: + (dict[str, Any]): A dictionary containing concatenated and processed labels for the mosaic image, including: - im_file (str): File path of the first image in the mosaic. - - ori_shape (Tuple[int, int]): Original shape of the first image. - - resized_shape (Tuple[int, int]): Shape of the mosaic image (imgsz * 2, imgsz * 2). + - ori_shape (tuple[int, int]): Original shape of the first image. + - resized_shape (tuple[int, int]): Shape of the mosaic image (imgsz * 2, imgsz * 2). - cls (np.ndarray): Concatenated class labels. - instances (Instances): Concatenated instance annotations. - - mosaic_border (Tuple[int, int]): Mosaic border size. - - texts (List[str], optional): Text labels if present in the original labels. + - mosaic_border (tuple[int, int]): Mosaic border size. + - texts (list[str], optional): Text labels if present in the original labels. Examples: >>> mosaic = Mosaic(dataset, imgsz=640) @@ -912,10 +912,10 @@ class MixUp(BaseMixTransform): "mixup: Beyond Empirical Risk Minimization" (https://arxiv.org/abs/1710.09412). Args: - labels (Dict[str, Any]): A dictionary containing the original image and label information. + labels (dict[str, Any]): A dictionary containing the original image and label information. Returns: - (Dict[str, Any]): A dictionary containing the mixed-up image and combined label information. + (dict[str, Any]): A dictionary containing the mixed-up image and combined label information. Examples: >>> mixer = MixUp(dataset) @@ -978,7 +978,7 @@ class CutMix(BaseMixTransform): height (int): Height of the image. Returns: - (Tuple[int]): (x1, y1, x2, y2) coordinates of the bounding box. + (tuple[int]): (x1, y1, x2, y2) coordinates of the bounding box. """ # Sample mixing ratio from Beta distribution lam = np.random.beta(self.beta, self.beta) @@ -1004,10 +1004,10 @@ class CutMix(BaseMixTransform): Apply CutMix augmentation to the input labels. Args: - labels (Dict[str, Any]): A dictionary containing the original image and label information. + labels (dict[str, Any]): A dictionary containing the original image and label information. Returns: - (Dict[str, Any]): A dictionary containing the mixed image and adjusted labels. + (dict[str, Any]): A dictionary containing the mixed image and adjusted labels. Examples: >>> cutter = CutMix(dataset) @@ -1061,7 +1061,7 @@ class RandomPerspective: scale (float): Scaling factor range, e.g., scale=0.1 means 0.9-1.1. shear (float): Maximum shear angle in degrees. perspective (float): Perspective distortion factor. - border (Tuple[int, int]): Mosaic border size as (x, y). + border (tuple[int, int]): Mosaic border size as (x, y). pre_transform (Callable | None): Optional transform to apply before the random perspective. Methods: @@ -1103,7 +1103,7 @@ class RandomPerspective: scale (float): Scaling factor interval, e.g., a scale factor of 0.5 allows a resize between 50%-150%. shear (float): Shear intensity (angle in degrees). perspective (float): Perspective distortion factor. - border (Tuple[int, int]): Tuple specifying mosaic border (top/bottom, left/right). + border (tuple[int, int]): Tuple specifying mosaic border (top/bottom, left/right). pre_transform (Callable | None): Function/transform to apply to the image before starting the random transformation. @@ -1129,7 +1129,7 @@ class RandomPerspective: Args: img (np.ndarray): Input image to be transformed. - border (Tuple[int, int]): Border dimensions for the transformed image. + border (tuple[int, int]): Border dimensions for the transformed image. Returns: img (np.ndarray): Transformed image. @@ -1296,20 +1296,20 @@ class RandomPerspective: and keypoints accordingly. Args: - labels (Dict[str, Any]): A dictionary containing image data and annotations. + labels (dict[str, Any]): A dictionary containing image data and annotations. Must include: 'img' (np.ndarray): The input image. 'cls' (np.ndarray): Class labels. 'instances' (Instances): Object instances with bounding boxes, segments, and keypoints. May include: - 'mosaic_border' (Tuple[int, int]): Border size for mosaic augmentation. + 'mosaic_border' (tuple[int, int]): Border size for mosaic augmentation. Returns: - (Dict[str, Any]): Transformed labels dictionary containing: + (dict[str, Any]): Transformed labels dictionary containing: - 'img' (np.ndarray): The transformed image. - 'cls' (np.ndarray): Updated class labels. - 'instances' (Instances): Updated object instances. - - 'resized_shape' (Tuple[int, int]): New image shape after transformation. + - 'resized_shape' (tuple[int, int]): New image shape after transformation. Examples: >>> transform = RandomPerspective() @@ -1463,11 +1463,11 @@ class RandomHSV: The adjustments are made within the limits set by hgain, sgain, and vgain during initialization. Args: - labels (Dict[str, Any]): A dictionary containing image data and metadata. Must include an 'img' key with + labels (dict[str, Any]): A dictionary containing image data and metadata. Must include an 'img' key with the image as a numpy array. Returns: - (Dict[str, Any]): A dictionary containing the mixed image and adjusted labels. + (dict[str, Any]): A dictionary containing the mixed image and adjusted labels. Examples: >>> hsv_augmenter = RandomHSV(hgain=0.5, sgain=0.5, vgain=0.5) @@ -1527,7 +1527,7 @@ class RandomFlip: Args: p (float): The probability of applying the flip. Must be between 0 and 1. direction (str): The direction to apply the flip. Must be 'horizontal' or 'vertical'. - flip_idx (List[int] | None): Index mapping for flipping keypoints, if any. + flip_idx (list[int] | None): Index mapping for flipping keypoints, if any. Raises: AssertionError: If direction is not 'horizontal' or 'vertical', or if p is not between 0 and 1. @@ -1552,13 +1552,13 @@ class RandomFlip: match the flipped image. Args: - labels (Dict[str, Any]): A dictionary containing the following keys: + labels (dict[str, Any]): A dictionary containing the following keys: 'img' (np.ndarray): The image to be flipped. 'instances' (ultralytics.utils.instance.Instances): An object containing bounding boxes and optionally keypoints. Returns: - (Dict[str, Any]): The same dictionary with the flipped image and updated instances: + (dict[str, Any]): The same dictionary with the flipped image and updated instances: 'img' (np.ndarray): The flipped image. 'instances' (ultralytics.utils.instance.Instances): Updated instances matching the flipped image. @@ -1633,7 +1633,7 @@ class LetterBox: tasks. It supports various resizing modes including auto-sizing, scale-fill, and letterboxing. Args: - new_shape (Tuple[int, int]): Target size (height, width) for the resized image. + new_shape (tuple[int, int]): Target size (height, width) for the resized image. auto (bool): If True, use minimum rectangle to resize. If False, use new_shape directly. scale_fill (bool): If True, stretch the image to new_shape without padding. scaleup (bool): If True, allow scaling up. If False, only scale down. @@ -1643,7 +1643,7 @@ class LetterBox: interpolation (int): Interpolation method for resizing. Default is cv2.INTER_LINEAR. Attributes: - new_shape (Tuple[int, int]): Target size for the resized image. + new_shape (tuple[int, int]): Target size for the resized image. auto (bool): Flag for using minimum rectangle resizing. scale_fill (bool): Flag for stretching image without padding. scaleup (bool): Flag for allowing upscaling. @@ -1672,11 +1672,11 @@ class LetterBox: aspect ratio and adding padding to fit the new shape. It also updates any associated labels accordingly. Args: - labels (Dict[str, Any] | None): A dictionary containing image data and associated labels, or empty dict if None. + labels (dict[str, Any] | None): A dictionary containing image data and associated labels, or empty dict if None. image (np.ndarray | None): The input image as a numpy array. If None, the image is taken from 'labels'. Returns: - (Dict[str, Any] | nd.ndarray): If 'labels' is provided, returns an updated dictionary with the resized and padded image, + (dict[str, Any] | nd.ndarray): If 'labels' is provided, returns an updated dictionary with the resized and padded image, updated labels, and additional metadata. If 'labels' is empty, returns the resized and padded image. @@ -1751,13 +1751,13 @@ class LetterBox: to account for resizing and padding applied during letterboxing. Args: - labels (Dict[str, Any]): A dictionary containing image labels and instances. - ratio (Tuple[float, float]): Scaling ratios (width, height) applied to the image. + labels (dict[str, Any]): A dictionary containing image labels and instances. + ratio (tuple[float, float]): Scaling ratios (width, height) applied to the image. padw (float): Padding width added to the image. padh (float): Padding height added to the image. Returns: - (Dict[str, Any]): Updated labels dictionary with modified instance coordinates. + (dict[str, Any]): Updated labels dictionary with modified instance coordinates. Examples: >>> letterbox = LetterBox(new_shape=(640, 640)) @@ -2021,13 +2021,13 @@ class Albumentations: spatial and non-spatial transformations on the input image and its corresponding labels. Args: - labels (Dict[str, Any]): A dictionary containing image data and annotations. Expected keys are: + labels (dict[str, Any]): A dictionary containing image data and annotations. Expected keys are: - 'img': np.ndarray representing the image - 'cls': np.ndarray of class labels - 'instances': object containing bounding boxes and other instance information Returns: - (Dict[str, Any]): The input dictionary with augmented image and updated annotations. + (dict[str, Any]): The input dictionary with augmented image and updated annotations. Examples: >>> transform = Albumentations(p=0.5) @@ -2164,13 +2164,13 @@ class Format: applying normalization if required. Args: - labels (Dict[str, Any]): A dictionary containing image and annotation data with the following keys: + labels (dict[str, Any]): A dictionary containing image and annotation data with the following keys: - 'img': The input image as a numpy array. - 'cls': Class labels for instances. - 'instances': An Instances object containing bounding boxes, segments, and keypoints. Returns: - (Dict[str, Any]): A dictionary with formatted data, including: + (dict[str, Any]): A dictionary with formatted data, including: - 'img': Formatted image tensor. - 'cls': Class label's tensor. - 'bboxes': Bounding boxes tensor in the specified format. @@ -2324,10 +2324,10 @@ class LoadVisualPrompt: Process labels to create visual prompts. Args: - labels (Dict[str, Any]): Dictionary containing image data and annotations. + labels (dict[str, Any]): Dictionary containing image data and annotations. Returns: - (Dict[str, Any]): Updated labels with visual prompts added. + (dict[str, Any]): Updated labels with visual prompts added. """ imgsz = labels["img"].shape[1:] bboxes, masks = None, None @@ -2352,7 +2352,7 @@ class LoadVisualPrompt: Args: category (int | np.ndarray | torch.Tensor): The category labels for the objects. - shape (Tuple[int, int]): The shape of the image (height, width). + shape (tuple[int, int]): The shape of the image (height, width). bboxes (np.ndarray | torch.Tensor, optional): Bounding boxes for the objects, xyxy format. masks (np.ndarray | torch.Tensor, optional): Masks for the objects. @@ -2398,7 +2398,7 @@ class RandomLoadText: Attributes: prompt_format (str): Format string for text prompts. - neg_samples (Tuple[int, int]): Range for randomly sampling negative texts. + neg_samples (tuple[int, int]): Range for randomly sampling negative texts. max_samples (int): Maximum number of different text samples in one image. padding (bool): Whether to pad texts to max_samples. padding_value (str): The text used for padding when padding is True. @@ -2431,7 +2431,7 @@ class RandomLoadText: Args: prompt_format (str): Format string for the prompt. The format string should contain a single pair of curly braces {} where the text will be inserted. - neg_samples (Tuple[int, int]): A range to randomly sample negative texts. The first integer + neg_samples (tuple[int, int]): A range to randomly sample negative texts. The first integer specifies the minimum number of negative samples, and the second integer specifies the maximum. max_samples (int): The maximum number of different text samples in one image. @@ -2441,7 +2441,7 @@ class RandomLoadText: Attributes: prompt_format (str): The format string for the prompt. - neg_samples (Tuple[int, int]): The range for sampling negative texts. + neg_samples (tuple[int, int]): The range for sampling negative texts. max_samples (int): The maximum number of text samples. padding (bool): Whether padding is enabled. padding_value (str): The value used for padding. @@ -2470,10 +2470,10 @@ class RandomLoadText: new sampled text order. Args: - labels (Dict[str, Any]): A dictionary containing image labels and metadata. Must include 'texts' and 'cls' keys. + labels (dict[str, Any]): A dictionary containing image labels and metadata. Must include 'texts' and 'cls' keys. Returns: - (Dict[str, Any]): Updated labels dictionary with new 'cls' and 'texts' entries. + (dict[str, Any]): Updated labels dictionary with new 'cls' and 'texts' entries. Examples: >>> loader = RandomLoadText(prompt_format="A photo of {}", neg_samples=(5, 10), max_samples=20) @@ -2613,8 +2613,8 @@ def classify_transforms( Args: size (int | tuple): The target size for the transformed image. If an int, it defines the shortest edge. If a tuple, it defines (height, width). - mean (Tuple[float, float, float]): Mean values for each RGB channel used in normalization. - std (Tuple[float, float, float]): Standard deviation values for each RGB channel used in normalization. + mean (tuple[float, float, float]): Mean values for each RGB channel used in normalization. + std (tuple[float, float, float]): Standard deviation values for each RGB channel used in normalization. interpolation (str): Interpolation method of either 'NEAREST', 'BILINEAR' or 'BICUBIC'. crop_fraction (float): Deprecated, will be removed in a future version. @@ -2671,10 +2671,10 @@ def classify_augmentations( Args: size (int): Target size for the image after transformations. - mean (Tuple[float, float, float]): Mean values for each RGB channel used in normalization. - std (Tuple[float, float, float]): Standard deviation values for each RGB channel used in normalization. - scale (Tuple[float, float] | None): Range of size of the origin size cropped. - ratio (Tuple[float, float] | None): Range of aspect ratio of the origin aspect ratio cropped. + mean (tuple[float, float, float]): Mean values for each RGB channel used in normalization. + std (tuple[float, float, float]): Standard deviation values for each RGB channel used in normalization. + scale (tuple[float, float] | None): Range of size of the origin size cropped. + ratio (tuple[float, float] | None): Range of aspect ratio of the origin aspect ratio cropped. hflip (float): Probability of horizontal flip. vflip (float): Probability of vertical flip. auto_augment (str | None): Auto augmentation policy. Can be 'randaugment', 'augmix', 'autoaugment' or None. @@ -2783,7 +2783,7 @@ class ClassifyLetterBox: pads images to a specified size while maintaining the original aspect ratio. Args: - size (int | Tuple[int, int]): Target size for the letterboxed image. If an int, a square image of + size (int | tuple[int, int]): Target size for the letterboxed image. If an int, a square image of (size, size) is created. If a tuple, it should be (height, width). auto (bool): If True, automatically calculates the short side based on stride. stride (int): The stride value, used when 'auto' is True. @@ -2872,7 +2872,7 @@ class CenterCrop: It performs a center crop on input images to a specified size. Args: - size (int | Tuple[int, int]): The desired output size of the crop. If size is an int, a square crop + size (int | tuple[int, int]): The desired output size of the crop. If size is an int, a square crop (size, size) is made. If size is a sequence like (h, w), it is used as the output size. Returns: diff --git a/ultralytics/data/base.py b/ultralytics/data/base.py index 950b671e..6a3fea65 100644 --- a/ultralytics/data/base.py +++ b/ultralytics/data/base.py @@ -36,8 +36,8 @@ class BaseDataset(Dataset): fraction (float): Fraction of dataset to utilize. channels (int): Number of channels in the images (1 for grayscale, 3 for RGB). cv2_flag (int): OpenCV flag for reading images. - im_files (List[str]): List of image file paths. - labels (List[Dict]): List of label data dictionaries. + im_files (list[str]): List of image file paths. + labels (list[dict]): List of label data dictionaries. ni (int): Number of images in the dataset. rect (bool): Whether to use rectangular training. batch_size (int): Size of batches. @@ -48,7 +48,7 @@ class BaseDataset(Dataset): ims (list): List of loaded images. im_hw0 (list): List of original image dimensions (h, w). im_hw (list): List of resized image dimensions (h, w). - npy_files (List[Path]): List of numpy file paths. + npy_files (list[Path]): List of numpy file paths. cache (str): Cache images to RAM or disk during training. transforms (callable): Image transformation function. batch_shapes (np.ndarray): Batch shapes for rectangular training. @@ -90,18 +90,18 @@ class BaseDataset(Dataset): Initialize BaseDataset with given configuration and options. Args: - img_path (str | List[str]): Path to the folder containing images or list of image paths. + img_path (str | list[str]): Path to the folder containing images or list of image paths. imgsz (int): Image size for resizing. cache (bool | str): Cache images to RAM or disk during training. augment (bool): If True, data augmentation is applied. - hyp (Dict[str, Any]): Hyperparameters to apply data augmentation. + hyp (dict[str, Any]): Hyperparameters to apply data augmentation. prefix (str): Prefix to print in log messages. rect (bool): If True, rectangular training is used. batch_size (int): Size of batches. stride (int): Stride used in the model. pad (float): Padding value. single_cls (bool): If True, single class training is used. - classes (List[int], optional): List of included classes. + classes (list[int], optional): List of included classes. fraction (float): Fraction of dataset to utilize. channels (int): Number of channels in the images (1 for grayscale, 3 for RGB). """ @@ -152,10 +152,10 @@ class BaseDataset(Dataset): Read image files from the specified path. Args: - img_path (str | List[str]): Path or list of paths to image directories or files. + img_path (str | list[str]): Path or list of paths to image directories or files. Returns: - (List[str]): List of image file paths. + (list[str]): List of image file paths. Raises: FileNotFoundError: If no images are found or the path doesn't exist. @@ -190,7 +190,7 @@ class BaseDataset(Dataset): Update labels to include only specified classes. Args: - include_class (List[int], optional): List of classes to include. If None, all classes are included. + include_class (list[int], optional): List of classes to include. If None, all classes are included. """ include_class_array = np.array(include_class).reshape(1, -1) for i in range(len(self.labels)): @@ -219,8 +219,8 @@ class BaseDataset(Dataset): Returns: im (np.ndarray): Loaded image as a NumPy array. - hw_original (Tuple[int, int]): Original image dimensions in (height, width) format. - hw_resized (Tuple[int, int]): Resized image dimensions in (height, width) format. + hw_original (tuple[int, int]): Original image dimensions in (height, width) format. + hw_resized (tuple[int, int]): Resized image dimensions in (height, width) format. Raises: FileNotFoundError: If the image file is not found. @@ -388,7 +388,7 @@ class BaseDataset(Dataset): index (int): Index of the image to retrieve. Returns: - (Dict[str, Any]): Label dictionary with image and metadata. + (dict[str, Any]): Label dictionary with image and metadata. """ label = deepcopy(self.labels[index]) # requires deepcopy() https://github.com/ultralytics/ultralytics/pull/1948 label.pop("shape", None) # shape is for rect, remove it diff --git a/ultralytics/data/converter.py b/ultralytics/data/converter.py index b463fe45..e0b9263f 100644 --- a/ultralytics/data/converter.py +++ b/ultralytics/data/converter.py @@ -25,7 +25,7 @@ def coco91_to_coco80_class() -> list[int]: Convert 91-index COCO class IDs to 80-index COCO class IDs. Returns: - (List[int]): A list of 91 class IDs where the index represents the 80-index class ID and the value + (list[int]): A list of 91 class IDs where the index represents the 80-index class ID and the value is the corresponding 91-index class ID. """ return [ @@ -128,7 +128,7 @@ def coco80_to_coco91_class() -> list[int]: Convert 80-index (val2014) to 91-index (paper). Returns: - (List[int]): A list of 80 class IDs where each value is the corresponding 91-index class ID. + (list[int]): A list of 80 class IDs where each value is the corresponding 91-index class ID. References: https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/ @@ -539,11 +539,11 @@ def merge_multi_segment(segments: list[list]): This function connects these coordinates with a thin line to merge all segments into one. Args: - segments (List[List]): Original segmentations in COCO's JSON file. + segments (list[list]): Original segmentations in COCO's JSON file. Each element is a list of coordinates, like [segmentation1, segmentation2,...]. Returns: - s (List[np.ndarray]): A list of connected segments represented as NumPy arrays. + s (list[np.ndarray]): A list of connected segments represented as NumPy arrays. """ s = [] segments = [np.array(i).reshape(-1, 2) for i in segments] diff --git a/ultralytics/data/dataset.py b/ultralytics/data/dataset.py index 2eadb170..470775f1 100644 --- a/ultralytics/data/dataset.py +++ b/ultralytics/data/dataset.py @@ -164,7 +164,7 @@ class YOLODataset(BaseDataset): This method loads labels from disk or cache, verifies their integrity, and prepares them for training. Returns: - (List[dict]): List of label dictionaries, each containing information about an image and its annotations. + (list[dict]): List of label dictionaries, each containing information about an image and its annotations. """ self.label_files = img2label_paths(self.im_files) cache_path = Path(self.label_files[0]).parent.with_suffix(".cache") @@ -291,7 +291,7 @@ class YOLODataset(BaseDataset): Collate data samples into batches. Args: - batch (List[dict]): List of dictionaries containing sample data. + batch (list[dict]): List of dictionaries containing sample data. Returns: (dict): Collated batch with stacked tensors. @@ -392,7 +392,7 @@ class YOLOMultiModalDataset(YOLODataset): Return category names for the dataset. Returns: - (Set[str]): List of class names. + (set[str]): List of class names. """ names = self.data["names"].values() return {n.strip() for name in names for n in name.split("/")} # category names @@ -474,7 +474,7 @@ class GroundingDataset(YOLODataset): against a predefined set of datasets with known instance counts. Args: - labels (List[Dict[str, Any]]): List of label dictionaries, where each dictionary + labels (list[dict[str, Any]]): List of label dictionaries, where each dictionary contains dataset annotations. Each label dict must have a 'bboxes' key with a numpy array or tensor containing bounding box coordinates. @@ -508,7 +508,7 @@ class GroundingDataset(YOLODataset): path (Path): Path where to save the cache file. Returns: - (Dict[str, Any]): Dictionary containing cached labels and related information. + (dict[str, Any]): Dictionary containing cached labels and related information. """ x = {"labels": []} LOGGER.info("Loading annotation file...") @@ -596,7 +596,7 @@ class GroundingDataset(YOLODataset): Load labels from cache or generate them from JSON file. Returns: - (List[dict]): List of label dictionaries, each containing information about an image and its annotations. + (list[dict]): List of label dictionaries, each containing information about an image and its annotations. """ cache_path = Path(self.json_file).with_suffix(".cache") try: @@ -682,7 +682,7 @@ class YOLOConcatDataset(ConcatDataset): Collate data samples into batches. Args: - batch (List[dict]): List of dictionaries containing sample data. + batch (list[dict]): List of dictionaries containing sample data. Returns: (dict): Collated batch with stacked tensors. diff --git a/ultralytics/data/loaders.py b/ultralytics/data/loaders.py index a3ee3655..39e6e7ff 100644 --- a/ultralytics/data/loaders.py +++ b/ultralytics/data/loaders.py @@ -59,17 +59,17 @@ class LoadStreams: streams simultaneously, making it suitable for real-time video analysis tasks. Attributes: - sources (List[str]): The source input paths or URLs for the video streams. + sources (list[str]): The source input paths or URLs for the video streams. vid_stride (int): Video frame-rate stride. buffer (bool): Whether to buffer input streams. running (bool): Flag to indicate if the streaming thread is running. mode (str): Set to 'stream' indicating real-time capture. - imgs (List[List[np.ndarray]]): List of image frames for each stream. - fps (List[float]): List of FPS for each stream. - frames (List[int]): List of total frames for each stream. - threads (List[Thread]): List of threads for each stream. - shape (List[Tuple[int, int, int]]): List of shapes for each stream. - caps (List[cv2.VideoCapture]): List of cv2.VideoCapture objects for each stream. + imgs (list[list[np.ndarray]]): List of image frames for each stream. + fps (list[float]): List of FPS for each stream. + frames (list[int]): List of total frames for each stream. + threads (list[Thread]): List of threads for each stream. + shape (list[tuple[int, int, int]]): List of shapes for each stream. + caps (list[cv2.VideoCapture]): List of cv2.VideoCapture objects for each stream. bs (int): Batch size for processing. cv2_flag (int): OpenCV flag for image reading (grayscale or RGB). @@ -245,7 +245,7 @@ class LoadScreenshots: sct (mss.mss): Screen capture object from `mss` library. bs (int): Batch size, set to 1. fps (int): Frames per second, set to 30. - monitor (Dict[str, int]): Monitor configuration details. + monitor (dict[str, int]): Monitor configuration details. cv2_flag (int): OpenCV flag for image reading (grayscale or RGB). Methods: @@ -314,9 +314,9 @@ class LoadImagesAndVideos: single image files, video files, and lists of image and video paths. Attributes: - files (List[str]): List of image and video file paths. + files (list[str]): List of image and video file paths. nf (int): Total number of files (images and videos). - video_flag (List[bool]): Flags indicating whether a file is a video (True) or an image (False). + video_flag (list[bool]): Flags indicating whether a file is a video (True) or an image (False). mode (str): Current mode, 'image' or 'video'. vid_stride (int): Stride for video frame-rate. bs (int): Batch size. @@ -351,7 +351,7 @@ class LoadImagesAndVideos: Initialize dataloader for images and videos, supporting various input formats. Args: - path (str | Path | List): Path to images/videos, directory, or list of paths. + path (str | Path | list): Path to images/videos, directory, or list of paths. batch (int): Batch size for processing. vid_stride (int): Video frame-rate stride. channels (int): Number of image channels (1 for grayscale, 3 for RGB). @@ -497,8 +497,8 @@ class LoadPilAndNumpy: validation and format conversion to ensure that the images are in the required format for downstream processing. Attributes: - paths (List[str]): List of image paths or autogenerated filenames. - im0 (List[np.ndarray]): List of images stored as Numpy arrays. + paths (list[str]): List of image paths or autogenerated filenames. + im0 (list[np.ndarray]): List of images stored as Numpy arrays. mode (str): Type of data being processed, set to 'image'. bs (int): Batch size, equivalent to the length of `im0`. @@ -521,7 +521,7 @@ class LoadPilAndNumpy: Initialize a loader for PIL and Numpy images, converting inputs to a standardized format. Args: - im0 (PIL.Image.Image | np.ndarray | List): Single image or list of images in PIL or numpy format. + im0 (PIL.Image.Image | np.ndarray | list): Single image or list of images in PIL or numpy format. channels (int): Number of image channels (1 for grayscale, 3 for RGB). """ if not isinstance(im0, list): @@ -574,7 +574,7 @@ class LoadTensor: im0 (torch.Tensor): The input tensor containing the image(s) with shape (B, C, H, W). bs (int): Batch size, inferred from the shape of `im0`. mode (str): Current processing mode, set to 'image'. - paths (List[str]): List of image paths or auto-generated filenames. + paths (list[str]): List of image paths or auto-generated filenames. Methods: _single_check: Validates and formats an input tensor. diff --git a/ultralytics/data/split_dota.py b/ultralytics/data/split_dota.py index 07bbce42..3534cb71 100644 --- a/ultralytics/data/split_dota.py +++ b/ultralytics/data/split_dota.py @@ -73,7 +73,7 @@ def load_yolo_dota(data_root: str, split: str = "train") -> list[dict[str, Any]] split (str, optional): The split data set, could be 'train' or 'val'. Returns: - (List[Dict[str, Any]]): List of annotation dictionaries containing image information. + (list[dict[str, Any]]): List of annotation dictionaries containing image information. Notes: The directory structure assumed for the DOTA dataset: @@ -111,9 +111,9 @@ def get_windows( Get the coordinates of sliding windows for image cropping. Args: - im_size (Tuple[int, int]): Original image size, (H, W). - crop_sizes (Tuple[int, ...], optional): Crop size of windows. - gaps (Tuple[int, ...], optional): Gap between crops. + im_size (tuple[int, int]): Original image size, (H, W). + crop_sizes (tuple[int, ...], optional): Crop size of windows. + gaps (tuple[int, ...], optional): Gap between crops. im_rate_thr (float, optional): Threshold of windows areas divided by image areas. eps (float, optional): Epsilon value for math operations. @@ -179,9 +179,9 @@ def crop_and_save( Crop images and save new labels for each window. Args: - anno (Dict[str, Any]): Annotation dict, including 'filepath', 'label', 'ori_size' as its keys. + anno (dict[str, Any]): Annotation dict, including 'filepath', 'label', 'ori_size' as its keys. windows (np.ndarray): Array of windows coordinates with shape (N, 4). - window_objs (List[np.ndarray]): A list of labels inside each window. + window_objs (list[np.ndarray]): A list of labels inside each window. im_dir (str): The output directory path of images. lb_dir (str): The output directory path of labels. allow_background_images (bool, optional): Whether to include background images without labels. @@ -233,8 +233,8 @@ def split_images_and_labels( data_root (str): Root directory of the dataset. save_dir (str): Directory to save the split dataset. split (str, optional): The split data set, could be 'train' or 'val'. - crop_sizes (Tuple[int, ...], optional): Tuple of crop sizes. - gaps (Tuple[int, ...], optional): Tuple of gaps between crops. + crop_sizes (tuple[int, ...], optional): Tuple of crop sizes. + gaps (tuple[int, ...], optional): Tuple of gaps between crops. Notes: The directory structure assumed for the DOTA dataset: @@ -273,7 +273,7 @@ def split_trainval( save_dir (str): Directory to save the split dataset. crop_size (int, optional): Base crop size. gap (int, optional): Base gap between crops. - rates (Tuple[float, ...], optional): Scaling rates for crop_size and gap. + rates (tuple[float, ...], optional): Scaling rates for crop_size and gap. Notes: The directory structure assumed for the DOTA dataset: @@ -312,7 +312,7 @@ def split_test( save_dir (str): Directory to save the split dataset. crop_size (int, optional): Base crop size. gap (int, optional): Base gap between crops. - rates (Tuple[float, ...], optional): Scaling rates for crop_size and gap. + rates (tuple[float, ...], optional): Scaling rates for crop_size and gap. Notes: The directory structure assumed for the DOTA dataset: diff --git a/ultralytics/data/utils.py b/ultralytics/data/utils.py index 45843b5e..452bd572 100644 --- a/ultralytics/data/utils.py +++ b/ultralytics/data/utils.py @@ -57,7 +57,7 @@ def check_file_speeds( It samples up to 5 files from the provided list and warns if access times exceed the threshold. Args: - files (List[str]): List of file paths to check for access speed. + files (list[str]): List of file paths to check for access speed. threshold_ms (float, optional): Threshold in milliseconds for ping time warnings. threshold_mb (float, optional): Threshold in megabytes per second for read speed warnings. max_files (int, optional): The maximum number of files to check. @@ -261,7 +261,7 @@ def visualize_image_annotations(image_path: str, txt_path: str, label_map: dict[ Args: image_path (str): The path to the image file to annotate, and it can be in formats supported by PIL. txt_path (str): The path to the annotation file in YOLO format, that should contain one line per object. - label_map (Dict[int, str]): A dictionary that maps class IDs (integers) to class labels (strings). + label_map (dict[int, str]): A dictionary that maps class IDs (integers) to class labels (strings). Examples: >>> label_map = {0: "cat", 1: "dog", 2: "bird"} # It should include all annotated classes details @@ -300,8 +300,8 @@ def polygon2mask( Convert a list of polygons to a binary mask of the specified image size. Args: - imgsz (Tuple[int, int]): The size of the image as (height, width). - polygons (List[np.ndarray]): A list of polygons. Each polygon is an array with shape (N, M), where + imgsz (tuple[int, int]): The size of the image as (height, width). + polygons (list[np.ndarray]): A list of polygons. Each polygon is an array with shape (N, M), where N is the number of polygons, and M is the number of points such that M % 2 = 0. color (int, optional): The color value to fill in the polygons on the mask. downsample_ratio (int, optional): Factor by which to downsample the mask. @@ -325,8 +325,8 @@ def polygons2masks( Convert a list of polygons to a set of binary masks of the specified image size. Args: - imgsz (Tuple[int, int]): The size of the image as (height, width). - polygons (List[np.ndarray]): A list of polygons. Each polygon is an array with shape (N, M), where + imgsz (tuple[int, int]): The size of the image as (height, width). + polygons (list[np.ndarray]): A list of polygons. Each polygon is an array with shape (N, M), where N is the number of polygons, and M is the number of points such that M % 2 = 0. color (int): The color value to fill in the polygons on the masks. downsample_ratio (int, optional): Factor by which to downsample each mask. @@ -400,7 +400,7 @@ def check_det_dataset(dataset: str, autodownload: bool = True) -> dict[str, Any] autodownload (bool, optional): Whether to automatically download the dataset if not found. Returns: - (Dict[str, Any]): Parsed dataset information and paths. + (dict[str, Any]): Parsed dataset information and paths. """ file = check_file(dataset) @@ -494,13 +494,13 @@ def check_cls_dataset(dataset: str | Path, split: str = "") -> dict[str, Any]: split (str, optional): The split of the dataset. Either 'val', 'test', or ''. Returns: - (Dict[str, Any]): A dictionary containing the following keys: + (dict[str, Any]): A dictionary containing the following keys: - 'train' (Path): The directory path containing the training set of the dataset. - 'val' (Path): The directory path containing the validation set of the dataset. - 'test' (Path): The directory path containing the test set of the dataset. - 'nc' (int): The number of classes in the dataset. - - 'names' (Dict[int, str]): A dictionary of class names in the dataset. + - 'names' (dict[int, str]): A dictionary of class names in the dataset. """ # Download (optional if dataset=https://file.zip is passed directly) if str(dataset).startswith(("http:/", "https:/")): @@ -588,8 +588,8 @@ class HUBDatasetStats: task (str): Dataset task type. hub_dir (Path): Directory path for HUB dataset files. im_dir (Path): Directory path for compressed images. - stats (Dict): Statistics dictionary containing dataset information. - data (Dict): Dataset configuration data. + stats (dict): Statistics dictionary containing dataset information. + data (dict): Dataset configuration data. Methods: get_json: Return dataset JSON for Ultralytics HUB. diff --git a/ultralytics/engine/model.py b/ultralytics/engine/model.py index 7ab279d3..4705c9e7 100644 --- a/ultralytics/engine/model.py +++ b/ultralytics/engine/model.py @@ -168,14 +168,14 @@ class Model(torch.nn.Module): directly with the required arguments. Args: - source (str | Path | int | PIL.Image | np.ndarray | torch.Tensor | List | Tuple): The source of + source (str | Path | int | PIL.Image | np.ndarray | torch.Tensor | list | tuple): The source of the image(s) to make predictions on. Can be a file path, URL, PIL image, numpy array, PyTorch tensor, or a list/tuple of these. stream (bool): If True, treat the input source as a continuous stream for predictions. **kwargs (Any): Additional keyword arguments to configure the prediction process. Returns: - (List[ultralytics.engine.results.Results]): A list of prediction results, each encapsulated in a + (list[ultralytics.engine.results.Results]): A list of prediction results, each encapsulated in a Results object. Examples: @@ -433,7 +433,7 @@ class Model(torch.nn.Module): verbose (bool): If True, prints the information. If False, returns the information as a list. Returns: - (List[str]): A list of strings containing various types of information about the model, including + (list[str]): A list of strings containing various types of information about the model, including model summary, layer details, and parameter counts. Empty if verbose is True. Examples: @@ -477,13 +477,13 @@ class Model(torch.nn.Module): source. It allows customization of the embedding process through various keyword arguments. Args: - source (str | Path | int | List | Tuple | np.ndarray | torch.Tensor): The source of the image for + source (str | Path | int | list | tuple | np.ndarray | torch.Tensor): The source of the image for generating embeddings. Can be a file path, URL, PIL image, numpy array, etc. stream (bool): If True, predictions are streamed. **kwargs (Any): Additional keyword arguments for configuring the embedding process. Returns: - (List[torch.Tensor]): A list containing the image embeddings. + (list[torch.Tensor]): A list containing the image embeddings. Examples: >>> model = YOLO("yolo11n.pt") @@ -510,7 +510,7 @@ class Model(torch.nn.Module): types of image sources and can operate in a streaming mode. Args: - source (str | Path | int | PIL.Image | np.ndarray | torch.Tensor | List | Tuple): The source + source (str | Path | int | PIL.Image | np.ndarray | torch.Tensor | list | tuple): The source of the image(s) to make predictions on. Accepts various types including file paths, URLs, PIL images, numpy arrays, and torch tensors. stream (bool): If True, treats the input source as a continuous stream for predictions. @@ -519,7 +519,7 @@ class Model(torch.nn.Module): **kwargs (Any): Additional keyword arguments for configuring the prediction process. Returns: - (List[ultralytics.engine.results.Results]): A list of prediction results, each encapsulated in a + (list[ultralytics.engine.results.Results]): A list of prediction results, each encapsulated in a Results object. Examples: @@ -571,14 +571,14 @@ class Model(torch.nn.Module): The method registers trackers if not already present and can persist them between calls. Args: - source (str | Path | int | List | Tuple | np.ndarray | torch.Tensor, optional): Input source for object + source (str | Path | int | list | tuple | np.ndarray | torch.Tensor, optional): Input source for object tracking. Can be a file path, URL, or video stream. stream (bool): If True, treats the input source as a continuous video stream. persist (bool): If True, persists trackers between different calls to this method. **kwargs (Any): Additional keyword arguments for configuring the tracking process. Returns: - (List[ultralytics.engine.results.Results]): A list of tracking results, each a Results object. + (list[ultralytics.engine.results.Results]): A list of tracking results, each a Results object. Examples: >>> model = YOLO("yolo11n.pt") @@ -650,7 +650,7 @@ class Model(torch.nn.Module): verbose (bool): Whether to print detailed benchmark information. format (str): Export format name for specific benchmarking. **kwargs (Any): Arbitrary keyword arguments to customize the benchmarking process. Common options include: - - imgsz (int | List[int]): Image size for benchmarking. + - imgsz (int | list[int]): Image size for benchmarking. - half (bool): Whether to use half-precision (FP16) mode. - int8 (bool): Whether to use int8 precision mode. - device (str): Device to run the benchmark on (e.g., 'cpu', 'cuda'). @@ -765,7 +765,7 @@ class Model(torch.nn.Module): patience (int): Epochs to wait for no observable improvement for early stopping of training. Returns: - (Dict | None): Training metrics if available and training is successful; otherwise, None. + (dict | None): Training metrics if available and training is successful; otherwise, None. Examples: >>> model = YOLO("yolo11n.pt") @@ -893,7 +893,7 @@ class Model(torch.nn.Module): initialized, it sets it up before retrieving the names. Returns: - (Dict[int, str]): A dictionary of class names associated with the model, where keys are class indices and + (dict[int, str]): A dictionary of class names associated with the model, where keys are class indices and values are the corresponding class names. Raises: @@ -1109,7 +1109,7 @@ class Model(torch.nn.Module): various tasks and modes within the Ultralytics framework. Returns: - (Dict[str, Dict[str, Any]]): A dictionary mapping task names to nested dictionaries. Each nested dictionary + (dict[str, dict[str, Any]]): A dictionary mapping task names to nested dictionaries. Each nested dictionary contains mappings for 'model', 'trainer', 'validator', and 'predictor' keys to their respective class implementations for that task. diff --git a/ultralytics/engine/predictor.py b/ultralytics/engine/predictor.py index b4a8f6bb..b358ce0f 100644 --- a/ultralytics/engine/predictor.py +++ b/ultralytics/engine/predictor.py @@ -81,15 +81,15 @@ class BasePredictor: data (dict): Data configuration. device (torch.device): Device used for prediction. dataset (Dataset): Dataset used for prediction. - vid_writer (Dict[str, cv2.VideoWriter]): Dictionary of {save_path: video_writer} for saving video output. + vid_writer (dict[str, cv2.VideoWriter]): Dictionary of {save_path: video_writer} for saving video output. plotted_img (np.ndarray): Last plotted image. source_type (SimpleNamespace): Type of input source. seen (int): Number of images processed. - windows (List[str]): List of window names for visualization. + windows (list[str]): List of window names for visualization. batch (tuple): Current batch data. - results (List[Any]): Current batch results. + results (list[Any]): Current batch results. transforms (callable): Image transforms for classification. - callbacks (Dict[str, List[callable]]): Callback functions for different events. + callbacks (dict[str, list[callable]]): Callback functions for different events. txt_path (Path): Path to save text results. _lock (threading.Lock): Lock for thread-safe inference. @@ -154,7 +154,7 @@ class BasePredictor: Prepare input image before inference. Args: - im (torch.Tensor | List[np.ndarray]): Images of shape (N, 3, H, W) for tensor, [(H, W, 3) x N] for list. + im (torch.Tensor | list[np.ndarray]): Images of shape (N, 3, H, W) for tensor, [(H, W, 3) x N] for list. Returns: (torch.Tensor): Preprocessed image tensor of shape (N, 3, H, W). @@ -188,10 +188,10 @@ class BasePredictor: Pre-transform input image before inference. Args: - im (List[np.ndarray]): List of images with shape [(H, W, 3) x N]. + im (list[np.ndarray]): List of images with shape [(H, W, 3) x N]. Returns: - (List[np.ndarray]): List of transformed images. + (list[np.ndarray]): List of transformed images. """ same_shapes = len({x.shape for x in im}) == 1 letterbox = LetterBox( @@ -212,7 +212,7 @@ class BasePredictor: Perform inference on an image or stream. Args: - source (str | Path | List[str] | List[Path] | List[np.ndarray] | np.ndarray | torch.Tensor, optional): + source (str | Path | list[str] | list[Path] | list[np.ndarray] | np.ndarray | torch.Tensor, optional): Source for inference. model (str | Path | torch.nn.Module, optional): Model for inference. stream (bool): Whether to stream the inference results. If True, returns a generator. @@ -220,7 +220,7 @@ class BasePredictor: **kwargs (Any): Additional keyword arguments for the inference method. Returns: - (List[ultralytics.engine.results.Results] | generator): Results objects or generator of Results objects. + (list[ultralytics.engine.results.Results] | generator): Results objects or generator of Results objects. """ self.stream = stream if stream: @@ -237,7 +237,7 @@ class BasePredictor: generator without storing results. Args: - source (str | Path | List[str] | List[Path] | List[np.ndarray] | np.ndarray | torch.Tensor, optional): + source (str | Path | list[str] | list[Path] | list[np.ndarray] | np.ndarray | torch.Tensor, optional): Source for inference. model (str | Path | torch.nn.Module, optional): Model for inference. @@ -254,7 +254,7 @@ class BasePredictor: Set up source and inference mode. Args: - source (str | Path | List[str] | List[Path] | List[np.ndarray] | np.ndarray | torch.Tensor): + source (str | Path | list[str] | list[Path] | list[np.ndarray] | np.ndarray | torch.Tensor): Source for inference. """ self.imgsz = check_imgsz(self.args.imgsz, stride=self.model.stride, min_dim=2) # check image size @@ -285,7 +285,7 @@ class BasePredictor: Stream real-time inference on camera feed and save results to file. Args: - source (str | Path | List[str] | List[Path] | List[np.ndarray] | np.ndarray | torch.Tensor, optional): + source (str | Path | list[str] | list[Path] | list[np.ndarray] | np.ndarray | torch.Tensor, optional): Source for inference. model (str | Path | torch.nn.Module, optional): Model for inference. *args (Any): Additional arguments for the inference method. @@ -418,7 +418,7 @@ class BasePredictor: i (int): Index of the current image in the batch. p (Path): Path to the current image. im (torch.Tensor): Preprocessed image tensor. - s (List[str]): List of result strings. + s (list[str]): List of result strings. Returns: (str): String with result information. diff --git a/ultralytics/engine/results.py b/ultralytics/engine/results.py index 8e40263b..022240ac 100644 --- a/ultralytics/engine/results.py +++ b/ultralytics/engine/results.py @@ -30,7 +30,7 @@ class BaseTensor(SimpleClass): Attributes: data (torch.Tensor | np.ndarray): Prediction data such as bounding boxes, masks, or keypoints. - orig_shape (Tuple[int, int]): Original shape of the image, typically in the format (height, width). + orig_shape (tuple[int, int]): Original shape of the image, typically in the format (height, width). Methods: cpu: Return a copy of the tensor stored in CPU memory. @@ -54,7 +54,7 @@ class BaseTensor(SimpleClass): Args: data (torch.Tensor | np.ndarray): Prediction data such as bounding boxes, masks, or keypoints. - orig_shape (Tuple[int, int]): Original shape of the image in (height, width) format. + orig_shape (tuple[int, int]): Original shape of the image in (height, width) format. Examples: >>> import torch @@ -72,7 +72,7 @@ class BaseTensor(SimpleClass): Return the shape of the underlying data tensor. Returns: - (Tuple[int, ...]): The shape of the data tensor. + (tuple[int, ...]): The shape of the data tensor. Examples: >>> data = torch.rand(100, 4) @@ -174,7 +174,7 @@ class BaseTensor(SimpleClass): Return a new BaseTensor instance containing the specified indexed elements of the data tensor. Args: - idx (int | List[int] | torch.Tensor): Index or indices to select from the data tensor. + idx (int | list[int] | torch.Tensor): Index or indices to select from the data tensor. Returns: (BaseTensor): A new BaseTensor instance containing the indexed data. @@ -199,7 +199,7 @@ class Results(SimpleClass, DataExportMixin): Attributes: orig_img (np.ndarray): The original image as a numpy array. - orig_shape (Tuple[int, int]): Original image shape in (height, width) format. + orig_shape (tuple[int, int]): Original image shape in (height, width) format. boxes (Boxes | None): Detected bounding boxes. masks (Masks | None): Segmentation masks. probs (Probs | None): Classification probabilities. @@ -261,7 +261,7 @@ class Results(SimpleClass, DataExportMixin): probs (torch.Tensor | None): A 1D tensor of probabilities of each class for classification task. keypoints (torch.Tensor | None): A 2D tensor of keypoint coordinates for each detection. obb (torch.Tensor | None): A 2D tensor of oriented bounding box coordinates for each detection. - speed (Dict | None): A dictionary containing preprocess, inference, and postprocess speeds (ms/image). + speed (dict | None): A dictionary containing preprocess, inference, and postprocess speeds (ms/image). Examples: >>> results = model("path/to/image.jpg") @@ -799,7 +799,7 @@ class Results(SimpleClass, DataExportMixin): decimals (int): Number of decimal places to round the output values to. Returns: - (List[Dict[str, Any]]): A list of dictionaries, each containing summarized information for a single detection + (list[dict[str, Any]]): A list of dictionaries, each containing summarized information for a single detection or classification result. The structure of each dictionary varies based on the task type (classification or detection) and available information (boxes, masks, keypoints). @@ -862,7 +862,7 @@ class Boxes(BaseTensor): Attributes: data (torch.Tensor | np.ndarray): The raw tensor containing detection boxes and associated data. - orig_shape (Tuple[int, int]): The original image dimensions (height, width). + orig_shape (tuple[int, int]): The original image dimensions (height, width). is_track (bool): Indicates whether tracking IDs are included in the box data. xyxy (torch.Tensor | np.ndarray): Boxes in [x1, y1, x2, y2] format. conf (torch.Tensor | np.ndarray): Confidence scores for each box. @@ -901,11 +901,11 @@ class Boxes(BaseTensor): boxes (torch.Tensor | np.ndarray): A tensor or numpy array with detection boxes of shape (num_boxes, 6) or (num_boxes, 7). Columns should contain [x1, y1, x2, y2, confidence, class, (optional) track_id]. - orig_shape (Tuple[int, int]): The original image shape as (height, width). Used for normalization. + orig_shape (tuple[int, int]): The original image shape as (height, width). Used for normalization. Attributes: data (torch.Tensor): The raw tensor containing detection boxes and their associated data. - orig_shape (Tuple[int, int]): The original image size, used for normalization. + orig_shape (tuple[int, int]): The original image size, used for normalization. is_track (bool): Indicates whether tracking IDs are included in the box data. Examples: @@ -1081,8 +1081,8 @@ class Masks(BaseTensor): Attributes: data (torch.Tensor | np.ndarray): The raw tensor or array containing mask data. orig_shape (tuple): Original image shape in (height, width) format. - xy (List[np.ndarray]): A list of segments in pixel coordinates. - xyn (List[np.ndarray]): A list of normalized segments. + xy (list[np.ndarray]): A list of segments in pixel coordinates. + xyn (list[np.ndarray]): A list of normalized segments. Methods: cpu: Return a copy of the Masks object with the mask tensor on CPU memory. @@ -1127,7 +1127,7 @@ class Masks(BaseTensor): are normalized relative to the original image shape. Returns: - (List[np.ndarray]): A list of numpy arrays, where each array contains the normalized xy-coordinates + (list[np.ndarray]): A list of numpy arrays, where each array contains the normalized xy-coordinates of a single segmentation mask. Each array has shape (N, 2), where N is the number of points in the mask contour. @@ -1152,7 +1152,7 @@ class Masks(BaseTensor): Masks object. The coordinates are scaled to match the original image dimensions. Returns: - (List[np.ndarray]): A list of numpy arrays, where each array contains the [x, y] pixel + (list[np.ndarray]): A list of numpy arrays, where each array contains the [x, y] pixel coordinates for a single segmentation mask. Each array has shape (N, 2), where N is the number of points in the segment. @@ -1179,7 +1179,7 @@ class Keypoints(BaseTensor): Attributes: data (torch.Tensor): The raw tensor containing keypoint data. - orig_shape (Tuple[int, int]): The original image dimensions (height, width). + orig_shape (tuple[int, int]): The original image dimensions (height, width). has_visible (bool): Indicates whether visibility information is available for keypoints. xy (torch.Tensor): Keypoint coordinates in [x, y] format. xyn (torch.Tensor): Normalized keypoint coordinates in [x, y] format, relative to orig_shape. @@ -1213,7 +1213,7 @@ class Keypoints(BaseTensor): keypoints (torch.Tensor): A tensor containing keypoint data. Shape can be either: - (num_objects, num_keypoints, 2) for x, y coordinates only - (num_objects, num_keypoints, 3) for x, y coordinates and confidence scores - orig_shape (Tuple[int, int]): The original image dimensions (height, width). + orig_shape (tuple[int, int]): The original image dimensions (height, width). Examples: >>> kpts = torch.rand(1, 17, 3) # 1 object, 17 keypoints (COCO format), x,y,conf @@ -1301,7 +1301,7 @@ class Probs(BaseTensor): data (torch.Tensor | np.ndarray): The raw tensor or array containing classification probabilities. orig_shape (tuple | None): The original image shape as (height, width). Not used in this class. top1 (int): Index of the class with the highest probability. - top5 (List[int]): Indices of the top 5 classes by probability. + top5 (list[int]): Indices of the top 5 classes by probability. top1conf (torch.Tensor | np.ndarray): Confidence score of the top 1 class. top5conf (torch.Tensor | np.ndarray): Confidence scores of the top 5 classes. @@ -1339,7 +1339,7 @@ class Probs(BaseTensor): Attributes: data (torch.Tensor | np.ndarray): The raw tensor or array containing classification probabilities. top1 (int): Index of the top 1 class. - top5 (List[int]): Indices of the top 5 classes. + top5 (list[int]): Indices of the top 5 classes. top1conf (torch.Tensor | np.ndarray): Confidence of the top 1 class. top5conf (torch.Tensor | np.ndarray): Confidences of the top 5 classes. @@ -1379,7 +1379,7 @@ class Probs(BaseTensor): Return the indices of the top 5 class probabilities. Returns: - (List[int]): A list containing the indices of the top 5 class probabilities, sorted in descending order. + (list[int]): A list containing the indices of the top 5 class probabilities, sorted in descending order. Examples: >>> probs = Probs(torch.tensor([0.1, 0.2, 0.3, 0.4, 0.5])) @@ -1476,11 +1476,11 @@ class OBB(BaseTensor): boxes (torch.Tensor | np.ndarray): A tensor or numpy array containing the detection boxes, with shape (num_boxes, 7) or (num_boxes, 8). The last two columns contain confidence and class values. If present, the third last column contains track IDs, and the fifth column contains rotation. - orig_shape (Tuple[int, int]): Original image size, in the format (height, width). + orig_shape (tuple[int, int]): Original image size, in the format (height, width). Attributes: data (torch.Tensor | np.ndarray): The raw OBB tensor. - orig_shape (Tuple[int, int]): The original image shape. + orig_shape (tuple[int, int]): The original image shape. is_track (bool): Whether the boxes include tracking IDs. Raises: diff --git a/ultralytics/hub/google/__init__.py b/ultralytics/hub/google/__init__.py index d34f2e58..5cff4e0f 100644 --- a/ultralytics/hub/google/__init__.py +++ b/ultralytics/hub/google/__init__.py @@ -15,7 +15,7 @@ class GCPRegions: geographical location, tier classification, and network latency. Attributes: - regions (Dict[str, Tuple[int, str, str]]): A dictionary of GCP regions with their tier, city, and country. + regions (dict[str, tuple[int, str, str]]): A dictionary of GCP regions with their tier, city, and country. Methods: tier1: Returns a list of tier 1 GCP regions. @@ -136,7 +136,7 @@ class GCPRegions: attempts (int, optional): Number of ping attempts per region. Returns: - (List[Tuple[str, float, float, float, float]]): List of tuples containing region information and + (list[tuple[str, float, float, float, float]]): List of tuples containing region information and latency statistics. Each tuple contains (region, mean_latency, std_dev, min_latency, max_latency). Examples: diff --git a/ultralytics/hub/session.py b/ultralytics/hub/session.py index 4badfd48..180c1c2f 100644 --- a/ultralytics/hub/session.py +++ b/ultralytics/hub/session.py @@ -28,13 +28,13 @@ class HUBTrainingSession: Attributes: model_id (str): Identifier for the YOLO model being trained. model_url (str): URL for the model in Ultralytics HUB. - rate_limits (Dict[str, int]): Rate limits for different API calls in seconds. - timers (Dict[str, Any]): Timers for rate limiting. - metrics_queue (Dict[str, Any]): Queue for the model's metrics. - metrics_upload_failed_queue (Dict[str, Any]): Queue for metrics that failed to upload. + rate_limits (dict[str, int]): Rate limits for different API calls in seconds. + timers (dict[str, Any]): Timers for rate limiting. + metrics_queue (dict[str, Any]): Queue for the model's metrics. + metrics_upload_failed_queue (dict[str, Any]): Queue for metrics that failed to upload. model (Any): Model data fetched from Ultralytics HUB. model_file (str): Path to the model file. - train_args (Dict[str, Any]): Arguments for training the model. + train_args (dict[str, Any]): Arguments for training the model. client (Any): Client for interacting with Ultralytics HUB. filename (str): Filename of the model. @@ -98,7 +98,7 @@ class HUBTrainingSession: Args: identifier (str): Model identifier used to initialize the HUB training session. - args (Dict[str, Any], optional): Arguments for creating a new model if identifier is not a HUB model URL. + args (dict[str, Any], optional): Arguments for creating a new model if identifier is not a HUB model URL. Returns: session (HUBTrainingSession | None): An authenticated session or None if creation fails. @@ -144,7 +144,7 @@ class HUBTrainingSession: Initialize a HUB training session with the specified model arguments. Args: - model_args (Dict[str, Any]): Arguments for creating the model, including batch size, epochs, image size, + model_args (dict[str, Any]): Arguments for creating the model, including batch size, epochs, image size, etc. Returns: diff --git a/ultralytics/models/fastsam/model.py b/ultralytics/models/fastsam/model.py index 3e620ff8..f5156858 100644 --- a/ultralytics/models/fastsam/model.py +++ b/ultralytics/models/fastsam/model.py @@ -63,14 +63,14 @@ class FastSAM(Model): source (str | PIL.Image | np.ndarray): Input source for prediction, can be a file path, URL, PIL image, or numpy array. stream (bool): Whether to enable real-time streaming mode for video inputs. - bboxes (List, optional): Bounding box coordinates for prompted segmentation in format [[x1, y1, x2, y2]]. - points (List, optional): Point coordinates for prompted segmentation in format [[x, y]]. - labels (List, optional): Class labels for prompted segmentation. - texts (List, optional): Text prompts for segmentation guidance. + bboxes (list, optional): Bounding box coordinates for prompted segmentation in format [[x1, y1, x2, y2]]. + points (list, optional): Point coordinates for prompted segmentation in format [[x, y]]. + labels (list, optional): Class labels for prompted segmentation. + texts (list, optional): Text prompts for segmentation guidance. **kwargs (Any): Additional keyword arguments passed to the predictor. Returns: - (List): List of Results objects containing the prediction results. + (list): List of Results objects containing the prediction results. """ prompts = dict(bboxes=bboxes, points=points, labels=labels, texts=texts) return super().predict(source, stream, prompts=prompts, **kwargs) diff --git a/ultralytics/models/fastsam/predict.py b/ultralytics/models/fastsam/predict.py index 3af3010b..2e7c6938 100644 --- a/ultralytics/models/fastsam/predict.py +++ b/ultralytics/models/fastsam/predict.py @@ -52,12 +52,12 @@ class FastSAMPredictor(SegmentationPredictor): Apply postprocessing to FastSAM predictions and handle prompts. Args: - preds (List[torch.Tensor]): Raw predictions from the model. + preds (list[torch.Tensor]): Raw predictions from the model. img (torch.Tensor): Input image tensor that was fed to the model. - orig_imgs (List[np.ndarray]): Original images before preprocessing. + orig_imgs (list[np.ndarray]): Original images before preprocessing. Returns: - (List[Results]): Processed results with prompts applied. + (list[Results]): Processed results with prompts applied. """ bboxes = self.prompts.pop("bboxes", None) points = self.prompts.pop("points", None) @@ -80,14 +80,14 @@ class FastSAMPredictor(SegmentationPredictor): Perform image segmentation inference based on cues like bounding boxes, points, and text prompts. Args: - results (Results | List[Results]): Original inference results from FastSAM models without any prompts. - bboxes (np.ndarray | List, optional): Bounding boxes with shape (N, 4), in XYXY format. - points (np.ndarray | List, optional): Points indicating object locations with shape (N, 2), in pixels. - labels (np.ndarray | List, optional): Labels for point prompts, shape (N, ). 1 = foreground, 0 = background. - texts (str | List[str], optional): Textual prompts, a list containing string objects. + results (Results | list[Results]): Original inference results from FastSAM models without any prompts. + bboxes (np.ndarray | list, optional): Bounding boxes with shape (N, 4), in XYXY format. + points (np.ndarray | list, optional): Points indicating object locations with shape (N, 2), in pixels. + labels (np.ndarray | list, optional): Labels for point prompts, shape (N, ). 1 = foreground, 0 = background. + texts (str | list[str], optional): Textual prompts, a list containing string objects. Returns: - (List[Results]): Output results filtered and determined by the provided prompts. + (list[Results]): Output results filtered and determined by the provided prompts. """ if bboxes is None and points is None and texts is None: return results @@ -154,8 +154,8 @@ class FastSAMPredictor(SegmentationPredictor): Perform CLIP inference to calculate similarity between images and text prompts. Args: - images (List[PIL.Image]): List of source images, each should be PIL.Image with RGB channel order. - texts (List[str]): List of prompt texts, each should be a string object. + images (list[PIL.Image]): List of source images, each should be PIL.Image with RGB channel order. + texts (list[str]): List of prompt texts, each should be a string object. Returns: (torch.Tensor): Similarity matrix between given images and texts with shape (M, N). diff --git a/ultralytics/models/nas/model.py b/ultralytics/models/nas/model.py index 32905ed2..eb740c0a 100644 --- a/ultralytics/models/nas/model.py +++ b/ultralytics/models/nas/model.py @@ -91,7 +91,7 @@ class NAS(Model): verbose (bool): Controls verbosity. Returns: - (Dict[str, Any]): Model information dictionary. + (dict[str, Any]): Model information dictionary. """ return model_info(self.model, detailed=detailed, verbose=verbose, imgsz=640) diff --git a/ultralytics/models/rtdetr/predict.py b/ultralytics/models/rtdetr/predict.py index c3be1faa..804ef787 100644 --- a/ultralytics/models/rtdetr/predict.py +++ b/ultralytics/models/rtdetr/predict.py @@ -47,7 +47,7 @@ class RTDETRPredictor(BasePredictor): orig_imgs (list | torch.Tensor): Original, unprocessed images. Returns: - results (List[Results]): A list of Results objects containing the post-processed bounding boxes, + results (list[Results]): A list of Results objects containing the post-processed bounding boxes, confidence scores, and class labels. """ if not isinstance(preds, (list, tuple)): # list for PyTorch inference but list[0] Tensor for export inference @@ -82,7 +82,7 @@ class RTDETRPredictor(BasePredictor): (640) and scale_filled. Args: - im (List[np.ndarray] | torch.Tensor): Input images of shape (N, 3, H, W) for tensor, + im (list[np.ndarray] | torch.Tensor): Input images of shape (N, 3, H, W) for tensor, [(H, W, 3) x N] for list. Returns: diff --git a/ultralytics/models/rtdetr/val.py b/ultralytics/models/rtdetr/val.py index c257eb7f..5fbfe561 100644 --- a/ultralytics/models/rtdetr/val.py +++ b/ultralytics/models/rtdetr/val.py @@ -163,11 +163,11 @@ class RTDETRValidator(DetectionValidator): Apply Non-maximum suppression to prediction outputs. Args: - preds (torch.Tensor | List | Tuple): Raw predictions from the model. If tensor, should have shape + preds (torch.Tensor | list | tuple): Raw predictions from the model. If tensor, should have shape (batch_size, num_predictions, num_classes + 4) where last dimension contains bbox coords and class scores. Returns: - (List[Dict[str, torch.Tensor]]): List of dictionaries for each image, each containing: + (list[dict[str, torch.Tensor]]): List of dictionaries for each image, each containing: - 'bboxes': Tensor of shape (N, 4) with bounding box coordinates - 'conf': Tensor of shape (N,) with confidence scores - 'cls': Tensor of shape (N,) with class indices @@ -194,9 +194,9 @@ class RTDETRValidator(DetectionValidator): Serialize YOLO predictions to COCO json format. Args: - predn (Dict[str, torch.Tensor]): Predictions dictionary containing 'bboxes', 'conf', and 'cls' keys + predn (dict[str, torch.Tensor]): Predictions dictionary containing 'bboxes', 'conf', and 'cls' keys with bounding box coordinates, confidence scores, and class predictions. - pbatch (Dict[str, Any]): Batch dictionary containing 'imgsz', 'ori_shape', 'ratio_pad', and 'im_file'. + pbatch (dict[str, Any]): Batch dictionary containing 'imgsz', 'ori_shape', 'ratio_pad', and 'im_file'. """ path = Path(pbatch["im_file"]) stem = path.stem diff --git a/ultralytics/models/sam/amg.py b/ultralytics/models/sam/amg.py index 9effd0b7..f5e78040 100644 --- a/ultralytics/models/sam/amg.py +++ b/ultralytics/models/sam/amg.py @@ -19,8 +19,8 @@ def is_box_near_crop_edge( Args: boxes (torch.Tensor): Bounding boxes in XYXY format. - crop_box (List[int]): Crop box coordinates in [x0, y0, x1, y1] format. - orig_box (List[int]): Original image box coordinates in [x0, y0, x1, y1] format. + crop_box (list[int]): Crop box coordinates in [x0, y0, x1, y1] format. + orig_box (list[int]): Original image box coordinates in [x0, y0, x1, y1] format. atol (float, optional): Absolute tolerance for edge proximity detection. Returns: @@ -53,7 +53,7 @@ def batch_iterator(batch_size: int, *args) -> Generator[list[Any]]: *args (Any): Variable length input iterables to batch. All iterables must have the same length. Yields: - (List[Any]): A list of batched elements from each input iterable. + (list[Any]): A list of batched elements from each input iterable. Examples: >>> data = [1, 2, 3, 4, 5] @@ -121,13 +121,13 @@ def generate_crop_boxes( Generate crop boxes of varying sizes for multiscale image processing, with layered overlapping regions. Args: - im_size (Tuple[int, ...]): Height and width of the input image. + im_size (tuple[int, ...]): Height and width of the input image. n_layers (int): Number of layers to generate crop boxes for. overlap_ratio (float): Ratio of overlap between adjacent crop boxes. Returns: - crop_boxes (List[List[int]]): List of crop boxes in [x0, y0, x1, y1] format. - layer_idxs (List[int]): List of layer indices corresponding to each crop box. + crop_boxes (list[list[int]]): List of crop boxes in [x0, y0, x1, y1] format. + layer_idxs (list[int]): List of layer indices corresponding to each crop box. Examples: >>> im_size = (800, 1200) # Height, width diff --git a/ultralytics/models/sam/build.py b/ultralytics/models/sam/build.py index ac3f6a57..b5af7aba 100644 --- a/ultralytics/models/sam/build.py +++ b/ultralytics/models/sam/build.py @@ -130,10 +130,10 @@ def _build_sam( Build a Segment Anything Model (SAM) with specified encoder parameters. Args: - encoder_embed_dim (int | List[int]): Embedding dimension for the encoder. - encoder_depth (int | List[int]): Depth of the encoder. - encoder_num_heads (int | List[int]): Number of attention heads in the encoder. - encoder_global_attn_indexes (List[int] | None): Indexes for global attention in the encoder. + encoder_embed_dim (int | list[int]): Embedding dimension for the encoder. + encoder_depth (int | list[int]): Depth of the encoder. + encoder_num_heads (int | list[int]): Number of attention heads in the encoder. + encoder_global_attn_indexes (list[int] | None): Indexes for global attention in the encoder. checkpoint (str | None, optional): Path to the model checkpoint file. mobile_sam (bool, optional): Whether to build a Mobile-SAM model. @@ -228,12 +228,12 @@ def _build_sam2( Args: encoder_embed_dim (int, optional): Embedding dimension for the encoder. - encoder_stages (List[int], optional): Number of blocks in each stage of the encoder. + encoder_stages (list[int], optional): Number of blocks in each stage of the encoder. encoder_num_heads (int, optional): Number of attention heads in the encoder. - encoder_global_att_blocks (List[int], optional): Indices of global attention blocks in the encoder. - encoder_backbone_channel_list (List[int], optional): Channel dimensions for each level of the encoder backbone. - encoder_window_spatial_size (List[int], optional): Spatial size of the window for position embeddings. - encoder_window_spec (List[int], optional): Window specifications for each stage of the encoder. + encoder_global_att_blocks (list[int], optional): Indices of global attention blocks in the encoder. + encoder_backbone_channel_list (list[int], optional): Channel dimensions for each level of the encoder backbone. + encoder_window_spatial_size (list[int], optional): Spatial size of the window for position embeddings. + encoder_window_spec (list[int], optional): Window specifications for each stage of the encoder. checkpoint (str | None, optional): Path to the checkpoint file for loading pre-trained weights. Returns: diff --git a/ultralytics/models/sam/model.py b/ultralytics/models/sam/model.py index f59ba671..2a151f82 100644 --- a/ultralytics/models/sam/model.py +++ b/ultralytics/models/sam/model.py @@ -91,9 +91,9 @@ class SAM(Model): source (str | PIL.Image | np.ndarray): Path to the image or video file, or a PIL.Image object, or a np.ndarray object. stream (bool): If True, enables real-time streaming. - bboxes (List[List[float]] | None): List of bounding box coordinates for prompted segmentation. - points (List[List[float]] | None): List of points for prompted segmentation. - labels (List[int] | None): List of labels for prompted segmentation. + bboxes (list[list[float]] | None): List of bounding box coordinates for prompted segmentation. + points (list[list[float]] | None): List of points for prompted segmentation. + labels (list[int] | None): List of labels for prompted segmentation. **kwargs (Any): Additional keyword arguments for prediction. Returns: @@ -121,9 +121,9 @@ class SAM(Model): source (str | PIL.Image | np.ndarray | None): Path to the image or video file, or a PIL.Image object, or a np.ndarray object. stream (bool): If True, enables real-time streaming. - bboxes (List[List[float]] | None): List of bounding box coordinates for prompted segmentation. - points (List[List[float]] | None): List of points for prompted segmentation. - labels (List[int] | None): List of labels for prompted segmentation. + bboxes (list[list[float]] | None): List of bounding box coordinates for prompted segmentation. + points (list[list[float]] | None): List of points for prompted segmentation. + labels (list[int] | None): List of labels for prompted segmentation. **kwargs (Any): Additional keyword arguments to be passed to the predict method. Returns: @@ -160,7 +160,7 @@ class SAM(Model): Provide a mapping from the 'segment' task to its corresponding 'Predictor'. Returns: - (Dict[str, Dict[str, Type[Predictor]]]): A dictionary mapping the 'segment' task to its corresponding + (dict[str, dict[str, Type[Predictor]]]): A dictionary mapping the 'segment' task to its corresponding Predictor class. For SAM2 models, it maps to SAM2Predictor, otherwise to the standard Predictor. Examples: diff --git a/ultralytics/models/sam/modules/blocks.py b/ultralytics/models/sam/modules/blocks.py index 3e7777cf..2c68a34c 100644 --- a/ultralytics/models/sam/modules/blocks.py +++ b/ultralytics/models/sam/modules/blocks.py @@ -593,7 +593,7 @@ class MultiScaleBlock(nn.Module): norm1 (nn.Module): First normalization layer. window_size (int): Size of the window for partitioning. pool (nn.Module | None): Pooling layer for query downsampling. - q_stride (Tuple[int, int] | None): Stride for query pooling. + q_stride (tuple[int, int] | None): Stride for query pooling. attn (MultiScaleAttention): Multi-scale attention module. drop_path (nn.Module): Drop path layer for regularization. norm2 (nn.Module): Second normalization layer. @@ -934,7 +934,7 @@ class Block(nn.Module): use_rel_pos (bool): If True, uses relative positional embeddings in attention. rel_pos_zero_init (bool): If True, initializes relative positional parameters to zero. window_size (int): Size of attention window. If 0, uses global attention. - input_size (Tuple[int, int] | None): Input resolution for calculating relative positional parameter size. + input_size (tuple[int, int] | None): Input resolution for calculating relative positional parameter size. Examples: >>> block = Block(dim=256, num_heads=8, window_size=7) @@ -1026,7 +1026,7 @@ class REAttention(nn.Module): qkv_bias (bool): If True, adds a learnable bias to query, key, value projections. use_rel_pos (bool): If True, uses relative positional encodings. rel_pos_zero_init (bool): If True, initializes relative positional parameters to zero. - input_size (Tuple[int, int] | None): Input resolution for calculating relative positional parameter size. + input_size (tuple[int, int] | None): Input resolution for calculating relative positional parameter size. Required if use_rel_pos is True. Examples: @@ -1106,9 +1106,9 @@ class PatchEmbed(nn.Module): image data into a suitable format for subsequent transformer blocks. Args: - kernel_size (Tuple[int, int]): Size of the convolutional kernel for patch extraction. - stride (Tuple[int, int]): Stride of the convolutional operation. - padding (Tuple[int, int]): Padding applied to the input before convolution. + kernel_size (tuple[int, int]): Size of the convolutional kernel for patch extraction. + stride (tuple[int, int]): Stride of the convolutional operation. + padding (tuple[int, int]): Padding applied to the input before convolution. in_chans (int): Number of input image channels. embed_dim (int): Dimensionality of the output patch embeddings. diff --git a/ultralytics/models/sam/modules/decoders.py b/ultralytics/models/sam/modules/decoders.py index 3e7553cb..0797a8e2 100644 --- a/ultralytics/models/sam/modules/decoders.py +++ b/ultralytics/models/sam/modules/decoders.py @@ -329,7 +329,7 @@ class SAM2MaskDecoder(nn.Module): dense_prompt_embeddings (torch.Tensor): Embeddings of the mask inputs with shape (B, C, H, W). multimask_output (bool): Whether to return multiple masks or a single mask. repeat_image (bool): Flag to repeat the image embeddings. - high_res_features (List[torch.Tensor] | None, optional): Optional high-resolution features. + high_res_features (list[torch.Tensor] | None, optional): Optional high-resolution features. Returns: masks (torch.Tensor): Batched predicted masks with shape (B, N, H, W). diff --git a/ultralytics/models/sam/modules/encoders.py b/ultralytics/models/sam/modules/encoders.py index a894f9d5..d778f9e2 100644 --- a/ultralytics/models/sam/modules/encoders.py +++ b/ultralytics/models/sam/modules/encoders.py @@ -83,7 +83,7 @@ class ImageEncoderViT(nn.Module): use_rel_pos (bool): If True, adds relative positional embeddings to attention maps. rel_pos_zero_init (bool): If True, initializes relative positional parameters to zero. window_size (int): Size of attention window for windowed attention blocks. - global_attn_indexes (Tuple[int, ...]): Indices of blocks that use global attention. + global_attn_indexes (tuple[int, ...]): Indices of blocks that use global attention. Examples: >>> encoder = ImageEncoderViT(img_size=224, patch_size=16, embed_dim=768, depth=12, num_heads=12) @@ -161,13 +161,13 @@ class PromptEncoder(nn.Module): Attributes: embed_dim (int): Dimension of the embeddings. - input_image_size (Tuple[int, int]): Size of the input image as (H, W). - image_embedding_size (Tuple[int, int]): Spatial size of the image embedding as (H, W). + input_image_size (tuple[int, int]): Size of the input image as (H, W). + image_embedding_size (tuple[int, int]): Spatial size of the image embedding as (H, W). pe_layer (PositionEmbeddingRandom): Module for random position embedding. num_point_embeddings (int): Number of point embeddings for different types of points. point_embeddings (nn.ModuleList): List of point embeddings. not_a_point_embed (nn.Embedding): Embedding for points that are not part of any label. - mask_input_size (Tuple[int, int]): Size of the input mask. + mask_input_size (tuple[int, int]): Size of the input mask. mask_downscaling (nn.Sequential): Neural network for downscaling the mask. no_mask_embed (nn.Embedding): Embedding for cases where no mask is provided. @@ -198,8 +198,8 @@ class PromptEncoder(nn.Module): Args: embed_dim (int): The dimension of the embeddings. - image_embedding_size (Tuple[int, int]): The spatial size of the image embedding as (H, W). - input_image_size (Tuple[int, int]): The padded size of the input image as (H, W). + image_embedding_size (tuple[int, int]): The spatial size of the image embedding as (H, W). + input_image_size (tuple[int, int]): The padded size of the input image as (H, W). mask_in_chans (int): The number of hidden channels used for encoding input masks. activation (Type[nn.Module]): The activation function to use when encoding input masks. @@ -310,7 +310,7 @@ class PromptEncoder(nn.Module): Embed different types of prompts, returning both sparse and dense embeddings. Args: - points (Tuple[torch.Tensor, torch.Tensor] | None): Point coordinates and labels to embed. The first + points (tuple[torch.Tensor, torch.Tensor] | None): Point coordinates and labels to embed. The first tensor contains coordinates with shape (B, N, 2), and the second tensor contains labels with shape (B, N). boxes (torch.Tensor | None): Boxes to embed with shape (B, M, 2, 2), where M is the number of boxes. @@ -522,10 +522,10 @@ class FpnNeck(nn.Module): Attributes: position_encoding (PositionEmbeddingSine): Sinusoidal positional encoding module. convs (nn.ModuleList): List of convolutional layers for each backbone level. - backbone_channel_list (List[int]): List of channel dimensions from the backbone. + backbone_channel_list (list[int]): List of channel dimensions from the backbone. fpn_interp_model (str): Interpolation mode for FPN feature resizing. fuse_type (str): Type of feature fusion, either 'sum' or 'avg'. - fpn_top_down_levels (List[int]): Levels to have top-down features in outputs. + fpn_top_down_levels (list[int]): Levels to have top-down features in outputs. Methods: forward: Perform forward pass through the FPN neck. @@ -558,13 +558,13 @@ class FpnNeck(nn.Module): Args: d_model (int): Dimension of the model. - backbone_channel_list (List[int]): List of channel dimensions from the backbone. + backbone_channel_list (list[int]): List of channel dimensions from the backbone. kernel_size (int): Kernel size for the convolutional layers. stride (int): Stride for the convolutional layers. padding (int): Padding for the convolutional layers. fpn_interp_model (str): Interpolation mode for FPN feature resizing. fuse_type (str): Type of feature fusion, either 'sum' or 'avg'. - fpn_top_down_levels (Optional[List[int]]): Levels to have top-down features in outputs. + fpn_top_down_levels (Optional[list[int]]): Levels to have top-down features in outputs. Examples: >>> backbone_channels = [64, 128, 256, 512] @@ -610,12 +610,12 @@ class FpnNeck(nn.Module): and top-down feature fusion. It generates output feature maps and corresponding positional encodings. Args: - xs (List[torch.Tensor]): List of input tensors from the backbone, each with shape (B, C, H, W). + xs (list[torch.Tensor]): List of input tensors from the backbone, each with shape (B, C, H, W). Returns: - out (List[torch.Tensor]): List of output feature maps after FPN processing, each with shape + out (list[torch.Tensor]): List of output feature maps after FPN processing, each with shape (B, d_model, H, W). - pos (List[torch.Tensor]): List of positional encodings corresponding to each output feature map. + pos (list[torch.Tensor]): List of positional encodings corresponding to each output feature map. Examples: >>> fpn_neck = FpnNeck(d_model=256, backbone_channel_list=[64, 128, 256, 512]) @@ -664,18 +664,18 @@ class Hiera(nn.Module): with optional pooling and global attention mechanisms. Attributes: - window_spec (Tuple[int, ...]): Window sizes for each stage. - q_stride (Tuple[int, int]): Downsampling stride between stages. - stage_ends (List[int]): Indices of the last block in each stage. - q_pool_blocks (List[int]): Indices of blocks where pooling is applied. + window_spec (tuple[int, ...]): Window sizes for each stage. + q_stride (tuple[int, int]): Downsampling stride between stages. + stage_ends (list[int]): Indices of the last block in each stage. + q_pool_blocks (list[int]): Indices of blocks where pooling is applied. return_interm_layers (bool): Whether to return intermediate layer outputs. patch_embed (PatchEmbed): Module for patch embedding. - global_att_blocks (Tuple[int, ...]): Indices of blocks with global attention. - window_pos_embed_bkg_spatial_size (Tuple[int, int]): Spatial size for window positional embedding background. + global_att_blocks (tuple[int, ...]): Indices of blocks with global attention. + window_pos_embed_bkg_spatial_size (tuple[int, int]): Spatial size for window positional embedding background. pos_embed (nn.Parameter): Positional embedding for the background. pos_embed_window (nn.Parameter): Positional embedding for the window. blocks (nn.ModuleList): List of MultiScaleBlock modules. - channel_list (List[int]): List of output channel dimensions for each stage. + channel_list (list[int]): List of output channel dimensions for each stage. Methods: _get_pos_embed: Generate positional embeddings by interpolating and combining window and background embeddings. @@ -727,13 +727,13 @@ class Hiera(nn.Module): num_heads (int): Initial number of attention heads. drop_path_rate (float): Stochastic depth rate. q_pool (int): Number of query pooling stages. - q_stride (Tuple[int, int]): Downsampling stride between stages. - stages (Tuple[int, ...]): Number of blocks per stage. + q_stride (tuple[int, int]): Downsampling stride between stages. + stages (tuple[int, ...]): Number of blocks per stage. dim_mul (float): Dimension multiplier factor at stage transitions. head_mul (float): Head multiplier factor at stage transitions. - window_pos_embed_bkg_spatial_size (Tuple[int, int]): Spatial size for window positional embedding background. - window_spec (Tuple[int, ...]): Window sizes for each stage when not using global attention. - global_att_blocks (Tuple[int, ...]): Indices of blocks that use global attention. + window_pos_embed_bkg_spatial_size (tuple[int, int]): Spatial size for window positional embedding background. + window_spec (tuple[int, ...]): Window sizes for each stage when not using global attention. + global_att_blocks (tuple[int, ...]): Indices of blocks that use global attention. return_interm_layers (bool): Whether to return intermediate layer outputs. Examples: @@ -823,7 +823,7 @@ class Hiera(nn.Module): x (torch.Tensor): Input tensor with shape (B, C, H, W) representing a batch of images. Returns: - (List[torch.Tensor]): List of feature maps at different scales, each with shape (B, C_i, H_i, W_i), where + (list[torch.Tensor]): List of feature maps at different scales, each with shape (B, C_i, H_i, W_i), where C_i is the channel dimension and H_i, W_i are the spatial dimensions at scale i. The list is ordered from highest resolution (fine features) to lowest resolution (coarse features) if return_interm_layers is True, otherwise contains only the final output. diff --git a/ultralytics/models/sam/modules/sam.py b/ultralytics/models/sam/modules/sam.py index 413b112c..43cfe4e2 100644 --- a/ultralytics/models/sam/modules/sam.py +++ b/ultralytics/models/sam/modules/sam.py @@ -68,8 +68,8 @@ class SAMModel(nn.Module): image_encoder (ImageEncoderViT): The backbone used to encode the image into image embeddings. prompt_encoder (PromptEncoder): Encodes various types of input prompts. mask_decoder (MaskDecoder): Predicts masks from the image embeddings and encoded prompts. - pixel_mean (List[float]): Mean values for normalizing pixels in the input image. - pixel_std (List[float]): Standard deviation values for normalizing pixels in the input image. + pixel_mean (list[float]): Mean values for normalizing pixels in the input image. + pixel_std (list[float]): Standard deviation values for normalizing pixels in the input image. Examples: >>> image_encoder = ImageEncoderViT(...) @@ -435,14 +435,14 @@ class SAM2Model(torch.nn.Module): Args: backbone_features (torch.Tensor): Image features with shape (B, C, H, W). - point_inputs (Dict[str, torch.Tensor] | None): Dictionary containing point prompts. + point_inputs (dict[str, torch.Tensor] | None): Dictionary containing point prompts. 'point_coords': Tensor of shape (B, P, 2) with float32 dtype, containing absolute pixel-unit coordinates in (x, y) format for P input points. 'point_labels': Tensor of shape (B, P) with int32 dtype, where 1 means positive clicks, 0 means negative clicks, and -1 means padding. mask_inputs (torch.Tensor | None): Mask of shape (B, 1, H*16, W*16), float or bool, with the same spatial size as the image. - high_res_features (List[torch.Tensor] | None): List of two feature maps with shapes + high_res_features (list[torch.Tensor] | None): List of two feature maps with shapes (B, C, 4*H, 4*W) and (B, C, 2*H, 2*W) respectively, used as high-resolution feature maps for SAM decoder. multimask_output (bool): If True, output 3 candidate masks and their IoU estimates; if False, diff --git a/ultralytics/models/sam/modules/tiny_encoder.py b/ultralytics/models/sam/modules/tiny_encoder.py index 905f029b..ea8ea9c9 100644 --- a/ultralytics/models/sam/modules/tiny_encoder.py +++ b/ultralytics/models/sam/modules/tiny_encoder.py @@ -81,7 +81,7 @@ class PatchEmbed(nn.Module): effectively downsampling the spatial dimensions while increasing the channel dimension. Attributes: - patches_resolution (Tuple[int, int]): Resolution of the patches after embedding. + patches_resolution (tuple[int, int]): Resolution of the patches after embedding. num_patches (int): Total number of patches. in_chans (int): Number of input channels. embed_dim (int): Dimension of the embedding. @@ -203,7 +203,7 @@ class PatchMerging(nn.Module): resolution while potentially increasing channel dimensions. Attributes: - input_resolution (Tuple[int, int]): The input resolution (height, width) of the feature map. + input_resolution (tuple[int, int]): The input resolution (height, width) of the feature map. dim (int): The input dimension of the feature map. out_dim (int): The output dimension after merging and projection. act (nn.Module): The activation function used between convolutions. @@ -225,7 +225,7 @@ class PatchMerging(nn.Module): Initialize the PatchMerging module for merging and projecting neighboring patches in feature maps. Args: - input_resolution (Tuple[int, int]): The input resolution (height, width) of the feature map. + input_resolution (tuple[int, int]): The input resolution (height, width) of the feature map. dim (int): The input dimension of the feature map. out_dim (int): The output dimension after merging and projection. activation (nn.Module): The activation function used between convolutions. @@ -267,7 +267,7 @@ class ConvLayer(nn.Module): Attributes: dim (int): Dimensionality of the input and output. - input_resolution (Tuple[int, int]): Resolution of the input image. + input_resolution (tuple[int, int]): Resolution of the input image. depth (int): Number of MBConv layers in the block. use_checkpoint (bool): Whether to use gradient checkpointing to save memory. blocks (nn.ModuleList): List of MBConv layers. @@ -301,10 +301,10 @@ class ConvLayer(nn.Module): Args: dim (int): The dimensionality of the input and output. - input_resolution (Tuple[int, int]): The resolution of the input image. + input_resolution (tuple[int, int]): The resolution of the input image. depth (int): The number of MBConv layers in the block. activation (nn.Module): Activation function applied after each convolution. - drop_path (float | List[float], optional): Drop path rate. Single float or a list of floats for each MBConv. + drop_path (float | list[float], optional): Drop path rate. Single float or a list of floats for each MBConv. downsample (Optional[nn.Module], optional): Function for downsampling the output. None to skip downsampling. use_checkpoint (bool, optional): Whether to use gradient checkpointing to save memory. out_dim (Optional[int], optional): The dimensionality of the output. None means it will be the same as `dim`. @@ -456,7 +456,7 @@ class Attention(torch.nn.Module): key_dim (int): The dimensionality of the keys and queries. num_heads (int, optional): Number of attention heads. attn_ratio (float, optional): Attention ratio, affecting the dimensions of the value vectors. - resolution (Tuple[int, int], optional): Spatial resolution of the input feature map. + resolution (tuple[int, int], optional): Spatial resolution of the input feature map. """ super().__init__() @@ -530,7 +530,7 @@ class TinyViTBlock(nn.Module): Attributes: dim (int): The dimensionality of the input and output. - input_resolution (Tuple[int, int]): Spatial resolution of the input feature map. + input_resolution (tuple[int, int]): Spatial resolution of the input feature map. num_heads (int): Number of attention heads. window_size (int): Size of the attention window. mlp_ratio (float): Ratio of MLP hidden dimension to embedding dimension. @@ -567,7 +567,7 @@ class TinyViTBlock(nn.Module): Args: dim (int): Dimensionality of the input and output features. - input_resolution (Tuple[int, int]): Spatial resolution of the input feature map (height, width). + input_resolution (tuple[int, int]): Spatial resolution of the input feature map (height, width). num_heads (int): Number of attention heads. window_size (int, optional): Size of the attention window. Must be greater than 0. mlp_ratio (float, optional): Ratio of MLP hidden dimension to embedding dimension. @@ -674,7 +674,7 @@ class BasicLayer(nn.Module): Attributes: dim (int): The dimensionality of the input and output features. - input_resolution (Tuple[int, int]): Spatial resolution of the input feature map. + input_resolution (tuple[int, int]): Spatial resolution of the input feature map. depth (int): Number of TinyViT blocks in this layer. use_checkpoint (bool): Whether to use gradient checkpointing to save memory. blocks (nn.ModuleList): List of TinyViT blocks that make up this layer. @@ -712,13 +712,13 @@ class BasicLayer(nn.Module): Args: dim (int): Dimensionality of the input and output features. - input_resolution (Tuple[int, int]): Spatial resolution of the input feature map (height, width). + input_resolution (tuple[int, int]): Spatial resolution of the input feature map (height, width). depth (int): Number of TinyViT blocks in this layer. num_heads (int): Number of attention heads in each TinyViT block. window_size (int): Size of the local window for attention computation. mlp_ratio (float, optional): Ratio of MLP hidden dimension to embedding dimension. drop (float, optional): Dropout rate. - drop_path (float | List[float], optional): Stochastic depth rate. Can be a float or a list of floats for each block. + drop_path (float | list[float], optional): Stochastic depth rate. Can be a float or a list of floats for each block. downsample (nn.Module | None, optional): Downsampling layer at the end of the layer. None to skip downsampling. use_checkpoint (bool, optional): Whether to use gradient checkpointing to save memory. local_conv_size (int, optional): Kernel size for the local convolution in each TinyViT block. @@ -778,11 +778,11 @@ class TinyViT(nn.Module): Attributes: img_size (int): Input image size. num_classes (int): Number of classification classes. - depths (Tuple[int, int, int, int]): Number of blocks in each stage. + depths (tuple[int, int, int, int]): Number of blocks in each stage. num_layers (int): Total number of layers in the network. mlp_ratio (float): Ratio of MLP hidden dimension to embedding dimension. patch_embed (PatchEmbed): Module for patch embedding. - patches_resolution (Tuple[int, int]): Resolution of embedded patches. + patches_resolution (tuple[int, int]): Resolution of embedded patches. layers (nn.ModuleList): List of network layers. norm_head (nn.LayerNorm): Layer normalization for the classifier head. head (nn.Linear): Linear layer for final classification. @@ -823,10 +823,10 @@ class TinyViT(nn.Module): img_size (int, optional): Size of the input image. in_chans (int, optional): Number of input channels. num_classes (int, optional): Number of classes for classification. - embed_dims (Tuple[int, int, int, int], optional): Embedding dimensions for each stage. - depths (Tuple[int, int, int, int], optional): Number of blocks in each stage. - num_heads (Tuple[int, int, int, int], optional): Number of attention heads in each stage. - window_sizes (Tuple[int, int, int, int], optional): Window sizes for each stage. + embed_dims (tuple[int, int, int, int], optional): Embedding dimensions for each stage. + depths (tuple[int, int, int, int], optional): Number of blocks in each stage. + num_heads (tuple[int, int, int, int], optional): Number of attention heads in each stage. + window_sizes (tuple[int, int, int, int], optional): Window sizes for each stage. mlp_ratio (float, optional): Ratio of MLP hidden dim to embedding dim. drop_rate (float, optional): Dropout rate. drop_path_rate (float, optional): Stochastic depth rate. diff --git a/ultralytics/models/sam/modules/utils.py b/ultralytics/models/sam/modules/utils.py index 0afb19b0..e8923ff5 100644 --- a/ultralytics/models/sam/modules/utils.py +++ b/ultralytics/models/sam/modules/utils.py @@ -14,12 +14,12 @@ def select_closest_cond_frames(frame_idx: int, cond_frame_outputs: dict[int, Any Args: frame_idx (int): Current frame index. - cond_frame_outputs (Dict[int, Any]): Dictionary of conditioning frame outputs keyed by frame indices. + cond_frame_outputs (dict[int, Any]): Dictionary of conditioning frame outputs keyed by frame indices. max_cond_frame_num (int): Maximum number of conditioning frames to select. Returns: - selected_outputs (Dict[int, Any]): Selected items from cond_frame_outputs. - unselected_outputs (Dict[int, Any]): Items not selected from cond_frame_outputs. + selected_outputs (dict[int, Any]): Selected items from cond_frame_outputs. + unselected_outputs (dict[int, Any]): Items not selected from cond_frame_outputs. Examples: >>> frame_idx = 5 @@ -229,7 +229,7 @@ def window_partition(x: torch.Tensor, window_size: int): Returns: windows (torch.Tensor): Partitioned windows with shape (B * num_windows, window_size, window_size, C). - padded_h_w (Tuple[int, int]): Padded height and width before partition. + padded_h_w (tuple[int, int]): Padded height and width before partition. Examples: >>> x = torch.randn(1, 16, 16, 3) @@ -262,8 +262,8 @@ def window_unpartition(windows: torch.Tensor, window_size: int, pad_hw: tuple[in window_size, C), where B is the batch size, num_windows is the number of windows, window_size is the size of each window, and C is the number of channels. window_size (int): Size of each window. - pad_hw (Tuple[int, int]): Padded height and width (Hp, Wp) of the input before windowing. - hw (Tuple[int, int]): Original height and width (H, W) of the input before padding and windowing. + pad_hw (tuple[int, int]): Padded height and width (Hp, Wp) of the input before windowing. + hw (tuple[int, int]): Original height and width (H, W) of the input before padding and windowing. Returns: (torch.Tensor): Unpartitioned sequences with shape (B, H, W, C), where B is the batch size, H and W @@ -350,8 +350,8 @@ def add_decomposed_rel_pos( q (torch.Tensor): Query tensor in the attention layer with shape (B, q_h * q_w, C). rel_pos_h (torch.Tensor): Relative position embeddings for height axis with shape (Lh, C). rel_pos_w (torch.Tensor): Relative position embeddings for width axis with shape (Lw, C). - q_size (Tuple[int, int]): Spatial sequence size of query q as (q_h, q_w). - k_size (Tuple[int, int]): Spatial sequence size of key k as (k_h, k_w). + q_size (tuple[int, int]): Spatial sequence size of query q as (q_h, q_w). + k_size (tuple[int, int]): Spatial sequence size of key k as (k_h, k_w). Returns: (torch.Tensor): Updated attention map with added relative positional embeddings, shape diff --git a/ultralytics/models/sam/predict.py b/ultralytics/models/sam/predict.py index 8cf19501..7b34d3a2 100644 --- a/ultralytics/models/sam/predict.py +++ b/ultralytics/models/sam/predict.py @@ -51,7 +51,7 @@ class Predictor(BasePredictor): device (torch.device): The device (CPU or GPU) on which the model is loaded. im (torch.Tensor): The preprocessed input image. features (torch.Tensor): Extracted image features. - prompts (Dict[str, Any]): Dictionary to store various types of prompts (e.g., bboxes, points, masks). + prompts (dict[str, Any]): Dictionary to store various types of prompts (e.g., bboxes, points, masks). segment_all (bool): Flag to indicate if full image segmentation should be performed. mean (torch.Tensor): Mean values for image normalization. std (torch.Tensor): Standard deviation values for image normalization. @@ -116,7 +116,7 @@ class Predictor(BasePredictor): torch.Tensor and list of np.ndarray as input formats. Args: - im (torch.Tensor | List[np.ndarray]): Input image(s) in BCHW tensor format or list of HWC numpy arrays. + im (torch.Tensor | list[np.ndarray]): Input image(s) in BCHW tensor format or list of HWC numpy arrays. Returns: (torch.Tensor): The preprocessed image tensor, normalized and converted to the appropriate dtype. @@ -149,10 +149,10 @@ class Predictor(BasePredictor): Currently, batched inference is not supported; hence the list length should be 1. Args: - im (List[np.ndarray]): List containing a single image in HWC numpy array format. + im (list[np.ndarray]): List containing a single image in HWC numpy array format. Returns: - (List[np.ndarray]): List containing the transformed image. + (list[np.ndarray]): List containing the transformed image. Raises: AssertionError: If the input list contains more than one image. @@ -177,9 +177,9 @@ class Predictor(BasePredictor): Args: im (torch.Tensor): The preprocessed input image in tensor format, with shape (N, C, H, W). - bboxes (np.ndarray | List | None): Bounding boxes with shape (N, 4), in XYXY format. - points (np.ndarray | List | None): Points indicating object locations with shape (N, 2), in pixels. - labels (np.ndarray | List | None): Labels for point prompts, shape (N,). 1 = foreground, 0 = background. + bboxes (np.ndarray | list | None): Bounding boxes with shape (N, 4), in XYXY format. + points (np.ndarray | list | None): Points indicating object locations with shape (N, 2), in pixels. + labels (np.ndarray | list | None): Labels for point prompts, shape (N,). 1 = foreground, 0 = background. masks (np.ndarray | None): Low-resolution masks from previous predictions, shape (N, H, W). For SAM H=W=256. multimask_output (bool): Flag to return multiple masks. Helpful for ambiguous prompts. *args (Any): Additional positional arguments. @@ -215,9 +215,9 @@ class Predictor(BasePredictor): Args: im (torch.Tensor): Preprocessed input image tensor with shape (N, C, H, W). - bboxes (np.ndarray | List | None): Bounding boxes in XYXY format with shape (N, 4). - points (np.ndarray | List | None): Points indicating object locations with shape (N, 2) or (N, num_points, 2), in pixels. - labels (np.ndarray | List | None): Point prompt labels with shape (N) or (N, num_points). 1 for foreground, 0 for background. + bboxes (np.ndarray | list | None): Bounding boxes in XYXY format with shape (N, 4). + points (np.ndarray | list | None): Points indicating object locations with shape (N, 2) or (N, num_points, 2), in pixels. + labels (np.ndarray | list | None): Point prompt labels with shape (N) or (N, num_points). 1 for foreground, 0 for background. masks (np.ndarray | None): Low-res masks from previous predictions with shape (N, H, W). For SAM, H=W=256. multimask_output (bool): Flag to return multiple masks for ambiguous prompts. @@ -250,10 +250,10 @@ class Predictor(BasePredictor): Args: features (torch.Tensor): Extracted image features with shape (B, C, H, W) from the SAM model image encoder. - bboxes (np.ndarray | List[List[float]] | None): Bounding boxes in XYXY format with shape (N, 4). - points (np.ndarray | List[List[float]] | None): Object location points with shape (N, 2), in pixels. - labels (np.ndarray | List[int] | None): Point prompt labels with shape (N,). 1 = foreground, 0 = background. - masks (List[np.ndarray] | np.ndarray | None): Masks for the objects, where each mask is a 2D array. + bboxes (np.ndarray | list[list[float]] | None): Bounding boxes in XYXY format with shape (N, 4). + points (np.ndarray | list[list[float]] | None): Object location points with shape (N, 2), in pixels. + labels (np.ndarray | list[int] | None): Point prompt labels with shape (N,). 1 = foreground, 0 = background. + masks (list[np.ndarray] | np.ndarray | None): Masks for the objects, where each mask is a 2D array. multimask_output (bool): Flag to return multiple masks for ambiguous prompts. Returns: @@ -282,12 +282,12 @@ class Predictor(BasePredictor): Prepare and transform the input prompts for processing based on the destination shape. Args: - dst_shape (Tuple[int, int]): The target shape (height, width) for the prompts. - src_shape (Tuple[int, int]): The source shape (height, width) of the input image. - bboxes (np.ndarray | List | None): Bounding boxes in XYXY format with shape (N, 4). - points (np.ndarray | List | None): Points indicating object locations with shape (N, 2) or (N, num_points, 2), in pixels. - labels (np.ndarray | List | None): Point prompt labels with shape (N) or (N, num_points). 1 for foreground, 0 for background. - masks (List[np.ndarray] | np.ndarray | None): Masks for the objects, where each mask is a 2D array with shape (H, W). + dst_shape (tuple[int, int]): The target shape (height, width) for the prompts. + src_shape (tuple[int, int]): The source shape (height, width) of the input image. + bboxes (np.ndarray | list | None): Bounding boxes in XYXY format with shape (N, 4). + points (np.ndarray | list | None): Points indicating object locations with shape (N, 2) or (N, num_points, 2), in pixels. + labels (np.ndarray | list | None): Point prompt labels with shape (N) or (N, num_points). 1 for foreground, 0 for background. + masks (list[np.ndarray] | np.ndarray | None): Masks for the objects, where each mask is a 2D array with shape (H, W). Returns: bboxes (torch.Tensor | None): Transformed bounding boxes. @@ -351,7 +351,7 @@ class Predictor(BasePredictor): crop_n_layers (int): Number of layers for additional mask predictions on image crops. crop_overlap_ratio (float): Overlap between crops, scaled down in subsequent layers. crop_downscale_factor (int): Scaling factor for sampled points-per-side in each layer. - point_grids (List[np.ndarray] | None): Custom grids for point sampling normalized to [0,1]. + point_grids (list[np.ndarray] | None): Custom grids for point sampling normalized to [0,1]. points_stride (int): Number of points to sample along each side of the image. points_batch_size (int): Batch size for the number of points processed simultaneously. conf_thres (float): Confidence threshold [0,1] for filtering based on mask quality prediction. @@ -490,10 +490,10 @@ class Predictor(BasePredictor): - pred_scores (torch.Tensor): Confidence scores for each mask with shape (N, 1). - pred_bboxes (torch.Tensor, optional): Predicted bounding boxes if segment_all is True. img (torch.Tensor): The processed input image tensor with shape (C, H, W). - orig_imgs (List[np.ndarray] | torch.Tensor): The original, unprocessed images. + orig_imgs (list[np.ndarray] | torch.Tensor): The original, unprocessed images. Returns: - (List[Results]): List of Results objects containing detection masks, bounding boxes, and other + (list[Results]): List of Results objects containing detection masks, bounding boxes, and other metadata for each processed image. Examples: @@ -623,7 +623,7 @@ class Predictor(BasePredictor): Returns: new_masks (torch.Tensor): Processed masks with small regions removed, shape (N, H, W). - keep (List[int]): Indices of remaining masks after NMS, for filtering corresponding boxes. + keep (list[int]): Indices of remaining masks after NMS, for filtering corresponding boxes. Examples: >>> masks = torch.rand(5, 640, 640) > 0.5 # 5 random binary masks @@ -673,13 +673,13 @@ class Predictor(BasePredictor): Perform prompts preprocessing and inference on provided image features using the SAM model. Args: - features (torch.Tensor | Dict[str, Any]): Extracted image features from the SAM/SAM2 model image encoder. - src_shape (Tuple[int, int]): The source shape (height, width) of the input image. - dst_shape (Tuple[int, int] | None): The target shape (height, width) for the prompts. If None, defaults to (imgsz, imgsz). - bboxes (np.ndarray | List[List[float]] | None): Bounding boxes in xyxy format with shape (N, 4). - points (np.ndarray | List[List[float]] | None): Points indicating object locations with shape (N, 2), in pixels. - labels (np.ndarray | List[int] | None): Point prompt labels with shape (N, ). - masks (List[np.ndarray] | np.ndarray | None): Masks for the objects, where each mask is a 2D array. + features (torch.Tensor | dict[str, Any]): Extracted image features from the SAM/SAM2 model image encoder. + src_shape (tuple[int, int]): The source shape (height, width) of the input image. + dst_shape (tuple[int, int] | None): The target shape (height, width) for the prompts. If None, defaults to (imgsz, imgsz). + bboxes (np.ndarray | list[list[float]] | None): Bounding boxes in xyxy format with shape (N, 4). + points (np.ndarray | list[list[float]] | None): Points indicating object locations with shape (N, 2), in pixels. + labels (np.ndarray | list[int] | None): Point prompt labels with shape (N, ). + masks (list[np.ndarray] | np.ndarray | None): Masks for the objects, where each mask is a 2D array. multimask_output (bool): Flag to return multiple masks for ambiguous prompts. Returns: @@ -688,7 +688,7 @@ class Predictor(BasePredictor): Each box is in xyxy format with additional columns for score and class. Notes: - - The input features is a torch.Tensor of shape (B, C, H, W) if performing on SAM, or a Dict[str, Any] if performing on SAM2. + - The input features is a torch.Tensor of shape (B, C, H, W) if performing on SAM, or a dict[str, Any] if performing on SAM2. """ dst_shape = dst_shape or (self.args.imgsz, self.args.imgsz) prompts = self._prepare_prompts(dst_shape, src_shape, bboxes, points, labels, masks) @@ -714,12 +714,12 @@ class SAM2Predictor(Predictor): prompt-based inference. Attributes: - _bb_feat_sizes (List[tuple]): Feature sizes for different backbone levels. + _bb_feat_sizes (list[tuple]): Feature sizes for different backbone levels. model (torch.nn.Module): The loaded SAM2 model. device (torch.device): The device (CPU or GPU) on which the model is loaded. features (dict): Cached image features for efficient inference. segment_all (bool): Flag to indicate if all segments should be predicted. - prompts (Dict[str, Any]): Dictionary to store various types of prompts for inference. + prompts (dict[str, Any]): Dictionary to store various types of prompts for inference. Methods: get_model: Retrieve and initialize the SAM2 model. @@ -752,12 +752,12 @@ class SAM2Predictor(Predictor): Prepare and transform the input prompts for processing based on the destination shape. Args: - dst_shape (Tuple[int, int]): The target shape (height, width) for the prompts. - src_shape (Tuple[int, int]): The source shape (height, width) of the input image. - bboxes (np.ndarray | List | None): Bounding boxes in XYXY format with shape (N, 4). - points (np.ndarray | List | None): Points indicating object locations with shape (N, 2) or (N, num_points, 2), in pixels. - labels (np.ndarray | List | None): Point prompt labels with shape (N,) or (N, num_points). 1 for foreground, 0 for background. - masks (List | np.ndarray | None): Masks for the objects, where each mask is a 2D array. + dst_shape (tuple[int, int]): The target shape (height, width) for the prompts. + src_shape (tuple[int, int]): The source shape (height, width) of the input image. + bboxes (np.ndarray | list | None): Bounding boxes in XYXY format with shape (N, 4). + points (np.ndarray | list | None): Points indicating object locations with shape (N, 2) or (N, num_points, 2), in pixels. + labels (np.ndarray | list | None): Point prompt labels with shape (N,) or (N, num_points). 1 for foreground, 0 for background. + masks (list | np.ndarray | None): Masks for the objects, where each mask is a 2D array. Returns: points (torch.Tensor | None): Transformed points. @@ -842,13 +842,13 @@ class SAM2Predictor(Predictor): Perform inference on image features using the SAM2 model. Args: - features (torch.Tensor | Dict[str, Any]): Extracted image features with shape (B, C, H, W) from the SAM2 model image encoder, it + features (torch.Tensor | dict[str, Any]): Extracted image features with shape (B, C, H, W) from the SAM2 model image encoder, it could also be a dictionary including: - image_embed (torch.Tensor): Image embedding with shape (B, C, H, W). - - high_res_feats (List[torch.Tensor]): List of high-resolution feature maps from the backbone, each with shape (B, C, H, W). - points (np.ndarray | List[List[float]] | None): Object location points with shape (N, 2), in pixels. - labels (np.ndarray | List[int] | None): Point prompt labels with shape (N,). 1 = foreground, 0 = background. - masks (List[np.ndarray] | np.ndarray | None): Masks for the objects, where each mask is a 2D array. + - high_res_feats (list[torch.Tensor]): List of high-resolution feature maps from the backbone, each with shape (B, C, H, W). + points (np.ndarray | list[list[float]] | None): Object location points with shape (N, 2), in pixels. + labels (np.ndarray | list[int] | None): Point prompt labels with shape (N,). 1 = foreground, 0 = background. + masks (list[np.ndarray] | np.ndarray | None): Masks for the objects, where each mask is a 2D array. multimask_output (bool): Flag to return multiple masks for ambiguous prompts. img_idx (int): Index of the image in the batch to process. @@ -962,9 +962,9 @@ class SAM2VideoPredictor(SAM2Predictor): Args: im (torch.Tensor): The preprocessed input image in tensor format, with shape (N, C, H, W). - bboxes (np.ndarray | List, optional): Bounding boxes with shape (N, 4), in XYXY format. - points (np.ndarray | List, optional): Points indicating object locations with shape (N, 2), in pixels. - labels (np.ndarray | List, optional): Labels for point prompts, shape (N, ). 1 = foreground, 0 = background. + bboxes (np.ndarray | list, optional): Bounding boxes with shape (N, 4), in XYXY format. + points (np.ndarray | list, optional): Points indicating object locations with shape (N, 2), in pixels. + labels (np.ndarray | list, optional): Labels for point prompts, shape (N, ). 1 = foreground, 0 = background. masks (np.ndarray, optional): Low-resolution masks from previous predictions shape (N,H,W). For SAM H=W=256. Returns: @@ -1036,9 +1036,9 @@ class SAM2VideoPredictor(SAM2Predictor): the masks do not overlap, which can be useful for certain applications. Args: - preds (Tuple[torch.Tensor, torch.Tensor]): The predicted masks and scores from the model. + preds (tuple[torch.Tensor, torch.Tensor]): The predicted masks and scores from the model. img (torch.Tensor): The processed image tensor. - orig_imgs (List[np.ndarray]): The original images before processing. + orig_imgs (list[np.ndarray]): The original images before processing. Returns: (list): The post-processed predictions. @@ -1286,7 +1286,7 @@ class SAM2VideoPredictor(SAM2Predictor): Returns: vis_feats (torch.Tensor): The visual features extracted from the image. vis_pos_embed (torch.Tensor): The positional embeddings for the visual features. - feat_sizes (List[tuple]): A list containing the sizes of the extracted features. + feat_sizes (list[tuple]): A list containing the sizes of the extracted features. Note: - If `batch` is greater than 1, the features are expanded to fit the batch size. @@ -1442,11 +1442,11 @@ class SAM2VideoPredictor(SAM2Predictor): the current batch size. Args: - out_maskmem_pos_enc (List[torch.Tensor] | None): The positional encoding for mask memory. + out_maskmem_pos_enc (list[torch.Tensor] | None): The positional encoding for mask memory. Should be a list of tensors or None. Returns: - (List[torch.Tensor]): The positional encoding for mask memory, either cached or expanded. + (list[torch.Tensor]): The positional encoding for mask memory, either cached or expanded. Note: - The method assumes that `out_maskmem_pos_enc` is a list of tensors or None. @@ -1730,10 +1730,10 @@ class SAM2DynamicInteractivePredictor(SAM2Predictor): specified overrides Args: - cfg (Dict[str, Any]): Configuration dictionary containing default settings. - overrides (Dict[str, Any] | None): Dictionary of values to override default configuration. + cfg (dict[str, Any]): Configuration dictionary containing default settings. + overrides (dict[str, Any] | None): Dictionary of values to override default configuration. max_obj_num (int): Maximum number of objects to track. Default is 3. this is set to keep fix feature size for the model. - _callbacks (Dict[str, Any] | None): Dictionary of callback functions to customize behavior. + _callbacks (dict[str, Any] | None): Dictionary of callback functions to customize behavior. Examples: >>> predictor = SAM2DynamicInteractivePredictor(cfg=DEFAULT_CFG) @@ -1778,11 +1778,11 @@ class SAM2DynamicInteractivePredictor(SAM2Predictor): Args: im (torch.Tensor | np.ndarray): The input image tensor or numpy array. - bboxes (List[List[float]] | None): Optional list of bounding boxes to update the memory. - masks (List[torch.Tensor | np.ndarray] | None): Optional masks to update the memory. - points (List[List[float]] | None): Optional list of points to update the memory, each point is [x, y]. - labels (List[int] | None): Optional list of object IDs corresponding to the points (>0 for positive, 0 for negative). - obj_ids (List[int] | None): Optional list of object IDs corresponding to the prompts. + bboxes (list[list[float]] | None): Optional list of bounding boxes to update the memory. + masks (list[torch.Tensor | np.ndarray] | None): Optional masks to update the memory. + points (list[list[float]] | None): Optional list of points to update the memory, each point is [x, y]. + labels (list[int] | None): Optional list of object IDs corresponding to the points (>0 for positive, 0 for negative). + obj_ids (list[int] | None): Optional list of object IDs corresponding to the prompts. update_memory (bool): Flag to indicate whether to update the memory with new objects. Returns: @@ -1855,7 +1855,7 @@ class SAM2DynamicInteractivePredictor(SAM2Predictor): Append the imgState to the memory_bank and update the memory for the model. Args: - obj_ids (List[int]): List of object IDs corresponding to the prompts. + obj_ids (list[int]): List of object IDs corresponding to the prompts. points (torch.Tensor | None): Tensor of shape (B, N, 2) representing the input points for N objects. labels (torch.Tensor | None): Tensor of shape (B, N) representing the labels for the input points. masks (torch.Tensor | None): Optional tensor of shape (N, H, W) representing the input masks for N objects. @@ -2009,7 +2009,7 @@ class SAM2DynamicInteractivePredictor(SAM2Predictor): mask (torch.Tensor | None): The mask input for the object with shape (H, W). Returns: - current_out (Dict[str, Any]): A dictionary containing the current output with mask predictions and object pointers. + current_out (dict[str, Any]): A dictionary containing the current output with mask predictions and object pointers. Keys include 'point_inputs', 'mask_inputs', 'pred_masks', 'pred_masks_high_res', 'obj_ptr', 'object_score_logits'. """ if mask is not None and self.model.use_mask_input_as_output_without_sam: diff --git a/ultralytics/models/utils/loss.py b/ultralytics/models/utils/loss.py index 1bf9b06d..eca29e72 100644 --- a/ultralytics/models/utils/loss.py +++ b/ultralytics/models/utils/loss.py @@ -23,7 +23,7 @@ class DETRLoss(nn.Module): Attributes: nc (int): Number of classes. - loss_gain (Dict[str, float]): Coefficients for different loss components. + loss_gain (dict[str, float]): Coefficients for different loss components. aux_loss (bool): Whether to compute auxiliary losses. use_fl (bool): Whether to use FocalLoss. use_vfl (bool): Whether to use VarifocalLoss. @@ -55,7 +55,7 @@ class DETRLoss(nn.Module): Args: nc (int): Number of classes. - loss_gain (Dict[str, float], optional): Coefficients for different loss components. + loss_gain (dict[str, float], optional): Coefficients for different loss components. aux_loss (bool): Whether to use auxiliary losses from each decoder layer. use_fl (bool): Whether to use FocalLoss. use_vfl (bool): Whether to use VarifocalLoss. @@ -93,7 +93,7 @@ class DETRLoss(nn.Module): postfix (str, optional): String to append to the loss name for identification in multi-loss scenarios. Returns: - (Dict[str, torch.Tensor]): Dictionary containing classification loss value. + (dict[str, torch.Tensor]): Dictionary containing classification loss value. Notes: The function supports different classification loss types: @@ -133,7 +133,7 @@ class DETRLoss(nn.Module): postfix (str, optional): String to append to the loss names for identification in multi-loss scenarios. Returns: - (Dict[str, torch.Tensor]): Dictionary containing: + (dict[str, torch.Tensor]): Dictionary containing: - loss_bbox{postfix}: L1 loss between predicted and ground truth boxes, scaled by the bbox loss gain. - loss_giou{postfix}: GIoU loss between predicted and ground truth boxes, scaled by the giou loss gain. @@ -207,14 +207,14 @@ class DETRLoss(nn.Module): pred_scores (torch.Tensor): Predicted scores from auxiliary layers. gt_bboxes (torch.Tensor): Ground truth bounding boxes. gt_cls (torch.Tensor): Ground truth classes. - gt_groups (List[int]): Number of ground truths per image. - match_indices (List[Tuple], optional): Pre-computed matching indices. + gt_groups (list[int]): Number of ground truths per image. + match_indices (list[tuple], optional): Pre-computed matching indices. postfix (str, optional): String to append to loss names. masks (torch.Tensor, optional): Predicted masks if using segmentation. gt_mask (torch.Tensor, optional): Ground truth masks if using segmentation. Returns: - (Dict[str, torch.Tensor]): Dictionary of auxiliary losses. + (dict[str, torch.Tensor]): Dictionary of auxiliary losses. """ # NOTE: loss class, bbox, giou, mask, dice loss = torch.zeros(5 if masks is not None else 3, device=pred_bboxes.device) @@ -265,10 +265,10 @@ class DETRLoss(nn.Module): Extract batch indices, source indices, and destination indices from match indices. Args: - match_indices (List[Tuple]): List of tuples containing matched indices. + match_indices (list[tuple]): List of tuples containing matched indices. Returns: - batch_idx (Tuple[torch.Tensor, torch.Tensor]): Tuple containing (batch_idx, src_idx). + batch_idx (tuple[torch.Tensor, torch.Tensor]): Tuple containing (batch_idx, src_idx). dst_idx (torch.Tensor): Destination indices. """ batch_idx = torch.cat([torch.full_like(src, i) for i, (src, _) in enumerate(match_indices)]) @@ -285,7 +285,7 @@ class DETRLoss(nn.Module): Args: pred_bboxes (torch.Tensor): Predicted bounding boxes. gt_bboxes (torch.Tensor): Ground truth bounding boxes. - match_indices (List[Tuple]): List of tuples containing matched indices. + match_indices (list[tuple]): List of tuples containing matched indices. Returns: pred_assigned (torch.Tensor): Assigned predicted bounding boxes. @@ -325,14 +325,14 @@ class DETRLoss(nn.Module): pred_scores (torch.Tensor): Predicted class scores. gt_bboxes (torch.Tensor): Ground truth bounding boxes. gt_cls (torch.Tensor): Ground truth classes. - gt_groups (List[int]): Number of ground truths per image. + gt_groups (list[int]): Number of ground truths per image. masks (torch.Tensor, optional): Predicted masks if using segmentation. gt_mask (torch.Tensor, optional): Ground truth masks if using segmentation. postfix (str, optional): String to append to loss names. - match_indices (List[Tuple], optional): Pre-computed matching indices. + match_indices (list[tuple], optional): Pre-computed matching indices. Returns: - (Dict[str, torch.Tensor]): Dictionary of losses. + (dict[str, torch.Tensor]): Dictionary of losses. """ if match_indices is None: match_indices = self.matcher( @@ -370,12 +370,12 @@ class DETRLoss(nn.Module): Args: pred_bboxes (torch.Tensor): Predicted bounding boxes, shape (L, B, N, 4). pred_scores (torch.Tensor): Predicted class scores, shape (L, B, N, C). - batch (Dict[str, Any]): Batch information containing cls, bboxes, and gt_groups. + batch (dict[str, Any]): Batch information containing cls, bboxes, and gt_groups. postfix (str, optional): Postfix for loss names. **kwargs (Any): Additional arguments, may include 'match_indices'. Returns: - (Dict[str, torch.Tensor]): Computed losses, including main and auxiliary (if enabled). + (dict[str, torch.Tensor]): Computed losses, including main and auxiliary (if enabled). Notes: Uses last elements of pred_bboxes and pred_scores for main loss, and the rest for auxiliary losses if @@ -419,14 +419,14 @@ class RTDETRDetectionLoss(DETRLoss): Forward pass to compute detection loss with optional denoising loss. Args: - preds (Tuple[torch.Tensor, torch.Tensor]): Tuple containing predicted bounding boxes and scores. - batch (Dict[str, Any]): Batch data containing ground truth information. + preds (tuple[torch.Tensor, torch.Tensor]): Tuple containing predicted bounding boxes and scores. + batch (dict[str, Any]): Batch data containing ground truth information. dn_bboxes (torch.Tensor, optional): Denoising bounding boxes. dn_scores (torch.Tensor, optional): Denoising scores. - dn_meta (Dict[str, Any], optional): Metadata for denoising. + dn_meta (dict[str, Any], optional): Metadata for denoising. Returns: - (Dict[str, torch.Tensor]): Dictionary containing total loss and denoising loss if applicable. + (dict[str, torch.Tensor]): Dictionary containing total loss and denoising loss if applicable. """ pred_bboxes, pred_scores = preds total_loss = super().forward(pred_bboxes, pred_scores, batch) @@ -456,12 +456,12 @@ class RTDETRDetectionLoss(DETRLoss): Get match indices for denoising. Args: - dn_pos_idx (List[torch.Tensor]): List of tensors containing positive indices for denoising. + dn_pos_idx (list[torch.Tensor]): List of tensors containing positive indices for denoising. dn_num_group (int): Number of denoising groups. - gt_groups (List[int]): List of integers representing number of ground truths per image. + gt_groups (list[int]): List of integers representing number of ground truths per image. Returns: - (List[Tuple[torch.Tensor, torch.Tensor]]): List of tuples containing matched indices for denoising. + (list[tuple[torch.Tensor, torch.Tensor]]): List of tuples containing matched indices for denoising. """ dn_match_indices = [] idx_groups = torch.as_tensor([0, *gt_groups[:-1]]).cumsum_(0) diff --git a/ultralytics/models/utils/ops.py b/ultralytics/models/utils/ops.py index 5f0ca5d9..298123fa 100644 --- a/ultralytics/models/utils/ops.py +++ b/ultralytics/models/utils/ops.py @@ -22,7 +22,7 @@ class HungarianMatcher(nn.Module): used in end-to-end object detection models like DETR. Attributes: - cost_gain (Dict[str, float]): Dictionary of cost coefficients for 'class', 'bbox', 'giou', 'mask', and 'dice' + cost_gain (dict[str, float]): Dictionary of cost coefficients for 'class', 'bbox', 'giou', 'mask', and 'dice' components. use_fl (bool): Whether to use Focal Loss for classification cost calculation. with_mask (bool): Whether the model makes mask predictions. @@ -60,7 +60,7 @@ class HungarianMatcher(nn.Module): Initialize HungarianMatcher for optimal assignment of predicted and ground truth bounding boxes. Args: - cost_gain (Dict[str, float], optional): Dictionary of cost coefficients for different matching cost + cost_gain (dict[str, float], optional): Dictionary of cost coefficients for different matching cost components. Should contain keys 'class', 'bbox', 'giou', 'mask', and 'dice'. use_fl (bool): Whether to use Focal Loss for classification cost calculation. with_mask (bool): Whether the model makes mask predictions. @@ -100,12 +100,12 @@ class HungarianMatcher(nn.Module): num_classes). gt_bboxes (torch.Tensor): Ground truth bounding boxes with shape (num_gts, 4). gt_cls (torch.Tensor): Ground truth class labels with shape (num_gts,). - gt_groups (List[int]): Number of ground truth boxes for each image in the batch. + gt_groups (list[int]): Number of ground truth boxes for each image in the batch. masks (torch.Tensor, optional): Predicted masks with shape (batch_size, num_queries, height, width). - gt_mask (List[torch.Tensor], optional): Ground truth masks, each with shape (num_masks, Height, Width). + gt_mask (list[torch.Tensor], optional): Ground truth masks, each with shape (num_masks, Height, Width). Returns: - (List[Tuple[torch.Tensor, torch.Tensor]]): A list of size batch_size, each element is a tuple + (list[tuple[torch.Tensor, torch.Tensor]]): A list of size batch_size, each element is a tuple (index_i, index_j), where index_i is the tensor of indices of the selected predictions (in order) and index_j is the tensor of indices of the corresponding selected ground truth targets (in order). For each batch element, it holds: len(index_i) = len(index_j) = min(num_queries, num_target_boxes). @@ -205,8 +205,8 @@ def get_cdn_group( bounding boxes and class labels. It generates both positive and negative samples to improve model robustness. Args: - batch (Dict[str, Any]): Batch dictionary containing 'gt_cls' (torch.Tensor with shape (num_gts,)), - 'gt_bboxes' (torch.Tensor with shape (num_gts, 4)), and 'gt_groups' (List[int]) indicating number of + batch (dict[str, Any]): Batch dictionary containing 'gt_cls' (torch.Tensor with shape (num_gts,)), + 'gt_bboxes' (torch.Tensor with shape (num_gts, 4)), and 'gt_groups' (list[int]) indicating number of ground truths per image. num_classes (int): Total number of object classes. num_queries (int): Number of object queries. @@ -220,7 +220,7 @@ def get_cdn_group( padding_cls (torch.Tensor | None): Modified class embeddings for denoising with shape (bs, num_dn, embed_dim). padding_bbox (torch.Tensor | None): Modified bounding boxes for denoising with shape (bs, num_dn, 4). attn_mask (torch.Tensor | None): Attention mask for denoising with shape (tgt_size, tgt_size). - dn_meta (Dict[str, Any] | None): Meta information dictionary containing denoising parameters. + dn_meta (dict[str, Any] | None): Meta information dictionary containing denoising parameters. Examples: Generate denoising group for training diff --git a/ultralytics/models/yolo/classify/predict.py b/ultralytics/models/yolo/classify/predict.py index a517c530..4668abd1 100644 --- a/ultralytics/models/yolo/classify/predict.py +++ b/ultralytics/models/yolo/classify/predict.py @@ -78,10 +78,10 @@ class ClassificationPredictor(BasePredictor): Args: preds (torch.Tensor): Raw predictions from the model. img (torch.Tensor): Input images after preprocessing. - orig_imgs (List[np.ndarray] | torch.Tensor): Original images before preprocessing. + orig_imgs (list[np.ndarray] | torch.Tensor): Original images before preprocessing. Returns: - (List[Results]): List of Results objects containing classification results for each image. + (list[Results]): List of Results objects containing classification results for each image. """ if not isinstance(orig_imgs, list): # Input images are a torch.Tensor, not a list orig_imgs = ops.convert_torch2numpy_batch(orig_imgs) diff --git a/ultralytics/models/yolo/classify/train.py b/ultralytics/models/yolo/classify/train.py index 2f6b7260..702faee4 100644 --- a/ultralytics/models/yolo/classify/train.py +++ b/ultralytics/models/yolo/classify/train.py @@ -25,8 +25,8 @@ class ClassificationTrainer(BaseTrainer): Attributes: model (ClassificationModel): The classification model to be trained. - data (Dict[str, Any]): Dictionary containing dataset information including class names and number of classes. - loss_names (List[str]): Names of the loss functions used during training. + data (dict[str, Any]): Dictionary containing dataset information including class names and number of classes. + loss_names (list[str]): Names of the loss functions used during training. validator (ClassificationValidator): Validator instance for model evaluation. Methods: @@ -59,9 +59,9 @@ class ClassificationTrainer(BaseTrainer): image size if not specified. Args: - cfg (Dict[str, Any], optional): Default configuration dictionary containing training parameters. - overrides (Dict[str, Any], optional): Dictionary of parameter overrides for the default configuration. - _callbacks (List[Any], optional): List of callback functions to be executed during training. + cfg (dict[str, Any], optional): Default configuration dictionary containing training parameters. + overrides (dict[str, Any], optional): Dictionary of parameter overrides for the default configuration. + _callbacks (list[Any], optional): List of callback functions to be executed during training. Examples: Create a trainer with custom configuration @@ -196,8 +196,8 @@ class ClassificationTrainer(BaseTrainer): prefix (str, optional): Prefix to prepend to loss names. Returns: - keys (List[str]): List of loss keys if loss_items is None. - loss_dict (Dict[str, float]): Dictionary of loss items if loss_items is provided. + keys (list[str]): List of loss keys if loss_items is None. + loss_dict (dict[str, float]): Dictionary of loss items if loss_items is provided. """ keys = [f"{prefix}/{x}" for x in self.loss_names] if loss_items is None: @@ -227,7 +227,7 @@ class ClassificationTrainer(BaseTrainer): Plot training samples with their annotations. Args: - batch (Dict[str, torch.Tensor]): Batch containing images and class labels. + batch (dict[str, torch.Tensor]): Batch containing images and class labels. ni (int): Number of iterations. """ batch["batch_idx"] = torch.arange(len(batch["img"])) # add batch index for plotting diff --git a/ultralytics/models/yolo/classify/val.py b/ultralytics/models/yolo/classify/val.py index 7451c235..7099deee 100644 --- a/ultralytics/models/yolo/classify/val.py +++ b/ultralytics/models/yolo/classify/val.py @@ -22,8 +22,8 @@ class ClassificationValidator(BaseValidator): confusion matrix generation, and visualization of results. Attributes: - targets (List[torch.Tensor]): Ground truth class labels. - pred (List[torch.Tensor]): Model predictions. + targets (list[torch.Tensor]): Ground truth class labels. + pred (list[torch.Tensor]): Model predictions. metrics (ClassifyMetrics): Object to calculate and store classification metrics. names (dict): Mapping of class indices to class names. nc (int): Number of classes. @@ -170,7 +170,7 @@ class ClassificationValidator(BaseValidator): Plot validation image samples with their ground truth labels. Args: - batch (Dict[str, Any]): Dictionary containing batch data with 'img' (images) and 'cls' (class labels). + batch (dict[str, Any]): Dictionary containing batch data with 'img' (images) and 'cls' (class labels). ni (int): Batch index used for naming the output file. Examples: @@ -191,7 +191,7 @@ class ClassificationValidator(BaseValidator): Plot images with their predicted class labels and save the visualization. Args: - batch (Dict[str, Any]): Batch data containing images and other information. + batch (dict[str, Any]): Batch data containing images and other information. preds (torch.Tensor): Model predictions with shape (batch_size, num_classes). ni (int): Batch index used for naming the output file. diff --git a/ultralytics/models/yolo/detect/predict.py b/ultralytics/models/yolo/detect/predict.py index 60ad3514..a98252ac 100644 --- a/ultralytics/models/yolo/detect/predict.py +++ b/ultralytics/models/yolo/detect/predict.py @@ -96,12 +96,12 @@ class DetectionPredictor(BasePredictor): Construct a list of Results objects from model predictions. Args: - preds (List[torch.Tensor]): List of predicted bounding boxes and scores for each image. + preds (list[torch.Tensor]): List of predicted bounding boxes and scores for each image. img (torch.Tensor): Batch of preprocessed images used for inference. - orig_imgs (List[np.ndarray]): List of original images before preprocessing. + orig_imgs (list[np.ndarray]): List of original images before preprocessing. Returns: - (List[Results]): List of Results objects containing detection information for each image. + (list[Results]): List of Results objects containing detection information for each image. """ return [ self.construct_result(pred, img, orig_img, img_path) diff --git a/ultralytics/models/yolo/detect/train.py b/ultralytics/models/yolo/detect/train.py index 44316b83..fd86f00b 100644 --- a/ultralytics/models/yolo/detect/train.py +++ b/ultralytics/models/yolo/detect/train.py @@ -29,7 +29,7 @@ class DetectionTrainer(BaseTrainer): Attributes: model (DetectionModel): The YOLO detection model being trained. - data (Dict): Dictionary containing dataset information including class names and number of classes. + data (dict): Dictionary containing dataset information including class names and number of classes. loss_names (tuple): Names of the loss components used in training (box_loss, cls_loss, dfl_loss). Methods: @@ -96,10 +96,10 @@ class DetectionTrainer(BaseTrainer): Preprocess a batch of images by scaling and converting to float. Args: - batch (Dict): Dictionary containing batch data with 'img' tensor. + batch (dict): Dictionary containing batch data with 'img' tensor. Returns: - (Dict): Preprocessed batch with normalized images. + (dict): Preprocessed batch with normalized images. """ batch["img"] = batch["img"].to(self.device, non_blocking=True).float() / 255 if self.args.multi_scale: @@ -158,11 +158,11 @@ class DetectionTrainer(BaseTrainer): Return a loss dict with labeled training loss items tensor. Args: - loss_items (List[float], optional): List of loss values. + loss_items (list[float], optional): List of loss values. prefix (str): Prefix for keys in the returned dictionary. Returns: - (Dict | List): Dictionary of labeled loss items if loss_items is provided, otherwise list of keys. + (dict | list): Dictionary of labeled loss items if loss_items is provided, otherwise list of keys. """ keys = [f"{prefix}/{x}" for x in self.loss_names] if loss_items is not None: @@ -186,7 +186,7 @@ class DetectionTrainer(BaseTrainer): Plot training samples with their annotations. Args: - batch (Dict[str, Any]): Dictionary containing batch data. + batch (dict[str, Any]): Dictionary containing batch data. ni (int): Number of iterations. """ plot_images( diff --git a/ultralytics/models/yolo/detect/val.py b/ultralytics/models/yolo/detect/val.py index 39bb5c9f..2b03e4cb 100644 --- a/ultralytics/models/yolo/detect/val.py +++ b/ultralytics/models/yolo/detect/val.py @@ -27,13 +27,13 @@ class DetectionValidator(BaseValidator): Attributes: is_coco (bool): Whether the dataset is COCO. is_lvis (bool): Whether the dataset is LVIS. - class_map (List[int]): Mapping from model class indices to dataset class indices. + class_map (list[int]): Mapping from model class indices to dataset class indices. metrics (DetMetrics): Object detection metrics calculator. iouv (torch.Tensor): IoU thresholds for mAP calculation. niou (int): Number of IoU thresholds. - lb (List[Any]): List for storing ground truth labels for hybrid saving. - jdict (List[Dict[str, Any]]): List for storing JSON detection results. - stats (Dict[str, List[torch.Tensor]]): Dictionary for storing statistics during validation. + lb (list[Any]): List for storing ground truth labels for hybrid saving. + jdict (list[dict[str, Any]]): List for storing JSON detection results. + stats (dict[str, list[torch.Tensor]]): Dictionary for storing statistics during validation. Examples: >>> from ultralytics.models.yolo.detect import DetectionValidator @@ -49,8 +49,8 @@ class DetectionValidator(BaseValidator): Args: dataloader (torch.utils.data.DataLoader, optional): Dataloader to use for validation. save_dir (Path, optional): Directory to save results. - args (Dict[str, Any], optional): Arguments for the validator. - _callbacks (List[Any], optional): List of callback functions. + args (dict[str, Any], optional): Arguments for the validator. + _callbacks (list[Any], optional): List of callback functions. """ super().__init__(dataloader, save_dir, args, _callbacks) self.is_coco = False @@ -66,10 +66,10 @@ class DetectionValidator(BaseValidator): Preprocess batch of images for YOLO validation. Args: - batch (Dict[str, Any]): Batch containing images and annotations. + batch (dict[str, Any]): Batch containing images and annotations. Returns: - (Dict[str, Any]): Preprocessed batch. + (dict[str, Any]): Preprocessed batch. """ batch["img"] = batch["img"].to(self.device, non_blocking=True) batch["img"] = (batch["img"].half() if self.args.half else batch["img"].float()) / 255 @@ -114,7 +114,7 @@ class DetectionValidator(BaseValidator): preds (torch.Tensor): Raw predictions from the model. Returns: - (List[Dict[str, torch.Tensor]]): Processed predictions after NMS, where each dict contains + (list[dict[str, torch.Tensor]]): Processed predictions after NMS, where each dict contains 'bboxes', 'conf', 'cls', and 'extra' tensors. """ outputs = nms.non_max_suppression( @@ -136,10 +136,10 @@ class DetectionValidator(BaseValidator): Args: si (int): Batch index. - batch (Dict[str, Any]): Batch data containing images and annotations. + batch (dict[str, Any]): Batch data containing images and annotations. Returns: - (Dict[str, Any]): Prepared batch with processed annotations. + (dict[str, Any]): Prepared batch with processed annotations. """ idx = batch["batch_idx"] == si cls = batch["cls"][idx].squeeze(-1) @@ -163,10 +163,10 @@ class DetectionValidator(BaseValidator): Prepare predictions for evaluation against ground truth. Args: - pred (Dict[str, torch.Tensor]): Post-processed predictions from the model. + pred (dict[str, torch.Tensor]): Post-processed predictions from the model. Returns: - (Dict[str, torch.Tensor]): Prepared predictions in native space. + (dict[str, torch.Tensor]): Prepared predictions in native space. """ if self.args.single_cls: pred["cls"] *= 0 @@ -177,8 +177,8 @@ class DetectionValidator(BaseValidator): Update metrics with new predictions and ground truth. Args: - preds (List[Dict[str, torch.Tensor]]): List of predictions from the model. - batch (Dict[str, Any]): Batch data containing ground truth. + preds (list[dict[str, torch.Tensor]]): List of predictions from the model. + batch (dict[str, Any]): Batch data containing ground truth. """ for si, pred in enumerate(preds): self.seen += 1 @@ -232,7 +232,7 @@ class DetectionValidator(BaseValidator): Calculate and return metrics statistics. Returns: - (Dict[str, Any]): Dictionary containing metrics results. + (dict[str, Any]): Dictionary containing metrics results. """ self.metrics.process(save_dir=self.save_dir, plot=self.args.plots, on_plot=self.on_plot) self.metrics.clear_stats() @@ -263,11 +263,11 @@ class DetectionValidator(BaseValidator): Return correct prediction matrix. Args: - preds (Dict[str, torch.Tensor]): Dictionary containing prediction data with 'bboxes' and 'cls' keys. - batch (Dict[str, Any]): Batch dictionary containing ground truth data with 'bboxes' and 'cls' keys. + preds (dict[str, torch.Tensor]): Dictionary containing prediction data with 'bboxes' and 'cls' keys. + batch (dict[str, Any]): Batch dictionary containing ground truth data with 'bboxes' and 'cls' keys. Returns: - (Dict[str, np.ndarray]): Dictionary containing 'tp' key with correct prediction matrix of shape (N, 10) for 10 IoU levels. + (dict[str, np.ndarray]): Dictionary containing 'tp' key with correct prediction matrix of shape (N, 10) for 10 IoU levels. """ if len(batch["cls"]) == 0 or len(preds["cls"]) == 0: return {"tp": np.zeros((len(preds["cls"]), self.niou), dtype=bool)} @@ -307,7 +307,7 @@ class DetectionValidator(BaseValidator): Plot validation image samples. Args: - batch (Dict[str, Any]): Batch containing images and annotations. + batch (dict[str, Any]): Batch containing images and annotations. ni (int): Batch index. """ plot_images( @@ -325,8 +325,8 @@ class DetectionValidator(BaseValidator): Plot predicted bounding boxes on input images and save the result. Args: - batch (Dict[str, Any]): Batch containing images and annotations. - preds (List[Dict[str, torch.Tensor]]): List of predictions from the model. + batch (dict[str, Any]): Batch containing images and annotations. + preds (list[dict[str, torch.Tensor]]): List of predictions from the model. ni (int): Batch index. max_det (Optional[int]): Maximum number of detections to plot. """ @@ -352,9 +352,9 @@ class DetectionValidator(BaseValidator): Save YOLO detections to a txt file in normalized coordinates in a specific format. Args: - predn (Dict[str, torch.Tensor]): Dictionary containing predictions with keys 'bboxes', 'conf', and 'cls'. + predn (dict[str, torch.Tensor]): Dictionary containing predictions with keys 'bboxes', 'conf', and 'cls'. save_conf (bool): Whether to save confidence scores. - shape (Tuple[int, int]): Shape of the original image (height, width). + shape (tuple[int, int]): Shape of the original image (height, width). file (Path): File path to save the detections. """ from ultralytics.engine.results import Results @@ -371,9 +371,9 @@ class DetectionValidator(BaseValidator): Serialize YOLO predictions to COCO json format. Args: - predn (Dict[str, torch.Tensor]): Predictions dictionary containing 'bboxes', 'conf', and 'cls' keys + predn (dict[str, torch.Tensor]): Predictions dictionary containing 'bboxes', 'conf', and 'cls' keys with bounding box coordinates, confidence scores, and class predictions. - pbatch (Dict[str, Any]): Batch dictionary containing 'imgsz', 'ori_shape', 'ratio_pad', and 'im_file'. + pbatch (dict[str, Any]): Batch dictionary containing 'imgsz', 'ori_shape', 'ratio_pad', and 'im_file'. Examples: >>> result = { @@ -417,10 +417,10 @@ class DetectionValidator(BaseValidator): Evaluate YOLO output in JSON format and return performance statistics. Args: - stats (Dict[str, Any]): Current statistics dictionary. + stats (dict[str, Any]): Current statistics dictionary. Returns: - (Dict[str, Any]): Updated statistics dictionary with COCO/LVIS evaluation results. + (dict[str, Any]): Updated statistics dictionary with COCO/LVIS evaluation results. """ pred_json = self.save_dir / "predictions.json" # predictions anno_json = ( @@ -446,16 +446,16 @@ class DetectionValidator(BaseValidator): including mAP50, mAP50-95, and LVIS-specific metrics if applicable. Args: - stats (Dict[str, Any]): Dictionary to store computed metrics and statistics. + stats (dict[str, Any]): Dictionary to store computed metrics and statistics. pred_json (str | Path]): Path to JSON file containing predictions in COCO format. anno_json (str | Path]): Path to JSON file containing ground truth annotations in COCO format. - iou_types (str | List[str]]): IoU type(s) for evaluation. Can be single string or list of strings. + iou_types (str | list[str]]): IoU type(s) for evaluation. Can be single string or list of strings. Common values include "bbox", "segm", "keypoints". Defaults to "bbox". - suffix (str | List[str]]): Suffix to append to metric names in stats dictionary. Should correspond + suffix (str | list[str]]): Suffix to append to metric names in stats dictionary. Should correspond to iou_types if multiple types provided. Defaults to "Box". Returns: - (Dict[str, Any]): Updated stats dictionary containing the computed COCO/LVIS evaluation metrics. + (dict[str, Any]): Updated stats dictionary containing the computed COCO/LVIS evaluation metrics. """ if self.args.save_json and (self.is_coco or self.is_lvis) and len(self.jdict): LOGGER.info(f"\nEvaluating faster-coco-eval mAP using {pred_json} and {anno_json}...") diff --git a/ultralytics/models/yolo/model.py b/ultralytics/models/yolo/model.py index 702a453e..3dc01b51 100644 --- a/ultralytics/models/yolo/model.py +++ b/ultralytics/models/yolo/model.py @@ -185,7 +185,7 @@ class YOLOWorld(Model): Set the model's class names for detection. Args: - classes (List[str]): A list of categories i.e. ["person"]. + classes (list[str]): A list of categories i.e. ["person"]. """ self.model.set_classes(classes) # Remove background if it's given @@ -299,8 +299,8 @@ class YOLOE(Model): classification tasks. The model must be an instance of YOLOEModel. Args: - vocab (List[str]): Vocabulary list containing tokens or words used by the model for text processing. - names (List[str]): List of class names that the model can detect or classify. + vocab (list[str]): Vocabulary list containing tokens or words used by the model for text processing. + names (list[str]): List of class names that the model can detect or classify. Raises: AssertionError: If the model is not an instance of YOLOEModel. @@ -322,7 +322,7 @@ class YOLOE(Model): Set the model's class names and embeddings for detection. Args: - classes (List[str]): A list of categories i.e. ["person"]. + classes (list[str]): A list of categories i.e. ["person"]. embeddings (torch.Tensor): Embeddings corresponding to the classes. """ assert isinstance(self.model, YOLOEModel) @@ -381,7 +381,7 @@ class YOLOE(Model): directory paths, URL/YouTube streams, PIL images, numpy arrays, or webcam indices. stream (bool): Whether to stream the prediction results. If True, results are yielded as a generator as they are computed. - visual_prompts (Dict[str, List]): Dictionary containing visual prompts for the model. Must include + visual_prompts (dict[str, list]): Dictionary containing visual prompts for the model. Must include 'bboxes' and 'cls' keys when non-empty. refer_image (str | PIL.Image | np.ndarray, optional): Reference image for visual prompts. predictor (callable, optional): Custom predictor function. If None, a predictor is automatically @@ -389,7 +389,7 @@ class YOLOE(Model): **kwargs (Any): Additional keyword arguments passed to the predictor. Returns: - (List | generator): List of Results objects or generator of Results objects if stream=True. + (list | generator): List of Results objects or generator of Results objects if stream=True. Examples: >>> model = YOLOE("yoloe-11s-seg.pt") diff --git a/ultralytics/models/yolo/obb/train.py b/ultralytics/models/yolo/obb/train.py index fe3dbeef..fe9f7920 100644 --- a/ultralytics/models/yolo/obb/train.py +++ b/ultralytics/models/yolo/obb/train.py @@ -45,7 +45,7 @@ class OBBTrainer(yolo.detect.DetectionTrainer): model configuration. overrides (dict, optional): Dictionary of parameter overrides for the configuration. Any values here will take precedence over those in cfg. - _callbacks (List[Any], optional): List of callback functions to be invoked during training. + _callbacks (list[Any], optional): List of callback functions to be invoked during training. Examples: >>> from ultralytics.models.yolo.obb import OBBTrainer diff --git a/ultralytics/models/yolo/obb/val.py b/ultralytics/models/yolo/obb/val.py index ff96281d..d560c929 100644 --- a/ultralytics/models/yolo/obb/val.py +++ b/ultralytics/models/yolo/obb/val.py @@ -77,13 +77,13 @@ class OBBValidator(DetectionValidator): Compute the correct prediction matrix for a batch of detections and ground truth bounding boxes. Args: - preds (Dict[str, torch.Tensor]): Prediction dictionary containing 'cls' and 'bboxes' keys with detected + preds (dict[str, torch.Tensor]): Prediction dictionary containing 'cls' and 'bboxes' keys with detected class labels and bounding boxes. - batch (Dict[str, torch.Tensor]): Batch dictionary containing 'cls' and 'bboxes' keys with ground truth + batch (dict[str, torch.Tensor]): Batch dictionary containing 'cls' and 'bboxes' keys with ground truth class labels and bounding boxes. Returns: - (Dict[str, np.ndarray]): Dictionary containing 'tp' key with the correct prediction matrix as a numpy + (dict[str, np.ndarray]): Dictionary containing 'tp' key with the correct prediction matrix as a numpy array with shape (N, 10), which includes 10 IoU levels for each detection, indicating the accuracy of predictions compared to the ground truth. @@ -104,7 +104,7 @@ class OBBValidator(DetectionValidator): preds (torch.Tensor): Raw predictions from the model. Returns: - (List[Dict[str, torch.Tensor]]): Processed predictions with angle information concatenated to bboxes. + (list[dict[str, torch.Tensor]]): Processed predictions with angle information concatenated to bboxes. """ preds = super().postprocess(preds) for pred in preds: @@ -117,7 +117,7 @@ class OBBValidator(DetectionValidator): Args: si (int): Batch index to process. - batch (Dict[str, Any]): Dictionary containing batch data with keys: + batch (dict[str, Any]): Dictionary containing batch data with keys: - batch_idx: Tensor of batch indices - cls: Tensor of class labels - bboxes: Tensor of bounding boxes @@ -126,7 +126,7 @@ class OBBValidator(DetectionValidator): - ratio_pad: Ratio and padding information Returns: - (Dict[str, Any]): Prepared batch data with scaled bounding boxes and metadata. + (dict[str, Any]): Prepared batch data with scaled bounding boxes and metadata. """ idx = batch["batch_idx"] == si cls = batch["cls"][idx].squeeze(-1) @@ -150,8 +150,8 @@ class OBBValidator(DetectionValidator): Plot predicted bounding boxes on input images and save the result. Args: - batch (Dict[str, Any]): Batch data containing images, file paths, and other metadata. - preds (List[torch.Tensor]): List of prediction tensors for each image in the batch. + batch (dict[str, Any]): Batch data containing images, file paths, and other metadata. + preds (list[torch.Tensor]): List of prediction tensors for each image in the batch. ni (int): Batch index used for naming the output file. Examples: @@ -170,9 +170,9 @@ class OBBValidator(DetectionValidator): Convert YOLO predictions to COCO JSON format with rotated bounding box information. Args: - predn (Dict[str, torch.Tensor]): Prediction dictionary containing 'bboxes', 'conf', and 'cls' keys + predn (dict[str, torch.Tensor]): Prediction dictionary containing 'bboxes', 'conf', and 'cls' keys with bounding box coordinates, confidence scores, and class predictions. - pbatch (Dict[str, Any]): Batch dictionary containing 'imgsz', 'ori_shape', 'ratio_pad', and 'im_file'. + pbatch (dict[str, Any]): Batch dictionary containing 'imgsz', 'ori_shape', 'ratio_pad', and 'im_file'. Notes: This method processes rotated bounding box predictions and converts them to both rbox format @@ -204,7 +204,7 @@ class OBBValidator(DetectionValidator): predn (torch.Tensor): Predicted detections with shape (N, 7) containing bounding boxes, confidence scores, class predictions, and angles in format (x, y, w, h, conf, cls, angle). save_conf (bool): Whether to save confidence scores in the text file. - shape (Tuple[int, int]): Original image shape in format (height, width). + shape (tuple[int, int]): Original image shape in format (height, width). file (Path): Output file path to save detections. Examples: @@ -237,10 +237,10 @@ class OBBValidator(DetectionValidator): Evaluate YOLO output in JSON format and save predictions in DOTA format. Args: - stats (Dict[str, Any]): Performance statistics dictionary. + stats (dict[str, Any]): Performance statistics dictionary. Returns: - (Dict[str, Any]): Updated performance statistics. + (dict[str, Any]): Updated performance statistics. """ if self.args.save_json and self.is_dota and len(self.jdict): import json diff --git a/ultralytics/models/yolo/pose/val.py b/ultralytics/models/yolo/pose/val.py index f81a1ad5..ca49c07a 100644 --- a/ultralytics/models/yolo/pose/val.py +++ b/ultralytics/models/yolo/pose/val.py @@ -22,7 +22,7 @@ class PoseValidator(DetectionValidator): Attributes: sigma (np.ndarray): Sigma values for OKS calculation, either OKS_SIGMA or ones divided by number of keypoints. - kpt_shape (List[int]): Shape of the keypoints, typically [17, 3] for COCO format. + kpt_shape (list[int]): Shape of the keypoints, typically [17, 3] for COCO format. args (dict): Arguments for the validator including task set to "pose". metrics (PoseMetrics): Metrics object for pose evaluation. @@ -132,7 +132,7 @@ class PoseValidator(DetectionValidator): bounding boxes, confidence scores, class predictions, and keypoint data. Returns: - (Dict[torch.Tensor]): Dict of processed prediction dictionaries, each containing: + (dict[torch.Tensor]): Dict of processed prediction dictionaries, each containing: - 'bboxes': Bounding box coordinates - 'conf': Confidence scores - 'cls': Class predictions @@ -154,10 +154,10 @@ class PoseValidator(DetectionValidator): Args: si (int): Batch index. - batch (Dict[str, Any]): Dictionary containing batch data with keys like 'keypoints', 'batch_idx', etc. + batch (dict[str, Any]): Dictionary containing batch data with keys like 'keypoints', 'batch_idx', etc. Returns: - (Dict[str, Any]): Prepared batch with keypoints scaled to original image dimensions. + (dict[str, Any]): Prepared batch with keypoints scaled to original image dimensions. Notes: This method extends the parent class's _prepare_batch method by adding keypoint processing. @@ -177,13 +177,13 @@ class PoseValidator(DetectionValidator): Return correct prediction matrix by computing Intersection over Union (IoU) between detections and ground truth. Args: - preds (Dict[str, torch.Tensor]): Dictionary containing prediction data with keys 'cls' for class predictions + preds (dict[str, torch.Tensor]): Dictionary containing prediction data with keys 'cls' for class predictions and 'keypoints' for keypoint predictions. - batch (Dict[str, Any]): Dictionary containing ground truth data with keys 'cls' for class labels, + batch (dict[str, Any]): Dictionary containing ground truth data with keys 'cls' for class labels, 'bboxes' for bounding boxes, and 'keypoints' for keypoint annotations. Returns: - (Dict[str, np.ndarray]): Dictionary containing the correct prediction matrix including 'tp_p' for pose + (dict[str, np.ndarray]): Dictionary containing the correct prediction matrix including 'tp_p' for pose true positives across 10 IoU levels. Notes: @@ -207,9 +207,9 @@ class PoseValidator(DetectionValidator): Save YOLO pose detections to a text file in normalized coordinates. Args: - predn (Dict[str, torch.Tensor]): Dictionary containing predictions with keys 'bboxes', 'conf', 'cls' and 'keypoints. + predn (dict[str, torch.Tensor]): Dictionary containing predictions with keys 'bboxes', 'conf', 'cls' and 'keypoints. save_conf (bool): Whether to save confidence scores. - shape (Tuple[int, int]): Shape of the original image (height, width). + shape (tuple[int, int]): Shape of the original image (height, width). file (Path): Output file path to save detections. Notes: @@ -234,9 +234,9 @@ class PoseValidator(DetectionValidator): to COCO format, and appends the results to the internal JSON dictionary (self.jdict). Args: - predn (Dict[str, torch.Tensor]): Prediction dictionary containing 'bboxes', 'conf', 'cls', + predn (dict[str, torch.Tensor]): Prediction dictionary containing 'bboxes', 'conf', 'cls', and 'keypoints' tensors. - pbatch (Dict[str, Any]): Batch dictionary containing 'imgsz', 'ori_shape', 'ratio_pad', and 'im_file'. + pbatch (dict[str, Any]): Batch dictionary containing 'imgsz', 'ori_shape', 'ratio_pad', and 'im_file'. Notes: The method extracts the image ID from the filename stem (either as an integer if numeric, or as a string), diff --git a/ultralytics/models/yolo/segment/predict.py b/ultralytics/models/yolo/segment/predict.py index ce459a09..7c06b4ca 100644 --- a/ultralytics/models/yolo/segment/predict.py +++ b/ultralytics/models/yolo/segment/predict.py @@ -71,13 +71,13 @@ class SegmentationPredictor(DetectionPredictor): Construct a list of result objects from the predictions. Args: - preds (List[torch.Tensor]): List of predicted bounding boxes, scores, and masks. + preds (list[torch.Tensor]): List of predicted bounding boxes, scores, and masks. img (torch.Tensor): The image after preprocessing. - orig_imgs (List[np.ndarray]): List of original images before preprocessing. - protos (List[torch.Tensor]): List of prototype masks. + orig_imgs (list[np.ndarray]): List of original images before preprocessing. + protos (list[torch.Tensor]): List of prototype masks. Returns: - (List[Results]): List of result objects containing the original images, image paths, class names, + (list[Results]): List of result objects containing the original images, image paths, class names, bounding boxes, and masks. """ return [ diff --git a/ultralytics/models/yolo/segment/train.py b/ultralytics/models/yolo/segment/train.py index a3f4924d..9d59809e 100644 --- a/ultralytics/models/yolo/segment/train.py +++ b/ultralytics/models/yolo/segment/train.py @@ -19,7 +19,7 @@ class SegmentationTrainer(yolo.detect.DetectionTrainer): functionality including model initialization, validation, and visualization. Attributes: - loss_names (Tuple[str]): Names of the loss components used during training. + loss_names (tuple[str]): Names of the loss components used during training. Examples: >>> from ultralytics.models.yolo.segment import SegmentationTrainer diff --git a/ultralytics/models/yolo/segment/val.py b/ultralytics/models/yolo/segment/val.py index a7ee3c78..1c0b65be 100644 --- a/ultralytics/models/yolo/segment/val.py +++ b/ultralytics/models/yolo/segment/val.py @@ -57,10 +57,10 @@ class SegmentationValidator(DetectionValidator): Preprocess batch of images for YOLO segmentation validation. Args: - batch (Dict[str, Any]): Batch containing images and annotations. + batch (dict[str, Any]): Batch containing images and annotations. Returns: - (Dict[str, Any]): Preprocessed batch. + (dict[str, Any]): Preprocessed batch. """ batch = super().preprocess(batch) batch["masks"] = batch["masks"].to(self.device, non_blocking=True).float() @@ -100,10 +100,10 @@ class SegmentationValidator(DetectionValidator): Post-process YOLO predictions and return output detections with proto. Args: - preds (List[torch.Tensor]): Raw predictions from the model. + preds (list[torch.Tensor]): Raw predictions from the model. Returns: - List[Dict[str, torch.Tensor]]: Processed detection predictions with masks. + list[dict[str, torch.Tensor]]: Processed detection predictions with masks. """ proto = preds[1][-1] if len(preds[1]) == 3 else preds[1] # second output is len 3 if pt, but only 1 if exported preds = super().postprocess(preds[0]) @@ -127,10 +127,10 @@ class SegmentationValidator(DetectionValidator): Args: si (int): Batch index. - batch (Dict[str, Any]): Batch data containing images and annotations. + batch (dict[str, Any]): Batch data containing images and annotations. Returns: - (Dict[str, Any]): Prepared batch with processed annotations. + (dict[str, Any]): Prepared batch with processed annotations. """ prepared_batch = super()._prepare_batch(si, batch) nl = len(prepared_batch["cls"]) @@ -151,11 +151,11 @@ class SegmentationValidator(DetectionValidator): Compute correct prediction matrix for a batch based on bounding boxes and optional masks. Args: - preds (Dict[str, torch.Tensor]): Dictionary containing predictions with keys like 'cls' and 'masks'. - batch (Dict[str, Any]): Dictionary containing batch data with keys like 'cls' and 'masks'. + preds (dict[str, torch.Tensor]): Dictionary containing predictions with keys like 'cls' and 'masks'. + batch (dict[str, Any]): Dictionary containing batch data with keys like 'cls' and 'masks'. Returns: - (Dict[str, np.ndarray]): A dictionary containing correct prediction matrices including 'tp_m' for mask IoU. + (dict[str, np.ndarray]): A dictionary containing correct prediction matrices including 'tp_m' for mask IoU. Notes: - If `masks` is True, the function computes IoU between predicted and ground truth masks. @@ -181,8 +181,8 @@ class SegmentationValidator(DetectionValidator): Plot batch predictions with masks and bounding boxes. Args: - batch (Dict[str, Any]): Batch containing images and annotations. - preds (List[Dict[str, torch.Tensor]]): List of predictions from the model. + batch (dict[str, Any]): Batch containing images and annotations. + preds (list[dict[str, torch.Tensor]]): List of predictions from the model. ni (int): Batch index. """ for p in preds: @@ -199,7 +199,7 @@ class SegmentationValidator(DetectionValidator): Args: predn (torch.Tensor): Predictions in the format (x1, y1, x2, y2, conf, class). save_conf (bool): Whether to save confidence scores. - shape (Tuple[int, int]): Shape of the original image. + shape (tuple[int, int]): Shape of the original image. file (Path): File path to save the detections. """ from ultralytics.engine.results import Results @@ -217,8 +217,8 @@ class SegmentationValidator(DetectionValidator): Save one JSON result for COCO evaluation. Args: - predn (Dict[str, torch.Tensor]): Predictions containing bboxes, masks, confidence scores, and classes. - pbatch (Dict[str, Any]): Batch dictionary containing 'imgsz', 'ori_shape', 'ratio_pad', and 'im_file'. + predn (dict[str, torch.Tensor]): Predictions containing bboxes, masks, confidence scores, and classes. + pbatch (dict[str, Any]): Batch dictionary containing 'imgsz', 'ori_shape', 'ratio_pad', and 'im_file'. """ from faster_coco_eval.core.mask import encode # noqa diff --git a/ultralytics/models/yolo/world/train.py b/ultralytics/models/yolo/world/train.py index f3311192..1690be7e 100644 --- a/ultralytics/models/yolo/world/train.py +++ b/ultralytics/models/yolo/world/train.py @@ -32,10 +32,10 @@ class WorldTrainer(DetectionTrainer): accelerate training with multi-modal data. Attributes: - text_embeddings (Dict[str, torch.Tensor] | None): Cached text embeddings for category names to accelerate + text_embeddings (dict[str, torch.Tensor] | None): Cached text embeddings for category names to accelerate training. model (WorldModel): The YOLO World model being trained. - data (Dict[str, Any]): Dataset configuration containing class information. + data (dict[str, Any]): Dataset configuration containing class information. args (Any): Training arguments and configuration. Methods: @@ -58,9 +58,9 @@ class WorldTrainer(DetectionTrainer): Initialize a WorldTrainer object with given arguments. Args: - cfg (Dict[str, Any]): Configuration for the trainer. - overrides (Dict[str, Any], optional): Configuration overrides. - _callbacks (List[Any], optional): List of callback functions. + cfg (dict[str, Any]): Configuration for the trainer. + overrides (dict[str, Any], optional): Configuration overrides. + _callbacks (list[Any], optional): List of callback functions. """ if overrides is None: overrides = {} @@ -72,7 +72,7 @@ class WorldTrainer(DetectionTrainer): Return WorldModel initialized with specified config and weights. Args: - cfg (Dict[str, Any] | str, optional): Model configuration. + cfg (dict[str, Any] | str, optional): Model configuration. weights (str, optional): Path to pretrained weights. verbose (bool): Whether to display model info. @@ -121,7 +121,7 @@ class WorldTrainer(DetectionTrainer): for these categories to improve training efficiency. Args: - datasets (List[Any]): List of datasets from which to extract category names. + datasets (list[Any]): List of datasets from which to extract category names. batch (int | None): Batch size used for processing. Notes: @@ -144,12 +144,12 @@ class WorldTrainer(DetectionTrainer): Generate text embeddings for a list of text samples. Args: - texts (List[str]): List of text samples to encode. + texts (list[str]): List of text samples to encode. batch (int): Batch size for processing. cache_dir (Path): Directory to save/load cached embeddings. Returns: - (Dict[str, torch.Tensor]): Dictionary mapping text samples to their embeddings. + (dict[str, torch.Tensor]): Dictionary mapping text samples to their embeddings. """ model = "clip:ViT-B/32" cache_path = cache_dir / f"text_embeddings_{model.replace(':', '_').replace('/', '_')}.pt" diff --git a/ultralytics/models/yolo/world/train_world.py b/ultralytics/models/yolo/world/train_world.py index bcf13334..9b77603c 100644 --- a/ultralytics/models/yolo/world/train_world.py +++ b/ultralytics/models/yolo/world/train_world.py @@ -94,7 +94,7 @@ class WorldTrainerFromScratch(WorldTrainer): standard YOLO datasets and grounding datasets with different formats. Args: - img_path (List[str] | str): Path to the folder containing images or list of paths. + img_path (list[str] | str): Path to the folder containing images or list of paths. mode (str): 'train' mode or 'val' mode, allowing customized augmentations for each mode. batch (int, optional): Size of batches, used for rectangular training/validation. diff --git a/ultralytics/models/yolo/yoloe/predict.py b/ultralytics/models/yolo/yoloe/predict.py index 57264023..ee9a5225 100644 --- a/ultralytics/models/yolo/yoloe/predict.py +++ b/ultralytics/models/yolo/yoloe/predict.py @@ -75,12 +75,12 @@ class YOLOEVPDetectPredictor(DetectionPredictor): else: # NOTE: only supports bboxes as prompts for now assert bboxes is not None, f"Expected bboxes, but got {bboxes}!" - # NOTE: needs List[np.ndarray] + # NOTE: needs list[np.ndarray] assert isinstance(bboxes, list) and all(isinstance(b, np.ndarray) for b in bboxes), ( - f"Expected List[np.ndarray], but got {bboxes}!" + f"Expected list[np.ndarray], but got {bboxes}!" ) assert isinstance(category, list) and all(isinstance(b, np.ndarray) for b in category), ( - f"Expected List[np.ndarray], but got {category}!" + f"Expected list[np.ndarray], but got {category}!" ) assert len(im) == len(category) == len(bboxes), ( f"Expected same length for all inputs, but got {len(im)}vs{len(category)}vs{len(bboxes)}!" @@ -149,7 +149,7 @@ class YOLOEVPDetectPredictor(DetectionPredictor): Process the source to get the visual prompt embeddings (VPE). Args: - source (str | Path | int | PIL.Image | np.ndarray | torch.Tensor | List | Tuple): The source + source (str | Path | int | PIL.Image | np.ndarray | torch.Tensor | list | tuple): The source of the image to make predictions on. Accepts various types including file paths, URLs, PIL images, numpy arrays, and torch tensors. diff --git a/ultralytics/models/yolo/yoloe/train.py b/ultralytics/models/yolo/yoloe/train.py index 63960f33..01425733 100644 --- a/ultralytics/models/yolo/yoloe/train.py +++ b/ultralytics/models/yolo/yoloe/train.py @@ -183,7 +183,7 @@ class YOLOETrainerFromScratch(YOLOETrainer, WorldTrainerFromScratch): standard YOLO datasets and grounding datasets with different formats. Args: - img_path (List[str] | str): Path to the folder containing images or list of paths. + img_path (list[str] | str): Path to the folder containing images or list of paths. mode (str): 'train' mode or 'val' mode, allowing customized augmentations for each mode. batch (int, optional): Size of batches, used for rectangular training/validation. @@ -207,7 +207,7 @@ class YOLOETrainerFromScratch(YOLOETrainer, WorldTrainerFromScratch): Generate text embeddings for a list of text samples. Args: - texts (List[str]): List of text samples to encode. + texts (list[str]): List of text samples to encode. batch (int): Batch size for processing. cache_dir (Path): Directory to save/load cached embeddings. @@ -262,7 +262,7 @@ class YOLOEPEFreeTrainer(YOLOEPETrainer, YOLOETrainerFromScratch): in the parent directory of the first dataset's image path. Args: - datasets (List[Dataset]): List of datasets containing category names to process. + datasets (list[Dataset]): List of datasets containing category names to process. batch (int): Batch size for processing text embeddings. Notes: @@ -290,7 +290,7 @@ class YOLOEVPTrainer(YOLOETrainerFromScratch): Build YOLO Dataset for training or validation with visual prompts. Args: - img_path (List[str] | str): Path to the folder containing images or list of paths. + img_path (list[str] | str): Path to the folder containing images or list of paths. mode (str): 'train' mode or 'val' mode, allowing customized augmentations for each mode. batch (int, optional): Size of batches, used for rectangular training/validation. diff --git a/ultralytics/nn/autobackend.py b/ultralytics/nn/autobackend.py index 24d6bde2..3016cb5b 100644 --- a/ultralytics/nn/autobackend.py +++ b/ultralytics/nn/autobackend.py @@ -624,7 +624,7 @@ class AutoBackend(nn.Module): **kwargs (Any): Additional keyword arguments for model configuration. Returns: - (torch.Tensor | List[torch.Tensor]): The raw output tensor(s) from the model. + (torch.Tensor | list[torch.Tensor]): The raw output tensor(s) from the model. """ b, ch, h, w = im.shape # batch, channel, height, width if self.fp16 and im.dtype != torch.float16: @@ -860,7 +860,7 @@ class AutoBackend(nn.Module): p (str): Path to the model file. Returns: - (List[bool]): List of booleans indicating the model type. + (list[bool]): List of booleans indicating the model type. Examples: >>> model = AutoBackend(model="path/to/model.onnx") diff --git a/ultralytics/nn/modules/block.py b/ultralytics/nn/modules/block.py index 13bad3c8..f5a1c60a 100644 --- a/ultralytics/nn/modules/block.py +++ b/ultralytics/nn/modules/block.py @@ -745,7 +745,7 @@ class ImagePoolingAttn(nn.Module): Forward pass of ImagePoolingAttn. Args: - x (List[torch.Tensor]): List of input feature maps. + x (list[torch.Tensor]): List of input feature maps. text (torch.Tensor): Text embeddings. Returns: @@ -1032,7 +1032,7 @@ class CBLinear(nn.Module): Args: c1 (int): Input channels. - c2s (List[int]): List of output channel sizes. + c2s (list[int]): List of output channel sizes. k (int): Kernel size. s (int): Stride. p (int | None): Padding. @@ -1055,7 +1055,7 @@ class CBFuse(nn.Module): Initialize CBFuse module. Args: - idx (List[int]): Indices for feature selection. + idx (list[int]): Indices for feature selection. """ super().__init__() self.idx = idx @@ -1065,7 +1065,7 @@ class CBFuse(nn.Module): Forward pass through CBFuse layer. Args: - xs (List[torch.Tensor]): List of input tensors. + xs (list[torch.Tensor]): List of input tensors. Returns: (torch.Tensor): Fused output tensor. @@ -1676,7 +1676,7 @@ class TorchVision(nn.Module): x (torch.Tensor): Input tensor. Returns: - (torch.Tensor | List[torch.Tensor]): Output tensor or list of tensors. + (torch.Tensor | list[torch.Tensor]): Output tensor or list of tensors. """ if self.split: y = [x] @@ -1979,7 +1979,7 @@ class SAVPE(nn.Module): Initialize SAVPE module with channels, intermediate channels, and embedding dimension. Args: - ch (List[int]): List of input channel dimensions. + ch (list[int]): List of input channel dimensions. c3 (int): Intermediate channels. embed (int): Embedding dimension. """ diff --git a/ultralytics/nn/modules/conv.py b/ultralytics/nn/modules/conv.py index cf14eddc..5c6ea003 100644 --- a/ultralytics/nn/modules/conv.py +++ b/ultralytics/nn/modules/conv.py @@ -675,7 +675,7 @@ class Concat(nn.Module): Concatenate input tensors along specified dimension. Args: - x (List[torch.Tensor]): List of input tensors. + x (list[torch.Tensor]): List of input tensors. Returns: (torch.Tensor): Concatenated tensor. @@ -706,7 +706,7 @@ class Index(nn.Module): Select and return a particular index from input. Args: - x (List[torch.Tensor]): List of input tensors. + x (list[torch.Tensor]): List of input tensors. Returns: (torch.Tensor): Selected tensor. diff --git a/ultralytics/nn/modules/head.py b/ultralytics/nn/modules/head.py index 483e44c0..a277aa66 100644 --- a/ultralytics/nn/modules/head.py +++ b/ultralytics/nn/modules/head.py @@ -130,7 +130,7 @@ class Detect(nn.Module): Perform forward pass of the v10Detect module. Args: - x (List[torch.Tensor]): Input feature maps from different levels. + x (list[torch.Tensor]): Input feature maps from different levels. Returns: outputs (dict | tuple): Training mode returns dict with one2many and one2one outputs. @@ -154,7 +154,7 @@ class Detect(nn.Module): Decode predicted bounding boxes and class probabilities based on multiple-level feature maps. Args: - x (List[torch.Tensor]): List of feature maps from different detection layers. + x (list[torch.Tensor]): List of feature maps from different detection layers. Returns: (torch.Tensor): Concatenated tensor of decoded bounding boxes and class probabilities. @@ -985,7 +985,7 @@ class RTDETRDecoder(nn.Module): Run the forward pass of the module, returning bounding box and classification scores for the input. Args: - x (List[torch.Tensor]): List of feature maps from the backbone. + x (list[torch.Tensor]): List of feature maps from the backbone. batch (dict, optional): Batch information for training. Returns: @@ -1075,7 +1075,7 @@ class RTDETRDecoder(nn.Module): Process and return encoder inputs by getting projection features from input and concatenating them. Args: - x (List[torch.Tensor]): List of feature maps from the backbone. + x (list[torch.Tensor]): List of feature maps from the backbone. Returns: feats (torch.Tensor): Processed features. diff --git a/ultralytics/nn/tasks.py b/ultralytics/nn/tasks.py index b4b07bf6..8c2cf7ee 100644 --- a/ultralytics/nn/tasks.py +++ b/ultralytics/nn/tasks.py @@ -329,7 +329,7 @@ class BaseModel(torch.nn.Module): Args: batch (dict): Batch to compute loss on. - preds (torch.Tensor | List[torch.Tensor], optional): Predictions. + preds (torch.Tensor | list[torch.Tensor], optional): Predictions. """ if getattr(self, "criterion", None) is None: self.criterion = self.init_criterion() @@ -480,10 +480,10 @@ class DetectionModel(BaseModel): Clip YOLO augmented inference tails. Args: - y (List[torch.Tensor]): List of detection tensors. + y (list[torch.Tensor]): List of detection tensors. Returns: - (List[torch.Tensor]): Clipped detection tensors. + (list[torch.Tensor]): Clipped detection tensors. """ nl = self.model[-1].nl # number of detection layers (P3-P5) g = sum(4**x for x in range(nl)) # grid points @@ -874,7 +874,7 @@ class WorldModel(DetectionModel): Set classes in advance so that model could do offline-inference without clip model. Args: - text (List[str]): List of class names. + text (list[str]): List of class names. batch (int): Batch size for processing text tokens. cache_clip_model (bool): Whether to cache the CLIP model. """ @@ -886,7 +886,7 @@ class WorldModel(DetectionModel): Set classes in advance so that model could do offline-inference without clip model. Args: - text (List[str]): List of class names. + text (list[str]): List of class names. batch (int): Batch size for processing text tokens. cache_clip_model (bool): Whether to cache the CLIP model. @@ -956,7 +956,7 @@ class WorldModel(DetectionModel): Args: batch (dict): Batch to compute loss on. - preds (torch.Tensor | List[torch.Tensor], optional): Predictions. + preds (torch.Tensor | list[torch.Tensor], optional): Predictions. """ if not hasattr(self, "criterion"): self.criterion = self.init_criterion() @@ -1012,7 +1012,7 @@ class YOLOEModel(DetectionModel): Set classes in advance so that model could do offline-inference without clip model. Args: - text (List[str]): List of class names. + text (list[str]): List of class names. batch (int): Batch size for processing text tokens. cache_clip_model (bool): Whether to cache the CLIP model. without_reprta (bool): Whether to return text embeddings cooperated with reprta module. @@ -1060,7 +1060,7 @@ class YOLOEModel(DetectionModel): Args: vocab (nn.ModuleList): List of vocabulary items. - names (List[str]): List of class names. + names (list[str]): List of class names. """ assert not self.training head = self.model[-1] @@ -1114,7 +1114,7 @@ class YOLOEModel(DetectionModel): Set classes in advance so that model could do offline-inference without clip model. Args: - names (List[str]): List of class names. + names (list[str]): List of class names. embeddings (torch.Tensor): Embeddings tensor. """ assert not hasattr(self.model[-1], "lrpc"), ( @@ -1203,7 +1203,7 @@ class YOLOEModel(DetectionModel): Args: batch (dict): Batch to compute loss on. - preds (torch.Tensor | List[torch.Tensor], optional): Predictions. + preds (torch.Tensor | list[torch.Tensor], optional): Predictions. """ if not hasattr(self, "criterion"): from ultralytics.utils.loss import TVPDetectLoss @@ -1251,7 +1251,7 @@ class YOLOESegModel(YOLOEModel, SegmentationModel): Args: batch (dict): Batch to compute loss on. - preds (torch.Tensor | List[torch.Tensor], optional): Predictions. + preds (torch.Tensor | list[torch.Tensor], optional): Predictions. """ if not hasattr(self, "criterion"): from ultralytics.utils.loss import TVPSegmentLoss diff --git a/ultralytics/nn/text_model.py b/ultralytics/nn/text_model.py index f142d9fa..234bb176 100644 --- a/ultralytics/nn/text_model.py +++ b/ultralytics/nn/text_model.py @@ -97,7 +97,7 @@ class CLIP(TextModel): Convert input texts to CLIP tokens. Args: - texts (str | List[str]): Input text or list of texts to tokenize. + texts (str | list[str]): Input text or list of texts to tokenize. Returns: (torch.Tensor): Tokenized text tensor with shape (batch_size, context_length) ready for model processing. @@ -240,7 +240,7 @@ class MobileCLIP(TextModel): Convert input texts to MobileCLIP tokens. Args: - texts (List[str]): List of text strings to tokenize. + texts (list[str]): List of text strings to tokenize. Returns: (torch.Tensor): Tokenized text inputs with shape (batch_size, sequence_length). @@ -325,7 +325,7 @@ class MobileCLIPTS(TextModel): Convert input texts to MobileCLIP tokens. Args: - texts (List[str]): List of text strings to tokenize. + texts (list[str]): List of text strings to tokenize. Returns: (torch.Tensor): Tokenized text inputs with shape (batch_size, sequence_length). diff --git a/ultralytics/solutions/ai_gym.py b/ultralytics/solutions/ai_gym.py index 17e0eb09..9a5c9146 100644 --- a/ultralytics/solutions/ai_gym.py +++ b/ultralytics/solutions/ai_gym.py @@ -14,10 +14,10 @@ class AIGym(BaseSolution): repetitions of exercises based on predefined angle thresholds for up and down positions. Attributes: - states (Dict[float, int, str]): Stores per-track angle, count, and stage for workout monitoring. + states (dict[float, int, str]): Stores per-track angle, count, and stage for workout monitoring. up_angle (float): Angle threshold for considering the 'up' position of an exercise. down_angle (float): Angle threshold for considering the 'down' position of an exercise. - kpts (List[int]): Indices of keypoints used for angle calculation. + kpts (list[int]): Indices of keypoints used for angle calculation. Methods: process: Process a frame to detect poses, calculate angles, and count repetitions. diff --git a/ultralytics/solutions/analytics.py b/ultralytics/solutions/analytics.py index d0e61f4a..e62eaf03 100644 --- a/ultralytics/solutions/analytics.py +++ b/ultralytics/solutions/analytics.py @@ -29,12 +29,12 @@ class Analytics(BaseSolution): fontsize (int): Font size for text display. color_cycle (cycle): Cyclic iterator for chart colors. total_counts (int): Total count of detected objects (used for line charts). - clswise_count (Dict[str, int]): Dictionary for class-wise object counts. + clswise_count (dict[str, int]): Dictionary for class-wise object counts. fig (Figure): Matplotlib figure object for the chart. ax (Axes): Matplotlib axes object for the chart. canvas (FigureCanvasAgg): Canvas for rendering the chart. lines (dict): Dictionary to store line objects for area charts. - color_mapping (Dict[str, str]): Dictionary mapping class labels to colors for consistent visualization. + color_mapping (dict[str, str]): Dictionary mapping class labels to colors for consistent visualization. Methods: process: Process image data and update the chart. @@ -144,7 +144,7 @@ class Analytics(BaseSolution): Args: frame_number (int): The current frame number. - count_dict (Dict[str, int], optional): Dictionary with class names as keys and counts as values for + count_dict (dict[str, int], optional): Dictionary with class names as keys and counts as values for multiple classes. If None, updates a single line graph. plot (str): Type of the plot. Options are 'line', 'bar', 'pie', or 'area'. diff --git a/ultralytics/solutions/config.py b/ultralytics/solutions/config.py index cc27fe6b..d63d4fdf 100644 --- a/ultralytics/solutions/config.py +++ b/ultralytics/solutions/config.py @@ -20,20 +20,20 @@ class SolutionConfig: Attributes: source (str, optional): Path to the input source (video, RTSP, etc.). Only usable with Solutions CLI. model (str, optional): Path to the Ultralytics YOLO model to be used for inference. - classes (List[int], optional): List of class indices to filter detections. + classes (list[int], optional): List of class indices to filter detections. show_conf (bool): Whether to show confidence scores on the visual output. show_labels (bool): Whether to display class labels on visual output. - region (List[Tuple[int, int]], optional): Polygonal region or line for object counting. + region (list[tuple[int, int]], optional): Polygonal region or line for object counting. colormap (int, optional): OpenCV colormap constant for visual overlays (e.g., cv2.COLORMAP_JET). show_in (bool): Whether to display count number for objects entering the region. show_out (bool): Whether to display count number for objects leaving the region. up_angle (float): Upper angle threshold used in pose-based workouts monitoring. down_angle (int): Lower angle threshold used in pose-based workouts monitoring. - kpts (List[int]): Keypoint indices to monitor, e.g., for pose analytics. + kpts (list[int]): Keypoint indices to monitor, e.g., for pose analytics. analytics_type (str): Type of analytics to perform ("line", "area", "bar", "pie", etc.). - figsize (Tuple[int, int], optional): Size of the matplotlib figure used for analytical plots (width, height). + figsize (tuple[int, int], optional): Size of the matplotlib figure used for analytical plots (width, height). blur_ratio (float): Ratio used to blur objects in the video frames (0.0 to 1.0). - vision_point (Tuple[int, int]): Reference point for directional tracking or perspective drawing. + vision_point (tuple[int, int]): Reference point for directional tracking or perspective drawing. crop_dir (str): Directory path to save cropped detection images. json_file (str): Path to a JSON file containing data for parking areas. line_width (int): Width for visual display i.e. bounding boxes, keypoints, counts. diff --git a/ultralytics/solutions/distance_calculation.py b/ultralytics/solutions/distance_calculation.py index 4523a415..03173f46 100644 --- a/ultralytics/solutions/distance_calculation.py +++ b/ultralytics/solutions/distance_calculation.py @@ -18,8 +18,8 @@ class DistanceCalculation(BaseSolution): Attributes: left_mouse_count (int): Counter for left mouse button clicks. - selected_boxes (Dict[int, List[float]]): Dictionary to store selected bounding boxes and their track IDs. - centroids (List[List[int]]): List to store centroids of selected bounding boxes. + selected_boxes (dict[int, list[float]]): Dictionary to store selected bounding boxes and their track IDs. + centroids (list[list[int]]): List to store centroids of selected bounding boxes. Methods: mouse_event_for_distance: Handle mouse events for selecting objects in the video stream. diff --git a/ultralytics/solutions/heatmap.py b/ultralytics/solutions/heatmap.py index 69942808..5fad50bd 100644 --- a/ultralytics/solutions/heatmap.py +++ b/ultralytics/solutions/heatmap.py @@ -57,7 +57,7 @@ class Heatmap(ObjectCounter): Efficiently calculate heatmap area and effect location for applying colormap. Args: - box (List[float]): Bounding box coordinates [x0, y0, x1, y1]. + box (list[float]): Bounding box coordinates [x0, y0, x1, y1]. """ x0, y0, x1, y1 = map(int, box) radius_squared = (min(x1 - x0, y1 - y0) // 2) ** 2 diff --git a/ultralytics/solutions/instance_segmentation.py b/ultralytics/solutions/instance_segmentation.py index ffdcead4..20847837 100644 --- a/ultralytics/solutions/instance_segmentation.py +++ b/ultralytics/solutions/instance_segmentation.py @@ -16,10 +16,10 @@ class InstanceSegmentation(BaseSolution): Attributes: model (str): The segmentation model to use for inference. line_width (int): Width of the bounding box and text lines. - names (Dict[int, str]): Dictionary mapping class indices to class names. - clss (List[int]): List of detected class indices. - track_ids (List[int]): List of track IDs for detected instances. - masks (List[np.ndarray]): List of segmentation masks for detected instances. + names (dict[int, str]): Dictionary mapping class indices to class names. + clss (list[int]): List of detected class indices. + track_ids (list[int]): List of track IDs for detected instances. + masks (list[np.ndarray]): List of segmentation masks for detected instances. show_conf (bool): Whether to display confidence scores. show_labels (bool): Whether to display class labels. show_boxes (bool): Whether to display bounding boxes. diff --git a/ultralytics/solutions/object_counter.py b/ultralytics/solutions/object_counter.py index 5c7d728b..95059511 100644 --- a/ultralytics/solutions/object_counter.py +++ b/ultralytics/solutions/object_counter.py @@ -19,8 +19,8 @@ class ObjectCounter(BaseSolution): Attributes: in_count (int): Counter for objects moving inward. out_count (int): Counter for objects moving outward. - counted_ids (List[int]): List of IDs of objects that have been counted. - classwise_counts (Dict[str, Dict[str, int]]): Dictionary for counts, categorized by object class. + counted_ids (list[int]): List of IDs of objects that have been counted. + classwise_counts (dict[str, dict[str, int]]): Dictionary for counts, categorized by object class. region_initialized (bool): Flag indicating whether the counting region has been initialized. show_in (bool): Flag to control display of inward count. show_out (bool): Flag to control display of outward count. @@ -63,9 +63,9 @@ class ObjectCounter(BaseSolution): Count objects within a polygonal or linear region based on their tracks. Args: - current_centroid (Tuple[float, float]): Current centroid coordinates (x, y) in the current frame. + current_centroid (tuple[float, float]): Current centroid coordinates (x, y) in the current frame. track_id (int): Unique identifier for the tracked object. - prev_position (Tuple[float, float], optional): Last frame position coordinates (x, y) of the track. + prev_position (tuple[float, float], optional): Last frame position coordinates (x, y) of the track. cls (int): Class index for classwise count updates. Examples: diff --git a/ultralytics/solutions/parking_management.py b/ultralytics/solutions/parking_management.py index f5a30d9a..ba3acf27 100644 --- a/ultralytics/solutions/parking_management.py +++ b/ultralytics/solutions/parking_management.py @@ -28,8 +28,8 @@ class ParkingPtsSelection: canvas (tk.Canvas): The canvas widget for displaying the image and drawing bounding boxes. image (PIL.Image.Image): The uploaded image. canvas_image (ImageTk.PhotoImage): The image displayed on the canvas. - rg_data (List[List[Tuple[int, int]]]): List of bounding boxes, each defined by 4 points. - current_box (List[Tuple[int, int]]): Temporary storage for the points of the current bounding box. + rg_data (list[list[tuple[int, int]]]): List of bounding boxes, each defined by 4 points. + current_box (list[tuple[int, int]]): Temporary storage for the points of the current bounding box. imgw (int): Original width of the uploaded image. imgh (int): Original height of the uploaded image. canvas_max_width (int): Maximum width of the canvas. @@ -186,11 +186,11 @@ class ParkingManagement(BaseSolution): Attributes: json_file (str): Path to the JSON file containing parking region details. - json (List[Dict]): Loaded JSON data containing parking region information. - pr_info (Dict[str, int]): Dictionary storing parking information (Occupancy and Available spaces). - arc (Tuple[int, int, int]): RGB color tuple for available region visualization. - occ (Tuple[int, int, int]): RGB color tuple for occupied region visualization. - dc (Tuple[int, int, int]): RGB color tuple for centroid visualization of detected objects. + json (list[dict]): Loaded JSON data containing parking region information. + pr_info (dict[str, int]): Dictionary storing parking information (Occupancy and Available spaces). + arc (tuple[int, int, int]): RGB color tuple for available region visualization. + occ (tuple[int, int, int]): RGB color tuple for occupied region visualization. + dc (tuple[int, int, int]): RGB color tuple for centroid visualization of detected objects. Methods: process: Process the input image for parking lot management and visualization. diff --git a/ultralytics/solutions/queue_management.py b/ultralytics/solutions/queue_management.py index adb3dccf..4cdbfa8a 100644 --- a/ultralytics/solutions/queue_management.py +++ b/ultralytics/solutions/queue_management.py @@ -15,10 +15,10 @@ class QueueManager(BaseSolution): Attributes: counts (int): The current count of objects in the queue. - rect_color (Tuple[int, int, int]): RGB color tuple for drawing the queue region rectangle. + rect_color (tuple[int, int, int]): RGB color tuple for drawing the queue region rectangle. region_length (int): The number of points defining the queue region. - track_line (List[Tuple[int, int]]): List of track line coordinates. - track_history (Dict[int, List[Tuple[int, int]]]): Dictionary storing tracking history for each object. + track_line (list[tuple[int, int]]): List of track line coordinates. + track_history (dict[int, list[tuple[int, int]]]): Dictionary storing tracking history for each object. Methods: initialize_region: Initialize the queue region. diff --git a/ultralytics/solutions/region_counter.py b/ultralytics/solutions/region_counter.py index f5309026..2f4d6fa8 100644 --- a/ultralytics/solutions/region_counter.py +++ b/ultralytics/solutions/region_counter.py @@ -64,12 +64,12 @@ class RegionCounter(BaseSolution): Args: name (str): Name assigned to the new region. - polygon_points (List[Tuple]): List of (x, y) coordinates defining the region's polygon. - region_color (Tuple[int, int, int]): BGR color for region visualization. - text_color (Tuple[int, int, int]): BGR color for the text within the region. + polygon_points (list[tuple]): List of (x, y) coordinates defining the region's polygon. + region_color (tuple[int, int, int]): BGR color for region visualization. + text_color (tuple[int, int, int]): BGR color for the text within the region. Returns: - (Dict[str, any]): Returns a dictionary including the region information i.e. name, region_color etc. + (dict[str, any]): Returns a dictionary including the region information i.e. name, region_color etc. """ region = self.region_template.copy() region.update( diff --git a/ultralytics/solutions/similarity_search.py b/ultralytics/solutions/similarity_search.py index a8ee192c..3f4f6b90 100644 --- a/ultralytics/solutions/similarity_search.py +++ b/ultralytics/solutions/similarity_search.py @@ -33,7 +33,7 @@ class VisualAISearch: data_dir (Path): Path object for the data directory. model: Loaded CLIP model. index: FAISS index for similarity search. - image_paths (List[str]): List of image file paths. + image_paths (list[str]): List of image file paths. Methods: extract_image_feature: Extract CLIP embedding from an image. @@ -138,7 +138,7 @@ class VisualAISearch: similarity_thresh (float, optional): Minimum similarity threshold for filtering results. Returns: - (List[str]): List of image filenames ranked by similarity score. + (list[str]): List of image filenames ranked by similarity score. Examples: Search for images matching a query diff --git a/ultralytics/solutions/solutions.py b/ultralytics/solutions/solutions.py index 1b288a54..b7da9e6e 100644 --- a/ultralytics/solutions/solutions.py +++ b/ultralytics/solutions/solutions.py @@ -30,31 +30,31 @@ class BaseSolution: Polygon: Class for creating polygon geometries from shapely. Point: Class for creating point geometries from shapely. prep: Prepared geometry function from shapely for optimized spatial operations. - CFG (Dict[str, Any]): Configuration dictionary loaded from YAML file and updated with kwargs. + CFG (dict[str, Any]): Configuration dictionary loaded from YAML file and updated with kwargs. LOGGER: Logger instance for solution-specific logging. annotator: Annotator instance for drawing on images. tracks: YOLO tracking results from the latest inference. track_data: Extracted tracking data (boxes or OBB) from tracks. - boxes (List): Bounding box coordinates from tracking results. - clss (List[int]): Class indices from tracking results. - track_ids (List[int]): Track IDs from tracking results. - confs (List[float]): Confidence scores from tracking results. + boxes (list): Bounding box coordinates from tracking results. + clss (list[int]): Class indices from tracking results. + track_ids (list[int]): Track IDs from tracking results. + confs (list[float]): Confidence scores from tracking results. track_line: Current track line for storing tracking history. masks: Segmentation masks from tracking results. r_s: Region or line geometry object for spatial operations. frame_no (int): Current frame number for logging purposes. - region (List[Tuple[int, int]]): List of coordinate tuples defining region of interest. + region (list[tuple[int, int]]): List of coordinate tuples defining region of interest. line_width (int): Width of lines used in visualizations. model (YOLO): Loaded YOLO model instance. - names (Dict[int, str]): Dictionary mapping class indices to class names. - classes (List[int]): List of class indices to track. + names (dict[int, str]): Dictionary mapping class indices to class names. + classes (list[int]): List of class indices to track. show_conf (bool): Flag to show confidence scores in annotations. show_labels (bool): Flag to show class labels in annotations. device (str): Device for model inference. - track_add_args (Dict[str, Any]): Additional arguments for tracking configuration. + track_add_args (dict[str, Any]): Additional arguments for tracking configuration. env_check (bool): Flag indicating whether environment supports image display. track_history (defaultdict): Dictionary storing tracking history for each object. - profilers (Tuple): Profiler instances for performance monitoring. + profilers (tuple): Profiler instances for performance monitoring. Methods: adjust_box_label: Generate formatted label for bounding box. @@ -193,7 +193,7 @@ class BaseSolution: Args: track_id (int): The unique identifier for the tracked object. - box (List[float]): The bounding box coordinates of the object in the format [x1, y1, x2, y2]. + box (list[float]): The bounding box coordinates of the object in the format [x1, y1, x2, y2]. Examples: >>> solution = BaseSolution() @@ -333,8 +333,8 @@ class SolutionAnnotator(Annotator): Draw a region or line on the image. Args: - reg_pts (List[Tuple[int, int]], optional): Region points (for line 2 points, for region 4+ points). - color (Tuple[int, int, int]): RGB color value for the region. + reg_pts (list[tuple[int, int]], optional): Region points (for line 2 points, for region 4+ points). + color (tuple[int, int, int]): RGB color value for the region. thickness (int): Line thickness for drawing the region. """ cv2.polylines(self.im, [np.array(reg_pts, dtype=np.int32)], isClosed=True, color=color, thickness=thickness) @@ -355,9 +355,9 @@ class SolutionAnnotator(Annotator): Args: label (str): Queue counts label. - points (List[Tuple[int, int]], optional): Region points for center point calculation to display text. - region_color (Tuple[int, int, int]): RGB queue region color. - txt_color (Tuple[int, int, int]): RGB text display color. + points (list[tuple[int, int]], optional): Region points for center point calculation to display text. + region_color (tuple[int, int, int]): RGB queue region color. + txt_color (tuple[int, int, int]): RGB text display color. """ x_values = [point[0] for point in points] y_values = [point[1] for point in points] @@ -402,9 +402,9 @@ class SolutionAnnotator(Annotator): Args: im0 (np.ndarray): Inference image. - text (Dict[str, Any]): Labels dictionary. - txt_color (Tuple[int, int, int]): Display color for text foreground. - bg_color (Tuple[int, int, int]): Display color for text background. + text (dict[str, Any]): Labels dictionary. + txt_color (tuple[int, int, int]): Display color for text foreground. + bg_color (tuple[int, int, int]): Display color for text background. margin (int): Gap between text and rectangle for better display. """ horizontal_gap = int(im0.shape[1] * 0.02) @@ -432,9 +432,9 @@ class SolutionAnnotator(Annotator): Calculate the angle between three points for workout monitoring. Args: - a (List[float]): The coordinates of the first point. - b (List[float]): The coordinates of the second point (vertex). - c (List[float]): The coordinates of the third point. + a (list[float]): The coordinates of the first point. + b (list[float]): The coordinates of the second point (vertex). + c (list[float]): The coordinates of the third point. Returns: (float): The angle in degrees between the three points. @@ -454,8 +454,8 @@ class SolutionAnnotator(Annotator): Draw specific keypoints for gym steps counting. Args: - keypoints (List[List[float]]): Keypoints data to be plotted, each in format [x, y, confidence]. - indices (List[int], optional): Keypoint indices to be plotted. + keypoints (list[list[float]]): Keypoints data to be plotted, each in format [x, y, confidence]. + indices (list[int], optional): Keypoint indices to be plotted. radius (int): Keypoint radius. conf_thresh (float): Confidence threshold for keypoints. @@ -491,9 +491,9 @@ class SolutionAnnotator(Annotator): Args: display_text (str): The text to be displayed. - position (Tuple[int, int]): Coordinates (x, y) on the image where the text will be placed. - color (Tuple[int, int, int]): Text background color. - txt_color (Tuple[int, int, int]): Text foreground color. + position (tuple[int, int]): Coordinates (x, y) on the image where the text will be placed. + color (tuple[int, int, int]): Text background color. + txt_color (tuple[int, int, int]): Text foreground color. Returns: (int): The height of the text. @@ -529,9 +529,9 @@ class SolutionAnnotator(Annotator): angle_text (str): Angle value for workout monitoring. count_text (str): Counts value for workout monitoring. stage_text (str): Stage decision for workout monitoring. - center_kpt (List[int]): Centroid pose index for workout monitoring. - color (Tuple[int, int, int]): Text background color. - txt_color (Tuple[int, int, int]): Text foreground color. + center_kpt (list[int]): Centroid pose index for workout monitoring. + color (tuple[int, int, int]): Text background color. + txt_color (tuple[int, int, int]): Text foreground color. """ # Format text angle_text, count_text, stage_text = f" {angle_text:.2f}", f"Steps : {count_text}", f" {stage_text}" @@ -559,9 +559,9 @@ class SolutionAnnotator(Annotator): Args: pixels_distance (float): Pixels distance between two bbox centroids. - centroids (List[Tuple[int, int]]): Bounding box centroids data. - line_color (Tuple[int, int, int]): Distance line color. - centroid_color (Tuple[int, int, int]): Bounding box centroid color. + centroids (list[tuple[int, int]]): Bounding box centroids data. + line_color (tuple[int, int, int]): Distance line color. + centroid_color (tuple[int, int, int]): Bounding box centroid color. """ # Get the text size text = f"Pixels Distance: {pixels_distance:.2f}" @@ -603,8 +603,8 @@ class SolutionAnnotator(Annotator): Args: im0 (np.ndarray): Inference image. text (str): Object/class name. - txt_color (Tuple[int, int, int]): Display color for text foreground. - bg_color (Tuple[int, int, int]): Display color for text background. + txt_color (tuple[int, int, int]): Display color for text foreground. + bg_color (tuple[int, int, int]): Display color for text background. x_center (float): The x position center point for bounding box. y_center (float): The y position center point for bounding box. margin (int): The gap between text and rectangle for better display. @@ -651,8 +651,8 @@ class SolutionAnnotator(Annotator): line_x (int): The x-coordinate of the sweep line. line_y (int): The y-coordinate limit of the sweep line. label (str, optional): Text label to be drawn in center of sweep line. If None, no label is drawn. - color (Tuple[int, int, int]): RGB color for the line and label background. - txt_color (Tuple[int, int, int]): RGB color for the label text. + color (tuple[int, int, int]): RGB color for the line and label background. + txt_color (tuple[int, int, int]): RGB color for the label text. """ # Draw the sweep line cv2.line(self.im, (line_x, 0), (line_x, line_y), color, self.tf * 2) @@ -688,10 +688,10 @@ class SolutionAnnotator(Annotator): Perform pinpoint human-vision eye mapping and plotting. Args: - box (List[float]): Bounding box coordinates in format [x1, y1, x2, y2]. - center_point (Tuple[int, int]): Center point for vision eye view. - color (Tuple[int, int, int]): Object centroid and line color. - pin_color (Tuple[int, int, int]): Visioneye point color. + box (list[float]): Bounding box coordinates in format [x1, y1, x2, y2]. + center_point (tuple[int, int]): Center point for vision eye view. + color (tuple[int, int, int]): Object centroid and line color. + pin_color (tuple[int, int, int]): Visioneye point color. """ center_bbox = int((box[0] + box[2]) / 2), int((box[1] + box[3]) / 2) cv2.circle(self.im, center_point, self.tf * 2, pin_color, -1) @@ -711,10 +711,10 @@ class SolutionAnnotator(Annotator): Draw a label with a background rectangle or circle centered within a given bounding box. Args: - box (Tuple[float, float, float, float]): The bounding box coordinates (x1, y1, x2, y2). + box (tuple[float, float, float, float]): The bounding box coordinates (x1, y1, x2, y2). label (str): The text label to be displayed. - color (Tuple[int, int, int]): The background color of the rectangle (B, G, R). - txt_color (Tuple[int, int, int]): The color of the text (R, G, B). + color (tuple[int, int, int]): The background color of the rectangle (B, G, R). + txt_color (tuple[int, int, int]): The color of the text (R, G, B). shape (str): The shape of the label i.e "circle" or "rect" margin (int): The margin between the text and the rectangle border. """ @@ -768,7 +768,7 @@ class SolutionResults: plot_im (np.ndarray): Processed image with counts, blurred, or other effects from solutions. in_count (int): The total number of "in" counts in a video stream. out_count (int): The total number of "out" counts in a video stream. - classwise_count (Dict[str, int]): A dictionary containing counts of objects categorized by class. + classwise_count (dict[str, int]): A dictionary containing counts of objects categorized by class. queue_count (int): The count of objects in a queue or waiting area. workout_count (int): The count of workout repetitions. workout_angle (float): The angle calculated during a workout exercise. @@ -778,10 +778,10 @@ class SolutionResults: filled_slots (int): The number of filled slots in a monitored area. email_sent (bool): A flag indicating whether an email notification was sent. total_tracks (int): The total number of tracked objects. - region_counts (Dict[str, int]): The count of objects within a specific region. - speed_dict (Dict[str, float]): A dictionary containing speed information for tracked objects. + region_counts (dict[str, int]): The count of objects within a specific region. + speed_dict (dict[str, float]): A dictionary containing speed information for tracked objects. total_crop_objects (int): Total number of cropped objects using ObjectCropper class. - speed (Dict[str, float]): Performance timing information for tracking and solution processing. + speed (dict[str, float]): Performance timing information for tracking and solution processing. """ def __init__(self, **kwargs): diff --git a/ultralytics/solutions/streamlit_inference.py b/ultralytics/solutions/streamlit_inference.py index 58b0493f..2429b474 100644 --- a/ultralytics/solutions/streamlit_inference.py +++ b/ultralytics/solutions/streamlit_inference.py @@ -34,7 +34,7 @@ class Inference: org_frame (Any): Container for the original frame to be displayed. ann_frame (Any): Container for the annotated frame to be displayed. vid_file_name (str | int): Name of the uploaded video file or webcam index. - selected_ind (List[int]): List of selected class indices for detection. + selected_ind (list[int]): List of selected class indices for detection. Methods: web_ui: Set up the Streamlit web interface with custom HTML elements. diff --git a/ultralytics/solutions/trackzone.py b/ultralytics/solutions/trackzone.py index b4377691..5505317b 100644 --- a/ultralytics/solutions/trackzone.py +++ b/ultralytics/solutions/trackzone.py @@ -19,10 +19,10 @@ class TrackZone(BaseSolution): Attributes: region (np.ndarray): The polygonal region for tracking, represented as a convex hull of points. line_width (int): Width of the lines used for drawing bounding boxes and region boundaries. - names (List[str]): List of class names that the model can detect. - boxes (List[np.ndarray]): Bounding boxes of tracked objects. - track_ids (List[int]): Unique identifiers for each tracked object. - clss (List[int]): Class indices of tracked objects. + names (list[str]): List of class names that the model can detect. + boxes (list[np.ndarray]): Bounding boxes of tracked objects. + track_ids (list[int]): Unique identifiers for each tracked object. + clss (list[int]): Class indices of tracked objects. Methods: process: Process each frame of the video, applying region-based tracking. diff --git a/ultralytics/solutions/vision_eye.py b/ultralytics/solutions/vision_eye.py index 27ecfc79..77323458 100644 --- a/ultralytics/solutions/vision_eye.py +++ b/ultralytics/solutions/vision_eye.py @@ -14,7 +14,7 @@ class VisionEye(BaseSolution): mapping vision points, and annotating results with bounding boxes and labels. Attributes: - vision_point (Tuple[int, int]): Coordinates (x, y) where vision will view objects and draw tracks. + vision_point (tuple[int, int]): Coordinates (x, y) where vision will view objects and draw tracks. Methods: process: Process the input image to detect objects, annotate them, and apply vision mapping. diff --git a/ultralytics/trackers/byte_tracker.py b/ultralytics/trackers/byte_tracker.py index 15138964..cdc7dcb7 100644 --- a/ultralytics/trackers/byte_tracker.py +++ b/ultralytics/trackers/byte_tracker.py @@ -58,7 +58,7 @@ class STrack(BaseTrack): Initialize a new STrack instance. Args: - xywh (List[float]): Bounding box coordinates and dimensions in the format (x, y, w, h, [a], idx), where + xywh (list[float]): Bounding box coordinates and dimensions in the format (x, y, w, h, [a], idx), where (x, y) is the center, (w, h) are width and height, [a] is optional aspect ratio, and idx is the id. score (float): Confidence score of the detection. cls (Any): Class label for the detected object. @@ -246,9 +246,9 @@ class BYTETracker: predicting the new object locations, and performs data association. Attributes: - tracked_stracks (List[STrack]): List of successfully activated tracks. - lost_stracks (List[STrack]): List of lost tracks. - removed_stracks (List[STrack]): List of removed tracks. + tracked_stracks (list[STrack]): List of successfully activated tracks. + lost_stracks (list[STrack]): List of lost tracks. + removed_stracks (list[STrack]): List of removed tracks. frame_id (int): The current frame ID. args (Namespace): Command-line arguments. max_time_lost (int): The maximum frames for a track to be considered as 'lost'. @@ -286,9 +286,9 @@ class BYTETracker: >>> args = Namespace(track_buffer=30) >>> tracker = BYTETracker(args, frame_rate=30) """ - self.tracked_stracks = [] # type: List[STrack] - self.lost_stracks = [] # type: List[STrack] - self.removed_stracks = [] # type: List[STrack] + self.tracked_stracks = [] # type: list[STrack] + self.lost_stracks = [] # type: list[STrack] + self.removed_stracks = [] # type: list[STrack] self.frame_id = 0 self.args = args @@ -320,7 +320,7 @@ class BYTETracker: detections = self.init_track(results, feats_keep) # Add newly detected tracklets to tracked_stracks unconfirmed = [] - tracked_stracks = [] # type: List[STrack] + tracked_stracks = [] # type: list[STrack] for track in self.tracked_stracks: if not track.is_activated: unconfirmed.append(track) @@ -439,9 +439,9 @@ class BYTETracker: def reset(self): """Reset the tracker by clearing all tracked, lost, and removed tracks and reinitializing the Kalman filter.""" - self.tracked_stracks = [] # type: List[STrack] - self.lost_stracks = [] # type: List[STrack] - self.removed_stracks = [] # type: List[STrack] + self.tracked_stracks = [] # type: list[STrack] + self.lost_stracks = [] # type: list[STrack] + self.removed_stracks = [] # type: list[STrack] self.frame_id = 0 self.kalman_filter = self.get_kalmanfilter() self.reset_id() diff --git a/ultralytics/trackers/utils/gmc.py b/ultralytics/trackers/utils/gmc.py index 7105e143..0eab5f2a 100644 --- a/ultralytics/trackers/utils/gmc.py +++ b/ultralytics/trackers/utils/gmc.py @@ -21,7 +21,7 @@ class GMC: method (str): The tracking method to use. Options include 'orb', 'sift', 'ecc', 'sparseOptFlow', 'none'. downscale (int): Factor by which to downscale the frames for processing. prevFrame (np.ndarray): Previous frame for tracking. - prevKeyPoints (List): Keypoints from the previous frame. + prevKeyPoints (list): Keypoints from the previous frame. prevDescriptors (np.ndarray): Descriptors from the previous frame. initializedFirstFrame (bool): Flag indicating if the first frame has been processed. @@ -96,7 +96,7 @@ class GMC: Args: raw_frame (np.ndarray): The raw frame to be processed, with shape (H, W, C). - detections (List, optional): List of detections to be used in the processing. + detections (list, optional): List of detections to be used in the processing. Returns: (np.ndarray): Transformation matrix with shape (2, 3). @@ -163,7 +163,7 @@ class GMC: Args: raw_frame (np.ndarray): The raw frame to be processed, with shape (H, W, C). - detections (List, optional): List of detections to be used in the processing. + detections (list, optional): List of detections to be used in the processing. Returns: (np.ndarray): Transformation matrix with shape (2, 3). diff --git a/ultralytics/trackers/utils/matching.py b/ultralytics/trackers/utils/matching.py index 4e741977..e85a78c3 100644 --- a/ultralytics/trackers/utils/matching.py +++ b/ultralytics/trackers/utils/matching.py @@ -66,8 +66,8 @@ def iou_distance(atracks: list, btracks: list) -> np.ndarray: Compute cost based on Intersection over Union (IoU) between tracks. Args: - atracks (List[STrack] | List[np.ndarray]): List of tracks 'a' or bounding boxes. - btracks (List[STrack] | List[np.ndarray]): List of tracks 'b' or bounding boxes. + atracks (list[STrack] | list[np.ndarray]): List of tracks 'a' or bounding boxes. + btracks (list[STrack] | list[np.ndarray]): List of tracks 'b' or bounding boxes. Returns: (np.ndarray): Cost matrix computed based on IoU with shape (len(atracks), len(btracks)). @@ -106,8 +106,8 @@ def embedding_distance(tracks: list, detections: list, metric: str = "cosine") - Compute distance between tracks and detections based on embeddings. Args: - tracks (List[STrack]): List of tracks, where each track contains embedding features. - detections (List[BaseTrack]): List of detections, where each detection contains embedding features. + tracks (list[STrack]): List of tracks, where each track contains embedding features. + detections (list[BaseTrack]): List of detections, where each detection contains embedding features. metric (str): Metric for distance computation. Supported metrics include 'cosine', 'euclidean', etc. Returns: @@ -137,7 +137,7 @@ def fuse_score(cost_matrix: np.ndarray, detections: list) -> np.ndarray: Args: cost_matrix (np.ndarray): The matrix containing cost values for assignments, with shape (N, M). - detections (List[BaseTrack]): List of detections, each containing a score attribute. + detections (list[BaseTrack]): List of detections, each containing a score attribute. Returns: (np.ndarray): Fused similarity matrix with shape (N, M). diff --git a/ultralytics/utils/autodevice.py b/ultralytics/utils/autodevice.py index 79e6668e..a0971bc8 100644 --- a/ultralytics/utils/autodevice.py +++ b/ultralytics/utils/autodevice.py @@ -23,7 +23,7 @@ class GPUInfo: pynvml (module | None): The `pynvml` module if successfully imported and initialized, otherwise `None`. nvml_available (bool): Indicates if `pynvml` is ready for use. True if import and `nvmlInit()` succeeded, False otherwise. - gpu_stats (List[Dict[str, Any]]): A list of dictionaries, each holding stats for one GPU. Populated on + gpu_stats (list[dict[str, Any]]): A list of dictionaries, each holding stats for one GPU. Populated on initialization and by `refresh_stats()`. Keys include: 'index', 'name', 'utilization' (%), 'memory_used' (MiB), 'memory_total' (MiB), 'memory_free' (MiB), 'temperature' (C), 'power_draw' (W), 'power_limit' (W or 'N/A'). Empty if NVML is unavailable or queries fail. @@ -146,7 +146,7 @@ class GPUInfo: min_util_fraction (float): Minimum free utilization rate required from 0.0 - 1.0. Returns: - (List[int]): Indices of the selected GPUs, sorted by idleness (lowest utilization first). + (list[int]): Indices of the selected GPUs, sorted by idleness (lowest utilization first). Notes: Returns fewer than 'count' if not enough qualify or exist. diff --git a/ultralytics/utils/benchmarks.py b/ultralytics/utils/benchmarks.py index 757a65b3..33ebb503 100644 --- a/ultralytics/utils/benchmarks.py +++ b/ultralytics/utils/benchmarks.py @@ -226,10 +226,10 @@ class RF100Benchmark: This class provides functionality to benchmark YOLO models on the RF100 dataset collection. Attributes: - ds_names (List[str]): Names of datasets used for benchmarking. - ds_cfg_list (List[Path]): List of paths to dataset configuration files. + ds_names (list[str]): Names of datasets used for benchmarking. + ds_cfg_list (list[Path]): List of paths to dataset configuration files. rf (Roboflow): Roboflow instance for accessing datasets. - val_metrics (List[str]): Metrics used for validation. + val_metrics (list[str]): Metrics used for validation. Methods: set_key: Set Roboflow API key for accessing datasets. @@ -270,8 +270,8 @@ class RF100Benchmark: ds_link_txt (str): Path to the file containing dataset links. Returns: - ds_names (List[str]): List of dataset names. - ds_cfg_list (List[Path]): List of paths to dataset configuration files. + ds_names (list[str]): List of dataset names. + ds_cfg_list (list[Path]): List of paths to dataset configuration files. Examples: >>> benchmark = RF100Benchmark() @@ -372,7 +372,7 @@ class ProfileModels: This class profiles the performance of different models, returning results such as model speed and FLOPs. Attributes: - paths (List[str]): Paths of the models to profile. + paths (list[str]): Paths of the models to profile. num_timed_runs (int): Number of timed runs for the profiling. num_warmup_runs (int): Number of warmup runs before profiling. min_time (float): Minimum number of seconds to profile for. @@ -414,7 +414,7 @@ class ProfileModels: Initialize the ProfileModels class for profiling models. Args: - paths (List[str]): List of paths of the models to be profiled. + paths (list[str]): List of paths of the models to be profiled. num_timed_runs (int): Number of timed runs for the profiling. num_warmup_runs (int): Number of warmup runs before the actual profiling starts. min_time (float): Minimum time in seconds for profiling a model. @@ -446,7 +446,7 @@ class ProfileModels: Profile YOLO models for speed and accuracy across various formats including ONNX and TensorRT. Returns: - (List[dict]): List of dictionaries containing profiling results for each model. + (list[dict]): List of dictionaries containing profiling results for each model. Examples: Profile models and print results @@ -501,7 +501,7 @@ class ProfileModels: Return a list of paths for all relevant model files given by the user. Returns: - (List[Path]): List of Path objects for the model files. + (list[Path]): List of Path objects for the model files. """ files = [] for path in self.paths: @@ -707,7 +707,7 @@ class ProfileModels: Print a formatted table of model profiling results. Args: - table_rows (List[str]): List of formatted table row strings. + table_rows (list[str]): List of formatted table row strings. """ gpu = torch.cuda.get_device_name(0) if torch.cuda.is_available() else "GPU" headers = [ diff --git a/ultralytics/utils/callbacks/clearml.py b/ultralytics/utils/callbacks/clearml.py index 3f223297..446ee010 100644 --- a/ultralytics/utils/callbacks/clearml.py +++ b/ultralytics/utils/callbacks/clearml.py @@ -19,7 +19,7 @@ def _log_debug_samples(files, title: str = "Debug Samples") -> None: Log files (images) as debug samples in the ClearML task. Args: - files (List[Path]): A list of file paths in PosixPath format. + files (list[Path]): A list of file paths in PosixPath format. title (str): A title that groups together images with the same values. """ import re diff --git a/ultralytics/utils/callbacks/comet.py b/ultralytics/utils/callbacks/comet.py index 2f9ef13a..f0941132 100644 --- a/ultralytics/utils/callbacks/comet.py +++ b/ultralytics/utils/callbacks/comet.py @@ -163,7 +163,7 @@ def _scale_bounding_box_to_original_image_shape( ratio_pad (tuple): Ratio and padding information for scaling. Returns: - (List[float]): Scaled bounding box coordinates in xywh format with top-left corner adjustment. + (list[float]): Scaled bounding box coordinates in xywh format with top-left corner adjustment. """ resized_image_height, resized_image_width = resized_image_shape @@ -297,7 +297,7 @@ def _extract_segmentation_annotation(segmentation_raw: str, decode: Callable) -> decode (Callable): Function to decode the compressed segmentation data. Returns: - (List[List[Any]] | None): List of polygon points or None if extraction fails. + (list[list[Any]] | None): List of polygon points or None if extraction fails. """ try: mask = decode(segmentation_raw) @@ -322,7 +322,7 @@ def _fetch_annotations(img_idx, image_path, batch, prediction_metadata_map, clas class_map (dict): Additional class mapping for label conversion. Returns: - (List | None): List of annotation dictionaries or None if no annotations exist. + (list | None): List of annotation dictionaries or None if no annotations exist. """ ground_truth_annotations = _format_ground_truth_annotations_for_detection( img_idx, image_path, batch, class_label_map @@ -365,9 +365,9 @@ def _log_images(experiment, image_paths, curr_step: int | None, annotations=None Args: experiment (comet_ml.CometExperiment): The Comet ML experiment to log images to. - image_paths (List[Path]): List of paths to images that will be logged. + image_paths (list[Path]): List of paths to images that will be logged. curr_step (int): Current training step/iteration for tracking in the experiment timeline. - annotations (List[List[dict]], optional): Nested list of annotation dictionaries for each image. Each + annotations (list[list[dict]], optional): Nested list of annotation dictionaries for each image. Each annotation contains visualization data like bounding boxes, labels, and confidence scores. """ if annotations: diff --git a/ultralytics/utils/checks.py b/ultralytics/utils/checks.py index 2ea6e8e1..99f4d731 100644 --- a/ultralytics/utils/checks.py +++ b/ultralytics/utils/checks.py @@ -60,7 +60,7 @@ def parse_requirements(file_path=ROOT.parent / "requirements.txt", package=""): package (str, optional): Python package to use instead of requirements.txt file. Returns: - requirements (List[SimpleNamespace]): List of parsed requirements as SimpleNamespace objects with `name` and + requirements (list[SimpleNamespace]): List of parsed requirements as SimpleNamespace objects with `name` and `specifier` attributes. Examples: @@ -120,14 +120,14 @@ def check_imgsz(imgsz, stride=32, min_dim=1, max_dim=2, floor=0): stride, update it to the nearest multiple of the stride that is greater than or equal to the given floor value. Args: - imgsz (int | List[int]): Image size. + imgsz (int | list[int]): Image size. stride (int): Stride value. min_dim (int): Minimum number of dimensions. max_dim (int): Maximum number of dimensions. floor (int): Minimum allowed value for image size. Returns: - (List[int] | int): Updated image size. + (list[int] | int): Updated image size. """ # Convert stride to integer if it is a tensor stride = int(stride.max() if isinstance(stride, torch.Tensor) else stride) @@ -363,7 +363,7 @@ def check_requirements(requirements=ROOT.parent / "requirements.txt", exclude=() Check if installed dependencies meet Ultralytics YOLO models requirements and attempt to auto-update if needed. Args: - requirements (Path | str | List[str]): Path to a requirements.txt file, a single package requirement as a + requirements (Path | str | list[str]): Path to a requirements.txt file, a single package requirement as a string, or a list of package requirements as strings. exclude (tuple): Tuple of package names to exclude from checking. install (bool): If True, attempt to auto-update packages that don't meet requirements. @@ -483,7 +483,7 @@ def check_suffix(file="yolo11n.pt", suffix=".pt", msg=""): Check file(s) for acceptable suffix. Args: - file (str | List[str]): File or list of files to check. + file (str | list[str]): File or list of files to check. suffix (str | tuple): Acceptable suffix or tuple of suffixes. msg (str): Additional message to display in case of error. """ diff --git a/ultralytics/utils/dist.py b/ultralytics/utils/dist.py index d117dcbc..125a5972 100644 --- a/ultralytics/utils/dist.py +++ b/ultralytics/utils/dist.py @@ -85,7 +85,7 @@ def generate_ddp_command(world_size: int, trainer): trainer (ultralytics.engine.trainer.BaseTrainer): The trainer containing configuration for distributed training. Returns: - cmd (List[str]): The command to execute for distributed training. + cmd (list[str]): The command to execute for distributed training. file (str): Path to the temporary file created for DDP training. """ import __main__ # noqa local import to avoid https://github.com/Lightning-AI/pytorch-lightning/issues/15218 diff --git a/ultralytics/utils/downloads.py b/ultralytics/utils/downloads.py index 30cbd417..6257d218 100644 --- a/ultralytics/utils/downloads.py +++ b/ultralytics/utils/downloads.py @@ -411,7 +411,7 @@ def get_github_assets( Returns: tag (str): The release tag. - assets (List[str]): A list of asset names. + assets (list[str]): A list of asset names. Examples: >>> tag, assets = get_github_assets(repo="ultralytics/assets", version="latest") @@ -503,7 +503,7 @@ def download( Supports concurrent downloads if multiple threads are specified. Args: - url (str | List[str]): The URL or list of URLs of the files to be downloaded. + url (str | list[str]): The URL or list of URLs of the files to be downloaded. dir (Path, optional): The directory where the files will be saved. unzip (bool, optional): Flag to unzip the files after downloading. delete (bool, optional): Flag to delete the zip files after extraction. diff --git a/ultralytics/utils/export.py b/ultralytics/utils/export.py index e8eefe05..2a2f49a9 100644 --- a/ultralytics/utils/export.py +++ b/ultralytics/utils/export.py @@ -27,9 +27,9 @@ def export_onnx( im (torch.Tensor): Example input tensor for the model. onnx_file (str): Path to save the exported ONNX file. opset (int): ONNX opset version to use for export. - input_names (List[str]): List of input tensor names. - output_names (List[str]): List of output tensor names. - dynamic (bool | Dict, optional): Whether to enable dynamic axes. + input_names (list[str]): List of input tensor names. + output_names (list[str]): List of output tensor names. + dynamic (bool | dict, optional): Whether to enable dynamic axes. Notes: Setting `do_constant_folding=True` may cause issues with DNN inference for torch>=1.12. @@ -71,10 +71,10 @@ def export_engine( half (bool, optional): Enable FP16 precision. int8 (bool, optional): Enable INT8 precision. dynamic (bool, optional): Enable dynamic input shapes. - shape (Tuple[int, int, int, int], optional): Input shape (batch, channels, height, width). + shape (tuple[int, int, int, int], optional): Input shape (batch, channels, height, width). dla (int, optional): DLA core to use (Jetson devices only). dataset (ultralytics.data.build.InfiniteDataLoader, optional): Dataset for INT8 calibration. - metadata (Dict, optional): Metadata to include in the engine file. + metadata (dict, optional): Metadata to include in the engine file. verbose (bool, optional): Enable verbose logging. prefix (str, optional): Prefix for log messages. diff --git a/ultralytics/utils/instance.py b/ultralytics/utils/instance.py index 3ebe0a63..bfc1d544 100644 --- a/ultralytics/utils/instance.py +++ b/ultralytics/utils/instance.py @@ -146,7 +146,7 @@ class Bboxes: Concatenate a list of Bboxes objects into a single Bboxes object. Args: - boxes_list (List[Bboxes]): A list of Bboxes objects to concatenate. + boxes_list (list[Bboxes]): A list of Bboxes objects to concatenate. axis (int, optional): The axis along which to concatenate the bounding boxes. Returns: @@ -458,7 +458,7 @@ class Instances: Concatenate a list of Instances objects into a single Instances object. Args: - instances_list (List[Instances]): A list of Instances objects to concatenate. + instances_list (list[Instances]): A list of Instances objects to concatenate. axis (int, optional): The axis along which the arrays will be concatenated. Returns: diff --git a/ultralytics/utils/metrics.py b/ultralytics/utils/metrics.py index 0b81f1c7..9409d6a1 100644 --- a/ultralytics/utils/metrics.py +++ b/ultralytics/utils/metrics.py @@ -318,7 +318,7 @@ class ConfusionMatrix(DataExportMixin): task (str): The type of task, either 'detect' or 'classify'. matrix (np.ndarray): The confusion matrix, with dimensions depending on the task. nc (int): The number of category. - names (List[str]): The names of the classes, used as labels on the plot. + names (list[str]): The names of the classes, used as labels on the plot. matches (dict): Contains the indices of ground truths and predictions categorized into TP, FP and FN. """ @@ -327,7 +327,7 @@ class ConfusionMatrix(DataExportMixin): Initialize a ConfusionMatrix instance. Args: - names (Dict[int, str], optional): Names of classes, used as labels on the plot. + names (dict[int, str], optional): Names of classes, used as labels on the plot. task (str, optional): Type of task, either 'detect' or 'classify'. save_matches (bool, optional): Save the indices of GTs, TPs, FPs, FNs for visualization. """ @@ -346,7 +346,7 @@ class ConfusionMatrix(DataExportMixin): Args: mtype (str): Match type identifier ('TP', 'FP', 'FN' or 'GT'). - batch (Dict[str, Any]): Batch data containing detection results with keys + batch (dict[str, Any]): Batch data containing detection results with keys like 'bboxes', 'cls', 'conf', 'keypoints', 'masks'. idx (int): Index of the specific detection to append from the batch. @@ -368,8 +368,8 @@ class ConfusionMatrix(DataExportMixin): Update confusion matrix for classification task. Args: - preds (List[N, min(nc,5)]): Predicted class labels. - targets (List[N, 1]): Ground truth class labels. + preds (list[N, min(nc,5)]): Predicted class labels. + targets (list[N, 1]): Ground truth class labels. """ preds, targets = torch.cat(preds)[:, 0], torch.cat(targets) for p, t in zip(preds.cpu().numpy(), targets.cpu().numpy()): @@ -386,10 +386,10 @@ class ConfusionMatrix(DataExportMixin): Update confusion matrix for object detection task. Args: - detections (Dict[str, torch.Tensor]): Dictionary containing detected bounding boxes and their associated information. + detections (dict[str, torch.Tensor]): Dictionary containing detected bounding boxes and their associated information. Should contain 'cls', 'conf', and 'bboxes' keys, where 'bboxes' can be Array[N, 4] for regular boxes or Array[N, 5] for OBB with angle. - batch (Dict[str, Any]): Batch dictionary containing ground truth data with 'bboxes' (Array[M, 4]| Array[M, 5]) and + batch (dict[str, Any]): Batch dictionary containing ground truth data with 'bboxes' (Array[M, 4]| Array[M, 5]) and 'cls' (Array[M]) keys, where M is the number of ground truth objects. conf (float, optional): Confidence threshold for detections. iou_thres (float, optional): IoU threshold for matching detections to ground truth. @@ -599,7 +599,7 @@ class ConfusionMatrix(DataExportMixin): decimals (int): Number of decimal places to round the output values to. Returns: - (List[Dict[str, float]]): A list of dictionaries, each representing one predicted class with corresponding values for all actual classes. + (list[dict[str, float]]): A list of dictionaries, each representing one predicted class with corresponding values for all actual classes. Examples: >>> results = model.val(data="coco8.yaml", plots=True) @@ -651,7 +651,7 @@ def plot_pr_curve( py (np.ndarray): Y values for the PR curve. ap (np.ndarray): Average precision values. save_dir (Path, optional): Path to save the plot. - names (Dict[int, str], optional): Dictionary mapping class indices to class names. + names (dict[int, str], optional): Dictionary mapping class indices to class names. on_plot (callable, optional): Function to call after plot is saved. """ import matplotlib.pyplot as plt # scope for faster 'import ultralytics' @@ -695,7 +695,7 @@ def plot_mc_curve( px (np.ndarray): X values for the metric-confidence curve. py (np.ndarray): Y values for the metric-confidence curve. save_dir (Path, optional): Path to save the plot. - names (Dict[int, str], optional): Dictionary mapping class indices to class names. + names (dict[int, str], optional): Dictionary mapping class indices to class names. xlabel (str, optional): X-axis label. ylabel (str, optional): Y-axis label. on_plot (callable, optional): Function to call after plot is saved. @@ -780,7 +780,7 @@ def ap_per_class( plot (bool, optional): Whether to plot PR curves or not. on_plot (callable, optional): A callback to pass plots path and data when they are rendered. save_dir (Path, optional): Directory to save the PR curves. - names (Dict[int, str], optional): Dictionary of class names to plot PR curves. + names (dict[int, str], optional): Dictionary of class names to plot PR curves. eps (float, optional): A small value to avoid division by zero. prefix (str, optional): A prefix string for saving the plot files. @@ -1034,11 +1034,11 @@ class DetMetrics(SimpleClass, DataExportMixin): Utility class for computing detection metrics such as precision, recall, and mean average precision (mAP). Attributes: - names (Dict[int, str]): A dictionary of class names. + names (dict[int, str]): A dictionary of class names. box (Metric): An instance of the Metric class for storing detection results. - speed (Dict[str, float]): A dictionary for storing execution times of different parts of the detection process. + speed (dict[str, float]): A dictionary for storing execution times of different parts of the detection process. task (str): The task type, set to 'detect'. - stats (Dict[str, List]): A dictionary containing lists for true positives, confidence scores, predicted classes, target classes, and target images. + stats (dict[str, list]): A dictionary containing lists for true positives, confidence scores, predicted classes, target classes, and target images. nt_per_class: Number of targets per class. nt_per_image: Number of targets per image. @@ -1063,7 +1063,7 @@ class DetMetrics(SimpleClass, DataExportMixin): Initialize a DetMetrics instance with a save directory, plot flag, and class names. Args: - names (Dict[int, str], optional): Dictionary of class names. + names (dict[int, str], optional): Dictionary of class names. """ self.names = names self.box = Metric() @@ -1078,7 +1078,7 @@ class DetMetrics(SimpleClass, DataExportMixin): Update statistics by appending new values to existing stat collections. Args: - stat (Dict[str, any]): Dictionary containing new statistical values to append. + stat (dict[str, any]): Dictionary containing new statistical values to append. Keys should match existing keys in self.stats. """ for k in self.stats.keys(): @@ -1094,7 +1094,7 @@ class DetMetrics(SimpleClass, DataExportMixin): on_plot (callable, optional): Function to call after plots are generated. Defaults to None. Returns: - (Dict[str, np.ndarray]): Dictionary containing concatenated statistics arrays. + (dict[str, np.ndarray]): Dictionary containing concatenated statistics arrays. """ stats = {k: np.concatenate(v, 0) for k, v in self.stats.items()} # to numpy if not stats: @@ -1176,7 +1176,7 @@ class DetMetrics(SimpleClass, DataExportMixin): decimals (int): Number of decimal places to round the metrics values to. Returns: - (List[Dict[str, Any]]): A list of dictionaries, each representing one class with corresponding metric values. + (list[dict[str, Any]]): A list of dictionaries, each representing one class with corresponding metric values. Examples: >>> results = model.val(data="coco8.yaml") @@ -1206,12 +1206,12 @@ class SegmentMetrics(DetMetrics): Calculate and aggregate detection and segmentation metrics over a given set of classes. Attributes: - names (Dict[int, str]): Dictionary of class names. + names (dict[int, str]): Dictionary of class names. box (Metric): An instance of the Metric class for storing detection results. seg (Metric): An instance of the Metric class to calculate mask segmentation metrics. - speed (Dict[str, float]): A dictionary for storing execution times of different parts of the detection process. + speed (dict[str, float]): A dictionary for storing execution times of different parts of the detection process. task (str): The task type, set to 'segment'. - stats (Dict[str, List]): A dictionary containing lists for true positives, confidence scores, predicted classes, target classes, and target images. + stats (dict[str, list]): A dictionary containing lists for true positives, confidence scores, predicted classes, target classes, and target images. nt_per_class: Number of targets per class. nt_per_image: Number of targets per image. @@ -1232,7 +1232,7 @@ class SegmentMetrics(DetMetrics): Initialize a SegmentMetrics instance with a save directory, plot flag, and class names. Args: - names (Dict[int, str], optional): Dictionary of class names. + names (dict[int, str], optional): Dictionary of class names. """ DetMetrics.__init__(self, names) self.seg = Metric() @@ -1249,7 +1249,7 @@ class SegmentMetrics(DetMetrics): on_plot (callable, optional): Function to call after plots are generated. Defaults to None. Returns: - (Dict[str, np.ndarray]): Dictionary containing concatenated statistics arrays. + (dict[str, np.ndarray]): Dictionary containing concatenated statistics arrays. """ stats = DetMetrics.process(self, save_dir, plot, on_plot=on_plot) # process box stats results_mask = ap_per_class( @@ -1320,7 +1320,7 @@ class SegmentMetrics(DetMetrics): decimals (int): Number of decimal places to round the metrics values to. Returns: - (List[Dict[str, Any]]): A list of dictionaries, each representing one class with corresponding metric values. + (list[dict[str, Any]]): A list of dictionaries, each representing one class with corresponding metric values. Examples: >>> results = model.val(data="coco8-seg.yaml") @@ -1343,12 +1343,12 @@ class PoseMetrics(DetMetrics): Calculate and aggregate detection and pose metrics over a given set of classes. Attributes: - names (Dict[int, str]): Dictionary of class names. + names (dict[int, str]): Dictionary of class names. pose (Metric): An instance of the Metric class to calculate pose metrics. box (Metric): An instance of the Metric class for storing detection results. - speed (Dict[str, float]): A dictionary for storing execution times of different parts of the detection process. + speed (dict[str, float]): A dictionary for storing execution times of different parts of the detection process. task (str): The task type, set to 'pose'. - stats (Dict[str, List]): A dictionary containing lists for true positives, confidence scores, predicted classes, target classes, and target images. + stats (dict[str, list]): A dictionary containing lists for true positives, confidence scores, predicted classes, target classes, and target images. nt_per_class: Number of targets per class. nt_per_image: Number of targets per image. @@ -1369,7 +1369,7 @@ class PoseMetrics(DetMetrics): Initialize the PoseMetrics class with directory path, class names, and plotting options. Args: - names (Dict[int, str], optional): Dictionary of class names. + names (dict[int, str], optional): Dictionary of class names. """ super().__init__(names) self.pose = Metric() @@ -1386,7 +1386,7 @@ class PoseMetrics(DetMetrics): on_plot (callable, optional): Function to call after plots are generated. Returns: - (Dict[str, np.ndarray]): Dictionary containing concatenated statistics arrays. + (dict[str, np.ndarray]): Dictionary containing concatenated statistics arrays. """ stats = DetMetrics.process(self, save_dir, plot, on_plot=on_plot) # process box stats results_pose = ap_per_class( @@ -1461,7 +1461,7 @@ class PoseMetrics(DetMetrics): decimals (int): Number of decimal places to round the metrics values to. Returns: - (List[Dict[str, Any]]): A list of dictionaries, each representing one class with corresponding metric values. + (list[dict[str, Any]]): A list of dictionaries, each representing one class with corresponding metric values. Examples: >>> results = model.val(data="coco8-pose.yaml") @@ -1553,7 +1553,7 @@ class ClassifyMetrics(SimpleClass, DataExportMixin): decimals (int): Number of decimal places to round the metrics values to. Returns: - (List[Dict[str, float]]): A list with one dictionary containing Top-1 and Top-5 classification accuracy. + (list[dict[str, float]]): A list with one dictionary containing Top-1 and Top-5 classification accuracy. Examples: >>> results = model.val(data="imagenet10") @@ -1568,11 +1568,11 @@ class OBBMetrics(DetMetrics): Metrics for evaluating oriented bounding box (OBB) detection. Attributes: - names (Dict[int, str]): Dictionary of class names. + names (dict[int, str]): Dictionary of class names. box (Metric): An instance of the Metric class for storing detection results. - speed (Dict[str, float]): A dictionary for storing execution times of different parts of the detection process. + speed (dict[str, float]): A dictionary for storing execution times of different parts of the detection process. task (str): The task type, set to 'obb'. - stats (Dict[str, List]): A dictionary containing lists for true positives, confidence scores, predicted classes, target classes, and target images. + stats (dict[str, list]): A dictionary containing lists for true positives, confidence scores, predicted classes, target classes, and target images. nt_per_class: Number of targets per class. nt_per_image: Number of targets per image. @@ -1585,7 +1585,7 @@ class OBBMetrics(DetMetrics): Initialize an OBBMetrics instance with directory, plotting, and class names. Args: - names (Dict[int, str], optional): Dictionary of class names. + names (dict[int, str], optional): Dictionary of class names. """ DetMetrics.__init__(self, names) # TODO: probably remove task as well diff --git a/ultralytics/utils/nms.py b/ultralytics/utils/nms.py index b60a94a8..bc486057 100644 --- a/ultralytics/utils/nms.py +++ b/ultralytics/utils/nms.py @@ -38,10 +38,10 @@ def non_max_suppression( containing boxes, classes, and optional masks. conf_thres (float): Confidence threshold for filtering detections. Valid values are between 0.0 and 1.0. iou_thres (float): IoU threshold for NMS filtering. Valid values are between 0.0 and 1.0. - classes (List[int], optional): List of class indices to consider. If None, all classes are considered. + classes (list[int], optional): List of class indices to consider. If None, all classes are considered. agnostic (bool): Whether to perform class-agnostic NMS. multi_label (bool): Whether each box can have multiple labels. - labels (List[List[Union[int, float, torch.Tensor]]]): A priori labels for each image. + labels (list[list[Union[int, float, torch.Tensor]]]): A priori labels for each image. max_det (int): Maximum number of detections to keep per image. nc (int): Number of classes. Indices after this are considered masks. max_time_img (float): Maximum time in seconds for processing one image. @@ -52,9 +52,9 @@ def non_max_suppression( return_idxs (bool): Whether to return the indices of kept detections. Returns: - output (List[torch.Tensor]): List of detections per image with shape (num_boxes, 6 + num_masks) + output (list[torch.Tensor]): List of detections per image with shape (num_boxes, 6 + num_masks) containing (x1, y1, x2, y2, confidence, class, mask1, mask2, ...). - keepi (List[torch.Tensor]): Indices of kept detections if return_idxs=True. + keepi (list[torch.Tensor]): Indices of kept detections if return_idxs=True. """ # Checks assert 0 <= conf_thres <= 1, f"Invalid Confidence threshold {conf_thres}, valid values are between 0.0 and 1.0" diff --git a/ultralytics/utils/patches.py b/ultralytics/utils/patches.py index 4478f7bc..4527daec 100644 --- a/ultralytics/utils/patches.py +++ b/ultralytics/utils/patches.py @@ -51,7 +51,7 @@ def imwrite(filename: str, img: np.ndarray, params: list[int] | None = None) -> Args: filename (str): Path to the file to write. img (np.ndarray): Image to write. - params (List[int], optional): Additional parameters for image encoding. + params (list[int], optional): Additional parameters for image encoding. Returns: (bool): True if the file was written successfully, False otherwise. @@ -172,7 +172,7 @@ def override_configs(args, overrides: dict[str, Any] | None = None): Args: args (IterableSimpleNamespace): Original configuration arguments. - overrides (Dict[str, Any]): Dictionary of overrides to apply. + overrides (dict[str, Any]): Dictionary of overrides to apply. Yields: (IterableSimpleNamespace): Configuration arguments with overrides applied. diff --git a/ultralytics/utils/plotting.py b/ultralytics/utils/plotting.py index 0d375b70..05ff01ad 100644 --- a/ultralytics/utils/plotting.py +++ b/ultralytics/utils/plotting.py @@ -26,7 +26,7 @@ class Colors: RGB values and accessing predefined color schemes for object detection and pose estimation. Attributes: - palette (List[tuple]): List of RGB color tuples for general use. + palette (list[tuple]): List of RGB color tuples for general use. n (int): The number of colors in the palette. pose_palette (np.ndarray): A specific color palette array for pose estimation with dtype np.uint8. @@ -176,9 +176,9 @@ class Annotator: pil (bool): Whether to use PIL or cv2 for drawing annotations. font (ImageFont.truetype | ImageFont.load_default): Font used for text annotations. lw (float): Line width for drawing. - skeleton (List[List[int]]): Skeleton structure for keypoints. - limb_color (List[int]): Color palette for limbs. - kpt_color (List[int]): Color palette for keypoints. + skeleton (list[list[int]]): Skeleton structure for keypoints. + limb_color (list[int]): Color palette for limbs. + kpt_color (list[int]): Color palette for keypoints. dark_colors (set): Set of colors considered dark for text contrast. light_colors (set): Set of colors considered light for text contrast. @@ -369,7 +369,7 @@ class Annotator: Args: masks (torch.Tensor | np.ndarray): Predicted masks with shape: [n, h, w] - colors (List[List[int]]): Colors for predicted masks, [[r, g, b] * n] + colors (list[list[int]]): Colors for predicted masks, [[r, g, b] * n] im_gpu (torch.Tensor | None): Image is in cuda, shape: [3, h, w], range: [0, 1] alpha (float, optional): Mask transparency: 0.0 fully transparent, 1.0 opaque. retina_masks (bool, optional): Whether to use high resolution masks or not. @@ -484,7 +484,7 @@ class Annotator: Add text to an image using PIL or cv2. Args: - xy (List[int]): Top-left coordinates for text placement. + xy (list[int]): Top-left coordinates for text placement. text (str): Text to be drawn. txt_color (tuple, optional): Text color (R, G, B). anchor (str, optional): Text anchor position ('top' or 'bottom'). @@ -695,11 +695,11 @@ def plot_images( Plot image grid with labels, bounding boxes, masks, and keypoints. Args: - labels (Dict[str, Any]): Dictionary containing detection data with keys like 'cls', 'bboxes', 'conf', 'masks', 'keypoints', 'batch_idx', 'img'. + labels (dict[str, Any]): Dictionary containing detection data with keys like 'cls', 'bboxes', 'conf', 'masks', 'keypoints', 'batch_idx', 'img'. images (torch.Tensor | np.ndarray]): Batch of images to plot. Shape: (batch_size, channels, height, width). - paths (Optional[List[str]]): List of file paths for each image in the batch. + paths (Optional[list[str]]): List of file paths for each image in the batch. fname (str): Output filename for the plotted image grid. - names (Optional[Dict[int, str]]): Dictionary mapping class indices to class names. + names (Optional[dict[int, str]]): Dictionary mapping class indices to class names. on_plot (Optional[Callable]): Optional callback function to be called after saving the plot. max_size (int): Maximum size of the output image grid. max_subplots (int): Maximum number of subplots in the image grid. diff --git a/ultralytics/utils/triton.py b/ultralytics/utils/triton.py index 4a7df273..6c122f3c 100644 --- a/ultralytics/utils/triton.py +++ b/ultralytics/utils/triton.py @@ -20,10 +20,10 @@ class TritonRemoteModel: triton_client: The Triton client (either HTTP or gRPC). InferInput: The input class for the Triton client. InferRequestedOutput: The output request class for the Triton client. - input_formats (List[str]): The data types of the model inputs. - np_input_formats (List[type]): The numpy data types of the model inputs. - input_names (List[str]): The names of the model inputs. - output_names (List[str]): The names of the model outputs. + input_formats (list[str]): The data types of the model inputs. + np_input_formats (list[type]): The numpy data types of the model inputs. + input_names (list[str]): The names of the model inputs. + output_names (list[str]): The names of the model outputs. metadata: The metadata associated with the model. Methods: @@ -96,7 +96,7 @@ class TritonRemoteModel: for the corresponding model input. Returns: - (List[np.ndarray]): Model outputs with the same dtype as the input. Each element in the list + (list[np.ndarray]): Model outputs with the same dtype as the input. Each element in the list corresponds to one of the model's output tensors. Examples: