# rafat2205/finetune-segment-anything-model-sam-with-custom-dataset architecture

> Architecture diagram of the GitHub repository rafat2205/finetune-segment-anything-model-sam-with-custom-dataset, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rafat2205/finetune-segment-anything-model-sam-with-custom-dataset
- Repository: https://github.com/rafat2205/finetune-segment-anything-model-sam-with-custom-dataset

## Overview

#### 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`](https://github.com/rafat2205/finetune-segment-anything-model-sam-with-custom-dataset/blob/main/SAM_custom_training_with_box_prompt.ipynb) (Box-prompt fine-tuning), [`SAM_custom_training_woithout_prompt.ipynb`](https://github.com/rafat2205/finetune-segment-anything-model-sam-with-custom-dataset/blob/main/SAM_custom_training_woithout_prompt.ipynb) (Prompt-free fine-tuning), [`Binary_Mask_for_SAM.ipynb`](https://github.com/rafat2205/finetune-segment-anything-model-sam-with-custom-dataset/blob/main/Binary_Mask_for_SAM.ipynb) (Binary mask conversion)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_dataset["Dataset preparation"]
  node_roboflow{{"Roboflow<br/>annotation service"}}
  node_yolo_export["YOLO dataset export<br/>annotation data"]
  node_mask_notebook["Binary mask conversion<br/>Jupyter notebook"]
  node_source_images["Source images<br/>image files"]
  node_binary_masks["Binary masks<br/>mask files"]
  node_box_prompts["Box prompts<br/>bounding-box coordinates"]
end

subgraph group_training["Fine-tuning workflows"]
  node_notebook_runtime["Python Jupyter runtime<br/>execution environment"]
  node_sam_checkpoint{{"SAM implementation and checkpoint<br/>external model dependency"}}
  node_prompted_notebook["Box-prompt fine-tuning<br/>Jupyter notebook"]
  node_prompt_free_notebook["Prompt-free fine-tuning<br/>Jupyter notebook"]
  node_fine_tuned_sam["Fine-tuned SAM<br/>trained model"]
  node_segmentation_quality["Segmentation quality<br/>evaluation outcome"]
end

node_roboflow -->|"exports"| node_yolo_export
node_yolo_export -->|"provides"| node_source_images
node_yolo_export -->|"annotations"| node_mask_notebook
node_source_images -->|"aligns with"| node_mask_notebook
node_mask_notebook -->|"generates"| node_binary_masks
node_yolo_export -->|"derives"| node_box_prompts
node_notebook_runtime -->|"executes"| node_prompted_notebook
node_notebook_runtime -->|"executes"| node_prompt_free_notebook
node_sam_checkpoint -->|"loads"| node_prompted_notebook
node_sam_checkpoint -->|"loads"| node_prompt_free_notebook
node_source_images -->|"trains on"| node_prompted_notebook
node_binary_masks -->|"supervises"| node_prompted_notebook
node_box_prompts -->|"guides"| node_prompted_notebook
node_source_images -->|"trains on"| node_prompt_free_notebook
node_binary_masks -->|"supervises"| node_prompt_free_notebook
node_prompted_notebook -->|"fine-tunes"| node_fine_tuned_sam
node_prompt_free_notebook -.->|"alternative fine-tune"| node_fine_tuned_sam
node_fine_tuned_sam -->|"predicts masks for"| node_segmentation_quality

click node_mask_notebook "https://github.com/rafat2205/finetune-segment-anything-model-sam-with-custom-dataset/blob/main/Binary_Mask_for_SAM.ipynb"
click node_prompted_notebook "https://github.com/rafat2205/finetune-segment-anything-model-sam-with-custom-dataset/blob/main/SAM_custom_training_with_box_prompt.ipynb"
click node_prompt_free_notebook "https://github.com/rafat2205/finetune-segment-anything-model-sam-with-custom-dataset/blob/main/SAM_custom_training_woithout_prompt.ipynb"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_roboflow,node_yolo_export,node_mask_notebook,node_source_images,node_binary_masks,node_box_prompts toneBlue
class node_notebook_runtime,node_sam_checkpoint,node_prompted_notebook,node_prompt_free_notebook,node_fine_tuned_sam,node_segmentation_quality toneAmber
```

## Components

### Dataset preparation

External authoring and local conversion boundary.

- **Roboflow**: annotation service · Authors boxes and polygon masks outside the repository.
- **YOLO dataset export**: annotation data · Local images with class, box, or polygon coordinates.
- [Binary mask conversion](https://github.com/rafat2205/finetune-segment-anything-model-sam-with-custom-dataset/blob/main/Binary_Mask_for_SAM.ipynb): Jupyter notebook · Converts segmentation annotations into aligned white-foreground masks. · `Binary_Mask_for_SAM.ipynb`
- **Source images**: image files
- **Binary masks**: mask files · SAM target masks with foreground/background polarity.
- **Box prompts**: bounding-box coordinates · Prompt coordinates retained from exported annotations.

### Fine-tuning workflows

Alternative SAM training branches executed in notebooks.

- **Python Jupyter runtime**: execution environment · Hosts deep-learning dependencies and notebook execution.
- **SAM implementation and checkpoint**: external model dependency · Pretrained Segment Anything model used for fine-tuning.
- [Box-prompt fine-tuning](https://github.com/rafat2205/finetune-segment-anything-model-sam-with-custom-dataset/blob/main/SAM_custom_training_with_box_prompt.ipynb): Jupyter notebook · Preferred training path using box-guided mask prediction. · `SAM_custom_training_with_box_prompt.ipynb`
- [Prompt-free fine-tuning](https://github.com/rafat2205/finetune-segment-anything-model-sam-with-custom-dataset/blob/main/SAM_custom_training_woithout_prompt.ipynb): Jupyter notebook · Experimental comparison path without box guidance. · `SAM_custom_training_woithout_prompt.ipynb`
- **Fine-tuned SAM**: trained model
- **Segmentation quality**: evaluation outcome · Predicted masks are compared with binary-mask targets.

## Connections

- 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
