-
Notifications
You must be signed in to change notification settings - Fork 0
/
amg.py
309 lines (262 loc) · 10.1 KB
/
amg.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import argparse
import os
import torch
import yaml
import numpy as np
from tqdm import tqdm
from easydict import EasyDict
from collections import Counter
from dataloader.dataset import get_collate_class
from dataloader.pc_dataset import get_pc_model_class
from plg.segment_anything import sam_model_registry, SamPseudoLabelGenerator
parser = argparse.ArgumentParser()
parser.add_argument(
"--model-type",
type=str,
default='default',
help="The type of model to load, in ['default', 'vit_h', 'vit_l', 'vit_b']",
)
parser.add_argument(
"--checkpoint",
type=str,
default='../segment-anything/checkpoint/sam_vit_h_4b8939.pth',
help="The path to the SAM checkpoint to use for mask generation.",
)
parser.add_argument(
"--device",
type=str,
default="cuda",
help="The device to run generation on."
)
parser.add_argument(
'--config_path',
default='config/SAMPLG-semantickitti.yaml'
)
parser.add_argument(
'--dirname',
default='procssed_sk'
)
amg_settings = parser.add_argument_group("AMG Settings")
amg_settings.add_argument(
"--points-per-side",
type=int,
default=None,
help="Generate masks by sampling a grid over the image with this many points to a side.",
)
amg_settings.add_argument(
"--points-per-batch",
type=int,
default=None,
help="How many input points to process simultaneously in one batch.",
)
amg_settings.add_argument(
"--pred-iou-thresh",
type=float,
default=None,
help="Exclude masks with a predicted score from the model that is lower than this threshold.",
)
amg_settings.add_argument(
"--stability-score-thresh",
type=float,
default=None,
help="Exclude masks with a stability score lower than this threshold.",
)
amg_settings.add_argument(
"--stability-score-offset",
type=float,
default=None,
help="Larger values perturb the mask more when measuring stability score.",
)
amg_settings.add_argument(
"--box-nms-thresh",
type=float,
default=None,
help="The overlap threshold for excluding a duplicate mask.",
)
amg_settings.add_argument(
"--crop-n-layers",
type=int,
default=None,
help=(
"If >0, mask generation is run on smaller crops of the image to generate more masks. "
"The value sets how many different scales to crop at."
),
)
amg_settings.add_argument(
"--crop-nms-thresh",
type=float,
default=None,
help="The overlap threshold for excluding duplicate masks across different crops.",
)
amg_settings.add_argument(
"--crop-overlap-ratio",
type=int,
default=None,
help="Larger numbers mean image crops will overlap more.",
)
amg_settings.add_argument(
"--crop-n-points-downscale-factor",
type=int,
default=None,
help="The number of points-per-side in each layer of crop is reduced by this factor.",
)
amg_settings.add_argument(
"--min-mask-region-area",
type=int,
default=None,
help=(
"Disconnected mask regions or holes with area smaller than this value "
"in pixels are removed by postprocessing."
),
)
def get_amg_kwargs(args):
amg_kwargs = {
"points_per_side": args.points_per_side,
"points_per_batch": args.points_per_batch,
"pred_iou_thresh": args.pred_iou_thresh,
"stability_score_thresh": args.stability_score_thresh,
"stability_score_offset": args.stability_score_offset,
"box_nms_thresh": args.box_nms_thresh,
"crop_n_layers": args.crop_n_layers,
"crop_nms_thresh": args.crop_nms_thresh,
"crop_overlap_ratio": args.crop_overlap_ratio,
"crop_n_points_downscale_factor": args.crop_n_points_downscale_factor,
"min_mask_region_area": args.min_mask_region_area,
}
amg_kwargs = {k: v for k, v in amg_kwargs.items() if v is not None}
return amg_kwargs
def load_yaml(file_name):
with open(file_name, 'r') as f:
try:
config = yaml.load(f, Loader=yaml.FullLoader)
except:
config = yaml.load(f)
return config
def main(args: argparse.Namespace) -> None:
config = load_yaml(args.config_path)
config.update(vars(args))
config = EasyDict(config)
pc_dataset = get_pc_model_class(config.dataset_params.pc_dataset_type)
train_config = config.dataset_params.train_data_loader
dataset = pc_dataset(config, data_path=train_config['data_path'])
dataloader = torch.utils.data.DataLoader(dataset=dataset,
collate_fn=get_collate_class(config.dataset_params.collate_type))
print("Loading model...")
sam = sam_model_registry[args.model_type](checkpoint=args.checkpoint).to(device=args.device)
amg_kwargs = get_amg_kwargs(args)
# from plg.segment_anything import SamAutomaticMaskGenerator
# generator = SamAutomaticMaskGenerator(sam, **amg_kwargs)
generator = SamPseudoLabelGenerator(sam, **amg_kwargs)
for data_dict in tqdm(dataloader):
img = data_dict['img'][0] # (H, W, rgb)
img_indices = data_dict['img_indices'][0] # (PI, 2) in [range(H), range(W)]
img_label = data_dict['img_label'][0] # (PI, 1) in range(NC)
instance_label = data_dict['instance_label'][0] # (PI, 1) in range(NC)
path = data_dict['path'][0]
save_file = path.replace('sequences', config.dirname).replace('velodyne', 'image_2_labels').replace('.bin',
'.npy')
save_base = "/".join(save_file.split('/')[:-1])
os.makedirs(save_base, exist_ok=True)
if os.path.exists(save_file):
print(f"Skip {save_file}")
else:
# masks = generator.generate(img)
masks = generator.generate(img, img_indices, img_label, instance_label)
"""
import matplotlib.pyplot as plt
def show_mask(mask, ax, random_color=False):
if random_color:
color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
else:
color = np.array([30 / 255, 144 / 255, 255 / 255, 0.6])
h, w = mask.shape[-2:]
mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
ax.imshow(mask_image)
def show_points(coords, ax, marker_size=375):
ax.scatter(coords[0], coords[1], color='green', marker='*', s=marker_size,
edgecolor='white', linewidth=1.25)
def show_box(box, ax):
x0, y0, w, h = box[0], box[1], box[2], box[3]
ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor='green', facecolor=(0, 0, 0, 0), lw=2))
for i, mask in enumerate(masks):
plt.figure(figsize=(10, 10))
plt.imshow(img)
show_mask(mask['segmentation'], plt.gca())
show_points(mask['point_coords'] * 1241 / 1024, plt.gca())
show_box(mask['bbox'], plt.gca())
plt.title(f"Mask {i + 1}, Score: {mask['predicted_iou']:.3f}", fontsize=18)
plt.axis('off')
plt.show()
"""
gt_label = np.ones((2, img.shape[0], img.shape[1]), dtype='uint16') * 65535
for i in range(len(img_indices)):
h, w = img_indices[i]
gt_label[0, h, w] = img_label[i][0]
gt_label[1, h, w] = instance_label[i][0]
pseudo_label = gt_label.copy()
for mask in reversed(masks):
counter = Counter(gt_label[0][mask['segmentation']])
counter[65535] = 0
semantic_max = counter.most_common(1)[0][0]
if semantic_max not in [0, 65535]:
pseudo_label[0][mask['segmentation']] = semantic_max
counter = Counter(gt_label[1][mask['segmentation']])
counter[65535] = 0
instance_max = counter.most_common(1)[0][0]
if instance_max not in [0, 65535]:
pseudo_label[1][mask['segmentation']] = instance_max
pseudo_label[pseudo_label == 65535] = 0
"""
gt_label[gt_label == 65535] = 0
with open(config['dataset_params']['label_mapping'], 'r') as stream:
import yaml
semkittiyaml = yaml.safe_load(stream)
color_map = semkittiyaml['color_map']
learning_map_inv = semkittiyaml['learning_map_inv']
color = []
for i in learning_map_inv.values():
color.append(color_map[i])
color = np.array(color)
color = np.fliplr(color)
"""
"""
from PIL import Image
tmp = Image.fromarray(img)
tmp.save('img.png')
tmp = Image.fromarray(color[gt_label[0]].astype('uint8'))
tmp.save('semantic_gt_label.png')
tmp = Image.fromarray(color[pseudo_label[0]].astype('uint8'))
tmp.save('semantic_pseudo_label.png')
tmp = Image.fromarray(gt_label[1].astype('uint8'))
tmp.save('instance_gt_label.png')
tmp = Image.fromarray(pseudo_label[1].astype('uint8'))
tmp.save('instance_pseudo_label.png')
"""
"""
import matplotlib.pyplot as plt
plt.figure(figsize=(13, 4))
plt.axis('off')
plt.imshow(img)
plt.show()
plt.figure(figsize=(13, 4))
plt.axis('off')
plt.imshow(color[gt_label[0]])
plt.show()
plt.figure(figsize=(13, 4))
plt.axis('off')
plt.imshow(color[pseudo_label[0]])
plt.show()
plt.figure(figsize=(13, 4))
plt.axis('off')
plt.imshow(gt_label[1])
plt.show()
plt.figure(figsize=(13, 4))
plt.axis('off')
plt.imshow(pseudo_label[1])
plt.show()
"""
np.save(save_file, pseudo_label)
print("Done!")
if __name__ == "__main__":
args = parser.parse_args()
main(args)