forked from ahmedfgad/Mask-RCNN-TF2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maskrcnn_predict_directed_RPN.py
76 lines (58 loc) · 3.58 KB
/
maskrcnn_predict_directed_RPN.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
import mrcnn_directed
import mrcnn_directed.config
import mrcnn_directed.model
import mrcnn_directed.visualize
import cv2
import os
import numpy
# load the class label names from disk, one label per line
# CLASS_NAMES = open("coco_labels.txt").read().strip().split("\n")
CLASS_NAMES = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
POST_NMS_ROIS_INFERENCE = 5
REGION_PROPOSALS = numpy.zeros(shape=(1, POST_NMS_ROIS_INFERENCE, 4), dtype=numpy.float32)
REGION_PROPOSALS[0, 0, :] = [0.0, 0.0 , 0.2, 0.3]
REGION_PROPOSALS[0, 1, :] = [0.42, 0.02, 0.8, 0.267]
REGION_PROPOSALS[0, 2, :] = [0.12, 0.52, 0.55, 0.84]
REGION_PROPOSALS[0, 3, :] = [0.61, 0.71, 0.87, 0.21]
REGION_PROPOSALS[0, 4, :] = [0.074, 0.83, 0.212, 0.94]
# REGION_PROPOSALS[0, 0, :] = [0.49552074, 0. , 0.53763664, 0.09105143]
# REGION_PROPOSALS[0, 1, :] = [0.5294977 , 0.39210293, 0.63644147, 0.44242138]
# REGION_PROPOSALS[0, 2, :] = [0.36204672, 0.40500385, 0.6706183 , 0.54514766]
# REGION_PROPOSALS[0, 3, :] = [0.48107424, 0.08110721, 0.51513755, 0.17086479]
# REGION_PROPOSALS[0, 4, :] = [0.45803332, 0.15717855, 0.4798005 , 0.20352092]
class SimpleConfig(mrcnn_directed.config.Config):
# Give the configuration a recognizable name
NAME = "coco_inference"
# set the number of GPUs to use along with the number of images per GPU
GPU_COUNT = 1
IMAGES_PER_GPU = 1
# Number of classes = number of classes + 1 (+1 for the background). The background class is named BG
NUM_CLASSES = len(CLASS_NAMES)
POST_NMS_ROIS_INFERENCE = POST_NMS_ROIS_INFERENCE
# If REGION_PROPOSALS is None, then the region proposals are produced by the RPN.
# Otherwise, the user-defined region proposals are used.
REGION_PROPOSALS = REGION_PROPOSALS
# REGION_PROPOSALS = None
# Initialize the Mask R-CNN model for inference and then load the weights.
# This step builds the Keras model architecture.
model = mrcnn_directed.model.MaskRCNNDirectedRPN(mode="inference",
config=SimpleConfig(),
model_dir=os.getcwd())
# Load the weights into the model.
model.load_weights(filepath="mask_rcnn_coco.h5",
by_name=True)
# load the input image, convert it from BGR to RGB channel
image = cv2.imread("test.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Perform a forward pass of the network to obtain the results
r = model.detect([image], verbose=0)
# Get the results for the first image.
r = r[0]
r2 = r.copy()
r2[:, 0] = r2[:, 0] * image.shape[0]
r2[:, 2] = r2[:, 2] * image.shape[0]
r2[:, 1] = r2[:, 1] * image.shape[1]
r2[:, 3] = r2[:, 3] * image.shape[1]
# Visualize the detected objects.
mrcnn_directed.visualize.display_instances_RPN(image=image,
boxes=r2)