Skip to content

Commit a435b41

Browse files
benhoffnmanovic
authored andcommitted
added mask RCNN script (#780)
1 parent dd821ca commit a435b41

File tree

4 files changed

+181
-0
lines changed

4 files changed

+181
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Ability to [get basic information about users without admin permissions](
1111
https://github.com/opencv/cvat/issues/750).
1212
- Changed REST API: removed PUT and added DELETE methods for /api/v1/users/ID.
13+
- Added Mask-RCNN Auto Annotation Script
1314

1415
### Changed
1516
-
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# mask_rcnn_inception_resnet_v2_atrous_coco
2+
3+
## Use Case and High-Level Description
4+
5+
Mask R-CNN Inception Resnet V2 Atrous is trained on COCO dataset and used for object instance segmentation.
6+
For details, see a [paper](https://arxiv.org/pdf/1703.06870.pdf).
7+
8+
## Specification
9+
10+
| Metric | Value |
11+
|---------------------------------|-------------------------------------------|
12+
| Type | Instance segmentation |
13+
| GFlops | 675.314 |
14+
| MParams | 92.368 |
15+
| Source framework | TensorFlow\* |
16+
17+
## Legal Information
18+
19+
[https://raw.githubusercontent.com/tensorflow/models/master/LICENSE]()
20+
21+
## OpenVINO Conversion Notes
22+
23+
In order to convert the code into the openvino format, please see the [following link](https://docs.openvinotoolkit.org/latest/_docs_MO_DG_prepare_model_convert_model_tf_specific_Convert_Object_Detection_API_Models.html#mask_r_cnn_topologies).
24+
25+
The conversion command from the command line prompt will look something like the following.
26+
27+
```shell
28+
$ python /opt/intel/openvino/deployment_tools/model_optimizer/mo_tf.py \
29+
--input_model /path/to/frozen_inference_graph.pb \
30+
--tensorflow_use_custom_operations_config /opt/intel/openvino/deployment_tools/model_optimizer/extensions/front/tf/mask_rcnn_support.json \
31+
--tensorflow_object_detection_api_pipeline_config /path/to/pipeline.config
32+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import numpy as np
2+
import cv2
3+
4+
5+
MASK_THRESHOLD = .5
6+
PROBABILITY_THRESHOLD = 0.2
7+
8+
9+
# Ref: https://software.intel.com/en-us/forums/computer-vision/topic/804895
10+
def segm_postprocess(box: list, raw_cls_mask, im_h, im_w, threshold):
11+
ymin, xmin, ymax, xmax = box
12+
13+
width = int(abs(xmax - xmin))
14+
height = int(abs(ymax - ymin))
15+
16+
result = np.zeros((im_h, im_w), dtype=np.uint8)
17+
resized_mask = cv2.resize(raw_cls_mask, dsize=(height, width), interpolation=cv2.INTER_CUBIC)
18+
19+
# extract the ROI of the image
20+
ymin = int(round(ymin))
21+
xmin = int(round(xmin))
22+
ymax = ymin + height
23+
xmax = xmin + width
24+
result[xmin:xmax, ymin:ymax] = (resized_mask>threshold).astype(np.uint8) * 255
25+
26+
return result
27+
28+
29+
for detection in detections:
30+
frame_number = detection['frame_id']
31+
height = detection['frame_height']
32+
width = detection['frame_width']
33+
detection = detection['detections']
34+
35+
masks = detection['masks']
36+
boxes = detection['reshape_do_2d']
37+
38+
for index, box in enumerate(boxes):
39+
label = int(box[1])
40+
obj_value = box[2]
41+
if obj_value >= PROBABILITY_THRESHOLD:
42+
x = box[3] * width
43+
y = box[4] * height
44+
right = box[5] * width
45+
bottom = box[6] * height
46+
mask = masks[index][label]
47+
48+
mask = segm_postprocess((x, y, right, bottom),
49+
mask,
50+
height,
51+
width,
52+
MASK_THRESHOLD)
53+
54+
contour, _ = cv2.findContours(mask,
55+
cv2.RETR_EXTERNAL,
56+
cv2.CHAIN_APPROX_TC89_KCOS)
57+
58+
contour = contour[0]
59+
contour = contour.tolist()
60+
contour = [x[0] for x in contour]
61+
62+
# NOTE: if you want to see the boxes, uncomment next line
63+
# results.add_box(x, y, right, bottom, label, frame_number)
64+
results.add_polygon(contour, label, frame_number)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"label_map": {
3+
"1": "person",
4+
"2": "bicycle",
5+
"3": "car",
6+
"4": "motorcycle",
7+
"5": "airplane",
8+
"6": "bus",
9+
"7": "train",
10+
"8": "truck",
11+
"9": "boat",
12+
"10": "traffic_light",
13+
"11": "fire_hydrant",
14+
"13": "stop_sign",
15+
"14": "parking_meter",
16+
"15": "bench",
17+
"16": "bird",
18+
"17": "cat",
19+
"18": "dog",
20+
"19": "horse",
21+
"20": "sheep",
22+
"21": "cow",
23+
"22": "elephant",
24+
"23": "bear",
25+
"24": "zebra",
26+
"25": "giraffe",
27+
"27": "backpack",
28+
"28": "umbrella",
29+
"31": "handbag",
30+
"32": "tie",
31+
"33": "suitcase",
32+
"34": "frisbee",
33+
"35": "skis",
34+
"36": "snowboard",
35+
"37": "sports_ball",
36+
"38": "kite",
37+
"39": "baseball_bat",
38+
"40": "baseball_glove",
39+
"41": "skateboard",
40+
"42": "surfboard",
41+
"43": "tennis_racket",
42+
"44": "bottle",
43+
"46": "wine_glass",
44+
"47": "cup",
45+
"48": "fork",
46+
"49": "knife",
47+
"50": "spoon",
48+
"51": "bowl",
49+
"52": "banana",
50+
"53": "apple",
51+
"54": "sandwich",
52+
"55": "orange",
53+
"56": "broccoli",
54+
"57": "carrot",
55+
"58": "hot_dog",
56+
"59": "pizza",
57+
"60": "donut",
58+
"61": "cake",
59+
"62": "chair",
60+
"63": "couch",
61+
"64": "potted_plant",
62+
"65": "bed",
63+
"67": "dining_table",
64+
"70": "toilet",
65+
"72": "tv",
66+
"73": "laptop",
67+
"74": "mouse",
68+
"75": "remote",
69+
"76": "keyboard",
70+
"77": "cell_phone",
71+
"78": "microwave",
72+
"79": "oven",
73+
"80": "toaster",
74+
"81": "sink",
75+
"83": "refrigerator",
76+
"84": "book",
77+
"85": "clock",
78+
"86": "vase",
79+
"87": "scissors",
80+
"88": "teddy_bear",
81+
"89": "hair_drier",
82+
"90": "toothbrush"
83+
}
84+
}

0 commit comments

Comments
 (0)