Home > Blockchain >  Keras Creating Image Detection incompatible shape
Keras Creating Image Detection incompatible shape

Time:12-11

I am working on a small project where i wanted to classify some images by training them within a convolutional network, with a pretrained VGG16 network.

These are the steps i took:

#Setting Conditions
img_size = (180,180)
batch_size = 600
num_classes = len(class_names)

#Using Keras built in preprocessing method which facilitates the preprocessing instead of having to do it manually.

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "Data/Train",
    validation_split = 0.2,
    subset = "training",
    seed = 1337, # creating a seed so t
    image_size = img_size,
    batch_size = batch_size,
)

valid_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "Data/Train",
    validation_split = 0.2,
    subset = "validation",
    seed = 1337, # creating a seed so t
    image_size = img_size,
    batch_size = batch_size,
)

BUILDING MODEL

#Creating Model
model = Sequential()

#ADDING The VGG16 Pre trained network
model.add(VGG16(pooling ='avg',weights="imagenet", include_top=False))

#adding a dense layer
model.add(Dense(num_classes,activation = 'softmax'))

#Setting the trainable parameter for VGG16 to false, as we want to use this pretrained network, and train the new images.

model.layers[0].trainable = False

#The compile() method: specifying a loss, metrics, and an optimizer To train a model with fit(), #you need to specify a loss function, an optimizer, and optionally, some metrics to monitor.
#You pass these to the model as arguments to the compile() method

model.compile(optimizer = 'adam',loss = 'categorical_crossentropy', metrics =['accuracy'])
epoch_train = len(train_ds)
opoch_val = len(valid_ds)
numbers_epochs = 2


fit_model = model.fit(train_ds, steps_per_epoch = epoch_train,verbose = 1, validation_data = valid_ds, validation_steps = opoch_val,)

When i try to fit the model, i get the following error:

ValueError: Shapes (None, 1) and (None, 43) are incompatible

If there is an expert out there to call out what i did wrong or what steps i skipped... I would be very greatful!

CodePudding user response:

You need to set label_mode='categorical in image_dataset_from_directory if you plan to train your model with categorical_crossentropy. By default, the image_dataset_from_directory method assumes that the labels are encoded as integers (label_mode='int'), meaning you should being using the sparse_categorical_crossentropy loss. Here is a working example:

import tensorflow as tf
import pathlib
import matplotlib.pyplot as plt

dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)

batch_size = 32
num_classes = 5
train_ds = tf.keras.utils.image_dataset_from_directory(data_dir, shuffle=True, batch_size=batch_size, label_mode='categorical')

model = tf.keras.Sequential()
model.add(tf.keras.applications.VGG16(pooling ='avg',weights="imagenet", include_top=False))
model.add(tf.keras.layers.Dense(num_classes, activation = 'softmax'))

model.layers[0].trainable = False

model.compile(optimizer = 'adam',loss = 'categorical_crossentropy', metrics =['accuracy'])
numbers_epochs = 2
fit_model = model.fit(train_ds, epochs=numbers_epochs)
  • Related