2024-06-09 04:29:29 +08:00
|
|
|
# Ultralytics YOLOv5 🚀, AGPL-3.0 license
|
2024-01-08 08:29:14 +08:00
|
|
|
"""Dataloaders and dataset utils."""
|
2020-11-14 18:50:32 +08:00
|
|
|
|
2022-07-25 22:52:28 +08:00
|
|
|
import contextlib
|
2020-05-30 08:04:54 +08:00
|
|
|
import glob
|
2021-05-26 20:26:52 +08:00
|
|
|
import hashlib
|
2021-06-12 19:26:41 +08:00
|
|
|
import json
|
2022-01-05 11:32:42 +08:00
|
|
|
import math
|
2020-05-30 08:04:54 +08:00
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import shutil
|
|
|
|
import time
|
2020-11-07 09:18:18 +08:00
|
|
|
from itertools import repeat
|
2021-11-05 00:24:25 +08:00
|
|
|
from multiprocessing.pool import Pool, ThreadPool
|
2020-05-30 08:04:54 +08:00
|
|
|
from pathlib import Path
|
|
|
|
from threading import Thread
|
2022-03-20 21:18:05 +08:00
|
|
|
from urllib.parse import urlparse
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
import numpy as np
|
2022-11-04 22:30:12 +08:00
|
|
|
import psutil
|
2020-05-30 08:04:54 +08:00
|
|
|
import torch
|
2021-01-05 11:54:09 +08:00
|
|
|
import torch.nn.functional as F
|
New YOLOv5 Classification Models (#8956)
* Update
* Logger step fix: Increment step with epochs (#8654)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Allow logging models from GenericLogger (#8676)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* support final model logging
* update
* update
* update
* update
* remove curses
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update __init__.py
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update dataset download
* Update dataset download
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Pass imgsz to classify_transforms()
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Cos scheduler
* Cos scheduler
* Remove unused args
* Update
* Add seed
* Add seed
* Update
* Update
* Add run(), main()
* Merge master
* Merge master
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Create YOLOv5 BaseModel class (#8829)
* Create BaseModel
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Hub load device fix
* Update
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add experiment
* Merge master
* Attach names
* weight decay = 1e-4
* weight decay = 5e-5
* update smart_optimizer console printout
* fashion-mnist fix
* Merge master
* Update Table
* Update Table
* Remove destroy process group
* add kwargs to forward()
* fuse fix for resnet50
* nc, names fix for resnet50
* nc, names fix for resnet50
* ONNX CPU inference fix
* revert
* cuda
* if augment or visualize
* if augment or visualize
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* New smart_inference_mode()
* Update README
* Refactor into /classify dir
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* reset defaults
* reset defaults
* fix gpu predict
* warmup
* ema half fix
* spacing
* remove data
* remove cache
* remove denormalize
* save run settings
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* verbose false on initial plots
* new save_yaml() function
* Update ci-testing.yml
* Path(data) CI fix
* Separate classification CI
* fix val
* fix val
* fix val
* smartCrossEntropyLoss
* skip validation on hub load
* autodownload with working dir root
* str(data)
* Dataset usage example
* im_show normalize
* im_show normalize
* add imagenet simple names to multibackend
* Add validation speeds
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* 24-space names
* Update bash scripts
* Update permissions
* Add bash script arguments
* remove verbose
* TRT data fix
* names generator fix
* optimize if names
* update usage
* Add local loading
* Verbose=False
* update names printing
* Add Usage examples
* Add Usage examples
* Add Usage examples
* Add Usage examples
* named_children
* reshape_classifier_outputs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* update
* fix CI
* fix incorrect class substitution
* fix incorrect class substitution
* remove denormalize
* ravel fix
* cleanup
* update opt file printing
* update opt file printing
* update defaults
* add opt to checkpoint
* Add warning
* Add comment
* plot half bug fix
* Use NotImplementedError
* fix export shape report
* Fix TRT load
* cleanup CI
* profile comment
* CI fix
* Add cls models
* avoid inplace error
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix usage examples
* Update README
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
Co-authored-by: Ayush Chaurasia <ayush.chaurarsia@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-08-17 17:59:01 +08:00
|
|
|
import torchvision
|
2021-06-09 05:09:45 +08:00
|
|
|
import yaml
|
2021-11-05 00:24:25 +08:00
|
|
|
from PIL import ExifTags, Image, ImageOps
|
2021-11-13 20:07:32 +08:00
|
|
|
from torch.utils.data import DataLoader, Dataset, dataloader, distributed
|
2022-04-27 06:00:01 +08:00
|
|
|
from tqdm import tqdm
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
from utils.augmentations import (
|
|
|
|
Albumentations,
|
|
|
|
augment_hsv,
|
|
|
|
classify_albumentations,
|
|
|
|
classify_transforms,
|
|
|
|
copy_paste,
|
|
|
|
letterbox,
|
|
|
|
mixup,
|
|
|
|
random_perspective,
|
|
|
|
)
|
|
|
|
from utils.general import (
|
|
|
|
DATASETS_DIR,
|
|
|
|
LOGGER,
|
|
|
|
NUM_THREADS,
|
|
|
|
TQDM_BAR_FORMAT,
|
|
|
|
check_dataset,
|
|
|
|
check_requirements,
|
|
|
|
check_yaml,
|
|
|
|
clean_str,
|
|
|
|
cv2,
|
|
|
|
is_colab,
|
|
|
|
is_kaggle,
|
|
|
|
segments2boxes,
|
|
|
|
unzip_file,
|
|
|
|
xyn2xy,
|
|
|
|
xywh2xyxy,
|
|
|
|
xywhn2xyxy,
|
|
|
|
xyxy2xywhn,
|
|
|
|
)
|
2022-01-28 15:18:01 +08:00
|
|
|
from utils.torch_utils import torch_distributed_zero_first
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2020-11-14 18:50:32 +08:00
|
|
|
# Parameters
|
2024-01-08 08:29:14 +08:00
|
|
|
HELP_URL = "See https://docs.ultralytics.com/yolov5/tutorials/train_custom_data"
|
|
|
|
IMG_FORMATS = "bmp", "dng", "jpeg", "jpg", "mpo", "png", "tif", "tiff", "webp", "pfm" # include image suffixes
|
|
|
|
VID_FORMATS = "asf", "avi", "gif", "m4v", "mkv", "mov", "mp4", "mpeg", "mpg", "ts", "wmv" # include video suffixes
|
|
|
|
LOCAL_RANK = int(os.getenv("LOCAL_RANK", -1)) # https://pytorch.org/docs/stable/elastic/run.html
|
|
|
|
RANK = int(os.getenv("RANK", -1))
|
|
|
|
WORLD_SIZE = int(os.getenv("WORLD_SIZE", 1))
|
|
|
|
PIN_MEMORY = str(os.getenv("PIN_MEMORY", True)).lower() == "true" # global pin_memory for dataloaders
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
# Get orientation exif tag
|
|
|
|
for orientation in ExifTags.TAGS.keys():
|
2024-01-08 08:29:14 +08:00
|
|
|
if ExifTags.TAGS[orientation] == "Orientation":
|
2020-05-30 08:04:54 +08:00
|
|
|
break
|
|
|
|
|
|
|
|
|
2021-05-26 20:26:52 +08:00
|
|
|
def get_hash(paths):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Generates a single SHA256 hash for a list of file or directory paths by combining their sizes and paths."""
|
2021-05-26 20:26:52 +08:00
|
|
|
size = sum(os.path.getsize(p) for p in paths if os.path.exists(p)) # sizes
|
2023-02-09 20:57:18 +08:00
|
|
|
h = hashlib.sha256(str(size).encode()) # hash sizes
|
2024-01-08 08:29:14 +08:00
|
|
|
h.update("".join(paths).encode()) # hash paths
|
2021-05-26 20:26:52 +08:00
|
|
|
return h.hexdigest() # return hash
|
2020-07-10 11:07:16 +08:00
|
|
|
|
|
|
|
|
2020-05-30 08:04:54 +08:00
|
|
|
def exif_size(img):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Returns corrected PIL image size (width, height) considering EXIF orientation."""
|
2020-05-30 08:04:54 +08:00
|
|
|
s = img.size # (width, height)
|
2022-07-25 22:52:28 +08:00
|
|
|
with contextlib.suppress(Exception):
|
2020-05-30 08:04:54 +08:00
|
|
|
rotation = dict(img._getexif().items())[orientation]
|
2022-05-15 22:38:26 +08:00
|
|
|
if rotation in [6, 8]: # rotation 270 or 90
|
2020-05-30 08:04:54 +08:00
|
|
|
s = (s[1], s[0])
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
2021-07-02 19:25:54 +08:00
|
|
|
def exif_transpose(image):
|
|
|
|
"""
|
|
|
|
Transpose a PIL image accordingly if it has an EXIF Orientation tag.
|
2024-08-26 08:37:25 +08:00
|
|
|
Inplace version of https://github.com/python-pillow/Pillow/blob/master/src/PIL/ImageOps.py exif_transpose().
|
2021-07-02 19:25:54 +08:00
|
|
|
|
|
|
|
:param image: The image to transpose.
|
|
|
|
:return: An image.
|
|
|
|
"""
|
|
|
|
exif = image.getexif()
|
|
|
|
orientation = exif.get(0x0112, 1) # default 1
|
|
|
|
if orientation > 1:
|
2022-03-31 22:52:34 +08:00
|
|
|
method = {
|
|
|
|
2: Image.FLIP_LEFT_RIGHT,
|
|
|
|
3: Image.ROTATE_180,
|
|
|
|
4: Image.FLIP_TOP_BOTTOM,
|
|
|
|
5: Image.TRANSPOSE,
|
|
|
|
6: Image.ROTATE_270,
|
|
|
|
7: Image.TRANSVERSE,
|
2024-01-08 08:29:14 +08:00
|
|
|
8: Image.ROTATE_90,
|
|
|
|
}.get(orientation)
|
2021-07-02 19:25:54 +08:00
|
|
|
if method is not None:
|
|
|
|
image = image.transpose(method)
|
|
|
|
del exif[0x0112]
|
2024-01-08 08:29:14 +08:00
|
|
|
image.info["exif"] = exif.tobytes()
|
2021-07-02 19:25:54 +08:00
|
|
|
return image
|
|
|
|
|
|
|
|
|
2022-07-23 07:25:17 +08:00
|
|
|
def seed_worker(worker_id):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""
|
|
|
|
Sets the seed for a dataloader worker to ensure reproducibility, based on PyTorch's randomness notes.
|
|
|
|
|
|
|
|
See https://pytorch.org/docs/stable/notes/randomness.html#dataloader.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
worker_seed = torch.initial_seed() % 2**32
|
2022-07-23 07:25:17 +08:00
|
|
|
np.random.seed(worker_seed)
|
|
|
|
random.seed(worker_seed)
|
|
|
|
|
|
|
|
|
2024-01-03 15:15:07 +08:00
|
|
|
# Inherit from DistributedSampler and override iterator
|
|
|
|
# https://github.com/pytorch/pytorch/blob/master/torch/utils/data/distributed.py
|
|
|
|
class SmartDistributedSampler(distributed.DistributedSampler):
|
2024-08-26 21:46:32 +08:00
|
|
|
"""A distributed sampler ensuring deterministic shuffling and balanced data distribution across GPUs."""
|
|
|
|
|
2024-01-03 15:15:07 +08:00
|
|
|
def __iter__(self):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Yields indices for distributed data sampling, shuffled deterministically based on epoch and seed."""
|
2024-01-03 15:15:07 +08:00
|
|
|
g = torch.Generator()
|
|
|
|
g.manual_seed(self.seed + self.epoch)
|
|
|
|
|
2024-05-30 04:09:30 +08:00
|
|
|
# determine the eventual size (n) of self.indices (DDP indices)
|
2024-01-03 15:15:07 +08:00
|
|
|
n = int((len(self.dataset) - self.rank - 1) / self.num_replicas) + 1 # num_replicas == WORLD_SIZE
|
|
|
|
idx = torch.randperm(n, generator=g)
|
|
|
|
if not self.shuffle:
|
|
|
|
idx = idx.sort()[0]
|
|
|
|
|
|
|
|
idx = idx.tolist()
|
|
|
|
if self.drop_last:
|
2024-01-08 08:29:14 +08:00
|
|
|
idx = idx[: self.num_samples]
|
2024-01-03 15:15:07 +08:00
|
|
|
else:
|
|
|
|
padding_size = self.num_samples - len(idx)
|
|
|
|
if padding_size <= len(idx):
|
|
|
|
idx += idx[:padding_size]
|
|
|
|
else:
|
|
|
|
idx += (idx * math.ceil(padding_size / len(idx)))[:padding_size]
|
|
|
|
|
|
|
|
return iter(idx)
|
|
|
|
|
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
def create_dataloader(
|
|
|
|
path,
|
|
|
|
imgsz,
|
|
|
|
batch_size,
|
|
|
|
stride,
|
|
|
|
single_cls=False,
|
|
|
|
hyp=None,
|
|
|
|
augment=False,
|
|
|
|
cache=False,
|
|
|
|
pad=0.0,
|
|
|
|
rect=False,
|
|
|
|
rank=-1,
|
|
|
|
workers=8,
|
|
|
|
image_weights=False,
|
|
|
|
quad=False,
|
|
|
|
prefix="",
|
|
|
|
shuffle=False,
|
|
|
|
seed=0,
|
|
|
|
):
|
2024-07-01 06:17:24 +08:00
|
|
|
"""Creates and returns a configured DataLoader instance for loading and processing image datasets."""
|
2021-11-13 20:07:32 +08:00
|
|
|
if rect and shuffle:
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.warning("WARNING ⚠️ --rect is incompatible with DataLoader shuffle, setting shuffle=False")
|
2021-11-13 20:07:32 +08:00
|
|
|
shuffle = False
|
|
|
|
with torch_distributed_zero_first(rank): # init dataset *.cache only once if DDP
|
2022-03-31 22:52:34 +08:00
|
|
|
dataset = LoadImagesAndLabels(
|
|
|
|
path,
|
|
|
|
imgsz,
|
|
|
|
batch_size,
|
|
|
|
augment=augment, # augmentation
|
|
|
|
hyp=hyp, # hyperparameters
|
|
|
|
rect=rect, # rectangular batches
|
|
|
|
cache_images=cache,
|
|
|
|
single_cls=single_cls,
|
|
|
|
stride=int(stride),
|
|
|
|
pad=pad,
|
|
|
|
image_weights=image_weights,
|
2024-01-03 15:15:07 +08:00
|
|
|
prefix=prefix,
|
2024-01-08 08:29:14 +08:00
|
|
|
rank=rank,
|
|
|
|
)
|
2020-06-27 09:56:13 +08:00
|
|
|
|
|
|
|
batch_size = min(batch_size, len(dataset))
|
2022-01-28 15:18:01 +08:00
|
|
|
nd = torch.cuda.device_count() # number of CUDA devices
|
2022-02-21 04:47:46 +08:00
|
|
|
nw = min([os.cpu_count() // max(nd, 1), batch_size if batch_size > 1 else 0, workers]) # number of workers
|
2024-01-03 15:15:07 +08:00
|
|
|
sampler = None if rank == -1 else SmartDistributedSampler(dataset, shuffle=shuffle)
|
2021-11-13 20:07:32 +08:00
|
|
|
loader = DataLoader if image_weights else InfiniteDataLoader # only DataLoader allows for attribute updates
|
2022-07-23 07:25:17 +08:00
|
|
|
generator = torch.Generator()
|
2022-12-19 17:27:34 +08:00
|
|
|
generator.manual_seed(6148914691236517205 + seed + RANK)
|
2024-01-08 08:29:14 +08:00
|
|
|
return loader(
|
|
|
|
dataset,
|
|
|
|
batch_size=batch_size,
|
|
|
|
shuffle=shuffle and sampler is None,
|
|
|
|
num_workers=nw,
|
|
|
|
sampler=sampler,
|
2024-10-16 00:22:02 +08:00
|
|
|
drop_last=quad,
|
2024-01-08 08:29:14 +08:00
|
|
|
pin_memory=PIN_MEMORY,
|
|
|
|
collate_fn=LoadImagesAndLabels.collate_fn4 if quad else LoadImagesAndLabels.collate_fn,
|
|
|
|
worker_init_fn=seed_worker,
|
|
|
|
generator=generator,
|
|
|
|
), dataset
|
2021-11-13 20:07:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
class InfiniteDataLoader(dataloader.DataLoader):
|
2024-01-08 08:29:14 +08:00
|
|
|
"""
|
|
|
|
Dataloader that reuses workers.
|
2020-09-01 02:01:25 +08:00
|
|
|
|
2020-11-14 18:50:32 +08:00
|
|
|
Uses same syntax as vanilla DataLoader
|
2020-09-02 08:02:47 +08:00
|
|
|
"""
|
2022-04-05 04:47:00 +08:00
|
|
|
|
2020-09-01 02:01:25 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Initializes an InfiniteDataLoader that reuses workers with standard DataLoader syntax, augmenting with a
|
|
|
|
repeating sampler.
|
|
|
|
"""
|
2020-09-01 02:01:25 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2024-01-08 08:29:14 +08:00
|
|
|
object.__setattr__(self, "batch_sampler", _RepeatSampler(self.batch_sampler))
|
2020-09-01 02:01:25 +08:00
|
|
|
self.iterator = super().__iter__()
|
|
|
|
|
|
|
|
def __len__(self):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Returns the length of the batch sampler's sampler in the InfiniteDataLoader."""
|
2020-09-01 02:01:25 +08:00
|
|
|
return len(self.batch_sampler.sampler)
|
|
|
|
|
|
|
|
def __iter__(self):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Yields batches of data indefinitely in a loop by resetting the sampler when exhausted."""
|
2022-05-15 22:38:26 +08:00
|
|
|
for _ in range(len(self)):
|
2020-09-01 02:01:25 +08:00
|
|
|
yield next(self.iterator)
|
|
|
|
|
|
|
|
|
2021-10-29 00:35:01 +08:00
|
|
|
class _RepeatSampler:
|
2024-01-08 08:29:14 +08:00
|
|
|
"""
|
|
|
|
Sampler that repeats forever.
|
2020-09-01 02:01:25 +08:00
|
|
|
|
2020-09-11 03:27:35 +08:00
|
|
|
Args:
|
|
|
|
sampler (Sampler)
|
|
|
|
"""
|
2022-04-05 04:47:00 +08:00
|
|
|
|
2020-09-11 03:27:35 +08:00
|
|
|
def __init__(self, sampler):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Initializes a perpetual sampler wrapping a provided `Sampler` instance for endless data iteration."""
|
2020-09-11 03:27:35 +08:00
|
|
|
self.sampler = sampler
|
2020-09-01 02:01:25 +08:00
|
|
|
|
2020-09-11 03:27:35 +08:00
|
|
|
def __iter__(self):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Returns an infinite iterator over the dataset by repeatedly yielding from the given sampler."""
|
2020-09-11 03:27:35 +08:00
|
|
|
while True:
|
|
|
|
yield from iter(self.sampler)
|
2020-09-01 02:01:25 +08:00
|
|
|
|
|
|
|
|
2022-09-23 05:58:14 +08:00
|
|
|
class LoadScreenshots:
|
2024-08-26 21:46:32 +08:00
|
|
|
"""Loads and processes screenshots for YOLOv5 detection from specified screen regions using mss."""
|
|
|
|
|
2022-09-23 05:58:14 +08:00
|
|
|
def __init__(self, source, img_size=640, stride=32, auto=True, transforms=None):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""
|
|
|
|
Initializes a screenshot dataloader for YOLOv5 with specified source region, image size, stride, auto, and
|
|
|
|
transforms.
|
|
|
|
|
|
|
|
Source = [screen_number left top width height] (pixels)
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
check_requirements("mss")
|
2022-09-23 05:58:14 +08:00
|
|
|
import mss
|
|
|
|
|
|
|
|
source, *params = source.split()
|
|
|
|
self.screen, left, top, width, height = 0, None, None, None, None # default to full screen 0
|
|
|
|
if len(params) == 1:
|
|
|
|
self.screen = int(params[0])
|
|
|
|
elif len(params) == 4:
|
|
|
|
left, top, width, height = (int(x) for x in params)
|
|
|
|
elif len(params) == 5:
|
|
|
|
self.screen, left, top, width, height = (int(x) for x in params)
|
|
|
|
self.img_size = img_size
|
|
|
|
self.stride = stride
|
|
|
|
self.transforms = transforms
|
|
|
|
self.auto = auto
|
2024-01-08 08:29:14 +08:00
|
|
|
self.mode = "stream"
|
2022-09-23 05:58:14 +08:00
|
|
|
self.frame = 0
|
|
|
|
self.sct = mss.mss()
|
|
|
|
|
|
|
|
# Parse monitor shape
|
|
|
|
monitor = self.sct.monitors[self.screen]
|
2024-01-08 08:29:14 +08:00
|
|
|
self.top = monitor["top"] if top is None else (monitor["top"] + top)
|
|
|
|
self.left = monitor["left"] if left is None else (monitor["left"] + left)
|
|
|
|
self.width = width or monitor["width"]
|
|
|
|
self.height = height or monitor["height"]
|
|
|
|
self.monitor = {"left": self.left, "top": self.top, "width": self.width, "height": self.height}
|
2022-09-23 05:58:14 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Iterates over itself, enabling use in loops and iterable contexts."""
|
2022-09-23 05:58:14 +08:00
|
|
|
return self
|
|
|
|
|
|
|
|
def __next__(self):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Captures and returns the next screen frame as a BGR numpy array, cropping to only the first three channels
|
|
|
|
from BGRA.
|
|
|
|
"""
|
2022-09-23 05:58:14 +08:00
|
|
|
im0 = np.array(self.sct.grab(self.monitor))[:, :, :3] # [:, :, :3] BGRA to BGR
|
2024-01-08 08:29:14 +08:00
|
|
|
s = f"screen {self.screen} (LTWH): {self.left},{self.top},{self.width},{self.height}: "
|
2022-09-23 05:58:14 +08:00
|
|
|
|
|
|
|
if self.transforms:
|
|
|
|
im = self.transforms(im0) # transforms
|
|
|
|
else:
|
|
|
|
im = letterbox(im0, self.img_size, stride=self.stride, auto=self.auto)[0] # padded resize
|
|
|
|
im = im.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
|
|
|
|
im = np.ascontiguousarray(im) # contiguous
|
|
|
|
self.frame += 1
|
|
|
|
return str(self.screen), im, im0, None, s # screen, img, original img, im0s, s
|
|
|
|
|
|
|
|
|
2021-10-08 07:04:08 +08:00
|
|
|
class LoadImages:
|
2024-08-26 08:37:25 +08:00
|
|
|
"""YOLOv5 image/video dataloader, i.e. `python detect.py --source image.jpg/vid.mp4`."""
|
2024-02-25 21:04:01 +08:00
|
|
|
|
2022-09-04 23:15:53 +08:00
|
|
|
def __init__(self, path, img_size=640, stride=32, auto=True, transforms=None, vid_stride=1):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Initializes YOLOv5 loader for images/videos, supporting glob patterns, directories, and lists of paths."""
|
2024-01-08 08:29:14 +08:00
|
|
|
if isinstance(path, str) and Path(path).suffix == ".txt": # *.txt file with img/vid/dir on each line
|
2022-12-04 06:58:58 +08:00
|
|
|
path = Path(path).read_text().rsplit()
|
2022-07-08 20:32:40 +08:00
|
|
|
files = []
|
|
|
|
for p in sorted(path) if isinstance(path, (list, tuple)) else [path]:
|
|
|
|
p = str(Path(p).resolve())
|
2024-01-08 08:29:14 +08:00
|
|
|
if "*" in p:
|
2022-07-08 20:32:40 +08:00
|
|
|
files.extend(sorted(glob.glob(p, recursive=True))) # glob
|
|
|
|
elif os.path.isdir(p):
|
2024-01-08 08:29:14 +08:00
|
|
|
files.extend(sorted(glob.glob(os.path.join(p, "*.*")))) # dir
|
2022-07-08 20:32:40 +08:00
|
|
|
elif os.path.isfile(p):
|
|
|
|
files.append(p) # files
|
|
|
|
else:
|
2024-01-08 08:29:14 +08:00
|
|
|
raise FileNotFoundError(f"{p} does not exist")
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
images = [x for x in files if x.split(".")[-1].lower() in IMG_FORMATS]
|
|
|
|
videos = [x for x in files if x.split(".")[-1].lower() in VID_FORMATS]
|
2020-07-13 05:14:51 +08:00
|
|
|
ni, nv = len(images), len(videos)
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
self.img_size = img_size
|
2021-01-31 05:47:23 +08:00
|
|
|
self.stride = stride
|
2020-05-30 08:04:54 +08:00
|
|
|
self.files = images + videos
|
2020-07-13 05:14:51 +08:00
|
|
|
self.nf = ni + nv # number of files
|
|
|
|
self.video_flag = [False] * ni + [True] * nv
|
2024-01-08 08:29:14 +08:00
|
|
|
self.mode = "image"
|
Add TensorFlow and TFLite export (#1127)
* Add models/tf.py for TensorFlow and TFLite export
* Set auto=False for int8 calibration
* Update requirements.txt for TensorFlow and TFLite export
* Read anchors directly from PyTorch weights
* Add --tf-nms to append NMS in TensorFlow SavedModel and GraphDef export
* Remove check_anchor_order, check_file, set_logging from import
* Reformat code and optimize imports
* Autodownload model and check cfg
* update --source path, img-size to 320, single output
* Adjust representative_dataset
* Put representative dataset in tfl_int8 block
* detect.py TF inference
* weights to string
* weights to string
* cleanup tf.py
* Add --dynamic-batch-size
* Add xywh normalization to reduce calibration error
* Update requirements.txt
TensorFlow 2.3.1 -> 2.4.0 to avoid int8 quantization error
* Fix imports
Move C3 from models.experimental to models.common
* Add models/tf.py for TensorFlow and TFLite export
* Set auto=False for int8 calibration
* Update requirements.txt for TensorFlow and TFLite export
* Read anchors directly from PyTorch weights
* Add --tf-nms to append NMS in TensorFlow SavedModel and GraphDef export
* Remove check_anchor_order, check_file, set_logging from import
* Reformat code and optimize imports
* Autodownload model and check cfg
* update --source path, img-size to 320, single output
* Adjust representative_dataset
* detect.py TF inference
* Put representative dataset in tfl_int8 block
* weights to string
* weights to string
* cleanup tf.py
* Add --dynamic-batch-size
* Add xywh normalization to reduce calibration error
* Update requirements.txt
TensorFlow 2.3.1 -> 2.4.0 to avoid int8 quantization error
* Fix imports
Move C3 from models.experimental to models.common
* implement C3() and SiLU()
* Fix reshape dim to support dynamic batching
* Add epsilon argument in tf_BN, which is different between TF and PT
* Set stride to None if not using PyTorch, and do not warmup without PyTorch
* Add list support in check_img_size()
* Add list input support in detect.py
* sys.path.append('./') to run from yolov5/
* Add int8 quantization support for TensorFlow 2.5
* Add get_coco128.sh
* Remove --no-tfl-detect in models/tf.py (Use tf-android-tfl-detect branch for EdgeTPU)
* Update requirements.txt
* Replace torch.load() with attempt_load()
* Update requirements.txt
* Add --tf-raw-resize to set half_pixel_centers=False
* Add --agnostic-nms for TF class-agnostic NMS
* Cleanup after merge
* Cleanup2 after merge
* Cleanup3 after merge
* Add tf.py docstring with credit and usage
* pb saved_model and tflite use only one model in detect.py
* Add use cases in docstring of tf.py
* Remove redundant `stride` definition
* Remove keras direct import
* Fix `check_requirements(('tensorflow>=2.4.1',))`
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
2021-08-17 19:18:16 +08:00
|
|
|
self.auto = auto
|
2022-08-18 04:41:43 +08:00
|
|
|
self.transforms = transforms # optional
|
2022-09-04 23:15:53 +08:00
|
|
|
self.vid_stride = vid_stride # video frame-rate stride
|
2020-05-30 08:04:54 +08:00
|
|
|
if any(videos):
|
2022-09-02 04:30:26 +08:00
|
|
|
self._new_video(videos[0]) # new video
|
2020-05-30 08:04:54 +08:00
|
|
|
else:
|
|
|
|
self.cap = None
|
2025-01-13 02:05:20 +08:00
|
|
|
assert (
|
|
|
|
self.nf > 0
|
|
|
|
), f"No images or videos found in {p}. Supported formats are:\nimages: {IMG_FORMATS}\nvideos: {VID_FORMATS}"
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Initializes iterator by resetting count and returns the iterator object itself."""
|
2020-05-30 08:04:54 +08:00
|
|
|
self.count = 0
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __next__(self):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Advances to the next file in the dataset, raising StopIteration if at the end."""
|
2020-07-13 05:14:51 +08:00
|
|
|
if self.count == self.nf:
|
2020-05-30 08:04:54 +08:00
|
|
|
raise StopIteration
|
|
|
|
path = self.files[self.count]
|
|
|
|
|
|
|
|
if self.video_flag[self.count]:
|
|
|
|
# Read video
|
2024-01-08 08:29:14 +08:00
|
|
|
self.mode = "video"
|
2022-09-19 20:01:46 +08:00
|
|
|
for _ in range(self.vid_stride):
|
|
|
|
self.cap.grab()
|
|
|
|
ret_val, im0 = self.cap.retrieve()
|
2021-12-02 17:57:39 +08:00
|
|
|
while not ret_val:
|
2020-05-30 08:04:54 +08:00
|
|
|
self.count += 1
|
|
|
|
self.cap.release()
|
2020-07-13 05:14:51 +08:00
|
|
|
if self.count == self.nf: # last video
|
2020-05-30 08:04:54 +08:00
|
|
|
raise StopIteration
|
2022-05-15 22:38:26 +08:00
|
|
|
path = self.files[self.count]
|
2022-09-02 04:30:26 +08:00
|
|
|
self._new_video(path)
|
2022-08-18 04:41:43 +08:00
|
|
|
ret_val, im0 = self.cap.read()
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
self.frame += 1
|
2022-09-04 23:15:53 +08:00
|
|
|
# im0 = self._cv2_rotate(im0) # for use if cv2 autorotation is False
|
2024-01-08 08:29:14 +08:00
|
|
|
s = f"video {self.count + 1}/{self.nf} ({self.frame}/{self.frames}) {path}: "
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
else:
|
|
|
|
# Read image
|
|
|
|
self.count += 1
|
2022-08-18 04:41:43 +08:00
|
|
|
im0 = cv2.imread(path) # BGR
|
2024-01-08 08:29:14 +08:00
|
|
|
assert im0 is not None, f"Image Not Found {path}"
|
|
|
|
s = f"image {self.count}/{self.nf} {path}: "
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2022-08-18 04:41:43 +08:00
|
|
|
if self.transforms:
|
2022-08-30 21:17:58 +08:00
|
|
|
im = self.transforms(im0) # transforms
|
2022-08-18 04:41:43 +08:00
|
|
|
else:
|
|
|
|
im = letterbox(im0, self.img_size, stride=self.stride, auto=self.auto)[0] # padded resize
|
|
|
|
im = im.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
|
|
|
|
im = np.ascontiguousarray(im) # contiguous
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2022-08-18 04:41:43 +08:00
|
|
|
return path, im, im0, self.cap, s
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2022-09-02 04:30:26 +08:00
|
|
|
def _new_video(self, path):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Initializes a new video capture object with path, frame count adjusted by stride, and orientation
|
|
|
|
metadata.
|
|
|
|
"""
|
2020-05-30 08:04:54 +08:00
|
|
|
self.frame = 0
|
|
|
|
self.cap = cv2.VideoCapture(path)
|
2022-09-04 23:15:53 +08:00
|
|
|
self.frames = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT) / self.vid_stride)
|
2022-09-02 04:30:26 +08:00
|
|
|
self.orientation = int(self.cap.get(cv2.CAP_PROP_ORIENTATION_META)) # rotation degrees
|
|
|
|
# self.cap.set(cv2.CAP_PROP_ORIENTATION_AUTO, 0) # disable https://github.com/ultralytics/yolov5/issues/8493
|
|
|
|
|
|
|
|
def _cv2_rotate(self, im):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Rotates a cv2 image based on its orientation; supports 0, 90, and 180 degrees rotations."""
|
2022-09-02 04:30:26 +08:00
|
|
|
if self.orientation == 0:
|
|
|
|
return cv2.rotate(im, cv2.ROTATE_90_CLOCKWISE)
|
|
|
|
elif self.orientation == 180:
|
|
|
|
return cv2.rotate(im, cv2.ROTATE_90_COUNTERCLOCKWISE)
|
|
|
|
elif self.orientation == 90:
|
|
|
|
return cv2.rotate(im, cv2.ROTATE_180)
|
|
|
|
return im
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
def __len__(self):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Returns the number of files in the dataset."""
|
2020-07-13 05:14:51 +08:00
|
|
|
return self.nf # number of files
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
|
2021-10-08 07:04:08 +08:00
|
|
|
class LoadStreams:
|
2024-08-26 21:46:32 +08:00
|
|
|
"""Loads and processes video streams for YOLOv5, supporting various sources including YouTube and IP cameras."""
|
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
def __init__(self, sources="file.streams", img_size=640, stride=32, auto=True, transforms=None, vid_stride=1):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Initializes a stream loader for processing video streams with YOLOv5, supporting various sources including
|
|
|
|
YouTube.
|
|
|
|
"""
|
2022-09-02 20:12:10 +08:00
|
|
|
torch.backends.cudnn.benchmark = True # faster for fixed-size inference
|
2024-01-08 08:29:14 +08:00
|
|
|
self.mode = "stream"
|
2020-05-30 08:04:54 +08:00
|
|
|
self.img_size = img_size
|
2021-01-31 05:47:23 +08:00
|
|
|
self.stride = stride
|
2022-09-04 23:15:53 +08:00
|
|
|
self.vid_stride = vid_stride # video frame-rate stride
|
2022-10-24 22:20:47 +08:00
|
|
|
sources = Path(sources).read_text().rsplit() if os.path.isfile(sources) else [sources]
|
2020-05-30 08:04:54 +08:00
|
|
|
n = len(sources)
|
2020-12-18 09:20:20 +08:00
|
|
|
self.sources = [clean_str(x) for x in sources] # clean source names for later
|
2022-09-02 20:12:10 +08:00
|
|
|
self.imgs, self.fps, self.frames, self.threads = [None] * n, [0] * n, [0] * n, [None] * n
|
2021-04-16 23:58:28 +08:00
|
|
|
for i, s in enumerate(sources): # index, source
|
|
|
|
# Start thread to read frames from video stream
|
2024-01-08 08:29:14 +08:00
|
|
|
st = f"{i + 1}/{n}: {s}... "
|
|
|
|
if urlparse(s).hostname in ("www.youtube.com", "youtube.com", "youtu.be"): # if source is YouTube video
|
2023-09-10 10:59:27 +08:00
|
|
|
# YouTube format i.e. 'https://www.youtube.com/watch?v=Zgi9g1ksQHc' or 'https://youtu.be/LNwODJXcvt4'
|
2024-01-08 08:29:14 +08:00
|
|
|
check_requirements(("pafy", "youtube_dl==2020.12.2"))
|
2021-04-12 00:53:40 +08:00
|
|
|
import pafy
|
2024-01-08 08:29:14 +08:00
|
|
|
|
|
|
|
s = pafy.new(s).getbest(preftype="mp4").url # YouTube URL
|
2021-04-16 23:58:28 +08:00
|
|
|
s = eval(s) if s.isnumeric() else s # i.e. s = '0' local webcam
|
2022-06-14 20:28:21 +08:00
|
|
|
if s == 0:
|
2024-01-08 08:29:14 +08:00
|
|
|
assert not is_colab(), "--source 0 webcam unsupported on Colab. Rerun command in a local environment."
|
|
|
|
assert not is_kaggle(), "--source 0 webcam unsupported on Kaggle. Rerun command in a local environment."
|
2021-04-16 23:58:28 +08:00
|
|
|
cap = cv2.VideoCapture(s)
|
2024-01-08 08:29:14 +08:00
|
|
|
assert cap.isOpened(), f"{st}Failed to open {s}"
|
2020-05-30 08:04:54 +08:00
|
|
|
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
|
|
|
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
2022-01-05 11:32:42 +08:00
|
|
|
fps = cap.get(cv2.CAP_PROP_FPS) # warning: may return 0 or nan
|
2024-01-08 08:29:14 +08:00
|
|
|
self.frames[i] = max(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), 0) or float("inf") # infinite stream fallback
|
2022-01-05 11:32:42 +08:00
|
|
|
self.fps[i] = max((fps if math.isfinite(fps) else 0) % 100, 0) or 30 # 30 FPS fallback
|
2021-04-12 00:53:40 +08:00
|
|
|
|
2020-05-30 08:04:54 +08:00
|
|
|
_, self.imgs[i] = cap.read() # guarantee first frame
|
2021-10-08 14:26:43 +08:00
|
|
|
self.threads[i] = Thread(target=self.update, args=([i, cap, s]), daemon=True)
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.info(f"{st} Success ({self.frames[i]} frames {w}x{h} at {self.fps[i]:.2f} FPS)")
|
2021-05-21 22:51:07 +08:00
|
|
|
self.threads[i].start()
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.info("") # newline
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
# check for common shapes
|
2022-08-23 20:37:46 +08:00
|
|
|
s = np.stack([letterbox(x, img_size, stride=stride, auto=auto)[0].shape for x in self.imgs])
|
2020-05-30 08:04:54 +08:00
|
|
|
self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal
|
2022-08-23 20:37:46 +08:00
|
|
|
self.auto = auto and self.rect
|
|
|
|
self.transforms = transforms # optional
|
2020-05-30 08:04:54 +08:00
|
|
|
if not self.rect:
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.warning("WARNING ⚠️ Stream shapes differ. For optimal performance supply similarly-shaped streams.")
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2021-10-08 14:26:43 +08:00
|
|
|
def update(self, i, cap, stream):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Reads frames from stream `i`, updating imgs array; handles stream reopening on signal loss."""
|
2022-09-04 23:15:53 +08:00
|
|
|
n, f = 0, self.frames[i] # frame number, frame array
|
2021-05-21 22:51:07 +08:00
|
|
|
while cap.isOpened() and n < f:
|
2020-05-30 08:04:54 +08:00
|
|
|
n += 1
|
2022-09-02 20:12:10 +08:00
|
|
|
cap.grab() # .read() = .grab() followed by .retrieve()
|
2022-09-04 23:15:53 +08:00
|
|
|
if n % self.vid_stride == 0:
|
2021-02-16 03:02:20 +08:00
|
|
|
success, im = cap.retrieve()
|
2021-10-08 14:26:43 +08:00
|
|
|
if success:
|
|
|
|
self.imgs[i] = im
|
|
|
|
else:
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.warning("WARNING ⚠️ Video stream unresponsive, please check your IP camera connection.")
|
2021-12-02 17:51:19 +08:00
|
|
|
self.imgs[i] = np.zeros_like(self.imgs[i])
|
2021-10-08 14:26:43 +08:00
|
|
|
cap.open(stream) # re-open stream if signal was lost
|
2022-06-17 23:59:27 +08:00
|
|
|
time.sleep(0.0) # wait time
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Resets and returns the iterator for iterating over video frames or images in a dataset."""
|
2020-05-30 08:04:54 +08:00
|
|
|
self.count = -1
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __next__(self):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Iterates over video frames or images, halting on thread stop or 'q' key press, raising `StopIteration` when
|
|
|
|
done.
|
|
|
|
"""
|
2020-05-30 08:04:54 +08:00
|
|
|
self.count += 1
|
2024-01-08 08:29:14 +08:00
|
|
|
if not all(x.is_alive() for x in self.threads) or cv2.waitKey(1) == ord("q"): # q to quit
|
2020-05-30 08:04:54 +08:00
|
|
|
cv2.destroyAllWindows()
|
|
|
|
raise StopIteration
|
|
|
|
|
2022-08-23 20:37:46 +08:00
|
|
|
im0 = self.imgs.copy()
|
|
|
|
if self.transforms:
|
2022-08-30 21:17:58 +08:00
|
|
|
im = np.stack([self.transforms(x) for x in im0]) # transforms
|
2022-08-23 20:37:46 +08:00
|
|
|
else:
|
|
|
|
im = np.stack([letterbox(x, self.img_size, stride=self.stride, auto=self.auto)[0] for x in im0]) # resize
|
|
|
|
im = im[..., ::-1].transpose((0, 3, 1, 2)) # BGR to RGB, BHWC to BCHW
|
|
|
|
im = np.ascontiguousarray(im) # contiguous
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
return self.sources, im, im0, None, ""
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
def __len__(self):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Returns the number of sources in the dataset, supporting up to 32 streams at 30 FPS over 30 years."""
|
2021-07-04 18:55:57 +08:00
|
|
|
return len(self.sources) # 1E12 frames = 32 streams at 30 FPS for 30 years
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
|
2020-11-24 23:13:04 +08:00
|
|
|
def img2label_paths(img_paths):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Generates label file paths from corresponding image file paths by replacing `/images/` with `/labels/` and
|
|
|
|
extension with `.txt`.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
sa, sb = f"{os.sep}images{os.sep}", f"{os.sep}labels{os.sep}" # /images/, /labels/ substrings
|
|
|
|
return [sb.join(x.rsplit(sa, 1)).rsplit(".", 1)[0] + ".txt" for x in img_paths]
|
2020-11-24 23:13:04 +08:00
|
|
|
|
|
|
|
|
2021-10-08 07:04:08 +08:00
|
|
|
class LoadImagesAndLabels(Dataset):
|
2024-08-26 21:46:32 +08:00
|
|
|
"""Loads images and their corresponding labels for training and validation in YOLOv5."""
|
|
|
|
|
2021-10-16 03:32:08 +08:00
|
|
|
cache_version = 0.6 # dataset labels *.cache version
|
2022-05-13 20:26:11 +08:00
|
|
|
rand_interp_methods = [cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4]
|
2021-09-18 00:01:52 +08:00
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
path,
|
|
|
|
img_size=640,
|
|
|
|
batch_size=16,
|
|
|
|
augment=False,
|
|
|
|
hyp=None,
|
|
|
|
rect=False,
|
|
|
|
image_weights=False,
|
|
|
|
cache_images=False,
|
|
|
|
single_cls=False,
|
|
|
|
stride=32,
|
|
|
|
pad=0.0,
|
|
|
|
min_items=0,
|
|
|
|
prefix="",
|
|
|
|
rank=-1,
|
|
|
|
seed=0,
|
|
|
|
):
|
2024-07-01 06:17:24 +08:00
|
|
|
"""Initializes the YOLOv5 dataset loader, handling images and their labels, caching, and preprocessing."""
|
2020-10-24 21:09:19 +08:00
|
|
|
self.img_size = img_size
|
|
|
|
self.augment = augment
|
|
|
|
self.hyp = hyp
|
|
|
|
self.image_weights = image_weights
|
|
|
|
self.rect = False if image_weights else rect
|
|
|
|
self.mosaic = self.augment and not self.rect # load 4 images at a time into a mosaic (only during training)
|
|
|
|
self.mosaic_border = [-img_size // 2, -img_size // 2]
|
|
|
|
self.stride = stride
|
2021-02-02 13:38:41 +08:00
|
|
|
self.path = path
|
2022-09-22 01:53:26 +08:00
|
|
|
self.albumentations = Albumentations(size=img_size) if augment else None
|
2021-02-06 01:06:23 +08:00
|
|
|
|
2020-05-30 08:04:54 +08:00
|
|
|
try:
|
2020-07-10 11:07:16 +08:00
|
|
|
f = [] # image files
|
2020-07-10 04:45:55 +08:00
|
|
|
for p in path if isinstance(path, list) else [path]:
|
2020-11-16 23:24:57 +08:00
|
|
|
p = Path(p) # os-agnostic
|
|
|
|
if p.is_dir(): # dir
|
2024-01-08 08:29:14 +08:00
|
|
|
f += glob.glob(str(p / "**" / "*.*"), recursive=True)
|
2021-10-21 01:25:33 +08:00
|
|
|
# f = list(p.rglob('*.*')) # pathlib
|
2020-11-16 23:24:57 +08:00
|
|
|
elif p.is_file(): # file
|
2021-10-29 00:35:01 +08:00
|
|
|
with open(p) as t:
|
2020-11-29 18:58:14 +08:00
|
|
|
t = t.read().strip().splitlines()
|
2020-11-16 23:24:57 +08:00
|
|
|
parent = str(p.parent) + os.sep
|
2024-01-08 08:29:14 +08:00
|
|
|
f += [x.replace("./", parent, 1) if x.startswith("./") else x for x in t] # to global path
|
2022-11-16 02:00:33 +08:00
|
|
|
# f += [p.parent / x.lstrip(os.sep) for x in t] # to global path (pathlib)
|
2020-07-09 17:52:12 +08:00
|
|
|
else:
|
2024-01-08 08:29:14 +08:00
|
|
|
raise FileNotFoundError(f"{prefix}{p} does not exist")
|
|
|
|
self.im_files = sorted(x.replace("/", os.sep) for x in f if x.split(".")[-1].lower() in IMG_FORMATS)
|
2021-10-21 01:25:33 +08:00
|
|
|
# self.img_files = sorted([x for x in f if x.suffix[1:].lower() in IMG_FORMATS]) # pathlib
|
2024-01-08 08:29:14 +08:00
|
|
|
assert self.im_files, f"{prefix}No images found"
|
2020-07-10 07:28:20 +08:00
|
|
|
except Exception as e:
|
2024-01-08 08:29:14 +08:00
|
|
|
raise Exception(f"{prefix}Error loading data from {path}: {e}\n{HELP_URL}") from e
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2020-07-10 11:07:16 +08:00
|
|
|
# Check cache
|
2022-03-06 23:16:17 +08:00
|
|
|
self.label_files = img2label_paths(self.im_files) # labels
|
2024-01-08 08:29:14 +08:00
|
|
|
cache_path = (p if p.is_file() else Path(self.label_files[0]).parent).with_suffix(".cache")
|
2021-07-10 20:18:46 +08:00
|
|
|
try:
|
|
|
|
cache, exists = np.load(cache_path, allow_pickle=True).item(), True # load dict
|
2024-01-08 08:29:14 +08:00
|
|
|
assert cache["version"] == self.cache_version # matches current version
|
|
|
|
assert cache["hash"] == get_hash(self.label_files + self.im_files) # identical hash
|
2022-02-05 22:22:59 +08:00
|
|
|
except Exception:
|
2022-06-30 22:19:22 +08:00
|
|
|
cache, exists = self.cache_labels(cache_path, prefix), False # run cache ops
|
2020-06-14 06:05:41 +08:00
|
|
|
|
2020-11-24 23:13:04 +08:00
|
|
|
# Display cache
|
2024-01-08 08:29:14 +08:00
|
|
|
nf, nm, ne, nc, n = cache.pop("results") # found, missing, empty, corrupt, total
|
2022-05-15 22:38:26 +08:00
|
|
|
if exists and LOCAL_RANK in {-1, 0}:
|
2024-01-08 08:29:14 +08:00
|
|
|
d = f"Scanning {cache_path}... {nf} images, {nm + ne} backgrounds, {nc} corrupt"
|
2022-11-19 04:39:13 +08:00
|
|
|
tqdm(None, desc=prefix + d, total=n, initial=n, bar_format=TQDM_BAR_FORMAT) # display cache results
|
2024-01-08 08:29:14 +08:00
|
|
|
if cache["msgs"]:
|
|
|
|
LOGGER.info("\n".join(cache["msgs"])) # display warnings
|
|
|
|
assert nf > 0 or not augment, f"{prefix}No labels found in {cache_path}, can not start training. {HELP_URL}"
|
2020-11-24 23:13:04 +08:00
|
|
|
|
2020-10-24 20:50:50 +08:00
|
|
|
# Read cache
|
2024-01-08 08:29:14 +08:00
|
|
|
[cache.pop(k) for k in ("hash", "version", "msgs")] # remove items
|
2021-02-12 13:22:45 +08:00
|
|
|
labels, shapes, self.segments = zip(*cache.values())
|
2022-08-19 21:07:04 +08:00
|
|
|
nl = len(np.concatenate(labels, 0)) # number of labels
|
2024-01-08 08:29:14 +08:00
|
|
|
assert nl > 0 or not augment, f"{prefix}All labels empty in {cache_path}, can not start training. {HELP_URL}"
|
2020-07-10 11:07:16 +08:00
|
|
|
self.labels = list(labels)
|
2022-08-04 23:06:08 +08:00
|
|
|
self.shapes = np.array(shapes)
|
2022-03-06 23:16:17 +08:00
|
|
|
self.im_files = list(cache.keys()) # update
|
2020-10-24 20:50:50 +08:00
|
|
|
self.label_files = img2label_paths(cache.keys()) # update
|
2022-11-01 21:53:14 +08:00
|
|
|
|
|
|
|
# Filter images
|
|
|
|
if min_items:
|
2022-11-01 23:41:37 +08:00
|
|
|
include = np.array([len(x) >= min_items for x in self.labels]).nonzero()[0].astype(int)
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.info(f"{prefix}{n - len(include)}/{n} images filtered from dataset")
|
2022-11-01 21:53:14 +08:00
|
|
|
self.im_files = [self.im_files[i] for i in include]
|
|
|
|
self.label_files = [self.label_files[i] for i in include]
|
|
|
|
self.labels = [self.labels[i] for i in include]
|
|
|
|
self.segments = [self.segments[i] for i in include]
|
|
|
|
self.shapes = self.shapes[include] # wh
|
|
|
|
|
|
|
|
# Create indices
|
|
|
|
n = len(self.shapes) # number of images
|
2022-09-07 15:11:30 +08:00
|
|
|
bi = np.floor(np.arange(n) / batch_size).astype(int) # batch index
|
2020-10-24 21:09:19 +08:00
|
|
|
nb = bi[-1] + 1 # number of batches
|
|
|
|
self.batch = bi # batch index of image
|
|
|
|
self.n = n
|
2024-01-03 15:15:07 +08:00
|
|
|
self.indices = np.arange(n)
|
|
|
|
if rank > -1: # DDP indices (see: SmartDistributedSampler)
|
|
|
|
# force each rank (i.e. GPU process) to sample the same subset of data on every epoch
|
|
|
|
self.indices = self.indices[np.random.RandomState(seed=seed).permutation(n) % WORLD_SIZE == RANK]
|
2020-10-24 21:09:19 +08:00
|
|
|
|
2021-10-14 06:48:54 +08:00
|
|
|
# Update labels
|
|
|
|
include_class = [] # filter labels to include only these classes (optional)
|
2023-03-24 03:29:11 +08:00
|
|
|
self.segments = list(self.segments)
|
2021-10-14 06:48:54 +08:00
|
|
|
include_class_array = np.array(include_class).reshape(1, -1)
|
|
|
|
for i, (label, segment) in enumerate(zip(self.labels, self.segments)):
|
|
|
|
if include_class:
|
|
|
|
j = (label[:, 0:1] == include_class_array).any(1)
|
|
|
|
self.labels[i] = label[j]
|
|
|
|
if segment:
|
2023-03-24 03:29:11 +08:00
|
|
|
self.segments[i] = [segment[idx] for idx, elem in enumerate(j) if elem]
|
2021-10-14 06:48:54 +08:00
|
|
|
if single_cls: # single-class training, merge all classes into 0
|
|
|
|
self.labels[i][:, 0] = 0
|
|
|
|
|
2020-10-24 20:50:50 +08:00
|
|
|
# Rectangular Training
|
2020-05-30 08:04:54 +08:00
|
|
|
if self.rect:
|
|
|
|
# Sort by aspect ratio
|
2020-06-14 06:05:41 +08:00
|
|
|
s = self.shapes # wh
|
2020-05-30 08:04:54 +08:00
|
|
|
ar = s[:, 1] / s[:, 0] # aspect ratio
|
|
|
|
irect = ar.argsort()
|
2022-03-06 23:16:17 +08:00
|
|
|
self.im_files = [self.im_files[i] for i in irect]
|
2020-05-30 08:04:54 +08:00
|
|
|
self.label_files = [self.label_files[i] for i in irect]
|
2020-07-10 11:39:11 +08:00
|
|
|
self.labels = [self.labels[i] for i in irect]
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
self.segments = [self.segments[i] for i in irect]
|
2020-05-30 08:04:54 +08:00
|
|
|
self.shapes = s[irect] # wh
|
|
|
|
ar = ar[irect]
|
|
|
|
|
|
|
|
# Set training image shapes
|
|
|
|
shapes = [[1, 1]] * nb
|
|
|
|
for i in range(nb):
|
|
|
|
ari = ar[bi == i]
|
|
|
|
mini, maxi = ari.min(), ari.max()
|
|
|
|
if maxi < 1:
|
|
|
|
shapes[i] = [maxi, 1]
|
|
|
|
elif mini > 1:
|
|
|
|
shapes[i] = [1, 1 / mini]
|
|
|
|
|
2022-09-07 15:11:30 +08:00
|
|
|
self.batch_shapes = np.ceil(np.array(shapes) * img_size / stride + pad).astype(int) * stride
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2022-11-04 22:30:12 +08:00
|
|
|
# Cache images into RAM/disk for faster training
|
2024-01-08 08:29:14 +08:00
|
|
|
if cache_images == "ram" and not self.check_cache_ram(prefix=prefix):
|
2022-11-04 22:30:12 +08:00
|
|
|
cache_images = False
|
2022-03-06 23:16:17 +08:00
|
|
|
self.ims = [None] * n
|
2024-01-08 08:29:14 +08:00
|
|
|
self.npy_files = [Path(f).with_suffix(".npy") for f in self.im_files]
|
2020-07-10 11:07:16 +08:00
|
|
|
if cache_images:
|
2022-11-04 22:30:12 +08:00
|
|
|
b, gb = 0, 1 << 30 # bytes of cached images, bytes per gigabytes
|
2022-03-06 23:16:17 +08:00
|
|
|
self.im_hw0, self.im_hw = [None] * n, [None] * n
|
2024-01-08 08:29:14 +08:00
|
|
|
fcn = self.cache_images_to_disk if cache_images == "disk" else self.load_image
|
2024-01-03 15:15:07 +08:00
|
|
|
results = ThreadPool(NUM_THREADS).imap(lambda i: (i, fcn(i)), self.indices)
|
|
|
|
pbar = tqdm(results, total=len(self.indices), bar_format=TQDM_BAR_FORMAT, disable=LOCAL_RANK > 0)
|
2020-11-07 09:18:18 +08:00
|
|
|
for i, x in pbar:
|
2024-01-08 08:29:14 +08:00
|
|
|
if cache_images == "disk":
|
2022-11-04 22:30:12 +08:00
|
|
|
b += self.npy_files[i].stat().st_size
|
2022-02-08 01:59:26 +08:00
|
|
|
else: # 'ram'
|
2022-03-06 23:16:17 +08:00
|
|
|
self.ims[i], self.im_hw0[i], self.im_hw[i] = x # im, hw_orig, hw_resized = load_image(self, i)
|
2024-01-03 15:15:07 +08:00
|
|
|
b += self.ims[i].nbytes * WORLD_SIZE
|
2024-01-08 08:29:14 +08:00
|
|
|
pbar.desc = f"{prefix}Caching images ({b / gb:.1f}GB {cache_images})"
|
2021-03-29 18:21:25 +08:00
|
|
|
pbar.close()
|
2021-04-12 00:53:40 +08:00
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
def check_cache_ram(self, safety_margin=0.1, prefix=""):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Checks if available RAM is sufficient for caching images, adjusting for a safety margin."""
|
2022-11-04 22:30:12 +08:00
|
|
|
b, gb = 0, 1 << 30 # bytes of cached images, bytes per gigabytes
|
|
|
|
n = min(self.n, 30) # extrapolate from 30 random images
|
|
|
|
for _ in range(n):
|
|
|
|
im = cv2.imread(random.choice(self.im_files)) # sample image
|
|
|
|
ratio = self.img_size / max(im.shape[0], im.shape[1]) # max(h, w) # ratio
|
2024-01-08 08:29:14 +08:00
|
|
|
b += im.nbytes * ratio**2
|
2022-11-04 22:30:12 +08:00
|
|
|
mem_required = b * self.n / n # GB required to cache dataset into RAM
|
|
|
|
mem = psutil.virtual_memory()
|
|
|
|
cache = mem_required * (1 + safety_margin) < mem.available # to cache or not to cache, that is the question
|
|
|
|
if not cache:
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.info(
|
2025-01-10 08:42:04 +08:00
|
|
|
f"{prefix}{mem_required / gb:.1f}GB RAM required, "
|
|
|
|
f"{mem.available / gb:.1f}/{mem.total / gb:.1f}GB available, "
|
2024-01-08 08:29:14 +08:00
|
|
|
f"{'caching images ✅' if cache else 'not caching images ⚠️'}"
|
|
|
|
)
|
2022-11-04 22:30:12 +08:00
|
|
|
return cache
|
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
def cache_labels(self, path=Path("./labels.cache"), prefix=""):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Caches dataset labels, verifies images, reads shapes, and tracks dataset integrity."""
|
2020-07-10 11:07:16 +08:00
|
|
|
x = {} # dict
|
2021-06-18 16:21:47 +08:00
|
|
|
nm, nf, ne, nc, msgs = 0, 0, 0, 0, [] # number missing, found, empty, corrupt, messages
|
2024-01-08 08:29:14 +08:00
|
|
|
desc = f"{prefix}Scanning {path.parent / path.stem}..."
|
2021-07-19 16:43:01 +08:00
|
|
|
with Pool(NUM_THREADS) as pool:
|
2024-01-08 08:29:14 +08:00
|
|
|
pbar = tqdm(
|
|
|
|
pool.imap(verify_image_label, zip(self.im_files, self.label_files, repeat(prefix))),
|
|
|
|
desc=desc,
|
|
|
|
total=len(self.im_files),
|
|
|
|
bar_format=TQDM_BAR_FORMAT,
|
|
|
|
)
|
2022-02-05 22:22:59 +08:00
|
|
|
for im_file, lb, shape, segments, nm_f, nf_f, ne_f, nc_f, msg in pbar:
|
2021-06-09 00:36:40 +08:00
|
|
|
nm += nm_f
|
|
|
|
nf += nf_f
|
|
|
|
ne += ne_f
|
|
|
|
nc += nc_f
|
2021-06-09 00:00:21 +08:00
|
|
|
if im_file:
|
2022-02-05 22:22:59 +08:00
|
|
|
x[im_file] = [lb, shape, segments]
|
2021-06-18 16:21:47 +08:00
|
|
|
if msg:
|
|
|
|
msgs.append(msg)
|
2024-01-08 08:29:14 +08:00
|
|
|
pbar.desc = f"{desc} {nf} images, {nm + ne} backgrounds, {nc} corrupt"
|
2021-04-12 00:53:40 +08:00
|
|
|
|
2021-06-09 00:36:40 +08:00
|
|
|
pbar.close()
|
2021-06-18 16:21:47 +08:00
|
|
|
if msgs:
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.info("\n".join(msgs))
|
2020-11-24 23:13:04 +08:00
|
|
|
if nf == 0:
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.warning(f"{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
|
|
|
|
x["version"] = self.cache_version # cache version
|
2021-04-30 06:56:44 +08:00
|
|
|
try:
|
2021-07-10 20:18:46 +08:00
|
|
|
np.save(path, x) # save cache for next time
|
2024-01-08 08:29:14 +08:00
|
|
|
path.with_suffix(".cache.npy").rename(path) # remove .npy suffix
|
|
|
|
LOGGER.info(f"{prefix}New cache created: {path}")
|
2021-04-30 06:56:44 +08:00
|
|
|
except Exception as e:
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.warning(f"{prefix}WARNING ⚠️ Cache directory {path.parent} is not writeable: {e}") # not writeable
|
2020-07-10 11:07:16 +08:00
|
|
|
return x
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
def __len__(self):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Returns the number of images in the dataset."""
|
2022-03-06 23:16:17 +08:00
|
|
|
return len(self.im_files)
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
# def __iter__(self):
|
|
|
|
# self.count = -1
|
2020-11-24 23:13:04 +08:00
|
|
|
# print('ran dataset iter')
|
2020-05-30 08:04:54 +08:00
|
|
|
# #self.shuffled_vector = np.random.permutation(self.nF) if self.augment else np.arange(self.nF)
|
|
|
|
# return self
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Fetches the dataset item at the given index, considering linear, shuffled, or weighted sampling."""
|
2020-11-26 18:49:01 +08:00
|
|
|
index = self.indices[index] # linear, shuffled, or image_weights
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
hyp = self.hyp
|
2025-01-10 08:42:04 +08:00
|
|
|
if mosaic := self.mosaic and random.random() < hyp["mosaic"]:
|
2020-05-30 08:04:54 +08:00
|
|
|
# Load mosaic
|
2022-02-08 01:59:26 +08:00
|
|
|
img, labels = self.load_mosaic(index)
|
2020-05-30 08:04:54 +08:00
|
|
|
shapes = None
|
|
|
|
|
2021-07-05 00:14:04 +08:00
|
|
|
# MixUp augmentation
|
2024-01-08 08:29:14 +08:00
|
|
|
if random.random() < hyp["mixup"]:
|
2024-01-03 15:15:07 +08:00
|
|
|
img, labels = mixup(img, labels, *self.load_mosaic(random.choice(self.indices)))
|
2021-07-05 00:14:04 +08:00
|
|
|
|
2020-05-30 08:04:54 +08:00
|
|
|
else:
|
|
|
|
# Load image
|
2022-02-08 01:59:26 +08:00
|
|
|
img, (h0, w0), (h, w) = self.load_image(index)
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
# Letterbox
|
|
|
|
shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape
|
|
|
|
img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment)
|
|
|
|
shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling
|
|
|
|
|
2021-01-20 05:33:52 +08:00
|
|
|
labels = self.labels[index].copy()
|
|
|
|
if labels.size: # normalized xywh to pixel xyxy format
|
|
|
|
labels[:, 1:] = xywhn2xyxy(labels[:, 1:], ratio[0] * w, ratio[1] * h, padw=pad[0], padh=pad[1])
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2021-07-06 00:01:54 +08:00
|
|
|
if self.augment:
|
2024-01-08 08:29:14 +08:00
|
|
|
img, labels = random_perspective(
|
|
|
|
img,
|
|
|
|
labels,
|
|
|
|
degrees=hyp["degrees"],
|
|
|
|
translate=hyp["translate"],
|
|
|
|
scale=hyp["scale"],
|
|
|
|
shear=hyp["shear"],
|
|
|
|
perspective=hyp["perspective"],
|
|
|
|
)
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2021-07-06 00:01:54 +08:00
|
|
|
nl = len(labels) # number of labels
|
|
|
|
if nl:
|
2024-01-08 08:29:14 +08:00
|
|
|
labels[:, 1:5] = xyxy2xywhn(labels[:, 1:5], w=img.shape[1], h=img.shape[0], clip=True, eps=1e-3)
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
if self.augment:
|
2021-07-06 00:01:54 +08:00
|
|
|
# Albumentations
|
|
|
|
img, labels = self.albumentations(img, labels)
|
2021-08-26 21:51:04 +08:00
|
|
|
nl = len(labels) # update after albumentations
|
2021-07-06 00:01:54 +08:00
|
|
|
|
|
|
|
# HSV color-space
|
2024-01-08 08:29:14 +08:00
|
|
|
augment_hsv(img, hgain=hyp["hsv_h"], sgain=hyp["hsv_s"], vgain=hyp["hsv_v"])
|
2021-07-06 00:01:54 +08:00
|
|
|
|
|
|
|
# Flip up-down
|
2024-01-08 08:29:14 +08:00
|
|
|
if random.random() < hyp["flipud"]:
|
2020-05-30 08:04:54 +08:00
|
|
|
img = np.flipud(img)
|
2021-07-06 00:01:54 +08:00
|
|
|
if nl:
|
2020-05-30 08:04:54 +08:00
|
|
|
labels[:, 2] = 1 - labels[:, 2]
|
|
|
|
|
2021-07-06 00:01:54 +08:00
|
|
|
# Flip left-right
|
2024-01-08 08:29:14 +08:00
|
|
|
if random.random() < hyp["fliplr"]:
|
2020-08-02 04:47:54 +08:00
|
|
|
img = np.fliplr(img)
|
2021-07-06 00:01:54 +08:00
|
|
|
if nl:
|
2020-08-02 04:47:54 +08:00
|
|
|
labels[:, 1] = 1 - labels[:, 1]
|
|
|
|
|
2021-07-06 00:01:54 +08:00
|
|
|
# Cutouts
|
2021-07-12 21:54:43 +08:00
|
|
|
# labels = cutout(img, labels, p=0.5)
|
2021-12-03 22:28:14 +08:00
|
|
|
# nl = len(labels) # update after cutout
|
2021-07-06 00:01:54 +08:00
|
|
|
|
|
|
|
labels_out = torch.zeros((nl, 6))
|
|
|
|
if nl:
|
2020-05-30 08:04:54 +08:00
|
|
|
labels_out[:, 1:] = torch.from_numpy(labels)
|
|
|
|
|
|
|
|
# Convert
|
2021-07-05 02:12:32 +08:00
|
|
|
img = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
|
2020-05-30 08:04:54 +08:00
|
|
|
img = np.ascontiguousarray(img)
|
|
|
|
|
2022-03-06 23:16:17 +08:00
|
|
|
return torch.from_numpy(img), labels_out, self.im_files[index], shapes
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2022-02-08 01:59:26 +08:00
|
|
|
def load_image(self, i):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""
|
|
|
|
Loads an image by index, returning the image, its original dimensions, and resized dimensions.
|
|
|
|
|
|
|
|
Returns (im, original hw, resized hw)
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
im, f, fn = (
|
|
|
|
self.ims[i],
|
|
|
|
self.im_files[i],
|
|
|
|
self.npy_files[i],
|
|
|
|
)
|
2022-02-08 01:59:26 +08:00
|
|
|
if im is None: # not cached in RAM
|
2022-03-06 23:16:17 +08:00
|
|
|
if fn.exists(): # load npy
|
|
|
|
im = np.load(fn)
|
2022-02-08 01:59:26 +08:00
|
|
|
else: # read image
|
|
|
|
im = cv2.imread(f) # BGR
|
2024-01-08 08:29:14 +08:00
|
|
|
assert im is not None, f"Image Not Found {f}"
|
2022-02-08 01:59:26 +08:00
|
|
|
h0, w0 = im.shape[:2] # orig hw
|
|
|
|
r = self.img_size / max(h0, w0) # ratio
|
|
|
|
if r != 1: # if sizes are not equal
|
2022-05-20 18:59:05 +08:00
|
|
|
interp = cv2.INTER_LINEAR if (self.augment or r > 1) else cv2.INTER_AREA
|
2022-12-22 21:55:09 +08:00
|
|
|
im = cv2.resize(im, (math.ceil(w0 * r), math.ceil(h0 * r)), interpolation=interp)
|
2022-02-08 01:59:26 +08:00
|
|
|
return im, (h0, w0), im.shape[:2] # im, hw_original, hw_resized
|
2022-07-25 06:02:09 +08:00
|
|
|
return self.ims[i], self.im_hw0[i], self.im_hw[i] # im, hw_original, hw_resized
|
2022-03-06 23:16:17 +08:00
|
|
|
|
|
|
|
def cache_images_to_disk(self, i):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Saves an image to disk as an *.npy file for quicker loading, identified by index `i`."""
|
2022-03-06 23:16:17 +08:00
|
|
|
f = self.npy_files[i]
|
|
|
|
if not f.exists():
|
|
|
|
np.save(f.as_posix(), cv2.imread(self.im_files[i]))
|
2022-02-08 01:59:26 +08:00
|
|
|
|
|
|
|
def load_mosaic(self, index):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Loads a 4-image mosaic for YOLOv5, combining 1 selected and 3 random images, with labels and segments."""
|
2022-02-08 01:59:26 +08:00
|
|
|
labels4, segments4 = [], []
|
|
|
|
s = self.img_size
|
|
|
|
yc, xc = (int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border) # mosaic center x, y
|
|
|
|
indices = [index] + random.choices(self.indices, k=3) # 3 additional image indices
|
|
|
|
random.shuffle(indices)
|
|
|
|
for i, index in enumerate(indices):
|
|
|
|
# Load image
|
|
|
|
img, _, (h, w) = self.load_image(index)
|
|
|
|
|
|
|
|
# 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
|
|
|
|
labels, segments = self.labels[index].copy(), self.segments[index].copy()
|
|
|
|
if labels.size:
|
|
|
|
labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padw, padh) # normalized xywh to pixel xyxy format
|
|
|
|
segments = [xyn2xy(x, w, h, padw, padh) for x in segments]
|
|
|
|
labels4.append(labels)
|
|
|
|
segments4.extend(segments)
|
|
|
|
|
|
|
|
# Concat/clip labels
|
|
|
|
labels4 = np.concatenate(labels4, 0)
|
|
|
|
for x in (labels4[:, 1:], *segments4):
|
|
|
|
np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective()
|
|
|
|
# img4, labels4 = replicate(img4, labels4) # replicate
|
|
|
|
|
|
|
|
# Augment
|
2024-01-08 08:29:14 +08:00
|
|
|
img4, labels4, segments4 = copy_paste(img4, labels4, segments4, p=self.hyp["copy_paste"])
|
|
|
|
img4, labels4 = random_perspective(
|
|
|
|
img4,
|
|
|
|
labels4,
|
|
|
|
segments4,
|
|
|
|
degrees=self.hyp["degrees"],
|
|
|
|
translate=self.hyp["translate"],
|
|
|
|
scale=self.hyp["scale"],
|
|
|
|
shear=self.hyp["shear"],
|
|
|
|
perspective=self.hyp["perspective"],
|
|
|
|
border=self.mosaic_border,
|
|
|
|
) # border to remove
|
2022-02-08 01:59:26 +08:00
|
|
|
|
|
|
|
return img4, labels4
|
|
|
|
|
|
|
|
def load_mosaic9(self, index):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Loads 1 image + 8 random images into a 9-image mosaic for augmented YOLOv5 training, returning labels and
|
|
|
|
segments.
|
|
|
|
"""
|
2022-02-08 01:59:26 +08:00
|
|
|
labels9, segments9 = [], []
|
|
|
|
s = self.img_size
|
|
|
|
indices = [index] + random.choices(self.indices, k=8) # 8 additional image indices
|
|
|
|
random.shuffle(indices)
|
|
|
|
hp, wp = -1, -1 # height, width previous
|
|
|
|
for i, index in enumerate(indices):
|
|
|
|
# Load image
|
|
|
|
img, _, (h, w) = self.load_image(index)
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
padx, pady = c[:2]
|
|
|
|
x1, y1, x2, y2 = (max(x, 0) for x in c) # allocate coords
|
|
|
|
|
|
|
|
# Labels
|
|
|
|
labels, segments = self.labels[index].copy(), self.segments[index].copy()
|
|
|
|
if labels.size:
|
|
|
|
labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padx, pady) # normalized xywh to pixel xyxy format
|
|
|
|
segments = [xyn2xy(x, w, h, padx, pady) for x in segments]
|
|
|
|
labels9.append(labels)
|
|
|
|
segments9.extend(segments)
|
|
|
|
|
|
|
|
# Image
|
2024-01-08 08:29:14 +08:00
|
|
|
img9[y1:y2, x1:x2] = img[y1 - pady :, x1 - padx :] # img9[ymin:ymax, xmin:xmax]
|
2022-02-08 01:59:26 +08:00
|
|
|
hp, wp = h, w # height, width previous
|
|
|
|
|
|
|
|
# Offset
|
|
|
|
yc, xc = (int(random.uniform(0, s)) for _ in self.mosaic_border) # mosaic center x, y
|
2024-01-08 08:29:14 +08:00
|
|
|
img9 = img9[yc : yc + 2 * s, xc : xc + 2 * s]
|
2022-02-08 01:59:26 +08:00
|
|
|
|
|
|
|
# Concat/clip labels
|
|
|
|
labels9 = np.concatenate(labels9, 0)
|
|
|
|
labels9[:, [1, 3]] -= xc
|
|
|
|
labels9[:, [2, 4]] -= yc
|
|
|
|
c = np.array([xc, yc]) # centers
|
|
|
|
segments9 = [x - c for x in segments9]
|
|
|
|
|
|
|
|
for x in (labels9[:, 1:], *segments9):
|
|
|
|
np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective()
|
|
|
|
# img9, labels9 = replicate(img9, labels9) # replicate
|
|
|
|
|
|
|
|
# Augment
|
2024-01-08 08:29:14 +08:00
|
|
|
img9, labels9, segments9 = copy_paste(img9, labels9, segments9, p=self.hyp["copy_paste"])
|
|
|
|
img9, labels9 = random_perspective(
|
|
|
|
img9,
|
|
|
|
labels9,
|
|
|
|
segments9,
|
|
|
|
degrees=self.hyp["degrees"],
|
|
|
|
translate=self.hyp["translate"],
|
|
|
|
scale=self.hyp["scale"],
|
|
|
|
shear=self.hyp["shear"],
|
|
|
|
perspective=self.hyp["perspective"],
|
|
|
|
border=self.mosaic_border,
|
|
|
|
) # border to remove
|
2022-02-08 01:59:26 +08:00
|
|
|
|
|
|
|
return img9, labels9
|
|
|
|
|
2020-05-30 08:04:54 +08:00
|
|
|
@staticmethod
|
|
|
|
def collate_fn(batch):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Batches images, labels, paths, and shapes, assigning unique indices to targets in merged label tensor."""
|
2022-03-06 23:16:17 +08:00
|
|
|
im, label, path, shapes = zip(*batch) # transposed
|
2022-02-05 22:22:59 +08:00
|
|
|
for i, lb in enumerate(label):
|
|
|
|
lb[:, 0] = i # add target image index for build_targets()
|
2022-03-06 23:16:17 +08:00
|
|
|
return torch.stack(im, 0), torch.cat(label, 0), path, shapes
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2021-01-05 11:54:09 +08:00
|
|
|
@staticmethod
|
|
|
|
def collate_fn4(batch):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Bundles a batch's data by quartering the number of shapes and paths, preparing it for model input."""
|
2022-08-23 20:37:46 +08:00
|
|
|
im, label, path, shapes = zip(*batch) # transposed
|
2021-01-05 11:54:09 +08:00
|
|
|
n = len(shapes) // 4
|
2022-03-06 23:16:17 +08:00
|
|
|
im4, label4, path4, shapes4 = [], [], path[:n], shapes[:n]
|
2021-01-05 11:54:09 +08:00
|
|
|
|
2021-11-04 06:36:53 +08:00
|
|
|
ho = torch.tensor([[0.0, 0, 0, 1, 0, 0]])
|
|
|
|
wo = torch.tensor([[0.0, 0, 1, 0, 0, 0]])
|
|
|
|
s = torch.tensor([[1, 1, 0.5, 0.5, 0.5, 0.5]]) # scale
|
2021-01-05 11:54:09 +08:00
|
|
|
for i in range(n): # zidane torch.zeros(16,3,720,1280) # BCHW
|
|
|
|
i *= 4
|
|
|
|
if random.random() < 0.5:
|
2024-01-08 08:29:14 +08:00
|
|
|
im1 = F.interpolate(im[i].unsqueeze(0).float(), scale_factor=2.0, mode="bilinear", align_corners=False)[
|
|
|
|
0
|
|
|
|
].type(im[i].type())
|
2022-02-05 22:22:59 +08:00
|
|
|
lb = label[i]
|
2021-01-05 11:54:09 +08:00
|
|
|
else:
|
2022-08-23 20:37:46 +08:00
|
|
|
im1 = torch.cat((torch.cat((im[i], im[i + 1]), 1), torch.cat((im[i + 2], im[i + 3]), 1)), 2)
|
2022-02-05 22:22:59 +08:00
|
|
|
lb = torch.cat((label[i], label[i + 1] + ho, label[i + 2] + wo, label[i + 3] + ho + wo), 0) * s
|
2022-08-23 20:37:46 +08:00
|
|
|
im4.append(im1)
|
2022-02-05 22:22:59 +08:00
|
|
|
label4.append(lb)
|
2021-01-05 11:54:09 +08:00
|
|
|
|
2022-02-05 22:22:59 +08:00
|
|
|
for i, lb in enumerate(label4):
|
|
|
|
lb[:, 0] = i # add target image index for build_targets()
|
2021-01-05 11:54:09 +08:00
|
|
|
|
2022-03-06 23:16:17 +08:00
|
|
|
return torch.stack(im4, 0), torch.cat(label4, 0), path4, shapes4
|
2021-01-05 11:54:09 +08:00
|
|
|
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2020-07-24 13:49:54 +08:00
|
|
|
# Ancillary functions --------------------------------------------------------------------------------------------------
|
2024-01-08 08:29:14 +08:00
|
|
|
def flatten_recursive(path=DATASETS_DIR / "coco128"):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Flattens a directory by copying all files from subdirectories to a new top-level directory, preserving
|
|
|
|
filenames.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
new_path = Path(f"{str(path)}_flat")
|
2022-07-25 22:52:28 +08:00
|
|
|
if os.path.exists(new_path):
|
|
|
|
shutil.rmtree(new_path) # delete output folder
|
|
|
|
os.makedirs(new_path) # make new output folder
|
2024-01-08 08:29:14 +08:00
|
|
|
for file in tqdm(glob.glob(f"{str(Path(path))}/**/*.*", recursive=True)):
|
2020-11-09 19:24:11 +08:00
|
|
|
shutil.copyfile(file, new_path / Path(file).name)
|
2020-11-24 00:18:21 +08:00
|
|
|
|
|
|
|
|
2024-02-27 06:16:32 +08:00
|
|
|
def extract_boxes(path=DATASETS_DIR / "coco128"):
|
|
|
|
"""
|
|
|
|
Converts a detection dataset to a classification dataset, creating a directory for each class and extracting
|
|
|
|
bounding boxes.
|
|
|
|
|
|
|
|
Example: from utils.dataloaders import *; extract_boxes()
|
|
|
|
"""
|
2020-11-24 23:13:04 +08:00
|
|
|
path = Path(path) # images dir
|
2024-01-08 08:29:14 +08:00
|
|
|
shutil.rmtree(path / "classification") if (path / "classification").is_dir() else None # remove existing
|
|
|
|
files = list(path.rglob("*.*"))
|
2020-11-24 23:13:04 +08:00
|
|
|
n = len(files) # number of files
|
|
|
|
for im_file in tqdm(files, total=n):
|
2021-07-19 16:43:01 +08:00
|
|
|
if im_file.suffix[1:] in IMG_FORMATS:
|
2020-11-24 23:13:04 +08:00
|
|
|
# image
|
|
|
|
im = cv2.imread(str(im_file))[..., ::-1] # BGR to RGB
|
|
|
|
h, w = im.shape[:2]
|
|
|
|
|
|
|
|
# labels
|
|
|
|
lb_file = Path(img2label_paths([str(im_file)])[0])
|
|
|
|
if Path(lb_file).exists():
|
2021-10-29 00:35:01 +08:00
|
|
|
with open(lb_file) as f:
|
2020-11-29 18:59:52 +08:00
|
|
|
lb = np.array([x.split() for x in f.read().strip().splitlines()], dtype=np.float32) # labels
|
2020-11-24 23:13:04 +08:00
|
|
|
|
|
|
|
for j, x in enumerate(lb):
|
|
|
|
c = int(x[0]) # class
|
2024-03-01 19:32:29 +08:00
|
|
|
f = (path / "classification") / f"{c}" / f"{path.stem}_{im_file.stem}_{j}.jpg" # new filename
|
2020-11-24 23:13:04 +08:00
|
|
|
if not f.parent.is_dir():
|
|
|
|
f.parent.mkdir(parents=True)
|
|
|
|
|
|
|
|
b = x[1:] * [w, h, w, h] # box
|
|
|
|
# b[2:] = b[2:].max() # rectangle to square
|
|
|
|
b[2:] = b[2:] * 1.2 + 3 # pad
|
2022-09-07 15:11:30 +08:00
|
|
|
b = xywh2xyxy(b.reshape(-1, 4)).ravel().astype(int)
|
2020-11-24 23:13:04 +08:00
|
|
|
|
|
|
|
b[[0, 2]] = np.clip(b[[0, 2]], 0, w) # clip boxes outside of image
|
|
|
|
b[[1, 3]] = np.clip(b[[1, 3]], 0, h)
|
2024-01-08 08:29:14 +08:00
|
|
|
assert cv2.imwrite(str(f), im[b[1] : b[3], b[0] : b[2]]), f"box failure in {f}"
|
2020-11-24 23:13:04 +08:00
|
|
|
|
2021-04-12 00:53:40 +08:00
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
def autosplit(path=DATASETS_DIR / "coco128/images", weights=(0.9, 0.1, 0.0), annotated_only=False):
|
|
|
|
"""Autosplit a dataset into train/val/test splits and save path/autosplit_*.txt files
|
2024-08-26 08:37:25 +08:00
|
|
|
Usage: from utils.dataloaders import *; autosplit().
|
2024-08-25 05:45:33 +08:00
|
|
|
|
|
|
|
Arguments:
|
2021-06-26 06:49:05 +08:00
|
|
|
path: Path to images directory
|
|
|
|
weights: Train, val, test weights (list, tuple)
|
|
|
|
annotated_only: Only use images with an annotated txt file
|
2020-11-24 00:18:21 +08:00
|
|
|
"""
|
|
|
|
path = Path(path) # images dir
|
2024-01-08 08:29:14 +08:00
|
|
|
files = sorted(x for x in path.rglob("*.*") if x.suffix[1:].lower() in IMG_FORMATS) # image files only
|
2020-11-24 01:35:25 +08:00
|
|
|
n = len(files) # number of files
|
2021-06-26 06:49:05 +08:00
|
|
|
random.seed(0) # for reproducibility
|
2020-11-24 01:35:25 +08:00
|
|
|
indices = random.choices([0, 1, 2], weights=weights, k=n) # assign each image to a split
|
Update autosplit() with annotated_only option (#2466)
* Be able to create dataset from annotated images only
Add the ability to create a dataset/splits only with images that have an annotation file, i.e a .txt file, associated to it. As we talked about this, the absence of a txt file could mean two things:
* either the image wasn't yet labelled by someone,
* either there is no object to detect.
When it's easy to create small datasets, when you have to create datasets with thousands of images (and more coming), it's hard to track where you at and you don't want to wait to have all of them annotated before starting to train. Which means some images would lack txt files and annotations, resulting in label inconsistency as you say in #2313. By adding the annotated_only argument to the function, people could create, if they want to, datasets/splits only with images that were labelled, for sure.
* Cleanup and update print()
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
2021-03-15 08:11:27 +08:00
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
txt = ["autosplit_train.txt", "autosplit_val.txt", "autosplit_test.txt"] # 3 txt files
|
2022-08-31 03:57:36 +08:00
|
|
|
for x in txt:
|
|
|
|
if (path.parent / x).exists():
|
|
|
|
(path.parent / x).unlink() # remove existing
|
Update autosplit() with annotated_only option (#2466)
* Be able to create dataset from annotated images only
Add the ability to create a dataset/splits only with images that have an annotation file, i.e a .txt file, associated to it. As we talked about this, the absence of a txt file could mean two things:
* either the image wasn't yet labelled by someone,
* either there is no object to detect.
When it's easy to create small datasets, when you have to create datasets with thousands of images (and more coming), it's hard to track where you at and you don't want to wait to have all of them annotated before starting to train. Which means some images would lack txt files and annotations, resulting in label inconsistency as you say in #2313. By adding the annotated_only argument to the function, people could create, if they want to, datasets/splits only with images that were labelled, for sure.
* Cleanup and update print()
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
2021-03-15 08:11:27 +08:00
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
print(f"Autosplitting images from {path}" + ", using *.txt labeled images only" * annotated_only)
|
2020-11-24 01:35:25 +08:00
|
|
|
for i, img in tqdm(zip(indices, files), total=n):
|
Update autosplit() with annotated_only option (#2466)
* Be able to create dataset from annotated images only
Add the ability to create a dataset/splits only with images that have an annotation file, i.e a .txt file, associated to it. As we talked about this, the absence of a txt file could mean two things:
* either the image wasn't yet labelled by someone,
* either there is no object to detect.
When it's easy to create small datasets, when you have to create datasets with thousands of images (and more coming), it's hard to track where you at and you don't want to wait to have all of them annotated before starting to train. Which means some images would lack txt files and annotations, resulting in label inconsistency as you say in #2313. By adding the annotated_only argument to the function, people could create, if they want to, datasets/splits only with images that were labelled, for sure.
* Cleanup and update print()
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
2021-03-15 08:11:27 +08:00
|
|
|
if not annotated_only or Path(img2label_paths([str(img)])[0]).exists(): # check label
|
2024-01-08 08:29:14 +08:00
|
|
|
with open(path.parent / txt[i], "a") as f:
|
|
|
|
f.write(f"./{img.relative_to(path.parent).as_posix()}" + "\n") # add image to txt file
|
2021-06-09 00:00:21 +08:00
|
|
|
|
|
|
|
|
2021-06-16 17:12:15 +08:00
|
|
|
def verify_image_label(args):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Verifies a single image-label pair, ensuring image format, size, and legal label values."""
|
2021-06-16 17:12:15 +08:00
|
|
|
im_file, lb_file, prefix = args
|
2024-01-08 08:29:14 +08:00
|
|
|
nm, nf, ne, nc, msg, segments = 0, 0, 0, 0, "", [] # number (missing, found, empty, corrupt), message, segments
|
2021-06-09 00:00:21 +08:00
|
|
|
try:
|
|
|
|
# verify images
|
|
|
|
im = Image.open(im_file)
|
|
|
|
im.verify() # PIL verify
|
|
|
|
shape = exif_size(im) # image size
|
2024-01-08 08:29:14 +08:00
|
|
|
assert (shape[0] > 9) & (shape[1] > 9), f"image size {shape} <10 pixels"
|
|
|
|
assert im.format.lower() in IMG_FORMATS, f"invalid image format {im.format}"
|
|
|
|
if im.format.lower() in ("jpg", "jpeg"):
|
|
|
|
with open(im_file, "rb") as f:
|
2021-06-16 19:31:26 +08:00
|
|
|
f.seek(-2, 2)
|
2024-01-08 08:29:14 +08:00
|
|
|
if f.read() != b"\xff\xd9": # corrupt JPEG
|
|
|
|
ImageOps.exif_transpose(Image.open(im_file)).save(im_file, "JPEG", subsampling=0, quality=100)
|
|
|
|
msg = f"{prefix}WARNING ⚠️ {im_file}: corrupt JPEG restored and saved"
|
2021-06-09 00:00:21 +08:00
|
|
|
|
|
|
|
# verify labels
|
|
|
|
if os.path.isfile(lb_file):
|
|
|
|
nf = 1 # label found
|
2021-10-29 00:35:01 +08:00
|
|
|
with open(lb_file) as f:
|
2022-02-05 22:22:59 +08:00
|
|
|
lb = [x.split() for x in f.read().strip().splitlines() if len(x)]
|
2022-03-11 01:41:47 +08:00
|
|
|
if any(len(x) > 6 for x in lb): # is segment
|
2022-02-05 22:22:59 +08:00
|
|
|
classes = np.array([x[0] for x in lb], dtype=np.float32)
|
|
|
|
segments = [np.array(x[1:], dtype=np.float32).reshape(-1, 2) for x in lb] # (cls, xy1...)
|
|
|
|
lb = np.concatenate((classes.reshape(-1, 1), segments2boxes(segments)), 1) # (cls, xywh)
|
|
|
|
lb = np.array(lb, dtype=np.float32)
|
2025-01-10 08:42:04 +08:00
|
|
|
if nl := len(lb):
|
2024-01-08 08:29:14 +08:00
|
|
|
assert lb.shape[1] == 5, f"labels require 5 columns, {lb.shape[1]} columns detected"
|
|
|
|
assert (lb >= 0).all(), f"negative label values {lb[lb < 0]}"
|
|
|
|
assert (lb[:, 1:] <= 1).all(), f"non-normalized or out of bounds coordinates {lb[:, 1:][lb[:, 1:] > 1]}"
|
2022-02-05 22:22:59 +08:00
|
|
|
_, i = np.unique(lb, axis=0, return_index=True)
|
2021-11-11 07:15:17 +08:00
|
|
|
if len(i) < nl: # duplicate row check
|
2022-02-05 22:22:59 +08:00
|
|
|
lb = lb[i] # remove duplicates
|
2021-11-11 07:15:17 +08:00
|
|
|
if segments:
|
2022-08-30 19:40:51 +08:00
|
|
|
segments = [segments[x] for x in i]
|
2024-01-08 08:29:14 +08:00
|
|
|
msg = f"{prefix}WARNING ⚠️ {im_file}: {nl - len(i)} duplicate labels removed"
|
2021-06-09 00:00:21 +08:00
|
|
|
else:
|
|
|
|
ne = 1 # label empty
|
2022-02-05 22:22:59 +08:00
|
|
|
lb = np.zeros((0, 5), dtype=np.float32)
|
2021-06-09 00:00:21 +08:00
|
|
|
else:
|
|
|
|
nm = 1 # label missing
|
2022-02-05 22:22:59 +08:00
|
|
|
lb = np.zeros((0, 5), dtype=np.float32)
|
|
|
|
return im_file, lb, shape, segments, nm, nf, ne, nc, msg
|
2021-06-09 00:00:21 +08:00
|
|
|
except Exception as e:
|
|
|
|
nc = 1
|
2024-01-08 08:29:14 +08:00
|
|
|
msg = f"{prefix}WARNING ⚠️ {im_file}: ignoring corrupt image/label: {e}"
|
2021-06-18 16:21:47 +08:00
|
|
|
return [None, None, None, None, nm, nf, ne, nc, msg]
|
2021-06-09 05:09:45 +08:00
|
|
|
|
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
class HUBDatasetStats:
|
|
|
|
"""
|
|
|
|
Class for generating HUB dataset JSON and `-hub` dataset directory.
|
2022-10-18 21:25:21 +08:00
|
|
|
|
2024-08-25 05:45:33 +08:00
|
|
|
Arguments:
|
2021-07-08 17:42:30 +08:00
|
|
|
path: Path to data.yaml or data.zip (with data.yaml inside data.zip)
|
2021-06-09 16:56:11 +08:00
|
|
|
autodownload: Attempt to download dataset if not found locally
|
2022-10-18 21:25:21 +08:00
|
|
|
|
|
|
|
Usage
|
|
|
|
from utils.dataloaders import HUBDatasetStats
|
|
|
|
stats = HUBDatasetStats('coco128.yaml', autodownload=True) # usage 1
|
|
|
|
stats = HUBDatasetStats('path/to/coco128.zip') # usage 2
|
|
|
|
stats.get_json(save=False)
|
|
|
|
stats.process_images()
|
2021-06-09 05:09:45 +08:00
|
|
|
"""
|
2022-04-05 04:47:00 +08:00
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
def __init__(self, path="coco128.yaml", autodownload=False):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Initializes HUBDatasetStats with optional auto-download for datasets, given a path to dataset YAML or ZIP
|
|
|
|
file.
|
|
|
|
"""
|
2022-07-26 00:20:01 +08:00
|
|
|
zipped, data_dir, yaml_path = self._unzip(Path(path))
|
|
|
|
try:
|
2024-01-08 08:29:14 +08:00
|
|
|
with open(check_yaml(yaml_path), errors="ignore") as f:
|
2022-07-26 00:20:01 +08:00
|
|
|
data = yaml.safe_load(f) # data dict
|
|
|
|
if zipped:
|
2024-01-08 08:29:14 +08:00
|
|
|
data["path"] = data_dir
|
2022-07-26 00:20:01 +08:00
|
|
|
except Exception as e:
|
2024-01-08 08:29:14 +08:00
|
|
|
raise Exception("error/HUB/dataset_stats/yaml_load") from e
|
2022-07-26 00:20:01 +08:00
|
|
|
|
|
|
|
check_dataset(data, autodownload) # download dataset if missing
|
2024-01-08 08:29:14 +08:00
|
|
|
self.hub_dir = Path(data["path"] + "-hub")
|
|
|
|
self.im_dir = self.hub_dir / "images"
|
2022-07-26 00:20:01 +08:00
|
|
|
self.im_dir.mkdir(parents=True, exist_ok=True) # makes /images
|
2024-01-08 08:29:14 +08:00
|
|
|
self.stats = {"nc": data["nc"], "names": list(data["names"].values())} # statistics dictionary
|
2022-07-26 00:20:01 +08:00
|
|
|
self.data = data
|
2021-06-17 19:59:52 +08:00
|
|
|
|
2022-07-26 00:20:01 +08:00
|
|
|
@staticmethod
|
2022-06-12 01:30:54 +08:00
|
|
|
def _find_yaml(dir):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Finds and returns the path to a single '.yaml' file in the specified directory, preferring files that match
|
|
|
|
the directory name.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
files = list(dir.glob("*.yaml")) or list(dir.rglob("*.yaml")) # try root level first and then recursive
|
|
|
|
assert files, f"No *.yaml file found in {dir}"
|
2022-06-12 01:30:54 +08:00
|
|
|
if len(files) > 1:
|
|
|
|
files = [f for f in files if f.stem == dir.stem] # prefer *.yaml files that match dir name
|
2024-01-08 08:29:14 +08:00
|
|
|
assert files, f"Multiple *.yaml files found in {dir}, only 1 *.yaml file allowed"
|
|
|
|
assert len(files) == 1, f"Multiple *.yaml files found: {files}, only 1 *.yaml file allowed in {dir}"
|
2022-06-12 01:30:54 +08:00
|
|
|
return files[0]
|
|
|
|
|
2022-07-26 00:20:01 +08:00
|
|
|
def _unzip(self, path):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Unzips a .zip file at 'path', returning success status, unzipped directory, and path to YAML file within."""
|
2024-01-08 08:29:14 +08:00
|
|
|
if not str(path).endswith(".zip"): # path is data.yaml
|
2021-07-08 17:42:30 +08:00
|
|
|
return False, None, path
|
2024-01-08 08:29:14 +08:00
|
|
|
assert Path(path).is_file(), f"Error unzipping {path}, file not found"
|
2022-10-18 22:35:04 +08:00
|
|
|
unzip_file(path, path=path.parent)
|
2024-01-08 08:29:14 +08:00
|
|
|
dir = path.with_suffix("") # dataset directory == zip name
|
|
|
|
assert dir.is_dir(), f"Error unzipping {path}, {dir} not found. path/to/abc.zip MUST unzip to path/to/abc/"
|
2022-07-26 00:20:01 +08:00
|
|
|
return True, str(dir), self._find_yaml(dir) # zipped, data_dir, yaml_path
|
2021-07-08 17:42:30 +08:00
|
|
|
|
2022-07-26 00:20:01 +08:00
|
|
|
def _hub_ops(self, f, max_dim=1920):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Resizes and saves an image at reduced quality for web/app viewing, supporting both PIL and OpenCV."""
|
2022-07-26 00:20:01 +08:00
|
|
|
f_new = self.im_dir / Path(f).name # dataset-hub image filename
|
2021-10-09 15:12:59 +08:00
|
|
|
try: # use PIL
|
|
|
|
im = Image.open(f)
|
|
|
|
r = max_dim / max(im.height, im.width) # ratio
|
|
|
|
if r < 1.0: # image too large
|
|
|
|
im = im.resize((int(im.width * r), int(im.height * r)))
|
2024-01-08 08:29:14 +08:00
|
|
|
im.save(f_new, "JPEG", quality=50, optimize=True) # save
|
2021-10-09 15:12:59 +08:00
|
|
|
except Exception as e: # use OpenCV
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.info(f"WARNING ⚠️ HUB ops PIL failure {f}: {e}")
|
2021-10-09 15:12:59 +08:00
|
|
|
im = cv2.imread(f)
|
|
|
|
im_height, im_width = im.shape[:2]
|
|
|
|
r = max_dim / max(im_height, im_width) # ratio
|
|
|
|
if r < 1.0: # image too large
|
2021-11-29 20:15:36 +08:00
|
|
|
im = cv2.resize(im, (int(im_width * r), int(im_height * r)), interpolation=cv2.INTER_AREA)
|
2021-10-09 15:12:59 +08:00
|
|
|
cv2.imwrite(str(f_new), im)
|
2021-07-28 08:04:10 +08:00
|
|
|
|
2022-07-26 00:20:01 +08:00
|
|
|
def get_json(self, save=False, verbose=False):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Generates dataset JSON for Ultralytics HUB, optionally saves or prints it; save=bool, verbose=bool."""
|
|
|
|
|
2022-07-26 00:20:01 +08:00
|
|
|
def _round(labels):
|
2024-02-27 06:16:32 +08:00
|
|
|
"""Rounds class labels to integers and coordinates to 4 decimal places for improved label accuracy."""
|
2022-07-26 00:20:01 +08:00
|
|
|
return [[int(c), *(round(x, 4) for x in points)] for c, *points in labels]
|
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
for split in "train", "val", "test":
|
2022-07-26 00:20:01 +08:00
|
|
|
if self.data.get(split) is None:
|
|
|
|
self.stats[split] = None # i.e. no test set
|
|
|
|
continue
|
|
|
|
dataset = LoadImagesAndLabels(self.data[split]) # load dataset
|
2024-01-08 08:29:14 +08:00
|
|
|
x = np.array(
|
|
|
|
[
|
|
|
|
np.bincount(label[:, 0].astype(int), minlength=self.data["nc"])
|
|
|
|
for label in tqdm(dataset.labels, total=dataset.n, desc="Statistics")
|
|
|
|
]
|
|
|
|
) # shape(128x80)
|
2022-07-26 00:20:01 +08:00
|
|
|
self.stats[split] = {
|
2024-01-08 08:29:14 +08:00
|
|
|
"instance_stats": {"total": int(x.sum()), "per_class": x.sum(0).tolist()},
|
|
|
|
"image_stats": {
|
|
|
|
"total": dataset.n,
|
|
|
|
"unlabelled": int(np.all(x == 0, 1).sum()),
|
|
|
|
"per_class": (x > 0).sum(0).tolist(),
|
|
|
|
},
|
|
|
|
"labels": [{str(Path(k).name): _round(v.tolist())} for k, v in zip(dataset.im_files, dataset.labels)],
|
|
|
|
}
|
2022-07-26 00:20:01 +08:00
|
|
|
|
|
|
|
# Save, print and return
|
|
|
|
if save:
|
2024-01-08 08:29:14 +08:00
|
|
|
stats_path = self.hub_dir / "stats.json"
|
|
|
|
print(f"Saving {stats_path.resolve()}...")
|
|
|
|
with open(stats_path, "w") as f:
|
2022-07-26 00:20:01 +08:00
|
|
|
json.dump(self.stats, f) # save stats.json
|
|
|
|
if verbose:
|
|
|
|
print(json.dumps(self.stats, indent=2, sort_keys=False))
|
|
|
|
return self.stats
|
|
|
|
|
|
|
|
def process_images(self):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Compresses images for Ultralytics HUB across 'train', 'val', 'test' splits and saves to specified
|
|
|
|
directory.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
for split in "train", "val", "test":
|
2022-07-26 00:20:01 +08:00
|
|
|
if self.data.get(split) is None:
|
|
|
|
continue
|
|
|
|
dataset = LoadImagesAndLabels(self.data[split]) # load dataset
|
2024-01-08 08:29:14 +08:00
|
|
|
desc = f"{split} images"
|
2022-07-26 00:20:01 +08:00
|
|
|
for _ in tqdm(ThreadPool(NUM_THREADS).imap(self._hub_ops, dataset.im_files), total=dataset.n, desc=desc):
|
2021-07-28 08:04:10 +08:00
|
|
|
pass
|
2024-01-08 08:29:14 +08:00
|
|
|
print(f"Done. All images saved to {self.im_dir}")
|
2022-07-26 00:20:01 +08:00
|
|
|
return self.im_dir
|
New YOLOv5 Classification Models (#8956)
* Update
* Logger step fix: Increment step with epochs (#8654)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Allow logging models from GenericLogger (#8676)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* support final model logging
* update
* update
* update
* update
* remove curses
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update __init__.py
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update dataset download
* Update dataset download
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Pass imgsz to classify_transforms()
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Cos scheduler
* Cos scheduler
* Remove unused args
* Update
* Add seed
* Add seed
* Update
* Update
* Add run(), main()
* Merge master
* Merge master
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Create YOLOv5 BaseModel class (#8829)
* Create BaseModel
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Hub load device fix
* Update
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add experiment
* Merge master
* Attach names
* weight decay = 1e-4
* weight decay = 5e-5
* update smart_optimizer console printout
* fashion-mnist fix
* Merge master
* Update Table
* Update Table
* Remove destroy process group
* add kwargs to forward()
* fuse fix for resnet50
* nc, names fix for resnet50
* nc, names fix for resnet50
* ONNX CPU inference fix
* revert
* cuda
* if augment or visualize
* if augment or visualize
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* New smart_inference_mode()
* Update README
* Refactor into /classify dir
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* reset defaults
* reset defaults
* fix gpu predict
* warmup
* ema half fix
* spacing
* remove data
* remove cache
* remove denormalize
* save run settings
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* verbose false on initial plots
* new save_yaml() function
* Update ci-testing.yml
* Path(data) CI fix
* Separate classification CI
* fix val
* fix val
* fix val
* smartCrossEntropyLoss
* skip validation on hub load
* autodownload with working dir root
* str(data)
* Dataset usage example
* im_show normalize
* im_show normalize
* add imagenet simple names to multibackend
* Add validation speeds
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* 24-space names
* Update bash scripts
* Update permissions
* Add bash script arguments
* remove verbose
* TRT data fix
* names generator fix
* optimize if names
* update usage
* Add local loading
* Verbose=False
* update names printing
* Add Usage examples
* Add Usage examples
* Add Usage examples
* Add Usage examples
* named_children
* reshape_classifier_outputs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* update
* fix CI
* fix incorrect class substitution
* fix incorrect class substitution
* remove denormalize
* ravel fix
* cleanup
* update opt file printing
* update opt file printing
* update defaults
* add opt to checkpoint
* Add warning
* Add comment
* plot half bug fix
* Use NotImplementedError
* fix export shape report
* Fix TRT load
* cleanup CI
* profile comment
* CI fix
* Add cls models
* avoid inplace error
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix usage examples
* Update README
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
Co-authored-by: Ayush Chaurasia <ayush.chaurarsia@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-08-17 17:59:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Classification dataloaders -------------------------------------------------------------------------------------------
|
|
|
|
class ClassificationDataset(torchvision.datasets.ImageFolder):
|
|
|
|
"""
|
|
|
|
YOLOv5 Classification Dataset.
|
2024-01-08 08:29:14 +08:00
|
|
|
|
2024-08-25 05:45:33 +08:00
|
|
|
Arguments:
|
New YOLOv5 Classification Models (#8956)
* Update
* Logger step fix: Increment step with epochs (#8654)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Allow logging models from GenericLogger (#8676)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* support final model logging
* update
* update
* update
* update
* remove curses
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update __init__.py
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update dataset download
* Update dataset download
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Pass imgsz to classify_transforms()
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Cos scheduler
* Cos scheduler
* Remove unused args
* Update
* Add seed
* Add seed
* Update
* Update
* Add run(), main()
* Merge master
* Merge master
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Create YOLOv5 BaseModel class (#8829)
* Create BaseModel
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Hub load device fix
* Update
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add experiment
* Merge master
* Attach names
* weight decay = 1e-4
* weight decay = 5e-5
* update smart_optimizer console printout
* fashion-mnist fix
* Merge master
* Update Table
* Update Table
* Remove destroy process group
* add kwargs to forward()
* fuse fix for resnet50
* nc, names fix for resnet50
* nc, names fix for resnet50
* ONNX CPU inference fix
* revert
* cuda
* if augment or visualize
* if augment or visualize
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* New smart_inference_mode()
* Update README
* Refactor into /classify dir
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* reset defaults
* reset defaults
* fix gpu predict
* warmup
* ema half fix
* spacing
* remove data
* remove cache
* remove denormalize
* save run settings
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* verbose false on initial plots
* new save_yaml() function
* Update ci-testing.yml
* Path(data) CI fix
* Separate classification CI
* fix val
* fix val
* fix val
* smartCrossEntropyLoss
* skip validation on hub load
* autodownload with working dir root
* str(data)
* Dataset usage example
* im_show normalize
* im_show normalize
* add imagenet simple names to multibackend
* Add validation speeds
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* 24-space names
* Update bash scripts
* Update permissions
* Add bash script arguments
* remove verbose
* TRT data fix
* names generator fix
* optimize if names
* update usage
* Add local loading
* Verbose=False
* update names printing
* Add Usage examples
* Add Usage examples
* Add Usage examples
* Add Usage examples
* named_children
* reshape_classifier_outputs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* update
* fix CI
* fix incorrect class substitution
* fix incorrect class substitution
* remove denormalize
* ravel fix
* cleanup
* update opt file printing
* update opt file printing
* update defaults
* add opt to checkpoint
* Add warning
* Add comment
* plot half bug fix
* Use NotImplementedError
* fix export shape report
* Fix TRT load
* cleanup CI
* profile comment
* CI fix
* Add cls models
* avoid inplace error
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix usage examples
* Update README
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
Co-authored-by: Ayush Chaurasia <ayush.chaurarsia@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-08-17 17:59:01 +08:00
|
|
|
root: Dataset path
|
|
|
|
transform: torchvision transforms, used by default
|
|
|
|
album_transform: Albumentations transforms, used if installed
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, root, augment, imgsz, cache=False):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Initializes YOLOv5 Classification Dataset with optional caching, augmentations, and transforms for image
|
|
|
|
classification.
|
|
|
|
"""
|
New YOLOv5 Classification Models (#8956)
* Update
* Logger step fix: Increment step with epochs (#8654)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Allow logging models from GenericLogger (#8676)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* support final model logging
* update
* update
* update
* update
* remove curses
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update __init__.py
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update dataset download
* Update dataset download
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Pass imgsz to classify_transforms()
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Cos scheduler
* Cos scheduler
* Remove unused args
* Update
* Add seed
* Add seed
* Update
* Update
* Add run(), main()
* Merge master
* Merge master
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Create YOLOv5 BaseModel class (#8829)
* Create BaseModel
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Hub load device fix
* Update
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add experiment
* Merge master
* Attach names
* weight decay = 1e-4
* weight decay = 5e-5
* update smart_optimizer console printout
* fashion-mnist fix
* Merge master
* Update Table
* Update Table
* Remove destroy process group
* add kwargs to forward()
* fuse fix for resnet50
* nc, names fix for resnet50
* nc, names fix for resnet50
* ONNX CPU inference fix
* revert
* cuda
* if augment or visualize
* if augment or visualize
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* New smart_inference_mode()
* Update README
* Refactor into /classify dir
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* reset defaults
* reset defaults
* fix gpu predict
* warmup
* ema half fix
* spacing
* remove data
* remove cache
* remove denormalize
* save run settings
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* verbose false on initial plots
* new save_yaml() function
* Update ci-testing.yml
* Path(data) CI fix
* Separate classification CI
* fix val
* fix val
* fix val
* smartCrossEntropyLoss
* skip validation on hub load
* autodownload with working dir root
* str(data)
* Dataset usage example
* im_show normalize
* im_show normalize
* add imagenet simple names to multibackend
* Add validation speeds
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* 24-space names
* Update bash scripts
* Update permissions
* Add bash script arguments
* remove verbose
* TRT data fix
* names generator fix
* optimize if names
* update usage
* Add local loading
* Verbose=False
* update names printing
* Add Usage examples
* Add Usage examples
* Add Usage examples
* Add Usage examples
* named_children
* reshape_classifier_outputs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* update
* fix CI
* fix incorrect class substitution
* fix incorrect class substitution
* remove denormalize
* ravel fix
* cleanup
* update opt file printing
* update opt file printing
* update defaults
* add opt to checkpoint
* Add warning
* Add comment
* plot half bug fix
* Use NotImplementedError
* fix export shape report
* Fix TRT load
* cleanup CI
* profile comment
* CI fix
* Add cls models
* avoid inplace error
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix usage examples
* Update README
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
Co-authored-by: Ayush Chaurasia <ayush.chaurarsia@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-08-17 17:59:01 +08:00
|
|
|
super().__init__(root=root)
|
|
|
|
self.torch_transforms = classify_transforms(imgsz)
|
|
|
|
self.album_transforms = classify_albumentations(augment, imgsz) if augment else None
|
2024-01-08 08:29:14 +08:00
|
|
|
self.cache_ram = cache is True or cache == "ram"
|
|
|
|
self.cache_disk = cache == "disk"
|
|
|
|
self.samples = [list(x) + [Path(x[0]).with_suffix(".npy"), None] for x in self.samples] # file, index, npy, im
|
New YOLOv5 Classification Models (#8956)
* Update
* Logger step fix: Increment step with epochs (#8654)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Allow logging models from GenericLogger (#8676)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* support final model logging
* update
* update
* update
* update
* remove curses
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update __init__.py
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update dataset download
* Update dataset download
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Pass imgsz to classify_transforms()
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Cos scheduler
* Cos scheduler
* Remove unused args
* Update
* Add seed
* Add seed
* Update
* Update
* Add run(), main()
* Merge master
* Merge master
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Create YOLOv5 BaseModel class (#8829)
* Create BaseModel
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Hub load device fix
* Update
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add experiment
* Merge master
* Attach names
* weight decay = 1e-4
* weight decay = 5e-5
* update smart_optimizer console printout
* fashion-mnist fix
* Merge master
* Update Table
* Update Table
* Remove destroy process group
* add kwargs to forward()
* fuse fix for resnet50
* nc, names fix for resnet50
* nc, names fix for resnet50
* ONNX CPU inference fix
* revert
* cuda
* if augment or visualize
* if augment or visualize
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* New smart_inference_mode()
* Update README
* Refactor into /classify dir
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* reset defaults
* reset defaults
* fix gpu predict
* warmup
* ema half fix
* spacing
* remove data
* remove cache
* remove denormalize
* save run settings
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* verbose false on initial plots
* new save_yaml() function
* Update ci-testing.yml
* Path(data) CI fix
* Separate classification CI
* fix val
* fix val
* fix val
* smartCrossEntropyLoss
* skip validation on hub load
* autodownload with working dir root
* str(data)
* Dataset usage example
* im_show normalize
* im_show normalize
* add imagenet simple names to multibackend
* Add validation speeds
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* 24-space names
* Update bash scripts
* Update permissions
* Add bash script arguments
* remove verbose
* TRT data fix
* names generator fix
* optimize if names
* update usage
* Add local loading
* Verbose=False
* update names printing
* Add Usage examples
* Add Usage examples
* Add Usage examples
* Add Usage examples
* named_children
* reshape_classifier_outputs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* update
* fix CI
* fix incorrect class substitution
* fix incorrect class substitution
* remove denormalize
* ravel fix
* cleanup
* update opt file printing
* update opt file printing
* update defaults
* add opt to checkpoint
* Add warning
* Add comment
* plot half bug fix
* Use NotImplementedError
* fix export shape report
* Fix TRT load
* cleanup CI
* profile comment
* CI fix
* Add cls models
* avoid inplace error
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix usage examples
* Update README
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
Co-authored-by: Ayush Chaurasia <ayush.chaurarsia@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-08-17 17:59:01 +08:00
|
|
|
|
|
|
|
def __getitem__(self, i):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Fetches and transforms an image sample by index, supporting RAM/disk caching and Augmentations."""
|
New YOLOv5 Classification Models (#8956)
* Update
* Logger step fix: Increment step with epochs (#8654)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Allow logging models from GenericLogger (#8676)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* support final model logging
* update
* update
* update
* update
* remove curses
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update __init__.py
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update dataset download
* Update dataset download
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Pass imgsz to classify_transforms()
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Cos scheduler
* Cos scheduler
* Remove unused args
* Update
* Add seed
* Add seed
* Update
* Update
* Add run(), main()
* Merge master
* Merge master
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Create YOLOv5 BaseModel class (#8829)
* Create BaseModel
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Hub load device fix
* Update
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add experiment
* Merge master
* Attach names
* weight decay = 1e-4
* weight decay = 5e-5
* update smart_optimizer console printout
* fashion-mnist fix
* Merge master
* Update Table
* Update Table
* Remove destroy process group
* add kwargs to forward()
* fuse fix for resnet50
* nc, names fix for resnet50
* nc, names fix for resnet50
* ONNX CPU inference fix
* revert
* cuda
* if augment or visualize
* if augment or visualize
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* New smart_inference_mode()
* Update README
* Refactor into /classify dir
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* reset defaults
* reset defaults
* fix gpu predict
* warmup
* ema half fix
* spacing
* remove data
* remove cache
* remove denormalize
* save run settings
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* verbose false on initial plots
* new save_yaml() function
* Update ci-testing.yml
* Path(data) CI fix
* Separate classification CI
* fix val
* fix val
* fix val
* smartCrossEntropyLoss
* skip validation on hub load
* autodownload with working dir root
* str(data)
* Dataset usage example
* im_show normalize
* im_show normalize
* add imagenet simple names to multibackend
* Add validation speeds
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* 24-space names
* Update bash scripts
* Update permissions
* Add bash script arguments
* remove verbose
* TRT data fix
* names generator fix
* optimize if names
* update usage
* Add local loading
* Verbose=False
* update names printing
* Add Usage examples
* Add Usage examples
* Add Usage examples
* Add Usage examples
* named_children
* reshape_classifier_outputs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* update
* fix CI
* fix incorrect class substitution
* fix incorrect class substitution
* remove denormalize
* ravel fix
* cleanup
* update opt file printing
* update opt file printing
* update defaults
* add opt to checkpoint
* Add warning
* Add comment
* plot half bug fix
* Use NotImplementedError
* fix export shape report
* Fix TRT load
* cleanup CI
* profile comment
* CI fix
* Add cls models
* avoid inplace error
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix usage examples
* Update README
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
Co-authored-by: Ayush Chaurasia <ayush.chaurarsia@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-08-17 17:59:01 +08:00
|
|
|
f, j, fn, im = self.samples[i] # filename, index, filename.with_suffix('.npy'), image
|
2022-08-30 21:17:58 +08:00
|
|
|
if self.cache_ram and im is None:
|
|
|
|
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))
|
|
|
|
im = np.load(fn)
|
|
|
|
else: # read image
|
|
|
|
im = cv2.imread(f) # BGR
|
New YOLOv5 Classification Models (#8956)
* Update
* Logger step fix: Increment step with epochs (#8654)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Allow logging models from GenericLogger (#8676)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* support final model logging
* update
* update
* update
* update
* remove curses
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update __init__.py
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update dataset download
* Update dataset download
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Pass imgsz to classify_transforms()
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Cos scheduler
* Cos scheduler
* Remove unused args
* Update
* Add seed
* Add seed
* Update
* Update
* Add run(), main()
* Merge master
* Merge master
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Create YOLOv5 BaseModel class (#8829)
* Create BaseModel
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Hub load device fix
* Update
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add experiment
* Merge master
* Attach names
* weight decay = 1e-4
* weight decay = 5e-5
* update smart_optimizer console printout
* fashion-mnist fix
* Merge master
* Update Table
* Update Table
* Remove destroy process group
* add kwargs to forward()
* fuse fix for resnet50
* nc, names fix for resnet50
* nc, names fix for resnet50
* ONNX CPU inference fix
* revert
* cuda
* if augment or visualize
* if augment or visualize
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* New smart_inference_mode()
* Update README
* Refactor into /classify dir
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* reset defaults
* reset defaults
* fix gpu predict
* warmup
* ema half fix
* spacing
* remove data
* remove cache
* remove denormalize
* save run settings
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* verbose false on initial plots
* new save_yaml() function
* Update ci-testing.yml
* Path(data) CI fix
* Separate classification CI
* fix val
* fix val
* fix val
* smartCrossEntropyLoss
* skip validation on hub load
* autodownload with working dir root
* str(data)
* Dataset usage example
* im_show normalize
* im_show normalize
* add imagenet simple names to multibackend
* Add validation speeds
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* 24-space names
* Update bash scripts
* Update permissions
* Add bash script arguments
* remove verbose
* TRT data fix
* names generator fix
* optimize if names
* update usage
* Add local loading
* Verbose=False
* update names printing
* Add Usage examples
* Add Usage examples
* Add Usage examples
* Add Usage examples
* named_children
* reshape_classifier_outputs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* update
* fix CI
* fix incorrect class substitution
* fix incorrect class substitution
* remove denormalize
* ravel fix
* cleanup
* update opt file printing
* update opt file printing
* update defaults
* add opt to checkpoint
* Add warning
* Add comment
* plot half bug fix
* Use NotImplementedError
* fix export shape report
* Fix TRT load
* cleanup CI
* profile comment
* CI fix
* Add cls models
* avoid inplace error
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix usage examples
* Update README
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
Co-authored-by: Ayush Chaurasia <ayush.chaurarsia@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-08-17 17:59:01 +08:00
|
|
|
if self.album_transforms:
|
2024-01-08 08:29:14 +08:00
|
|
|
sample = self.album_transforms(image=cv2.cvtColor(im, cv2.COLOR_BGR2RGB))["image"]
|
New YOLOv5 Classification Models (#8956)
* Update
* Logger step fix: Increment step with epochs (#8654)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Allow logging models from GenericLogger (#8676)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* support final model logging
* update
* update
* update
* update
* remove curses
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update __init__.py
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update dataset download
* Update dataset download
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Pass imgsz to classify_transforms()
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Cos scheduler
* Cos scheduler
* Remove unused args
* Update
* Add seed
* Add seed
* Update
* Update
* Add run(), main()
* Merge master
* Merge master
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Create YOLOv5 BaseModel class (#8829)
* Create BaseModel
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Hub load device fix
* Update
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add experiment
* Merge master
* Attach names
* weight decay = 1e-4
* weight decay = 5e-5
* update smart_optimizer console printout
* fashion-mnist fix
* Merge master
* Update Table
* Update Table
* Remove destroy process group
* add kwargs to forward()
* fuse fix for resnet50
* nc, names fix for resnet50
* nc, names fix for resnet50
* ONNX CPU inference fix
* revert
* cuda
* if augment or visualize
* if augment or visualize
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* New smart_inference_mode()
* Update README
* Refactor into /classify dir
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* reset defaults
* reset defaults
* fix gpu predict
* warmup
* ema half fix
* spacing
* remove data
* remove cache
* remove denormalize
* save run settings
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* verbose false on initial plots
* new save_yaml() function
* Update ci-testing.yml
* Path(data) CI fix
* Separate classification CI
* fix val
* fix val
* fix val
* smartCrossEntropyLoss
* skip validation on hub load
* autodownload with working dir root
* str(data)
* Dataset usage example
* im_show normalize
* im_show normalize
* add imagenet simple names to multibackend
* Add validation speeds
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* 24-space names
* Update bash scripts
* Update permissions
* Add bash script arguments
* remove verbose
* TRT data fix
* names generator fix
* optimize if names
* update usage
* Add local loading
* Verbose=False
* update names printing
* Add Usage examples
* Add Usage examples
* Add Usage examples
* Add Usage examples
* named_children
* reshape_classifier_outputs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* update
* fix CI
* fix incorrect class substitution
* fix incorrect class substitution
* remove denormalize
* ravel fix
* cleanup
* update opt file printing
* update opt file printing
* update defaults
* add opt to checkpoint
* Add warning
* Add comment
* plot half bug fix
* Use NotImplementedError
* fix export shape report
* Fix TRT load
* cleanup CI
* profile comment
* CI fix
* Add cls models
* avoid inplace error
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix usage examples
* Update README
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
Co-authored-by: Ayush Chaurasia <ayush.chaurarsia@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-08-17 17:59:01 +08:00
|
|
|
else:
|
2022-08-30 21:17:58 +08:00
|
|
|
sample = self.torch_transforms(im)
|
New YOLOv5 Classification Models (#8956)
* Update
* Logger step fix: Increment step with epochs (#8654)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Allow logging models from GenericLogger (#8676)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* support final model logging
* update
* update
* update
* update
* remove curses
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update __init__.py
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update dataset download
* Update dataset download
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Pass imgsz to classify_transforms()
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Cos scheduler
* Cos scheduler
* Remove unused args
* Update
* Add seed
* Add seed
* Update
* Update
* Add run(), main()
* Merge master
* Merge master
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Create YOLOv5 BaseModel class (#8829)
* Create BaseModel
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Hub load device fix
* Update
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add experiment
* Merge master
* Attach names
* weight decay = 1e-4
* weight decay = 5e-5
* update smart_optimizer console printout
* fashion-mnist fix
* Merge master
* Update Table
* Update Table
* Remove destroy process group
* add kwargs to forward()
* fuse fix for resnet50
* nc, names fix for resnet50
* nc, names fix for resnet50
* ONNX CPU inference fix
* revert
* cuda
* if augment or visualize
* if augment or visualize
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* New smart_inference_mode()
* Update README
* Refactor into /classify dir
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* reset defaults
* reset defaults
* fix gpu predict
* warmup
* ema half fix
* spacing
* remove data
* remove cache
* remove denormalize
* save run settings
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* verbose false on initial plots
* new save_yaml() function
* Update ci-testing.yml
* Path(data) CI fix
* Separate classification CI
* fix val
* fix val
* fix val
* smartCrossEntropyLoss
* skip validation on hub load
* autodownload with working dir root
* str(data)
* Dataset usage example
* im_show normalize
* im_show normalize
* add imagenet simple names to multibackend
* Add validation speeds
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* 24-space names
* Update bash scripts
* Update permissions
* Add bash script arguments
* remove verbose
* TRT data fix
* names generator fix
* optimize if names
* update usage
* Add local loading
* Verbose=False
* update names printing
* Add Usage examples
* Add Usage examples
* Add Usage examples
* Add Usage examples
* named_children
* reshape_classifier_outputs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* update
* fix CI
* fix incorrect class substitution
* fix incorrect class substitution
* remove denormalize
* ravel fix
* cleanup
* update opt file printing
* update opt file printing
* update defaults
* add opt to checkpoint
* Add warning
* Add comment
* plot half bug fix
* Use NotImplementedError
* fix export shape report
* Fix TRT load
* cleanup CI
* profile comment
* CI fix
* Add cls models
* avoid inplace error
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix usage examples
* Update README
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
Co-authored-by: Ayush Chaurasia <ayush.chaurarsia@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-08-17 17:59:01 +08:00
|
|
|
return sample, j
|
|
|
|
|
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
def create_classification_dataloader(
|
|
|
|
path, imgsz=224, batch_size=16, augment=True, cache=False, rank=-1, workers=8, shuffle=True
|
|
|
|
):
|
New YOLOv5 Classification Models (#8956)
* Update
* Logger step fix: Increment step with epochs (#8654)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Allow logging models from GenericLogger (#8676)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* support final model logging
* update
* update
* update
* update
* remove curses
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update __init__.py
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update dataset download
* Update dataset download
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Pass imgsz to classify_transforms()
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Cos scheduler
* Cos scheduler
* Remove unused args
* Update
* Add seed
* Add seed
* Update
* Update
* Add run(), main()
* Merge master
* Merge master
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Create YOLOv5 BaseModel class (#8829)
* Create BaseModel
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Hub load device fix
* Update
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add experiment
* Merge master
* Attach names
* weight decay = 1e-4
* weight decay = 5e-5
* update smart_optimizer console printout
* fashion-mnist fix
* Merge master
* Update Table
* Update Table
* Remove destroy process group
* add kwargs to forward()
* fuse fix for resnet50
* nc, names fix for resnet50
* nc, names fix for resnet50
* ONNX CPU inference fix
* revert
* cuda
* if augment or visualize
* if augment or visualize
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* New smart_inference_mode()
* Update README
* Refactor into /classify dir
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* reset defaults
* reset defaults
* fix gpu predict
* warmup
* ema half fix
* spacing
* remove data
* remove cache
* remove denormalize
* save run settings
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* verbose false on initial plots
* new save_yaml() function
* Update ci-testing.yml
* Path(data) CI fix
* Separate classification CI
* fix val
* fix val
* fix val
* smartCrossEntropyLoss
* skip validation on hub load
* autodownload with working dir root
* str(data)
* Dataset usage example
* im_show normalize
* im_show normalize
* add imagenet simple names to multibackend
* Add validation speeds
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* 24-space names
* Update bash scripts
* Update permissions
* Add bash script arguments
* remove verbose
* TRT data fix
* names generator fix
* optimize if names
* update usage
* Add local loading
* Verbose=False
* update names printing
* Add Usage examples
* Add Usage examples
* Add Usage examples
* Add Usage examples
* named_children
* reshape_classifier_outputs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* update
* fix CI
* fix incorrect class substitution
* fix incorrect class substitution
* remove denormalize
* ravel fix
* cleanup
* update opt file printing
* update opt file printing
* update defaults
* add opt to checkpoint
* Add warning
* Add comment
* plot half bug fix
* Use NotImplementedError
* fix export shape report
* Fix TRT load
* cleanup CI
* profile comment
* CI fix
* Add cls models
* avoid inplace error
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix usage examples
* Update README
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
Co-authored-by: Ayush Chaurasia <ayush.chaurarsia@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-08-17 17:59:01 +08:00
|
|
|
# Returns Dataloader object to be used with YOLOv5 Classifier
|
2024-07-01 06:17:24 +08:00
|
|
|
"""Creates a DataLoader for image classification, supporting caching, augmentation, and distributed training."""
|
New YOLOv5 Classification Models (#8956)
* Update
* Logger step fix: Increment step with epochs (#8654)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Allow logging models from GenericLogger (#8676)
* enhance
* revert
* allow training from scratch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update --img argument from train.py
single line
* fix image size from 640 to 128
* suport custom dataloader and augmentation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* format
* Update dataloaders.py
* Single line return, single line comment, remove unused argument
* address PR comments
* fix spelling
* don't augment eval set
* use fstring
* update augmentations.py
* new maning convention for transforms
* reverse if statement, inline ops
* reverse if statement, inline ops
* updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update dataloaders
* Remove additional if statement
* Remove is_train as redundant
* Cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Cleanup2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update augmentations.py
* fix: imshow clip warning
* update
* Revert ToTensorV2 removal
* Update classifier.py
* Update normalize values, revert uint8
* normalize image using cv2
* remove dedundant comment
* Update classifier.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* replace print with logger
* commit steps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* support final model logging
* update
* update
* update
* update
* remove curses
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update classifier.py
* Update __init__.py
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
* Update
* Update
* Update
* Update
* Update dataset download
* Update dataset download
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Pass imgsz to classify_transforms()
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Cos scheduler
* Cos scheduler
* Remove unused args
* Update
* Add seed
* Add seed
* Update
* Update
* Add run(), main()
* Merge master
* Merge master
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Create YOLOv5 BaseModel class (#8829)
* Create BaseModel
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Hub load device fix
* Update
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add experiment
* Merge master
* Attach names
* weight decay = 1e-4
* weight decay = 5e-5
* update smart_optimizer console printout
* fashion-mnist fix
* Merge master
* Update Table
* Update Table
* Remove destroy process group
* add kwargs to forward()
* fuse fix for resnet50
* nc, names fix for resnet50
* nc, names fix for resnet50
* ONNX CPU inference fix
* revert
* cuda
* if augment or visualize
* if augment or visualize
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* New smart_inference_mode()
* Update README
* Refactor into /classify dir
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* reset defaults
* reset defaults
* fix gpu predict
* warmup
* ema half fix
* spacing
* remove data
* remove cache
* remove denormalize
* save run settings
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* verbose false on initial plots
* new save_yaml() function
* Update ci-testing.yml
* Path(data) CI fix
* Separate classification CI
* fix val
* fix val
* fix val
* smartCrossEntropyLoss
* skip validation on hub load
* autodownload with working dir root
* str(data)
* Dataset usage example
* im_show normalize
* im_show normalize
* add imagenet simple names to multibackend
* Add validation speeds
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* 24-space names
* Update bash scripts
* Update permissions
* Add bash script arguments
* remove verbose
* TRT data fix
* names generator fix
* optimize if names
* update usage
* Add local loading
* Verbose=False
* update names printing
* Add Usage examples
* Add Usage examples
* Add Usage examples
* Add Usage examples
* named_children
* reshape_classifier_outputs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* update
* fix CI
* fix incorrect class substitution
* fix incorrect class substitution
* remove denormalize
* ravel fix
* cleanup
* update opt file printing
* update opt file printing
* update defaults
* add opt to checkpoint
* Add warning
* Add comment
* plot half bug fix
* Use NotImplementedError
* fix export shape report
* Fix TRT load
* cleanup CI
* profile comment
* CI fix
* Add cls models
* avoid inplace error
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix usage examples
* Update README
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
* Update README
Co-authored-by: Ayush Chaurasia <ayush.chaurarsia@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-08-17 17:59:01 +08:00
|
|
|
with torch_distributed_zero_first(rank): # init dataset *.cache only once if DDP
|
|
|
|
dataset = ClassificationDataset(root=path, imgsz=imgsz, augment=augment, cache=cache)
|
|
|
|
batch_size = min(batch_size, len(dataset))
|
|
|
|
nd = torch.cuda.device_count()
|
|
|
|
nw = min([os.cpu_count() // max(nd, 1), batch_size if batch_size > 1 else 0, workers])
|
|
|
|
sampler = None if rank == -1 else distributed.DistributedSampler(dataset, shuffle=shuffle)
|
|
|
|
generator = torch.Generator()
|
2022-09-25 01:35:07 +08:00
|
|
|
generator.manual_seed(6148914691236517205 + RANK)
|
2024-01-08 08:29:14 +08:00
|
|
|
return InfiniteDataLoader(
|
|
|
|
dataset,
|
|
|
|
batch_size=batch_size,
|
|
|
|
shuffle=shuffle and sampler is None,
|
|
|
|
num_workers=nw,
|
|
|
|
sampler=sampler,
|
|
|
|
pin_memory=PIN_MEMORY,
|
|
|
|
worker_init_fn=seed_worker,
|
|
|
|
generator=generator,
|
|
|
|
) # or DataLoader(persistent_workers=True)
|