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.

test_sampler.py 9.7 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import torch
  3. from mmdet.core.bbox.assigners import MaxIoUAssigner
  4. from mmdet.core.bbox.samplers import (OHEMSampler, RandomSampler,
  5. ScoreHLRSampler)
  6. def test_random_sampler():
  7. assigner = MaxIoUAssigner(
  8. pos_iou_thr=0.5,
  9. neg_iou_thr=0.5,
  10. ignore_iof_thr=0.5,
  11. ignore_wrt_candidates=False,
  12. )
  13. bboxes = torch.FloatTensor([
  14. [0, 0, 10, 10],
  15. [10, 10, 20, 20],
  16. [5, 5, 15, 15],
  17. [32, 32, 38, 42],
  18. ])
  19. gt_bboxes = torch.FloatTensor([
  20. [0, 0, 10, 9],
  21. [0, 10, 10, 19],
  22. ])
  23. gt_labels = torch.LongTensor([1, 2])
  24. gt_bboxes_ignore = torch.Tensor([
  25. [30, 30, 40, 40],
  26. ])
  27. assign_result = assigner.assign(
  28. bboxes,
  29. gt_bboxes,
  30. gt_bboxes_ignore=gt_bboxes_ignore,
  31. gt_labels=gt_labels)
  32. sampler = RandomSampler(
  33. num=10, pos_fraction=0.5, neg_pos_ub=-1, add_gt_as_proposals=True)
  34. sample_result = sampler.sample(assign_result, bboxes, gt_bboxes, gt_labels)
  35. assert len(sample_result.pos_bboxes) == len(sample_result.pos_inds)
  36. assert len(sample_result.neg_bboxes) == len(sample_result.neg_inds)
  37. def test_random_sampler_empty_gt():
  38. assigner = MaxIoUAssigner(
  39. pos_iou_thr=0.5,
  40. neg_iou_thr=0.5,
  41. ignore_iof_thr=0.5,
  42. ignore_wrt_candidates=False,
  43. )
  44. bboxes = torch.FloatTensor([
  45. [0, 0, 10, 10],
  46. [10, 10, 20, 20],
  47. [5, 5, 15, 15],
  48. [32, 32, 38, 42],
  49. ])
  50. gt_bboxes = torch.empty(0, 4)
  51. gt_labels = torch.empty(0, ).long()
  52. assign_result = assigner.assign(bboxes, gt_bboxes, gt_labels=gt_labels)
  53. sampler = RandomSampler(
  54. num=10, pos_fraction=0.5, neg_pos_ub=-1, add_gt_as_proposals=True)
  55. sample_result = sampler.sample(assign_result, bboxes, gt_bboxes, gt_labels)
  56. assert len(sample_result.pos_bboxes) == len(sample_result.pos_inds)
  57. assert len(sample_result.neg_bboxes) == len(sample_result.neg_inds)
  58. def test_random_sampler_empty_pred():
  59. assigner = MaxIoUAssigner(
  60. pos_iou_thr=0.5,
  61. neg_iou_thr=0.5,
  62. ignore_iof_thr=0.5,
  63. ignore_wrt_candidates=False,
  64. )
  65. bboxes = torch.empty(0, 4)
  66. gt_bboxes = torch.FloatTensor([
  67. [0, 0, 10, 9],
  68. [0, 10, 10, 19],
  69. ])
  70. gt_labels = torch.LongTensor([1, 2])
  71. assign_result = assigner.assign(bboxes, gt_bboxes, gt_labels=gt_labels)
  72. sampler = RandomSampler(
  73. num=10, pos_fraction=0.5, neg_pos_ub=-1, add_gt_as_proposals=True)
  74. sample_result = sampler.sample(assign_result, bboxes, gt_bboxes, gt_labels)
  75. assert len(sample_result.pos_bboxes) == len(sample_result.pos_inds)
  76. assert len(sample_result.neg_bboxes) == len(sample_result.neg_inds)
  77. def _context_for_ohem():
  78. import sys
  79. from os.path import dirname
  80. sys.path.insert(0, dirname(dirname(dirname(__file__))))
  81. from test_models.test_forward import _get_detector_cfg
  82. model = _get_detector_cfg(
  83. 'faster_rcnn/faster_rcnn_r50_fpn_ohem_1x_coco.py')
  84. model['pretrained'] = None
  85. from mmdet.models import build_detector
  86. context = build_detector(model).roi_head
  87. return context
  88. def test_ohem_sampler():
  89. assigner = MaxIoUAssigner(
  90. pos_iou_thr=0.5,
  91. neg_iou_thr=0.5,
  92. ignore_iof_thr=0.5,
  93. ignore_wrt_candidates=False,
  94. )
  95. bboxes = torch.FloatTensor([
  96. [0, 0, 10, 10],
  97. [10, 10, 20, 20],
  98. [5, 5, 15, 15],
  99. [32, 32, 38, 42],
  100. ])
  101. gt_bboxes = torch.FloatTensor([
  102. [0, 0, 10, 9],
  103. [0, 10, 10, 19],
  104. ])
  105. gt_labels = torch.LongTensor([1, 2])
  106. gt_bboxes_ignore = torch.Tensor([
  107. [30, 30, 40, 40],
  108. ])
  109. assign_result = assigner.assign(
  110. bboxes,
  111. gt_bboxes,
  112. gt_bboxes_ignore=gt_bboxes_ignore,
  113. gt_labels=gt_labels)
  114. context = _context_for_ohem()
  115. sampler = OHEMSampler(
  116. num=10,
  117. pos_fraction=0.5,
  118. context=context,
  119. neg_pos_ub=-1,
  120. add_gt_as_proposals=True)
  121. feats = [torch.rand(1, 256, int(2**i), int(2**i)) for i in [6, 5, 4, 3, 2]]
  122. sample_result = sampler.sample(
  123. assign_result, bboxes, gt_bboxes, gt_labels, feats=feats)
  124. assert len(sample_result.pos_bboxes) == len(sample_result.pos_inds)
  125. assert len(sample_result.neg_bboxes) == len(sample_result.neg_inds)
  126. def test_ohem_sampler_empty_gt():
  127. assigner = MaxIoUAssigner(
  128. pos_iou_thr=0.5,
  129. neg_iou_thr=0.5,
  130. ignore_iof_thr=0.5,
  131. ignore_wrt_candidates=False,
  132. )
  133. bboxes = torch.FloatTensor([
  134. [0, 0, 10, 10],
  135. [10, 10, 20, 20],
  136. [5, 5, 15, 15],
  137. [32, 32, 38, 42],
  138. ])
  139. gt_bboxes = torch.empty(0, 4)
  140. gt_labels = torch.LongTensor([])
  141. gt_bboxes_ignore = torch.Tensor([])
  142. assign_result = assigner.assign(
  143. bboxes,
  144. gt_bboxes,
  145. gt_bboxes_ignore=gt_bboxes_ignore,
  146. gt_labels=gt_labels)
  147. context = _context_for_ohem()
  148. sampler = OHEMSampler(
  149. num=10,
  150. pos_fraction=0.5,
  151. context=context,
  152. neg_pos_ub=-1,
  153. add_gt_as_proposals=True)
  154. feats = [torch.rand(1, 256, int(2**i), int(2**i)) for i in [6, 5, 4, 3, 2]]
  155. sample_result = sampler.sample(
  156. assign_result, bboxes, gt_bboxes, gt_labels, feats=feats)
  157. assert len(sample_result.pos_bboxes) == len(sample_result.pos_inds)
  158. assert len(sample_result.neg_bboxes) == len(sample_result.neg_inds)
  159. def test_ohem_sampler_empty_pred():
  160. assigner = MaxIoUAssigner(
  161. pos_iou_thr=0.5,
  162. neg_iou_thr=0.5,
  163. ignore_iof_thr=0.5,
  164. ignore_wrt_candidates=False,
  165. )
  166. bboxes = torch.empty(0, 4)
  167. gt_bboxes = torch.FloatTensor([
  168. [0, 0, 10, 10],
  169. [10, 10, 20, 20],
  170. [5, 5, 15, 15],
  171. [32, 32, 38, 42],
  172. ])
  173. gt_labels = torch.LongTensor([1, 2, 2, 3])
  174. gt_bboxes_ignore = torch.Tensor([])
  175. assign_result = assigner.assign(
  176. bboxes,
  177. gt_bboxes,
  178. gt_bboxes_ignore=gt_bboxes_ignore,
  179. gt_labels=gt_labels)
  180. context = _context_for_ohem()
  181. sampler = OHEMSampler(
  182. num=10,
  183. pos_fraction=0.5,
  184. context=context,
  185. neg_pos_ub=-1,
  186. add_gt_as_proposals=True)
  187. feats = [torch.rand(1, 256, int(2**i), int(2**i)) for i in [6, 5, 4, 3, 2]]
  188. sample_result = sampler.sample(
  189. assign_result, bboxes, gt_bboxes, gt_labels, feats=feats)
  190. assert len(sample_result.pos_bboxes) == len(sample_result.pos_inds)
  191. assert len(sample_result.neg_bboxes) == len(sample_result.neg_inds)
  192. def test_random_sample_result():
  193. from mmdet.core.bbox.samplers.sampling_result import SamplingResult
  194. SamplingResult.random(num_gts=0, num_preds=0)
  195. SamplingResult.random(num_gts=0, num_preds=3)
  196. SamplingResult.random(num_gts=3, num_preds=3)
  197. SamplingResult.random(num_gts=0, num_preds=3)
  198. SamplingResult.random(num_gts=7, num_preds=7)
  199. SamplingResult.random(num_gts=7, num_preds=64)
  200. SamplingResult.random(num_gts=24, num_preds=3)
  201. for i in range(3):
  202. SamplingResult.random(rng=i)
  203. def test_score_hlr_sampler_empty_pred():
  204. assigner = MaxIoUAssigner(
  205. pos_iou_thr=0.5,
  206. neg_iou_thr=0.5,
  207. ignore_iof_thr=0.5,
  208. ignore_wrt_candidates=False,
  209. )
  210. context = _context_for_ohem()
  211. sampler = ScoreHLRSampler(
  212. num=10,
  213. pos_fraction=0.5,
  214. context=context,
  215. neg_pos_ub=-1,
  216. add_gt_as_proposals=True)
  217. gt_bboxes_ignore = torch.Tensor([])
  218. feats = [torch.rand(1, 256, int(2**i), int(2**i)) for i in [6, 5, 4, 3, 2]]
  219. # empty bbox
  220. bboxes = torch.empty(0, 4)
  221. gt_bboxes = torch.FloatTensor([
  222. [0, 0, 10, 10],
  223. [10, 10, 20, 20],
  224. [5, 5, 15, 15],
  225. [32, 32, 38, 42],
  226. ])
  227. gt_labels = torch.LongTensor([1, 2, 2, 3])
  228. assign_result = assigner.assign(
  229. bboxes,
  230. gt_bboxes,
  231. gt_bboxes_ignore=gt_bboxes_ignore,
  232. gt_labels=gt_labels)
  233. sample_result, _ = sampler.sample(
  234. assign_result, bboxes, gt_bboxes, gt_labels, feats=feats)
  235. assert len(sample_result.neg_inds) == 0
  236. assert len(sample_result.pos_bboxes) == len(sample_result.pos_inds)
  237. assert len(sample_result.neg_bboxes) == len(sample_result.neg_inds)
  238. # empty gt
  239. bboxes = torch.FloatTensor([
  240. [0, 0, 10, 10],
  241. [10, 10, 20, 20],
  242. [5, 5, 15, 15],
  243. [32, 32, 38, 42],
  244. ])
  245. gt_bboxes = torch.empty(0, 4)
  246. gt_labels = torch.LongTensor([])
  247. assign_result = assigner.assign(
  248. bboxes,
  249. gt_bboxes,
  250. gt_bboxes_ignore=gt_bboxes_ignore,
  251. gt_labels=gt_labels)
  252. sample_result, _ = sampler.sample(
  253. assign_result, bboxes, gt_bboxes, gt_labels, feats=feats)
  254. assert len(sample_result.pos_inds) == 0
  255. assert len(sample_result.pos_bboxes) == len(sample_result.pos_inds)
  256. assert len(sample_result.neg_bboxes) == len(sample_result.neg_inds)
  257. # non-empty input
  258. bboxes = torch.FloatTensor([
  259. [0, 0, 10, 10],
  260. [10, 10, 20, 20],
  261. [5, 5, 15, 15],
  262. [32, 32, 38, 42],
  263. ])
  264. gt_bboxes = torch.FloatTensor([
  265. [0, 0, 10, 10],
  266. [10, 10, 20, 20],
  267. [5, 5, 15, 15],
  268. [32, 32, 38, 42],
  269. ])
  270. gt_labels = torch.LongTensor([1, 2, 2, 3])
  271. assign_result = assigner.assign(
  272. bboxes,
  273. gt_bboxes,
  274. gt_bboxes_ignore=gt_bboxes_ignore,
  275. gt_labels=gt_labels)
  276. sample_result, _ = sampler.sample(
  277. assign_result, bboxes, gt_bboxes, gt_labels, feats=feats)
  278. assert len(sample_result.pos_bboxes) == len(sample_result.pos_inds)
  279. assert len(sample_result.neg_bboxes) == len(sample_result.neg_inds)

No Description

Contributors (1)