Powerful HuggingFace Model pipelines: Hands-On Image Processing with Olympict

Gabriel Kasser
3 min readNov 13, 2023

--

Introduction

Greetings, fellow tech enthusiasts! In this hands-on guide, we’ll explore the powerful capabilities of Olympict, a Python library tailored for parallel image processing. We’ll delve into its seamless integration of Hugging Face’s cutting-edge image classification and object detection models, unlocking a new realm of efficiency in image-related tasks.

Under the hood:

This library is based on a multiprocessing parallelization one and leverages its CPU efficiency as described in there: Olympipe

Installation

Let’s kick things off by installing Olympict. Open your terminal and run:

poetry add olympict[hf]
or
pip install olympict[hf]

Integrating Hugging Face Models

Olympict makes it a breeze to incorporate Hugging Face’s models. We’ll walk through the process of setting up and utilizing these models for image classification.

# Image classification
# !pip install timm

import os
from olympict.pipelines import ImagePipeline
from olympict.files.o_image import OlympImage


def classify_path(o: OlympImage) -> OlympImage:
# change output path to copy image in classname folder
o.path = f"./output/{o.metadata['label']}/{os.path.basename(o.path)}"
return o


if __name__ == "__main__":
from torch.multiprocessing import set_start_method

set_start_method("spawn")

HF_model = "google/mobilenet_v2_1.0_224"

(
ImagePipeline.load_folder(os.path.expanduser("~/Downloads"))
.classify(HF_model)
.detect("facebook/detr-resnet-50")
.task(classify_path)
.save()
.wait_for_completion()
)

Parallel Processing Magic with detection

One of Olympict’s standout features is its ability to tap into parallel processing, maximizing hardware capabilities for lightning-fast image analysis. Let’s see this in action with a hands-on example showing the detection model results as it goes through the process.

import os

from olympict.pipelines import ImagePipeline
from torch.multiprocessing import set_start_method


if __name__ == "__main__":
set_start_method("spawn")

HF_MODEL = "facebook/detr-resnet-50"

res = (
ImagePipeline.load_folder(os.path.expanduser("~/Downloads"))
.detect(HF_MODEL)
.draw_relative_bboxes()
.debug_window('View')
.save_to_folder("./test_output/bboxes")
.wait_for_results()
)

print([r.metadata for r in res]) # Print bboxes

Real-World Computer Vision Project

Now that we’ve got the basics down, let’s apply Olympict to a real-world scenario. Suppose you’re working on a computer vision project that involves classifying a dataset of diverse images. Olympict can significantly speed up the classification process.

What this little snippet does is:
- Load an image folder in the natural order
- Resize the images to 640x480 using black as padding
- keeping 1 frame in 24 (if we want to process a 24fps and kepp one frame per second for example)
- detecting objects with the facebook DeTr model
- draw those bboxes
- save the whole video to “./video.mp4” file
- All of those operations are run at the same time on consecutive items to maximize throughput (as per olympipe default behaviour)

import os

from olympict import ImagePipeline
from olympict.files.o_image import OlympImage
from torch.multiprocessing import set_start_method


def path_fn(_: OlympImage):
return "./video.mp4"

if __name__ == "__main__":
set_start_method("spawn")
HF_MODEL = "facebook/detr-resnet-50"

(
ImagePipeline.load_folder(os.path.expanduser("~/Camera"))
.resize((480, 640), pad_color=(0, 0, 0))
.keep_each_frame_in(1, 23)
.detect(HF_MODEL)
.draw_relative_bboxes()
.to_video(path_fn, 1)
.wait_for_completion()
)

Contributing to Olympict

Olympict is an open-source project, and contributions are not only welcome but encouraged. Dive into the Gitlab repository, explore the codebase, and join the adventure.

Conclusion

In this hands-on guide, we’ve scratched the surface of Olympict’s capabilities. Whether you’re an image processing aficionado or a developer looking to streamline your workflow, Olympict provides a robust solution.

Explore the documentation, experiment with the code snippets, and witness the parallel power of Olympict in action. Your journey into efficient image processing begins now!

Happy coding!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response