-
Notifications
You must be signed in to change notification settings - Fork 3
/
sam_mask_refiner.py
115 lines (100 loc) · 3.6 KB
/
sam_mask_refiner.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
import argparse
import json
import os
import sys
# sys.path.append("..")
import cv2
import numpy as np
import pycocotools.mask as maskUtils
import torch
from segment_anything import sam_model_registry, SamPredictor
from tqdm import tqdm
def parse_args():
parser = argparse.ArgumentParser(
description='Refine MosaicFusion masks with SAM')
parser.add_argument(
'--ann_path',
type=str,
default='data',
help='input MosaicFusion json annotation file path')
parser.add_argument(
'--img_path',
type=str,
default='data',
help='input MosaicFusion image directory path')
parser.add_argument(
'--output_ann_path',
type=str,
default='',
help='output sam-refined json annotation file path')
parser.add_argument(
'--sam_ckpt',
type=str,
default='segment-anything/checkpoints/sam_vit_h_4b8939.pth',
help='sam checkpoint')
parser.add_argument(
'--model_type',
type=str,
default='vit_h',
help='sam model type')
parser.add_argument(
'--device',
type=str,
default='cuda',
help='device (cuda/cpu)')
args = parser.parse_args()
return args
def main():
# init
args = parse_args()
# sam
sam = sam_model_registry[args.model_type](checkpoint=args.sam_ckpt)
sam.to(device=args.device)
predictor = SamPredictor(sam)
# data
with open(args.ann_path, 'r') as f:
data = json.load(f)
iou_list = []
for img_dict in tqdm(data['images'], desc='Processing'):
image = cv2.imread(os.path.join(args.img_path, img_dict['coco_url']))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
predictor.set_image(image)
ann_inds = []
dt_masks = []
input_boxes = []
for i, ann_dict in enumerate(data['annotations']):
if ann_dict['image_id'] == img_dict['id']:
ann_inds.append(i)
dt_masks.append(ann_dict['segmentation'])
input_box = [ann_dict['bbox'][0], ann_dict['bbox'][1], ann_dict['bbox'][0] + ann_dict['bbox'][2], ann_dict['bbox'][1] + ann_dict['bbox'][3]]
input_boxes.append(input_box)
input_boxes = torch.tensor(input_boxes, device=predictor.device)
transformed_boxes = predictor.transform.apply_boxes_torch(input_boxes, image.shape[:2])
gt_masks, _, _ = predictor.predict_torch(
point_coords=None,
point_labels=None,
boxes=transformed_boxes,
multimask_output=False,
)
# mask -> rle
gt_masks = [np.asfortranarray(mask.squeeze().cpu().numpy()) for mask in gt_masks]
gt_masks = [maskUtils.encode(mask) for mask in gt_masks]
# update corresponding annotations
for i, mask in zip(ann_inds, gt_masks):
mask['counts'] = mask['counts'].decode()
data['annotations'][i]['segmentation'] = mask
data['annotations'][i]['area'] = float(maskUtils.area(mask))
data['annotations'][i]['bbox'] = maskUtils.toBbox(mask).tolist()
is_crowd = [0]
# iou per image
iou = [maskUtils.iou([dt], [gt], is_crowd) for dt, gt in zip(dt_masks, gt_masks)]
iou_list.append(np.mean(iou))
if args.output_ann_path:
with open(args.output_ann_path, 'w') as f:
json.dump(data, f)
print(f'saved SAM-refined MosaicFusion annotations to: {args.output_ann_path}')
mean_iou = sum(iou_list) / len(iou_list)
print('mIoU: ', mean_iou)
print('Done!')
if __name__ == '__main__':
main()