PERF: utils/cv/read_image_as_pil - 11x faster #1277
TristanBandat
started this conversation in
Ideas
Replies: 1 comment
-
|
I was able to test and added via this PR #1353 Thank you for detailed report and test. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I was line profiling the
get_sliced_prediction()function and continued with theget_prediction()function.In the
get_prediction()function I was wondering why every image was converted to a PIL image in order to be then converted to a contiguous numpy array. As theget_sliced_prediction()function already provides numpy arrays and the contiguity is anyway given withnp.ascontiguousarray(image_as_pil), I wanted to skip the np -> PIL -> np conversion.So I looked into the
read_image_as_pil()function.BTW that function was taking about 30% of the
get_sliced_prediction()computation time.As it turns out, there is a very small BUG in the code, that I intended to fix:
If you provide the function with a very small image, you introduce a hard-to-find bug.
Additionally, the PIL convertion (last line from above) takes ~99% of the CPU time of this function.
So I wrote a function based on cv2 (which is already imported) to fix the bug:
Next, I added a flag called
return_arrto theread_image_as_pil()function, with which one could skip that costly conversion:Of course you have to add some if/else and some PIL <-> numpy conversions as well.
So now the call in the
get_prediction()function looks like this:With those changes I was able to get a reliable performance improvement of ~11x of the
read_image_as_pil()function.This concluded to a reliable performance improvement of ~26% of the
get_sliced_prediction()function.And a small bug fixed.
NOTE:
I run the tests on my notebook with a Intel Core Ultra 7 155H and using a RTX 500 Ada GPU for the prediction.
Model is a torchvision FasterRCNN and I performed my tests on 108 slices.
The function
read_image_as_pil()was called twice in my setup, once withPIL.Image.Imageoutput and once with anp.arrayas output.I'm running SAHI 0.11.36 with Python 3.13.9.
I don't provide the line profiles on purpose (unless needed), one has to implement proper testing scripts and line profiling is not well suited for comparable numbers across machines...
Beta Was this translation helpful? Give feedback.
All reactions