Abstract
Have you ever wondered how your smart devices can recognize and distinguish your face from that of others? In this project, you will explore how to create and modify a facial recognition model using a type of artificial intelligence known as neural networks. This project is designed for beginners and requires little to no coding experience. Ready to give it a shot?
Summary
None
Readily available
No issues
Objective
Collect input pictures for a facial recognition AI model and explore how changing the threshold value affects its predictions.
Introduction
Have you ever used face ID to gain access to a smart device? Have you ever thought about how the smart device knows that it is you and not someone else? Or perhaps you have scrolled through your camera roll and noticed it can organize pictures by person?
This is possible through a remarkable technology called facial recognition. Facial recognition technology allows smart devices to identify and distinguish between different faces with impressive accuracy. But how does it work?
Artificial Intelligence (AI) is a branch of computer science focused on the creation of tools that can solve problems and analyze information. Machine learning is a subdivision of AI. Its goal is to create tools that can learn and improve over time using data. In this project, we will dive into the world of neural networks, which are sophisticated computer systems that mimic the human brain. Specifically, we will explore a type of neural network, called a Siamese neural network, known for its effectiveness in facial recognition tasks.
Watch this video for a clear and simple introduction to neural networks:
Siamese neural networks are specialized neural networks that can compare pairs of items, like faces or signatures, by understanding the similarities and differences between them. They work by processing two separate inputs at the same time through identical subnetworks. This comparative ability makes Siamese networks good at tasks like recognizing faces or matching images based on how similar they are, which is helpful in areas like computer vision and pattern recognition.
Watch this video for a clear and simple introduction to Siamese neural networks:
In this project, you will have two tasks. The first task is to gather anchor and positive images. This means collecting reference points called anchors. You also need to gather positive images or examples of the same person. We have already provided you with the negative images and examples that do not match the anchors. These steps are important for training the facial recognition model to tell faces apart accurately.

Examples of how each anchor, positive, and negative images should look relative to each other. As you can see, anchor and positive images are like each other, as they are images of the same person, while negative images are of different people.
Your second task is to adjust two different threshold values. The first value is the detection threshold, which determines whether a detected feature or pattern in an image, such as a face, is present in the image or not. A higher threshold reduces false positives (saying there is a face where there is not a face) but may increase false negatives (saying there is not a face when there really is). The second value is the verification threshold, which determines if the similarity score between the input face features and stored templates (the positive dataset) is high enough for to confirm the identity. Finding the right balance between convenience and security is crucial. You do not want the system to be overly strict, preventing the right person from gaining access. At the same time, you also do not want it to be too lenient, allowing the wrong person to gain access.
Watch this video for an introduction to thresholds:
Most of the code is already written for you, and you only need to focus on the two tasks given (i.e., capturing anchor and positive images and adjusting the threshold values). You do not need to know the ins and outs of all the code, but if you want a more detailed explanation of each section, you can expand the provided comments to learn more.
Terms and Concepts
- Facial recognition
- Artificial Intelligence (AI)
- Machine learning
- Neural network
- Siamese neural network
- Anchor
- Positive
- Negative
- Detection threshold
- Verification threshold
Questions
- What are Siamese neural networks, and why are they effective in facial recognition tasks?
- Explain the concept of anchor and positive images in the context of training a facial recognition model.
- What role do negative images play in training a facial recognition model?
- Describe the function of the detection threshold in a facial recognition system. How does adjusting the threshold impact the system’s performance?
- What is the verification threshold, and how does it contribute to the authentication process in facial recognition?
- Why is it crucial to find the right balance between convenience and security when adjusting threshold values in a facial recognition system?
Bibliography
This is the original paper that we have based our model on:
- Gregory R. Koch. (2015). Siamese Neural Networks for One0shot Image Recognition. Retrieved February 1, 2024.
Watch this video for a deeper dive on neural networks:
- StatQuest with Josh Starmer. (2020, August). The Essential Main Ideas of Neural Networks. YouTube. Retrieved February 1, 2024.
Watch these videos for further explanation of Siamese neural networks:
- Connor Shorten. (2019, August). Siamese Neural Networks. YouTube. Retrieved February 1, 2024.
- DeepLearningAI. (2017, November). C4W4L03 Siamese Network. YouTube. Retrieved February 1, 2024.
- Robotics with Sakshay. (2022, January). Siamese Networks | Face Recognition | Computer Vision on Humans. YouTube. Retrieved February 1, 2024.
This is the dataset we have used for the negative dataset and the pre-made dataset:
- Gary B. Huang, Manu Ramesh, Tamara Berg, and Erik Learned-Miller. (2007, October). LFW Face Database : Main. Retrieved February 1, 2024.
Our code is a modified version of this:
- Nicholas Renotte. (2021, September). GitHub - nicknochnack/FaceRecognition: Face recognition. Retrieved February 1, 2024.
Materials and Equipment
- Computer with internet access
Experimental Procedure

