Last active
September 5, 2023 11:59
-
-
Save sukhitashvili/745b7528f0418172e84d4ddb23f666b9 to your computer and use it in GitHub Desktop.
Revisions
-
sukhitashvili revised this gist
Sep 5, 2023 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,6 +20,8 @@ def non_max_supression(predictions: np.ndarray, conf_thr: float, iou_thr: float) Does NMS on predictions. Args: predictions: [[x1, y1, x2, y2, conf, cls], ...] conf_thr: float iou_thr: float Returns: predictions: [[x1, y1, x2, y2, conf, cls], ...] -
sukhitashvili revised this gist
Sep 5, 2023 . 1 changed file with 33 additions and 26 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,37 +1,44 @@ import torch def get_iou(pred1: torch.Tensor, pred2: torch.Tensor): """ prediction: [x1, y1, x2, y2, conf, cls] """ ar1 = abs((pred1[2] - pred1[0]) * (pred1[3] - pred1[1])) ar2 = abs((pred2[2] - pred2[0]) * (pred2[3] - pred2[1])) x1 = torch.max(pred1[0], pred2[0]) y1 = torch.max(pred1[1], pred2[1]) x2 = torch.min(pred1[2], pred2[2]) y2 = torch.min(pred1[3], pred2[3]) inter = (x2 - x1).clamp(0) * (y2 - y1).clamp(0) # clamp to zero if they don't intersect iou = inter / (ar1 + ar2 - inter) return iou def non_max_supression(predictions: np.ndarray, conf_thr: float, iou_thr: float) -> np.ndarray: """ Does NMS on predictions. Args: predictions: [[x1, y1, x2, y2, conf, cls], ...] Returns: predictions: [[x1, y1, x2, y2, conf, cls], ...] """ res_predictions = [pred for pred in predictions if pred[4] >= conf_thr] res_predictions = sorted(res_predictions, key=lambda x: x[4], reverse=True) if len(res_predictions) == 0: return predictions results = [] while len(res_predictions) > 0: cur_pred = res_predictions.pop(0) # recreate boxes res_predictions = [ pred for pred in res_predictions if (get_iou(torch.tensor(pred), torch.tensor(cur_pred)).item() < iou_thr) or (pred[4] != cur_pred[4]) ] results.append(cur_pred) results = np.array(results) return results -
sukhitashvili revised this gist
Sep 18, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,7 +27,7 @@ def non_max_supression(boxes, iou_val=0.5, prob_thr: 0.5, max_boxes=10000): rboxes = [] while len(boxes)>0: cur_box = boxes.pop(0) # recreate boxes boxes =[ box for box in boxes -
sukhitashvili revised this gist
Sep 18, 2022 . 1 changed file with 11 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,9 @@ import torch def get_iou(box1: torch.Tensor, box2: torch.Tensor): """ box - tensor of shape [5] """ ar1 = abs((box1[3] - box1[1]) * (box1[4] - box1[2])) ar2 = abs((box2[3] - box2[1]) * (box2[4] - box2[2])) x1 = torch.max(box1[1], box2[1]) @@ -12,20 +15,23 @@ def get_iou(box1: torch.Tensor, box2: torch.Tensor): return iou def non_max_supression(boxes, iou_val=0.5, prob_thr: 0.5, max_boxes=10000): """ boxes - [(class_id, score, x1, y1, x2, y2), ...] """ if max_boxes>0: boxes = boxes[:max_boxes] boxes = [box for box in boxes if box[1] >= prob_thr] boxes = sorted(boxes, key = lambda x:x[1], reverse=True) rboxes = [] while len(boxes)>0: cur_box = boxes.pop(0)cur_box # recreate boxes boxes =[ box for box in boxes if (get_iou(torch.tensor(box), torch.tensor(cur_box)).item() < iou_val) or (box[0] != cur_box[0]) ] rboxes.append(cur_box) return rboxes -
sukhitashvili revised this gist
Sep 17, 2022 . 1 changed file with 11 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,13 @@ import torch def get_iou(box1: torch.Tensor, box2: torch.Tensor): ar1 = abs((box1[3] - box1[1]) * (box1[4] - box1[2])) ar2 = abs((box2[3] - box2[1]) * (box2[4] - box2[2])) x1 = torch.max(box1[1], box2[1]) y1 = torch.max(box1[2], box2[2]) x2 = torch.min(box1[3], box2[3]) y2 = torch.min(box1[4], box2[4]) inter = (x2-x1).clamp(0) * (y2-y1).clamp(0) # clamp to zero if they don't intersect iou = inter/(ar1+ar2-inter) return iou @@ -23,7 +25,7 @@ def non_max_supression(boxes, iou_val=0.1, max_boxes=10000): # recreate boxes boxes =[ box for box in boxes if get_iou(torch.tensor(box), torch.tensor(cur_box)).item() < iou_val ] rboxes.append(cur_box) return rboxes -
sukhitashvili created this gist
Jun 23, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ def get_iou(box1, box2): ar1 = (box1[3] - box1[1]) * (box1[4] - box1[2]) ar2 = (box2[3] - box2[1]) * (box2[4] - box2[2]) x1 = max(box1[1], box2[1]) y1 = max(box1[2], box2[2]) x2 = min(box1[3], box2[3]) y2 = min(box1[4], box2[4]) inter = (x2-x1)*(y2-y1) iou = inter/(ar1+ar2-inter) return iou def non_max_supression(boxes, iou_val=0.1, max_boxes=10000): """ boxes - [(score, x1, y1, x2, y2), ...] """ boxes = sorted(boxes, key = lambda x:x[0], reverse=True) if max_boxes>0: boxes = boxes[:max_boxes] rboxes = [] while len(boxes)>0: cur_box = boxes.pop(0) # recreate boxes boxes =[ box for box in boxes if get_iou(box, cur_box)<iou_val ] rboxes.append(cur_box) return rboxes