gpt4 book ai didi

python - 未识别的图像错误 : cannot identify image file

转载 作者:行者123 更新时间:2023-12-04 14:30:47 25 4
gpt4 key购买 nike

你好,我正在用 TensorFlow 和 Keras 训练一个模型,数据集是从 https://www.microsoft.com/en-us/download/confirmation.aspx?id=54765 下载的
这是我在以下目录中拆分的 zip 文件夹:

.
├── test
│   ├── Cat
│   └── Dog
└── train
├── Cat
└── Dog
test.cat和test.dog每个文件夹有1000张jpg照片,train.cat和traing.dog每个文件夹有11500张jpg照片。
负载是用这个代码做的:
batch_size = 16

# Data augmentation and preprocess
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
validation_split=0.20) # set validation split

# Train dataset
train_generator = train_datagen.flow_from_directory(
'PetImages/train',
target_size=(244, 244),
batch_size=batch_size,
class_mode='binary',
subset='training') # set as training data

# Validation dataset
validation_generator = train_datagen.flow_from_directory(
'PetImages/train',
target_size=(244, 244),
batch_size=batch_size,
class_mode='binary',
subset='validation') # set as validation data

test_datagen = ImageDataGenerator(rescale=1./255)
# Test dataset
test_datagen = test_datagen.flow_from_directory(
'PetImages/test')
该模型正在使用以下代码进行训练:
history = model.fit(train_generator,
validation_data=validation_generator,
epochs=5)
我得到以下输入:
Epoch 1/5
1150/1150 [==============================] - ETA: 0s - loss: 0.0505 - accuracy: 0.9906
但是当时代在这一点上时,我收到以下错误:

UnidentifiedImageError: cannot identify image file <_io.BytesIO objectat 0x7f9e185347d0>


为了完成培训,我该如何解决这个问题?
谢谢

最佳答案

试试这个功能来检查图像是否都是正确的格式。

import os
from PIL import Image
folder_path = 'data\img'
extensions = []
for fldr in os.listdir(folder_path):
sub_folder_path = os.path.join(folder_path, fldr)
for filee in os.listdir(sub_folder_path):
file_path = os.path.join(sub_folder_path, filee)
print('** Path: {} **'.format(file_path), end="\r", flush=True)
im = Image.open(file_path)
rgb_im = im.convert('RGB')
if filee.split('.')[1] not in extensions:
extensions.append(filee.split('.')[1])

关于python - 未识别的图像错误 : cannot identify image file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63754311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com