You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 2.7 kB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import numpy as np
  2. def IoU(box, boxes):
  3. """Compute IoU between detect box and gt boxes
  4. Parameters:
  5. ----------
  6. box: numpy array , shape (5, ): x1, y1, x2, y2, score
  7. input box
  8. boxes: numpy array, shape (n, 4): x1, y1, x2, y2
  9. input ground truth boxes
  10. Returns:
  11. -------
  12. ovr: numpy.array, shape (n, )
  13. IoU
  14. """
  15. box_area = (box[2] - box[0] + 1) * (box[3] - box[1] + 1)
  16. area = (boxes[:, 2] - boxes[:, 0] + 1) * (boxes[:, 3] - boxes[:, 1] + 1)
  17. xx1 = np.maximum(box[0], boxes[:, 0])
  18. yy1 = np.maximum(box[1], boxes[:, 1])
  19. xx2 = np.minimum(box[2], boxes[:, 2])
  20. yy2 = np.minimum(box[3], boxes[:, 3])
  21. # compute the width and height of the bounding box
  22. w = np.maximum(0, xx2 - xx1 + 1)
  23. h = np.maximum(0, yy2 - yy1 + 1)
  24. inter = w * h
  25. ovr = np.true_divide(inter,(box_area + area - inter))
  26. #ovr = inter / (box_area + area - inter)
  27. return ovr
  28. def convert_to_square(bbox):
  29. """Convert bbox to square
  30. Parameters:
  31. ----------
  32. bbox: numpy array , shape n x 5
  33. input bbox
  34. Returns:
  35. -------
  36. square bbox
  37. """
  38. square_bbox = bbox.copy()
  39. h = bbox[:, 3] - bbox[:, 1] + 1
  40. w = bbox[:, 2] - bbox[:, 0] + 1
  41. max_side = np.maximum(h,w)
  42. square_bbox[:, 0] = bbox[:, 0] + w*0.5 - max_side*0.5
  43. square_bbox[:, 1] = bbox[:, 1] + h*0.5 - max_side*0.5
  44. square_bbox[:, 2] = square_bbox[:, 0] + max_side - 1
  45. square_bbox[:, 3] = square_bbox[:, 1] + max_side - 1
  46. return square_bbox
  47. def nms(dets, thresh, mode="Union"):
  48. """
  49. greedily select boxes with high confidence
  50. keep boxes overlap <= thresh
  51. rule out overlap > thresh
  52. :param dets: [[x1, y1, x2, y2 score]]
  53. :param thresh: retain overlap <= thresh
  54. :return: indexes to keep
  55. """
  56. x1 = dets[:, 0]
  57. y1 = dets[:, 1]
  58. x2 = dets[:, 2]
  59. y2 = dets[:, 3]
  60. scores = dets[:, 4]
  61. areas = (x2 - x1 + 1) * (y2 - y1 + 1)
  62. order = scores.argsort()[::-1]
  63. keep = []
  64. while order.size > 0:
  65. i = order[0]
  66. keep.append(i)
  67. xx1 = np.maximum(x1[i], x1[order[1:]])
  68. yy1 = np.maximum(y1[i], y1[order[1:]])
  69. xx2 = np.minimum(x2[i], x2[order[1:]])
  70. yy2 = np.minimum(y2[i], y2[order[1:]])
  71. w = np.maximum(0.0, xx2 - xx1 + 1)
  72. h = np.maximum(0.0, yy2 - yy1 + 1)
  73. inter = w * h
  74. if mode == "Union":
  75. ovr = inter / (areas[i] + areas[order[1:]] - inter)
  76. elif mode == "Minimum":
  77. ovr = inter / np.minimum(areas[i], areas[order[1:]])
  78. inds = np.where(ovr <= thresh)[0]
  79. order = order[inds + 1]
  80. return keep

开源的深度学习人脸检测和人脸识别系统。所有功能都采用 pytorch 框架开发。pytorch是一个由facebook开发的深度学习框架,它包含了一些比较有趣的高级特性,例如自动求导,动态构图等。DFace天然的继承了这些优点,使得它的训练过程可以更加简单方便,并且实现的代码可以更加清晰易懂

Contributors (1)