1

i am new to deep learning. and i am trying to make a cat/dog classifier. i have two folders of cat images and dog images. and i am trying to do transfer learning with mobilenetV2 model. but when the first epoch begins i get interrupted with error.

import matplotlib.pyplot as plt
import numpy as np
import os
import tensorflow as tf
import tensorflow.keras.layers as tfl

from tensorflow.keras.preprocessing import image_dataset_from_directory
from tensorflow.keras.layers.experimental.preprocessing import RandomFlip , RandomRotation

#creating dataset from a directory from our disk and spliting them into train and validation
BATCH_SIZE = 32
IMG_SIZE = (160, 160)
directory = "dataset/PetImages/"
train_dataset = image_dataset_from_directory(directory,
                                             shuffle=True,
                                             batch_size=BATCH_SIZE,
                                             image_size=IMG_SIZE,
                                             validation_split=0.2,
                                             subset='training',
                                             seed=42)
validation_dataset = image_dataset_from_directory(directory,
                                             shuffle=True,
                                             batch_size=BATCH_SIZE,
                                             image_size=IMG_SIZE,
                                             validation_split=0.2,
                                             subset='validation',
                                             seed=42)
#seeing some of the pictures
class_names = train_dataset.class_names

plt.figure(figsize=(10, 10))
for images, labels in train_dataset.take(1):
    for i in range(9):
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(images[i].numpy().astype("uint8"))
        plt.title(class_names[labels[i]])
        plt.axis("off")
        

AUTOTUNE = tf.data.experimental.AUTOTUNE
train_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)

#a function to augment datas
def data_augmenter():
    data_augmentation = tf.keras.Sequential()
    data_augmentation.add(RandomFlip('horizontal'))
    data_augmentation.add(RandomRotation(0.2))
    
    return data_augmentation

#to show augmented datas
data_augmentation = data_augmenter()

for image, _ in train_dataset.take(1):
    plt.figure(figsize=(10, 10))
    first_image = image[0]
    for i in range(9):
        ax = plt.subplot(3, 3, i + 1)
        augmented_image = data_augmentation(tf.expand_dims(first_image, 0))
        plt.imshow(augmented_image[0] / 255)
        plt.axis('off')
  
preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input


IMG_SHAPE = IMG_SIZE + (3,)
base_model = tf.keras.applications.MobileNetV2(input_shape = IMG_SHAPE,
                                               include_top = True,
                                               weights = 'imagenet')

#printing the model summary
base_model.summary()

nb_layers = len(base_model.layers)
print(base_model.layers[nb_layers - 2].name)
print(base_model.layers[nb_layers - 1].name)


image_batch , label_batch = next(iter(train_dataset))
feature_batch = base_model(image_batch)
print(feature_batch.shape)


#shows the different label probabilities in one tensor
label_batch


base_model.trainable = False
image_var = tf.Variable(image_batch)
pred = base_model(image_var)

tf.keras.applications.mobilenet_v2.decode_predictions(pred.numpy(), top = 2)


#layer freezing with the functional API
def catdog_model(image_shape=IMG_SIZE , data_augmentation = data_augmenter()):
    
    input_shape = image_shape + (3,)
    base_model = tf.keras.applications.MobileNetV2(input_shape = input_shape,
                                                   include_top = False,
                                                   weights = 'imagenet')
    base_model.trainable = False
    
    inputs = tf.keras.Input(shape = input_shape)
    x = data_augmentation(inputs)
    x = preprocess_input(x)
    x = base_model(x , training = False)
    x = tf.keras.layers.GlobalAveragePooling2D()(x)
    x = tf.keras.layers.Dropout(0.2)(x)
    prediction_layer = tf.keras.layers.Dense(1 , activation = 'linear')(x)
    
    outputs = prediction_layer
    model = tf.keras.Model(inputs , outputs)
    
    return model


#make an object from our model
model2 = catdog_model(IMG_SIZE , data_augmentation)


