Stateless vs Stateful hooks in your computer vision applications with Pipeless
Tags: computervision, serverless, python, opensource
Published: 2023-11-30 15:37:19


[Pipeless](https://www.pipeless.ai) 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](https://www.pipeless.ai/docs/docs/v1/getting-started#stateless-vs-stateful-hooks).

{% cta https://github.com/pipeless-ai/pipeless %}
If you like what we are doing at Pipeless consider sharing it and starring the GitHub repository.
{% endcta %}

We also invite you to [join the community](https://discord.gg/K2qxQ8uedG) and contribute to the project!

{% embed https://github.com/pipeless-ai/pipeless %}