Issues with shapes on face detection AI mdoel

import tensorflow as tf
from keras import layers, models
from keras.preprocessing.image import ImageDataGenerator
import os
import numpy as np
from PIL import Image 

class VGGFaces(tf.keras.Model):
    def __init__(self):
        super(VGGFaces, self).__init__()
        self.conv_blocks = models.Sequential([
            layers.Conv2D(64, kernel_size=3, padding='same', activation='relu'),
            layers.BatchNormalization(axis=-1),
            layers.Conv2D(64, kernel_size=3, padding='same', activation='relu'),
            layers.BatchNormalization(axis=-1),
            layers.MaxPooling2D(pool_size=2, strides=2),
            
            layers.Conv2D(128, kernel_size=3, padding='same', activation='relu'),
            layers.BatchNormalization(axis=-1),
            layers.Conv2D(128, kernel_size=3, padding='same', activation='relu'),
            layers.BatchNormalization(axis=-1),
            layers.MaxPooling2D(pool_size=2, strides=2),
            
            layers.Conv2D(256, kernel_size=3, padding='same', activation='relu'),
            layers.BatchNormalization(axis=-1),
            layers.Conv2D(256, kernel_size=3, padding='same', activation='relu'),
            layers.BatchNormalization(axis=-1),
            layers.Conv2D(256, kernel_size=3, padding='same', activation='relu'),
            layers.BatchNormalization(axis=-1),
            layers.MaxPooling2D(pool_size=2, strides=2),
            
            layers.Conv2D(512, kernel_size=3, padding='same', activation='relu'),
            layers.BatchNormalization(axis=-1),
            layers.Conv2D(512, kernel_size=3, padding='same', activation='relu'),
            layers.BatchNormalization(axis=-1),
            layers.Conv2D(512, kernel_size=3, padding='same', activation='relu'),
            layers.BatchNormalization(axis=-1),
            layers.MaxPooling2D(pool_size=2, strides=2),
            
            layers.Conv2D(512, kernel_size=3, padding='same', activation='relu'),
            layers.BatchNormalization(axis=-1),
            layers.Conv2D(512, kernel_size=3, padding='same', activation='relu'),
            layers.BatchNormalization(axis=-1),
            layers.Conv2D(512, kernel_size=3, padding='same', activation='relu'),
            layers.BatchNormalization(axis=-1),
            layers.MaxPooling2D(pool_size=2, strides=2),
        ])
        self.global_pooling = layers.GlobalMaxPooling2D()
        self.dense1 = layers.Dense(2048, activation='relu')
        self.dense2 = layers.Dense(4, activation='linear')

    def call(self, tensor):
        tensor = self.conv_blocks(tensor)
        tensor = self.global_pooling(tensor)
        tensor = self.dense1(tensor)
        tensor = self.dense2(tensor)
        return tensor
    
def parse_annotation(annotation_path):
    with open(annotation_path, 'r') as f:
        line = f.read()
    
    annotations = []
    parts = line.strip().split('|')
    num_people = int(parts[1])
    
    for i in range(num_people):
        bbox = parts[i+2].split(",")[:-1]
        annotations.append(bbox)
    
    return annotations

def load_and_preprocess_data(image_dir, annotation_dir):
    image_paths = [os.path.join(image_dir, filename) for filename in os.listdir(image_dir)]
    annotation_paths = [os.path.join(annotation_dir, filename.split('.')[0] + '.txt') for filename in os.listdir(image_dir)]
    
    image_data = []
    annotations_list = []  
    
    for image_path, annotation_path in zip(image_paths, annotation_paths):
        image = tf.io.read_file(image_path)
        image = tf.image.decode_jpeg(image, channels=3)
        image = tf.image.resize(image, (224, 224)) 
        
        annotations = parse_annotation(annotation_path)  
        image_data.append(image)
    
    return image_data, annotations

train_images, train_annotations = load_and_preprocess_data(
    r"C:\Users\Zains\OneDrive\Desktop\Coding Projects\Python Projects\faceRecognition2\trainProcessedImages",
    r"C:\Users\Zains\OneDrive\Desktop\Coding Projects\Python Projects\faceRecognition2\trainAnnotationFiles"
)

val_images, val_annotations = load_and_preprocess_data(
    r"C:\Users\Zains\OneDrive\Desktop\Coding Projects\Python Projects\faceRecognition2\valProcessedImages",
    r"C:\Users\Zains\OneDrive\Desktop\Coding Projects\Python Projects\faceRecognition2\valAnnotationFiles"
)

