Adithyan JS
Adithyan JS

Reputation: 11

Kernel shape must have the same length as input, but received kernel of shape A and input of shape B

I have created a model and when testing it is showing an error. The model predicts results well on colab. When running in VS Code the model is getting created but while testing the model by specifying the model path and image path it's showing the error. How can I correct the error?

import numpy as np 
import pandas as pd 
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, Dense, MaxPooling2D, Flatten, BatchNormalization, Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.applications import VGG16
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.resnet import preprocess_input 
import matplotlib.pyplot as plt
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras import Input

train_path_str ='C:/Users/adith/OneDrive/Desktop/Data/train' 
val_path_str ='C:/Users/adith/OneDrive/Desktop/Data/valid'
test_path_str ='C:/Users/adith/OneDrive/Desktop/Data/test'  

input_shape = (224, 224,3)
num_classes = 4

# Image Generators
trainGenerator = ImageDataGenerator()
valGenerator = ImageDataGenerator(
    preprocessing_function=preprocess_input,
    dtype='float32'
)
testGenerator = ImageDataGenerator(
    preprocessing_function=preprocess_input,
    dtype='float32'
)

# Data Generators
train_data = trainGenerator.flow_from_directory(
    train_path_str,
    target_size=(224, 224),
    batch_size=8,
    class_mode='categorical'
)
val_data = valGenerator.flow_from_directory(
    val_path_str,
    target_size=(224, 224),
    batch_size=8,  
    class_mode='categorical'
)
test_data = testGenerator.flow_from_directory(
    test_path_str,
    target_size=(224, 224),
    batch_size=8, 
    class_mode='categorical',
    shuffle=False
)

# Load VGG16 model
VGG16_model = VGG16(
    include_top=False,
    weights="imagenet",
    input_shape=input_shape
)
for layer in VGG16_model.layers:
    layer.trainable = False

model = Sequential()
model.add(VGG16_model)
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu')) 
model.add(BatchNormalization())
model.add(MaxPooling2D((2,2)))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

# Compilation
model.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
    loss='categorical_crossentropy',
    metrics=['accuracy']
)

# Model summary
model.summary()

# Define callbacks to save the model in .h5 format
checkpoint_callback = ModelCheckpoint('model.keras', save_best_only=True)

# Train the model
results = model.fit(
    train_data,
    validation_data=val_data,
    epochs=5,
    verbose=1,
    callbacks=[checkpoint_callback]  # Use the checkpoint callback here
)

# Evaluate the model on the test dataset
test_loss, test_accuracy = model.evaluate(test_data)

# Print test loss and accuracy
print('Test Loss:', test_loss)
print('Test Accuracy:', test_accuracy)

The model trains well and is getting saved, but when testing the model using below code, it shows the error.

from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.applications.vgg16 import preprocess_input as preprocess_input_vgg16
import numpy as np

# Load the trained model
model = load_model('C:/Users/adith/OneDrive/Desktop/code/hello/model.keras')

# Function to preprocess the image
def preprocess_image(image_path):
    # Load the image
    img = load_img(image_path, target_size=(224, 224))
    # Convert the image to an array
    img_array = img_to_array(img)
    # Add an extra dimension to the array
    img_array = np.expand_dims(img_array, axis=0)
    # Preprocess the image
    processed_img = preprocess_input_vgg16(img_array)
    return processed_img

# Function to make predictions
def predict_image(image_path, model):
    # Preprocess the image
    processed_img = preprocess_image(image_path)
    # Make predictions
    predictions = model.predict(processed_img)
    # Decode the predictions
    class_labels = ['class_1', 'class_2', 'class_3', 'class_4']  # Replace with your class labels
    predicted_class = np.argmax(predictions)
    predicted_label = class_labels[predicted_class]
    return predicted_label, predictions

# Path to the image you want to predict
image_path = 'path/to/your/image.jpg'  # Replace with the path to your image

# Make predictions
predicted_label, predictions = predict_image(image_path, model)

# Print the predicted label and probabilities
print("Predicted Label:", predicted_label)
print("Predicted Probabilities:", predictions)

While running the code it's showing the error of:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[94], line 7
      4 import numpy as np
      6 # Load the trained model
----> 7 model = load_model('C:/Users/adith/OneDrive/Desktop/code/hello/model.keras')
      9 # Function to preprocess the image
     10 def preprocess_image(image_path):
     11     # Load the image

File ~\AppData\Roaming\Python\Python312\site-packages\keras\src\saving\saving_api.py:176, in load_model(filepath, custom_objects, compile, safe_mode)
    173         is_keras_zip = True
    175 if is_keras_zip:
--> 176     return saving_lib.load_model(
    177         filepath,
    178         custom_objects=custom_objects,
    179         compile=compile,
    180         safe_mode=safe_mode,
    181     )
    182 if str(filepath).endswith((".h5", ".hdf5")):
    183     return legacy_h5_format.load_model_from_hdf5(filepath)

File ~\AppData\Roaming\Python\Python312\site-packages\keras\src\saving\saving_lib.py:153, in load_model(filepath, custom_objects, compile, safe_mode)
    151 # Construct the model from the configuration file in the archive.
    152 with ObjectSharingScope():
...
Kernel shape must have the same length as input, but received kernel of shape (3, 3, (None, 7, 7, 512), 64) and input of shape [(None, 7, 7, 512)].

Arguments received by Conv2D.call():
  • args=(['<KerasTensor shape=(None, 7, 7, 512), dtype=float32, sparse=False, name=keras_tensor_1105>'],)
  • kwargs=<class 'inspect._empty'>
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

Upvotes: 1

Views: 2231

Answers (0)

Related Questions