MCPcopy
hub / github.com/ZFTurbo/Weighted-Boxes-Fusion / nms_method

Function nms_method

ensemble_boxes/ensemble_boxes_nms.py:124–180  ·  view source on GitHub ↗

:param boxes: list of boxes predictions from each model, each box is 4 numbers. It has 3 dimensions (models_number, model_preds, 4) Order of boxes: x1, y1, x2, y2. We expect float normalized coordinates [0; 1] :param scores: list of scores for each model :param labels: list o

(boxes, scores, labels, method=3, iou_thr=0.5, sigma=0.5, thresh=0.001, weights=None)

Source from the content-addressed store, hash-verified

122
123
124def nms_method(boxes, scores, labels, method=3, iou_thr=0.5, sigma=0.5, thresh=0.001, weights=None):
125 """
126 :param boxes: list of boxes predictions from each model, each box is 4 numbers.
127 It has 3 dimensions (models_number, model_preds, 4)
128 Order of boxes: x1, y1, x2, y2. We expect float normalized coordinates [0; 1]
129 :param scores: list of scores for each model
130 :param labels: list of labels for each model
131 :param method: 1 - linear soft-NMS, 2 - gaussian soft-NMS, 3 - standard NMS
132 :param iou_thr: IoU value for boxes to be a match
133 :param sigma: Sigma value for SoftNMS
134 :param thresh: threshold for boxes to keep (important for SoftNMS)
135 :param weights: list of weights for each model. Default: None, which means weight == 1 for each model
136
137 :return: boxes: boxes coordinates (Order of boxes: x1, y1, x2, y2).
138 :return: scores: confidence scores
139 :return: labels: boxes labels
140 """
141
142 # If weights are specified
143 if weights is not None:
144 if len(boxes) != len(weights):
145 print('Incorrect number of weights: {}. Must be: {}. Skip it'.format(len(weights), len(boxes)))
146 else:
147 weights = np.array(weights)
148 for i in range(len(weights)):
149 scores[i] = (np.array(scores[i]) * weights[i]) / weights.sum()
150
151 # We concatenate everything
152 boxes = np.concatenate(boxes)
153 scores = np.concatenate(scores)
154 labels = np.concatenate(labels)
155
156 # Run NMS independently for each label
157 unique_labels = np.unique(labels)
158 final_boxes = []
159 final_scores = []
160 final_labels = []
161 for l in unique_labels:
162 condition = (labels == l)
163 boxes_by_label = boxes[condition]
164 scores_by_label = scores[condition]
165 labels_by_label = np.array([l] * len(boxes_by_label))
166
167 if method != 3:
168 keep = cpu_soft_nms_float(boxes_by_label.copy(), scores_by_label.copy(), Nt=iou_thr, sigma=sigma, thresh=thresh, method=method)
169 else:
170 # Use faster function
171 keep = nms_float_fast(boxes_by_label, scores_by_label, thresh=iou_thr)
172
173 final_boxes.append(boxes_by_label[keep])
174 final_scores.append(scores_by_label[keep])
175 final_labels.append(labels_by_label[keep])
176 final_boxes = np.concatenate(final_boxes)
177 final_scores = np.concatenate(final_scores)
178 final_labels = np.concatenate(final_labels)
179
180 return final_boxes, final_scores, final_labels
181

Callers 3

example_nms_2_modelsFunction · 0.85
nmsFunction · 0.85
soft_nmsFunction · 0.85

Calls 2

cpu_soft_nms_floatFunction · 0.85
nms_float_fastFunction · 0.85

Tested by

no test coverage detected