SAM3SAR: Ship Segmentation in SAR with SAM3‑UNet (Personal Project)
January 24, 2026 · View on GitHub
This is my personal project to train a SAM3‑UNet model to segment ships in SAR imagery. I started from the upstream SAM3‑UNet implementation and adapted it to a SAR ship segmentation workflow (dataset prep, evaluation, and inference on never‑seen Sentinel‑1 scenes).
Upstream references:
- SAM3‑UNet implementation: https://github.com/WZH0120/SAM3-UNet
- SAM3 (official): https://github.com/facebookresearch/sam3
- SSDD (SAR Ship Detection Dataset): https://opensar.sjtu.edu.cn/DataAndCodes.html
Notes:
data/andweights/are intentionally ignored by git (see.gitignore).- SAM3 weights may require approval to download (I had to request access).
Repo layout
train.py,train.sh: training entrypointstest.py,test.sh: dataset-style inference (expects images + masks)infer.py: inference on a single image or folder (supports tiled inference for huge scenes)eval_segmentation.py: threshold sweep + relaxed IoU + object metrics + overlay outputstools/find_singapore_scenes.py: download Sentinel‑1 over Singapore (and Sentinel‑2 preview) and optionally convert to PNGproject_writeup.md: blog-style writeup of the full projecttutorial_for_dummies.md: friendly “from zero” tutorial
Quickstart (existing SSDD-style dataset)
- Put your data in:
data/ships/train/images
data/ships/train/masks
data/ships/val/images
data/ships/val/masks
- Train:
bash train.sh
- Predict on validation:
python test.py \
--checkpoint runs/sam3_ssdd_wsl/SAM3-UNet-10.pth \
--test_image_path data/ships/val/images \
--test_gt_path data/ships/val/masks \
--save_path runs/sam3_ssdd_wsl/preds_ep10
- Evaluate:
python eval_segmentation.py \
--val_images data/ships/val/images \
--val_masks data/ships/val/masks \
--pred_dir runs/sam3_ssdd_wsl/preds_ep10 \
--out_dir runs/sam3_ssdd_wsl/eval_ep10
Inference on a new SAR image (no masks required)
Single image:
python infer.py \
--checkpoint runs/sam3_ssdd_wsl/SAM3-UNet-10.pth \
--input /path/to/image.png \
--out-dir runs/sam3_ssdd_wsl/preds_new
Tiled inference (recommended for huge Sentinel‑1 scenes):
python infer.py \
--checkpoint runs/sam3_ssdd_wsl/SAM3-UNet-10.pth \
--input /path/to/big_scene.png \
--out-dir runs/sam3_ssdd_wsl/preds_new \
--tile \
--tile-size 336 \
--stride 256 \
--minmax
Download a never-seen Sentinel‑1 scene (Singapore port)
Set CDSE credentials in your shell (don’t commit them):
export CDSE_USERNAME="your_cdse_email"
export CDSE_PASSWORD='your_password'
Download SAFE via OData, extract VV band, convert to PNG:
python tools/find_singapore_scenes.py \
--download-full-s1 \
--full-out-dir runs/cdse_full \
--s1-band vv \
--convert-png
Then run infer.py on the produced PNG.
License and attribution
- This repo builds on upstream SAM3‑UNet and SAM3; please follow their licenses and citation guidance if you publish results.