Tensorflow Keras未知错误:未识别的图像错误

2024-05-04 16:51:38 发布

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

我从this网站下载了疟疾检测数据集。之后,我将图像更新到我的google drive,并尝试使用内置的fit()函数训练神经网络,如下所示:

train_gen = train_aug.flow_from_directory(
    training_data_dir,
    class_mode="categorical",
    target_size=(64, 64),
    color_mode="rgb",
    shuffle=True,
    batch_size=BATCH_SIZE)

val_gen = val_aug.flow_from_directory(
    validation_data_dir,
    class_mode="categorical",
    target_size=(64, 64),
    color_mode="rgb",
    shuffle=False,
    batch_size=BATCH_SIZE)

history = model.fit(x=train_gen, steps_per_epoch=steps_per_epoch, epochs=EPOCH_NUM, 
      validation_data=val_gen, validation_steps=val_steps, callbacks=CALLBACKS)

在训练中,我得到以下错误信息:

Epoch 1/100
302/603 [==============>...............] - ETA: 44:54 - loss: 8.3442 - binary_accuracy: 0.4935
---------------------------------------------------------------------------
UnknownError                              Traceback (most recent call last)
<ipython-input-45-2fe1e94cba86> in <module>()
      1 history = model.fit(x=train_gen, steps_per_epoch=steps_per_epoch, epochs=EPOCH_NUM, 
----> 2       validation_data=val_gen, validation_steps=val_steps, callbacks=CALLBACKS)

8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

UnknownError:  UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f42ff5c2518>
Traceback (most recent call last):

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/script_ops.py", line 243, in __call__
    ret = func(*args)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py", line 309, in wrapper
    return func(*args, **kwargs)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 785, in generator_py_func
    values = next(generator_state.get_iterator(iterator_id))

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py", line 801, in wrapped_generator
    for data in generator_fn():

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py", line 932, in generator_fn
    yield x[i]

  File "/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/iterator.py", line 65, in __getitem__
    return self._get_batches_of_transformed_samples(index_array)

  File "/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/iterator.py", line 230, in _get_batches_of_transformed_samples
    interpolation=self.interpolation)

  File "/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/utils.py", line 114, in load_img
    img = pil_image.open(io.BytesIO(f.read()))

  File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 2862, in open
    "cannot identify image file %r" % (filename if filename else fp)

PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f42ff5c2518>


     [[{{node PyFunc}}]]
     [[IteratorGetNext]] [Op:__inference_train_function_35711]

Function call stack:
train_function

这个错误到底是什么?我如何正确处理它?我是否需要对GradientTape对象使用自定义训练循环,然后使用try/catch块,还是有其他方法

让我困惑的是,似乎有些图像无法解码或诸如此类的东西。但是,为什么ImageDataGenerator在训练前没有报告任何错误


Tags: inpyimagedatalibpackagesusrlocal