Greyscale test fix (#23903)

Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
Glenn Jocher 2026-03-14 15:48:00 +01:00 committed by GitHub
parent 63168d89be
commit 03ec60032b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 14 additions and 10 deletions

View file

@ -795,11 +795,7 @@ def test_grayscale(task: str, model: str, data: str, tmp_path) -> None:
npy_file.unlink()
model = YOLO(model)
model.train(data=grayscale_data, epochs=1, imgsz=32, close_mosaic=1, cache="disk")
# remove npy files in train/val splits if exists, avoiding interference with other tests
for split in {"train", "val"}:
for npy_file in (Path(data["path"]) / data[split]).glob("*.npy"):
npy_file.unlink()
model.train(data=grayscale_data, epochs=1, imgsz=32, close_mosaic=1, cache="ram")
model.val(data=grayscale_data)
im = np.zeros((32, 32, 1), dtype=np.uint8)
model.predict(source=im, imgsz=32, save_txt=True, save_crop=True, augment=True)

View file

@ -280,7 +280,11 @@ class BaseDataset(Dataset):
"""Save an image as an *.npy file for faster loading."""
f = self.npy_files[i]
if not f.exists():
np.save(f.as_posix(), imread(self.im_files[i], flags=self.cv2_flag), allow_pickle=False)
try:
np.save(f.as_posix(), imread(self.im_files[i], flags=self.cv2_flag), allow_pickle=False)
except Exception as e:
f.unlink(missing_ok=True)
LOGGER.warning(f"{self.prefix}WARNING ⚠️ Failed to cache image {f}: {e}")
def check_cache_disk(self, safety_margin: float = 0.5) -> bool:
"""Check if there's enough disk space for caching images.

View file

@ -806,8 +806,12 @@ def save_dataset_cache_file(prefix: str, path: Path, x: dict, version: str):
if is_dir_writeable(path.parent):
if path.exists():
path.unlink() # remove *.cache file if exists
with open(str(path), "wb") as file: # context manager here fixes windows async np.save bug
np.save(file, x)
LOGGER.info(f"{prefix}New cache created: {path}")
try:
with open(str(path), "wb") as file: # context manager here fixes windows async np.save bug
np.save(file, x)
LOGGER.info(f"{prefix}New cache created: {path}")
except Exception as e:
Path(path).unlink(missing_ok=True) # remove partially written file
LOGGER.warning(f"{prefix}WARNING ⚠️ Failed to save cache to {path}: {e}")
else:
LOGGER.warning(f"{prefix}Cache directory {path.parent} is not writable, cache not saved.")

View file

@ -962,7 +962,7 @@ class Metric(SimpleClass):
def fitness(self) -> float:
"""Return model fitness as a weighted combination of metrics."""
w = [0.0, 0.0, 0.0, 1.0] # weights for [P, R, mAP@0.5, mAP@0.5:0.95]
return (np.nan_to_num(np.array(self.mean_results())) * w).sum()
return float((np.nan_to_num(np.array(self.mean_results())) * w).sum())
def update(self, results: tuple):
"""Update the evaluation metrics with a new set of results.