2
\$\begingroup\$

My code takes two .jpg files (two selfies) and extracts a vector of 128 features from each selfie. After this extraction we take these two vectors and get their cosine distance.

I timed the script and it takes 1.6s to load the models and 2.2s to extract the 128 features from the selfies. The time to load the models doesn't annoy me, but of course it would be nice to know if there's a way to load them faster. What I really want to do is improve the extraction of the features and get the results faster.

I tried to get the features from both images at the same time by running the function face_vector with the threading library, but apparently it's not possible to multithread dlib functions. I'm not really sure why it's this way, but it's something related with GIL. It's new to me.

I want to know if it's possible to achieve a faster execution time making code changes or if maybe I should seek better hardware (should I try to run this in the cloud, with a better PC?)

The images I'm using in this code have 80kb in size and dimension 774x1032.

My PC (pretty old): i5-3570K @ 3.4GHz, 8GB RAM.

My code:

from scipy import spatial
import numpy as np
import dlib
import cv2

def load_image(path):
    img = cv2.imread(path, 1)
    return img[...,::-1]

def face_vector(face_image):
    face_locations = face_detector(face_image, 1)
    pose_predictor = pose_predictor_68_point
    raw_landmarks = [pose_predictor(face_image, face_location) for face_location in face_locations]
    return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, 1)) for raw_landmark_set in raw_landmarks]

# Loading models
face_detector = dlib.get_frontal_face_detector()
pose_predictor_68_point = dlib.shape_predictor('./models/shape_predictor_68_face_landmarks.dat')
face_encoder = dlib.face_recognition_model_v1('./models/dlib_face_recognition_resnet_model_v1.dat')

# Extracting features
image1 = load_image('josie1.jpg')
image2 = load_image('josie2.jpg')
f1 = face_vector(image1)[0]
f2 = face_vector(image2)[0]
d = spatial.distance.cosine(f1, f2)
print('distance: %s' %d)
\$\endgroup\$
  • \$\begingroup\$ If you want to know if hardware could change something, run it on Google Collaboratory with the GPU runtime (OpenCV supports cuda operations). If it's still slow, maybe there's a problem. But I wouldn't be surprised if it's an hardware issue. \$\endgroup\$ – IEatBagels Jan 25 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.