Merge branch 'main' into new-cache

new-cache
Laughing-q 8 months ago
commit 8726a6c47e
  1. 2
      pyproject.toml
  2. 2
      ultralytics/__init__.py
  3. 28
      ultralytics/data/base.py
  4. 1
      ultralytics/data/dataset.py
  5. 2
      ultralytics/engine/tuner.py

@ -19,7 +19,7 @@
# For comprehensive documentation and usage instructions, visit: https://docs.ultralytics.com
[build-system]
requires = ["setuptools>=43.0.0", "wheel"]
requires = ["setuptools>=57.0.0", "wheel"]
build-backend = "setuptools.build_meta"
# Project settings -----------------------------------------------------------------------------------------------------

@ -1,6 +1,6 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license
__version__ = "8.1.44"
__version__ = "8.1.45"
from ultralytics.data.explorer.explorer import Explorer
from ultralytics.models import RTDETR, SAM, YOLO, YOLOWorld

@ -81,19 +81,17 @@ class BaseDataset(Dataset):
if self.rect:
assert self.batch_size is not None
self.set_rectangle()
# 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
# Cache images
if isinstance(cache, str):
cache = cache.lower()
if cache == "ram" and not self.check_cache_ram():
cache = False
self.ims, self.im_hw0, self.im_hw = [None] * self.ni, [None] * self.ni, [None] * self.ni
self.npy_files = [Path(f).with_suffix(".npy") for f in self.im_files]
cache = cache.lower() if isinstance(cache, str) else "ram" if cache is True else None
self.cache_disk = cache == "disk" # cache images on hard drive as uncompressed *.npy files
self.cache_ram = cache and not self.cache_disk # cache images into RAM
self.cache_ram = cache == "ram" and self.check_cache_ram() # cache images into RAM
# Transforms
self.transforms = self.build_transforms(hyp=hyp)
@ -121,9 +119,7 @@ class BaseDataset(Dataset):
except Exception as e:
raise FileNotFoundError(f"{self.prefix}Error loading data from {img_path}\n{HELP_URL}") from e
if self.fraction < 1:
# im_files = im_files[: round(len(im_files) * self.fraction)]
num_elements_to_select = round(len(im_files) * self.fraction)
im_files = random.sample(im_files, num_elements_to_select)
im_files = im_files[: round(len(im_files) * self.fraction)] # retain a fraction of the dataset
return im_files
def update_labels(self, include_class: Optional[list]):
@ -174,7 +170,8 @@ class BaseDataset(Dataset):
self.buffer.append(i)
if len(self.buffer) >= self.max_buffer_length and not self.cache_ram:
j = self.buffer.pop(0)
self.ims[j], self.im_hw0[j], self.im_hw[j] = None, None, None
if self.cache != "ram":
self.ims[j], self.im_hw0[j], self.im_hw[j] = None, None, None
return im, (h0, w0), im.shape[:2]
@ -190,15 +187,14 @@ class BaseDataset(Dataset):
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:
success = mem_required < mem.available # to cache or not to cache, that is the question
if not success:
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 ⚠'}"
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, not caching images ⚠"
)
return cache
return success
def set_rectangle(self):
"""Sets the shape of bounding boxes for YOLO detections as rectangles."""

@ -1,4 +1,5 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license
import contextlib
import json
from collections import defaultdict

@ -218,7 +218,7 @@ class Tuner:
for ckpt in weights_dir.glob("*.pt"):
shutil.copy2(ckpt, self.tune_dir / "weights")
elif cleanup:
shutil.rmtree(ckpt_file.parent) # remove iteration weights/ dir to reduce storage space
shutil.rmtree(weights_dir, ignore_errors=True) # remove iteration weights/ dir to reduce storage space
# Plot tune results
plot_tune_results(self.tune_csv)

Loading…
Cancel
Save