UnidentifiedImageError: cannot identify image file

2024/11/15 2:13:42

Hello I am training a model with TensorFlow and Keras, and the dataset was downloaded from https://www.microsoft.com/en-us/download/confirmation.aspx?id=54765

This is a zip folder that I split in the following directories:

.
├── test
│   ├── Cat
│   └── Dog
└── train├── Cat└── Dog

Test.cat and test.dog have each folder 1000 jpg photos, and train.cat and traing.dog have each folder 11500 jpg photos.

The load is doing with this code:

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 datatest_datagen = ImageDataGenerator(rescale=1./255)
# Test dataset
test_datagen = test_datagen.flow_from_directory('PetImages/test')

THe model is training with the following code:

history = model.fit(train_generator,validation_data=validation_generator,epochs=5)

And i get the following input:

Epoch 1/5
1150/1150 [==============================] - ETA: 0s - loss: 0.0505 - accuracy: 0.9906

But when the epoch is in this point I get the following error:

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

How can I solve this, in order to finish the training?

Thanks

Answer

Try this function to check if the image are all in correct format.

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])
https://en.xdnf.cn/q/72027.html

Related Q&A

Pydub raw audio data

Im using Pydub in Python 3.4 to try to detect the pitch of some audio files.I have a working pitch detection algorithm (McLeod Pitch Method), which is robust for real-time applications (I even made an …

Create Duplicate Rows and Change Values in Specific Columns

How to create x amount of duplicates based on a row in the dataframe and change a single or multi variables from specific columns. The rows are then added to the end of the same dataframe.A B C D E F 0…

writing and saving CSV file from scraping data using python and Beautifulsoup4

I am trying to scrape data from the PGA.com website to get a table of all of the golf courses in the United States. In my CSV table I want to include the Name of the golf course ,Address ,Ownership ,We…

Performance issue turning rows with start - end into a dataframe with TimeIndex

I have a large dataset where each line represents the value of a certain type (think a sensor) for a time interval (between start and end). It looks like this: start end type value 2015-01-01…

How can I create a key using RSA/ECB/PKCS1Padding in python?

I am struggling to find any method of using RSA in ECB mode with PKCS1 padding in python. Ive looked into pyCrypto, but they dont have PKCS1 padding in the master branch (but do in a patch). Neverthel…

Do full-outer-join with pandas.merge_asof

Hi I need to align some time series data with nearest timestamps, so I think pandas.merge_asof could be a good candidate. However, it does not have an option to set how=outer like in the standard merge…

order of calling constructors in Python

#!/usr/bin/pythonclass Parent(object): # define parent classparentAttr = 100def __init__(self):print "Calling parent constructor"def parentMethod(self):print Calling parent methoddef s…

How do I access data from a python thread

I have a very simple threading example using Python 3.4.2. In this example I am creating a five threads that just returns the character string "Result" and appends it to an array titled thre…

How to tell if a full-screen application is running?

Is it possible in python to tell if a full screen application on linux is running? I have a feeling it might be possible using Xlib but I havent found a way.EDIT: By full screen I mean the WHOLE scree…

Pretty printers for maps throwing a type error

Ive configured pretty printers using http://wiki.eclipse.org/CDT/User/FAQ#How_can_I_inspect_the_contents_of_STL_containers.3F. It successfully works for vector and other containers. However I cant get …