batch_size = 32

train_images = np.stack(train_images)
val_images = np.stack(val_images)

train_annotations_ragged = tf.ragged.constant(train_annotations)
val_annotations_ragged = tf.ragged.constant(val_annotations)

train_annotations_dense = train_annotations_ragged.to_tensor(default_value="", shape=(12879, 4))
val_annotations_dense = val_annotations_ragged.to_tensor(default_value="", shape=(733, 4))

train_dataset = tf.data.Dataset.from_tensor_slices((train_images, train_annotations_dense))
val_dataset = tf.data.Dataset.from_tensor_slices((val_images, val_annotations_dense))

model = VGGFaces()

model.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
    loss=tf.keras.losses.mean_squared_error,
    metrics=[tf.keras.metrics.MeanIoU(num_classes=2)],
)

checkpoint_path = "faceDetectionModel.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)

cp_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_path,
    save_weights_only=True,
    verbose=1,
    save_best_only=True
)

epochs = 20
model.fit(
    train_dataset, 
    epochs=epochs, 
    validation_data=val_dataset,
    callbacks=[cp_callback] 
)

test_loss, test_iou = model.evaluate(val_dataset)
print(f"Test Loss: {test_loss:.4f}")
print(f"Test IoU: {test_iou:.4f}")

i am currently getting the error:

PS C:\Users\Zains\OneDrive\Desktop\Coding Projects\Python Projects\faceRecognition2> python -u "c:\Users\Zains\OneDrive\Desktop\Coding Projects\Python Projects\faceRecognition2\blah2.py"
2023-08-30 15:19:59.186145: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-08-30 15:21:11.413312: W tensorflow/tsl/framework/cpu_allocator_impl.cc:83] Allocation of 7754600448 exceeds 10% of free system memory.
Epoch 1/20
Traceback (most recent call last):
  File "c:\Users\Zains\OneDrive\Desktop\Coding Projects\Python Projects\faceRecognition2\blah2.py", line 133, in <module>
    model.fit(
  File "C:\Users\Zains\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\Zains\AppData\Local\Temp\__autograph_generated_filexleyrxv7.py", line 15, in tf__train_function
    retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
    ^^^^^
  File "C:\Users\Zains\AppData\Local\Temp\__autograph_generated_filerbrioefb.py", line 10, in tf__call
    tensor = ag__.converted_call(ag__.ld(self).conv_blocks, (ag__.ld(tensor),), None, fscope)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: in user code:

    File "C:\Users\Zains\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\training.py", line 1338, in train_function  *
        return step_function(self, iterator)
    File "C:\Users\Zains\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\training.py", line 1322, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:\Users\Zains\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\training.py", line 1303, in run_step  **
        outputs = model.train_step(data)
    File "C:\Users\Zains\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\training.py", line 1080, in train_step
        y_pred = self(x, training=True)
    File "C:\Users\Zains\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\utils\traceback_utils.py", line 70, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "C:\Users\Zains\AppData\Local\Temp\__autograph_generated_filerbrioefb.py", line 10, in tf__call
        tensor = ag__.converted_call(ag__.ld(self).conv_blocks, (ag__.ld(tensor),), None, fscope)

    ValueError: Exception encountered when calling layer 'vgg_faces' (type VGGFaces).

    in user code:

        File "c:\Users\Zains\OneDrive\Desktop\Coding Projects\Python Projects\faceRecognition2\blah2.py", line 53, in call  *
            tensor = self.conv_blocks(tensor)
        File "C:\Users\Zains\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\utils\traceback_utils.py", line 70, in error_handler  **
            raise e.with_traceback(filtered_tb) from None
        File "C:\Users\Zains\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\input_spec.py", line 253, in assert_input_compatibility
            raise ValueError(

        ValueError: Exception encountered when calling layer 'sequential' (type Sequential).

        Input 0 of layer "conv2d" is incompatible with the layer: expected min_ndim=4, found ndim=3. Full shape received: (224, 224, 3)

        Call arguments received by layer 'sequential' (type Sequential):
          • inputs=tf.Tensor(shape=(224, 224, 3), dtype=float32)
          • training=True
          • mask=None


    Call arguments received by layer 'vgg_faces' (type VGGFaces):
      • tensor=tf.Tensor(shape=(224, 224, 3), dtype=float32)

Does anyone know how I can fix this?

Hi @zainsharief, Could you please try by adding the batch dimension explicitly to the data you are passing to the model. Thank You.