Skip to main content

Deploy and access image and data processing models across processes.

Project description

Nahual: Communication layer to send and transform data across environments and/or processes.

The problem: When trying to train, compare and deploy many different models (deep learning or otherwise), the number of dependencies in one Python environment can get out of control very quickly (e.g., one model requires PyTorch 2.1 and another one 2.7).

Potential solution: I figured that if we can move parameters and numpy arrays between environments, we can isolate each model and having them process our data on-demand.

Thus the goal of this tool is provide a way to deploy model(s) in one (or many) environments, and access them from another one, usually an orchestrator.

Available models and tools

I deployed tools using Nix.

  • BABY: Segmentation, tracking and lineage assignment for budding yeast.
  • Cellpose: Generalist segmentation model.
  • DINOv2: Generalist self-supervised model to obtain visual features.
  • Trackastra: Transformer-based tracking trained on a multitude of datasets.
  • ViT: HuggingFace's Visual Transformers model. OpenPhenom, MorphEM.
  • SubCell: Encoder of single cell morphology and protein localisation.
  • DINOv3: Generalist self-supervised model, latest iteration.

Future supported tools

Usage

Step 1: Deploy server

cd to the model you want to deploy. In this case we will test the image embedding model DINOv2.

git clone https://github.com/afermg/dinov2.git
cd dinov2
nix develop --command bash -c "python server.py ipc:///tmp/dinov2.ipc"

Step 2: Run client

Once the server is running, you can call it from a different python script.

import numpy

from nahual.process import dispatch_setup_process

setup, process = dispatch_setup_process("dinov2")
address = "ipc:///tmp/dinov2.ipc"

# %%Load models server-side
parameters = {"repo_or_dir": "facebookresearch/dinov2", "model": "dinov2_vits14_lc"}
response = setup(parameters, address=address)

# %% Define custom data
data = numpy.random.random_sample((1, 3, 420, 420))
result = process(data + 1000, address=address)

You can press C-c C-c from the terminal where the server lives to kill it. We will also add a way to kill the server from within the client.

Design decisions and details

I strive to be as lean as possible (both in dependency count and architectural complexity), it is designed around three layers:

  • Server deployment: A collection of functions/tool (we could even call it a "model zoo" if we are trying to sound cool) that we may want to use, (e.g., Cellpose for object segmentation or Trackastra for tracking).
  • Transport layer: We need to move the data between environments. I also wrote my own (trivially simple) numpy serializer. Since we have Python at both ends of the connection, we can reuse these functions server-side.
  • Orchestration: This can be a script, or my own pipelining framework aliby, massages the data into the desired shape/type, and then hands it over to nahual.

This tool is my personal one-stop-shop source for multiple models to process imaging data or their derivatives. Please note that this is work in progress, and very likely to undergo major changes as I develop a better understanding of the main challenges.

To reduce maintenance burden, we support only the necessary data types:

  • Dictionaries: To send parameters to deploy and evaluate models/functions.
  • Numpy arrays (and numpy-able lists/tuples): The main type of data we deal with.

Tech stack

  • Model/tool deployment I use Nix, and at the moment do not plan to support containers. The logic behind gives me unique guarantees of reproducibility, whilst allowing me to use bleeding edge models and libraries.
  • Transport layer I use pynng, I like that it is very minimalistic and provides easy-to-reproduce examples. An alternative would have been gRPC + protobuf, but since I am trying to understand the constraints and tradeoffs I do not want to commit to a big framework unless I have a compelling reason to do so.

Adding support for new models

Any model requires a thin layer that communicates using nng. You can see an example of trackastra's server and client.

Roadmap

  • Support multiple instances of a model loaded on memory server-side.
  • Formalize supported packet formats: (e.g., numpy arrays, dictionary).
  • Increase number of supported models/methods.
  • Document server-side API.
  • Integrate into the aliby pipelining framework, in a way that is agnostic to which model is being used.
  • Support containers that wrap the Nix derivations.

Why nahual?

In Mesoamerican folklore, a Nahual is a shaman able to transform into different animals.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nahual-0.0.8.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nahual-0.0.8-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file nahual-0.0.8.tar.gz.

File metadata

  • Download URL: nahual-0.0.8.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"25.05","id":"warbler","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for nahual-0.0.8.tar.gz
Algorithm Hash digest
SHA256 230c13f9c5c28d4614af77d2117f3519f6d6037a71125e019ae4515e8b6af500
MD5 3aa1c303c50b0df3a8dab0f56324eb6a
BLAKE2b-256 972957d56ec465965bdae2af0c5bfd79601daf3aa281307c2f2fe0506a2b7b91

See more details on using hashes here.

File details

Details for the file nahual-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: nahual-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"25.05","id":"warbler","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for nahual-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 6522ca546036b68801e72c448bade94bcaa597362ee30e9da3edca95de6decf7
MD5 13b51e1daa2099f91d725fea0c1d32c3
BLAKE2b-256 c4e6653b321115da1a239f333eb7be93b81762f2062fffaf8e2df80c3e0e706c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page