You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
288 lines
12 KiB
288 lines
12 KiB
2 years ago
|
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
||
2 years ago
|
|
||
2 years ago
|
import glob
|
||
2 years ago
|
import math
|
||
2 years ago
|
import os
|
||
2 years ago
|
import random
|
||
2 years ago
|
from copy import deepcopy
|
||
2 years ago
|
from multiprocessing.pool import ThreadPool
|
||
|
from pathlib import Path
|
||
|
from typing import Optional
|
||
|
|
||
|
import cv2
|
||
|
import numpy as np
|
||
2 years ago
|
import psutil
|
||
2 years ago
|
from torch.utils.data import Dataset
|
||
|
from tqdm import tqdm
|
||
|
|
||
1 year ago
|
from ultralytics.utils import DEFAULT_CFG, LOCAL_RANK, LOGGER, NUM_THREADS, TQDM_BAR_FORMAT
|
||
|
|
||
2 years ago
|
from .utils import HELP_URL, IMG_FORMATS
|
||
2 years ago
|
|
||
|
|
||
|
class BaseDataset(Dataset):
|
||
2 years ago
|
"""
|
||
|
Base dataset class for loading and processing image data.
|
||
|
|
||
2 years ago
|
Args:
|
||
2 years ago
|
img_path (str): Path to the folder containing images.
|
||
|
imgsz (int, optional): Image size. Defaults to 640.
|
||
|
cache (bool, optional): Cache images to RAM or disk during training. Defaults to False.
|
||
|
augment (bool, optional): If True, data augmentation is applied. Defaults to True.
|
||
|
hyp (dict, optional): Hyperparameters to apply data augmentation. Defaults to None.
|
||
|
prefix (str, optional): Prefix to print in log messages. Defaults to ''.
|
||
|
rect (bool, optional): If True, rectangular training is used. Defaults to False.
|
||
|
batch_size (int, optional): Size of batches. Defaults to None.
|
||
|
stride (int, optional): Stride. Defaults to 32.
|
||
|
pad (float, optional): Padding. Defaults to 0.0.
|
||
|
single_cls (bool, optional): If True, single class training is used. Defaults to False.
|
||
2 years ago
|
classes (list): List of included classes. Default is None.
|
||
2 years ago
|
fraction (float): Fraction of dataset to utilize. Default is 1.0 (use all data).
|
||
2 years ago
|
|
||
|
Attributes:
|
||
|
im_files (list): List of image file paths.
|
||
|
labels (list): List of label data dictionaries.
|
||
|
ni (int): Number of images in the dataset.
|
||
|
ims (list): List of loaded images.
|
||
|
npy_files (list): List of numpy file paths.
|
||
|
transforms (callable): Image transformation function.
|
||
2 years ago
|
"""
|
||
|
|
||
2 years ago
|
def __init__(self,
|
||
|
img_path,
|
||
|
imgsz=640,
|
||
|
cache=False,
|
||
|
augment=True,
|
||
2 years ago
|
hyp=DEFAULT_CFG,
|
||
2 years ago
|
prefix='',
|
||
|
rect=False,
|
||
2 years ago
|
batch_size=16,
|
||
2 years ago
|
stride=32,
|
||
|
pad=0.5,
|
||
2 years ago
|
single_cls=False,
|
||
2 years ago
|
classes=None,
|
||
|
fraction=1.0):
|
||
2 years ago
|
super().__init__()
|
||
|
self.img_path = img_path
|
||
2 years ago
|
self.imgsz = imgsz
|
||
2 years ago
|
self.augment = augment
|
||
2 years ago
|
self.single_cls = single_cls
|
||
2 years ago
|
self.prefix = prefix
|
||
2 years ago
|
self.fraction = fraction
|
||
2 years ago
|
self.im_files = self.get_img_files(self.img_path)
|
||
|
self.labels = self.get_labels()
|
||
2 years ago
|
self.update_labels(include_class=classes) # single_cls and include_class
|
||
2 years ago
|
self.ni = len(self.labels) # number of images
|
||
2 years ago
|
self.rect = rect
|
||
|
self.batch_size = batch_size
|
||
|
self.stride = stride
|
||
|
self.pad = pad
|
||
|
if self.rect:
|
||
|
assert self.batch_size is not None
|
||
|
self.set_rectangle()
|
||
|
|
||
2 years ago
|
# Buffer thread for mosaic images
|
||
|
self.buffer = [] # buffer size = batch size
|
||
|
self.max_buffer_length = min((self.ni, self.batch_size * 8, 1000)) if self.augment else 0
|
||
|
|
||
2 years ago
|
# Cache stuff
|
||
2 years ago
|
if cache == 'ram' and not self.check_cache_ram():
|
||
|
cache = False
|
||
2 years ago
|
self.ims, self.im_hw0, self.im_hw = [None] * self.ni, [None] * self.ni, [None] * self.ni
|
||
2 years ago
|
self.npy_files = [Path(f).with_suffix('.npy') for f in self.im_files]
|
||
2 years ago
|
if cache:
|
||
2 years ago
|
self.cache_images(cache)
|
||
2 years ago
|
|
||
2 years ago
|
# Transforms
|
||
2 years ago
|
self.transforms = self.build_transforms(hyp=hyp)
|
||
|
|
||
|
def get_img_files(self, img_path):
|
||
|
"""Read image files."""
|
||
|
try:
|
||
|
f = [] # image files
|
||
|
for p in img_path if isinstance(img_path, list) else [img_path]:
|
||
|
p = Path(p) # os-agnostic
|
||
|
if p.is_dir(): # dir
|
||
2 years ago
|
f += glob.glob(str(p / '**' / '*.*'), recursive=True)
|
||
2 years ago
|
# F = list(p.rglob('*.*')) # pathlib
|
||
2 years ago
|
elif p.is_file(): # file
|
||
|
with open(p) as t:
|
||
|
t = t.read().strip().splitlines()
|
||
|
parent = str(p.parent) + os.sep
|
||
2 years ago
|
f += [x.replace('./', parent) if x.startswith('./') else x for x in t] # local to global path
|
||
2 years ago
|
# F += [p.parent / x.lstrip(os.sep) for x in t] # local to global path (pathlib)
|
||
2 years ago
|
else:
|
||
2 years ago
|
raise FileNotFoundError(f'{self.prefix}{p} does not exist')
|
||
|
im_files = sorted(x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in IMG_FORMATS)
|
||
2 years ago
|
# self.img_files = sorted([x for x in f if x.suffix[1:].lower() in IMG_FORMATS]) # pathlib
|
||
2 years ago
|
assert im_files, f'{self.prefix}No images found'
|
||
2 years ago
|
except Exception as e:
|
||
2 years ago
|
raise FileNotFoundError(f'{self.prefix}Error loading data from {img_path}\n{HELP_URL}') from e
|
||
2 years ago
|
if self.fraction < 1:
|
||
|
im_files = im_files[:round(len(im_files) * self.fraction)]
|
||
2 years ago
|
return im_files
|
||
|
|
||
|
def update_labels(self, include_class: Optional[list]):
|
||
2 years ago
|
"""include_class, filter labels to include only these classes (optional)."""
|
||
2 years ago
|
include_class_array = np.array(include_class).reshape(1, -1)
|
||
|
for i in range(len(self.labels)):
|
||
2 years ago
|
if include_class is not None:
|
||
2 years ago
|
cls = self.labels[i]['cls']
|
||
|
bboxes = self.labels[i]['bboxes']
|
||
|
segments = self.labels[i]['segments']
|
||
2 years ago
|
keypoints = self.labels[i]['keypoints']
|
||
2 years ago
|
j = (cls == include_class_array).any(1)
|
||
2 years ago
|
self.labels[i]['cls'] = cls[j]
|
||
|
self.labels[i]['bboxes'] = bboxes[j]
|
||
2 years ago
|
if segments:
|
||
2 years ago
|
self.labels[i]['segments'] = [segments[si] for si, idx in enumerate(j) if idx]
|
||
2 years ago
|
if keypoints is not None:
|
||
|
self.labels[i]['keypoints'] = keypoints[j]
|
||
2 years ago
|
if self.single_cls:
|
||
2 years ago
|
self.labels[i]['cls'][:, 0] = 0
|
||
2 years ago
|
|
||
|
def load_image(self, i):
|
||
2 years ago
|
"""Loads 1 image from dataset index 'i', returns (im, resized hw)."""
|
||
2 years ago
|
im, f, fn = self.ims[i], self.im_files[i], self.npy_files[i]
|
||
|
if im is None: # not cached in RAM
|
||
|
if fn.exists(): # load npy
|
||
|
im = np.load(fn)
|
||
|
else: # read image
|
||
|
im = cv2.imread(f) # BGR
|
||
2 years ago
|
if im is None:
|
||
2 years ago
|
raise FileNotFoundError(f'Image Not Found {f}')
|
||
2 years ago
|
h0, w0 = im.shape[:2] # orig hw
|
||
2 years ago
|
r = self.imgsz / max(h0, w0) # ratio
|
||
2 years ago
|
if r != 1: # if sizes are not equal
|
||
|
interp = cv2.INTER_LINEAR if (self.augment or r > 1) else cv2.INTER_AREA
|
||
2 years ago
|
im = cv2.resize(im, (min(math.ceil(w0 * r), self.imgsz), min(math.ceil(h0 * r), self.imgsz)),
|
||
|
interpolation=interp)
|
||
2 years ago
|
|
||
|
# Add to buffer if training with augmentations
|
||
|
if self.augment:
|
||
|
self.ims[i], self.im_hw0[i], self.im_hw[i] = im, (h0, w0), im.shape[:2] # im, hw_original, hw_resized
|
||
|
self.buffer.append(i)
|
||
|
if len(self.buffer) >= self.max_buffer_length:
|
||
|
j = self.buffer.pop(0)
|
||
|
self.ims[j], self.im_hw0[j], self.im_hw[j] = None, None, None
|
||
|
|
||
|
return im, (h0, w0), im.shape[:2]
|
||
|
|
||
|
return self.ims[i], self.im_hw0[i], self.im_hw[i]
|
||
2 years ago
|
|
||
2 years ago
|
def cache_images(self, cache):
|
||
2 years ago
|
"""Cache images to memory or disk."""
|
||
2 years ago
|
b, gb = 0, 1 << 30 # bytes of cached images, bytes per gigabytes
|
||
2 years ago
|
fcn = self.cache_images_to_disk if cache == 'disk' else self.load_image
|
||
2 years ago
|
with ThreadPool(NUM_THREADS) as pool:
|
||
|
results = pool.imap(fcn, range(self.ni))
|
||
|
pbar = tqdm(enumerate(results), total=self.ni, bar_format=TQDM_BAR_FORMAT, disable=LOCAL_RANK > 0)
|
||
|
for i, x in pbar:
|
||
2 years ago
|
if cache == 'disk':
|
||
2 years ago
|
b += self.npy_files[i].stat().st_size
|
||
2 years ago
|
else: # 'ram'
|
||
|
self.ims[i], self.im_hw0[i], self.im_hw[i] = x # im, hw_orig, hw_resized = load_image(self, i)
|
||
2 years ago
|
b += self.ims[i].nbytes
|
||
|
pbar.desc = f'{self.prefix}Caching images ({b / gb:.1f}GB {cache})'
|
||
2 years ago
|
pbar.close()
|
||
2 years ago
|
|
||
|
def cache_images_to_disk(self, i):
|
||
2 years ago
|
"""Saves an image as an *.npy file for faster loading."""
|
||
2 years ago
|
f = self.npy_files[i]
|
||
|
if not f.exists():
|
||
|
np.save(f.as_posix(), cv2.imread(self.im_files[i]))
|
||
|
|
||
2 years ago
|
def check_cache_ram(self, safety_margin=0.5):
|
||
|
"""Check image caching requirements vs available memory."""
|
||
|
b, gb = 0, 1 << 30 # bytes of cached images, bytes per gigabytes
|
||
|
n = min(self.ni, 30) # extrapolate from 30 random images
|
||
|
for _ in range(n):
|
||
|
im = cv2.imread(random.choice(self.im_files)) # sample image
|
||
|
ratio = self.imgsz / max(im.shape[0], im.shape[1]) # max(h, w) # ratio
|
||
|
b += im.nbytes * ratio ** 2
|
||
|
mem_required = b * self.ni / n * (1 + safety_margin) # GB required to cache dataset into RAM
|
||
|
mem = psutil.virtual_memory()
|
||
|
cache = mem_required < mem.available # to cache or not to cache, that is the question
|
||
|
if not cache:
|
||
|
LOGGER.info(f'{self.prefix}{mem_required / gb:.1f}GB RAM required to cache images '
|
||
|
f'with {int(safety_margin * 100)}% safety margin but only '
|
||
|
f'{mem.available / gb:.1f}/{mem.total / gb:.1f}GB available, '
|
||
|
f"{'caching images ✅' if cache else 'not caching images ⚠️'}")
|
||
|
return cache
|
||
|
|
||
2 years ago
|
def set_rectangle(self):
|
||
2 years ago
|
"""Sets the shape of bounding boxes for YOLO detections as rectangles."""
|
||
2 years ago
|
bi = np.floor(np.arange(self.ni) / self.batch_size).astype(int) # batch index
|
||
|
nb = bi[-1] + 1 # number of batches
|
||
|
|
||
2 years ago
|
s = np.array([x.pop('shape') for x in self.labels]) # hw
|
||
2 years ago
|
ar = s[:, 0] / s[:, 1] # aspect ratio
|
||
|
irect = ar.argsort()
|
||
|
self.im_files = [self.im_files[i] for i in irect]
|
||
|
self.labels = [self.labels[i] for i in irect]
|
||
|
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]
|
||
|
|
||
2 years ago
|
self.batch_shapes = np.ceil(np.array(shapes) * self.imgsz / self.stride + self.pad).astype(int) * self.stride
|
||
2 years ago
|
self.batch = bi # batch index of image
|
||
|
|
||
|
def __getitem__(self, index):
|
||
2 years ago
|
"""Returns transformed label information for given index."""
|
||
2 years ago
|
return self.transforms(self.get_image_and_label(index))
|
||
2 years ago
|
|
||
2 years ago
|
def get_image_and_label(self, index):
|
||
2 years ago
|
"""Get and return label information from the dataset."""
|
||
2 years ago
|
label = deepcopy(self.labels[index]) # requires deepcopy() https://github.com/ultralytics/ultralytics/pull/1948
|
||
2 years ago
|
label.pop('shape', None) # shape is for rect, remove it
|
||
|
label['img'], label['ori_shape'], label['resized_shape'] = self.load_image(index)
|
||
2 years ago
|
label['ratio_pad'] = (label['resized_shape'][0] / label['ori_shape'][0],
|
||
|
label['resized_shape'][1] / label['ori_shape'][1]) # for evaluation
|
||
2 years ago
|
if self.rect:
|
||
2 years ago
|
label['rect_shape'] = self.batch_shapes[self.batch[index]]
|
||
2 years ago
|
return self.update_labels_info(label)
|
||
2 years ago
|
|
||
|
def __len__(self):
|
||
2 years ago
|
"""Returns the length of the labels list for the dataset."""
|
||
2 years ago
|
return len(self.labels)
|
||
2 years ago
|
|
||
|
def update_labels_info(self, label):
|
||
2 years ago
|
"""custom your label format here."""
|
||
2 years ago
|
return label
|
||
|
|
||
|
def build_transforms(self, hyp=None):
|
||
|
"""Users can custom augmentations here
|
||
|
like:
|
||
|
if self.augment:
|
||
2 years ago
|
# Training transforms
|
||
2 years ago
|
return Compose([])
|
||
|
else:
|
||
2 years ago
|
# Val transforms
|
||
2 years ago
|
return Compose([])
|
||
|
"""
|
||
|
raise NotImplementedError
|
||
|
|
||
|
def get_labels(self):
|
||
|
"""Users can custom their own format here.
|
||
|
Make sure your output is a list with each element like below:
|
||
|
dict(
|
||
|
im_file=im_file,
|
||
|
shape=shape, # format: (height, width)
|
||
|
cls=cls,
|
||
|
bboxes=bboxes, # xywh
|
||
|
segments=segments, # xy
|
||
|
keypoints=keypoints, # xy
|
||
|
normalized=True, # or False
|
||
|
bbox_format="xyxy", # or xywh, ltwh
|
||
|
)
|
||
|
"""
|
||
|
raise NotImplementedError
|