imread FileNotFoundError:没有这样的文件:

2024-04-20 04:32:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Sequence类创建了一个数据生成器

import tensorflow.keras as keras
from skimage.io import imread
from skimage.transform import resize
import numpy as np
import math
from tensorflow.keras.utils import Sequence

# Here, `x_set` is list of path to the images
# and `y_set` are the associated classes.

class DataGenerator(Sequence):

    def __init__(self, x_set, y_set, batch_size):
        self.x, self.y = x_set, y_set
        self.batch_size = batch_size

    def __len__(self):
        return math.ceil(len(self.x) / self.batch_size)

    def __getitem__(self, idx):
        batch_x = self.x[idx * self.batch_size:(idx + 1) *
        self.batch_size]
        batch_y = self.y[idx * self.batch_size:(idx + 1) *
        self.batch_size]

        return np.array([
            resize(imread(file_name), (224, 224))
               for file_name in batch_x]), np.array(batch_y)

然后,我将其应用于我的培训和验证数据X_train是字符串列表,其中包含指向训练数据的图像路径y_train是训练数据的一个热编码标签。验证数据也是如此。 我将数据生成器应用于培训和验证数据:

training_generator = DataGenerator(X_train, y_train, batch_size=32)
validation_generator = DataGenerator(X_val, y_val, batch_size=32)

之后,我使用fit_generator方法运行了一个模型:

model.fit_generator(generator=training_generator,
                    validation_data=validation_generator,
                    steps_per_epoch = num_train_samples // 32,
                    validation_steps = num_val_samples // 32,
                    epochs = 10,
                    use_multiprocessing=True,
                    workers=2)

并得到以下错误:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-79-f43ade94ee10> in <module>()
      5                     epochs = 10,
      6                     use_multiprocessing=True,
----> 7                     workers=2)

16 frames
/usr/local/lib/python3.6/dist-packages/imageio/core/request.py in _parse_uri(self, uri)
    271                 # Reading: check that the file exists (but is allowed a dir)
    272                 if not os.path.exists(fn):
--> 273                     raise FileNotFoundError("No such file: '%s'" % fn)
    274             else:
    275                 # Writing: check that the directory to write to does exist

FileNotFoundError: No such file: 'path/to/images/1_1.png'

在我看来,imread方法找不到该文件。我检查了图像路径,它是正确的

有人知道我应该在程序中更改什么吗


Tags: theto数据importselfsizebatchtrain
2条回答

您正在使用的文件名包含wildcards
opencv中的imread方法根本不支持它们。
您需要在不使用通配符的情况下提供文件名

开始检查x_set变量(在实例化中,这转换为X_train):打印出此图像路径列表的一小部分,例如

print(f"{X_train[:2] = }")

然后开始检查这些图像路径中的一个是否存在,以及它是否与您从Python脚本指向的路径相同

相关问题 更多 >