-
Notifications
You must be signed in to change notification settings - Fork 0
/
grounded_sam2_hf_model_demo.py
238 lines (196 loc) · 8.12 KB
/
grounded_sam2_hf_model_demo.py
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
import argparse
import os
os.environ["HF_ENDPOINT"]='https://hf-mirror.com'
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
os.environ["TORCH_CUDA_ARCH_LIST"] = '8.9'
import cv2
import json
import torch
import numpy as np
import supervision as sv
import pycocotools.mask as mask_util
from pathlib import Path
from supervision.draw.color import ColorPalette
from utils.supervision_utils import CUSTOM_COLOR_MAP
from PIL import Image
from sam2.build_sam import build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor
from transformers import AutoProcessor, AutoModelForZeroShotObjectDetection
"""
Hyper parameters
"""
parser = argparse.ArgumentParser()
parser.add_argument('--grounding-model', default="IDEA-Research/grounding-dino-tiny")
parser.add_argument("--text-prompt", default="car.")
parser.add_argument("--img-path", default="notebooks/images/truck.jpg")
parser.add_argument("--sam2-checkpoint", default="./checkpoints/sam2.1_hiera_large.pt")
parser.add_argument("--sam2-model-config", default="configs/sam2.1/sam2.1_hiera_l.yaml")
parser.add_argument("--output-dir", default="outputs/test_sam2.1")
parser.add_argument("--no-dump-json", action="store_true")
parser.add_argument("--force-cpu", action="store_true")
args = parser.parse_args()
GROUNDING_MODEL = args.grounding_model
TEXT_PROMPT = args.text_prompt
IMG_PATH = args.img_path
SAM2_CHECKPOINT = args.sam2_checkpoint
SAM2_MODEL_CONFIG = args.sam2_model_config
DEVICE = "cuda" if torch.cuda.is_available() and not args.force_cpu else "cpu"
OUTPUT_DIR = Path(args.output_dir)
DUMP_JSON_RESULTS = not args.no_dump_json
# create output directory
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
# environment settings
# use bfloat16
torch.autocast(device_type=DEVICE, dtype=torch.bfloat16).__enter__()
if torch.cuda.get_device_properties(0).major >= 8:
# turn on tfloat32 for Ampere GPUs (https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices)
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
# build SAM2 image predictor
sam2_checkpoint = SAM2_CHECKPOINT
model_cfg = SAM2_MODEL_CONFIG
sam2_model = build_sam2(model_cfg, sam2_checkpoint, device=DEVICE)
sam2_predictor = SAM2ImagePredictor(sam2_model)
# build grounding dino from huggingface
model_id = GROUNDING_MODEL
processor = AutoProcessor.from_pretrained(model_id)
grounding_model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(DEVICE)
# setup the input image and text prompt for SAM 2 and Grounding DINO
# VERY important: text queries need to be lowercased + end with a dot
text = TEXT_PROMPT
img_path = IMG_PATH
image = Image.open(img_path)
sam2_predictor.set_image(np.array(image.convert("RGB")))
inputs = processor(images=image, text=text, return_tensors="pt").to(DEVICE)
with torch.no_grad():
outputs = grounding_model(**inputs)
results = processor.post_process_grounded_object_detection(
outputs,
inputs.input_ids,
box_threshold=0.4,
text_threshold=0.3,
target_sizes=[image.size[::-1]]
)
"""
Results is a list of dict with the following structure:
[
{
'scores': tensor([0.7969, 0.6469, 0.6002, 0.4220], device='cuda:0'),
'labels': ['car', 'tire', 'tire', 'tire'],
'boxes': tensor([[ 89.3244, 278.6940, 1710.3505, 851.5143],
[1392.4701, 554.4064, 1628.6133, 777.5872],
[ 436.1182, 621.8940, 676.5255, 851.6897],
[1236.0990, 688.3547, 1400.2427, 753.1256]], device='cuda:0')
}
]
"""
# get the box prompt for SAM 2
input_boxes = results[0]["boxes"].cpu().numpy()
masks, scores, logits = sam2_predictor.predict(
point_coords=None,
point_labels=None,
box=input_boxes,
multimask_output=False,
)
"""
Post-process the output of the model to get the masks, scores, and logits for visualization
"""
# convert the shape to (n, H, W)
if masks.ndim == 4:
masks = masks.squeeze(1)
confidences = results[0]["scores"].cpu().numpy().tolist()
class_names = results[0]["labels"]
class_ids = np.array(list(range(len(class_names))))
labels = [
f"{class_name} {confidence:.2f}"
for class_name, confidence
in zip(class_names, confidences)
]
"""
Visualize image with supervision useful API
"""
img = cv2.imread(img_path)
detections = sv.Detections(
xyxy=input_boxes, # (n, 4)
mask=masks.astype(bool), # (n, h, w)
class_id=class_ids
)
"""
Note that if you want to use default color map,
you can set color=ColorPalette.DEFAULT
"""
# box_annotator = sv.BoxAnnotator(color=ColorPalette.from_hex(CUSTOM_COLOR_MAP))
# annotated_frame = box_annotator.annotate(scene=img.copy(), detections=detections)
# label_annotator = sv.LabelAnnotator(color=ColorPalette.from_hex(CUSTOM_COLOR_MAP))
# annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)
# cv2.imwrite(os.path.join(OUTPUT_DIR, "groundingdino_annotated_image.jpg"), annotated_frame)
# mask_annotator = sv.MaskAnnotator(color=ColorPalette.from_hex(CUSTOM_COLOR_MAP))
# annotated_frame = mask_annotator.annotate(scene=annotated_frame, detections=detections)
# cv2.imwrite(os.path.join(OUTPUT_DIR, "grounded_sam2_annotated_image_with_mask.jpg"), annotated_frame)
# Create combined mask for all detected objects
combined_mask = np.zeros_like(masks[0], dtype=bool)
# Create inverse mask for background
background_mask = np.logical_not(combined_mask)
# Convert image to RGBA
masked_img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
# # Apply mask to image by setting alpha channel to 0 for background
# masked_img[background_mask, 3] = 0 # Set background alpha to 0 (transparent)
# # Save masked image with transparent background
# transparent_output_dir = OUTPUT_DIR / "transparent"
# transparent_output_dir.mkdir(parents=True, exist_ok=True)
# output_img_path = transparent_output_dir / f"{Path(img_path).stem}_transparent.png"
# cv2.imwrite(str(output_img_path), masked_img)
# Create combined mask for all detected objects
combined_mask = np.zeros_like(masks[0], dtype=bool)
for mask in masks:
combined_mask = np.logical_or(combined_mask, mask.astype(bool))
# Create inverse mask for background
background_mask = np.logical_not(combined_mask)
# Convert image to RGBA
masked_img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
# Option to make masked parts white instead of transparent
white_background_img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
white_background_img[background_mask] = [0, 0, 0, 0] # Set background to white
# Save masked image with white background
white_output_dir = OUTPUT_DIR / "black_and_transparent"
white_output_dir.mkdir(parents=True, exist_ok=True)
output_img_path_white = white_output_dir / f"{Path(img_path).stem}_bt.png"
cv2.imwrite(str(output_img_path_white), white_background_img)
# Crop image to bounding box after making background white and transparent
box = input_boxes[0]
x1, y1, x2, y2 = map(int, box)
cropped_img = white_background_img[y1:y2, x1:x2]
cropped_output_dir = OUTPUT_DIR / "cropped"
cropped_output_dir.mkdir(parents=True, exist_ok=True)
output_img_path_cropped = cropped_output_dir / f"{Path(img_path).stem}_crop.png"
cv2.imwrite(str(output_img_path_cropped), cropped_img)
"""
Dump the results in standard format and save as json files
"""
def single_mask_to_rle(mask):
rle = mask_util.encode(np.array(mask[:, :, None], order="F", dtype="uint8"))[0]
rle["counts"] = rle["counts"].decode("utf-8")
return rle
if DUMP_JSON_RESULTS:
# convert mask into rle format
mask_rles = [single_mask_to_rle(mask) for mask in masks]
input_boxes = input_boxes.tolist()
scores = scores.tolist()
# save the results in standard format
results = {
"image_path": img_path,
"annotations" : [
{
"class_name": class_name,
"bbox": box,
"segmentation": mask_rle,
"score": score,
}
for class_name, box, mask_rle, score in zip(class_names, input_boxes, mask_rles, scores)
],
"box_format": "xyxy",
"img_width": image.width,
"img_height": image.height,
}
with open(os.path.join(OUTPUT_DIR, "grounded_sam2_hf_model_demo_results.json"), "w") as f:
json.dump(results, f, indent=4)