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