Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batching on .represent to improve performance and utilize GPU in full #1433

Merged
merged 13 commits into from
Feb 16, 2025

Conversation

galthran-wq
Copy link
Contributor

Tickets

#1101

What has been done

With this PR, .represent is able to accept a list of paths/numpy arrays and process them all in a batch.

How to test

make lint && make test

I've made a collab notebook
https://colab.research.google.com/drive/1bV0yyrdT1a0a4dyemoeL5xf28w3ql1fd#scrollTo=lVndmFF5Kls7
which shows the >10x performance improvement and also the fact, that with batch_size=1 the GPU is almost not utilized

download

@serengil
Copy link
Owner

Would you please write unit test to make it clearer while reviewing?

@@ -81,3 +83,49 @@ def test_max_faces():
max_faces = 1
results = DeepFace.represent(img_path="dataset/couple.jpg", max_faces=max_faces)
assert len(results) == max_faces


@pytest.mark.parametrize("model_name", [
Copy link
Owner

@serengil serengil Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do the with only one model - e.g. Facenet

I excluded some of those models from test otherwise it will take too long tests to be performed in github

Copy link
Contributor Author

@galthran-wq galthran-wq Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's just that some models have custom forward logic, which I also had to tweak a little bit.

maybe still keep those models(Dlib, SFace, VGGFace), along with some keras one, like, Facenet?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay but some are optional (e.g. dlib), these are not installed in github actions. so, your tests will be failed.

@serengil
Copy link
Owner

There are some linting issues broken the actions

@serengil
Copy link
Owner

LGTM

Thank you for your contribution

@serengil serengil merged commit ca73032 into serengil:master Feb 16, 2025
2 checks passed
"dataset/img2.jpg",
"dataset/img3.jpg",
"dataset/img4.jpg",
"dataset/img5.jpg",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you add couple.jpg here, there are 6 input images but in the response we will have 7 items.

the bad part, we cannot understand that which image has 2 faces. i am creating a PR to store input image's index in the response payload.

we may consider to have List of List of Dict response type for batch inputs in the future.

Copy link
Contributor Author

@galthran-wq galthran-wq Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a good idea to have List of List of Dict. I could make a PR now, or perhaps later, when batched detection is merged, because I also had in mind to use batched detection for .represent (now it is done in the for loop)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do the initial changes. PRs are always welcome!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i merged a workaround PR for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will optimize this because if batch size is long, this approach gives O(n^2) complexity.

I plan to do something with dict, which decrease the complexity O(n)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants