Keras 凯拉';ImageDataGenerator随机崩溃

Keras 凯拉';ImageDataGenerator随机崩溃,keras,Keras,我有以下结构,我想从测试中读取jpg文件 ./cats_dogs_small ├── test │ ├── cats <- 1000 images │ └── dogs <- 1000 images 每次运行时,我都会收到相同的错误消息: 2020-11-19 16:08:56.973416: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic li

我有以下结构,我想从测试中读取jpg文件

./cats_dogs_small
├── test
│   ├── cats  <- 1000 images
│   └── dogs  <- 1000 images
每次运行时,我都会收到相同的错误消息:

2020-11-19 16:08:56.973416: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory
2020-11-19 16:08:56.973436: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Found 2000 images belonging to 2 classes.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 Traceback (most recent call last):

  File "/~/Documents/keras/untitled0.py", line 30, in <module>
    train_features, train_labels = extract_features(train_dir)

  File "/~/Documents/keras/untitled0.py", line 25, in extract_features
    for inputs_batch, labels_batch in generator:

  File "/.local/lib/python3.8/site-packages/keras_preprocessing/image/iterator.py", line 104, in __next__
    return self.next(*args, **kwargs)

  File "/.local/lib/python3.8/site-packages/keras_preprocessing/image/iterator.py", line 116, in next
    return self._get_batches_of_transformed_samples(index_array)

  File "/.local/lib/python3.8/site-packages/keras_preprocessing/image/iterator.py", line 227, in _get_batches_of_transformed_samples
    img = load_img(filepaths[j],

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

  File "/anaconda3/envs/keras28/lib/python3.8/site-packages/PIL/Image.py", line 2943, in open
    raise UnidentifiedImageError(

UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f41286f3090>
2020-11-19 16:08:56.973416:W tensorflow/stream_executor/platform/default/dso_loader.cc:59]无法加载动态库“libcudart.so.10.1”;dlerror:libcudart.so.10.1:无法打开共享对象文件:没有此类文件或目录
2020-11-19 16:08:56.973436:I tensorflow/stream_executor/cuda/cudart_stub.cc:29]如果您的机器上没有设置GPU,请忽略上面的cudart dler错误。
找到了2000张属于2个类的图片。
01 2 3 4 5 6 7 8 9 10 11 12 14 15 17 18 19 20 21 22 23 24 25 27 28 30 31 32 33 35 36 37 38 39 40 41 42 44 45 46 47 48 49 50 51 52 54 55 56 57 58 59 60回溯(最后一次通话):
文件“/~/Documents/keras/untitled0.py”,第30行,在
列车特征,列车标签=提取列车特征(列车方向)
文件“/~/Documents/keras/untitled0.py”,第25行,在extract_features中
对于输入\u批次,在生成器中标记\u批次:
文件“/.local/lib/python3.8/site packages/keras_preprocessing/image/iterator.py”,第104行,下一行__
返回self.next(*args,**kwargs)
文件“/.local/lib/python3.8/site packages/keras_preprocessing/image/iterator.py”,下一页第116行
返回self.\u获取\u批次\u转换的\u样本(索引\u数组)
文件“/.local/lib/python3.8/site packages/keras_preprocessing/image/iterator.py”,第227行,在转换样本的“获取批次”中
img=load_img(文件路径[j],
文件“/.local/lib/python3.8/site packages/keras\u preprocessing/image/utils.py”,第114行,在load\u img中
img=pil_image.open(io.BytesIO(f.read()))
打开文件“/anaconda3/envs/keras28/lib/python3.8/site packages/PIL/Image.py”,第2943行
引发无法识别的图像错误(
UnidentifiedImageError:无法识别图像文件
错误随机出现。我在这里发布了代码在60时崩溃,但有时在43、69或任何其他数字时崩溃。问题似乎与特定图像无关,但与我使用来自目录/ImageDataGenerator的flow_的方式有关


Keras版本:2.4.3

检查路径是否存在,如果PIL能够打开JPEG文件,@Prabindh谢谢!运行您引用的代码,发现问题是损坏的图像。使用修复的图像重新运行上述代码,并按预期工作
2020-11-19 16:08:56.973416: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory
2020-11-19 16:08:56.973436: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Found 2000 images belonging to 2 classes.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 Traceback (most recent call last):

  File "/~/Documents/keras/untitled0.py", line 30, in <module>
    train_features, train_labels = extract_features(train_dir)

  File "/~/Documents/keras/untitled0.py", line 25, in extract_features
    for inputs_batch, labels_batch in generator:

  File "/.local/lib/python3.8/site-packages/keras_preprocessing/image/iterator.py", line 104, in __next__
    return self.next(*args, **kwargs)

  File "/.local/lib/python3.8/site-packages/keras_preprocessing/image/iterator.py", line 116, in next
    return self._get_batches_of_transformed_samples(index_array)

  File "/.local/lib/python3.8/site-packages/keras_preprocessing/image/iterator.py", line 227, in _get_batches_of_transformed_samples
    img = load_img(filepaths[j],

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

  File "/anaconda3/envs/keras28/lib/python3.8/site-packages/PIL/Image.py", line 2943, in open
    raise UnidentifiedImageError(

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