det.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import copy
  15. from . import cv
  16. from .cv.models.utils.visualize import visualize_detection, draw_pr_curve
  17. from paddlex.cv.transforms.operators import _NormalizeBox, _PadBox, _BboxXYXY2XYWH
  18. from paddlex.cv.transforms.batch_operators import BatchCompose, BatchRandomResize, BatchRandomResizeByShort, \
  19. _BatchPadding, _Gt2YoloTarget
  20. import paddlex.utils.logging as logging
  21. visualize = visualize_detection
  22. draw_pr_curve = draw_pr_curve
  23. class FasterRCNN(cv.models.FasterRCNN):
  24. def __init__(self,
  25. num_classes=81,
  26. backbone='ResNet50',
  27. with_fpn=True,
  28. aspect_ratios=[0.5, 1.0, 2.0],
  29. anchor_sizes=[32, 64, 128, 256, 512],
  30. with_dcn=None,
  31. rpn_cls_loss=None,
  32. rpn_focal_loss_alpha=None,
  33. rpn_focal_loss_gamma=None,
  34. rcnn_bbox_loss=None,
  35. rcnn_nms=None,
  36. keep_top_k=100,
  37. nms_threshold=0.5,
  38. score_threshold=0.05,
  39. softnms_sigma=None,
  40. bbox_assigner=None,
  41. fpn_num_channels=256,
  42. input_channel=None,
  43. rpn_batch_size_per_im=256,
  44. rpn_fg_fraction=0.5,
  45. test_pre_nms_top_n=None,
  46. test_post_nms_top_n=1000):
  47. if with_dcn is not None:
  48. logging.warning(
  49. "`with_dcn` is deprecated in PaddleX 2.0 and won't take effect. Defaults to False."
  50. )
  51. if rpn_cls_loss is not None:
  52. logging.warning(
  53. "`rpn_cls_loss` is deprecated in PaddleX 2.0 and won't take effect. "
  54. "Defaults to 'SigmoidCrossEntropy'.")
  55. if rpn_focal_loss_alpha is not None or rpn_focal_loss_gamma is not None:
  56. logging.warning(
  57. "Focal loss is deprecated in PaddleX 2.0."
  58. " `rpn_focal_loss_alpha` and `rpn_focal_loss_gamma` won't take effect."
  59. )
  60. if rcnn_bbox_loss is not None:
  61. logging.warning(
  62. "`rcnn_bbox_loss` is deprecated in PaddleX 2.0 and won't take effect. "
  63. "Defaults to 'SmoothL1Loss'")
  64. if rcnn_nms is not None:
  65. logging.warning(
  66. "MultiClassSoftNMS is deprecated in PaddleX 2.0. "
  67. "`rcnn_nms` and `softnms_sigma` won't take effect. MultiClassNMS will be used by default"
  68. )
  69. if bbox_assigner is not None:
  70. logging.warning(
  71. "`bbox_assigner` is deprecated in PaddleX 2.0 and won't take effect. "
  72. "Defaults to 'BBoxAssigner'")
  73. if input_channel is not None:
  74. logging.warning(
  75. "`input_channel` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 3."
  76. )
  77. if isinstance(anchor_sizes[0], int):
  78. anchor_sizes = [[size] for size in anchor_sizes]
  79. super(FasterRCNN, self).__init__(
  80. num_classes=num_classes - 1,
  81. backbone=backbone,
  82. with_fpn=with_fpn,
  83. aspect_ratios=aspect_ratios,
  84. anchor_sizes=anchor_sizes,
  85. keep_top_k=keep_top_k,
  86. nms_threshold=nms_threshold,
  87. score_threshold=score_threshold,
  88. fpn_num_channels=fpn_num_channels,
  89. rpn_batch_size_per_im=rpn_batch_size_per_im,
  90. rpn_fg_fraction=rpn_fg_fraction,
  91. test_pre_nms_top_n=test_pre_nms_top_n,
  92. test_post_nms_top_n=test_post_nms_top_n)
  93. class YOLOv3(cv.models.YOLOv3):
  94. def __init__(self,
  95. num_classes=80,
  96. backbone='MobileNetV1',
  97. anchors=None,
  98. anchor_masks=None,
  99. ignore_threshold=0.7,
  100. nms_score_threshold=0.01,
  101. nms_topk=1000,
  102. nms_keep_topk=100,
  103. nms_iou_threshold=0.45,
  104. label_smooth=False,
  105. train_random_shapes=[
  106. 320, 352, 384, 416, 448, 480, 512, 544, 576, 608
  107. ],
  108. input_channel=None):
  109. if input_channel is not None:
  110. logging.warning(
  111. "`input_channel` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 3."
  112. )
  113. if anchors is None:
  114. anchors = [[10, 13], [16, 30], [33, 23], [30, 61], [62, 45],
  115. [59, 119], [116, 90], [156, 198], [373, 326]]
  116. if anchor_masks is None:
  117. anchor_masks = [[6, 7, 8], [3, 4, 5], [0, 1, 2]]
  118. super(YOLOv3, self).__init__(
  119. num_classes=num_classes,
  120. backbone=backbone,
  121. anchors=anchors,
  122. anchor_masks=anchor_masks,
  123. ignore_threshold=ignore_threshold,
  124. nms_score_threshold=nms_score_threshold,
  125. nms_topk=nms_topk,
  126. nms_keep_topk=nms_keep_topk,
  127. nms_iou_threshold=nms_iou_threshold,
  128. label_smooth=label_smooth)
  129. self.train_random_shapes = train_random_shapes
  130. def _compose_batch_transform(self, transforms, mode='train'):
  131. if mode == 'train':
  132. default_batch_transforms = [
  133. _BatchPadding(pad_to_stride=-1), _NormalizeBox(),
  134. _PadBox(getattr(self, 'num_max_boxes', 50)), _BboxXYXY2XYWH(),
  135. _Gt2YoloTarget(
  136. anchor_masks=self.anchor_masks,
  137. anchors=self.anchors,
  138. downsample_ratios=getattr(self, 'downsample_ratios',
  139. [32, 16, 8]),
  140. num_classes=self.num_classes)
  141. ]
  142. else:
  143. default_batch_transforms = [_BatchPadding(pad_to_stride=-1)]
  144. if mode == 'eval' and self.metric == 'voc':
  145. collate_batch = False
  146. else:
  147. collate_batch = True
  148. custom_batch_transforms = []
  149. random_shape_defined = False
  150. for i, op in enumerate(transforms.transforms):
  151. if isinstance(op, (BatchRandomResize, BatchRandomResizeByShort)):
  152. if mode != 'train':
  153. raise Exception(
  154. "{} cannot be present in the {} transforms. ".format(
  155. op.__class__.__name__, mode) +
  156. "Please check the {} transforms.".format(mode))
  157. custom_batch_transforms.insert(0, copy.deepcopy(op))
  158. random_shape_defined = True
  159. if not random_shape_defined:
  160. default_batch_transforms.insert(
  161. 0,
  162. BatchRandomResize(
  163. target_sizes=self.train_random_shapes, interp='RANDOM'))
  164. batch_transforms = BatchCompose(
  165. custom_batch_transforms + default_batch_transforms,
  166. collate_batch=collate_batch)
  167. return batch_transforms
  168. class PPYOLO(cv.models.PPYOLO):
  169. def __init__(
  170. self,
  171. num_classes=80,
  172. backbone='ResNet50_vd_ssld',
  173. with_dcn_v2=None,
  174. # YOLO Head
  175. anchors=None,
  176. anchor_masks=None,
  177. use_coord_conv=True,
  178. use_iou_aware=True,
  179. use_spp=True,
  180. use_drop_block=True,
  181. scale_x_y=1.05,
  182. # PPYOLO Loss
  183. ignore_threshold=0.7,
  184. label_smooth=False,
  185. use_iou_loss=True,
  186. # NMS
  187. use_matrix_nms=True,
  188. nms_score_threshold=0.01,
  189. nms_topk=1000,
  190. nms_keep_topk=100,
  191. nms_iou_threshold=0.45,
  192. train_random_shapes=[
  193. 320, 352, 384, 416, 448, 480, 512, 544, 576, 608
  194. ],
  195. input_channel=None):
  196. if backbone == 'ResNet50_vd_ssld':
  197. backbone = 'ResNet50_vd_dcn'
  198. if with_dcn_v2 is not None:
  199. logging.warning(
  200. "`with_dcn_v2` is deprecated in PaddleX 2.0 and will not take effect. "
  201. "To use backbone with deformable convolutional networks, "
  202. "please specify in `backbone_name`. "
  203. "Currently the only backbone with dcn is 'ResNet50_vd_dcn'.")
  204. if input_channel is not None:
  205. logging.warning(
  206. "`input_channel` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 3."
  207. )
  208. super(PPYOLO, self).__init__(
  209. num_classes=num_classes,
  210. backbone=backbone,
  211. anchors=anchors,
  212. anchor_masks=anchor_masks,
  213. use_coord_conv=use_coord_conv,
  214. use_iou_aware=use_iou_aware,
  215. use_spp=use_spp,
  216. use_drop_block=use_drop_block,
  217. scale_x_y=scale_x_y,
  218. ignore_threshold=ignore_threshold,
  219. label_smooth=label_smooth,
  220. use_iou_loss=use_iou_loss,
  221. use_matrix_nms=use_matrix_nms,
  222. nms_score_threshold=nms_score_threshold,
  223. nms_topk=nms_topk,
  224. nms_keep_topk=nms_keep_topk,
  225. nms_iou_threshold=nms_iou_threshold)
  226. self.train_random_shapes = train_random_shapes
  227. def _compose_batch_transform(self, transforms, mode='train'):
  228. if mode == 'train':
  229. default_batch_transforms = [
  230. _BatchPadding(pad_to_stride=-1), _NormalizeBox(),
  231. _PadBox(getattr(self, 'num_max_boxes', 50)), _BboxXYXY2XYWH(),
  232. _Gt2YoloTarget(
  233. anchor_masks=self.anchor_masks,
  234. anchors=self.anchors,
  235. downsample_ratios=getattr(self, 'downsample_ratios',
  236. [32, 16, 8]),
  237. num_classes=self.num_classes)
  238. ]
  239. else:
  240. default_batch_transforms = [_BatchPadding(pad_to_stride=-1)]
  241. if mode == 'eval' and self.metric == 'voc':
  242. collate_batch = False
  243. else:
  244. collate_batch = True
  245. custom_batch_transforms = []
  246. random_shape_defined = False
  247. for i, op in enumerate(transforms.transforms):
  248. if isinstance(op, (BatchRandomResize, BatchRandomResizeByShort)):
  249. if mode != 'train':
  250. raise Exception(
  251. "{} cannot be present in the {} transforms. ".format(
  252. op.__class__.__name__, mode) +
  253. "Please check the {} transforms.".format(mode))
  254. custom_batch_transforms.insert(0, copy.deepcopy(op))
  255. random_shape_defined = True
  256. if not random_shape_defined:
  257. default_batch_transforms.insert(
  258. 0,
  259. BatchRandomResize(
  260. target_sizes=self.train_random_shapes, interp='RANDOM'))
  261. batch_transforms = BatchCompose(
  262. custom_batch_transforms + default_batch_transforms,
  263. collate_batch=collate_batch)
  264. return batch_transforms