base_learning_rate = 0.001
model2.compile(optimizer = tf.keras.optimizers.Adam(learning_rate = base_learning_rate),
               loss = tf.keras.losses.BinaryCrossentropy(from_logits = True),
               metrics = ['accuracy'])

initial_epochs = 5
history = model2.fit(train_dataset, validation_data = validation_dataset, epochs = initial_epochs)

here is the error:

--------------------------------------------------------------------------- 
InvalidArgumentError                      
Traceback (most recent call last) <ipython-input-13-974c37e3cdfd> in <module>
      9 
     10 initial_epochs = 5
---> 11 history = model2.fit(train_dataset, validation_data = validation_dataset, epochs = initial_epochs)

~\AppData\Roaming\Python\Python37\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)    1182                 _r=1):    1183            callbacks.on_train_batch_begin(step)
-> 1184               tmp_logs = self.train_function(iterator)    1185               if data_handler.should_sync:    1186                 context.async_wait()

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\util\traceback_utils.py in error_handler(*args, **kwargs)
    137     except Exception as e:
    138       filtered_tb = _process_traceback_frames(e.__traceback__)
--> 139       raise e.with_traceback(filtered_tb) from None
    140     finally:
    141       del filtered_tb

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError: 2 root error(s) found.   (0) Invalid argument:  Input is empty.    [[{{node decode_image/DecodeImage}}]]   [[IteratorGetNext]]   (1) Invalid argument:  Input is empty.    [[{{node decode_image/DecodeImage}}]]   [[IteratorGetNext]]     [[IteratorGetNext/_6]] 0 successful operations. 0 derived errors ignored. [Op:__inference_train_function_31277]

Function call stack: train_function -> train_function
desertnaut
  • 57,590
  • 26
  • 140
  • 166
maryam gh
  • 163
  • 1
  • 5
  • 11
  • Probably you have some invalid images in your directory. Check your dataset directory for corrupted or unexpected image files. – Kaveh Jul 24 '21 at 17:45
  • @Kaveh i only saw a Thumbs.db file and i deleted that. but nothing changed. – maryam gh Jul 24 '21 at 18:05
  • Yes. you may see everything ok, but some image may be corrupted. Check corrupted image files with [this](https://gist.github.com/Kaveh8/9a8e1097b76c8ee92a4644f4413fa57f) snippet I have provided for you. If a file is not a valid image it prints the name of file, then delete that file and run you code again. Keep me posted if it worked. change path and run it for all of image dirs you have. – Kaveh Jul 24 '21 at 18:13
  • @Kaveh i really appreciate that. i found lots of corrupted images in my dataset. however my problem still remains. but i'm sure something is wrong about my dataset. because i tried my code with another dataset (alpaca/not alpaca) and it works. i have no idea what else could be wrong about it. – maryam gh Jul 24 '21 at 18:36
  • @Kaveh Yes. i checked both Dogs and Cats directories. and deleted all corrupted files. but it still does not work – maryam gh Jul 24 '21 at 18:41

1 Answers1

3

You probably have zero size images in your dataset. you can check this and fix it by doing the following:

If you are in windows simply open to the root folder of your dataset in windows explorer and type in the search bar at the top right side : size:empty

Let it run for a couple of minutes until it finishes and then select all results using Ctrl+A and then delete all of them Shift+Delete ... note that all results should be zero sized.

You can search google for command line alternatives if you feel comfortable with it, I wanted to list the simplest solution.

If in linux, you can use find: find /tmp -size 0 -print -delete from top solution in this question

Disclaimer: Be Careful When Applying This fix, Make Backups Before Trying

mhk777
  • 83
  • 1
  • 9
  • The idea worked for me, thank you! In Windows, you can also open it in Explorer and sort the images by size and delete the ones that have a size of 0 KiB – Crazy Engineer Oct 13 '22 at 15:21