{"record":{"id":"7eda4dac19a14005","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"error-loading-data-from","errorCode":null,"errorMessage":"Error loading data from {}. {}","messagePattern":"Error loading data from (.+?)\\. (.+?)","errorType":"exception","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"pytorch_object_detection/yolov3_spp/build_utils/datasets.py","lineNumber":78,"sourceCode":"                 cache_images=False,  # 是否缓存图片到内存中\n                 single_cls=False, pad=0.0, rank=-1):\n\n        try:\n            path = str(Path(path))\n            # parent = str(Path(path).parent) + os.sep\n            if os.path.isfile(path):  # file\n                # 读取对应my_train/val_data.txt文件，读取每一行的图片路劲信息\n                with open(path, \"r\") as f:\n                    f = f.read().splitlines()\n            else:\n                raise Exception(\"%s does not exist\" % path)\n\n            # 检查每张图片后缀格式是否在支持的列表中，保存支持的图像路径\n            # img_formats = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.dng']\n            self.img_files = [x for x in f if os.path.splitext(x)[-1].lower() in img_formats]\n            self.img_files.sort()  # 防止不同系统排序不同，导致shape文件出现差异\n        except Exception as e:\n            raise FileNotFoundError(\"Error loading data from {}. {}\".format(path, e))\n\n        # 如果图片列表中没有图片，则报错\n        n = len(self.img_files)\n        assert n > 0, \"No images found in %s. See %s\" % (path, help_url)\n\n        # batch index\n        # 将数据划分到一个个batch中\n        bi = np.floor(np.arange(n) / batch_size).astype(np.int)\n        # 记录数据集划分后的总batch数\n        nb = bi[-1] + 1  # number of batches\n\n        self.n = n  # number of images 图像总数目\n        self.batch = bi  # batch index of image 记录哪些图片属于哪个batch\n        self.img_size = img_size  # 这里设置的是预处理后输出的图片尺寸\n        self.augment = augment  # 是否启用augment_hsv\n        self.hyp = hyp  # 超参数字典，其中包含图像增强会使用到的超参数\n        self.rect = rect  # 是否使用rectangular training\n        # 注意: 开启rect后，mosaic就默认关闭","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_object_detection/yolov3_spp/build_utils/datasets.py#L60-L96","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the chained message after '{}.' to see the real cause and fix that (usually a missing file path)","Verify the listing file exists and is readable: os.path.isfile(path) and open(path).readline()","Regenerate the listing file and check line endings/encoding (UTF-8, no BOM)"],"exampleFix":"// before\ndataset = LoadImagesAndLabels('data/my_train (copy).txt', img_size=512)\n# FileNotFoundError: Error loading data from data/my_train (copy).txt. [Errno 2] No such file...\n// after\ndataset = LoadImagesAndLabels('data/my_train.txt', img_size=512)","handlingStrategy":"try-catch","validationCode":"import os\nassert os.path.isfile(txt), f'{txt} is not a readable file'","typeGuard":null,"tryCatchPattern":"try:\n    dataset = LoadImagesAndLabels(txt, img_size=img_size)\nexcept FileNotFoundError as e:\n    print(e)  # chained message reveals the real cause\n    raise","preventionTips":["Read the chained '{e}' part of the message to identify the root cause","Check file permissions and encoding (UTF-8) of the listing file","Validate the listing path with os.path.isfile before instantiating the dataset"],"tags":["python","file-not-found","yolo","dataset"],"backgroundTag":"file-not-found","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}