For the complete documentation index, see llms.txt. This page is also available as Markdown.

Add your own node

Fork ML Pipelines, add nodes with your own Python code and packages, and run them as a private app

Introduction

ML Pipelines builds data pipelines from nodes. When the nodes it ships with are not enough, for example because a step needs your own algorithm or your company's private Python packages, you can add your own nodes:

  1. Fork the app's repository: supervisely-ecosystem/data-nodes.

  2. Put your nodes in the src/custom_nodes/ folder of the fork. They appear in the Custom group of the app.

  3. Put your packages into the fork's Docker image.

  4. Release the fork as a private app and run it on any agent.

Your code lives in your fork and runs in your app's container. Nothing is typed into the UI.

This tutorial builds one complete video pipeline, one node per step:

Node
What it does

Select Videos by Tag

keeps the videos that have a tag

Take Subset

keeps a random share of them

Scan Frames

goes over all frames, resizes them and scores each one with OpenCV, several videos at a time

Pick Frame Range

picks the frame ranges with high scores

Cut Clips

cuts a clip for every range, with its annotation

The full pipeline is Videos Project β†’ Select Videos by Tag β†’ Take Subset β†’ Scan Frames β†’ Pick Frame Range β†’ Cut Clips β†’ Create New Project. Copy the nodes and replace their logic with yours.

How a node is made

A node is two classes:

  • a compute class, a subclass of Layer from src/compute/Layer.py. It processes the data;

  • a UI class, a subclass of Action from src/ui/dtl/Action.py. It draws the node and its settings.

Both are registered automatically when they are in src/custom_nodes/: you do not edit any file of the app. That is what keeps the fork easy to update (see Keep the fork up to date).

Give every node its own folder. A folder needs an empty __init__.py; the file names inside are up to you:

The two classes are linked by name: Action.name must be the same as Layer.action. Nodes are listed in the Custom group in the alphabetical order of their folders.

The compute class

Member
Meaning

action

the node's id, the same as name of the UI class

layer_settings

JSON schema of the node's settings; the app validates the settings against it before the run

process(data_el)

handles one item. Yields what goes on, see below

process_batch(data_els) + has_batch_processing() returning True

handles a list of items at once. Yields one list with everything that goes on

requires_item()

True if the node reads the media file. Default False

modifies_data()

True if the node changes the media or creates new files. Default False

video_batch_size()

how many videos the node wants per process_batch call. Default 1, see Run videos in parallel

self.settings

the node's settings, as returned by the UI class

An item is a pair (vid_desc, ann):

  • vid_desc.item_data is the local path of the video file. The video is downloaded the first time a node reads item_data, so videos that never reach such a node are never downloaded;

  • vid_desc.info.item_info is the video's VideoInfo: id, name, frames_count, frames_to_timecodes and so on;

  • ann is the video's VideoAnnotation.

What the node yields decides where the item goes:

  • (vid_desc, ann) sends it to the node's only output;

  • (vid_desc, ann, i) sends it to output number i (from 0) of a node with several outputs. To drop items, give the node a second output and leave it unconnected.

Every item must go to some output: a node must not yield nothing at all.

To pass a value to the next nodes, set it as an attribute of the descriptor: vid_desc.frame_scores = scores. The descriptor travels with the video through the pipeline.

The UI class

Member
Meaning

name

the node's id, the same as action of the compute class

title, description

shown in the sidebar and on the node

md_description

documentation of the node, usually read from the README.md next to it

modalities

["videos"], ["images"] or both. Without it, a VideoAction subclass is a video node and anything else is an image node

create_new_layer()

creates the node's settings widgets and returns a UI Layer

create_outputs()

the node's outputs. One output by default

The parent class sets the node's color and icon: VideoAction, FilterAndConditionAction, OtherAction and so on. The settings are ordinary widgets. In create_new_layer() you write two functions:

  • get_settings(options_json) returns the settings as a dict: this is self.settings of the compute class;

  • create_options(src, dst, settings) puts the saved settings into the widgets (when a pipeline is loaded) and returns the widgets to show.

Step 1. Fork and run locally

Fork supervisely-ecosystem/data-nodes on GitHub, or copy it to your own git server, then:

Create local.env in the root of the repository:

The app takes the server address and your token from ~/supervisely.env, see Basics of authentication. Start the app and open http://localhost:8000:

The app runs on your computer and works with the data on your instance. Restart it after you change a node. The repository also has a VS Code launch configuration, Uvicorn, if you prefer to debug.

Clips are cut with ffmpeg, so install it locally to run the Cut Clips node outside the app's container.

Step 2. Select Videos by Tag

The simplest node: no media, one setting, two outputs. Videos with the tag go to Selected, the rest to Other.

ML Pipelines already has Filter Videos by Tags, with tag values and more options. This node is here as the smallest complete example.

src/custom_nodes/select_videos_by_tag/compute.py:

src/custom_nodes/select_videos_by_tag/ui.py:

Add an empty src/custom_nodes/select_videos_by_tag/__init__.py and, optionally, a README.md. Restart the app: Select Videos by Tag is in the Custom group.

Step 3. Take Subset

