Facial recognition with Deep Learning

 

Advanced Deep Learning and Computer Vision

 

Objective: Create a facial recognition tool using a relevant deep learning algorithm, leveraging the provided resources.

 

Context: An American AI company that has developed a healthcare app for doctors. The app utilizes deep learning algorithms to aid in diagnosing patients for genetic disorders and their variants. It converts patient photos into de-identified mathematical facial descriptors, which are then compared to syndrome-specific computational-based classifiers to determine similarity. The app provides a prioritized list of syndromes with similar morphology, suggesting phenotypic traits and genes for feature annotation and syndrome prioritization.

 

Management has given priority to empowering and entrusting the in-house AI team. As a new member of the team, your task is to build a baseline model for facial recognition. The goal is to further enhance the app’s existing features and add more value to the business based on this baseline model.

 

Dataset Details: The ORL Database of Faces consists of 400 images from 40 different subjects. The images were captured at different times, under varying lighting conditions, with different facial expressions (open, closed eyes, smiling, not smiling), and with or without glasses. All the images have a dark homogeneous background, and the subjects are positioned upright and frontal with some tolerance for side movement. Each image has a size of 92×112 pixels and 256 grey levels per pixel.

 

Data can be downloaded from the following link:

https://www.kaggle.com/datasets/kasikrit/att-database-of-faces

 

 

Steps to be followed: The following steps will guide you in building the model.

 

1. Import the relevant packages and collect all the necessary dependencies.

 

2. Upload and import the data.

 

3. View a few images to get a sense of the data.

 

4. Create a validation framework and split the data into train, test, and validation datasets.

 

5. Perform necessary transformations to prepare the data for input to the CNN model.

 

6. Build a CNN model with three main layers: a convolutional layer, a pooling layer, and a fully connected layer. You can also consider utilizing state-of-the-art architectures using transfer learning.

 

7. Train the model using the prepared data.

 

8. Plot the results to evaluate the model’s performance.

 

9. Iterate on the model, making adjustments and improvements, until you achieve an accuracy above 90%.

 

Note: Please refer to the Chicago Manual of Style for any specific formatting or citation requirements.

 

 

# facial_recognition.ipynb

# ============================================================
# 1. Imports & Setup
# ============================================================
import os
import numpy as np
import matplotlib.pyplot as plt
import kagglehub
import cv2

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout

# ============================================================
# 2. Download Dataset with kagglehub
# ============================================================
# Download latest version of ORL Faces dataset
path = kagglehub.dataset_download(“kasikrit/att-database-of-faces”)
print(“Path to dataset files:”, path)

# Images are inside subfolders s1, s2, … s40
dataset_path = os.path.join(path, “att-database-of-faces”)

# ============================================================
# 3. Load and Preprocess Data
# ============================================================
X = [] y = []

for subject in os.listdir(dataset_path):
subject_path = os.path.join(dataset_path, subject)
if os.path.isdir(subject_path):
for img_name in os.listdir(subject_path):
img_path = os.path.join(subject_path, img_name)
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) # grayscale
img = cv2.resize(img, (92, 112)) # resize to (92×112)
X.append(img)
y.append(subject)

X = np.array(X)
y = np.array(y)

print(“Dataset shape:”, X.shape, y.shape) # Expect (400, 112, 92), (400,)

# Normalize images
X = X / 255.0
X = np.expand_dims(X, -1) # add channel dimension

# Encode labels (subjects s1..s40)
le = LabelEncoder()
y_enc = le.fit_transform(y)
y_cat = to_categorical(y_enc, num_classes=len(np.unique(y)))

# ============================================================
# 4. Train-Test Split
# ============================================================
X_train, X_temp, y_train, y_temp = train_test_split(X, y_cat, test_size=0.3, stratify=y_cat, random_state=42)
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, stratify=y_temp, random_state=42)

print(“Train:”, X_train.shape, “Val:”, X_val.shape, “Test:”, X_test.shape)

# ============================================================
# 5. Build CNN Model
# ============================================================
model = Sequential([
Conv2D(32, (3,3), activation=’relu’, input_shape=(112, 92, 1)),
MaxPooling2D((2,2)),

Conv2D(64, (3,3), activation=’relu’),
MaxPooling2D((2,2)),

Flatten(),
Dense(128, activation=’relu’),
Dropout(0.5),
Dense(40, activation=’softmax’) # 40 subjects
])

model.compile(optimizer=’adam’,
loss=’categorical_crossentropy’,
metrics=[‘accuracy’])

model.summary()

# ============================================================
# 6. Train Model
# ============================================================
history = model.fit(
X_train, y_train,
epochs=30,
batch_size=16,
validation_data=(X_val, y_val),
verbose=1
)

# ============================================================
# 7. Evaluate on Test Set
# ============================================================
test_loss, test_acc = model.evaluate(X_test, y_test, verbose=0)
print(f”Test Accuracy: {test_acc*100:.2f}%”)

# ============================================================
# 8. Plot Training Results
# ============================================================
plt.figure(figsize=(12,4))

# Accuracy
plt.subplot(1,2,1)
plt.plot(history.history[‘accuracy’], label=’Train’)
plt.plot(history.history[‘val_accuracy’], label=’Val’)
plt.title(‘Model Accuracy’)
plt.xlabel(‘Epochs’)
plt.ylabel(‘Accuracy’)
plt.legend()

# Loss
plt.subplot(1,2,2)
plt.plot(history.history[‘loss’], label=’Train’)
plt.plot(history.history[‘val_loss’], label=’Val’)
plt.title(‘Model Loss’)
plt.xlabel(‘Epochs’)
plt.ylabel(‘Loss’)
plt.legend()

plt.show()

# ============================================================
# 9. Save Model
# ============================================================
os.makedirs(“models”, exist_ok=True)
model.save(“models/facial_recognition_cnn.h5”)
print(“Model saved to models/facial_recognition_cnn.h5”)

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.