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 .extract_faces to improve GPU utilization and throughput #116

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

galthran-wq
Copy link

Tickets

#105

What has been done

With this PR, .extract_faces can accept a list of images and processed them in a batch

How to test

make lint && make test

@galthran-wq galthran-wq changed the title Batching on .extract_faces to improve GPU utilization Batching on .extract_faces to improve GPU utilization and throughput Feb 12, 2025
@galthran-wq
Copy link
Author

galthran-wq commented Feb 12, 2025

several problems:

  • currently input images have to be of the same size (hence resize in the test), otherwise it raises an uninformative error. maybe do the same resizing in the .extract_faces, but I am not quite sure if it would hurt performance
  • I am not yet unable to reproduce a performance improvement in colab, but here is what my local 4090 says when processing 1k images:
Batch Size: 1, Total Time: 101.9
Batch Size: 10, Total Time: 61.3
Batch Size: 20, Total Time: 59.9

I'll update on this

@serengil what do you think?

@serengil
Copy link
Owner

several problems:

  • currently input images have to be of the same size (hence resize in the test), otherwise it raises an uninformative error. maybe do the same resizing in the .extract_faces, but I am not quite sure if it would hurt performance
  • I am not yet unable to reproduce a performance improvement in colab, but here is what my local 4090 says when processing 1k images:
Batch Size: 1, Total Time: 101.9
Batch Size: 10, Total Time: 61.3
Batch Size: 20, Total Time: 59.9

I'll update on this

@serengil what do you think?

IMO, batch should not be supported at all :) You should raise a ticket first to discuss this.

@galthran-wq
Copy link
Author

why not if it improves performance without hurting anything else (I hope)?

@serengil
Copy link
Owner

why not if it improves performance without hurting anything else (I hope)?

My personal opinion, I will not close this because of the time you spend


def test_batch_resize():
img_path = "tests/dataset/img11.jpg"
faces = RetinaFace.extract_faces(img_path=[img_path, img_path], target_size=(224, 224))
Copy link
Owner

Choose a reason for hiding this comment

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

would you please run this test for 2 images where 1st one has 1 face and 2nd one has many faces.

i want to see result is size of 2 in this case, and 2nd item of that list is having many items.

you can add this image to dataset folder and use it.

@@ -117,3 +117,68 @@ def test_resize():
plt.imshow(face)
plt.show()
logger.info("✅ resize test done")

Copy link
Owner

Choose a reason for hiding this comment

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

do you mind to add tests for detect_faces also? at least its return type for single item and many items.

@@ -15,6 +15,31 @@ def int_tuple(t):
return tuple(int(x) for x in t)


def resize_images(img_paths: list[str]) -> list[np.ndarray]:
Copy link
Author

@galthran-wq galthran-wq Feb 17, 2025

Choose a reason for hiding this comment

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

this is the key thing to make batching work. It is required to be able to concat images' tensors. It will not currently work it we do not call this function beforehand.

basically, it adds black strips to each image to be max width and max height of the batch (therefore all of the same size)

ideally, I think, we'd like for this operation to happen behind the scenes on .extract_faces and the results (facial area, eyes, nose) be accordingly transformed after prediction in accordance with the original image.

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