Semantic and instance segmentation using pretrained napari-convpaint models. This processing function enables batch inference with custom-trained convpaint models for TZYX time-series data, processing each timepoint independently.
Convpaint uses feature extractors (like DINO, VGG, Gaussian filters) to segment images based on annotations. This function allows you to apply your pretrained models to new datasets at scale.
- Pretrained Model Support: Load custom
.pklmodel checkpoints - Batch Processing: Process entire folders of images automatically
- Time Series Support: Handles TZYX data by processing each timepoint independently
- Semantic & Instance Output: Choose between semantic classes or instance labels
- Memory Optimization: Configurable downsampling to reduce GPU memory usage
- CPU Fallback: Automatic or manual CPU execution for GPU compatibility issues
- Background Removal: Configurable background label removal
- Automatic Environment Management: Creates dedicated environment if convpaint not installed
The plugin automatically creates a dedicated convpaint-env environment on first use. No manual installation required.
If you want napari-convpaint in your main environment:
pip install napari-convpaintPath to the pretrained convpaint model (.pkl file).
Example: /path/to/your/model.pkl
How to train a model: Use napari-convpaint to train models on annotated data. See napari-convpaint documentation for training instructions.
Downsampling factor for processing. Output is automatically upsampled to match input dimensions.
Recommendations:
1: No downsampling (highest quality, most memory)2: 2x downsampling (recommended for most cases)4: 4x downsampling (for very large images or limited GPU memory)8: 8x downsampling (extreme memory constraints)
Memory impact: Higher downsampling reduces memory usage quadratically (2x = 4× less memory, 4x = 16× less memory).
Type of output labels.
Options:
-
"semantic": Each class has the same label value- Example: All class 1 objects = 1, all class 2 objects = 2
- Use for: Classification maps, class-based analysis
-
"instance": Each connected component gets a unique label- Example: Object 1 = 1, Object 2 = 2, Object 3 = 3 (even if same class)
- Use for: Counting objects, tracking, individual object analysis
- Processing: 3D volumes use 3D connected components, time series process per-timepoint
Label ID representing the background class in the model output.
All pixels with this label value will be set to 0 (standard background) in the output.
Common values:
1: Default (most convpaint models use 1 for background)0: If your model already uses 0 for background- Other: If your model uses a different class ID for background
Force CPU execution even if GPU is available.
When to enable:
- GPU not compatible with PyTorch (e.g., very new GPUs like RTX 5000 series)
- Out of GPU memory errors
- Testing/debugging
Note: CPU execution is significantly slower but works on any hardware.
Force using the dedicated convpaint environment even if napari-convpaint is installed in the main environment.
Process 2D or 3D images with a pretrained model:
# Parameters:
# - model_path: /home/user/models/cell_segmentation.pkl
# - image_downsample: 2
# - output_type: semantic
# - background_label: 1
# Input: sample.tif (2048×2048 pixels)
# Output: sample_convpaint_labels.tif (semantic classes)Get individual cell labels for counting:
# Parameters:
# - model_path: /home/user/models/nuclei_model.pkl
# - image_downsample: 2
# - output_type: instance
# - background_label: 1
# Input: nuclei.tif (YX)
# Output: nuclei_convpaint_labels.tif (each nucleus has unique ID)Process 4D time series (each timepoint processed independently):
# Parameters:
# - model_path: /home/user/models/organoid_3d.pkl
# - image_downsample: 2
# - output_type: instance
# - background_label: 1
# Input: timelapse.tif (20 timepoints × 50 Z-slices × 1024×1024)
# Output: timelapse_convpaint_labels.tif (TZYX with instance labels per timepoint)Use higher downsampling for very large images:
# Parameters:
# - model_path: /home/user/models/tissue.pkl
# - image_downsample: 4
# - output_type: semantic
# - background_label: 1
# - use_cpu: True (if GPU runs out of memory)
# Input: whole_slide.tif (10000×10000 pixels)
# Output: whole_slide_convpaint_labels.tif| Format | Description | Example Shape |
|---|---|---|
| YX | 2D image | (1024, 1024) |
| ZYX | 3D volume (Z-stack) | (50, 512, 512) |
| TYX | 2D time series | (100, 512, 512) |
| TZYX | 3D time series | (20, 50, 512, 512) |
The function automatically detects dimension order using these heuristics:
- 3D data: First dimension < 100 → ZYX (3D volume)
- 3D data: First dimension ≥ 100 → TYX (2D time series)
- 4D data: Always treated as TZYX
Output files are saved in the same directory as input with suffix _convpaint_labels.tif:
Input: my_image.tif
Output: my_image_convpaint_labels.tif
Output format:
- Data type:
uint32(supports up to 4 billion unique labels) - Compression: zlib
- Metadata: Preserved from input when possible
- Background: Always 0 (after background_label removal)
1. File Selection → Select folder with images
2. Convpaint Prediction → Apply pretrained model
- Set model_path to your .pkl file
- Choose output_type (semantic or instance)
- Adjust image_downsample if needed
3. (Optional) Post-processing:
- Remove small labels
- Apply morphological operations
4. Quantification → Extract regionprops
5. Analysis → Colocalization, tracking, etc.
-
Training (in napari-convpaint):
- Load training images
- Create annotations (paint labels)
- Train model with desired feature extractor
- Save model as
.pklfile
-
Batch Inference (in napari-tmidas):
- Load test images in batch processing
- Select "Convpaint Prediction"
- Point to trained
.pklmodel - Process entire folder
- 2D (YX): Direct processing
- 3D (ZYX): Processed as 3D volume (not slice-by-slice)
- Time Series (TYX/TZYX): Each timepoint processed independently
- Prevents temporal artifacts
- Allows parallel processing
- Matches other AI method behavior (Cellpose, CAREamics)
When output_type="instance":
- Process semantic segmentation normally
- Apply connected components analysis:
- 2D: 8-connected (3×3 neighborhood)
- 3D: 26-connected (3×3×3 neighborhood)
- Each class processed separately to avoid label conflicts
- Unique labels assigned across all classes
The function automatically manages a dedicated Python environment:
Environment location: ~/.napari-tmidas/envs/convpaint/
First use:
- Detects if napari-convpaint is available
- If not, creates dedicated environment
- Installs: napari-convpaint, PyTorch (with CUDA if available), dependencies
Subsequent uses:
- Reuses existing environment
- No reinstallation needed
Manual environment recreation:
from napari_tmidas.processing_functions.convpaint_env_manager import recreate_convpaint_env
recreate_convpaint_env()Problem: CUDA error: no kernel image is available for execution
Solution: Enable CPU mode
- Set
use_cpu=Truein parameters - GPU is too new for PyTorch version
Problem: GPU runs out of memory during processing
Solutions:
- Increase
image_downsample(try 4 or 8) - Enable
use_cpu=True(slower but no memory limit) - Process smaller images or crops
Problem: "Model file not found" or loading errors
Checklist:
- Verify
.pklfile exists at specified path - Check file permissions
- Ensure model was saved with compatible napari-convpaint version
- Try absolute path instead of relative path
Problem: Background pixels not set to 0
Solution: Adjust background_label parameter
- Check your model's class definitions
- Use napari to inspect output and identify background class ID
- Set
background_labelto that ID
Problem: Processing takes very long
Optimizations:
- Ensure GPU is being used (
use_cpu=False) - Increase
image_downsamplefor faster processing - Check GPU utilization:
nvidia-smi - Close other GPU-using applications
- Use GPU: 10-100× faster than CPU
- Increase downsampling: 2× → 4× gives ~4× speedup
- Process in batches: Use batch processing widget
- Semantic output: Faster than instance (no connected components)
- Reduce image size: Crop or bin images before processing
- Increase downsampling: Reduces memory quadratically
- Process timepoints sequentially: Automatic for TZYX
- Use CPU: Unlimited memory (but slower)
| Downsample | Speed | Memory | Quality |
|---|---|---|---|
| 1× | 1× (baseline) | High | Best |
| 2× | ~4× faster | 4× less | Good |
| 4× | ~16× faster | 16× less | Fair |
| 8× | ~64× faster | 64× less | Rough |
To train custom convpaint models, see:
Training tips:
- Use diverse training data representative of your test images
- Choose appropriate feature extractor (DINO, VGG, Gaussian)
- Test on held-out validation data
- Save models with descriptive names
If you use convpaint in your research, please cite:
Wigger, G. E. (2024). napari-convpaint: Interactive pixel classification with feature extractors.
GitHub. https://github.com/guiwitz/napari-convpaint
- Cellpose Segmentation - Deep learning instance segmentation
- CAREamics Denoising - AI-based denoising
- Semantic to Instance - Post-processing conversion
- Batch Processing Guide - General batch processing workflow