Face Recognition for Custom Images using Tensorflow

Abhishek Agarwal
4 min readJul 15, 2023

--

Face recognition is a complex task that typically involves the use of deep learning models and neural networks. While TensorFlow provides a powerful framework for building and training such models, it does not have a built-in face recognition module. However, TensorFlow can be used in conjunction with other libraries and techniques to implement face recognition systems.

One common approach is to use a pre-trained deep learning model, such as a Convolutional Neural Network (CNN), for face recognition. TensorFlow provides tools for loading and using pre-trained models, such as the TensorFlow Hub and the Keras API.

Here how we can do it Face Recognition for our custom images using Tensorflow

Before writing the code we need 3 separate folders named Train, Validation and Test all having two folders named Messi and Ronaldo with their images.

import tensorflow as tf
import numpy as np
import cv2
import os
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
  1. First import the necessary libraries
img = image.load_img('/content/custom image tensorflow/test/messi/download (1).jpg')
plt.imshow(img)

2. In this line, load_img function from TensorFlow's Keras image module to load an image file. The file path to the image is specified as '/content/custom image tensorflow/test/messi/download (1).jpg'. This function returns a PIL (Python Imaging Library) image object.

train = ImageDataGenerator(rescale = 1/255)
validation = ImageDataGenerator(rescale = 1/255)
train_dataset = train.flow_from_directory('/content/custom image tensorflow/train/',
target_size = (200,200),
batch_size=3,
class_mode='binary')
validation_dataset = train.flow_from_directory('/content/custom image tensorflow/validation/',
target_size = (200,200),
batch_size=3,
class_mode='binary')

3. In these lines, you create two instances of the ImageDataGenerator class: train and validation. The rescale argument is used to normalize the pixel values of the images by dividing them by 255, which scales the pixel values between 0 and 1.

model= tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape = (200,200,3)),
tf.keras.layers.MaxPool2D(2,2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPool2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPool2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid'),
])

4. In this code, you define a sequential model using tf.keras.models.Sequential. The model is a stack of layers, where the data flows sequentially through each layer.
The layers included in this model are:

  1. Conv2D: This layer performs convolutional operations on the input data. The first Conv2D layer has 16 filters, a kernel size of (3,3), and uses the ReLU activation function. The input_shape parameter specifies the shape of the input images as (200,200,3), where 3 represents the number of color channels (RGB).
  2. MaxPool2D: This layer performs max pooling to reduce the spatial dimensions of the data. The pooling size is (2,2), meaning it downsamples the input by taking the maximum value in each 2x2 region.
  3. Two additional sets of Conv2D and MaxPool2D layers are added, increasing the number of filters to 32 and 64, respectively. These layers help capture higher-level features in the data.
  4. Flatten: This layer flattens the 3D output from the previous layer into a 1D vector, preparing it for the dense layers.
  5. Dense: This layer is a fully connected (dense) layer with 512 units and uses the ReLU activation function.
  6. Dense: The final dense layer has 1 unit and uses the sigmoid activation function, suitable for binary classification tasks.
model.compile(loss='binary_crossentropy',
optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.001),
metrics=['acc'])
model_fit = model.fit(train_dataset,
steps_per_epoch=3,
epochs=30,
validation_data = validation_dataset

)

5. In this section, you compile the model using the compile method. The loss parameter is set to 'binary_crossentropy', which is commonly used for binary classification tasks. The optimizer parameter is set to tf.keras.optimizers.RMSprop with a learning rate of 0.001. You can adjust the learning rate or use different optimizers based on your requirements. Finally, you specify 'acc' as the metric to monitor during training, which represents the accuracy of the model.
Here, you train the model using the fit method. The train_dataset and validation_dataset are the data generators you created earlier. The steps_per_epoch parameter determines the number of batches to consider as one epoch during training. In this case, you set it to 3, meaning each epoch will process 3 batches. The epochs parameter specifies the total number of training epochs. You can adjust this value based on the convergence of your model. Lastly, you provide the validation_dataset as the validation data to monitor the model's performance during training.

dir_path = '/content/custom image tensorflow/test/ronaldo'
for i in os.listdir(dir_path):
img = image.load_img(dir_path+'//'+ i, target_size=(200,200))
plt.imshow(img)
plt.show()
X = image.img_to_array(img)
X = np.expand_dims(X, axis=0)
images= np.vstack([X])

val = model.predict(images)
if val==0:
print("MESSI")
else:
print("RONALDO")

6. Here, you convert the loaded image into a NumPy array using image.img_to_array. Then, you expand the dimensions of the array using np.expand_dims to match the shape expected by the model. Finally, you stack the image array vertically using np.vstack to create a batch-like structure.

So here are the results:

The Model is able to predict the image of Ronaldo and Messi.

It’s important to note that face recognition is a complex task that often requires a large and diverse dataset, significant computational resources, and expertise in deep learning and computer vision.

--

--