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 import det_transforms
  18. from paddlex.cv.transforms.operators import _NormalizeBox, _PadBox, _BboxXYXY2XYWH
  19. from paddlex.cv.transforms.batch_operators import BatchCompose, BatchRandomResize, BatchRandomResizeByShort, \
  20. _BatchPadding, _Gt2YoloTarget
  21. import paddlex.utils.logging as logging
  22. transforms = det_transforms
  23. visualize = visualize_detection
  24. draw_pr_curve = draw_pr_curve
  25. class FasterRCNN(cv.models.FasterRCNN):
  26. def __init__(self,
  27. num_classes=81,
  28. backbone='ResNet50',
  29. with_fpn=True,
  30. aspect_ratios=[0.5, 1.0, 2.0],
  31. anchor_sizes=[32, 64, 128, 256, 512],
  32. with_dcn=None,
  33. rpn_cls_loss=None,
  34. rpn_focal_loss_alpha=None,
  35. rpn_focal_loss_gamma=None,
  36. rcnn_bbox_loss=None,
  37. rcnn_nms=None,
  38. keep_top_k=100,
  39. nms_threshold=0.5,
  40. score_threshold=0.05,
  41. softnms_sigma=None,
  42. bbox_assigner=None,
  43. fpn_num_channels=256,
  44. input_channel=None,
  45. rpn_batch_size_per_im=256,
  46. rpn_fg_fraction=0.5,
  47. test_pre_nms_top_n=None,
  48. test_post_nms_top_n=1000):
  49. if with_dcn is not None:
  50. logging.warning(
  51. "`with_dcn` is deprecated in PaddleX 2.0 and won't take effect. Defaults to False."
  52. )
  53. if rpn_cls_loss is not None:
  54. logging.warning(
  55. "`rpn_cls_loss` is deprecated in PaddleX 2.0 and won't take effect. "
  56. "Defaults to 'SigmoidCrossEntropy'.")
  57. if rpn_focal_loss_alpha is not None or rpn_focal_loss_gamma is not None:
  58. logging.warning(
  59. "Focal loss is deprecated in PaddleX 2.0."
  60. " `rpn_focal_loss_alpha` and `rpn_focal_loss_gamma` won't take effect."
  61. )
  62. if rcnn_bbox_loss is not None:
  63. logging.warning(
  64. "`rcnn_bbox_loss` is deprecated in PaddleX 2.0 and won't take effect. "
  65. "Defaults to 'SmoothL1Loss'")
  66. if rcnn_nms is not None:
  67. logging.warning(
  68. "MultiClassSoftNMS is deprecated in PaddleX 2.0. "
  69. "`rcnn_nms` and `softnms_sigma` won't take effect. MultiClassNMS will be used by default"
  70. )
  71. if bbox_assigner is not None:
  72. logging.warning(
  73. "`bbox_assigner` is deprecated in PaddleX 2.0 and won't take effect. "
  74. "Defaults to 'BBoxAssigner'")
  75. if input_channel is not None:
  76. logging.warning(
  77. "`input_channel` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 3."
  78. )
  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. super(YOLOv3, self).__init__(
  114. num_classes=num_classes,
  115. backbone=backbone,
  116. anchors=anchors,
  117. anchor_masks=anchor_masks,
  118. ignore_threshold=ignore_threshold,
  119. nms_score_threshold=nms_score_threshold,
  120. nms_topk=nms_topk,
  121. nms_keep_topk=nms_keep_topk,
  122. nms_iou_threshold=nms_iou_threshold,
  123. label_smooth=label_smooth)
  124. self.train_random_shapes = train_random_shapes
  125. def _compose_batch_transform(self, transforms, mode='train'):
  126. if mode == 'train':
  127. default_batch_transforms = [
  128. _BatchPadding(pad_to_stride=-1), _NormalizeBox(),
  129. _PadBox(getattr(self, 'num_max_boxes', 50)), _BboxXYXY2XYWH(),
  130. _Gt2YoloTarget(
  131. anchor_masks=self.anchor_masks,
  132. anchors=self.anchors,
  133. downsample_ratios=getattr(self, 'downsample_ratios',
  134. [32, 16, 8]),
  135. num_classes=self.num_classes)
  136. ]
  137. else:
  138. default_batch_transforms = [_BatchPadding(pad_to_stride=-1)]
  139. if mode == 'eval' and self.metric == 'voc':
  140. collate_batch = False
  141. else:
  142. collate_batch = True
  143. custom_batch_transforms = []
  144. for i, op in enumerate(transforms.transforms):
  145. if isinstance(op, (BatchRandomResize, BatchRandomResizeByShort)):
  146. if mode != 'train':
  147. raise Exception(
  148. "{} cannot be present in the {} transforms. ".format(
  149. op.__class__.__name__, mode) +
  150. "Please check the {} transforms.".format(mode))
  151. custom_batch_transforms.insert(0, copy.deepcopy(op))
  152. random_shape_defined = True
  153. if not random_shape_defined:
  154. default_batch_transforms.insert(
  155. 0,
  156. BatchRandomResize(
  157. target_sizes=self.train_random_shapes, interp='RANDOM'))
  158. batch_transforms = BatchCompose(
  159. custom_batch_transforms + default_batch_transforms,
  160. collate_batch=collate_batch)
  161. return batch_transforms
  162. class PPYOLO(cv.models.PPYOLO):
  163. def __init__(
  164. self,
  165. num_classes=80,
  166. backbone='ResNet50_vd_ssld',
  167. with_dcn_v2=None,
  168. # YOLO Head
  169. anchors=None,
  170. anchor_masks=None,
  171. use_coord_conv=True,
  172. use_iou_aware=True,
  173. use_spp=True,
  174. use_drop_block=True,
  175. scale_x_y=1.05,
  176. # PPYOLO Loss
  177. ignore_threshold=0.7,
  178. label_smooth=False,
  179. use_iou_loss=True,
  180. # NMS
  181. use_matrix_nms=True,
  182. nms_score_threshold=0.01,
  183. nms_topk=1000,
  184. nms_keep_topk=100,
  185. nms_iou_threshold=0.45,
  186. train_random_shapes=[
  187. 320, 352, 384, 416, 448, 480, 512, 544, 576, 608
  188. ],
  189. input_channel=None):
  190. if backbone == 'ResNet50_vd_ssld':
  191. backbone = 'ResNet50_vd_dcn'
  192. if with_dcn_v2 is not None:
  193. logging.warning(
  194. "`with_dcn_v2` is deprecated in PaddleX 2.0 and will not take effect. "
  195. "To use backbone with deformable convolutional networks, "
  196. "please specify in `backbone_name`. "
  197. "Currently the only backbone with dcn is 'ResNet50_vd_dcn'.")
  198. if train_random_shapes is not None:
  199. logging.warning(
  200. "`train_random_shapes` is deprecated in PaddleX 2.0 and won't take effect. "
  201. "To apply multi_scale training, please refer to paddlex.transforms.BatchRandomResize: "
  202. "'https://github.com/PaddlePaddle/PaddleX/blob/develop/dygraph/paddlex/cv/transforms/batch_operators.py#L53'"
  203. )
  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. for i, op in enumerate(transforms.transforms):
  247. if isinstance(op, (BatchRandomResize, BatchRandomResizeByShort)):
  248. if mode != 'train':
  249. raise Exception(
  250. "{} cannot be present in the {} transforms. ".format(
  251. op.__class__.__name__, mode) +
  252. "Please check the {} transforms.".format(mode))
  253. custom_batch_transforms.insert(0, copy.deepcopy(op))
  254. random_shape_defined = True
  255. if not random_shape_defined:
  256. default_batch_transforms.insert(
  257. 0,
  258. BatchRandomResize(
  259. target_sizes=self.train_random_shapes, interp='RANDOM'))
  260. batch_transforms = BatchCompose(
  261. custom_batch_transforms + default_batch_transforms,
  262. collate_batch=collate_batch)
  263. return batch_transforms