{"name":"napari-czann-segment","display_name":"CZANN Segmentation and Processing","visibility":"public","icon":null,"categories":[],"schema_version":"0.2.1","on_activate":null,"on_deactivate":null,"contributions":{"commands":[{"id":"napari-czann-segment.get_czann_widget","title":"CZANN Segmentation and Processing","python_name":"napari_czann_segment.dock_widget:segment_with_czann","short_title":null,"category":null,"icon":null,"enablement":null}],"readers":null,"writers":null,"widgets":[{"command":"napari-czann-segment.get_czann_widget","display_name":"Segment or Process with CZANN Model","autogenerate":false}],"sample_data":null,"themes":null,"menus":{},"submenus":null,"keybindings":null,"configuration":[]},"package_metadata":{"metadata_version":"2.4","name":"napari-czann-segment","version":"0.1.0","dynamic":["license-file"],"platform":null,"supported_platform":null,"summary":"Semantic Segmentation and Image processing using Deep Learning ONNX models packaged as *.czann files","description":"# napari-czann-segment\n\n[![License](https://img.shields.io/pypi/l/napari-czann-segment.svg?color=green)](https://github.com/sebi06/napari-czann-segment/raw/main/LICENSE)\n[![PyPI](https://img.shields.io/pypi/v/napari-czann-segment.svg?color=green)](https://pypi.org/project/napari-czann-segment)\n[![Python Version](https://img.shields.io/pypi/pyversions/napari-czann-segment.svg?color=green)](https://python.org)\n[![napari hub](https://img.shields.io/endpoint?url=https://api.napari-hub.org/shields/napari-czann-segment)](https://napari-hub.org/plugins/napari-czann-segment)\n\nSemantic Segmentation of multidimensional images using Deep Learning ONNX models packaged as *.czann files.\n\n----------------------------------\n\nThis [napari] plugin was generated with [Cookiecutter] using [@napari]'s [cookiecutter-napari-plugin] template.\n\n![Train on APEER and use model in Napari](https://github.com/sebi06/napari-czann-segment/raw/main/readme_images/Train_APEER_run_Napari_CZANN_no_highlights_small.gif)\n\n## Installation\n\nBefore installing, please setup a conda environment. If you have never worked with conda environments, go through [this tutorial](https://biapol.github.io/blog/johannes_mueller/anaconda_getting_started/) first.\n\nYou can then install `napari-czann-segment` via [pip]. **You must choose either CPU or GPU support**:\n\n**For CPU inference** (works on all platforms):\n\n    pip install napari-czann-segment[cpu]\n\n**For GPU inference** (Windows/Linux with NVIDIA GPU only):\n\n    pip install napari-czann-segment[gpu]\n\nSee [CPU vs. GPU Inference](#cpu-vs-gpu-inference) for detailed GPU setup instructions.\n\n## What does the plugin do\n\nThe plugin allows you to:\n\n- Use a *.czann file containing the Deep Neural Network (ONNX) for semantic segmentation and metadata\n- Segmentation will be applied per 2D plane for all dimensions\n- Processing larger multidimensional images it uses the [cztile] package to chunk the individual 2d arrays using a specific overlap.\n- multidimensional images will be processed plane-by-plane\n\n## What does the plugin NOT do\n\n**Before one can actually use a model it needs to be trained, which is NOT done by this plugin**.\n\nThere are two main ways hwo such a model can be created:\n\n- Train the segmentation model fully automated on [APEER] and download the *.czann file\n- Train your model in a Jupyter notebook etc. and package it using the [czmodel] python package as an *.czann\n\n## Using this plugin\n\n### Sample Data\n\nA test image and a *.czann model file can be downloaded [here](https://github.com/sebi06/napari-czann-segment/tree/main/src/napari_czann_segment/_data).\n\n- `PGC_20X.ome.tiff` --> use `PGC_20X_nucleus_detector.czann` to segment\n\nIn order to use this plugin the user has to do the following things:\n\n- Open the image using \"File - Open Files(s)\" (requires [napari-bioio] plugin).\n- Click **napari-czann-segment: Segment with CZANN model** in the \"Plugins\" menu.\n- **Select a czann file** to use the model for segmentation.\n- metadata of the model will be shown (see example below)\n\n| Parameter    | Value                                        | Explanation                                        |\n| :----------- | :------------------------------------------- | -------------------------------------------------- |\n| model_type   | ModelType.SINGLE_CLASS_SEMANTIC_SEGMENTATION | see: [czmodel] for details                         |\n| input_shape  | [1024, 1024, 1]                              | tile dimensions of model input                     |\n| output_shape | [1024, 1024, 3]                              | tile dimensions of model output                    |\n| model_id     | ba32bc6d-6bc9-4774-8b47-20646c7cb838         | unique GUID for that model                         |\n| min_overlap  | [128, 128]                                   | tile overlap used during training (for this model) |\n| classes      | ['background', 'grains', 'inclusions']       | available classes                                  |\n| model_name   | APEER-trained model                          | name of the model                                  |\n\n![Napari - Image loaded and czann selected](https://github.com/sebi06/napari-czann-segment/raw/main/readme_images/napari_czann1.png)\n\n- Adjust the **minimum overlap** for the tiling (optional, see [cztile] for details).\n- Select the **layer** to be segmented.\n- Toggle **Use GPU for inference** checkbox to enable / disable using a GPU (Nvidia) for the segmentation (experimental feature).\n- Press **Segment Selected Image Layer** to run the segmentation.\n\n![Napari - Image successfully segmented](https://github.com/sebi06/napari-czann-segment/raw/main/readme_images/napari_czann3.png)\n\nA successful is obviously only the starting point for further image analysis steps to extract the desired numbers from the segmented image.\nAnother example is shown below demonstrating a simple \"Grain Size Analysis\" using a deep-learning model trained on [APEER] used in [napari]\n\n![Napari - Simple Grain Size Analysis](https://github.com/sebi06/napari-czann-segment/raw/main/readme_images/grainsize_czann_napari.png)\n\n### Remarks\n\n> **IMPORTANT**: Currently the plugin only supports using models trained on a **single channel** image. Therefore, make sure that during the training on [APEER] or somewhere else the correct inputs images are used.\n> It is quite simple to train a single RGB image, which actually has three channels, load this image in [napari] and notice only then that the model will not work, because the image will 3 channels inside [napari].\n\n## CPU vs. GPU Inference\n\n### CPU (default - works everywhere)\n\nCPU inference via [ONNX-CPU] works on **all platforms** (Windows, Linux, macOS including Apple Silicon):\n\n    pip install napari-czann-segment[cpu]\n\nWhen the plugin starts, it checks GPU availability and logs the result. If no GPU is detected, the \"Use GPU\" checkbox is automatically disabled and all inference runs on CPU.\n\n### macOS\n\nOn macOS (both Intel and Apple Silicon), the plugin works with **CPU inference only**. NVIDIA CUDA is not available on macOS, so the `[gpu]` extra should **not** be installed — `onnxruntime-gpu` does not publish macOS wheels and installation will fail. Simply use:\n\n    pip install napari-czann-segment[cpu]\n\nThe GPU checkbox will be automatically disabled on macOS.\n\n### GPU (optional - Windows/Linux with NVIDIA GPU)\n\nGPU acceleration uses [ONNX-GPU] and requires an **NVIDIA GPU** with the correct CUDA runtime libraries. For the ONNX Runtime versions targeted by this project, the `[gpu]` extra installs pinned pip `nvidia-*-cu12` runtime packages, using a CUDA 12.4 + cuDNN 9.1 stack that still works on older NVIDIA GPUs such as GTX 10xx / Pascal.\n\n**Why the conda environment file is recommended:**\n\nThe conda environment file ([napari-env.yml](napari-env.yml)) already has the **correct versions of CUDA and cuDNN** tested and working with `onnxruntime-gpu`. You don't need to guess which versions to install:\n\n    conda env create --file napari-env.yml\n\nThen verify GPU support:\n\n    python -c \"import onnxruntime; print(onnxruntime.get_available_providers())\"\n\nYou should see `CUDAExecutionProvider` in the list.\n\n**If you must install manually:**\n\nDifferent versions of `onnxruntime-gpu` require different CUDA/cuDNN versions. Check the [ONNX Runtime CUDA Provider documentation](https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html#requirements) for your version's requirements.\n\nFor example, for `onnxruntime-gpu >= 1.21`, you need CUDA 12.x and cuDNN 9.x. The `[gpu]` extra pins tested runtime wheels:\n\n1. **If you previously installed with `[cpu]`, uninstall onnxruntime first**:\n\n       pip uninstall onnxruntime -y\n\n2. **Install the GPU extra**:\n\n       pip install napari-czann-segment[gpu]\n\n3. **Verify GPU support**:\n\n       python -c \"import onnxruntime; print(onnxruntime.get_available_providers())\"\n\n   You should see `CUDAExecutionProvider` in the list. If you only see `CPUExecutionProvider`, the CUDA libraries are missing or incompatible — check the [requirements](https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html#requirements) and reinstall.\n\n4. **(Alternative) Use conda packages** if you prefer not to use pip-provided CUDA runtime wheels:\n\n       conda install nvidia::cuda-runtime nvidia::cudnn\n\n5. **Use the repo environment file** for a tested setup. See the example [conda environment YAML](napari-env.yml):\n\n       conda env create --file napari-env.yml\n\n**Note:** If you have PyTorch with CUDA installed, onnxruntime-gpu (>= 1.21) can automatically reuse PyTorch's CUDA/cuDNN DLLs via its `preload_dlls()` mechanism. The plugin calls this automatically at startup.\n\n### CUDA preflight check\n\nBefore running GPU inference, the plugin performs a one-time **CUDA preflight** per model: it creates a CUDA ONNX session and runs a single dummy inference in a **separate subprocess**. This is intentional — some CUDA/cuDNN/cuBLAS mismatches abort the process from native code (an error Python cannot catch), so running the probe out-of-process lets napari fall back to CPU safely instead of crashing. The result is cached per model for the session.\n\nThe **first** CUDA session on a machine can be slow to initialize (cuDNN/cuBLAS DLL loading, kernel JIT compilation, driver warm-up). If the preflight does not finish within its timeout, the plugin logs a warning and falls back to CPU for that session. If your GPU is healthy but simply slow to warm up, increase the timeout via an environment variable **before launching napari**:\n\n    # Windows (PowerShell) - timeout in seconds\n    $env:NAPARI_CZANN_CUDA_PREFLIGHT_TIMEOUT = \"300\"\n\n    # Linux / macOS\n    export NAPARI_CZANN_CUDA_PREFLIGHT_TIMEOUT=300\n\nThe default is **180 seconds**.\n\n### Troubleshooting GPU\n\n- **Only seeing `CPUExecutionProvider` after installing `[gpu]`**: You likely have both `onnxruntime` and `onnxruntime-gpu` installed (they conflict). **Solution**: Run `pip uninstall onnxruntime -y` to remove the CPU version, keeping only the GPU version.\n- **Switching from `[cpu]` to `[gpu]`**: First uninstall the CPU version: `pip uninstall onnxruntime -y`, then install with `pip install napari-czann-segment[gpu]`.\n- **`libcudnn.so.9`, `cublasLt64_12.dll`, or `cufft64_11.dll` not found**: CUDA runtime libraries are missing. The easiest fix is ensuring you installed with `[gpu]` extra, which includes them. Alternatively, install via conda: `conda install nvidia::cuda-runtime nvidia::cudnn`.\n- **`CUDNN_FE failure 11` with `no kernel image is available for execution on the device`**: The installed pip `nvidia-*-cu12` wheels are probably too new for your GPU generation. Reinstall the local package with the pinned `[gpu]` extra:\n\n       pip install --force-reinstall -e .[gpu]\n\n- **`Failed to allocate memory for requested buffer` during a Conv node**: The GPU ran out of memory during inference. Lower the batch size in the plugin UI; the plugin also retries GPU inference with smaller batches and falls back to CPU if one tile still cannot fit.\n- **Plugin shows \"GPU support is not available\"**: Check the napari log output for detailed diagnostics. The plugin always falls back to CPU safely.\n- **`CUDA preflight ... timed out` warning / GPU falls back to CPU**: The one-time CUDA warm-up exceeded the preflight timeout (default 180 s). If your GPU is healthy, raise it by setting `NAPARI_CZANN_CUDA_PREFLIGHT_TIMEOUT` (seconds) before launching napari — see [CUDA preflight check](#cuda-preflight-check).\n- **CUDA version mismatch**: `onnxruntime-gpu` requires specific CUDA versions. Check the [ONNX Runtime GPU requirements](https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html#requirements).\n\n## For developers\n\n- **Please clone this repository first using your favorite tool.**\n\n- **Ideally one creates a new [conda] environment or use an existing environment that already contains [Napari].**\n\nFeel free to create a new environment using the example [YAML](napari-env.yml) file at your own risk:\n\n    cd the-github-repo-with-YAML-file\n    conda env create --file napari-env.yml\n    conda activate napari-env\n\n- **Install the plugin locally**\n\nPlease run the following command:\n\n    pip install -e .\n\nTo install latest development version:\n\n    pip install git+https://github.com/sebi06/napari_czann_segment.git\n\n### Running tests\n\nRun the automated test suite with:\n\n    pytest\n\nThe `_tests/` directory also contains **manual diagnostic scripts** (`test_color_conversion.py`, `diagnostic_color_test.py`, `debug_tiling.py`) that perform full end-to-end inference on real CZI files and take several minutes. These are automatically **skipped** by `pytest`. To run them manually:\n\n    python src/napari_czann_segment/_tests/test_color_conversion.py\n    python src/napari_czann_segment/_tests/diagnostic_color_test.py\n    python src/napari_czann_segment/_tests/debug_tiling.py\n\n## Contributing\n\nContributions and Feedback are very welcome.\n\n## License\n\nDistributed under the terms of the [BSD-3] license,\n\"napari-czann-segment\" is free and open source software\n\n## Issues\n\nIf you encounter any problems, please [file an issue] along with a detailed description.\n\n[napari]: https://github.com/napari/napari\n[Cookiecutter]: https://github.com/audreyr/cookiecutter\n[@napari]: https://github.com/napari\n[MIT]: http://opensource.org/licenses/MIT\n[BSD-3]: http://opensource.org/licenses/BSD-3-Clause\n[GNU GPL v3.0]: http://www.gnu.org/licenses/gpl-3.0.txt\n[GNU LGPL v3.0]: http://www.gnu.org/licenses/lgpl-3.0.txt\n[Apache Software License 2.0]: http://www.apache.org/licenses/LICENSE-2.0\n[Mozilla Public License 2.0]: https://www.mozilla.org/media/MPL/2.0/index.txt\n[cookiecutter-napari-plugin]: https://github.com/napari/cookiecutter-napari-plugin\n[file an issue]: https://github.com/sebi06/napari-czann-segment/issues\n[tox]: https://tox.readthedocs.io/en/latest/\n[pip]: https://pypi.org/project/pip/\n[PyPI]: https://pypi.org/\n[czmodel]: https://pypi.org/project/czmodel/\n[cztile]: https://pypi.org/project/cztile/\n[APEER]: https://www.apeer.com\n[napari-bioio-reader]: https://github.com/imcf/napari-bioio-reader\n[ONNX-GPU]: https://pypi.org/project/onnxruntime-gpu/\n[ONNX-CPU]: https://pypi.org/project/onnxruntime/\n[conda]: https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html\n[pytorch]: https://pytorch.org/get-started/locally\n","description_content_type":"text/markdown","keywords":null,"home_page":"https://github.com/sebi06/napari-czann-segment","download_url":null,"author":"Sebastian Rhode","author_email":"sebrhode@gmail.com","maintainer":null,"maintainer_email":null,"license":"BSD-3-Clause","classifier":["Development Status :: 3 - Alpha","Framework :: napari","Intended Audience :: Developers","Intended Audience :: Science/Research","License :: OSI Approved :: BSD License","Operating System :: Unix","Operating System :: Microsoft :: Windows","Operating System :: MacOS","Programming Language :: Python","Programming Language :: Python :: 3","Programming Language :: Python :: 3 :: Only","Programming Language :: Python :: 3.11","Programming Language :: Python :: 3.12","Programming Language :: Python :: 3.13","Topic :: Scientific/Engineering :: Image Processing"],"requires_dist":["napari>=0.7.0","cztile>=2","czmodel>=6","tiler","ryomen","onnxruntime>=1.19; extra == \"cpu\"","onnxruntime-gpu[cuda,cudnn]<1.27,>=1.19; extra == \"gpu\"","nvidia-cuda-runtime-cu12==12.4.127; extra == \"gpu\"","nvidia-cuda-nvrtc-cu12==12.4.127; extra == \"gpu\"","nvidia-cublas-cu12==12.4.5.8; extra == \"gpu\"","nvidia-cudnn-cu12==9.1.0.70; extra == \"gpu\"","nvidia-cufft-cu12==11.2.1.3; extra == \"gpu\"","nvidia-curand-cu12==10.3.5.147; extra == \"gpu\"","nvidia-nvjitlink-cu12==12.4.127; extra == \"gpu\"","tox; extra == \"testing\"","pytest; extra == \"testing\"","pytest-cov; extra == \"testing\"","pytest-qt; extra == \"testing\"","napari; extra == \"testing\"","pyqt5; extra == \"testing\"","bioio; extra == \"testing\"","bioio-ome-tiff; extra == \"testing\"","bioio-imageio; extra == \"testing\"","tiler; extra == \"testing\"","ryomen; extra == \"testing\"","onnxruntime>=1.19; extra == \"testing\""],"requires_python":">=3.11","requires_external":null,"project_url":["Bug Tracker, https://github.com/sebi06/napari-czann-segment/issues","Documentation, https://github.com/sebi06/napari-czann-segment#README.md","Source Code, https://github.com/sebi06/napari-czann-segment","User Support, https://github.com/sebi06/napari-czann-segment/issues"],"provides_extra":["cpu","gpu","testing"],"provides_dist":null,"obsoletes_dist":null},"npe1_shim":false}