Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ checkpoints/*.pt
demo/backend/checkpoints/*.pt
datasets/*
*.zip
*.txt
*.txt
data
179 changes: 179 additions & 0 deletions convert.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "84f466df",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Converting sequence: bike-packing\n",
"→ Saved gray masks in data/sam2_preds_og_gray/bike-packing\n",
"Converting sequence: blackswan\n",
"→ Saved gray masks in data/sam2_preds_og_gray/blackswan\n",
"Converting sequence: bmx-trees\n",
"→ Saved gray masks in data/sam2_preds_og_gray/bmx-trees\n",
"Converting sequence: breakdance\n",
"→ Saved gray masks in data/sam2_preds_og_gray/breakdance\n",
"Converting sequence: camel\n",
"→ Saved gray masks in data/sam2_preds_og_gray/camel\n",
"Converting sequence: car-roundabout\n",
"→ Saved gray masks in data/sam2_preds_og_gray/car-roundabout\n",
"Converting sequence: car-shadow\n",
"→ Saved gray masks in data/sam2_preds_og_gray/car-shadow\n",
"Converting sequence: cows\n",
"→ Saved gray masks in data/sam2_preds_og_gray/cows\n",
"Converting sequence: dance-twirl\n",
"→ Saved gray masks in data/sam2_preds_og_gray/dance-twirl\n",
"Converting sequence: dog\n",
"→ Saved gray masks in data/sam2_preds_og_gray/dog\n",
"Converting sequence: dogs-jump\n",
"→ Saved gray masks in data/sam2_preds_og_gray/dogs-jump\n",
"Converting sequence: drift-chicane\n",
"→ Saved gray masks in data/sam2_preds_og_gray/drift-chicane\n",
"Converting sequence: drift-straight\n",
"→ Saved gray masks in data/sam2_preds_og_gray/drift-straight\n",
"Converting sequence: goat\n",
"→ Saved gray masks in data/sam2_preds_og_gray/goat\n",
"Converting sequence: gold-fish\n",
"→ Saved gray masks in data/sam2_preds_og_gray/gold-fish\n",
"Converting sequence: horsejump-high\n",
"→ Saved gray masks in data/sam2_preds_og_gray/horsejump-high\n",
"Converting sequence: india\n",
"→ Saved gray masks in data/sam2_preds_og_gray/india\n",
"Converting sequence: judo\n",
"→ Saved gray masks in data/sam2_preds_og_gray/judo\n",
"Converting sequence: kite-surf\n",
"→ Saved gray masks in data/sam2_preds_og_gray/kite-surf\n",
"Converting sequence: lab-coat\n",
"→ Saved gray masks in data/sam2_preds_og_gray/lab-coat\n",
"Converting sequence: libby\n",
"→ Saved gray masks in data/sam2_preds_og_gray/libby\n",
"Converting sequence: loading\n",
"→ Saved gray masks in data/sam2_preds_og_gray/loading\n",
"Converting sequence: mbike-trick\n",
"→ Saved gray masks in data/sam2_preds_og_gray/mbike-trick\n",
"Converting sequence: motocross-jump\n",
"→ Saved gray masks in data/sam2_preds_og_gray/motocross-jump\n",
"Converting sequence: paragliding-launch\n",
"→ Saved gray masks in data/sam2_preds_og_gray/paragliding-launch\n",
"Converting sequence: parkour\n",
"→ Saved gray masks in data/sam2_preds_og_gray/parkour\n",
"Converting sequence: pigs\n",
"→ Saved gray masks in data/sam2_preds_og_gray/pigs\n",
"Converting sequence: scooter-black\n",
"→ Saved gray masks in data/sam2_preds_og_gray/scooter-black\n",
"Converting sequence: shooting\n",
"→ Saved gray masks in data/sam2_preds_og_gray/shooting\n",
"Converting sequence: soapbox\n",
"→ Saved gray masks in data/sam2_preds_og_gray/soapbox\n",
"All sequences converted. Now run DAVIS evaluation on data/sam2_preds_og_gray\n"
]
}
],
"source": [
"import os\n",
"from pathlib import Path\n",
"import imageio.v3 as iio\n",
"import numpy as np\n",
"\n",
"# ── USER CONFIG ─────────────────────────────────────────────────────────\n",
"SOURCE_DIR = Path(\"./data/sam2_preds_og\") # your colored‐RGB predictions\n",
"TARGET_DIR = Path(\"./data/sam2_preds_og_gray\") # where we'll save single‐channel PNGs\n",
"\n",
"TARGET_DIR.mkdir(parents=True, exist_ok=True)\n",
"\n",
"# ── HELPER: find the “object‐colors → ID” mapping from frame 0 ─────────────\n",
"def extract_color_to_id_map(first_color_png):\n",
" \"\"\"\n",
" Read the RGB image first_color_png (H×W×3), find all non‐black colors,\n",
" and assign them IDs 1,2,3,… in sorted order. Returns:\n",
" • color2id: dict mapping (R,G,B) tuples → integer ID\n",
" • H, W: image dimensions\n",
" \"\"\"\n",
" rgb = iio.imread(str(first_color_png))\n",
" if rgb.ndim != 3 or rgb.shape[2] != 3:\n",
" raise RuntimeError(f\"Expected a (H, W, 3) image at {first_color_png}\")\n",
"\n",
" flat = rgb.reshape(-1, 3)\n",
" uniq = np.unique(flat, axis=0) # (K,3) array of all colors present\n",
" # Drop black (0,0,0):\n",
" non_black = [tuple(c) for c in uniq if not np.all(c == 0)]\n",
" if len(non_black) == 0:\n",
" raise RuntimeError(f\"No non‐black colors found in {first_color_png}\")\n",
"\n",
" # Sort by RGB lex order (optional) to assign stable IDs:\n",
" non_black.sort()\n",
" color2id = {color: (i + 1) for i, color in enumerate(non_black)}\n",
" return color2id, rgb.shape[0], rgb.shape[1]\n",
"\n",
"# ── MAIN LOOP: for each sequence, convert every frame’s RGB to single‐channel IDs ───\n",
"for seq_folder in sorted(SOURCE_DIR.iterdir()):\n",
" if not seq_folder.is_dir():\n",
" continue\n",
"\n",
" print(f\"Converting sequence: {seq_folder.name}\")\n",
" out_seq = TARGET_DIR / seq_folder.name\n",
" out_seq.mkdir(parents=True, exist_ok=True)\n",
"\n",
" # (1) find frame 00000.png and build the color→ID lookup\n",
" first_frame = seq_folder / \"00000.png\"\n",
" if not first_frame.exists():\n",
" raise RuntimeError(f\"Cannot find {first_frame}\")\n",
"\n",
" color2id, H, W = extract_color_to_id_map(first_frame)\n",
" # Example: color2id might be {(200,0,0): 1, (0,200,0): 2}\n",
"\n",
" # (2) iterate over all PNGs in this sequence folder (00000.png, 00001.png, …)\n",
" all_frames = sorted(seq_folder.glob(\"*.png\"))\n",
"\n",
" for frame_path in all_frames:\n",
" rgb = iio.imread(str(frame_path))\n",
" if rgb.ndim != 3 or rgb.shape[:2] != (H, W):\n",
" raise RuntimeError(f\"Unexpected image shape in {frame_path}: {rgb.shape}\")\n",
"\n",
" # Build a blank H×W array of uint8\n",
" id_map = np.zeros((H, W), dtype=np.uint8)\n",
"\n",
" # For each distinct color in color2id, mask and assign ID\n",
" # (Pixels that remain black → ID=0)\n",
" for (R, G, B), obj_id in color2id.items():\n",
" mask = (rgb[:, :, 0] == R) & (rgb[:, :, 1] == G) & (rgb[:, :, 2] == B)\n",
" if mask.any():\n",
" id_map[mask] = obj_id\n",
"\n",
" # Save the new single‐channel PNG\n",
" out_path = out_seq / frame_path.name\n",
" iio.imwrite(str(out_path), id_map)\n",
"\n",
" print(f\"→ Saved gray masks in {out_seq}\")\n",
"\n",
"print(\"All sequences converted. Now run DAVIS evaluation on\", TARGET_DIR)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "sam2",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading