Skip to content

Batching on .extract_faces to improve GPU utilization and throughput#116

Closed
galthran-wq wants to merge 7 commits intoserengil:masterfrom
galthran-wq:batching
Closed

Batching on .extract_faces to improve GPU utilization and throughput#116
galthran-wq wants to merge 7 commits intoserengil:masterfrom
galthran-wq:batching

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.

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.

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.

@ZxUkr
Copy link

ZxUkr commented May 7, 2025

accept a list of images and processed them in a batch

Second attempt :)
#55

@serengil serengil closed this Nov 27, 2025
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.

3 participants

Comments