Skip to main content

Redirect Ultralytics disk cache files into a local temporary cache root.

Project description

ultralytics-disk-cache-hook

Redirect ultralytics cache files away from the dataset directory and into a local cache root on the training node.

中文说明

Quick Start

pip install ultralytics-disk-cache-hook
from ultralytics import YOLO

model = YOLO("yolov8n.pt")
model.train(data="coco128.yaml", cache="disk")

After installation, the package auto-enables itself for new Python processes via the ultralytics_disk_cache_hook_auto_enable_startup.pth file in site-packages.

Configuration

You can control the startup defaults with environment variables:

export ULTRALYTICS_IMAGE_DISK_CACHE=1
export ULTRALYTICS_DATASET_META_CACHE=0

Supported false values are 0, false, no, and off.

If you disable or bypass the startup defaults and want to control the hooks explicitly in code:

from ultralytics_disk_cache_hook import enable

enable(image_disk_cache=True, dataset_meta_cache=False)
enable(image_disk_cache=False, dataset_meta_cache=True)

What It Does

When ultralytics runs with cache="disk", it writes *.npy cache files next to the original images by default.

It also writes dataset metadata *.cache files such as labels/train.cache, annotations.cache, or dataset_root.cache next to the dataset source paths.

This plugin monkey patches the internal dataset implementation and redirects those cache files into a local temporary cache root instead.

  • By default affects cache="disk" image caches
  • By default affects dataset metadata *.cache files
  • Lets you disable either hook independently via environment variables or enable(...)
  • Does not affect cache="ram" or disabled cache
  • Rewrites self.npy_files for detection, segmentation, pose, and other tasks built on BaseDataset
  • Rewrites *.npy paths inside ClassificationDataset.samples for classification tasks
  • Can redirect dataset metadata cache helpers shared by detection, grounding, and classification datasets
  • Writes cache files into hash buckets instead of mirroring the original dataset directory tree

Cache Root

The cache root is derived from ULTRALYTICS_DISK_CACHE_TMPDIR when set, otherwise from tempfile.gettempdir(), with ultralytics-disk-cache appended to it.

Override it with an environment variable:

export ULTRALYTICS_DISK_CACHE_TMPDIR=/local_nvme/tmp

Example cache path:

/mnt/shared-storage/datasets/coco/images/train2017/000000000001.jpg
-> <cache-root>/d1/3f/d13f474cca61f46ba06ecba11c1b3046.npy

Dataset metadata cache example:

/mnt/shared-storage/datasets/coco/labels/train.cache
-> <cache-root>/7a/9c/7a9c5f8af885b2f5c6c2f67066342c0a.cache

Version Support

This plugin monkey patches non-public ultralytics internals, so it only claims support for versions whose source layout has been checked.

The current code enforces the following validated range:

  • Minimum supported version: 8.4.0
  • Maximum validated version: 8.4.38
  • Allowed range: 8.4.0 <= ultralytics <= 8.4.38

If the installed version is outside that range, enable() raises UnsupportedUltralyticsVersionError.

Why:

  • v8.0.x still uses the old ultralytics/yolo/data/... layout
  • v8.1.x through v8.3.x do not match the internal hook points used by this patch
  • Starting from v8.4.0, the disk cache structure in BaseDataset and ClassificationDataset matches this plugin

This range is based on checked GitHub source files and release/tag history. As of 2026-04-17, the latest verified release I checked is v8.4.38.

Check the installed version with:

python -c "import ultralytics; print(ultralytics.__version__)"

Disk Space

This plugin does not currently validate whether the cache disk has enough free space.

When cache="disk" is enabled, the plugin prints a warning with the cache root and asks the user to manage disk space manually.

If the local cache disk fills up, the error will occur when *.npy files are actually being written.

References

Copyright

Copyright (c) xx025. All rights reserved.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ultralytics_disk_cache_hook-0.2.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ultralytics_disk_cache_hook-0.2.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file ultralytics_disk_cache_hook-0.2.0.tar.gz.

File metadata

File hashes

Hashes for ultralytics_disk_cache_hook-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2ef9cb45906c65963579a6dd53194a949c1f2a7b4825bbf8b8bad40eb4553006
MD5 986752c35dae7ec1b6f5384fd802128f
BLAKE2b-256 458bdf9f8dc32c3440a2a65dfb8ac10ce349e5e5b4d99832b249f664353edfb1

See more details on using hashes here.

Provenance

The following attestation bundles were made for ultralytics_disk_cache_hook-0.2.0.tar.gz:

Publisher: publish.yml on xx025/ultralytics-disk-cache-hook

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ultralytics_disk_cache_hook-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ultralytics_disk_cache_hook-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c028aab1f35d1e33dcd25589b425eceaa5f955e12e0a38bf7543d296222a780f
MD5 696302bfcdf30a86f3f6847a2010495f
BLAKE2b-256 1abc4b1a84f1eba08a8830a5d9d726900243230fbf5e2347f6f59b33d34b8c18

See more details on using hashes here.

Provenance

The following attestation bundles were made for ultralytics_disk_cache_hook-0.2.0-py3-none-any.whl:

Publisher: publish.yml on xx025/ultralytics-disk-cache-hook

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page