serengil / deepface

A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
https://www.youtube.com/watch?v=WnUVYQP4h44&list=PLsS_1RYmYQQFdWqxQggXHynP1rqaYXv_E&index=1
MIT License
14.12k stars 2.18k forks source link

[FEATURE]: Performance Impact of Image.BICUBIC Resampling in Face Alignment #1378

Closed matluuk closed 1 hour ago

matluuk commented 2 days ago

Description

In pull request #1269, the face alignment method was changed to use Image.BICUBIC resampling. While this change may improve image quality, it significantly impacts performance.

Performance Testing

I tested the performance impact by running the ´DeepFace.represent´ function with timeit for 100 rounds. The “yunet” detector and “SFace” face recognition model were used.

from deepface import DeepFace
from deepface.models.FacialRecognition import FacialRecognition

import cv2
import timeit

# Read the image
img = cv2.imread('absolute/path/to/image.jpg')

model_name = "SFace"

model = DeepFace.build_model(model_name=model_name)

execution_time = timeit.timeit('DeepFace.represent(img_path=img, model_name=model_name, detector_backend="yunet")', globals=globals(), number=100)
print(f"Execution time: {execution_time} seconds")

Results

Suggestion

Since the alignment method has no impact on the face recognition performance, it would be beneficial to make the resampling method configurable. This way, users can choose whether to use Image.BICUBIC resampling based on their performance needs.

Environment Details

Additional Info

No response

serengil commented 2 days ago

The sentence Since the alignment method has no impact on the face recognition performance is totally false. As you can confirm from here, alignment increases facial recognition pipeline's accuracy up to 6%.

If you still insist to skip alignment, you already have an option in the input argument. If you set align to False in any function, then this is going to skip alignment as mentioned here. Similarly, you can skip detection as well while setting detector_backend argument to skip as mentioned here.

matluuk commented 2 days ago

I could have described the suggestion better. I am using alignment in both situations that I measured.

The with the sentence Since the alignment method has no impact on the face recognition performance was badly shaped. I tried to refer to comment https://github.com/serengil/deepface/pull/1269#issuecomment-2206364746 that expressed that the usage of ´Image.BICUBIC´ resampling doesn't affect the face recognition performance. Thus it would not matter from face recognition perspective if we are using img = np.array(Image.fromarray(img).rotate(angle)) or img = np.array(Image.fromarray(img).rotate(angle, resample=Image.BICUBIC)) . Changing the image rotation to the latter has significant performance impact I measured with timeit.

To clarifying further: Both timeit measurements were done with alingment enabled. Faster run (7.931031430998701 seconds) was done with the old image rotation that doesn't use ´Image.BICUBIC´ resampling. The slower run (41.79532476099848 seconds) was done with newer image rotation that uses ´Image.BICUBIC´ resampling.

serengil commented 2 days ago

Okay, I could understand it now.

Your request is fair enough. Thank you for the explanation.

serengil commented 1 hour ago

In the current source code, this seems retired already - https://github.com/serengil/deepface/blob/master/deepface/modules/detection.py#L346