您好,我正在使用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和train.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 object at 0x7f9e185347d0>
为了完成培训,我如何解决这个问题
谢谢
我以前遇到过这个问题。因此,我开发了一个python脚本来测试有效图像文件的培训和测试目录。文件扩展名必须是jpg、png、bmp或gif中的一个,因此它首先检查扩展名是否正确。然后它尝试使用cv2读取图像。如果未输入有效图像,则会创建异常。在每种情况下,都会打印出错误的文件名。最后,一个名为bad_list的列表包含坏文件路径列表。注意:目录名称必须为“test”和“train”
我不知道这是否仍然相关,但对于将来会遇到同样问题的人:
在此特定情况下,dog_cat数据集中有两个损坏的文件:
只要移除它们,它就会工作
尝试此功能检查图像的格式是否正确
相关问题 更多 >
编程相关推荐