Label Anything is a novel method for multi-class few-shot semantic segmentation using visual prompts. This repository contains the official implementation of our ECAI 2025 paper, enabling precise segmentation with just a few prompted examples.
Experience Label Anything instantly with our streamlined demo:
uvx --from git+https://github.com/pasqualedem/LabelAnything app๐ก Pro Tip: This command uses uv for lightning-fast package management and execution.
For development and customization:
# Clone the repository
git clone https://github.com/pasqualedem/LabelAnything.git
cd LabelAnything
# Create virtual environment with uv
uv sync
source .venv/bin/activate
โ ๏ธ System Requirements: Linux environment with CUDA 12.1 support
Access our collection of state-of-the-art checkpoints:
| ๐ง Encoder | ๐ Embedding Size | ๐ผ๏ธ Image Size | ๐ Fold | ๐ Checkpoint |
|---|---|---|---|---|
| SAM | 512 | 1024 | - | |
| ViT-MAE | 256 | 480 | - | |
| ViT-MAE | 256 | 480 | 0 | |
| ViT-MAE | 256 | 480 | 1 | |
| ViT-MAE | 256 | 480 | 2 | |
| ViT-MAE | 256 | 480 | 3 |
from label_anything.models import LabelAnything
# Load pre-trained model
model = LabelAnything.from_pretrained("pasqualedem/label_anything_sam_1024_coco")Prepare the COCO dataset with our automated setup:
# Navigate to data directory
cd data && mkdir coco && cd coco
# Download COCO 2017 images and 2014 annotations
wget http://images.cocodataset.org/zips/train2017.zip
wget http://images.cocodataset.org/zips/val2017.zip
wget http://images.cocodataset.org/annotations/annotations_trainval2014.zip
# Extract and organize
unzip "*.zip" && rm *.zip
mv val2017/* train2017/ && mv train2017 train_val_2017 && rm -rf val2017Synchronize filenames between images and annotations:
python main.py rename_coco20i_json --instances_path data/coco/annotations/instances_train2014.json
python main.py rename_coco20i_json --instances_path data/coco/annotations/instances_val2014.json# Download SAM checkpoint
cd checkpoints
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth
# Extract embeddings (optional but recommended for speed)
mkdir -p data/coco/vit_sam_embeddings/{last_hidden_state,last_block_state}
python main.py generate_embeddings \
--encoder vit_b \
--checkpoint checkpoints/sam_vit_b_01ec64.pth \
--use_sam_checkpoint \
--directory data/coco/train_val_2017 \
--batch_size 16 \
--num_workers 8 \
--outfolder data/coco/vit_sam_embeddings/last_hidden_state \
--last_block_dir data/coco/vit_sam_embeddings/last_block_state \
--custom_preprocess# Base ViT-MAE (1024px)
python main.py generate_embeddings \
--encoder vit_b_mae \
--directory data/coco/train_val_2017 \
--batch_size 32 \
--outfolder data/coco/embeddings_vit_mae_1024/ \
--model_name facebook/vit-mae-base \
--image_resolution 1024 \
--huggingface
# Base ViT-MAE (480px)
python main.py generate_embeddings \
--encoder vit_b_mae \
--directory data/coco/train_val_2017 \
--batch_size 64 \
--outfolder data/coco/embeddings_vit_mae_480 \
--model_name facebook/vit-mae-base \
--image_resolution 480 \
--huggingface
# Large ViT-MAE (480px)
python main.py generate_embeddings \
--encoder vit_l_mae \
--directory data/coco/train_val_2017 \
--batch_size 64 \
--outfolder data/coco/embeddings_vit_mae_l_480 \
--model_name facebook/vit-mae-large \
--image_resolution 480 \
--huggingface
# DinoV3
python main.py generate_embeddings \
--directory data/coco/train_val_2017 \
--batch_size 64 \
--outfolder data/coco/embeddings_dinov3_480 \
--model_name facebook/dinov3-vitb16-pretrain-lvd1689m \
--image_resolution 480 \
--huggingface# Train with pre-extracted embeddings
python main.py experiment --parameters="parameters/trainval/coco20i/mae.yaml"
# Train without pre-extracted embeddings
python main.py experiment --parameters="parameters/trainval/coco20i/mae_noembs.yaml"# Accelerated training for faster convergence
accelerate launch --multi_gpu main.py experiment --parameters="parameters/trainval/coco20i/mae.yaml"๐ Experiment Tracking: All experiments are automatically logged to Weights & Biases. Results are saved in
offline/wandb/run-<date>-<run_id>/files/.
๐ฆ LabelAnything
๐ Core Components
โโโ label_anything/ # ๐ง Main codebase
โ โโโ **main**.py # ๐ช CLI entry point
โ โโโ cli.py # ๐ป Command interface
โ โโโ data/ # ๐ Dataset handling
โ โโโ demo/ # ๐ฎ Interactive demos
โ โโโ experiment/ # ๐งช Training workflows
โ โโโ models/ # ๐ค Neural architectures
โ โโโ loss/ # ๐ Loss functions
โ โโโ utils/ # ๐ ๏ธ Utilities
โโโ parameters/ # โ๏ธ Configuration files
โโโ trainval/ # ๐ Training configs
โโโ validation/ # ๐ Validation configs
โโโ test/ # ๐งช Testing configs
๐ Resources
โโโ notebooks/ # ๐ Analysis & demos
โโโ assets/ # ๐ผ๏ธ Media files
โโโ data/ # ๐พ Dataset storage
โโโ checkpoints/ # ๐ Model weights
๐ Deployment
โโโ slurm/ # โก HPC job scripts
โโโ app.py # ๐ Web application
- ๐ฏ Few-Shot Learning: Achieve remarkable results with minimal training data
- ๐ผ๏ธ Visual Prompting: Intuitive interaction through visual cues
- โก Multi-GPU Support: Accelerated training on modern hardware
- ๐ Cross-Validation: Robust 4-fold evaluation protocol
- ๐ Rich Logging: Comprehensive experiment tracking
- ๐ค HuggingFace Integration: Seamless model sharing and deployment
If you find Label Anything useful in your research, please cite our work:
@incollection{demarinisLabelAnythingMultiClass2025,
title = {Label {Anything}: {Multi}-{Class} {Few}-{Shot} {Semantic} {Segmentation} with {Visual} {Prompts}},
shorttitle = {Label {Anything}},
url = {https://ebooks.iospress.nl/doi/10.3233/FAIA251289},
language = {en},
booktitle = {{ECAI} 2025},
publisher = {IOS Press},
author = {De Marinis, Pasquale and Fanelli, Nicola and Scaringi, Raffaele and Colonna, Emanuele and Fiameni, Giuseppe and Vessio, Gennaro and Castellano, Giovanna},
year = {2025},
doi = {10.3233/FAIA251289},
pages = {4016--4023},
}We welcome contributions! Feel free to:
- ๐ Report bugs by opening an issue
- ๐ก Suggest new features or improvements
- ๐ง Submit pull requests with bug fixes or enhancements
- ๐ Improve documentation and examples
This project is licensed under the MIT License - see the LICENSE file for details.
Made with โค๏ธ by the CilabUniba Label Anything Team
