Add python bindings
Closed this issue · 12 comments
Hey @etemesi254,
It would be interesting to add binding python binding and compare the speed to cv2 or pil JPEG decoding.
Best,
T.C
hi
I'm not familiar with generating python/language bindings yet
but I had this in mind but felt itt would be better if it's released as a whole library(zune-image), we have more decoders with really great speeds hence that's why I wanted to pause such things.
Dear @etemesi254.
I haven't used it myself but you can use: https://github.com/PyO3/pyo3.
Here is an example for loading a single image in python using JPEG PIL for large scale vision training. JPEG decoding takes roughly 60 % of the time and conversion to PyTorch tensors + resize takes 35 %.
If your library could accelerate such things while being simple to install with python, it could have a strong impact in the AI computer vision.
Right now, I am going to try out: https://github.com/lilohuang/PyTurboJPEG
See https://github.com/etemesi254/zune-image/tree/dev/zune-python.
Speed varies, slightly faster than cv2, but I'm measuring cv2.imdecode
with zune_python.decode_image
which have different return types and cv2 has to read the file,(mine doesn't take into account that)
My current recommendation would be to use opencv for now
Correction, a lot faster than I expected when compared to opencv.
Image: https://github.com/etemesi254/zune-image/blob/dev/test-images/jpeg/benchmarks/speed_bench.jpg
Command used (Ipython)
%timeit cv2.imread("./test-images/jpeg/benchmarks/speed_bench.jpg",cv2.IMREAD_COLOR)
Result
419 ms ± 1.22 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit zune_python.decode_file("./test-images/jpeg/benchmarks/speed_bench.jpg")
Result
218 ms ± 12.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Though note the difference in return types (Image, vs ndarray) don't know how much influence that has
https://github.com/etemesi254/zune-image/tree/dev/crates/zune-python
I won't release it in the current cycle, probably the next.
On random images I had, consistently beats opencv in decoding(percentage varies) and as a nicety is that it's fully self contained, i.e doesn't need system libraries and it's a small library (2.2 mb on release).
Building instructions are present in the README
@etemesi254 have you benchmarked against the turbojpeg crate ? that's the one we are using in kornia-rs (opencv like lib for cv/dl) and happy to switch jpeg backend if there's any speed improvement. we got already rust -> numpy apis which improves opencv and pil
turbojpeg uses libjpeg-turbo, which is the same one mozjpeg uses for decoding so the online benchmarks comparing mozjpeg should be similar to turbojpeg
benchmarks at https://etemesi254.github.io/posts/Zune-Benchmarks/
awesome! i'll run integration benchmarks
just benchmarked and definitely no difference between the turbojpeg crate and zune. I'll switch in the next release. Thanks !