Skip to main content

Retinalysis models inference

This repository implements inference ensembles for retinalysis model releases. It includes the same pre-processing code that was used to train the models. For example, fundus preprocessing will detect bounds, crop the smalles square that contains these bounds, and resize it to a fixed resolution, currently 1024x1024px.

Models have been tested to run on a single nvidia GPU with at least 10GB VRAM. Using them for distributed inference in multiple GPUs will require some adaptation.

Inference contracts

Inference has two batch methods because they serve different hardware and consumers:

  • predict_step({"image": tensor}) -> Tensor is the GPU primitive. It returns the decoded, member-aggregated N... tensor without a CUDA synchronization, CPU refinement, resizing, or file I/O.
  • predict_step_full(batch) -> dict is the inspection primitive. It returns a Pydantic-validated plain dictionary containing per-member NumPy values in NM... order, the aggregate in N... order, intermediates such as averaged logits, and canonical preprocessing context when the batch came from a package dataloader.

The two methods intentionally expose different arrays: full["prediction"] retains the member axis, while full["aggregate"] is the NumPy representation of predict_step.

Two higher-level methods handle datasets:

  • predict_preprocessed(data, dest_path=None, ...) accepts images already in canonical fundusprep space.
  • predict_dataframe(df, ..., preprocess=True) accepts a table and may preprocess raw inputs first.

High-level spatial outputs always end in canonical preprocessed geometry, normally 1024×1024. The package does not convert results back to the original photograph geometry. predict_step and raw arrays in predict_step_full remain in model prediction geometry; call postprocess_item on a decollated full item to run family-specific CPU refinement and resize-only canonical restoration.

predict and predict_dataset remain undocumented raw-input compatibility entrypoints. _predict_batch remains temporarily available as a prediction-geometry adapter and emits FutureWarning.

Family prediction aggregate Full-output intermediates
Regression/classification NMC NC —
Segmentation NMHWC member logits NHWC softmax averaged logits: NHWC
Overlap/LUNet NMHWC member logits NHWC sigmoid averaged logits: NHWC
Embedding NMD ND —
Keypoints NMK2 NK2 —
Heatmap NMK2 NK2 optional heatmaps: NMKHW
Halo artery/vein NMHW4 member logits NHW4 probabilities averaged logits: NHW4, exact four names and runtime refinement configuration

Configuring inference

Runtime settings from config.inference can be overridden when constructing an ensemble. Constructor values win over the release defaults. Nested mappings such as graph_refinement are merged field by field.

ensemble = make_ensemble(
    release_path,
    tta=False,
    overlap=0.25,
    tile_batch_size=8,
    graph_refinement={"mode": "full", "direction_cost_weight": 0.0},
)
  • batch_size is the image/dataloader batch size.
  • tile_batch_size is the sliding-window tile micro-batch. Halo models still fall back to embedded inference.batch_size when tile_batch_size is omitted.
  • New ensembles must read these settings through _inference_setting, _inference_mapping, and _tile_batch_size, not from self.config["inference"] directly.

For artery/vein models, embedded inference.graph_refinement is the default. refinement_mode and refinement_parameters remain supported aliases and take precedence over graph_refinement. Passing refinement_mode=None still selects basic per-pixel decoding.

In full mode, configurable-radius junction regions expose segment ports. The optimizer scores continuation, bifurcation, overlap, and cut configurations from spline direction, thickness, vessel/crossing logits, and A/V compatibility. A mixed-integer program jointly assigns segment classes and configurations while forcing the artery and vein graphs to be acyclic rooted forests. When disc_mask and bounds_mask are supplied to infer_artery_vein, disc pixels are removed and roots are prioritized as disc contact, fundus-boundary fallback, then internal fallback. The inward fundus boundary width is configurable with bounds_boundary_width. Short components below min_segment_size are retained as microsegments when they connect at least two junction boundaries. Microsegments use their endpoint chord instead of a spline, cannot become roots, and must connect at every port when active. Individual evidence costs and the disc, bounds, internal, distance, and thickness root costs can be ablated independently by setting their corresponding weights or penalties to zero.

Do not add logit-specific step or bulk aliases. Read logits from predict_step_full(batch)["logits"]; a bulk export is a short dataloader loop that restores each array to canonical geometry before writing it.

Installation

  1. Install torch and torchvision that match your cuda environment. For example:
pip3 install torch torchvision torchaudio  # pip and CUDA 12
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia # conda and CUDA 12
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 # pip and CUDA 11
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia # conda and CUDA 11

Pytorch installation instructions are here.

retinalysis-inference declares torch>=2.4.1,<3 and torchvision>=0.19,<1. Install the CUDA build that matches your machine first so pip reuses it; a bare install still pulls a default PyTorch from PyPI.

  1. Install rtnls_fundusprep and rtnls-inference:
pip install retinalysis-fundusprep
pip install retinalysis-inference
  1. Done! You can now download and use rtnls-inference ensembles. See this notebook for an example. The models are automatically downloaded from huggingface.

Metadata

Release files for retinalysis-inference 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for retinalysis-inference 0.9.0
File Size Uploaded
retinalysis_inference-0.9.0.tar.gz 10.2 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for retinalysis-inference 0.9.0
File Interpreter ABI Platform
retinalysis_inference-0.9.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.3 MB

Release files / retinalysis_inference-0.9.0.tar.gz

Download URL retinalysis_inference-0.9.0.tar.gz
Size 10.2 MB
Tags Source
SHA-256 checksum
How to use checksums
10c3e61fb1107ea8bc92f6f353b2618120c221b8d63fa9023f6b61ff877be354
BLAKE2b-256 checksum
How to use checksums
cbf4881679abd2f84fc4462b5a306b8796499ee37298f0e77ea9da0702062ba1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release files / retinalysis_inference-0.9.0-py3-none-any.whl

Download URL retinalysis_inference-0.9.0-py3-none-any.whl
Size 65.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
55168d2ad3592228e602d25ada4cd506db56622c47f9005ccb758b785326abaf
BLAKE2b-256 checksum
How to use checksums
49b2182cacb800fb4c6374e38413204df8927abb6acab6173ba00f8d5c675583
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release history Release notifications | RSS feed

This release

0.9.0 This release

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page