Issue
I'm training a machine learning model for a cat and dog image classification project and I seem to have encountered an error when attempting to train the model which I think could be due to the type of input data I'm passing or flaws in the model's structure.
Creating directory, preprocessing,and dataset variables:
# Get project files
!wget https://cdn.freecodecamp.org/project-data/cats-and-dogs/cats_and_dogs.zip
!unzip cats_and_dogs.zip
PATH = 'cats_and_dogs'
train_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')
test_dir = os.path.join(PATH, 'test')
# Get number of files in each directory. The train and validation directories
# each have the subdirecories "dogs" and "cats".
total_train = sum([len(files) for r, d, files in os.walk(train_dir)])
total_val = sum([len(files) for r, d, files in os.walk(validation_dir)])
total_test = len(os.listdir(test_dir))
# Variables for pre-processing and training.
batch_size = 128
epochs = 15
IMG_HEIGHT = 150
IMG_WIDTH = 150
train_image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=0.00392156862)
validation_image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=0.00392156862)
test_image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=0.00392156862)
train_data_gen = train_image_generator.flow_from_directory(directory=train_dir, target_size=(IMG_HEIGHT,IMG_WIDTH),class_mode='binary',batch_size=batch_size,shuffle=True,color_mode='rgb')
val_data_gen = validation_image_generator.flow_from_directory(directory=validation_dir, target_size=(IMG_HEIGHT,IMG_WIDTH),class_mode='binary',batch_size=batch_size,shuffle=True,color_mode='rgb')
test_data_gen = test_image_generator.flow_from_directory(directory=PATH,target_size=(IMG_HEIGHT,IMG_WIDTH),classes=['test'],class_mode='input',batch_size=batch_size,shuffle=False,color_mode='rgb')
Building and training the model:
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(32,(3,3),activation='relu',input_shape=(32,32,3)))
model.add(tf.keras.layers.MaxPooling2D((2,2)))
model.add(tf.keras.layers.Conv2D(64,(3,3),activation='relu'))
model.add(tf.keras.layers.MaxPooling2D((2,2)))
model.add(tf.keras.layers.Conv2D(64,(3,3),activation='relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(32))
model.add(tf.keras.layers.Dense(16))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
model.summary()
model.compile(optimizer="Adam",loss='binary_crossentropy',metrics=['accuracy'])
#training
history = model.fit(train_data_gen,steps_per_epoch=round(total_train)//batch_size,epochs=epochs,batch_size=batch_size)
Error Screenshot: InvalidArgumentError
Any ideas on what could be causing the problem?
Solution
In your generators you specify IMG_HEIGHT = 150 and IMG_WIDTH = 150. However in your model you have the code
model.add(tf.keras.layers.Conv2D(32,(3,3),activation='relu',input_shape=(32,32,3)))
change the input shape to (150, 150,3). As a side note you code
train_image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=0.00392156862)
validation_image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=0.00392156862)
test_image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=0.00392156862)
since the are identical just use
gen=tf.keras.preprocessing.image.ImageDataGenerator(rescale=0.00392156862)
then
train_data_gen = gen.flow_from_directory(directory=train_dir, target_size=(IMG_HEIGHT,IMG_WIDTH),class_mode='binary',batch_size=batch_size,shuffle=True,color_mode='rgb')
val_data_gen = gen.flow_from_directory(directory=validation_dir, target_size=(IMG_HEIGHT,IMG_WIDTH),class_mode='binary',batch_size=batch_size,shuffle=True,color_mode='rgb')
test_data_gen = gen.flow_from_directory(directory=PATH,target_size=(IMG_HEIGHT,IMG_WIDTH),classes=['test'],class_mode='input',batch_size=batch_size,shuffle=False,color_mode='rgb')
Also in model.fit since you are using generators you should not include the parameter batch_size as it is already specified within the generator. I also do not include the parameter steps_per_epoch. If you do not include it then model.fit will calculate it properly internally.
Answered By - Gerry P
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.