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

file {i} does not exists.

Error message

file {i} does not exists.

What it means

DriveDataset.__init__ builds paths to the '1st_manual' folder (ground-truth manual segmentations named <id>_manual1.gif) and raises FileNotFoundError if any expected manual file is missing from the DRIVE dataset directory. The dataset validates all files up front so failures surface at construction, not during epoch iteration.

Source

Thrown at pytorch_segmentation/unet/my_dataset.py:21

import numpy as np
from torch.utils.data import Dataset


class DriveDataset(Dataset):
    def __init__(self, root: str, train: bool, transforms=None):
        super(DriveDataset, self).__init__()
        self.flag = "training" if train else "test"
        data_root = os.path.join(root, "DRIVE", self.flag)
        assert os.path.exists(data_root), f"path '{data_root}' does not exists."
        self.transforms = transforms
        img_names = [i for i in os.listdir(os.path.join(data_root, "images")) if i.endswith(".tif")]
        self.img_list = [os.path.join(data_root, "images", i) for i in img_names]
        self.manual = [os.path.join(data_root, "1st_manual", i.split("_")[0] + "_manual1.gif")
                       for i in img_names]
        # check files
        for i in self.manual:
            if os.path.exists(i) is False:
                raise FileNotFoundError(f"file {i} does not exists.")

        self.roi_mask = [os.path.join(data_root, "mask", i.split("_")[0] + f"_{self.flag}_mask.gif")
                         for i in img_names]
        # check files
        for i in self.roi_mask:
            if os.path.exists(i) is False:
                raise FileNotFoundError(f"file {i} does not exists.")

    def __getitem__(self, idx):
        img = Image.open(self.img_list[idx]).convert('RGB')
        manual = Image.open(self.manual[idx]).convert('L')
        manual = np.array(manual) / 255
        roi_mask = Image.open(self.roi_mask[idx]).convert('L')
        roi_mask = 255 - np.array(roi_mask)
        mask = np.clip(manual + roi_mask, a_min=0, a_max=255)

        # 这里转回PIL的原因是,transforms中是对PIL数据进行处理
        mask = Image.fromarray(mask)

View on GitHub (pinned to 1ec3fe6f37)

Solutions

  1. Re-download/restore the full DRIVE dataset so 1st_manual contains all <id>_manual1.gif files
  2. Verify each images/<id>_training.png has a matching 1st_manual/<id>_manual1.gif
  3. If using your own data, restructure it to the DRIVE layout or edit my_dataset.py to match your naming

Example fix

// before
self.manual = [os.path.join(data_root, "1st_manual", i.split("_")[0] + "_manual1.gif") for i in img_names]
// after
# ensure the file exists, e.g. restore DRIVE/1st_manual/21_manual1.gif missing from the download
Defensive patterns

Strategy: validation

Validate before calling

import os, glob
manual_dir = os.path.join(data_root, "DRIVE", "train", "1st_manual")
missing = [os.path.basename(p) for p in glob.glob(os.path.join(img_dir, "*_training.png"))
           if not os.path.exists(os.path.join(manual_dir, os.path.basename(p).split("_")[0] + "_manual1.gif"))]
assert not missing, f"missing manual files: {missing}"

Type guard

def drive_manual_ok(data_root: str, split: str = "train") -> bool:
    return os.path.isdir(os.path.join(data_root, "DRIVE", split, "1st_manual"))

Try / catch

try:
    dataset = DriveDataset(data_root, train=True, transforms=transforms)
except FileNotFoundError as e:
    print(f"dataset incomplete, re-extract DRIVE: {e}"); raise

Prevention

When it happens

Trigger: Constructing DriveDataset with a data_root whose DRIVE/train/1st_manual (or test/1st_manual) directory lacks the <id>_manual1.gif file for one of the img_names, or img_names don't follow the DRIVE naming convention (e.g. 21_training.png expects 21_manual1.gif).

Common situations: Incomplete/partial download of the DRIVE dataset, renamed manual files, extracting only the 'images' folder, or pointing data_path at a custom dataset not structured like DRIVE.

Related errors


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