-
Notifications
You must be signed in to change notification settings - Fork 5
/
davis_objectness.py
140 lines (102 loc) · 4.74 KB
/
davis_objectness.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
import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
import json
import time
# Root directory of the project
ROOT_DIR = os.path.abspath("/home/zhuo/work/toolbox/Mask_RCNN/")
print (ROOT_DIR)
# Import Mask RCNN
sys.path.append(ROOT_DIR) # To find local version of the library
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "samples/coco")) # To find local version
import coco
get_ipython().run_line_magic('matplotlib', 'inline')
# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "models", "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
utils.download_trained_weights(COCO_MODEL_PATH)
# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")
class InferenceConfig(coco.CocoConfig):
# Set batch size to 1 since we'll be running inference on
# one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
GPU_COUNT = 1
IMAGES_PER_GPU = 1
DETECTION_MIN_CONFIDENCE = 0.5
DETECTION_NMS_THRESHOLD = 0.3
config = InferenceConfig()
config.display()
# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
# Load weights trained on MS-COCO
model.load_weights(COCO_MODEL_PATH, by_name=True)
# COCO Class names
# Index of the class in the list is its ID. For example, to get ID of
# the teddy bear class, use: class_names.index('teddy bear')
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']
class NumpyEncoder(json.JSONEncoder):
""" Special json encoder for numpy types """
def default(self, obj):
if isinstance(obj, (np.int_, np.intc, np.intp, np.int8, np.int16, np.int32, np.int64,
np.uint8, np.uint16, np.uint32, np.uint64)):
return int(obj)
elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)):
return float(obj)
elif isinstance(obj,(np.ndarray,)): #### This is the fix
return obj.tolist()
return json.JSONEncoder.default(self, obj)
# save objectness results to binary masks
path = '/home/zhuo/work/dataset/CVPR2016_VOS_Benchmark/davis/JPEGImages/480p'
dir_save = 'results/objmask'
video_list = os.listdir(path)
video_list.sort()
for k in range(len(video_list)):
video = video_list[k]
frame_list = os.listdir(os.path.join(path, video))
frame_list.sort()
dir_save_name = os.path.join(dir_save, video)
if not os.path.exists(dir_save_name):
os.makedirs(dir_save_name)
time_all = 0;
for frame_id in range(len(frame_list)):
frame = skimage.io.imread(os.path.join(path, video, '{:05d}.jpg'.format(frame_id)))
# object proposals
start_time = time.time()
results = model.detect([frame], verbose=0)
result = results[0]
obj_mask = np.zeros((frame.shape[0], frame.shape[1])).astype(np.bool)
for i in range(result['masks'].shape[2]):
obj_mask = np.logical_or(obj_mask, result['masks'][:,:,i])
elapsed_time = time.time() - start_time
time_all = time_all + elapsed_time
file_save_name = os.path.join(dir_save_name, '{:05d}.jpg'.format(frame_id))
skimage.io.imsave(file_save_name, obj_mask.astype(np.uint8)*255)
print ('Progress: video = ', k, '/', len(video_list), ', frame = ', frame_id, '/', len(frame_list))
avg_time = time_all/len(frame_list)
print (avg_time)