File size: 7,986 Bytes
34a4bcb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "37048f21",
"metadata": {},
"outputs": [],
"source": [
"# Copyright (c) Meta Platforms, Inc. and affiliates."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "154d8663",
"metadata": {},
"outputs": [],
"source": [
"using_colab = False"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b85d99d9",
"metadata": {},
"outputs": [],
"source": [
"if using_colab:\n",
" import torch\n",
" import torchvision\n",
" print(\"PyTorch version:\", torch.__version__)\n",
" print(\"Torchvision version:\", torchvision.__version__)\n",
" print(\"CUDA is available:\", torch.cuda.is_available())\n",
" import sys\n",
" !{sys.executable} -m pip install opencv-python matplotlib scikit-learn\n",
" !{sys.executable} -m pip install 'git+https://github.com/facebookresearch/sam3.git'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "da21a3bc",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from glob import glob\n",
"\n",
"import numpy as np\n",
"import sam3.visualization_utils as utils\n",
"\n",
"from matplotlib import pyplot as plt\n",
"\n",
"COLORS = utils.pascal_color_map()[1:]"
]
},
{
"cell_type": "markdown",
"id": "57e85e7e",
"metadata": {},
"source": [
"1. Load the data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a796734e",
"metadata": {},
"outputs": [],
"source": [
"# Preapre the data path\n",
"ANNOT_DIR = None # PUT YOUR ANNOTATION PATH HERE\n",
"IMG_DIR = None # PUT YOUR IMAGE PATH HERE\n",
"\n",
"# Load the SA-CO/Gold annotation files\n",
"annot_file_list = glob(os.path.join(ANNOT_DIR, \"*gold*.json\"))\n",
"annot_dfs = utils.get_annot_dfs(file_list=annot_file_list)"
]
},
{
"cell_type": "markdown",
"id": "74bf92b1",
"metadata": {},
"source": [
"Show the annotation files being loaded"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a95620ec",
"metadata": {},
"outputs": [],
"source": [
"annot_dfs.keys()"
]
},
{
"cell_type": "markdown",
"id": "5ce211d3",
"metadata": {},
"source": [
"2. Examples of the data format"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6ba749db",
"metadata": {},
"outputs": [],
"source": [
"annot_dfs[\"gold_fg_sports_equipment_merged_a_release_test\"].keys()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b6dc186",
"metadata": {},
"outputs": [],
"source": [
"annot_dfs[\"gold_fg_sports_equipment_merged_a_release_test\"][\"info\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c41091b3",
"metadata": {},
"outputs": [],
"source": [
"annot_dfs[\"gold_fg_sports_equipment_merged_a_release_test\"][\"images\"].head(3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a7df5771",
"metadata": {},
"outputs": [],
"source": [
"annot_dfs[\"gold_fg_sports_equipment_merged_a_release_test\"][\"annotations\"].head(3)"
]
},
{
"cell_type": "markdown",
"id": "5673a63f",
"metadata": {},
"source": [
"3. Visualize the data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b1fc2a24",
"metadata": {},
"outputs": [],
"source": [
"# Select a target dataset\n",
"target_dataset_name = \"gold_fg_food_merged_a_release_test\"\n",
"\n",
"import cv2\n",
"from pycocotools import mask as mask_util\n",
"from collections import defaultdict\n",
"\n",
"# Group GT annotations by image_id\n",
"gt_image_np_pairs = annot_dfs[target_dataset_name][\"images\"]\n",
"gt_annotations = annot_dfs[target_dataset_name][\"annotations\"]\n",
"\n",
"gt_image_np_map = {img[\"id\"]: img for _, img in gt_image_np_pairs.iterrows()}\n",
"gt_image_np_ann_map = defaultdict(list)\n",
"for _, ann in gt_annotations.iterrows():\n",
" image_id = ann[\"image_id\"]\n",
" if image_id not in gt_image_np_ann_map:\n",
" gt_image_np_ann_map[image_id] = []\n",
" gt_image_np_ann_map[image_id].append(ann)\n",
"\n",
"positiveNPs = common_image_ids = [img_id for img_id in gt_image_np_map.keys() if img_id in gt_image_np_ann_map and gt_image_np_ann_map[img_id]]\n",
"negativeNPs = [img_id for img_id in gt_image_np_map.keys() if img_id not in gt_image_np_ann_map or not gt_image_np_ann_map[img_id]]\n",
"\n",
"num_image_nps_to_show = 10\n",
"fig, axes = plt.subplots(num_image_nps_to_show, 3, figsize=(15, 5 * num_image_nps_to_show))\n",
"for idx in range(num_image_nps_to_show):\n",
" rand_idx = np.random.randint(len(positiveNPs))\n",
" image_id = positiveNPs[rand_idx]\n",
" noun_phrase = gt_image_np_map[image_id][\"text_input\"]\n",
" img_rel_path = gt_image_np_map[image_id][\"file_name\"]\n",
" full_path = os.path.join(IMG_DIR, f\"{img_rel_path}\")\n",
" img = cv2.imread(full_path)\n",
" img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
" gt_annotation = gt_image_np_ann_map[image_id]\n",
"\n",
" def display_image_in_subplot(img, axes, row, col, title=\"\"):\n",
" axes[row, col].imshow(img)\n",
" axes[row, col].set_title(title)\n",
" axes[row, col].axis('off')\n",
"\n",
"\n",
" noun_phrases = [noun_phrase]\n",
" annot_masks = [mask_util.decode(ann[\"segmentation\"]) for ann in gt_annotation]\n",
"\n",
" # Show the image\n",
" display_image_in_subplot(img, axes, idx, 0, f\"{noun_phrase}\")\n",
"\n",
" # Show all masks over a white background\n",
" all_masks = utils.draw_masks_to_frame(\n",
" frame=np.ones_like(img)*255, masks=annot_masks, colors=COLORS[: len(annot_masks)]\n",
" )\n",
" display_image_in_subplot(all_masks, axes, idx, 1, f\"{noun_phrase} - Masks only\")\n",
"\n",
" # Show masks overlaid on the image\n",
" masked_frame = utils.draw_masks_to_frame(\n",
" frame=img, masks=annot_masks, colors=COLORS[: len(annot_masks)]\n",
" )\n",
" display_image_in_subplot(masked_frame, axes, idx, 2, f\"{noun_phrase} - Masks overlaid\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "84a20e0e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"fileHeader": "",
"fileUid": "a2cedcd3-26e1-430d-b718-764d51077f86",
"isAdHoc": false,
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|