【发布时间】:2020-06-18 14:05:43
【问题描述】:
使用这里的代码https://keras.io/api/utils/python_utils/#sequence-class,我编写了一个自定义数据生成器。
# 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 和 X_val,它们是包含我的图像文件的图像路径的列表以及 y_train 和 y_val,它们是一个热编码标签,并将此数据应用于 DataGenerator。
training_generator = DataGenerator(X_train, y_train, batch_size=32)
validation_generator = DataGenerator(X_val, y_val, batch_size=32)
然后拟合模型:
model.fit_generator(generator=training_generator,
validation_data=validation_generator,
steps_per_epoch = num_train_samples // batch_size,
validation_steps = num_val_samples // batch_size,
epochs = 10,
use_multiprocessing=True,
workers=6)
运行此代码时,我收到此错误:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-54-411e62536182> in <module>()
5 epochs = 10,
6 use_multiprocessing=True,
----> 7 workers=6)
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: '/content/gdrive/My Drive/data/2017-IWT4S-CarsReId_LP-dataset/s01_l01/1_1.png'
X_train 是一个列表,其中包含我在 Google Drive 中的数据的绝对文件路径作为字符串。
X_train[0]
'/content/gdrive/My Drive/data/2017-IWT4S-CarsReId_LP-dataset/s01_l01/1_1.png'
如何修改代码才能让生成器找到我的文件? 这可能是因为 Colab Notebook 文件和数据不在同一个文件夹中? -> 我将我的 Colab Jupyter Notebook 放在文件夹“/content/gdrive/My Drive”中,我也得到了 FileNotFoundError”。 还是我必须路由到正确的文件夹?
【问题讨论】:
标签: python error-handling file-not-found