JPEG2RGB Array Showdown: libjpeg-turbo vs kornia-rs vs TensorFlow vs torchvision
Tags: python, machinelearning, computervision, benchmarking
Published: 2024-03-11 22:55:26

In the realm of image processing and machine learning, the efficiency of loading and preprocessing images directly impacts our projects' performance. Drawing inspiration from the [Albumentations library benchmark](https://github.com/albumentations-team/albumentations/tree/main/benchmark),  I've conducted a detailed analysis comparing how different Python libraries handle the conversion of JPG images into RGB numpy arrays.

You can find all the code for this benchmark here : [https://github.com/ternaus/imread_benchmark](https://github.com/ternaus/imread_benchmark)

## The Need for Speed in Benchmarking

Our goal is straightforward: assess the efficiency of libraries for a task that's routine yet crucial in machine learning - turning JPGs into RGB numpy arrays. We're not just comparing numbers; we're looking for practical insights that can influence choice and implementation.

## Ensuring a Level Playing Field

A fair benchmark requires uniformity, hence the conversion of all images to RGB numpy arrays regardless of the libraries' default formats (like BGR for OpenCV). This step, although necessary, introduces an additional layer to our testing but doesn't significantly skew the results based on our preliminary analysis.

## Benchmark Setup

**Hardware Used:** AMD Ryzen Threadripper 3970X 32-Core Processor

With this powerhouse CPU, we ensure that our benchmarks focus purely on library performance without hardware-induced bottlenecks.

## Observations and Insights

![Plots with results](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4gvse8qg00jzcf5no39e.png)

![Table with results](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ae2s1irg0c6xs0rh3fhj.png)

The benchmark revealed a mix of expected and surprising results:

* Traditional choices like OpenCV and imageio hold up well in terms of reliability.
* Newer or specialized solutions like TensorFlow, kornia-rs, and jpeg4py, however, show a noticeable edge in performance, potentially changing how we approach data preparation for neural network training.

## Making Informed Choices in Tool Selection

Time efficiency is crucial in data processing. Our findings highlight key performers and remind us of the importance of selecting the right tools based on our specific needs and workflows.

* [Kornia-rs](https://github.com/kornia/kornia-rs) stands out for those seeking modern, efficient image processing, particularly when not tied to TensorFlow or Torchvision ecosystems.
* Despite its efficiency, jpeg4py's lack of updates may raise concerns. If your workflow is entrenched in TensorFlow or Torchvision, their native image processing capabilities might suffice.
* For broader applications, particularly where libjpeg-turbo's performance can be leveraged, kornia-rs presents an appealing option.

In closing, this benchmark doesn't dictate a one-size-fits-all solution but rather provides data to help tailor tool selection to your project's requirements. Whether you're deep into AI research or developing the next big computer vision application, the right tools can significantly streamline your workflow.