546. Remove Boxes (HARD)

本文介绍了一种通过动态规划解决整数序列中连续相等子序列删除问题的方法,旨在获得最高的得分。文章详细解释了如何使用备忘录技术减少重复计算,并提供了具体的实现代码。

给定一个整数序列,每次删除其中连续相等的子序列,得分为序列长度的平方 求最高得分。

dp方程如下:

  memo[l][r][k] = max(memo[l][r][k], dfs(boxes,memo,l,i,k+1) + dfs(boxes,memo,i+1,r-1,0));

意思是在序列的l-r部分后接k长度的 r值序列 所能得到的最大得分。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    int removeBoxes(vector<int>& boxes) {
        int n=boxes.size();
        int memo[100][100][100] = {0};
        return dfs(boxes,memo,0,n-1,0);
    }
     
    int dfs(vector<int>& boxes,int memo[100][100][100], int l,int r,int k){
        if (l>r) return 0;
        if (memo[l][r][k]!=0) return memo[l][r][k];
 
        while (r>l && boxes[r]==boxes[r-1]) {r--;k++;}
        memo[l][r][k] = dfs(boxes,memo,l,r-1,0) + (k+1)*(k+1);
        for (int i=l; i<r; i++){
            if (boxes[i]==boxes[r]){
                memo[l][r][k] = max(memo[l][r][k], dfs(boxes,memo,l,i,k+1) + dfs(boxes,memo,i+1,r-1,0));
            }
        }
        return memo[l][r][k];
    }
};
# Ultralytics YOLO 🚀, AGPL-3.0 license import json import random from collections import defaultdict from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path import cv2 import numpy as np import torch from PIL import Image from torch.utils.data import ConcatDataset from ultralytics.utils import LOCAL_RANK, NUM_THREADS, TQDM, colorstr from ultralytics.utils.ops import resample_segments from ultralytics.utils.torch_utils import TORCHVISION_0_18 from .augment import ( Compose, Format, LetterBox, RandomLoadText, classify_augmentations, classify_transforms, v8_transforms, ) from .base import BaseDataset from .utils import ( HELP_URL, LOGGER, get_hash, img2label_paths, load_dataset_cache_file, save_dataset_cache_file, verify_image, verify_image_label, ) # Ultralytics dataset *.cache version, >= 1.0.0 for YOLOv8 DATASET_CACHE_VERSION = "1.0.3" # 修复点1: 添加完整的Instances类定义(包含convert_bbox和denormalize方法) class Instances: """Instances class for handling bounding boxes, segments, and keypoints in object detection.""" def __init__(self, bboxes, segments=None, keypoints=None, bbox_format="xywh", normalized=True): """ Initialize Instances. Args: bboxes (np.ndarray): Bounding boxes array segments (np.ndarray, optional): Segmentation masks keypoints (np.ndarray, optional): Keypoints bbox_format (str): Bounding box format ('xywh', 'xyxy', etc.) normalized (bool): Whether coordinates are normalized """ self.bboxes = bboxes self.segments = segments self.keypoints = keypoints self.bbox_format = bbox_format self.normalized = normalized self.cls = None # 添加cls属性占位 def __len__(self): """Return the number of instances.""" return len(self.bboxes) @classmethod def empty(cls): """Return an empty Instances object.""" return cls(np.zeros((0, 4), dtype=np.float32)) @classmethod def cat(cls, instances_list): """Concatenate multiple Instances objects into one.""" bboxes = np.concatenate([inst.bboxes for inst in instances_list], axis=0) segments = np.concatenate([inst.segments for inst in instances_list], axis=0) if instances_list[0].segments is not None else None keypoints = np.concatenate([inst.keypoints for inst in instances_list], axis=0) if instances_list[0].keypoints is not None else None return cls(bboxes, segments, keypoints, bbox_format=instances_list[0].bbox_format, normalized=instances_list[0].normalized) def convert_bbox(self, format): """Convert bounding box format. Args: format (str): Target format, either 'xyxy' or 'xywh'. """ if self.bbox_format == format: return if self.bbox_format == "xywh" and format == "xyxy": # Convert from xywh to xyxy x, y, w, h = self.bboxes.T xyxy = np.array([x - w/2, y - h/2, x + w/2, y + h/2]).T self.bboxes = xyxy self.bbox_format = "xyxy" elif self.bbox_format == "xyxy" and format == "xywh": # Convert from xyxy to xywh x1, y1, x2, y2 = self.bboxes.T xywh = np.array([(x1+x2)/2, (y1+y2)/2, x2-x1, y2-y1]).T self.bboxes = xywh self.bbox_format = "xywh" else: raise ValueError(f"Conversion from {self.bbox_format} to {format} not supported") # 添加缺失的denormalize方法 def denormalize(self, w, h): """ Denormalize bounding boxes from normalized coordinates to pixel coordinates. Args: w (int): Image width h (int): Image height """ if not self.normalized: return if self.bboxes is not None and len(self.bboxes) > 0: if self.bbox_format == "xywh": # Denormalize xywh format self.bboxes[:, 0] *= w self.bboxes[:, 1] *= h self.bboxes[:, 2] *= w self.bboxes[:, 3] *= h elif self.bbox_format == "xyxy": # Denormalize xyxy format self.bboxes[:, [0, 2]] *= w self.bboxes[:, [1, 3]] *= h # 处理segments(如果存在) if self.segments is not None and len(self.segments) > 0: # segments shape: (n, num_points, 2) self.segments[..., 0] *= w self.segments[..., 1] *= h # 处理keypoints(如果存在) if self.keypoints is not None and len(self.keypoints) > 0: # keypoints shape: (n, num_keypoints, 2 or 3) self.keypoints[..., 0] *= w self.keypoints[..., 1] *= h self.normalized = False class Mosaic: """Mosaic data augmentation for object detection datasets. This class combines 4 images into a single mosaic image, adjusting labels accordingly. """ def __init__(self, dataset, imgsz=640, p=0.5, border=[-320, -320]): """ Initialize Mosaic augmentation. Args: dataset (YOLODataset): The dataset object imgsz (int): Output image size (height and width) p (float): Probability of applying mosaic augmentation border (list): Border values for random center placement """ self.dataset = dataset self.imgsz = imgsz self.p = p self.border = border self.mosaic_border = [-imgsz // 2, -imgsz // 2] def __call__(self, data): """Apply mosaic augmentation to a batch of data.""" # Only apply mosaic with given probability if random.random() > self.p: return data # Check if data contains necessary components if 'img' not in data or 'instances' not in data: return data # Get current image and instances img = data['img'] instances = data['instances'] h0, w0 = img.shape[:2] # original height and width # Create mosaic image mosaic_img = np.full((self.imgsz * 2, self.imgsz * 2, img.shape[2]), 114, dtype=np.uint8) # Random center placement yc, xc = [int(random.uniform(-x, 2 * self.imgsz + x)) for x in self.mosaic_border] # Get 3 additional random indices indices = [random.randint(0, len(self.dataset) - 1) for _ in range(3)] mosaic_instances = [] # Place 4 images in mosaic for i, index in enumerate([0] + indices): if i == 0: # current image img_i, instances_i = img, instances else: # Get other image and instances from dataset data_i = self.dataset[index] img_i = data_i['img'] instances_i = data_i['instances'] # Resize image r = self.imgsz / max(img_i.shape[:2]) img_i = cv2.resize(img_i, (int(w0 * r), int(h0 * r)), interpolation=cv2.INTER_LINEAR) h, w = img_i.shape[:2] # Place image in mosaic if i == 0: # top left x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h elif i == 1: # top right x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, self.imgsz * 2), yc x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h elif i == 2: # bottom left x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(self.imgsz * 2, yc + h) x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(h, y2a - y1a) elif i == 3: # bottom right x1a, y1a, x2a, y2a = xc, yc, min(xc + w, self.imgsz * 2), min(self.imgsz * 2, yc + h) x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(h, y2a - y1a) # Place image segment in mosaic mosaic_img[y1a:y2a, x1a:x2a] = img_i[y1b:y2b, x1b:x2b] padw, padh = x1a - x1b, y1a - y1b # Adjust instances if they exist if instances_i is not None and len(instances_i) > 0: # 确保使用xyxy格式进行处理 if instances_i.bbox_format != "xyxy": instances_i.convert_bbox("xyxy") # 修复点2: 使用copy()代替clone()处理NumPy数组 bboxes_copy = instances_i.bboxes.copy() segments_copy = instances_i.segments.copy() if instances_i.segments is not None else None keypoints_copy = instances_i.keypoints.copy() if instances_i.keypoints is not None else None # Create a copy of instances to avoid modifying original new_instances = Instances( bboxes_copy, segments_copy, keypoints_copy, bbox_format=instances_i.bbox_format, normalized=instances_i.normalized ) # Adjust bboxes if new_instances.bboxes is not None and len(new_instances.bboxes) > 0: bboxes = new_instances.bboxes if new_instances.normalized: # Convert normalized coordinates to pixels bboxes[:, [0, 2]] = bboxes[:, [0, 2]] * w bboxes[:, [1, 3]] = bboxes[:, [1, 3]] * h # Adjust coordinates bboxes[:, [0, 2]] = bboxes[:, [0, 2]] * r + padw bboxes[:, [1, 3]] = bboxes[:, [1, 3]] * r + padh # Convert back to normalized coordinates bboxes[:, [0, 2]] = bboxes[:, [0, 2]] / (self.imgsz * 2) bboxes[:, [1, 3]] = bboxes[:, [1, 3]] / (self.imgsz * 2) # Filter boxes that are completely outside the mosaic valid = ( (bboxes[:, 0] < 1) & (bboxes[:, 1] < 1) & (bboxes[:, 2] > 0) & (bboxes[:, 3] > 0)) new_instances.bboxes = bboxes[valid] # Adjust class labels if present if new_instances.cls is not None: new_instances.cls = new_instances.cls[valid] # Add adjusted instances to mosaic mosaic_instances.append(new_instances) # Combine all instances if mosaic_instances: updated_instances = Instances.cat(mosaic_instances) else: updated_instances = Instances.empty() # Update data dictionary data['img'] = mosaic_img data['instances'] = updated_instances data['mosaic_border'] = self.mosaic_border return data class YOLODataset(BaseDataset): """ Dataset class for loading object detection and/or segmentation labels in YOLO format. Args: data (dict, optional): A dataset YAML dictionary. Defaults to None. task (str): An explicit arg to point current task, Defaults to 'detect'. Returns: (torch.utils.data.Dataset): A PyTorch dataset object that can be used for training an object detection model. """ def __init__(self, *args, data=None, task="detect", **kwargs): """Initializes the YOLODataset with optional configurations for segments and keypoints.""" self.use_segments = task == "segment" self.use_keypoints = task == "pose" self.use_obb = task == "obb" self.data = data self.mosaic_enabled = False # Will be enabled in build_transforms if conditions met assert not (self.use_segments and self.use_keypoints), "Can not use both segments and keypoints." super().__init__(*args, **kwargs) def cache_labels(self, path=Path("./labels.cache")): """ Cache dataset labels, check images and read shapes. Args: path (Path): Path where to save the cache file. Default is Path('./labels.cache'). Returns: (dict): labels. """ x = {"labels": []} nm, nf, ne, nc, msgs = 0, 0, 0, 0, [] # number missing, found, empty, corrupt, messages desc = f"{self.prefix}Scanning {path.parent / path.stem}..." total = len(self.im_files) nkpt, ndim = self.data.get("kpt_shape", (0, 0)) if self.use_keypoints and (nkpt <= 0 or ndim not in {2, 3}): raise ValueError( "'kpt_shape' in data.yaml missing or incorrect. Should be a list with [number of " "keypoints, number of dims (2 for x,y or 3 for x,y,visible)], i.e. 'kpt_shape: [17, 3]'" ) with ThreadPool(NUM_THREADS) as pool: results = pool.imap( func=verify_image_label, iterable=zip( self.im_files, self.label_files, repeat(self.prefix), repeat(self.use_keypoints), repeat(len(self.data["names"])), repeat(nkpt), repeat(ndim), ), ) pbar = TQDM(results, desc=desc, total=total) for im_file, lb, shape, segments, keypoint, nm_f, nf_f, ne_f, nc_f, msg in pbar: nm += nm_f nf += nf_f ne += ne_f nc += nc_f if im_file: x["labels"].append( { "im_file": im_file, "shape": shape, "cls": lb[:, 0:1], # n, 1 "bboxes": lb[:, 1:], # n, 4 "segments": segments, "keypoints": keypoint, "normalized": True, "bbox_format": "xywh", } ) if msg: msgs.append(msg) pbar.desc = f"{desc} {nf} images, {nm + ne} backgrounds, {nc} corrupt" pbar.close() if msgs: LOGGER.info("\n".join(msgs)) if nf == 0: LOGGER.warning(f"{self.prefix}WARNING ⚠️ No labels found in {path}. {HELP_URL}") x["hash"] = get_hash(self.label_files + self.im_files) x["results"] = nf, nm, ne, nc, len(self.im_files) x["msgs"] = msgs # warnings save_dataset_cache_file(self.prefix, path, x, DATASET_CACHE_VERSION) return x def get_labels(self): """Returns dictionary of labels for YOLO training.""" self.label_files = img2label_paths(self.im_files) cache_path = Path(self.label_files[0]).parent.with_suffix(".cache") try: cache, exists = load_dataset_cache_file(cache_path), True # attempt to load a *.cache file assert cache["version"] == DATASET_CACHE_VERSION # matches current version assert cache["hash"] == get_hash(self.label_files + self.im_files) # identical hash except (FileNotFoundError, AssertionError, AttributeError): cache, exists = self.cache_labels(cache_path), False # run cache ops # Display cache nf, nm, ne, nc, n = cache.pop("results") # found, missing, empty, corrupt, total if exists and LOCAL_RANK in {-1, 0}: d = f"Scanning {cache_path}... {nf} images, {nm + ne} backgrounds, {nc} corrupt" TQDM(None, desc=self.prefix + d, total=n, initial=n) # display results if cache["msgs"]: LOGGER.info("\n".join(cache["msgs"])) # display warnings # Read cache [cache.pop(k) for k in ("hash", "version", "msgs")] # remove items labels = cache["labels"] if not labels: LOGGER.warning(f"WARNING ⚠️ No images found in {cache_path}, training may not work correctly. {HELP_URL}") self.im_files = [lb["im_file"] for lb in labels] # update im_files # Check if the dataset is all boxes or all segments lengths = ((len(lb["cls"]), len(lb["bboxes"]), len(lb["segments"])) for lb in labels) len_cls, len_boxes, len_segments = (sum(x) for x in zip(*lengths)) if len_segments and len_boxes != len_segments: LOGGER.warning( f"WARNING ⚠️ Box and segment counts should be equal, but got len(segments) = {len_segments}, " f"len(boxes) = {len_boxes}. To resolve this only boxes will be used and all segments will be removed. " "To avoid this please supply either a detect or segment dataset, not a detect-segment mixed dataset." ) for lb in labels: lb["segments"] = [] if len_cls == 0: LOGGER.warning(f"WARNING ⚠️ No labels found in {cache_path}, training may not work correctly. {HELP_URL}") return labels def build_transforms(self, hyp=None): """Builds and appends transforms to the list.""" if self.augment: # Enable mosaic if specified in hyperparameters self.mosaic_enabled = hyp.mosaic > 0 if self.augment and not self.rect else False hyp.mosaic = hyp.mosaic if self.augment and not self.rect else 0.0 hyp.mixup = hyp.mixup if self.augment and not self.rect else 0.0 # Create transforms list transforms = [] # Add Mosaic transform if enabled if self.mosaic_enabled: transforms.append(Mosaic(self, self.imgsz, p=hyp.mosaic)) # Add other standard transforms transforms.extend(v8_transforms(self, self.imgsz, hyp)) else: transforms = [Compose(LetterBox(new_shape=(self.imgsz, self.imgsz), scaleup=False))] # Add format transform transforms.append( Format( bbox_format="xywh", normalize=True, return_mask=self.use_segments, return_keypoint=self.use_keypoints, return_obb=self.use_obb, batch_idx=True, mask_ratio=hyp.mask_ratio, mask_overlap=hyp.overlap_mask, bgr=hyp.bgr if self.augment else 0.0, # only affect training. ) ) # Return as Compose object return Compose(transforms) def close_mosaic(self, hyp): """Sets mosaic, copy_paste and mixup options to 0.0 and builds transformations.""" hyp.mosaic = 0.0 # set mosaic ratio=0.0 hyp.copy_paste = 0.0 # keep the same behavior as previous v8 close-mosaic hyp.mixup = 0.0 # keep the same behavior as previous v8 close-mosaic self.transforms = self.build_transforms(hyp) def update_labels_info(self, label): """ Custom your label format here. Note: cls is not with bboxes now, classification and semantic segmentation need an independent cls label Can also support classification and semantic segmentation by adding or removing dict keys there. """ bboxes = label.pop("bboxes") segments = label.pop("segments", []) keypoints = label.pop("keypoints", None) bbox_format = label.pop("bbox_format") normalized = label.pop("normalized") # NOTE: do NOT resample oriented boxes segment_resamples = 100 if self.use_obb else 1000 if len(segments) > 0: # list[np.array(1000, 2)] * num_samples # (N, 1000, 2) segments = np.stack(resample_segments(segments, n=segment_resamples), axis=0) else: segments = np.zeros((0, segment_resamples, 2), dtype=np.float32) label["instances"] = Instances(bboxes, segments, keypoints, bbox_format=bbox_format, normalized=normalized) return label @staticmethod def collate_fn(batch): """Collates data samples into batches.""" new_batch = {} keys = batch[0].keys() values = list(zip(*[list(b.values()) for b in batch])) for i, k in enumerate(keys): value = values[i] if k == "img": value = torch.stack(value, 0) if k in {"masks", "keypoints", "bboxes", "cls", "segments", "obb"}: value = torch.cat(value, 0) new_batch[k] = value new_batch["batch_idx"] = list(new_batch["batch_idx"]) for i in range(len(new_batch["batch_idx"])): new_batch["batch_idx"][i] += i # add target image index for build_targets() new_batch["batch_idx"] = torch.cat(new_batch["batch_idx"], 0) return new_batch class YOLOMultiModalDataset(YOLODataset): """ Dataset class for loading object detection and/or segmentation labels in YOLO format. Args: data (dict, optional): A dataset YAML dictionary. Defaults to None. task (str): An explicit arg to point current task, Defaults to 'detect'. Returns: (torch.utils.data.Dataset): A PyTorch dataset object that can be used for training an object detection model. """ def __init__(self, *args, data=None, task="detect", **kwargs): """Initializes a dataset object for object detection tasks with optional specifications.""" super().__init__(*args, data=data, task=task, **kwargs) def update_labels_info(self, label): """Add texts information for multi-modal model training.""" labels = super().update_labels_info(label) # NOTE: some categories are concatenated with its synonyms by `/`. labels["texts"] = [v.split("/") for _, v in self.data["names"].items()] return labels def build_transforms(self, hyp=None): """Enhances data transformations with optional text augmentation for multi-modal training.""" transforms = super().build_transforms(hyp) if self.augment: # NOTE: hard-coded the args for now. transforms.insert(-1, RandomLoadText(max_samples=min(self.data["nc"], 80), padding=True)) return transforms class GroundingDataset(YOLODataset): """Handles object detection tasks by loading annotations from a specified JSON file, supporting YOLO format.""" def __init__(self, *args, task="detect", json_file, **kwargs): """Initializes a GroundingDataset for object detection, loading annotations from a specified JSON file.""" assert task == "detect", "`GroundingDataset` only support `detect` task for now!" self.json_file = json_file super().__init__(*args, task=task, data={}, **kwargs) def get_img_files(self, img_path): """The image files would be read in `get_labels` function, return empty list here.""" return [] def get_labels(self): """Loads annotations from a JSON file, filters, and normalizes bounding boxes for each image.""" labels = [] LOGGER.info("Loading annotation file...") with open(self.json_file) as f: annotations = json.load(f) images = {f'{x["id"]:d}': x for x in annotations["images"]} img_to_anns = defaultdict(list) for ann in annotations["annotations"]: img_to_anns[ann["image_id"]].append(ann) for img_id, anns in TQDM(img_to_anns.items(), desc=f"Reading annotations {self.json_file}"): img = images[f"{img_id:d}"] h, w, f = img["height"], img["width"], img["file_name"] im_file = Path(self.img_path) / f if not im_file.exists(): continue self.im_files.append(str(im_file)) bboxes = [] cat2id = {} texts = [] for ann in anns: if ann["iscrowd"]: continue box = np.array(ann["bbox"], dtype=np.float32) box[:2] += box[2:] / 2 box[[0, 2]] /= float(w) box[[1, 3]] /= float(h) if box[2] <= 0 or box[3] <= 0: continue cat_name = " ".join([img["caption"][t[0] : t[1]] for t in ann["tokens_positive"]]) if cat_name not in cat2id: cat2id[cat_name] = len(cat2id) texts.append([cat_name]) cls = cat2id[cat_name] # class box = [cls] + box.tolist() if box not in bboxes: bboxes.append(box) lb = np.array(bboxes, dtype=np.float32) if len(bboxes) else np.zeros((0, 5), dtype=np.float32) labels.append( { "im_file": im_file, "shape": (h, w), "cls": lb[:, 0:1], # n, 1 "bboxes": lb[:, 1:], # n, 4 "normalized": True, "bbox_format": "xywh", "texts": texts, } ) return labels def build_transforms(self, hyp=None): """Configures augmentations for training with optional text loading; `hyp` adjusts augmentation intensity.""" transforms = super().build_transforms(hyp) if self.augment: # NOTE: hard-coded the args for now. transforms.insert(-1, RandomLoadText(max_samples=80, padding=True)) return transforms class YOLOConcatDataset(ConcatDataset): """ Dataset as a concatenation of multiple datasets. This class is useful to assemble different existing datasets. """ @staticmethod def collate_fn(batch): """Collates data samples into batches.""" return YOLODataset.collate_fn(batch) # TODO: support semantic segmentation class SemanticDataset(BaseDataset): """ Semantic Segmentation Dataset. This class is responsible for handling datasets used for semantic segmentation tasks. It inherits functionalities from the BaseDataset class. Note: This class is currently a placeholder and needs to be populated with methods and attributes for supporting semantic segmentation tasks. """ def __init__(self): """Initialize a SemanticDataset object.""" super().__init__() class ClassificationDataset: """ Extends torchvision ImageFolder to support YOLO classification tasks, offering functionalities like image augmentation, caching, and verification. It's designed to efficiently handle large datasets for training deep learning models, with optional image transformations and caching mechanisms to speed up training. This class allows for augmentations using both torchvision and Albumentations libraries, and supports caching images in RAM or on disk to reduce IO overhead during training. Additionally, it implements a robust verification process to ensure data integrity and consistency. Attributes: cache_ram (bool): Indicates if caching in RAM is enabled. cache_disk (bool): Indicates if caching on disk is enabled. samples (list): A list of tuples, each containing the path to an image, its class index, path to its .npy cache file (if caching on disk), and optionally the loaded image array (if caching in RAM). torch_transforms (callable): PyTorch transforms to be applied to the images. """ def __init__(self, root, args, augment=False, prefix=""): """ Initialize YOLO object with root, image size, augmentations, and cache settings. Args: root (str): Path to the dataset directory where images are stored in a class-specific folder structure. args (Namespace): Configuration containing dataset-related settings such as image size, augmentation parameters, and cache settings. It includes attributes like `imgsz` (image size), `fraction` (fraction of data to use), `scale`, `fliplr`, `flipud`, `cache` (disk or RAM caching for faster training), `auto_augment`, `hsv_h`, `hsv_s`, `hsv_v`, and `crop_fraction`. augment (bool, optional): Whether to apply augmentations to the dataset. Default is False. prefix (str, optional): Prefix for logging and cache filenames, aiding in dataset identification and debugging. Default is an empty string. """ import torchvision # scope for faster 'import ultralytics' # Base class assigned as attribute rather than used as base class to allow for scoping slow torchvision import if TORCHVISION_0_18: # 'allow_empty' argument first introduced in torchvision 0.18 self.base = torchvision.datasets.ImageFolder(root=root, allow_empty=True) else: self.base = torchvision.datasets.ImageFolder(root=root) self.samples = self.base.samples self.root = self.base.root # Initialize attributes if augment and args.fraction < 1.0: # reduce training fraction self.samples = self.samples[: round(len(self.samples) * args.fraction)] self.prefix = colorstr(f"{prefix}: ") if prefix else "" self.cache_ram = args.cache is True or str(args.cache).lower() == "ram" # cache images into RAM if self.cache_ram: LOGGER.warning( "WARNING ⚠️ Classification `cache_ram` training has known memory leak in " "https://github.com/ultralytics/ultralytics/issues/9824, setting `cache_ram=False`." ) self.cache_ram = False self.cache_disk = str(args.cache).lower() == "disk" # cache images on hard drive as uncompressed *.npy files self.samples = self.verify_images() # filter out bad images self.samples = [list(x) + [Path(x[0]).with_suffix(".npy"), None] for x in self.samples] # file, index, npy, im scale = (1.0 - args.scale, 1.0) # (0.08, 1.0) self.torch_transforms = ( classify_augmentations( size=args.imgsz, scale=scale, hflip=args.fliplr, vflip=args.flipud, erasing=args.erasing, auto_augment=args.auto_augment, hsv_h=args.hsv_h, hsv_s=args.hsv_s, hsv_v=args.hsv_v, ) if augment else classify_transforms(size=args.imgsz, crop_fraction=args.crop_fraction) ) def __getitem__(self, i): """Returns subset of data and targets corresponding to given indices.""" f, j, fn, im = self.samples[i] # filename, index, filename.with_suffix('.npy'), image if self.cache_ram: if im is None: # Warning: two separate if statements required here, do not combine this with previous line im = self.samples[i][3] = cv2.imread(f) elif self.cache_disk: if not fn.exists(): # load npy np.save(fn.as_posix(), cv2.imread(f), allow_pickle=False) im = np.load(fn) else: # read image im = cv2.imread(f) # BGR # Convert NumPy array to PIL image im = Image.fromarray(cv2.cvtColor(im, cv2.COLOR_BGR2RGB)) sample = self.torch_transforms(im) return {"img": sample, "cls": j} def __len__(self) -> int: """Return the total number of samples in the dataset.""" return len(self.samples) def verify_images(self): """Verify all images in dataset.""" desc = f"{self.prefix}Scanning {self.root}..." path = Path(self.root).with_suffix(".cache") # *.cache file path try: cache = load_dataset_cache_file(path) # attempt to load a *.cache file assert cache["version"] == DATASET_CACHE_VERSION # matches current version assert cache["hash"] == get_hash([x[0] for x in self.samples]) # identical hash nf, nc, n, samples = cache.pop("results") # found, missing, empty, corrupt, total if LOCAL_RANK in {-1, 0}: d = f"{desc} {nf} images, {nc} corrupt" TQDM(None, desc=d, total=n, initial=n) if cache["msgs"]: LOGGER.info("\n".join(cache["msgs"])) # display warnings return samples except (FileNotFoundError, AssertionError, AttributeError): # Run scan if *.cache retrieval failed nf, nc, msgs, samples, x = 0, 0, [], [], {} with ThreadPool(NUM_THREADS) as pool: results = pool.imap(func=verify_image, iterable=zip(self.samples, repeat(self.prefix))) pbar = TQDM(results, desc=desc, total=len(self.samples)) for sample, nf_f, nc_f, msg in pbar: if nf_f: samples.append(sample) if msg: msgs.append(msg) nf += nf_f nc += nc_f pbar.desc = f"{desc} {nf} images, {nc} corrupt" pbar.close() if msgs: LOGGER.info("\n".join(msgs)) x["hash"] = get_hash([x[0] for x in self.samples]) x["results"] = nf, nc, len(samples), samples x["msgs"] = msgs # warnings save_dataset_cache_file(self.prefix, path, x, DATASET_CACHE_VERSION) return samples(这是dataset.py代码)
09-25
# Ultralytics YOLO 🚀, AGPL-3.0 license import math import random from copy import deepcopy import cv2 import numpy as np import torch import torchvision.transforms as T from ultralytics.utils import LOGGER, colorstr from ultralytics.utils.checks import check_version from ultralytics.utils.instance import Instances from ultralytics.utils.metrics import bbox_ioa from ultralytics.utils.ops import segment2box, xyxyxyxy2xywhr from ultralytics.utils.torch_utils import TORCHVISION_0_10, TORCHVISION_0_11, TORCHVISION_0_13 from .utils import polygons2masks, polygons2masks_overlap DEFAULT_MEAN = (0.0, 0.0, 0.0) DEFAULT_STD = (1.0, 1.0, 1.0) DEFAULT_CROP_FTACTION = 1.0 # TODO: we might need a BaseTransform to make all these augments be compatible with both classification and semantic class BaseTransform: """ Base class for image transformations. This is a generic transformation class that can be extended for specific image processing needs. The class is designed to be compatible with both classification and semantic segmentation tasks. Methods: __init__: Initializes the BaseTransform object. apply_image: Applies image transformation to labels. apply_instances: Applies transformations to object instances in labels. apply_semantic: Applies semantic segmentation to an image. __call__: Applies all label transformations to an image, instances, and semantic masks. """ def __init__(self) -> None: """Initializes the BaseTransform object.""" pass def apply_image(self, labels): """Applies image transformations to labels.""" pass def apply_instances(self, labels): """Applies transformations to object instances in labels.""" pass def apply_semantic(self, labels): """Applies semantic segmentation to an image.""" pass def __call__(self, labels): """Applies all label transformations to an image, instances, and semantic masks.""" self.apply_image(labels) self.apply_instances(labels) self.apply_semantic(labels) class Compose: """Class for composing multiple image transformations.""" def __init__(self, transforms): """Initializes the Compose object with a list of transforms.""" self.transforms = transforms def __call__(self, data): """Applies a series of transformations to input data.""" for t in self.transforms: data = t(data) return data def append(self, transform): """Appends a new transform to the existing list of transforms.""" self.transforms.append(transform) def tolist(self): """Converts the list of transforms to a standard Python list.""" return self.transforms def __repr__(self): """Returns a string representation of the object.""" return f"{self.__class__.__name__}({', '.join([f'{t}' for t in self.transforms])})" class BaseMixTransform: """ Class for base mix (MixUp/Mosaic) transformations. This implementation is from mmyolo. """ def __init__(self, dataset, pre_transform=None, p=0.0) -> None: """Initializes the BaseMixTransform object with dataset, pre_transform, and probability.""" self.dataset = dataset self.pre_transform = pre_transform self.p = p def __call__(self, labels): """Applies pre-processing transforms and mixup/mosaic transforms to labels data.""" if random.uniform(0, 1) > self.p: return labels # Get index of one or three other images indexes = self.get_indexes() if isinstance(indexes, int): indexes = [indexes] # Get images information will be used for Mosaic or MixUp mix_labels = [self.dataset.get_image_and_label(i) for i in indexes] if self.pre_transform is not None: for i, data in enumerate(mix_labels): mix_labels[i] = self.pre_transform(data) labels["mix_labels"] = mix_labels # Mosaic or MixUp labels = self._mix_transform(labels) labels.pop("mix_labels", None) return labels def _mix_transform(self, labels): """Applies MixUp or Mosaic augmentation to the label dictionary.""" raise NotImplementedError def get_indexes(self): """Gets a list of shuffled indexes for mosaic augmentation.""" raise NotImplementedError class Mosaic(BaseMixTransform): """ Mosaic augmentation. This class performs mosaic augmentation by combining multiple (4 or 9) images into a single mosaic image. The augmentation is applied to a dataset with a given probability. Attributes: dataset: The dataset on which the mosaic augmentation is applied. imgsz (int, optional): Image size (height and width) after mosaic pipeline of a single image. Default to 640. p (float, optional): Probability of applying the mosaic augmentation. Must be in the range 0-1. Default to 1.0. n (int, optional): The grid size, either 4 (for 2x2) or 9 (for 3x3). """ def __init__(self, dataset, imgsz=640, p=1.0, n=4): """Initializes the object with a dataset, image size, probability, and border.""" assert 0 <= p <= 1.0, f"The probability should be in range [0, 1], but got {p}." assert n in (4, 9), "grid must be equal to 4 or 9." super().__init__(dataset=dataset, p=p) self.dataset = dataset self.imgsz = imgsz self.border = (-imgsz // 2, -imgsz // 2) # width, height self.n = n def get_indexes(self, buffer=True): """Return a list of random indexes from the dataset.""" if buffer: # select images from buffer return random.choices(list(self.dataset.buffer), k=self.n - 1) else: # select any images return [random.randint(0, len(self.dataset) - 1) for _ in range(self.n - 1)] def _mix_transform(self, labels): """Apply mixup transformation to the input image and labels.""" assert labels.get("rect_shape", None) is None, "rect and mosaic are mutually exclusive." assert len(labels.get("mix_labels", [])), "There are no other images for mosaic augment." return ( self._mosaic3(labels) if self.n == 3 else self._mosaic4(labels) if self.n == 4 else self._mosaic9(labels) ) # This code is modified for mosaic3 method. def _mosaic3(self, labels): """Create a 1x3 image mosaic.""" mosaic_labels = [] s = self.imgsz for i in range(3): labels_patch = labels if i == 0 else labels["mix_labels"][i - 1] # Load image img = labels_patch["img"] h, w = labels_patch.pop("resized_shape") # Place img in img3 if i == 0: # center img3 = np.full((s * 3, s * 3, img.shape[2]), 114, dtype=np.uint8) # base image with 3 tiles h0, w0 = h, w c = s, s, s + w, s + h # xmin, ymin, xmax, ymax (base) coordinates elif i == 1: # right c = s + w0, s, s + w0 + w, s + h elif i == 2: # left c = s - w, s + h0 - h, s, s + h0 padw, padh = c[:2] x1, y1, x2, y2 = (max(x, 0) for x in c) # allocate coords img3[y1:y2, x1:x2] = img[y1 - padh :, x1 - padw :] # img3[ymin:ymax, xmin:xmax] # hp, wp = h, w # height, width previous for next iteration # Labels assuming imgsz*2 mosaic size labels_patch = self._update_labels(labels_patch, padw + self.border[0], padh + self.border[1]) mosaic_labels.append(labels_patch) final_labels = self._cat_labels(mosaic_labels) final_labels["img"] = img3[-self.border[0] : self.border[0], -self.border[1] : self.border[1]] return final_labels def _mosaic4(self, labels): """Create a 2x2 image mosaic.""" mosaic_labels = [] s = self.imgsz yc, xc = (int(random.uniform(-x, 2 * s + x)) for x in self.border) # mosaic center x, y for i in range(4): labels_patch = labels if i == 0 else labels["mix_labels"][i - 1] # Load image img = labels_patch["img"] h, w = labels_patch.pop("resized_shape") # Place img in img4 if i == 0: # top left img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image) x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image) elif i == 1: # top right x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h elif i == 2: # bottom left x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h) x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h) elif i == 3: # bottom right x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h) x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h) img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax] padw = x1a - x1b padh = y1a - y1b labels_patch = self._update_labels(labels_patch, padw, padh) mosaic_labels.append(labels_patch) final_labels = self._cat_labels(mosaic_labels) final_labels["img"] = img4 return final_labels def _mosaic9(self, labels): """Create a 3x3 image mosaic.""" mosaic_labels = [] s = self.imgsz hp, wp = -1, -1 # height, width previous for i in range(9): labels_patch = labels if i == 0 else labels["mix_labels"][i - 1] # Load image img = labels_patch["img"] h, w = labels_patch.pop("resized_shape") # Place img in img9 if i == 0: # center img9 = np.full((s * 3, s * 3, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles h0, w0 = h, w c = s, s, s + w, s + h # xmin, ymin, xmax, ymax (base) coordinates elif i == 1: # top c = s, s - h, s + w, s elif i == 2: # top right c = s + wp, s - h, s + wp + w, s elif i == 3: # right c = s + w0, s, s + w0 + w, s + h elif i == 4: # bottom right c = s + w0, s + hp, s + w0 + w, s + hp + h elif i == 5: # bottom c = s + w0 - w, s + h0, s + w0, s + h0 + h elif i == 6: # bottom left c = s + w0 - wp - w, s + h0, s + w0 - wp, s + h0 + h elif i == 7: # left c = s - w, s + h0 - h, s, s + h0 elif i == 8: # top left c = s - w, s + h0 - hp - h, s, s + h0 - hp padw, padh = c[:2] x1, y1, x2, y2 = (max(x, 0) for x in c) # allocate coords # Image img9[y1:y2, x1:x2] = img[y1 - padh :, x1 - padw :] # img9[ymin:ymax, xmin:xmax] hp, wp = h, w # height, width previous for next iteration # Labels assuming imgsz*2 mosaic size labels_patch = self._update_labels(labels_patch, padw + self.border[0], padh + self.border[1]) mosaic_labels.append(labels_patch) final_labels = self._cat_labels(mosaic_labels) final_labels["img"] = img9[-self.border[0] : self.border[0], -self.border[1] : self.border[1]] return final_labels @staticmethod def _update_labels(labels, padw, padh): """Update labels.""" nh, nw = labels["img"].shape[:2] labels["instances"].convert_bbox(format="xyxy") labels["instances"].denormalize(nw, nh) labels["instances"].add_padding(padw, padh) return labels def _cat_labels(self, mosaic_labels): """Return labels with mosaic border instances clipped.""" if len(mosaic_labels) == 0: return {} cls = [] instances = [] imgsz = self.imgsz * 2 # mosaic imgsz for labels in mosaic_labels: cls.append(labels["cls"]) instances.append(labels["instances"]) # Final labels final_labels = { "im_file": mosaic_labels[0]["im_file"], "ori_shape": mosaic_labels[0]["ori_shape"], "resized_shape": (imgsz, imgsz), "cls": np.concatenate(cls, 0), "instances": Instances.concatenate(instances, axis=0), "mosaic_border": self.border, } final_labels["instances"].clip(imgsz, imgsz) good = final_labels["instances"].remove_zero_area_boxes() final_labels["cls"] = final_labels["cls"][good] return final_labels class MixUp(BaseMixTransform): """Class for applying MixUp augmentation to the dataset.""" def __init__(self, dataset, pre_transform=None, p=0.0) -> None: """Initializes MixUp object with dataset, pre_transform, and probability of applying MixUp.""" super().__init__(dataset=dataset, pre_transform=pre_transform, p=p) def get_indexes(self): """Get a random index from the dataset.""" return random.randint(0, len(self.dataset) - 1) def _mix_transform(self, labels): """Applies MixUp augmentation as per https://arxiv.org/pdf/1710.09412.pdf.""" r = np.random.beta(32.0, 32.0) # mixup ratio, alpha=beta=32.0 labels2 = labels["mix_labels"][0] labels["img"] = (labels["img"] * r + labels2["img"] * (1 - r)).astype(np.uint8) labels["instances"] = Instances.concatenate([labels["instances"], labels2["instances"]], axis=0) labels["cls"] = np.concatenate([labels["cls"], labels2["cls"]], 0) return labels class RandomPerspective: """ Implements random perspective and affine transformations on images and corresponding bounding boxes, segments, and keypoints. These transformations include rotation, translation, scaling, and shearing. The class also offers the option to apply these transformations conditionally with a specified probability. Attributes: degrees (float): Degree range for random rotations. translate (float): Fraction of total width and height for random translation. scale (float): Scaling factor interval, e.g., a scale factor of 0.1 allows a resize between 90%-110%. shear (float): Shear intensity (angle in degrees). perspective (float): Perspective distortion factor. border (tuple): Tuple specifying mosaic border. pre_transform (callable): A function/transform to apply to the image before starting the random transformation. Methods: affine_transform(img, border): Applies a series of affine transformations to the image. apply_bboxes(bboxes, M): Transforms bounding boxes using the calculated affine matrix. apply_segments(segments, M): Transforms segments and generates new bounding boxes. apply_keypoints(keypoints, M): Transforms keypoints. __call__(labels): Main method to apply transformations to both images and their corresponding annotations. box_candidates(box1, box2): Filters out bounding boxes that don't meet certain criteria post-transformation. """ def __init__( self, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, border=(0, 0), pre_transform=None ): """Initializes RandomPerspective object with transformation parameters.""" self.degrees = degrees self.translate = translate self.scale = scale self.shear = shear self.perspective = perspective self.border = border # mosaic border self.pre_transform = pre_transform def affine_transform(self, img, border): """ Applies a sequence of affine transformations centered around the image center. Args: img (ndarray): Input image. border (tuple): Border dimensions. Returns: img (ndarray): Transformed image. M (ndarray): Transformation matrix. s (float): Scale factor. """ # Center C = np.eye(3, dtype=np.float32) C[0, 2] = -img.shape[1] / 2 # x translation (pixels) C[1, 2] = -img.shape[0] / 2 # y translation (pixels) # Perspective P = np.eye(3, dtype=np.float32) P[2, 0] = random.uniform(-self.perspective, self.perspective) # x perspective (about y) P[2, 1] = random.uniform(-self.perspective, self.perspective) # y perspective (about x) # Rotation and Scale R = np.eye(3, dtype=np.float32) a = random.uniform(-self.degrees, self.degrees) # a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations s = random.uniform(1 - self.scale, 1 + self.scale) # s = 2 ** random.uniform(-scale, scale) R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s) # Shear S = np.eye(3, dtype=np.float32) S[0, 1] = math.tan(random.uniform(-self.shear, self.shear) * math.pi / 180) # x shear (deg) S[1, 0] = math.tan(random.uniform(-self.shear, self.shear) * math.pi / 180) # y shear (deg) # Translation T = np.eye(3, dtype=np.float32) T[0, 2] = random.uniform(0.5 - self.translate, 0.5 + self.translate) * self.size[0] # x translation (pixels) T[1, 2] = random.uniform(0.5 - self.translate, 0.5 + self.translate) * self.size[1] # y translation (pixels) # Combined rotation matrix M = T @ S @ R @ P @ C # order of operations (right to left) is IMPORTANT # Affine image if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any(): # image changed if self.perspective: img = cv2.warpPerspective(img, M, dsize=self.size, borderValue=(114, 114, 114)) else: # affine img = cv2.warpAffine(img, M[:2], dsize=self.size, borderValue=(114, 114, 114)) return img, M, s def apply_bboxes(self, bboxes, M): """ Apply affine to bboxes only. Args: bboxes (ndarray): list of bboxes, xyxy format, with shape (num_bboxes, 4). M (ndarray): affine matrix. Returns: new_bboxes (ndarray): bboxes after affine, [num_bboxes, 4]. """ n = len(bboxes) if n == 0: return bboxes xy = np.ones((n * 4, 3), dtype=bboxes.dtype) xy[:, :2] = bboxes[:, [0, 1, 2, 3, 0, 3, 2, 1]].reshape(n * 4, 2) # x1y1, x2y2, x1y2, x2y1 xy = xy @ M.T # transform xy = (xy[:, :2] / xy[:, 2:3] if self.perspective else xy[:, :2]).reshape(n, 8) # perspective rescale or affine # Create new boxes x = xy[:, [0, 2, 4, 6]] y = xy[:, [1, 3, 5, 7]] return np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1)), dtype=bboxes.dtype).reshape(4, n).T def apply_segments(self, segments, M): """ Apply affine to segments and generate new bboxes from segments. Args: segments (ndarray): list of segments, [num_samples, 500, 2]. M (ndarray): affine matrix. Returns: new_segments (ndarray): list of segments after affine, [num_samples, 500, 2]. new_bboxes (ndarray): bboxes after affine, [N, 4]. """ n, num = segments.shape[:2] if n == 0: return [], segments xy = np.ones((n * num, 3), dtype=segments.dtype) segments = segments.reshape(-1, 2) xy[:, :2] = segments xy = xy @ M.T # transform xy = xy[:, :2] / xy[:, 2:3] segments = xy.reshape(n, -1, 2) bboxes = np.stack([segment2box(xy, self.size[0], self.size[1]) for xy in segments], 0) segments[..., 0] = segments[..., 0].clip(bboxes[:, 0:1], bboxes[:, 2:3]) segments[..., 1] = segments[..., 1].clip(bboxes[:, 1:2], bboxes[:, 3:4]) return bboxes, segments def apply_keypoints(self, keypoints, M): """ Apply affine to keypoints. Args: keypoints (ndarray): keypoints, [N, 17, 3]. M (ndarray): affine matrix. Returns: new_keypoints (ndarray): keypoints after affine, [N, 17, 3]. """ n, nkpt = keypoints.shape[:2] if n == 0: return keypoints xy = np.ones((n * nkpt, 3), dtype=keypoints.dtype) visible = keypoints[..., 2].reshape(n * nkpt, 1) xy[:, :2] = keypoints[..., :2].reshape(n * nkpt, 2) xy = xy @ M.T # transform xy = xy[:, :2] / xy[:, 2:3] # perspective rescale or affine out_mask = (xy[:, 0] < 0) | (xy[:, 1] < 0) | (xy[:, 0] > self.size[0]) | (xy[:, 1] > self.size[1]) visible[out_mask] = 0 return np.concatenate([xy, visible], axis=-1).reshape(n, nkpt, 3) def __call__(self, labels): """ Affine images and targets. Args: labels (dict): a dict of `bboxes`, `segments`, `keypoints`. """ if self.pre_transform and "mosaic_border" not in labels: labels = self.pre_transform(labels) labels.pop("ratio_pad", None) # do not need ratio pad img = labels["img"] cls = labels["cls"] instances = labels.pop("instances") # Make sure the coord formats are right instances.convert_bbox(format="xyxy") instances.denormalize(*img.shape[:2][::-1]) border = labels.pop("mosaic_border", self.border) self.size = img.shape[1] + border[1] * 2, img.shape[0] + border[0] * 2 # w, h # M is affine matrix # Scale for func:`box_candidates` img, M, scale = self.affine_transform(img, border) bboxes = self.apply_bboxes(instances.bboxes, M) segments = instances.segments keypoints = instances.keypoints # Update bboxes if there are segments. if len(segments): bboxes, segments = self.apply_segments(segments, M) if keypoints is not None: keypoints = self.apply_keypoints(keypoints, M) new_instances = Instances(bboxes, segments, keypoints, bbox_format="xyxy", normalized=False) # Clip new_instances.clip(*self.size) # Filter instances instances.scale(scale_w=scale, scale_h=scale, bbox_only=True) # Make the bboxes have the same scale with new_bboxes i = self.box_candidates( box1=instances.bboxes.T, box2=new_instances.bboxes.T, area_thr=0.01 if len(segments) else 0.10 ) labels["instances"] = new_instances[i] labels["cls"] = cls[i] labels["img"] = img labels["resized_shape"] = img.shape[:2] return labels def box_candidates(self, box1, box2, wh_thr=2, ar_thr=100, area_thr=0.1, eps=1e-16): """ Compute box candidates based on a set of thresholds. This method compares the characteristics of the boxes before and after augmentation to decide whether a box is a candidate for further processing. Args: box1 (numpy.ndarray): The 4,n bounding box before augmentation, represented as [x1, y1, x2, y2]. box2 (numpy.ndarray): The 4,n bounding box after augmentation, represented as [x1, y1, x2, y2]. wh_thr (float, optional): The width and height threshold in pixels. Default is 2. ar_thr (float, optional): The aspect ratio threshold. Default is 100. area_thr (float, optional): The area ratio threshold. Default is 0.1. eps (float, optional): A small epsilon value to prevent division by zero. Default is 1e-16. Returns: (numpy.ndarray): A boolean array indicating which boxes are candidates based on the given thresholds. """ w1, h1 = box1[2] - box1[0], box1[3] - box1[1] w2, h2 = box2[2] - box2[0], box2[3] - box2[1] ar = np.maximum(w2 / (h2 + eps), h2 / (w2 + eps)) # aspect ratio return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + eps) > area_thr) & (ar < ar_thr) # candidates class RandomHSV: """ This class is responsible for performing random adjustments to the Hue, Saturation, and Value (HSV) channels of an image. The adjustments are random but within limits set by hgain, sgain, and vgain. """ def __init__(self, hgain=0.5, sgain=0.5, vgain=0.5) -> None: """ Initialize RandomHSV class with gains for each HSV channel. Args: hgain (float, optional): Maximum variation for hue. Default is 0.5. sgain (float, optional): Maximum variation for saturation. Default is 0.5. vgain (float, optional): Maximum variation for value. Default is 0.5. """ self.hgain = hgain self.sgain = sgain self.vgain = vgain def __call__(self, labels): """ Applies random HSV augmentation to an image within the predefined limits. The modified image replaces the original image in the input 'labels' dict. """ img = labels["img"] if self.hgain or self.sgain or self.vgain: r = np.random.uniform(-1, 1, 3) * [self.hgain, self.sgain, self.vgain] + 1 # random gains hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV)) dtype = img.dtype # uint8 x = np.arange(0, 256, dtype=r.dtype) lut_hue = ((x * r[0]) % 180).astype(dtype) lut_sat = np.clip(x * r[1], 0, 255).astype(dtype) lut_val = np.clip(x * r[2], 0, 255).astype(dtype) im_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))) cv2.cvtColor(im_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed return labels class RandomFlip: """ Applies a random horizontal or vertical flip to an image with a given probability. Also updates any instances (bounding boxes, keypoints, etc.) accordingly. """ def __init__(self, p=0.5, direction="horizontal", flip_idx=None) -> None: """ Initializes the RandomFlip class with probability and direction. Args: p (float, optional): The probability of applying the flip. Must be between 0 and 1. Default is 0.5. direction (str, optional): The direction to apply the flip. Must be 'horizontal' or 'vertical'. Default is 'horizontal'. flip_idx (array-like, optional): Index mapping for flipping keypoints, if any. """ assert direction in ["horizontal", "vertical"], f"Support direction `horizontal` or `vertical`, got {direction}" assert 0 <= p <= 1.0 self.p = p self.direction = direction self.flip_idx = flip_idx def __call__(self, labels): """ Applies random flip to an image and updates any instances like bounding boxes or keypoints accordingly. Args: labels (dict): A dictionary containing the keys 'img' and 'instances'. 'img' is the image to be flipped. 'instances' is an object containing bounding boxes and optionally keypoints. Returns: (dict): The same dict with the flipped image and updated instances under the 'img' and 'instances' keys. """ img = labels["img"] instances = labels.pop("instances") instances.convert_bbox(format="xywh") h, w = img.shape[:2] h = 1 if instances.normalized else h w = 1 if instances.normalized else w # Flip up-down if self.direction == "vertical" and random.random() < self.p: img = np.flipud(img) instances.flipud(h) if self.direction == "horizontal" and random.random() < self.p: img = np.fliplr(img) instances.fliplr(w) # For keypoints if self.flip_idx is not None and instances.keypoints is not None: instances.keypoints = np.ascontiguousarray(instances.keypoints[:, self.flip_idx, :]) labels["img"] = np.ascontiguousarray(img) labels["instances"] = instances return labels class LetterBox: """Resize image and padding for detection, instance segmentation, pose.""" def __init__(self, new_shape=(640, 640), auto=False, scaleFill=False, scaleup=True, center=True, stride=32): """Initialize LetterBox object with specific parameters.""" self.new_shape = new_shape self.auto = auto self.scaleFill = scaleFill self.scaleup = scaleup self.stride = stride self.center = center # Put the image in the middle or top-left def __call__(self, labels=None, image=None): """Return updated labels and image with added border.""" if labels is None: labels = {} img = labels.get("img") if image is None else image shape = img.shape[:2] # current shape [height, width] new_shape = labels.pop("rect_shape", self.new_shape) if isinstance(new_shape, int): new_shape = (new_shape, new_shape) # Scale ratio (new / old) r = min(new_shape[0] / shape[0], new_shape[1] / shape[1]) if not self.scaleup: # only scale down, do not scale up (for better val mAP) r = min(r, 1.0) # Compute padding ratio = r, r # width, height ratios new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r)) dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding if self.auto: # minimum rectangle dw, dh = np.mod(dw, self.stride), np.mod(dh, self.stride) # wh padding elif self.scaleFill: # stretch dw, dh = 0.0, 0.0 new_unpad = (new_shape[1], new_shape[0]) ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios if self.center: dw /= 2 # divide padding into 2 sides dh /= 2 if shape[::-1] != new_unpad: # resize img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR) top, bottom = int(round(dh - 0.1)) if self.center else 0, int(round(dh + 0.1)) left, right = int(round(dw - 0.1)) if self.center else 0, int(round(dw + 0.1)) img = cv2.copyMakeBorder( img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=(114, 114, 114) ) # add border if labels.get("ratio_pad"): labels["ratio_pad"] = (labels["ratio_pad"], (left, top)) # for evaluation if len(labels): labels = self._update_labels(labels, ratio, dw, dh) labels["img"] = img labels["resized_shape"] = new_shape return labels else: return img def _update_labels(self, labels, ratio, padw, padh): """Update labels.""" labels["instances"].convert_bbox(format="xyxy") labels["instances"].denormalize(*labels["img"].shape[:2][::-1]) labels["instances"].scale(*ratio) labels["instances"].add_padding(padw, padh) return labels class CopyPaste: """ Implements the Copy-Paste augmentation as described in the paper https://arxiv.org/abs/2012.07177. This class is responsible for applying the Copy-Paste augmentation on images and their corresponding instances. """ def __init__(self, p=0.5) -> None: """ Initializes the CopyPaste class with a given probability. Args: p (float, optional): The probability of applying the Copy-Paste augmentation. Must be between 0 and 1. Default is 0.5. """ self.p = p def __call__(self, labels): """ Applies the Copy-Paste augmentation to the given image and instances. Args: labels (dict): A dictionary containing: - 'img': The image to augment. - 'cls': Class labels associated with the instances. - 'instances': Object containing bounding boxes, and optionally, keypoints and segments. Returns: (dict): Dict with augmented image and updated instances under the 'img', 'cls', and 'instances' keys. Notes: 1. Instances are expected to have 'segments' as one of their attributes for this augmentation to work. 2. This method modifies the input dictionary 'labels' in place. """ im = labels["img"] cls = labels["cls"] h, w = im.shape[:2] instances = labels.pop("instances") instances.convert_bbox(format="xyxy") instances.denormalize(w, h) if self.p and len(instances.segments): n = len(instances) _, w, _ = im.shape # height, width, channels im_new = np.zeros(im.shape, np.uint8) # Calculate ioa first then select indexes randomly ins_flip = deepcopy(instances) ins_flip.fliplr(w) ioa = bbox_ioa(ins_flip.bboxes, instances.bboxes) # intersection over area, (N, M) indexes = np.nonzero((ioa < 0.30).all(1))[0] # (N, ) n = len(indexes) for j in random.sample(list(indexes), k=round(self.p * n)): cls = np.concatenate((cls, cls[[j]]), axis=0) instances = Instances.concatenate((instances, ins_flip[[j]]), axis=0) cv2.drawContours(im_new, instances.segments[[j]].astype(np.int32), -1, (1, 1, 1), cv2.FILLED) result = cv2.flip(im, 1) # augment segments (flip left-right) i = cv2.flip(im_new, 1).astype(bool) im[i] = result[i] labels["img"] = im labels["cls"] = cls labels["instances"] = instances return labels class Albumentations: """ Albumentations transformations. Optional, uninstall package to disable. Applies Blur, Median Blur, convert to grayscale, Contrast Limited Adaptive Histogram Equalization, random change of brightness and contrast, RandomGamma and lowering of image quality by compression. """ def __init__(self, p=1.0): """Initialize the transform object for YOLO bbox formatted params.""" self.p = p self.transform = None prefix = colorstr("albumentations: ") try: import albumentations as A check_version(A.__version__, "1.0.3", hard=True) # version requirement # Transforms T = [ A.Blur(p=0.01), A.MedianBlur(p=0.01), A.ToGray(p=0.01), A.CLAHE(p=0.01), A.RandomBrightnessContrast(p=0.0), A.RandomGamma(p=0.0), A.ImageCompression(quality_lower=75, p=0.0), ] self.transform = A.Compose(T, bbox_params=A.BboxParams(format="yolo", label_fields=["class_labels"])) LOGGER.info(prefix + ", ".join(f"{x}".replace("always_apply=False, ", "") for x in T if x.p)) except ImportError: # package not installed, skip pass except Exception as e: LOGGER.info(f"{prefix}{e}") def __call__(self, labels): """Generates object detections and returns a dictionary with detection results.""" im = labels["img"] cls = labels["cls"] if len(cls): labels["instances"].convert_bbox("xywh") labels["instances"].normalize(*im.shape[:2][::-1]) bboxes = labels["instances"].bboxes # TODO: add supports of segments and keypoints if self.transform and random.random() < self.p: new = self.transform(image=im, bboxes=bboxes, class_labels=cls) # transformed if len(new["class_labels"]) > 0: # skip update if no bbox in new im labels["img"] = new["image"] labels["cls"] = np.array(new["class_labels"]) bboxes = np.array(new["bboxes"], dtype=np.float32) labels["instances"].update(bboxes=bboxes) return labels # TODO: technically this is not an augmentation, maybe we should put this to another files class Format: """ Formats image annotations for object detection, instance segmentation, and pose estimation tasks. The class standardizes the image and instance annotations to be used by the `collate_fn` in PyTorch DataLoader. Attributes: bbox_format (str): Format for bounding boxes. Default is 'xywh'. normalize (bool): Whether to normalize bounding boxes. Default is True. return_mask (bool): Return instance masks for segmentation. Default is False. return_keypoint (bool): Return keypoints for pose estimation. Default is False. mask_ratio (int): Downsample ratio for masks. Default is 4. mask_overlap (bool): Whether to overlap masks. Default is True. batch_idx (bool): Keep batch indexes. Default is True. bgr (float): The probability to return BGR images. Default is 0.0. """ def __init__( self, bbox_format="xywh", normalize=True, return_mask=False, return_keypoint=False, return_obb=False, mask_ratio=4, mask_overlap=True, batch_idx=True, bgr=0.0, ): """Initializes the Format class with given parameters.""" self.bbox_format = bbox_format self.normalize = normalize self.return_mask = return_mask # set False when training detection only self.return_keypoint = return_keypoint self.return_obb = return_obb self.mask_ratio = mask_ratio self.mask_overlap = mask_overlap self.batch_idx = batch_idx # keep the batch indexes self.bgr = bgr def __call__(self, labels): """Return formatted image, classes, bounding boxes & keypoints to be used by 'collate_fn'.""" img = labels.pop("img") h, w = img.shape[:2] cls = labels.pop("cls") instances = labels.pop("instances") instances.convert_bbox(format=self.bbox_format) instances.denormalize(w, h) nl = len(instances) if self.return_mask: if nl: masks, instances, cls = self._format_segments(instances, cls, w, h) masks = torch.from_numpy(masks) else: masks = torch.zeros( 1 if self.mask_overlap else nl, img.shape[0] // self.mask_ratio, img.shape[1] // self.mask_ratio ) labels["masks"] = masks if self.normalize: instances.normalize(w, h) labels["img"] = self._format_img(img) labels["cls"] = torch.from_numpy(cls) if nl else torch.zeros(nl) labels["bboxes"] = torch.from_numpy(instances.bboxes) if nl else torch.zeros((nl, 4)) if self.return_keypoint: labels["keypoints"] = torch.from_numpy(instances.keypoints) if self.return_obb: labels["bboxes"] = ( xyxyxyxy2xywhr(torch.from_numpy(instances.segments)) if len(instances.segments) else torch.zeros((0, 5)) ) # Then we can use collate_fn if self.batch_idx: labels["batch_idx"] = torch.zeros(nl) return labels def _format_img(self, img): """Format the image for YOLO from Numpy array to PyTorch tensor.""" if len(img.shape) < 3: img = np.expand_dims(img, -1) img = img.transpose(2, 0, 1) img = np.ascontiguousarray(img[::-1] if random.uniform(0, 1) > self.bgr else img) img = torch.from_numpy(img) return img def _format_segments(self, instances, cls, w, h): """Convert polygon points to bitmap.""" segments = instances.segments if self.mask_overlap: masks, sorted_idx = polygons2masks_overlap((h, w), segments, downsample_ratio=self.mask_ratio) masks = masks[None] # (640, 640) -> (1, 640, 640) instances = instances[sorted_idx] cls = cls[sorted_idx] else: masks = polygons2masks((h, w), segments, color=1, downsample_ratio=self.mask_ratio) return masks, instances, cls def v8_transforms(dataset, imgsz, hyp, stretch=False): """Convert images to a size suitable for YOLOv8 training.""" pre_transform = Compose( [ Mosaic(dataset, imgsz=imgsz, p=hyp.mosaic), CopyPaste(p=hyp.copy_paste), RandomPerspective( degrees=hyp.degrees, translate=hyp.translate, scale=hyp.scale, shear=hyp.shear, perspective=hyp.perspective, pre_transform=None if stretch else LetterBox(new_shape=(imgsz, imgsz)), ), ] ) flip_idx = dataset.data.get("flip_idx", []) # for keypoints augmentation if dataset.use_keypoints: kpt_shape = dataset.data.get("kpt_shape", None) if len(flip_idx) == 0 and hyp.fliplr > 0.0: hyp.fliplr = 0.0 LOGGER.warning("WARNING ⚠️ No 'flip_idx' array defined in data.yaml, setting augmentation 'fliplr=0.0'") elif flip_idx and (len(flip_idx) != kpt_shape[0]): raise ValueError(f"data.yaml flip_idx={flip_idx} length must be equal to kpt_shape[0]={kpt_shape[0]}") return Compose( [ pre_transform, MixUp(dataset, pre_transform=pre_transform, p=hyp.mixup), Albumentations(p=1.0), RandomHSV(hgain=hyp.hsv_h, sgain=hyp.hsv_s, vgain=hyp.hsv_v), RandomFlip(direction="vertical", p=hyp.flipud), RandomFlip(direction="horizontal", p=hyp.fliplr, flip_idx=flip_idx), ] ) # transforms # Classification augmentations ----------------------------------------------------------------------------------------- def classify_transforms( size=224, mean=DEFAULT_MEAN, std=DEFAULT_STD, interpolation: T.InterpolationMode = T.InterpolationMode.BILINEAR, crop_fraction: float = DEFAULT_CROP_FTACTION, ): """ Classification transforms for evaluation/inference. Inspired by timm/data/transforms_factory.py. Args: size (int): image size mean (tuple): mean values of RGB channels std (tuple): std values of RGB channels interpolation (T.InterpolationMode): interpolation mode. default is T.InterpolationMode.BILINEAR. crop_fraction (float): fraction of image to crop. default is 1.0. Returns: (T.Compose): torchvision transforms """ if isinstance(size, (tuple, list)): assert len(size) == 2 scale_size = tuple(math.floor(x / crop_fraction) for x in size) else: scale_size = math.floor(size / crop_fraction) scale_size = (scale_size, scale_size) # aspect ratio is preserved, crops center within image, no borders are added, image is lost if scale_size[0] == scale_size[1]: # simple case, use torchvision built-in Resize w/ shortest edge mode (scalar size arg) tfl = [T.Resize(scale_size[0], interpolation=interpolation)] else: # resize shortest edge to matching target dim for non-square target tfl = [T.Resize(scale_size)] tfl += [T.CenterCrop(size)] tfl += [ T.ToTensor(), T.Normalize( mean=torch.tensor(mean), std=torch.tensor(std), ), ] return T.Compose(tfl) # Classification augmentations train --------------------------------------------------------------------------------------- def classify_augmentations( size=224, mean=DEFAULT_MEAN, std=DEFAULT_STD, scale=None, ratio=None, hflip=0.5, vflip=0.0, auto_augment=None, hsv_h=0.015, # image HSV-Hue augmentation (fraction) hsv_s=0.4, # image HSV-Saturation augmentation (fraction) hsv_v=0.4, # image HSV-Value augmentation (fraction) force_color_jitter=False, erasing=0.0, interpolation: T.InterpolationMode = T.InterpolationMode.BILINEAR, ): """ Classification transforms with augmentation for training. Inspired by timm/data/transforms_factory.py. Args: size (int): image size scale (tuple): scale range of the image. default is (0.08, 1.0) ratio (tuple): aspect ratio range of the image. default is (3./4., 4./3.) mean (tuple): mean values of RGB channels std (tuple): std values of RGB channels hflip (float): probability of horizontal flip vflip (float): probability of vertical flip auto_augment (str): auto augmentation policy. can be 'randaugment', 'augmix', 'autoaugment' or None. hsv_h (float): image HSV-Hue augmentation (fraction) hsv_s (float): image HSV-Saturation augmentation (fraction) hsv_v (float): image HSV-Value augmentation (fraction) force_color_jitter (bool): force to apply color jitter even if auto augment is enabled erasing (float): probability of random erasing interpolation (T.InterpolationMode): interpolation mode. default is T.InterpolationMode.BILINEAR. Returns: (T.Compose): torchvision transforms """ # Transforms to apply if albumentations not installed if not isinstance(size, int): raise TypeError(f"classify_transforms() size {size} must be integer, not (list, tuple)") scale = tuple(scale or (0.08, 1.0)) # default imagenet scale range ratio = tuple(ratio or (3.0 / 4.0, 4.0 / 3.0)) # default imagenet ratio range primary_tfl = [T.RandomResizedCrop(size, scale=scale, ratio=ratio, interpolation=interpolation)] if hflip > 0.0: primary_tfl += [T.RandomHorizontalFlip(p=hflip)] if vflip > 0.0: primary_tfl += [T.RandomVerticalFlip(p=vflip)] secondary_tfl = [] disable_color_jitter = False if auto_augment: assert isinstance(auto_augment, str) # color jitter is typically disabled if AA/RA on, # this allows override without breaking old hparm cfgs disable_color_jitter = not force_color_jitter if auto_augment == "randaugment": if TORCHVISION_0_11: secondary_tfl += [T.RandAugment(interpolation=interpolation)] else: LOGGER.warning('"auto_augment=randaugment" requires torchvision >= 0.11.0. Disabling it.') elif auto_augment == "augmix": if TORCHVISION_0_13: secondary_tfl += [T.AugMix(interpolation=interpolation)] else: LOGGER.warning('"auto_augment=augmix" requires torchvision >= 0.13.0. Disabling it.') elif auto_augment == "autoaugment": if TORCHVISION_0_10: secondary_tfl += [T.AutoAugment(interpolation=interpolation)] else: LOGGER.warning('"auto_augment=autoaugment" requires torchvision >= 0.10.0. Disabling it.') else: raise ValueError( f'Invalid auto_augment policy: {auto_augment}. Should be one of "randaugment", ' f'"augmix", "autoaugment" or None' ) if not disable_color_jitter: secondary_tfl += [T.ColorJitter(brightness=hsv_v, contrast=hsv_v, saturation=hsv_s, hue=hsv_h)] final_tfl = [ T.ToTensor(), T.Normalize(mean=torch.tensor(mean), std=torch.tensor(std)), T.RandomErasing(p=erasing, inplace=True), ] return T.Compose(primary_tfl + secondary_tfl + final_tfl) # NOTE: keep this class for backward compatibility class ClassifyLetterBox: """ YOLOv8 LetterBox class for image preprocessing, designed to be part of a transformation pipeline, e.g., T.Compose([LetterBox(size), ToTensor()]). Attributes: h (int): Target height of the image. w (int): Target width of the image. auto (bool): If True, automatically solves for short side using stride. stride (int): The stride value, used when 'auto' is True. """ def __init__(self, size=(640, 640), auto=False, stride=32): """ Initializes the ClassifyLetterBox class with a target size, auto-flag, and stride. Args: size (Union[int, Tuple[int, int]]): The target dimensions (height, width) for the letterbox. auto (bool): If True, automatically calculates the short side based on stride. stride (int): The stride value, used when 'auto' is True. """ super().__init__() self.h, self.w = (size, size) if isinstance(size, int) else size self.auto = auto # pass max size integer, automatically solve for short side using stride self.stride = stride # used with auto def __call__(self, im): """ Resizes the image and pads it with a letterbox method. Args: im (numpy.ndarray): The input image as a numpy array of shape HWC. Returns: (numpy.ndarray): The letterboxed and resized image as a numpy array. """ imh, imw = im.shape[:2] r = min(self.h / imh, self.w / imw) # ratio of new/old dimensions h, w = round(imh * r), round(imw * r) # resized image dimensions # Calculate padding dimensions hs, ws = (math.ceil(x / self.stride) * self.stride for x in (h, w)) if self.auto else (self.h, self.w) top, left = round((hs - h) / 2 - 0.1), round((ws - w) / 2 - 0.1) # Create padded image im_out = np.full((hs, ws, 3), 114, dtype=im.dtype) im_out[top : top + h, left : left + w] = cv2.resize(im, (w, h), interpolation=cv2.INTER_LINEAR) return im_out # NOTE: keep this class for backward compatibility class CenterCrop: """YOLOv8 CenterCrop class for image preprocessing, designed to be part of a transformation pipeline, e.g., T.Compose([CenterCrop(size), ToTensor()]). """ def __init__(self, size=640): """Converts an image from numpy array to PyTorch tensor.""" super().__init__() self.h, self.w = (size, size) if isinstance(size, int) else size def __call__(self, im): """ Resizes and crops the center of the image using a letterbox method. Args: im (numpy.ndarray): The input image as a numpy array of shape HWC. Returns: (numpy.ndarray): The center-cropped and resized image as a numpy array. """ imh, imw = im.shape[:2] m = min(imh, imw) # min dimension top, left = (imh - m) // 2, (imw - m) // 2 return cv2.resize(im[top : top + m, left : left + m], (self.w, self.h), interpolation=cv2.INTER_LINEAR) # NOTE: keep this class for backward compatibility class ToTensor: """YOLOv8 ToTensor class for image preprocessing, i.e., T.Compose([LetterBox(size), ToTensor()]).""" def __init__(self, half=False): """Initialize YOLOv8 ToTensor object with optional half-precision support.""" super().__init__() self.half = half def __call__(self, im): """ Transforms an image from a numpy array to a PyTorch tensor, applying optional half-precision and normalization. Args: im (numpy.ndarray): Input image as a numpy array with shape (H, W, C) in BGR order. Returns: (torch.Tensor): The transformed image as a PyTorch tensor in float32 or float16, normalized to [0, 1]. """ im = np.ascontiguousarray(im.transpose((2, 0, 1))[::-1]) # HWC to CHW -> BGR to RGB -> contiguous im = torch.from_numpy(im) # to torch im = im.half() if self.half else im.float() # uint8 to fp16/32 im /= 255.0 # 0-255 to 0.0-1.0 return im 在第几行
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值