Facial recognition systems face significant challenges when identifying individuals wearing masks, a scenario increasingly relevant in the post-pandemic world. This project implements a Siamese Network designed for facial recognition, capable of recognizing faces even when covered by masks. Leveraging a pretrained model, the system generates feature descriptor vectors for each image and stores them in a SQLite database. When presented with a query image, including masked images, the network compares its feature vector to the database entries to identify the closest match.
Face recognition has emerged as a pivotal technology in modern computer vision, finding applications in security, healthcare, and personalized services. However, the global adoption of face masks during the Covid-19 pandemic introduced significant challenges for traditional recognition systems. Masks occlude critical facial features, impairing the ability of conventional algorithms to identify individuals accurately.
This project addresses these challenges by implementing a Siamese Network for robust face identification. The network is designed to handle scenarios involving partially covered faces, such as individuals wearing masks. By using a pre-trained backbone, the model efficiently generates feature descriptor vectors for images, enabling precise matching in a database of celebrity images. The SQLite database serves as a flexible and lightweight storage solution, ensuring seamless retrieval and comparison of image descriptors.
While the initial implementation is centered on a celebrity image database, the system's modular design allows for easy adaptation to broader contexts. This includes secure access systems and personalized services, where accurate recognition remains essential despite the presence of occlusions.
The project uses preprocessed datasets. Images are loaded directly for training and evaluation.
from torchvision.datasets import ImageFolder from torchvision.transforms import ToTensor from torch.utils.data import DataLoader # Example of dataset loading dataset = ImageFolder(root='path_to_data', transform=ToTensor()) data_loader = DataLoader(dataset, batch_size=32, shuffle=True)
The Siamese Network is implemented with a pretrained ResNet18 backbone.
import torch.nn as nn import torchvision.models as models class SiameseNetwork(nn.Module): def __init__(self, pretrained_model): super(SiameseNetwork, self).__init__() # Load the pretrained model self.pretrained_model = pretrained_model # Replace the last fully connected layer with an identity layer self.pretrained_model.fc = nn.Identity() def forward_once(self, x): # Forward pass through the pretrained model output = self.pretrained_model(x) return output def forward(self, input1, input2): # Forward pass for both inputs through the pretrained model output1 = self.forward_once(input1) output2 = self.forward_once(input2) return output1, output2
The inference stage involves generating descriptor vectors from input images which will be stored in a database for subsequent matching.
import torch # Define the device for computation device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') siamese_net.to(device) # Set the model to evaluation mode siamese_net.eval() # Initialize lists to store descriptor vectors and file paths descriptor_vectors = [] file_paths = [] # Iterate through batches in the data loader for batch in data_loader: images, paths = batch images = images.to(device) with torch.no_grad(): # Forward pass through the Siamese network with image pairs outputs = siamese_net(images, images) descriptor_vectors.extend(outputs[0].cpu().numpy()) file_paths.extend(paths)
SQLite is utilized for storing the feature vectors.
import sqlite3 # Connect to SQLite database conn = sqlite3.connect('database.db') cursor = conn.cursor() # Create a table to store image paths and descriptor vectors cursor.execute('''CREATE TABLE IF NOT EXISTS ImageDescriptors (id INTEGER PRIMARY KEY, file_path TEXT, descriptor_vector BLOB)''')
The performance of the Siamese Network was evaluated by testing its ability to identify a masked image of an individual against a database containing their unmasked image.
This result highlights the effectiveness of the Siamese Network in recognizing individuals. The identification of the masked image with an accuracy of 82% underscores the robustness of the pretrained model. While the system showed strong performance, the slight reduction in confidence indicates room for improvement.
While the system achieved an accuracy of 82% for identifying masked images, this represents a decline compared to ideal conditions.
As the size of the database grows, the computational cost for comparing feature vectors also increases.
The model was trained on a specific dataset, which may limit its ability to generalize to images.
One notable challenge is the increased training time required by Siamese Networks compared to traditional neural network architectures and machine learning algorithms.
To further enhance the capabilities and versatility of the network, two areas of improvement and exploration are proposed:
This project successfully implements a Siamese Network for facial recognition, demonstrating its suitability in identifying individuals even under challenging conditions, such as wearing masks.
The experiment results, achieving an 82% accuracy in recognizing a masked image against an unmasked reference, highlight the system’s practical applicability in real-world scenarios.
Despite its strengths, the project reveals areas for improvement, including preprocessing techniques to accommodate non-preprocessed datasets.
A Comprehensive Guide to Siamese Neural Networks