From 4ad4a6cd96ea9b7aad6a57f7443855679662c197 Mon Sep 17 00:00:00 2001 From: GdoongMathew Date: Thu, 27 Mar 2025 23:53:56 +0800 Subject: [PATCH 1/3] fix: a temporary fix to unittests failing in `INaturalistTestCase`. --- test/common_utils.py | 13 ++++++------- test/test_datasets.py | 2 -- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/test/common_utils.py b/test/common_utils.py index 9a4b41e606f..f422f92920e 100644 --- a/test/common_utils.py +++ b/test/common_utils.py @@ -34,14 +34,13 @@ @contextlib.contextmanager def get_tmp_dir(src=None, **kwargs): - tmp_dir = tempfile.mkdtemp(**kwargs) - if src is not None: - os.rmdir(tmp_dir) - shutil.copytree(src, tmp_dir) - try: + with tempfile.TemporaryDirectory( + ignore_cleanup_errors=True, # todo remove this line once we find why in windows it is not deleting the temp dir + **kwargs, + ) as tmp_dir: + if src is not None: + shutil.copytree(src, tmp_dir) yield tmp_dir - finally: - shutil.rmtree(tmp_dir) def set_rng_seed(seed): diff --git a/test/test_datasets.py b/test/test_datasets.py index feaabd7acd2..84f243388aa 100644 --- a/test/test_datasets.py +++ b/test/test_datasets.py @@ -11,7 +11,6 @@ import re import shutil import string -import sys import unittest import xml.etree.ElementTree as ET import zipfile @@ -1904,7 +1903,6 @@ def test_class_to_idx(self): assert dataset.class_to_idx == class_to_idx -@pytest.mark.skipif(sys.platform in ("win32", "cygwin"), reason="temporarily disabled on Windows") class INaturalistTestCase(datasets_utils.ImageDatasetTestCase): DATASET_CLASS = datasets.INaturalist FEATURE_TYPES = (PIL.Image.Image, (int, tuple)) From eb47d937b60f4c4e971968b6af5b8dc009eb02f7 Mon Sep 17 00:00:00 2001 From: GdoongMathew Date: Sun, 30 Mar 2025 18:02:11 +0800 Subject: [PATCH 2/3] fix: move actual loading in `INaturalist` to __getitem__. --- test/common_utils.py | 2 +- torchvision/datasets/inaturalist.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test/common_utils.py b/test/common_utils.py index f422f92920e..3ab961fa21a 100644 --- a/test/common_utils.py +++ b/test/common_utils.py @@ -35,7 +35,7 @@ @contextlib.contextmanager def get_tmp_dir(src=None, **kwargs): with tempfile.TemporaryDirectory( - ignore_cleanup_errors=True, # todo remove this line once we find why in windows it is not deleting the temp dir + ignore_cleanup_errors=False, # todo remove this line once we find why in windows it is not deleting the temp dir **kwargs, ) as tmp_dir: if src is not None: diff --git a/torchvision/datasets/inaturalist.py b/torchvision/datasets/inaturalist.py index 8713bc041db..973f90fc32b 100644 --- a/torchvision/datasets/inaturalist.py +++ b/torchvision/datasets/inaturalist.py @@ -113,7 +113,7 @@ def __init__( for fname in files: self.index.append((dir_index, fname)) - self.loader = loader or Image.open + self.loader = loader def _init_2021(self) -> None: """Initialize based on 2021 layout""" @@ -184,7 +184,8 @@ def __getitem__(self, index: int) -> Tuple[Any, Any]: """ cat_id, fname = self.index[index] - img = self.loader(os.path.join(self.root, self.all_categories[cat_id], fname)) + image_path = os.path.join(self.root, self.all_categories[cat_id], fname) + img = self.loader(image_path) if self.loader is not None else Image.open(image_path) target: Any = [] for t in self.target_type: From 9d20e31ce2f162e0b247a8ccf071df97191897dc Mon Sep 17 00:00:00 2001 From: GdoongMathew Date: Sun, 30 Mar 2025 18:48:02 +0800 Subject: [PATCH 3/3] cleanup: remove ignore_cleanup_error args. --- test/common_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/common_utils.py b/test/common_utils.py index 3ab961fa21a..9acbd9f9ab8 100644 --- a/test/common_utils.py +++ b/test/common_utils.py @@ -35,7 +35,6 @@ @contextlib.contextmanager def get_tmp_dir(src=None, **kwargs): with tempfile.TemporaryDirectory( - ignore_cleanup_errors=False, # todo remove this line once we find why in windows it is not deleting the temp dir **kwargs, ) as tmp_dir: if src is not None: