-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsam_batchinfer.py
More file actions
61 lines (49 loc) · 2.13 KB
/
sam_batchinfer.py
File metadata and controls
61 lines (49 loc) · 2.13 KB
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
import json
import numpy as np
import torch
import cv2
import sys
sys.path.append("..")
from tqdm import tqdm
from segment_anything import sam_model_registry
with open('/hdd1/tb/vg/output.json', 'r') as file:
data2 = json.load(file)
sam_checkpoint = "/hdd1/tb/sam_vit_h_4b8939.pth"
model_type = "vit_h"
sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
device_ids = [ 0,2,3]
sam = torch.nn.DataParallel(sam, device_ids=device_ids).cuda(device_ids[0]) # 主设备是 device_ids[0]
count = -1
batched_input = []
for item in tqdm(data2):
length = len(item['regions'])
for i in range(0, length):
region = item['regions'][i]
image_path = region['url']
x, y, w, h = region['x'], region['y'], region['width'], region['height']
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 将 BGR 转换为 RGB
image_tensor = torch.tensor(image).permute(2, 0, 1).cuda(device_ids[0]) # 将 image 转换为 tensor 并移到主 GPU
input_box = torch.tensor([[x, y, x+w, y+h]], device=device_ids[0]) # 将 box 转换为 tensor 并移到主 GPU
batched_input.append({
'image': image_tensor,
'original_size': image.shape[:2],
'boxes': input_box
})
if len(batched_input) == 8:
outputs = sam.module.forward(batched_input, multimask_output=False)
for j, output in enumerate(outputs):
masks = output['masks'].cpu().numpy()
for k, mask in enumerate(masks):
count = count + 1
mask_to_save = (mask.squeeze() * 255).astype(np.uint8)
cv2.imwrite(f'./mask/mask{count}.png', mask_to_save)
batched_input.clear()
if batched_input:
outputs = sam.module.forward(batched_input, multimask_output=False)
for j, output in enumerate(outputs):
masks = output['masks'].cpu().numpy()
for k, mask in enumerate(masks):
count = count + 1
mask_to_save = (mask.squeeze() * 255).astype(np.uint8)
cv2.imwrite(f'./mask/mask{count}.png', mask_to_save)