🚀 トレーニングする時間がない!
トレーニング不要なリファレンスベースインスタンスセグメンテーション
最先端(Papers with Code)
_SOTA 1-shot_ | -21CBCE?style=flat&logo=paperswithcode)
🚨 更新 (2025年7月22日): カスタムデータセットの手順が追加されました!
🔔 更新 (2025年7月16日): コードが手順付きで更新されました!
📋 目次
- 🎯 ハイライト
- 📜 アブストラクト
- 🧠 アーキテクチャ
- 🛠️ インストール手順
- 1. リポジトリのクローン
- 2. conda環境の作成
- 3. SAM2とDinoV2のインストール
- 4. データセットのダウンロード
- 5. SAM2とDinoV2のチェックポイントのダウンロード
- 📊 推論コード:Few-shot COCOで30-shot SOTA結果の再現
- 0. 参照セットの作成
- 1. メモリに参照を格納
- 2. メモリバンクの後処理
- 3. ターゲット画像で推論
- 結果
- 🔍 カスタムデータセット
- 0. カスタムデータセットの準備 ⛵🐦
- 0.1 バウンディングボックスアノテーションのみ利用可能な場合
- 0.2 COCOアノテーションをpickleファイルに変換
- 1. メモリに参照を格納
- 2. メモリバンクの後処理
- 📚 引用
🎯 ハイライト
- 💡 学習不要: ファインチューニングもプロンプト設計も不要――参照画像のみ。
- 🖼️ 参照ベース: 少数の例だけで新規物体をセグメンテーション。
- 🔥 SOTA性能: COCO、PASCAL VOC、Cross-Domain FSODにて従来の学習不要手法を上回る。
📜 アブストラクト
The performance of image segmentation models has historically been constrained by the high cost of collecting large-scale annotated data. The Segment Anything Model (SAM) alleviates this original problem through a promptable, semantics-agnostic, segmentation paradigm and yet still requires manual visual-prompts or complex domain-dependent prompt-generation rules to process a new image. Towards reducing this new burden, our work investigates the task of object segmentation when provided with, alternatively, only a small set of reference images. Our key insight is to leverage strong semantic priors, as learned by foundation models, to identify corresponding regions between a reference and a target image. We find that correspondences enable automatic generation of instance-level segmentation masks for downstream tasks and instantiate our ideas via a multi-stage, training-free method incorporating (1) memory bank construction; (2) representation aggregation and (3) semantic-aware feature matching. Our experiments show significant improvements on segmentation metrics, leading to state-of-the-art performance on COCO FSOD (36.8% nAP), PASCAL VOC Few-Shot (71.2% nAP50) and outperforming existing training-free approaches on the Cross-Domain FSOD benchmark (22.4% nAP).
🧠 Architecture
🛠️ Installation instructions
1. Clone the repository
git clone https://github.com/miquel-espinosa/no-time-to-train.git
cd no-time-to-train
2. conda環境の作成
必要なパッケージを含むconda環境を作成します。
conda env create -f environment.yml
conda activate no-time-to-train
3. SAM2 と DinoV2 のインストール
SAM2 と DinoV2 をソースからインストールします。
pip install -e .
cd dinov2
pip install -e .
cd ..
4. データセットのダウンロード
COCOデータセットをダウンロードし、data/coco に配置してください。
5. SAM2およびDinoV2チェックポイントのダウンロード
論文で使用された正確なSAM2チェックポイントをダウンロードします。 (ただし、SAM2.1のチェックポイントはすでに利用可能であり、より良い性能を示す可能性があります。)
mkdir -p checkpoints/dinov2
cd checkpoints
wget https://dl.fbaipublicfiles.com/segment_anything_2/072824/sam2_hiera_large.pt
cd dinov2
wget https://dl.fbaipublicfiles.com/dinov2/dinov2_vitl14/dinov2_vitl14_pretrain.pth
cd ../..📊 推論コード
⚠️ 免責事項:これは研究用コードです — 多少の混乱はご容赦ください!
Few-shot COCOで30ショットSOTA結果の再現
有用な変数を定義し、結果用のフォルダを作成します:
CONFIG=./no_time_to_train/new_exps/coco_fewshot_10shot_Sam2L.yaml
CLASS_SPLIT="few_shot_classes"
RESULTS_DIR=work_dirs/few_shot_results
SHOTS=30
SEED=33
GPUS=4mkdir -p $RESULTS_DIR
FILENAME=few_shot_${SHOTS}shot_seed${SEED}.pkl
#### 0. 参照セットの作成python no_time_to_train/dataset/few_shot_sampling.py \
--n-shot $SHOTS \
--out-path ${RESULTS_DIR}/${FILENAME} \
--seed $SEED \
--dataset $CLASS_SPLIT
#### 1. 参照でメモリを埋めるpython run_lightening.py test --config $CONFIG \
--model.test_mode fill_memory \
--out_path ${RESULTS_DIR}/memory.ckpt \
--model.init_args.model_cfg.memory_bank_cfg.length $SHOTS \
--model.init_args.dataset_cfgs.fill_memory.memory_pkl ${RESULTS_DIR}/${FILENAME} \
--model.init_args.dataset_cfgs.fill_memory.memory_length $SHOTS \
--model.init_args.dataset_cfgs.fill_memory.class_split $CLASS_SPLIT \
--trainer.logger.save_dir ${RESULTS_DIR}/ \
--trainer.devices $GPUS
#### 2. ポストプロセスメモリバンクpython run_lightening.py test --config $CONFIG \
--model.test_mode postprocess_memory \
--model.init_args.model_cfg.memory_bank_cfg.length $SHOTS \
--ckpt_path ${RESULTS_DIR}/memory.ckpt \
--out_path ${RESULTS_DIR}/memory_postprocessed.ckpt \
--trainer.devices 1
#### 3. 対象画像に対する推論python run_lightening.py test --config $CONFIG \
--ckpt_path ${RESULTS_DIR}/memory_postprocessed.ckpt \
--model.init_args.test_mode test \
--model.init_args.model_cfg.memory_bank_cfg.length $SHOTS \
--model.init_args.model_cfg.dataset_name $CLASS_SPLIT \
--model.init_args.dataset_cfgs.test.class_split $CLASS_SPLIT \
--trainer.logger.save_dir ${RESULTS_DIR}/ \
--trainer.devices $GPUS
推論結果をオンラインで(計算されると同時に)表示したい場合は、次の引数を追加してください: --model.init_args.model_cfg.test.online_vis True
スコア閾値 score_thr パラメータを調整するには、引数を追加します(例:スコアが 0.4 より高いすべてのインスタンスを可視化する場合)。
--model.init_args.model_cfg.test.vis_thr 0.4
画像は results_analysis/few_shot_classes/ に保存されます。左側の画像は正解アノテーション、右側の画像は我々のトレーニング不要の手法で検出されたセグメント化インスタンスを示しています。この例では few_shot_classes 分割を使用しているため、この分割に含まれるクラスのインスタンスのみがセグメント化されることが期待されます(COCOのすべてのクラスではありません)。
#### 結果
バリデーションセット内のすべての画像を処理した後、以下のものが得られるはずです:
BBOX RESULTS:
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.368SEGM RESULTS:
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.342
🔍 カスタムデータセット
本パイプラインをカスタムデータセットで実行する手順を提供します。アノテーション形式は常にCOCOフォーマットです。
TLDR; カスタムデータセットでフルパイプラインを実行する方法を直接確認したい場合は、scripts/matching_cdfsod_pipeline.shおよびCD-FSODデータセットの例スクリプト(例:scripts/dior_fish.sh)をご覧ください。
0. カスタムデータセットの準備 ⛵🐦
カスタムデータセットでボート⛵と鳥🐦を検出したいと仮定します。本手法を利用するには以下が必要です:
- 各クラスごとに少なくとも1枚のアノテート済み参照画像(例:ボート用1枚、鳥用1枚)
- 対象クラスのインスタンスを見つけるための複数のターゲット画像
mkdir -p data/my_custom_dataset
python scripts/make_custom_dataset.py
これにより、以下のフォルダ構造を持つカスタムデータセットが作成されます。
data/my_custom_dataset/
├── annotations/
│ ├── custom_references.json
│ ├── custom_targets.json
│ └── references_visualisations/
│ ├── bird_1.jpg
│ └── boat_1.jpg
└── images/
├── 429819.jpg
├── 101435.jpg
└── (all target and reference images)
参照画像の可視化(1ショット):| BIRD 🐦 の1ショット参照画像 | BOAT ⛵ の1ショット参照画像 |
|:-----------------------------:|:------------------------------:|
| |
|
0.1 バウンディングボックスアノテーションのみが利用可能な場合
参照画像にバウンディングボックスアノテーションしかない場合にも対応できるよう、SAM2を用いてインスタンスレベルのセグメンテーションマスクを生成するスクリプトも提供しています。
# Download sam_h checkpoint. Feel free to use more recent checkpoints (note: code might need to be adapted)
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth -O checkpoints/sam_vit_h_4b8939.pth
Run automatic instance segmentation from ground truth bounding boxes.
python no_time_to_train/dataset/sam_bbox_to_segm_batch.py \
--input_json data/my_custom_dataset/annotations/custom_references.json \
--image_dir data/my_custom_dataset/images \
--sam_checkpoint checkpoints/sam_vit_h_4b8939.pth \
--model_type vit_h \
--device cuda \
--batch_size 8 \
--visualize
インスタンスレベルのセグメンテーションマスク付き参照画像(gtバウンディングボックスからSAM2で生成、1-shot):生成されたセグメンテーションマスクの可視化は、data/my_custom_dataset/annotations/custom_references_with_SAM_segm/references_visualisations/ に保存されています。
| BIRD 🐦 の1-shot参照画像(SAMで自動セグメント化) | BOAT ⛵ の1-shot参照画像(SAMで自動セグメント化) |
|:---------------------------------:|:----------------------------------:|
| |
|
0.2 cocoアノテーションをpickleファイルに変換
python no_time_to_train/dataset/coco_to_pkl.py \
data/my_custom_dataset/annotations/custom_references_with_segm.json \
data/my_custom_dataset/annotations/custom_references_with_segm.pkl \
1
1. 参照でメモリを埋める
最初に、便利な変数を定義し、結果用のフォルダを作成します。ラベルを正しく可視化するためには、クラス名をjsonファイルに記載されているカテゴリID順に並べる必要があります。例:bird のカテゴリIDは 16、boat のカテゴリIDは 9 です。したがって、CAT_NAMES=boat,bird となります。
DATASET_NAME=my_custom_dataset
DATASET_PATH=data/my_custom_dataset
CAT_NAMES=boat,bird
CATEGORY_NUM=2
SHOT=1
YAML_PATH=no_time_to_train/pl_configs/matching_cdfsod_template.yaml
PATH_TO_SAVE_CKPTS=./tmp_ckpts/my_custom_dataset
mkdir -p $PATH_TO_SAVE_CKPTS
ステップ1を実行します:python run_lightening.py test --config $YAML_PATH \
--model.test_mode fill_memory \
--out_path $PATH_TO_SAVE_CKPTS/$DATASET_NAME\_$SHOT\_refs_memory.pth \
--model.init_args.dataset_cfgs.fill_memory.root $DATASET_PATH/images \
--model.init_args.dataset_cfgs.fill_memory.json_file $DATASET_PATH/annotations/custom_references_with_segm.json \
--model.init_args.dataset_cfgs.fill_memory.memory_pkl $DATASET_PATH/annotations/custom_references_with_segm.pkl \
--model.init_args.dataset_cfgs.fill_memory.memory_length $SHOT \
--model.init_args.dataset_cfgs.fill_memory.cat_names $CAT_NAMES \
--model.init_args.model_cfg.dataset_name $DATASET_NAME \
--model.init_args.model_cfg.memory_bank_cfg.length $SHOT \
--model.init_args.model_cfg.memory_bank_cfg.category_num $CATEGORY_NUM \
--trainer.devices 1
2. ポストプロセスメモリバンク
python run_lightening.py test --config $YAML_PATH \
--model.test_mode postprocess_memory \
--ckpt_path $PATH_TO_SAVE_CKPTS/$DATASET_NAME\_$SHOT\_refs_memory.pth \
--out_path $PATH_TO_SAVE_CKPTS/$DATASET_NAME\_$SHOT\_refs_memory_postprocessed.pth \
--model.init_args.model_cfg.dataset_name $DATASET_NAME \
--model.init_args.model_cfg.memory_bank_cfg.length $SHOT \
--model.init_args.model_cfg.memory_bank_cfg.category_num $CATEGORY_NUM \
--trainer.devices 1
3. ターゲット画像での推論
ONLINE_VIS を True に設定すると、予測結果は results_analysis/my_custom_dataset/ に保存され、計算されると同時に表示されます。オンライン可視化を有効にすると処理速度が大幅に遅くなるので注意してください。
スコア閾値 VIS_THR を変更して、より多くまたは少ないセグメント化インスタンスを表示することができます。
ONLINE_VIS=True
VIS_THR=0.4
python run_lightening.py test --config $YAML_PATH \
--model.test_mode test \
--ckpt_path $PATH_TO_SAVE_CKPTS/$DATASET_NAME\_$SHOT\_refs_memory_postprocessed.pth \
--model.init_args.model_cfg.dataset_name $DATASET_NAME \
--model.init_args.model_cfg.memory_bank_cfg.length $SHOT \
--model.init_args.model_cfg.memory_bank_cfg.category_num $CATEGORY_NUM \
--model.init_args.model_cfg.test.imgs_path $DATASET_PATH/images \
--model.init_args.model_cfg.test.online_vis $ONLINE_VIS \
--model.init_args.model_cfg.test.vis_thr $VIS_THR \
--model.init_args.dataset_cfgs.test.root $DATASET_PATH/images \
--model.init_args.dataset_cfgs.test.json_file $DATASET_PATH/annotations/custom_targets.json \
--model.init_args.dataset_cfgs.test.cat_names $CAT_NAMES \
--trainer.devices 1
結果
パフォーマンス指標(上記のコマンドと全く同じパラメータを使用)は次のとおりです。
BBOX RESULTS:
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.478SEGM RESULTS:
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.458
results_analysis/my_custom_dataset/ に視覚的な結果が保存されます。なお、本手法は偽陰性、すなわち目的クラスのインスタンスが含まれていない画像にも対応しています。画像をクリックすると拡大表示されます ⬇️
| ボートがあるターゲット画像 ⛵(左:GT、右:予測) | 鳥がいるターゲット画像 🐦(左:GT、右:予測) |
|:----------------------:|:----------------------:|
| |
|
| ボートと鳥があるターゲット画像 ⛵🐦(左:GT、右:予測) | ボートや鳥がいないターゲット画像 🚫(左:GT、右:予測) |
|:---------------------------------:|:----------------------------------:|
| |
|
📚 引用
本研究を使用される場合は、以下のように引用してください:
@article{espinosa2025notimetotrain,
title={No time to train! Training-Free Reference-Based Instance Segmentation},
author={Miguel Espinosa and Chenhongyi Yang and Linus Ericsson and Steven McDonagh and Elliot J. Crowley},
journal={arXiv preprint arXiv:2507.02798},
year={2025},
primaryclass={cs.CV}
}--- Tranlated By Open Ai Tx | Last indexed: 2025-09-06 ---