-
Notifications
You must be signed in to change notification settings - Fork 0
/
detectron2_seg_threshold.py
156 lines (129 loc) · 4.79 KB
/
detectron2_seg_threshold.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=missing-docstring
import argparse
import glob
import multiprocessing as mp
import os
import time
import tqdm
import cv2 as cv
import numpy as np
import torch
from detectron2.config import get_cfg
from detectron2.data.detection_utils import read_image
from detectron2_predictor import VisualizationDemo
def _setup_cfg(args):
# load config from file and command-line arguments
cfg = get_cfg()
# To use demo for Panoptic-DeepLab, please uncomment the following two lines.
# from detectron2.projects.panoptic_deeplab import add_panoptic_deeplab_config # noqa
# add_panoptic_deeplab_config(cfg)
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
# Set score_threshold for builtin models
cfg.MODEL.RETINANET.SCORE_THRESH_TEST = args.confidence_threshold
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = args.confidence_threshold
cfg.MODEL.PANOPTIC_FPN.COMBINE.INSTANCES_CONFIDENCE_THRESH = args.confidence_threshold
cfg.freeze()
return cfg
def _parse_args():
parser = argparse.ArgumentParser(description="Detectron2 demo for builtin configs")
parser.add_argument(
"--config-file",
default="configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml",
metavar="FILE",
help="path to config file",
)
parser.add_argument(
"--input",
nargs="+",
required=True,
help="A list of space separated input images; "
"or a single glob pattern such as 'directory/*.jpg'",
)
parser.add_argument(
"--output",
help="A file or directory to save output visualizations. "
"If not given, will show output in an OpenCV window.",
)
parser.add_argument(
"--confidence-threshold",
type=float,
default=0.5,
help="Minimum score for instance predictions to be shown",
)
parser.add_argument(
"--opts",
default=[],
nargs=argparse.REMAINDER,
help="Modify config options using the command-line 'KEY VALUE' pairs",
)
args = parser.parse_args()
print("Args")
print(f" config_file: {args.config_file}")
print(f" input: {args.input}")
print(f" output: {args.output}")
print(f" confidence_threshold: {args.confidence_threshold}")
print(f" opts: {args.opts}")
return args
def _save_thresholding(class_names, predictions, filepath):
if "instances" not in predictions:
return
instances = predictions["instances"].to(torch.device("cpu"))
assert instances.has("pred_classes") and instances.has("pred_masks")
classes = instances.pred_classes.tolist()
masks = np.asarray(instances.pred_masks)
print("Save thresholding:")
for cls, mask in zip(classes, masks):
savepath = f"{os.path.splitext(filepath)[0]}_thres_{class_names[cls]}.png"
print(f" {savepath}")
img = np.zeros(instances.image_size)
img[mask] = 255
cv.imwrite(savepath, img)
def _main():
mp.set_start_method("spawn", force=True)
args = _parse_args()
cfg = _setup_cfg(args)
demo = VisualizationDemo(cfg)
class_names = demo.metadata.thing_classes
if len(args.input) == 1:
args.input = glob.glob(os.path.expanduser(args.input[0]))
assert args.input, "The input path(s) was not found"
for path in tqdm.tqdm(args.input, disable=not args.output):
# use PIL, to be consistent with evaluation
img = read_image(path, format="BGR")
start_time = time.time()
predictions, visualized_output = demo.run_on_image(img)
print(
"{}: {} in {:.2f}s".format(
path,
"detected {} instances".format(len(predictions["instances"]))
if "instances" in predictions
else "finished",
time.time() - start_time,
)
)
if args.output:
if os.path.isdir(args.output):
assert os.path.isdir(args.output), args.output
out_filename = os.path.join(args.output, os.path.basename(path))
else:
assert len(args.input) == 1, "Please specify a directory with args.output"
out_filename = args.output
visualized_output.save(out_filename)
_save_thresholding(class_names, predictions, out_filename)
else:
win_name = "output"
cv.namedWindow(win_name, cv.WINDOW_NORMAL)
cv.imshow(win_name, visualized_output.get_image()[:, :, ::-1])
key = cv.waitKey(0) & 0xFF
if key == 27 or key == ord('q'):
break
if __name__ == "__main__":
_main()
# Detectron2 开始: https://yyixx.com/docs/algo/detectron2
# export DETECTRON2_DIR=detectron2
# export DETECTRON2_MODELS_DIR=models
# mkdir -p _output
# python detectron2_seg_threshold.py --config-file $DETECTRON2_DIR/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input ../data/processing/dog_catch_ball.jpg --output _output --confidence-threshold 0.6 --opts MODEL.WEIGHTS $DETECTRON2_MODELS_DIR/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl