WZMIAOMIAO/deep-learning-for-image-processing · error · FileNotFoundError

Error loading data from {}. {}

Error message

Error loading data from {}. {}

What it means

The dataset __init__ wraps the whole listing-file parsing in try/except and re-raises any underlying failure (missing file, decode error, IO error) as FileNotFoundError('Error loading data from {path}. {e}'). It preserves the original exception message while normalizing the type.

Source

Thrown at pytorch_object_detection/yolov3_spp/build_utils/datasets.py:78

                 cache_images=False,  # 是否缓存图片到内存中
                 single_cls=False, pad=0.0, rank=-1):

        try:
            path = str(Path(path))
            # parent = str(Path(path).parent) + os.sep
            if os.path.isfile(path):  # file
                # 读取对应my_train/val_data.txt文件,读取每一行的图片路劲信息
                with open(path, "r") as f:
                    f = f.read().splitlines()
            else:
                raise Exception("%s does not exist" % path)

            # 检查每张图片后缀格式是否在支持的列表中,保存支持的图像路径
            # img_formats = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.dng']
            self.img_files = [x for x in f if os.path.splitext(x)[-1].lower() in img_formats]
            self.img_files.sort()  # 防止不同系统排序不同,导致shape文件出现差异
        except Exception as e:
            raise FileNotFoundError("Error loading data from {}. {}".format(path, e))

        # 如果图片列表中没有图片,则报错
        n = len(self.img_files)
        assert n > 0, "No images found in %s. See %s" % (path, help_url)

        # batch index
        # 将数据划分到一个个batch中
        bi = np.floor(np.arange(n) / batch_size).astype(np.int)
        # 记录数据集划分后的总batch数
        nb = bi[-1] + 1  # number of batches

        self.n = n  # number of images 图像总数目
        self.batch = bi  # batch index of image 记录哪些图片属于哪个batch
        self.img_size = img_size  # 这里设置的是预处理后输出的图片尺寸
        self.augment = augment  # 是否启用augment_hsv
        self.hyp = hyp  # 超参数字典,其中包含图像增强会使用到的超参数
        self.rect = rect  # 是否使用rectangular training
        # 注意: 开启rect后,mosaic就默认关闭

View on GitHub (pinned to 1ec3fe6f37)

Solutions

  1. Read the chained message after '{}.' to see the real cause and fix that (usually a missing file path)
  2. Verify the listing file exists and is readable: os.path.isfile(path) and open(path).readline()
  3. Regenerate the listing file and check line endings/encoding (UTF-8, no BOM)

Example fix

// before
dataset = LoadImagesAndLabels('data/my_train (copy).txt', img_size=512)
# FileNotFoundError: Error loading data from data/my_train (copy).txt. [Errno 2] No such file...
// after
dataset = LoadImagesAndLabels('data/my_train.txt', img_size=512)
Defensive patterns

Strategy: try-catch

Validate before calling

import os
assert os.path.isfile(txt), f'{txt} is not a readable file'

Try / catch

try:
    dataset = LoadImagesAndLabels(txt, img_size=img_size)
except FileNotFoundError as e:
    print(e)  # chained message reveals the real cause
    raise

Prevention

When it happens

Trigger: Any exception raised while opening/reading the image-listing file: the file does not exist, permission denied, encoding/decoding error, or a nested error during splitlines/filtering.

Common situations: Missing or corrupted my_train.txt; wrong path separator on Windows; file saved with unexpected encoding; root cause visible after the 'Error loading data from' prefix.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30). Data as JSON: /api/errors/7eda4dac19a14005. Report an issue: GitHub.