How rafat2205/finetune-segment-anything-model-sam-with-custom-dataset works
Purpose and Entry Points
This repository is a notebook-based workflow for fine-tuning Meta’s Segment Anything Model (SAM) on a custom segmentation dataset. The operational entry points are Jupyter notebooks rather than an application package or CLI:
• Binary_Mask_for_SAM.ipynb
• SAM_custom_training_with_box_prompt.ipynb
• SAM_custom_training_woithout_prompt.ipynb
Dataset Preparation Boundary
Dataset authoring occurs outside the repository, primarily through Roboflow. Images are annotated with bounding boxes and polygon masks, optionally augmented, then exported in YOLO format. The repository assumes that exported dataset is available locally before notebook execution.
The input boundary is therefore YOLO-style annotation data: class identifiers followed by box or polygon coordinates.
Mask Conversion Stage
Binary_Mask_for_SAM.ipynb converts exported segmentation annotations into SAM-compatible binary masks. The intended convention is white foreground for the target region and black background elsewhere.
This notebook is the adapter between annotation-platform output and the mask representation consumed by SAM training. It is a critical correctness boundary: mask geometry, image alignment, and foreground/background polarity must remain consistent with source images.
Prompted Fine-Tuning Path
SAM_custom_training_with_box_prompt.ipynb trains or fine-tunes SAM using bounding boxes as prompts alongside images and target masks.
Primary flow:
1. Load custom images, binary masks, and associated bounding boxes.
2. Pass image features and box prompts into SAM.
3. Compare predicted masks to binary-mask targets.
4. Backpropagate loss to fine-tune the selected SAM parameters.
The README identifies this as the preferred training path because box prompts produce better results than prompt-free training.
Prompt-Free Fine-Tuning Path
SAM_custom_training_woithout_prompt.ipynb provides an alternate fine-tuning workflow without an explicit prompt. It uses the same custom-dataset and binary-mask preparation assumptions but exercises SAM’s mask prediction without box guidance.
This is an experimental/comparative branch rather than the recommended default path.
Model and Runtime Dependencies
The material runtime is Python in Jupyter notebooks, with a SAM implementation/checkpoint and deep-learning framework dependencies expected to be installed in the execution environment. SAM is the central external model dependency; Roboflow is the external dataset annotation and export service.
No deployable service, API layer, persistent database, package structure, or infrastructure-as-code is present in the repository.
End-to-End Control Flow
The intended pipeline is:
1. Create and annotate a segmentation dataset externally in Roboflow.
2. Export annotations in YOLO format.
3. Generate aligned binary masks with Binary_Mask_for_SAM.ipynb.
4. Select a training branch:
- box-prompt training: SAM_custom_training_with_box_prompt.ipynb
- no-prompt training: SAM_custom_training_woithout_prompt.ipynb
5. Fine-tune SAM and assess predicted segmentation quality.
The principal interfaces between stages are image files, YOLO annotations, derived binary masks, and—only for the prompted path—bounding-box prompt coordinates.
Main entry points: SAM_custom_training_with_box_prompt.ipynb Box-prompt fine-tuning · SAM_custom_training_woithout_prompt.ipynb Prompt-free fine-tuning · Binary_Mask_for_SAM.ipynb Binary mask conversion
Components 12
Dataset preparation
External authoring and local conversion boundary.
- Roboflow · Authors boxes and polygon masks outside the repository.
- YOLO dataset export · Local images with class, box, or polygon coordinates.
- Binary mask conversion · Converts segmentation annotations into aligned white-foreground masks.
Binary_Mask_for_SAM.ipynb - Source images
- Binary masks · SAM target masks with foreground/background polarity.
- Box prompts · Prompt coordinates retained from exported annotations.
Fine-tuning workflows
Alternative SAM training branches executed in notebooks.
- Python Jupyter runtime · Hosts deep-learning dependencies and notebook execution.
- SAM implementation and checkpoint · Pretrained Segment Anything model used for fine-tuning.
- Box-prompt fine-tuning · Preferred training path using box-guided mask prediction.
SAM_custom_training_with_box_prompt.ipynb - Prompt-free fine-tuning · Experimental comparison path without box guidance.
SAM_custom_training_woithout_prompt.ipynb - Fine-tuned SAM
- Segmentation quality · Predicted masks are compared with binary-mask targets.
Connections 18
- Roboflow → YOLO dataset export: exports
- YOLO dataset export → Source images: provides
- YOLO dataset export → Binary mask conversion: annotations
- Source images → Binary mask conversion: aligns with
- Binary mask conversion → Binary masks: generates
- YOLO dataset export → Box prompts: derives
- Python Jupyter runtime → Box-prompt fine-tuning: executes
- Python Jupyter runtime → Prompt-free fine-tuning: executes
- SAM implementation and checkpoint → Box-prompt fine-tuning: loads
- SAM implementation and checkpoint → Prompt-free fine-tuning: loads
- Source images → Box-prompt fine-tuning: trains on
- Binary masks → Box-prompt fine-tuning: supervises
- Box prompts → Box-prompt fine-tuning: guides
- Source images → Prompt-free fine-tuning: trains on
- Binary masks → Prompt-free fine-tuning: supervises
- Box-prompt fine-tuning → Fine-tuned SAM: fine-tunes
- Prompt-free fine-tuning → Fine-tuned SAM: alternative fine-tune
- Fine-tuned SAM → Segmentation quality: predicts masks for