Home > Software design >  ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(

Time:04-26

I need help with some Machine Learning code of mine. I am using the base of a pretrained model by google and adding the last flattening and output layers of my own.

After training the model and saving it, I commented out the training part of the code and loaded and used the saved model.

my code:

import tensorflow as tf
from tensorflow import keras

import numpy as np
import matplotlib.pyplot as plt

import tensorflow_datasets as tfds
tfds.disable_progress_bar()

class_names = ['cat','dog']

#load the images
(raw_train, raw_validation, raw_test), metadata = tfds.load(
    'cats_vs_dogs',
    split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'],
    with_info=True,
    as_supervised=True,
)

get_label_name = metadata.features['label'].int2str

#function to resize image
IMG_SIZE=160
def format_example(image, label):
    image = tf.cast(image, tf.float32) #cast to convert integer values in pixels to float32
    image = (image/127.5) - 1
    image = tf.image.resize(image,  (IMG_SIZE, IMG_SIZE))
    return image,label

#.map is to apply a function to all raw images
train = raw_train.map(format_example)
validation = raw_validation.map(format_example)
test = raw_test.map(format_example)

BATCH_SIZE = 32
SHUFFLE_BUFFER_SIZE = 1000

train_batches = train.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)
validation_batches = validation.batch(BATCH_SIZE)
test_batches = test.batch(BATCH_SIZE)

IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)
#Create base model from pretrained model MobileNetV2
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                                include_top=False,
                                                weights='imagenet')

#freezing the base (preventing any more training)
base_model.trainable = False

#Flattening it down
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
#Adding dense layer of only 1 neuron as only 2 possibilities(cat/dog)
prediction_layer = keras.layers.Dense(1)

model = tf.keras.Sequential([
    base_model,
    global_average_layer,
    prediction_layer
])

#slow learning rate since it is a pretrained model
base_learning_rate = 0.0001
#Compiling the model
model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=base_learning_rate),
              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              metrics=['accuracy'])

initial_epochs = 3
validation_steps=20

loss0,accuracy0 = model.evaluate(validation_batches, steps = validation_steps)

print('training...')
history = model.fit(train_batches,
                    epochs=initial_epochs,
                    validation_data = validation_batches)

acc = history.history['accuracy']
print(acc)

model.save("dogs_vs_cats.h5")

model = tf.keras.models.load_model('dogs_vs_cats.h5', compile=True)
print('predicting... ')
prediction = model.predict(test, verbose=1)
print(prediction)

I am getting the following error:

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 160, 160, 3), found shape=(160, 160, 3)

CodePudding user response:

Do I understand correctly that you get this error only after you comment training code and run lines starting from model = tf.keras.models.load_model('dogs_vs_cats.h5', compile=True)? If yes, I think that you have a mistake in this line:

prediction = model.predict(test, verbose=1)

You use test with 3D-images, but not batched test_batches with 4D-images, this is why you obtain an error with 3D/4D shapes.

  • Related