gpu_nms.pyx 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. # --------------------------------------------------------
  2. # Faster R-CNN
  3. # Copyright (c) 2015 Microsoft
  4. # Licensed under The MIT License [see LICENSE for details]
  5. # Written by Ross Girshick
  6. # --------------------------------------------------------
  7. import numpy as np
  8. cimport numpy as np
  9. assert sizeof(int) == sizeof(np.int32_t)
  10. cdef extern from "gpu_nms.hpp":
  11. void _nms(np.int32_t*, int*, np.float32_t*, int, int, float, int)
  12. def gpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh,
  13. np.int32_t device_id=0):
  14. cdef int boxes_num = dets.shape[0]
  15. cdef int boxes_dim = dets.shape[1]
  16. cdef int num_out
  17. cdef np.ndarray[np.int32_t, ndim=1] \
  18. keep = np.zeros(boxes_num, dtype=np.int32)
  19. cdef np.ndarray[np.float32_t, ndim=1] \
  20. scores = dets[:, 4]
  21. #cdef np.ndarray[np.int_t, ndim=1] \ // 20160601, by MrX
  22. # order = scores.argsort()[::-1]
  23. cdef np.ndarray[np.intp_t, ndim=1] \
  24. order = scores.argsort()[::-1]
  25. cdef np.ndarray[np.float32_t, ndim=2] \
  26. sorted_dets = dets[order, :]
  27. _nms(&keep[0], &num_out, &sorted_dets[0, 0], boxes_num, boxes_dim, thresh, device_id)
  28. keep = keep[:num_out]
  29. return list(order[keep])