当前位置:   article > 正文

Windows 编译cython nms_cython——nms

cython——nms

目录

编译python库:

python编译通用库:用vs的native命令窗口

pytorch nms


https://github.com/pjl1995/CTracker/blob/master/model.py

调用:

from lib.cython_nms.cpu_nms import soft_nms as cython_soft_nms_wrapper

demo:

  1. import numpy as np
  2. import cpu_nms
  3. # from lib.cython_nms import cpu_nms import cython_soft_nms_wrapper
  4. x = np.ones([11,10], dtype =np.float32)
  5. aa= cpu_nms.soft_nms(x,0.7)
  6. print(aa)

编译python库:

python setup_cpu.py build_ext --inplace

python编译通用库:用vs的native命令窗口

python setup.py build_ext --inplace

有时报错,需要用vs的native命令窗口

(2)打开vs2019(或者vs2017)专用的命令行窗口

 因为我是64位的系统,因此选择的x64 native tools command prompt for vs2019

  1. from setuptools import setup, Extension
  2. import numpy as np
  3. from Cython.Build import cythonize
  4. from Cython.Distutils import build_ext
  5. # Obtain the numpy include directory. This logic works across numpy versions.
  6. try:
  7. numpy_include = np.get_include()
  8. except AttributeError:
  9. numpy_include = np.get_numpy_include()
  10. # extensions
  11. ext_args = dict(
  12. include_dirs=[numpy_include],
  13. language='c++',
  14. )
  15. ext_modules = [
  16. Extension(
  17. "bbox",
  18. ["bbox.pyx"],
  19. **ext_args
  20. ),
  21. Extension(
  22. "cpu_nms",
  23. ["cpu_nms.pyx"],
  24. **ext_args
  25. ),
  26. ]
  27. setup(
  28. name='frcnn_cython',
  29. ext_modules=cythonize(ext_modules),
  30. # inject our custom trigger
  31. cmdclass={'build_ext': build_ext},
  32. )

pytorch nms

  1. import torch
  2. def nms(boxes, scores, overlap=0.7, top_k=200):
  3. """
  4. 输入:
  5. boxes: 存储一个图片的所有预测框。[num_positive,4].
  6. scores:置信度。如果为多分类则需要将nms函数套在一个循环内。[num_positive].
  7. overlap: nms抑制时iou的阈值.
  8. top_k: 先选取置信度前top_k个框再进行nms.
  9. 返回:
  10. nms后剩余预测框的索引.
  11. """
  12. keep = scores.new(scores.size(0)).zero_().long()
  13. # 保存留下来的box的索引 [num_positive]
  14. # 函数new(): 构建一个有相同数据类型的tensor
  15. # 如果输入box为空则返回空Tensor
  16. if boxes.numel() == 0:
  17. return keep
  18. x1 = boxes[:, 0] # x1 坐标
  19. y1 = boxes[:, 1]
  20. x2 = boxes[:, 2]
  21. y2 = boxes[:, 3]
  22. area = torch.mul(x2 - x1, y2 - y1) # 并行化计算所有框的面积
  23. v, idx = scores.sort(0) # 升序排序
  24. idx = idx[-top_k:] # 前top-k的索引,从小到大
  25. xx1 = boxes.new()
  26. yy1 = boxes.new()
  27. xx2 = boxes.new() # new() 无参数,创建 相同类型的空值;
  28. yy2 = boxes.new()
  29. w = boxes.new()
  30. h = boxes.new()
  31. count = 0
  32. while idx.numel() > 0:
  33. i = idx[-1] # 目前最大score对应的索引 # 选取得分最大的框索引;
  34. keep[count] = i # 存储在keep中
  35. count += 1
  36. if idx.size(0) == 1: # 跳出循环条件:box被筛选完了
  37. break
  38. idx = idx[:-1] # 去掉最后一个
  39. # 剩下boxes的信息存储在xx,yy中
  40. torch.index_select(x1, 0, idx, out=xx1) # 从x1中再维度0选取索引为idx 数据 输出到xx1中;
  41. torch.index_select(y1, 0, idx, out=yy1) # torch.index_select() # 从tensor中按指定维度和索引 取值;
  42. torch.index_select(x2, 0, idx, out=xx2)
  43. torch.index_select(y2, 0, idx, out=yy2)
  44. # 计算当前最大置信框与其他剩余框的交集,不知道clamp的同学确实容易被误导
  45. xx1 = torch.clamp(xx1, min=x1[i]) # max(x1,xx1) # x1 y1 的最大值
  46. yy1 = torch.clamp(yy1, min=y1[i]) # max(y1,yy1)
  47. xx2 = torch.clamp(xx2, max=x2[i]) # min(x2,xx2) # x2 x3 最小值;
  48. yy2 = torch.clamp(yy2, max=y2[i]) # min(y2,yy2)
  49. w.resize_as_(xx2)
  50. h.resize_as_(yy2)
  51. w = xx2 - xx1 # w=min(x2,xx2)−max(x1,xx1)
  52. h = yy2 - yy1 # h=min(y2,yy2)−max(y1,yy1)
  53. w = torch.clamp(w, min=0.0) # max(w,0)
  54. h = torch.clamp(h, min=0.0) # max(h,0)
  55. inter = w * h
  56. # 计算当前最大置信框与其他剩余框的IOU
  57. # IoU = i / (area(a) + area(b) - i)
  58. rem_areas = torch.index_select(area, 0, idx) # 剩余的框的面积
  59. union = rem_areas + area[i] - inter # 并集
  60. IoU = inter / union # 计算iou
  61. # 选出IoU <= overlap的boxes(注意le函数的使用)
  62. idx = idx[IoU.le(overlap)] # le: 小于等于 返回的bool , 去除大于overlap的值;
  63. return keep, count

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/850401
推荐阅读
相关标签
  

闽ICP备14008679号