Aayushi Singh
Aayushi Singh

Reputation: 1

Resolving Dimension Mismatch Errors

Can someone help me out with error I am encountering? I am doing emotion extracion task using 3D CNN, I have provided the code where I am facing error(dim. mismatched error) I am facing an error in the last line of the code The error I am encountering typically arises when there's a discrepancy between the expected shape of the input data and the actual shape provided to the model. In your case, the model expects an input shape of (None, 16, 224, 224, 3) (assuming 16 frames per GIF, each with dimensions 224x224 and 3 channels), but the provided shape is (None, 224, 224, 3). Any help is appreciated.

ValueError: Exception encountered when calling Sequential.call().

Invalid input shape for input Tensor("data:0", shape=(None, 224, 224, 3), dtype=float32). Expected shape (None, 16, 224, 224, 3), but input has incompatible shape (None, 224, 224, 3)

Arguments received by Sequential.call():
  • inputs=tf.Tensor(shape=(None, 224, 224, 3), [text](https://stackoverflow.com)dtype=float32)
  • training=True
  • mask=None
import os
import zipfile
import shutil
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Conv3D, MaxPooling3D, Flatten, Dense
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from tensorflow.keras.callbacks import EarlyStopping

def load_data(data_dir, image_size, batch_size, validation_split=0.35):
    # Load and preprocess training data with validation split
    train_data = tf.keras.utils.image_dataset_from_directory(
        data_dir,
        labels='inferred',
        label_mode='categorical',
        color_mode='rgb',
        batch_size=batch_size,
        image_size=image_size[1:3],  # Only height and width needed here
        validation_split=validation_split,
        subset='training',
        seed=123
    )

    # Load and preprocess validation data with validation split
    val_data = tf.keras.utils.image_dataset_from_directory(
        data_dir,
        labels='inferred',
        label_mode='categorical',
        color_mode='rgb',
        batch_size=batch_size,
        image_size=image_size[1:3],  # Only height and width needed here
        validation_split=validation_split,
        subset='validation',
        seed=123
    )

    return train_data, val_data

def build_model(input_shape, num_classes):
    model = tf.keras.Sequential([
        #print(input_shape),
        Conv3D(16, kernel_size=(3, 3, 3), activation='relu', input_shape=input_shape),
        MaxPooling3D(pool_size=(2, 2, 2)),
        Conv3D(32, kernel_size=(3, 3, 3), activation='relu'),
        MaxPooling3D(pool_size=(2, 2, 2)),
        Conv3D(64, kernel_size=(3, 3, 3), activation='relu'),
        MaxPooling3D(pool_size=(2, 2, 2)),
        Flatten(),
        Dense(256, activation='relu'),
        Dense(num_classes, activation='softmax')
    ])
    return model

#set parameters
zip_file = 'downloaded_gifs.zip'
extract_dir = 'extracted_gifs'
data_dir = 'organized_gifs'
image_size = (16, 224, 224, 3)  # Corrected image size assuming each GIF has 16 frames, height, width

batch_size = 32
num_classes = 2  # Assuming 2 classes: happy and sad

#Extract and organize GIFs
extract_zip(zip_file, extract_dir)
organize_gifs_into_classes(data_dir, extract_dir)

#Load and preprocess data without data augmentation
train_data, val_data = load_data(data_dir, image_size, batch_size)
print(train_data, val_data)

model = build_model(input_shape=(16, 224, 224, 3), num_classes=num_classes)

#Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

#Train the model
history = model.fit(train_data, epochs=10, validation_data=val_data)

Upvotes: 0

Views: 41

Answers (0)

Related Questions