Setting Up the Google Colab Environment
- You will need a Google account. If you do not have one, make one when prompted.
- You have two options for downloading the zip file:
- Option 1: The facial_recognition.zip file does not include anchor and positive data. For this dataset, you will need to take your pictures to serve as anchor and positive data.
- Option 2: The facial_recognition_with_dataset.zip file. This dataset includes anchor and positive images of former Prime Minister Tony Blair.
- Once you have downloaded one of the zip files, unzip it by right-clicking it in your Downloads and clicking 'Extract all.’ This will create a new file folder alongside the Compressed zipped file. Upload the folder called ‘facial_recognition’ or ‘facial_recognition_with_dataset’ (depending on which zip file you chose) inside the newly extracted folder.
- Upload the file folder to Google Drive. You will need to sign in to your Google account at this point or make an account. Upload the file by clicking on the 'New’ button on the upper left corner of the main Google Drive screen, then selecting the 'Folder upload' option. Be sure to select the file folder, not the compressed zipped file.
- There are two ways you can access the Google Colab notebook:
- Within your Google Drive, double-click the folder you just uploaded. Then, double-click on the file called 'facial_recognition.ipynb'. This should automatically open the notebook in Google Colaboratory.
- Another option is to go directly to Google Colaboratory. On the pop-up menu, select 'Google Drive’ then select the file called ‘facial_recognition.ipynb’.
- Change the Runtime type by clicking on ‘Runtime,’ then ‘Change runtime type.’ Next, click on the ‘T4 GPU’ option and save.
- Read the Troubleshooting Tips and How to Use This Notebook sections. Follow the instructions you find there.
Overview
Before you start, briefly review the purpose of each section, which is listed below. The explanations provided here are brief, but you can read more detailed explanations by expanding the block using the triangle icon to the left of each section title. You can also collapse it again by clicking on the triangle icon again. You can also expand the blocks to check their status; a green checkmark indicates the block has been run.
- Setup. This section installs and imports libraries that include special machine-learning functions we will use in our code.
- Collect Positives and Anchors. In this section, if you are doing Option 1, you will collect positive and anchor images necessary for training our model. If you are doing Option 2, this section has already been completed for you.
- Load and Preprocess Images. This section prepares the positive and anchor images you provided and the negative images we provided for training.
- Model Engineering. This section creates the Siamese neural network model.
- Training. This section trains the model using the data that you collected and preprocessed.
- Evaluate Model. In this section, you will analyze the model's results to see how accurate it is. Was it able to tell the difference between you and other people?
- Save the Model. This section saves the model so that you can reuse it for another project without having to wait for a new model to train.
- Real-Time Test. In this section, you can further test your model by testing it in real-time, taking new pictures, and seeing if it will still be able to tell the difference between you and other people.
Now, let us start the project!
1. Setup
This section should have only hidden cells. You can run all of them at once by clicking on the play icon to prepare your coding environment. You can also expand the cells by clicking the arrow to the left of the section title.
2. Collect Positives and Anchors
If you are using the facial_recognition_with_dataset.zip file (Option 2), all you need to do for this section is run the entire section 2. For Option 1, follow the instructions as follows:
- Helper Functions for Collecting Images: These code blocks contain code that will allow you to take photos and save them to your Google Drive. Run this code block.
- Collect Positive and Anchor Classes: This code block will use your webcam to let you take anchor and positive images of yourself. You will need to give Google Colab permission to use the webcam. If you’d prefer, you can also upload images directly to the folder by opening Google Drive, locating the folder, and uploading your images in the ‘positive’ and ‘anchor’ folders. Once you’ve taken or uploaded the pictures, run this code block.
- The function asks for user input. Press ‘a’ to save an anchor image, ‘p’ to save a positive image, and ‘q’ to quit the function. Press ‘Enter’ on your keyboard after you type in one of the options. Click ‘Capture’ under the code block to take the picture.
- These two types of images should be of the same person. Take multiple images, alternating between saving anchor and positive images. Aim to take between 100 to 300 images for each type (anchor and positive). It is recommended to alternate between taking anchor and positive images so that you have an equal amount of data for each type, but it does not matter as long as you end up with roughly the same number of each type.
- You can turn your head and take multiple images of your face at different angles. This helps the model to recognize you from all angles.
- You can also make different facial expressions. This helps the model recognize you no matter what facial expression you may be making.
- You can also try different hairstyles, makeup styles, and accessories (e.g., glasses) to help the model learn to recognize your face even if you change your look.
- If you quit the program before taking enough pictures, you can simply run the cell again to take more.
- These two types of images should be of the same person. Take multiple images, alternating between saving anchor and positive images. Aim to take between 100 to 300 images for each type (anchor and positive). It is recommended to alternate between taking anchor and positive images so that you have an equal amount of data for each type, but it does not matter as long as you end up with roughly the same number of each type.
- The function asks for user input. Press ‘a’ to save an anchor image, ‘p’ to save a positive image, and ‘q’ to quit the function. Press ‘Enter’ on your keyboard after you type in one of the options. Click ‘Capture’ under the code block to take the picture.
- Function to Count Images in Anchor and Positive Directories
- The ‘count_files_in_directory’ function can help you keep track of how many images you have saved so far. When you run it, it will print how many pictures you have in the ANC_PATH and POS_PATH directories. Feel free to run this block multiple times. You may re-run the block above it to continue taking pictures.
3. Load and Preprocess Images
This code prepares images for facial recognition, organizes them for training and testing, and ensures efficient model use. Expand the cells to read more details in the comments. Run this code.
4. Model Engineering
This code creates two models: one to generate embeddings (numerical representations of images capturing their important features) and the Siamese neural network to compare facial similarities using these embeddings. Expand the cells to read more details in the comments. Run this code.
5. Training
- Sections 5.1-5.4 prepare the Siamese neural network for training. Expand the cells to read more details in the comments. Run each cell.
- Section 5.5 trains the Siamese neural network. This training may take a while because of the data size, model complexity, and other factors. Run this code.
6. Evaluate Model
- Make Predictions: Run these code blocks.
- The first code block gets a batch of test data from the test dataset for evaluating the trained Siamese network. It includes input images (‘test_input’), validation images (‘test_val’), and true labels (‘y_true’).
- The second code block predicts similarities between input and validation images using the trained Siamese network. It generates predictions stored in ‘y_hat’, which helps assess the network’s performance and accuracy against true labels. This step aids in evaluating the Siamese neural network’s performance on unseen data.
- The third code block simplifies predictions from the Siamese network by categorizing them as 1 for similarity if they are greater than 0.5 and 0 for dissimilarity if they are less than 0.5. It helps analyze the network’s ability to differentiate between similar and dissimilar image pairs.
- The fourth code block contains the true class labels. You can run the fourth code block to compare the predicted values with the true class labels.
- Calculate Metrics: These blocks will output a value between 0 and 1, with 0 meaning 0% and 1 meaning 100%. Run these code blocks.
- The first code block calculates the accuracy metric for evaluating the Siamese neural network’s performance.
- Accuracy is a measure used to evaluate the performance of a machine-learning model. It represents the proportion of correctly predicted outcomes or labels compared to the total number of instances in the dataset. In simpler terms, accuracy tells you how often the model’s predictions are correct. For example, if the model has an accuracy of 0.25, it means it gets the answer right 25% of the time, or once every four tries.
Mathematically, accuracy is calculated as:
- Accuracy is a measure used to evaluate the performance of a machine-learning model. It represents the proportion of correctly predicted outcomes or labels compared to the total number of instances in the dataset. In simpler terms, accuracy tells you how often the model’s predictions are correct. For example, if the model has an accuracy of 0.25, it means it gets the answer right 25% of the time, or once every four tries.
- The second code block calculates the recall metric for evaluating the Siamese neural network’s performance.
- Recall is another measurement used to evaluate the performance of a machine-learning model. Recall measures the ability of the model to identify all positive instances correctly. It answers the question: Of all the actual positive instances, how many did the model predict correctly? For instance, if the model has a recall of 0.90, it means that out of all of the true data, the model correctly identified 90% of them and 10% were missed or false negatives.
Mathematically, recall is calculated as:
- Recall is another measurement used to evaluate the performance of a machine-learning model. Recall measures the ability of the model to identify all positive instances correctly. It answers the question: Of all the actual positive instances, how many did the model predict correctly? For instance, if the model has a recall of 0.90, it means that out of all of the true data, the model correctly identified 90% of them and 10% were missed or false negatives.
- The third code block calculates the precision metric for evaluating the Siamese neural network’s performance.
- Precision is yet another measurement used to evaluate the performance of a machine learning model, and it focuses on the accuracy of positive predictions by the model. It answers the question: Of the instances the model predicted as positive, how many are actually positive? For instance, if the model has a precision of 0.80, it means that out of all the true data, 80% were actually true and 20% were false.
Mathematically, precision is calculated as:
- Precision is yet another measurement used to evaluate the performance of a machine learning model, and it focuses on the accuracy of positive predictions by the model. It answers the question: Of the instances the model predicted as positive, how many are actually positive? For instance, if the model has a precision of 0.80, it means that out of all the true data, 80% were actually true and 20% were false.
- In these formulas:
- True Positives (TP) are the cases where the model correctly predicted the positive class
- True Negatives (TN) are the cases where the model correctly predicted the negative class
- False Positives (FP) are the cases where the model incorrectly predicted the positive class when it was actually negative
- False Negatives (FN) are the cases where the model incorrectly predicted the negative class when it was actually positive
Table 1. Comparing the predicted result with the actual result, we can determine whether the prediction was a true positive, false positive, true negative, or false negative.Actual Positive Negative Predicted Positive True
PositiveFalse
PositiveNegative False
NegativeTrue
Negative
- The first code block calculates the accuracy metric for evaluating the Siamese neural network’s performance.
- Visualize Results: Run this code.
- This code block allows us to visually compare image pairs from the test dataset. You can change the value of the index (‘input_index’) to view different comparisons.
- Check if the model was accurate.
- If the y_hat and y_true values are both 0, that means that the model correctly identified the faces as not a match.
- If the y_hat and y_true values are both 1, that means that the model correctly identified the faces as a match.
7. Save Model
- Run the code blocks.
- The first code block saves the trained Siamese model's weights to a file named “siamesemodel.h5” in a designated directory. This saves the model's learned parameters and configurations for later use, making sure it works the same each time and can be used with other applications or systems.
- The second block reloads your saved Siamese model. You can resume training, make predictions, or continue analyses without starting from scratch.
- The third block makes additional predictions with your reloaded model.
- The fourth block prints a summary of the Siamese neural network model's architecture.
8. Real Time Test
- Sections 8.1-8.2 include code that allows real-time facial image verification. Expand the cells to read more details in the comments. Run these code blocks.
- Section 8.3 calls the ‘real_time_verification’ function. When this code runs, you can press 'v’ then ‘Enter' on your keyboard to open the webcam again. Make sure to click on 'Capture’ to capture a new image. The code will take a moment to verify whether the image matches the existing anchor images. If it matches, it will output 'True’, and if the image does not match, it will output ‘False.’
- Experiment with changing the ‘detection_threshold’ and ‘verification_threshold’ values.
- Start by changing only the detection_threshold value. Start at 0 and increase by 0.1 (0.1, 0.2, 0.3, etc.) until you get to 1. At which level does it start to recognize faces?
- Next, keep the detection_threshold at 0.5, and change the verification_threshold. Start at 0 and increase by 0.1 (0.1, 0.2, 0.3, etc.) until you reach 1. At which level does it start to recognize your face?
- Finally, experiment with changing both values to see which values of detection_threshold and verification_threshold are best for recognizing a face as yours or not.
- Try seeing if the model can tell the difference between you and a family member. If you are using the facial_recognition_with_dataset.zip file, try seeing if the model can tell the difference between you and Tony Blair (search for an image of Tony Blair on the internet and show it to the camera). If it cannot, try increasing the threshold by 0.1 until it can. Experiment with changing the threshold values for both the detection_threshold and verification_threshold to find which combination is the best for recognizing your face or Tony Blair’s face.
Ask an Expert
Global Goals
The United Nations Sustainable Development Goals (UNSDGs) are a blueprint to achieve a better and more sustainable future for all.
Variations
- Try adding images of a friend or a family member to the anchor and positive classes. Will the model classify both you and your friend?
- Implement the ROC (Receiver Operating Characteristic) and find the AUC (Area under ROC curve) to find the optimal threshold value. The ROC curve shows how well a model can distinguish between classes and AUC measures the overall performance of the model.
- Try testing the model with images of a celebrity, friend, or family member who shares your race or has facial features similar to yours. How does this affect the model’s accuracy?
Careers
If you like this project, you might enjoy exploring these related careers:
Related Links
- Science Fair Project Guide
- Other Ideas Like This
- Artificial Intelligence Project Ideas
- My Favorites
- AI Hyperrealism: Do AI Faces Look More Real Than Human Faces?
- Racial and Gender Bias in AI-Generated Images
- Happy or Sad? Use Artificial Intelligence to Classify Faces
- Can Humans Recognize AI-Generated Images?
- Can Humans Recognize AI-Generated Images?