Keeps about the given percent of the videos. Which videos are kept depends only on the video and the seed, not on the order of the videos, so a rerun keeps the same ones.

src/custom_nodes/take_subset/compute.py:

src/custom_nodes/take_subset/ui.py:

Step 4. Scan Frames: go over all frames, in parallel

This node reads every frame of every video, resizes it and scores it with OpenCV. The score here is motion: the mean difference from the previous frame. The scores are passed to the next node as vid_desc.frame_scores.

Reading frames is slow, so the node processes several videos at the same time, one per worker process. Keep the per-video function in its own module that imports only what it needs: every worker process imports it.

src/custom_nodes/scan_frames/scoring.py:

src/custom_nodes/scan_frames/compute.py:

What matters here:

  • requires_item() returns True: the node reads the files;

  • video_batch_size() asks for as many videos per batch as there are workers;

  • reading item_data downloads the video, so the node first reads it for the whole batch in a thread pool: the downloads run at the same time;

  • the worker processes use the spawn start method. The app runs threads, and fork in a process with threads can hang;

  • each video's error is caught separately: a broken video goes to the Failed output, the others go on.

src/custom_nodes/scan_frames/ui.py:

Step 5. Pick Frame Range

Uses the scores from Scan Frames: keeps the runs of frames with a score at or above the threshold that are at least the shortest length, widened by the padding. The ranges are inclusive (start, end) frame indexes, passed on as vid_desc.frame_ranges.

src/custom_nodes/pick_frame_range/compute.py:

src/custom_nodes/pick_frame_range/ui.py:

Step 6. Cut Clips

Cuts a clip for every frame range with the app's helper, cut_clips from src/compute/utils/video_clips.py:

  • ranges are inclusive (start, end) frame ranges. Each gives one clip, named <video name>_<n>.mp4;

  • the clips are re-encoded (H.264), so the cut is frame exact, whatever the keyframes are;

  • each Clip has path, name, info (its VideoInfo) and ann: the figures and the frame range tags inside the range, re-indexed so that the clip's first frame is frame 0. Video tags are kept.

clip_to_item(vid_desc, clip) turns a clip into an item for the next nodes. Connect Clips to Create New Project or Add to Existing Project to save them: the clips go to a dataset with the same name as the dataset of their video.

src/custom_nodes/cut_clips/compute.py:

ffmpeg runs as its own process, so threads are enough to cut several videos at the same time. threads splits the CPU cores between the videos.

src/custom_nodes/cut_clips/ui.py:

Step 7. The full pipeline

Connect the nodes in the app: Videos Project β†’ Select Videos by Tag (Selected) β†’ Take Subset (Subset) β†’ Scan Frames (Scanned) β†’ Pick Frame Range (Picked) β†’ Cut Clips (Clips) β†’ Create New Project. Leave the other outputs unconnected.

You can also load it from a file. Save this as /data-nodes/presets/videos/clips.json in Team Files, replace my-videos with the name of your videos project and tagged with your tag, and open it with LOAD in the app:

The result is a new project with the clips. Every clip keeps its annotation, re-indexed to the clip.

Run videos in parallel

  • Videos go through the pipeline in batches. A batch has 1 video unless a node asks for more with video_batch_size(); the pipeline uses the largest value among its nodes. A pipeline that does not read or change any media file uses large batches anyway.

  • Batches are made per dataset, before any node runs. If a filter before your node drops most of the videos, your node gets smaller batches.

  • Use processes (ProcessPoolExecutor) for Python and OpenCV work, threads for downloads and for external programs like ffmpeg.

  • Every video in a batch is on disk at the same time. Choose the batch size with the agent's disk and memory in mind.

  • Nodes without process_batch get the items of a batch one by one.

Step 8. Your own packages

Install your packages into the fork's Docker image. Start from the app's image, supervisely/data-nodes:<version>: take the version from docker_image in the app's config.json. Its Python is 3.12.

For example, if a private package acme-video-signals gives the frame score, scoring.py becomes:

Add docker/custom.Dockerfile to the fork:

  • The app's packages are in /opt/venv. The image has no pip there, so ensurepip adds it first.

  • Keep numpy<2 in the same command: the app uses imgaug, which does not work with NumPy 2. Without the pin, one of your packages can upgrade NumPy and break the app.

  • Your packages must support Python 3.12.

Build the image with your private index. For example, with AWS CodeArtifact (a repository with PyPI as its upstream also serves public packages):

The token is used only by the build: it is not in the image, its layers or its history. The agents that run the app need access to your registry: run docker login registry.example.com on their machines.

Then set the image in the fork's config.json, and give the app its own name:

To try the image before you release, run the app in it locally against your instance, with the fork mounted into the container.

Step 9. Release the fork as a private app

Release the fork with the Supervisely CLI from the root of the repository, as described in Add private app:

The app appears in the private apps of the Ecosystem. Run it like ML Pipelines, on any agent: for example a separate machine or an on-demand worker for the heavy video work. More about config.json is in config.json.

Keep the fork up to date

Your nodes are new files in src/custom_nodes/, so taking our updates is a clean rebase:

The only file you share with us is config.json: when we change its docker_image, keep yours, and change the FROM line of your Dockerfile to our new version.

Last updated