Reputation: 47
I have seen Tensorflow Keras error: Unknown image file format. One of JPEG, PNG, GIF, BMP required and Unknown image file format. One of JPEG, PNG, GIF, BMP required these answers. It did not help me completely
I am building a simple CNN in google colab
Epoch 1/5
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-29-a98bc2c91ee1> in <module>
----> 1 history = model_1.fit(train_data, epochs=5, steps_per_epoch=len(train_data), validation_data=test_data, validation_steps=int(0.25 * len(test_data)))
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
53 ctx.ensure_initialized()
54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:
InvalidArgumentError: Graph execution error:
2 root error(s) found.
(0) INVALID_ARGUMENT: Unknown image file format. One of JPEG, PNG, GIF, BMP required.
[[{{node decode_image/DecodeImage}}]]
[[IteratorGetNext]]
[[categorical_crossentropy/softmax_cross_entropy_with_logits/Shape_2/_10]]
(1) INVALID_ARGUMENT: Unknown image file format. One of JPEG, PNG, GIF, BMP required.
[[{{node decode_image/DecodeImage}}]]
[[IteratorGetNext]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_31356]
I am getting the above error. The error is while I try to fit the model Using the previous answers that I have linked, I have verified that there are no improper images in my folders. All images are jpeg only.
My code:
import tensorflow as tf
# Create training and test directory paths
train_dir = 'Dataset/train'
test_dir = 'Dataset/test'
IMG_SIZE = (224,224)
BATCH_SIZE=32
# Set up data loaders
import tensorflow as tf
IMG_SIZE = (224,224)
BATCH_SIZE=32
train_data = tf.keras.preprocessing.image_dataset_from_directory(directory=train_dir,
image_size=IMG_SIZE,
label_mode='categorical',
batch_size=BATCH_SIZE)
test_data = tf.keras.preprocessing.image_dataset_from_directory(directory=test_dir, image_size=IMG_SIZE, batch_size=BATCH_SIZE, label_mode='categorical')
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
data_augmentation = keras.Sequential([
preprocessing.RandomFlip('horizontal'),
preprocessing.RandomRotation(0.2),
preprocessing.RandomZoom(0.2),
preprocessing.RandomHeight(0.2),
preprocessing.RandomWidth(0.2),
# preprocessing.Rescale(1/255.) Keep this model for ResNet. Efficient Net has rescaling buit in
], name='data_augmentation')
input_shape = (224,224,3)
base_model = tf.keras.applications.EfficientNetB0(include_top=False)
base_model.trainable=False
# Create the input layer
inputs = layers.Input(shape=input_shape, name='input_layer')
x=data_augmentation(inputs)
# Give base model the inputs after augmentation.. Dont train it
x = base_model(x,training=False)
x = layers.GlobalAveragePooling2D()(x)
# Add a dense layer for output
outputs = layers.Dense(9, activation='softmax', name='output_layer')(x)
# Make a model using the inputs and outputs
model_1 = keras.Model(inputs,outputs)
# Compile the model
model_1.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model_1.fit(train_data, epochs=5, steps_per_epoch=len(train_data), validation_data=test_data, validation_steps=int(0.25 * len(test_data)))
I have downloaded all the images from google search only. Link to dataset: https://drive.google.com/file/d/1dKgzyq2lUF87ggZQ80KUhINhmtVrC_p-/view?usp=sharing
Upvotes: 3
Views: 676
Reputation: 11
I resolved the issue after removing some .DS*
files and .*.jpg
files on some directories.
The zipped file contains some invalid graphic files which naming are staring with .OOO
Upvotes: 1
Reputation: 84
Please just because an image has a .jpeg or .png extension doesn't mean it is good. Some images can have a correct extension and still be bad. Apart from extensions, the image may still have bad binaries which is what makes an image corrupt. You need a code base to properly fish out corrupt images from your directory during preprocessing.
Upvotes: 2