Face Recognition
[ Baseline ] Face Recognition
The baseline for the challenge using MSE for calculating the similariy between images
Starter Code for Face Recognition
In this baseline we will be using basic Mean Squared Error to compare the missing person image to all target faces ans generate our predictions.
Downloading Dataset¶
Installing puzzle datasets via aicrowd-cli
In [1]:
!pip install aicrowd-cli
# Make sure to re-run below code whenever you restart colab notebook
%load_ext aicrowd.magic
In [2]:
# Logging in from our AIcrowd account. Make sure you have accepted the puzzle rules before logging in!
%aicrowd login
In [3]:
# Creating a new data directory and downloading the dataset
!rm -rf data
!mkdir data
%aicrowd ds dl -c face-recognition -o data
In [4]:
# unzipping the data
!unzip data/data.zip -d /content/data > /dev/null
Importing Libraries¶
In [5]:
import pandas as pd
import os
import numpy as np
import random
from tqdm.notebook import tqdm
from google.colab.patches import cv2_imshow
import cv2
random.seed(42)
Reading Dataset¶
In [6]:
# Getting all image ids from a folder
image_ids = os.listdir("data/missing")
len(image_ids)
Out[6]:
In [7]:
# Reading a sample missing person image
sample_image_id = random.choice(image_ids)
sample_missing = cv2.imread(os.path.join("data/missing", sample_image_id))
cv2_imshow(sample_missing)
In [11]:
# Reading the corrosponding target faces
sample_target = cv2.imread(os.path.join("data/target", sample_image_id))
cv2_imshow(cv2.resize(sample_target, (512, 512)))
In [12]:
# We can also split all the faces in the target image to convert them into individual faces images
sample_target_faces = []
def get_target_face(face_no, target_image):
# Top-Left x, y corrdinates of the specific face
x, y = (int(face_no[0]))*216, (int(face_no[1]))*216
target_face = target_image[x:x+216, y:y+216]
return target_face
In [13]:
# Showing a sample face from a sample target image
sample_target_face = get_target_face("96", sample_target)
cv2_imshow(sample_target_face)
Generating Predictions¶
In [14]:
predictions = {"ImageID":[], "target":[]}
for img_id in tqdm(image_ids):
missing_image = cv2.imread(os.path.join("data/missing", img_id), 0)
missing_image = cv2.resize(missing_image, (216, 216))
target_image = cv2.imread(os.path.join("data/target", img_id), 0)
# Face no with minimum MSE
min_mse_face_no = 0
min_mse = 10000000
for face_no in range(100):
# Getting the specific face from the target image
face_no = str(face_no)
face_no = face_no.zfill(2)
target_face = get_target_face(face_no, target_image)
# Calculating MSE
mse = np.square(np.subtract(missing_image, target_face)).mean()
# print(mse)
if mse < min_mse:
min_mse = mse
min_mse_face_no = face_no
predictions['ImageID'].append(img_id.replace(".jpg", ""))
predictions['target'].append(min_mse_face_no)
In [15]:
submission = pd.DataFrame(predictions)
submission
Out[15]:
Saving the Predictions¶
In [16]:
# Saving the predictions
!rm -rf assets
!mkdir assets
submission.to_csv(os.path.join("assets", "submission.csv"), index=False)
Submitting our Predictions¶
In [17]:
%aicrowd notebook submit -c face-recognition -a assets --no-verify
Congratulations to making your first submission in the puzzle 🎉 . Let's continue with the journey by improving the baseline & making submission! Don't be shy to ask question related to any errors you are getting or doubts in any part of this notebook in discussion forum or in AIcrowd Discord sever, AIcrew will be happy to help you :)
Have a cool new idea that you want to see in the next blitz ? Let us know!
In [ ]:
Content
Comments
You must login before you can post a comment.