TensorFlow is a versatile tool to create machine-learning models.
In this post, we’ll look at how to create a face recognition system with TensorFlow, an open-source machine learning framework. We will go over the essential processes in creating a successful face recognition system, from gathering and preparing data to train and assessing a model.
You will get first-hand experience with TensorFlow to create facial recognition with the aid of code snippets and real-world examples. You are welcome to follow along as we proceed.
Introduction to TensorFlow
TensorFlow is a free and open-source library. It is a symbolic math toolbox that uses dataflow and differentiable programming. You can handle a range of tasks with it, including deep neural network training.
TensorFlow is powerful and adaptable. Likewise, it is a great tool for developing and deploying machine learning models. You can build complicated models with several layers and tensor operations. Also, pre-built models in the library can be fine-tuned for specific needs.
Furthermore, TensorFlow has a huge and expanding user community. So, there is a plethora of information and help for individuals who are new to the platform.
TensorFlow is popular for machine learning in part because it provides an end-to-end workflow. So, you can easily construct, train and deploy models. It provides tools and strategies for improving and scaling models to fit specific demands. It varies from data pre-processing to model deployment.
What is Face Recognition?
Face recognition is a computer vision task that identifies a person’s identification based on their face. This technique recognizes facial traits, such as the shape and texture of the eyes, nose, and mouth.
And, it compares them to a database of known faces to identify a match. Face recognition has several uses, including security systems, photo organization, and biometric authentication.
Face recognition algorithms’ accuracy has substantially increased in recent years as a result of breakthroughs in machine learning.
Importing Necessary Libraries
Before starting anything, we need to import the libraries needed for our model. Tensorflow (tf) is imported and utilized to create and train the model. <(p>
“numpy” performs mathematical calculations and data processing.
“matplotlib.pyplot” is imported as plt and used for data charting and visualizations.
Finally, “fetch lfw people” is imported from sklearn. datasets and used to load the facial recognition dataset. This function is part of the scikit-learn toolkit. Thanks to this function we didn’t have to upload another dataset. This is already built in sckit-learn.
And, it gives you access to a wide range of datasets for machine learning applications. In this scenario, we use the fetch lfw people method to retrieve the “Labeled Faces in the Wild” (LFW) dataset. It comprises photos of people’s faces as well as the labels that go with them.
These libraries are critical in the implementation and evaluation of our face recognition model.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt from sklearn.datasets
import fetch_lfw_people
Preprocessing and Loading the Face Recognition Dataset
In this part, we utilize the “fetch lfw people” function to preprocess the facial recognition data. First, we use fetch lfw people with the option “min faces per person=60”. This indicates that we only want to include persons in the dataset who have at least 60 photos. Hence, we ensure that our model has adequate data to learn. Also, this lowers the danger of overfitting.
The data and labels from the faces object are then extracted and assigned to the variables X and y. X hol.
We are now ready to train our facial recognition model using preprocessed data and labels.
faces = fetch_lfw_people(min_faces_per_person=60)
X = faces.data
y = faces.target
target_names = faces.target_names
Splitting Training and Test Sets
In this step, we split our face recognition dataset into two halves using the train test split method from sklearn.model selection. The goal of this split is to assess our model’s performance after training
The train test split function accepts as inputs data X and labels y. And, it divides them into training and test sets. We select test size=0.2 in this example. This implies that 20% of the data will be utilized as the test set and 80% as the training set. Furthermore, we use random state=42 to ensure that the data is divided consistently each time the code is performed.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Preparing the Data
The purpose of preprocessing data is to prepare it for entry into the model. The data is preprocessed in this code by dividing each data point by 255.
What motivated us to achieve this? Normalization is a preprocessing procedure used in machine learning to guarantee that all features are on the same scale. In this scenario, dividing by 255 scales the data to a range of 0 to 1, which is a usual picture data normalization step.
This speeds up the model’s convergence and can increase its performance.
X_train = X_train / 255.0
X_test = X_test / 255.0
Creating the Mode
We want to identify the individual whose face appears in a picture. In this case, we will use a fully connected network, often known as a dense network. It is an artificial neural network that was used to create the model.
Artificial neural networks are modeled after how the human brain operates and is organized. They are made up of information-processing nodes or neurons that are linked. Each neuron in a layer in a dense network is linked to every neuron in the layer above it.
The model has four layers in this code. To be fed into the next layer, the input data is flattened in the first layer into a one-dimensional array. The 128 and 64 neurons in the following two layers, accordingly, are completely linked.
The ReLU activation function is a unique activation function used by these layers. With that, we can get the model to learn non-linear correlations between the inputs and outputs. The last layer employs the softmax activation function to make predictions. And, it is a fully connected layer with as many neurons as there are potential classes.
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(62 * 47,)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(len(target_names), activation='softmax')
])
Compiling of the Model
The model is compiled using the “compile” function. We need to prepare the model for training. So, we will define the optimizer, loss function, and metrics that will be used to assess the model.
During training, the optimizer is in charge of changing the model’s parameters. The “adam” optimizer is a popular deep-learning optimization technique.
We use the loss function to assess the model’s performance on the training data. Because the target labels are integers reflecting the image’s class rather than one-hot encoded vectors, the “sparse categorical crossentropy” loss function is favorable.
Finally, we define the metrics to assess the model, in this case, “accuracy”.
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
Model Training
We will use the “fit” function to train the model.
We will be providing the training data (X train) and related labels (y train), as well as setting the number of epochs (iterations) to run as 10. The training procedure modifies the model weights to reduce a loss (the difference between predicted and real labels) and improve the accuracy of training data.
model.fit(X_train, y_train, epochs=10)
Model Evaluation
Now, we need to assess the trained model on the test data. We use the test loss and test accuracy are used to assess the model’s performance. On the test data X test and the test labels y test, we need to call “the model.evaluate function”
The function outputs the test accuracy and test loss. The variables test loss and test accuracy, respectively, contain these values. Finally, we use the “print” function to output the test accuracy.
test_loss, test_accuracy = model.evaluate(X_test, y_test)
print("Test accuracy:", test_accuracy)
Predicting Classes and Getting the Predicted Classes
Using the training model and the test data, the algorithm makes predictions. When the test data is passed to the “model.predict” method, it outputs an array of predictions for each picture in the test set.
The target class name for each picture is then retrieved from the “target names” list using the “np.argmax” function to identify the index with the greatest predicted probability. This index is then used to determine the predicted class for each image.
Using a list comprehension, all of the predictions in the “predictions” array are subjected to this method, resulting in the “predicted classes” list.
predictions = model.predict(X_test)
predicted_classes = [target_names[np.argmax(prediction)] for prediction in predictions]
Visualizing the Predictions
We can now see how our model looks.
To evaluate how well the model is doing, the first 10 photos and their predictions will be shown. It will plot the photos in grayscale and display both the actual class of the image and the class predicted by the model using the matplotlib.pyplot module.
The “imshow” function is used by the for loop to plot each of the first 10 test set photos. Target names[y test[i]] and predicted classes[i] are used to determine the image’s actual class and predicted class, respectively. The titles of each plot are then indicated by these classifications.
Finally, the plot is displayed using the plt.show() method.
for i in range(10):
plt.imshow(X_test[i].reshape(62, 47), cmap='gray')
plt.title(f"True: {target_names[y_test[i]]}, Predicted:{predicted_classes[i]}")
plt.show()
Wrap Up
TensorFlow offers a complete and flexible environment for creating machine learning models.
By fine-tuning the model to meet particular requirements or by adding new developments in machine learning, the accuracy of the model may be increased even further.
TensorFlow and facial recognition will likely be used increasingly in industries like security systems, biometric authentication, and healthcare in the future. We will be seeing fascinating innovations shortly.
Leave a Reply