DEV Community

Miguel Ángel Cabrera Miñagorri
Miguel Ángel Cabrera Miñagorri

Posted on

Stateless vs Stateful hooks in your computer vision applications with Pipeless

Pipeless is an open-source framework that allows you to create and deploy computer vision applications in minutes. Similar to how you develop serverless web applications, with Pipeless you just need to provide basic code functions, for example in Python, that take some frame data.

In Pipeless, all the hooks (functions) are written in the same way, however, there are two main kinds of hooks depending on whether they need to maintain an internal state or not.

Stateless hooks

Stateless hooks do not require to maintain an internal state. This is the default kind of hook that is created.

A stateless function is a function that produces an output that only depends on the inputs. A previous call to the function does not affect the result of the next calls.

In the context of Pipeless, where you provide hooks (functions) that take a video frame as input and produce some data as output, you often implement stateless hooks.

Direct examples of stateless hooks are pre-processing and post-processing hooks. A direct example of a stateless hook is a pre-processing hook to resize the frame. Resizing the frame is a typical step where you change the frame dimensions to match a model input format. This operation depends exclusively on the input frame, it does not get affected by the resizing of previous frames, thus, it is a stateless operation.

The best of stateless operations is that they can be easily parallelized for several frames, making the processing faster. In the example, if you provide Pipeless with a stateless pre-processing hook that resizes the image, Pipeless will resize several frames at the same time, thus, resizing a frame number N + 1 does not need to wait for the frame N to be resized.

In a traditional computer vision pipeline, pre-processing and post-processing are the steps taking most of the time, thus, if your use case allows you to implement them in a stateless way, Pipeless will automatically make your application significantly faster.

Stateful hooks

Stateful hooks are the opposite of stateless hooks.

A stateful function is a function whose result does not only depend on the inputs, but also in previous calls to the function.

A direct example of when you want to use stateful hooks in Pipeless is when using object tracking or object-counting models. These models require to maintain an internal state in order to work, thus, a stateless approach is not suitable for them.

In Pipeless, you can easily convert your hook into a stateful hook by simply adding a comment at the top of your code file when using a programming language or adding a field when using JSON hooks.

Stateful hooks come with a drawback, they offer a reduced performance compared to stateless hooks. This difference comes from the fact that, to avoid corrupting the internal state, stateful hooks cannot be executed for several frames at the same time, thus, a stateful hook guarantees that it won’t be executed for a frame N + 1 until it finishes for frame N.

Conclusion

With Pipeless you can create computer vision applications very easily, whether they need to maintain an internal state or not. Indeed, you will write the code in the exact same way in both cases, you just need to indicate to Pipeless which function should be stateful and it will handle everything for you, allowing you to be focused on your application and not on the low-level details.

You can find more information about stateless and stateful hooks in the Pipeless docs.

If you like what we are doing at Pipeless consider sharing it and starring the GitHub repository.

We also invite you to join the community and contribute to the project!

GitHub logo pipeless-ai / pipeless

An open-source computer vision framework to build and deploy apps in minutes without worrying about multimedia pipelines

Pipeless

Easily create, deploy and run computer vision applications.


Loading video...

Pipeless is an open-source computer vision framework to create and deploy applications without the complexity of building and maintaining multimedia pipelines. It ships everything you need to create and deploy efficient computer vision applications that work in real-time in just minutes.

Pipeless is inspired by modern serverless technologies. It provides the development experience of serverless frameworks applied to computer vision. You provide some functions that are executed for new video frames and Pipeless takes care of everything else.

You can easily use industry-standard models, such as YOLO, or load your custom model in one of the supported inference runtimes. Pipeless ships some of the most popular inference runtimes, such as the ONNX Runtime, allowing you to run inference with high performance on CPU or GPU out-of-the-box.

You can deploy your Pipeless application to edge and IoT devices or the cloud. We…




Top comments (0)