0

I am training a convolutional neural network to classify 11 classes of 11 different characters of a particular language(Gujarati). Below is my code:

TRAINING_DIR = "/tmp/characters/train1_set/"

train_datagen = ImageDataGenerator(rescale=1./255,
                                   shear_range=0.2,
                                   zoom_range=0.2,
                                   horizontal_flip=True)

train_generator = train_datagen.flow_from_directory(TRAINING_DIR,
                                                    batch_size=64,
                                                    class_mode='categorical',
                                                    target_size=(64,64))

VALIDATION_DIR = "/tmp/characters/test1_test/"

validation_datagen = ImageDataGenerator(rescale=1./255)
validation_generator = validation_datagen.flow_from_directory(VALIDATION_DIR,
                                                              batch_size=64,
                                                              class_mode='categorical',
                                                              target_size=(64,64))

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    
    tf.keras.layers.Dense(11, activation='softmax')
])

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])

history = model.fit_generator(train_generator,
                              steps_per_epoch = 8000,
                              epochs = 30, 
                              verbose = 1,  
                              validation_data=validation_generator)

But I am getting following error:

UnidentifiedImageError                    Traceback (most recent call last)
<ipython-input-25-04de914ce972> in <module>()
      3                               epochs = 30,
      4                               verbose = 1,
----> 5                               validation_data=validation_generator)
      6 

11 frames
/usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)
   2860         warnings.warn(message)
   2861     raise UnidentifiedImageError(
-> 2862         "cannot identify image file %r" % (filename if filename else fp)
   2863     )
   2864 

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

Please help me resolve the issue!!! Thanks in advance.

1 Answers1

0

It seems that the file the ImageDataGenerator is trying to read is not an image or might be a defect image. You can use Pillow to detect these types of defects very easily. However, the verify() method showed here does not detect truncated images. You could use something like this-

try:
  im = Image.load(filename)
  im.verify()
  im.close()
except: 
  #exceptions
Rishit Dagli
  • 1,000
  • 8
  • 20
  • My all images are in png format and are throwing exceptions of bad file when i run this above code. Will it work if i convert them into jpg format?! –  Sep 02 '20 at 05:55
  • I don't think that would do it, the image might be broken itself – Rishit Dagli Sep 06 '20 at 04:40