-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvild_tf2.py
210 lines (171 loc) · 7.02 KB
/
vild_tf2.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
import tensorflow as tf
import numpy as np
import torch
import clip
from scipy.special import softmax
import time
max_boxes_to_draw = 4 #@param {type:"integer"}
nms_threshold = 0.6 #@param {type:"slider", min:0, max:0.9, step:0.05}
min_rpn_score_thresh = 0.9 #@param {type:"slider", min:0, max:1, step:0.01}
min_box_area = 220 #@param {type:"slider", min:0, max:10000, step:1.0}
params = max_boxes_to_draw, nms_threshold, min_rpn_score_thresh, min_box_area
# optional, make sure running on GPU
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
tf.config.set_visible_devices(physical_devices[0], 'GPU')
# load vild model
model = tf.saved_model.load('./image_path_v2')
# Get the model's signature
infer = model.signatures['serving_default']
# helpfull functions
def nms(dets, scores, thresh, max_dets=1000):
"""Non-maximum suppression.
Args:
dets: [N, 4]
scores: [N,]
thresh: iou threshold. Float
max_dets: int.
"""
y1 = dets[:, 0]
x1 = dets[:, 1]
y2 = dets[:, 2]
x2 = dets[:, 3]
areas = (x2 - x1) * (y2 - y1)
order = scores.argsort()[::-1]
keep = []
while order.size > 0 and len(keep) < max_dets:
i = order[0]
keep.append(i)
xx1 = np.maximum(x1[i], x1[order[1:]])
yy1 = np.maximum(y1[i], y1[order[1:]])
xx2 = np.minimum(x2[i], x2[order[1:]])
yy2 = np.minimum(y2[i], y2[order[1:]])
w = np.maximum(0.0, xx2 - xx1)
h = np.maximum(0.0, yy2 - yy1)
intersection = w * h
overlap = intersection / (areas[i] + areas[order[1:]] - intersection + 1e-12)
inds = np.where(overlap <= thresh)[0]
order = order[inds + 1]
return keep
# Build text embedding
def article(name):
return 'an' if name[0] in 'aeiou' else 'a'
def processed_name(name, rm_dot=False):
# _ for lvis
# / for obj365
res = name.replace('_', ' ').replace('/', ' or ').lower()
if rm_dot:
res = res.rstrip('.')
return res
clip.available_models()
model, preprocess = clip.load("ViT-B/32")
def build_text_embedding(categories):
templates = [
'a photo of {article} {}.'
]
run_on_gpu = torch.cuda.is_available()
with torch.no_grad():
all_text_embeddings = []
print('Building text embeddings...')
for category in categories:
texts = [
template.format(processed_name(category['name'], rm_dot=True),
article=article(category['name']))
for template in templates]
texts = [
'This is ' + text if text.startswith('a') or text.startswith('the') else text
for text in texts
]
texts = clip.tokenize(texts) #tokenize
if run_on_gpu:
texts = texts.cuda()
text_embeddings = model.encode_text(texts) #embed with text encoder
text_embeddings /= text_embeddings.norm(dim=-1, keepdim=True)
text_embedding = text_embeddings.mean(dim=0)
text_embedding /= text_embedding.norm()
all_text_embeddings.append(text_embedding)
all_text_embeddings = torch.stack(all_text_embeddings, dim=1)
if run_on_gpu:
all_text_embeddings = all_text_embeddings.cuda()
return all_text_embeddings.cpu().numpy().T
def vild(image_tensor, category_name_string, params=params):
#################################################################
# Preprocessing categories and get params
category_names = [x.strip() for x in category_name_string.split(';')]
category_names = ['background'] + category_names
categories = [{'name': item, 'id': idx+1,} for idx, item in enumerate(category_names)]
# category_indices = {cat['id']: cat for cat in categories}
max_boxes_to_draw, nms_threshold, min_rpn_score_thresh, min_box_area = params
#################################################################
# Obtain results and read image
output = infer(input=image_tensor)
roi_boxes, roi_scores, detection_boxes, scores_unused, box_outputs, detection_masks, visual_features, image_info = output['roi_boxes'].numpy(), output['roi_scores'].numpy(), output['2nd_stage_boxes'].numpy(), output['2nd_stage_scores_unused'].numpy(), output['box_outputs'].numpy(), output['mask_outputs'].numpy(), output['visual_feat_outputs'].numpy(), output['image_info'].numpy()
roi_boxes = np.squeeze(roi_boxes, axis=0) # squeeze
# no need to clip the boxes, already done
roi_scores = np.squeeze(roi_scores, axis=0)
detection_boxes = np.squeeze(detection_boxes, axis=(0, 2))
scores_unused = np.squeeze(scores_unused, axis=0)
box_outputs = np.squeeze(box_outputs, axis=0)
detection_masks = np.squeeze(detection_masks, axis=0)
visual_features = np.squeeze(visual_features, axis=0)
image_info = np.squeeze(image_info, axis=0) # obtain image info
image_scale = np.tile(image_info[2:3, :], (1, 2))
rescaled_detection_boxes = detection_boxes / image_scale # rescale
#################################################################
# Filter boxes
# Apply non-maximum suppression to detected boxes with nms threshold.
nmsed_indices = nms(
detection_boxes,
roi_scores,
thresh=nms_threshold
)
# Compute RPN box size.
box_sizes = (rescaled_detection_boxes[:, 2] - rescaled_detection_boxes[:, 0]) * (rescaled_detection_boxes[:, 3] - rescaled_detection_boxes[:, 1])
# Filter out invalid rois (nmsed rois)
valid_indices = np.where(
np.logical_and(
np.isin(np.arange(len(roi_scores), dtype=int), nmsed_indices),
np.logical_and(
np.logical_not(np.all(roi_boxes == 0., axis=-1)),
np.logical_and(
roi_scores >= min_rpn_score_thresh,
box_sizes > min_box_area
)
)
)
)[0]
print('number of valid indices', len(valid_indices))
detection_visual_feat = visual_features[valid_indices][:max_boxes_to_draw, ...]
rescaled_detection_boxes = rescaled_detection_boxes[valid_indices][:max_boxes_to_draw, ...]
#################################################################
# Compute text embeddings and detection scores, and rank results
text_features = build_text_embedding(categories)
raw_scores = detection_visual_feat.dot(text_features.T)
scores_all = softmax(100.0 * raw_scores, axis=-1)
indices = np.argsort(-np.max(scores_all, axis=1)) # Results are ranked by scores
# Print found_objects
found_objects = []
for anno_idx in indices[0:int(rescaled_detection_boxes.shape[0])]:
scores = scores_all[anno_idx]
if np.argmax(scores) == 0:
continue
found_object = category_names[np.argmax(scores)]
if found_object == "background":
print("didn't find anything")
return -1
print("Founded! ", "with score:", np.max(scores))
found_objects.append(category_names[np.argmax(scores)])
return found_objects
# test
image_path = 'image.png'
image = tf.convert_to_tensor([image_path], dtype=tf.string)
target = 'trash can'
category_name_string = ';'.join([target])
start = time.time()
vild(image, category_name_string, params)
end = time.time()
print("timec: ", end - start)
start = time.time()
vild(image, category_name_string, params)
end = time.time()
print("timec: ", end - start)