Files
Grounded-SAM-2/notebooks/automatic_mask_generator_example.ipynb

366 lines
7.5 MiB
Plaintext
Raw Normal View History

2024-07-29 21:54:20 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "5fa21d44",
"metadata": {},
"outputs": [],
"source": [
"# Copyright (c) Meta Platforms, Inc. and affiliates.\n",
"# Lightly adapted from https://github.com/facebookresearch/segment-anything/blob/main/notebooks/automatic_mask_generator_example.ipynb"
]
},
{
"cell_type": "markdown",
"id": "b7c0041e",
"metadata": {},
"source": [
"# Automatically generating object masks with SAM"
]
},
{
"cell_type": "markdown",
"id": "289bb0b4",
"metadata": {},
"source": [
"Since SAM 2 can efficiently process prompts, masks for the entire image can be generated by sampling a large number of prompts over an image.\n",
"\n",
"The class `SAM2AutomaticMaskGenerator` implements this capability. It works by sampling single-point input prompts in a grid over the image, from each of which SAM can predict multiple masks. Then, masks are filtered for quality and deduplicated using non-maximal suppression. Additional options allow for further improvement of mask quality and quantity, such as running prediction on multiple crops of the image or postprocessing masks to remove small disconnected regions and holes."
]
},
{
"cell_type": "markdown",
"id": "c0b71431",
"metadata": {},
"source": [
"## Environment Set-up"
]
},
{
"cell_type": "markdown",
"id": "47e5a78f",
"metadata": {},
"source": [
"If running locally using jupyter, first install `SAM 2` in your environment using the installation instructions in the repository."
]
},
{
"cell_type": "markdown",
"id": "fd2bc687",
"metadata": {},
"source": [
"## Set-up"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "560725a2",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import torch\n",
"import matplotlib.pyplot as plt\n",
"from PIL import Image\n",
"\n",
"# use bfloat16 for the entire notebook\n",
"torch.autocast(device_type=\"cuda\", dtype=torch.bfloat16).__enter__()\n",
"\n",
"if torch.cuda.get_device_properties(0).major >= 8:\n",
" # turn on tfloat32 for Ampere GPUs (https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices)\n",
" torch.backends.cuda.matmul.allow_tf32 = True\n",
" torch.backends.cudnn.allow_tf32 = True"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "74b6e5f0",
"metadata": {},
"outputs": [],
"source": [
"def show_anns(anns, borders=True):\n",
" if len(anns) == 0:\n",
" return\n",
" sorted_anns = sorted(anns, key=(lambda x: x['area']), reverse=True)\n",
" ax = plt.gca()\n",
" ax.set_autoscale_on(False)\n",
"\n",
" img = np.ones((sorted_anns[0]['segmentation'].shape[0], sorted_anns[0]['segmentation'].shape[1], 4))\n",
" img[:,:,3] = 0\n",
" for ann in sorted_anns:\n",
" m = ann['segmentation']\n",
" color_mask = np.concatenate([np.random.random(3), [0.5]])\n",
" img[m] = color_mask \n",
" if borders:\n",
" import cv2\n",
" contours, _ = cv2.findContours(m.astype(np.uint8),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) \n",
" # Try to smooth contours\n",
" contours = [cv2.approxPolyDP(contour, epsilon=0.01, closed=True) for contour in contours]\n",
" cv2.drawContours(img, contours, -1, (0,0,1,0.4), thickness=1) \n",
"\n",
" ax.imshow(img)"
]
},
{
"cell_type": "markdown",
"id": "27c41445",
"metadata": {},
"source": [
"## Example image"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ad354922",
"metadata": {},
"outputs": [],
"source": [
"image = Image.open('images/cars.jpg')\n",
"image = np.array(image.convert(\"RGB\"))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "e0ac8c67",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABiIAAAQdCAYAAADTkVDqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/TGe4hAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz93bIkSZIeBn6qZuYRJ6uqu6cHmBkCnKGQlMXF3vBuZR9jH2afdnchQgFAArMApqcqK/NEuJup8kJVzdT9nB4UL0ZWZC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uLPeFemwfmZsHFrwRA7Jgl2kJ5TSoGOgcqUdFtf+5D576wD5fMJnVxEpi08dRy1u/dBTsiZ54xhPHB/7lOHu+oysTYAaNuGoy89Pnh03JXTOtTphABKz1ySwfSCbDeUWtGPY+o2Wd7kswxZGvw+85PQ5a/2RD7XkFN5DzMdBx/L+8TMUxfJ+u2kiWPx76uueaWrKy3lz2TZE/zKbP3zfVVVVOKTzMq8ND4rIii1GsaVRn7PdY6xL3nEXE/nDIAdQ5r3zfHdqQv2RUv5XXktJfHHaV9hnO5wlhV5j/O6s30yaczl1PWu5u/G3LKcP/GyJDMznWS6/OxM493xjqlLiNm3Md/4fO8dxARJdLNVX7PjkZnOF56WePYnP7/ywLw3WS+K9f0//x//9w/7+Nn4zY4IckKFKjQuLgAuDKoG6HMt6EdHKwVkKC80QFEA5IotEaOQbZKogFAALkAtoFJArYJgTABUULl67gaBREBS3QFijgmqBQxGLRUYCh4CHh1cCKWVCTQQmyAnZgxSoFaoErhUKNSYfGvAGFAHsKS7UqLmuNjaBpCBxrUZ4wkC4RAsrrAQmzP
"text/plain": [
"<Figure size 2000x2000 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.figure(figsize=(20,20))\n",
"plt.imshow(image)\n",
"plt.axis('off')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "b8c2824a",
"metadata": {},
"source": [
"## Automatic mask generation"
]
},
{
"cell_type": "markdown",
"id": "d9ef74c5",
"metadata": {},
"source": [
"To run automatic mask generation, provide a version of SAM 2 to the `SAM2AutomaticMaskGenerator` class. Set the path below to the SAM 2 checkpoint."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "1848a108",
"metadata": {},
"outputs": [],
"source": [
"from sam2.build_sam import build_sam2\n",
"from sam2.automatic_mask_generator import SAM2AutomaticMaskGenerator\n",
"\n",
"sam2_checkpoint = \"../checkpoints/sam2_hiera_large.pt\"\n",
"model_cfg = \"sam2_hiera_l.yaml\"\n",
"\n",
"sam2 = build_sam2(model_cfg, sam2_checkpoint, device ='cuda', apply_postprocessing=False)\n",
"\n",
"mask_generator = SAM2AutomaticMaskGenerator(sam2)"
]
},
{
"cell_type": "markdown",
"id": "d6b1ea21",
"metadata": {},
"source": [
"To generate masks, just run `generate` on an image."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "391771c1",
"metadata": {},
"outputs": [],
"source": [
"masks = mask_generator.generate(image)"
]
},
{
"cell_type": "markdown",
"id": "e36a1a39",
"metadata": {},
"source": [
"Mask generation returns a list over masks, where each mask is a dictionary containing various data about the mask. These keys are:\n",
"* `segmentation` : the mask\n",
"* `area` : the area of the mask in pixels\n",
"* `bbox` : the boundary box of the mask in XYWH format\n",
"* `predicted_iou` : the model's own prediction for the quality of the mask\n",
"* `point_coords` : the sampled input point that generated this mask\n",
"* `stability_score` : an additional measure of mask quality\n",
"* `crop_box` : the crop of the image used to generate this mask in XYWH format"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "4fae8d66",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"60\n",
"dict_keys(['segmentation', 'area', 'bbox', 'predicted_iou', 'point_coords', 'stability_score', 'crop_box'])\n"
]
}
],
"source": [
"print(len(masks))\n",
"print(masks[0].keys())"
]
},
{
"cell_type": "markdown",
"id": "53009a1f",
"metadata": {},
"source": [
"Show all the masks overlayed on the image."
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "77ac29c5",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABiIAAAQdCAYAAADTkVDqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/TGe4hAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz9W7Nty3EeiH1ZNeZca+997gfn4OAOEACJS1MSKRJutexuu1sdHeEHR0f41T/Dz/5DfuoIhcMPdoQV0R3htkXJLQqyRIEkCAHElQQOzmXvNeeoSj9kfplZY21Chw+MjnDMAeyz1pqXMaqysjK/vJaoquJ23a7bdbtu1+26Xbfrdt2u23W7btftul2363bdrtt1u27X7bpdt+vv4Gr/cw/gdt2u23W7btftul2363bdrtt1u27X7bpdt+t23a7bdbtu1+26Xf//e90CEbfrdt2u23W7btftul2363bdrtt1u27X7bpdt+t23a7bdbtu1+36O7tugYjbdbtu1+26Xbfrdt2u23W7btftul2363bdrtt1u27X7bpdt+t2/Z1dt0DE7bpdt+t23a7bdbtu1+26Xbfrdt2u23W7btftul2363bdrtt1u/7Orlsg4nbdrtt1u27X7bpdt+t23a7bdbtu1+26Xbfrdt2u23W7btftul1/Z9ctEHG7btftul2363bdrtt1u27X7bpdt+t23a7bdbtu1+26Xbfrdt2uv7PrFoi4Xbfrdt2u23W7btftul2363bdrtt1u27X7bpdt+t23a7bdbtu19/ZdQtE3K7bdbtu1+26Xbfrdt2u23W7btftul2363bdrtt1u27X7bpdt+vv7LoFIm7X7bpdt+t23a7bdbtu1+26Xbfrdt2u23W7btftul2363bdrtv1d3Ztn/SDX/z2/wn/zXcAKNBEoABEBBAAIhAR6FRAAIEAKpjXK2ROvPbKE7zy7CkgwPvv/xovnj9gv+7AnAAEaIJ22oBtA3qH9G4PVUB1YkLRROyzUOgY0KloAqgqpDdAbQxNgbEPzKG4uzvjjddfwd39HfYx8P4HH+Oy75hQoAEiFoeZUJ+LzUEVaALMOaGq0Dl93g0bGl558gSvv/YqzqcTem/ovUFVMafdZ8yBMXY8fPwcqnaPDz78AP/0n32ML/3ijwHdbW5zAqqAKqYKAOCCDT9+9xv43/zBBoUaLWE07n3D06dPsT15Hf/P//59vPP+d/Hxi4uNv3fMJlCxb+i+Yz5coXOibx1oDQoFpkJINwVEff7d3m/S/PsDMiZ02r/ttOGNt97E/ZN7KAQffPghXlyveLhcMaeibx2tNez7FaqKftrQTqegKS+9XDEvV5vP/RkQwf5wQesd/XyGAmitQZqgSUNT4O604fV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fqbqHsorEXm0X1UVm7RFZ1VZys+ab2Nz+zev+pzjGEmXenGsyzoDaKYccr8JMAuf6568VJ9V59KLfAz7CmPZw1VXVBrXeb/MDqSeOu7V+l2Orer5RZYVnVn5pPLly9aUz+YzAktM82NXv06Mswlm4Zvz5nOWFvZ1YN3rCT/56wd8+ksf1YWKsX3/jz6DL3/z149kYKVNxUWc3//xv/1PH9HxZdcnDkTYgO13FRfgfL25k7IJpgu+kJhImBogH+5UBNLZLg1o9o/OCVOGpmTcQwGhMKeHETDQA3ECA6IKmdOcs50LjRijiDst3Rij4eIUBXTaeHsD5oRqKunugFMkBUV1lE6dodxopMPvP2PM7hGP/+YrH+Eerz2zd+j8QVGYIoKtAecnG67vb1Bc7BOtQXo65OFrYv+crqrQNst6wMckBTg2c7TIDCdS3B8Sr4k0tN4B2R3cdfTNgK6OHa25s08kHduqmOL3IN8AkNZtyL1DpAVYIL+Y46KREY3/xBTmbHafqc5HrcXYpQm0NdD
"text/plain": [
"<Figure size 2000x2000 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.figure(figsize=(20,20))\n",
"plt.imshow(image)\n",
"show_anns(masks)\n",
"plt.axis('off')\n",
"plt.show() "
]
},
{
"cell_type": "markdown",
"id": "00b3d6b2",
"metadata": {},
"source": [
"## Automatic mask generation options"
]
},
{
"cell_type": "markdown",
"id": "183de84e",
"metadata": {},
"source": [
"There are several tunable parameters in automatic mask generation that control how densely points are sampled and what the thresholds are for removing low quality or duplicate masks. Additionally, generation can be automatically run on crops of the image to get improved performance on smaller objects, and post-processing can remove stray pixels and holes. Here is an example configuration that samples more masks:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "68364513",
"metadata": {},
"outputs": [],
"source": [
"mask_generator_2 = SAM2AutomaticMaskGenerator(\n",
" model=sam2,\n",
" points_per_side=64,\n",
" points_per_batch=128,\n",
" pred_iou_thresh=0.7,\n",
" stability_score_thresh=0.92,\n",
" stability_score_offset=0.7,\n",
" crop_n_layers=1,\n",
" box_nms_thresh=0.7,\n",
" crop_n_points_downscale_factor=2,\n",
" min_mask_region_area=25.0,\n",
" use_m2m=True,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "bebcdaf1",
"metadata": {},
"outputs": [],
"source": [
"masks2 = mask_generator_2.generate(image)\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "fb702ae3",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABiIAAAQdCAYAAADTkVDqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/TGe4hAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz92a91WXIfiP1irX3u/YbMrMysSlYVayCrVCxJFMVBFCU1JbW6ZbdlGUajYb81DLT9JPjB/jf8FzRgwH6xDT+1DbhbBqEJsqQWRFIcS6SKxVGsecqqyvn77jl7rfBDxC8i1r43yewHwoBxNpn13Xvu2XuvFSuGXwwrlqiq4npdr+t1va7X9bpe1+t6Xa/rdb2u1/W6Xtfrel2v63W9rtf1ul7X68/gav+/HsD1ul7X63pdr+t1va7X9bpe1+t6Xa/rdb2u1/W6Xtfrel2v63W9/v/3uiYirtf1ul7X63pdr+t1va7X9bpe1+t6Xa/rdb2u1/W6Xtfrel2v6/Vndl0TEdfrel2v63W9rtf1ul7X63pdr+t1va7X9bpe1+t6Xa/rdb2u1/X6M7uuiYjrdb2u1/W6Xtfrel2v63W9rtf1ul7X63pdr+t1va7X9bpe1+t6/Zld10TE9bpe1+t6Xa/rdb2u1/W6Xtfrel2v63W9rtf1ul7X63pdr+t1vf7Mrmsi4npdr+t1va7X9bpe1+t6Xa/rdb2u1/W6Xtfrel2v63W9rtf1ul5/Ztc1EXG9rtf1ul7X63pdr+t1va7X9bpe1+t6Xa/rdb2u1/W6Xtfrel2vP7Prmoi4Xtfrel2v63W9rtf1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apGkkZOYY0GHGxYLXUmg
"text/plain": [
"<Figure size 2000x2000 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.figure(figsize=(20,20))\n",
"plt.imshow(image)\n",
"show_anns(masks2)\n",
"plt.axis('off')\n",
"plt.show() "
]
}
],
"metadata": {
"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.14"
}
},
"nbformat": 4,
"nbformat_minor": 5
}