From b352ef88a8d9175504d7407fafa169eb0fbf28e7 Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Fri, 3 Mar 2023 10:15:47 +0800 Subject: [PATCH] add picodet ppyoloe_crn_s_300e_coco static training (#7859) --- ppdet/engine/export_utils.py | 40 +++++++++++++++++++ ppdet/modeling/assigners/atss_assigner.py | 7 ++-- .../assigners/task_aligned_assigner.py | 2 +- .../assigners/task_aligned_assigner_cr.py | 2 +- ppdet/modeling/heads/pico_head.py | 4 +- ppdet/modeling/heads/ppyoloe_contrast_head.py | 6 +-- ppdet/modeling/heads/ppyoloe_head.py | 29 +++++++------- ...et_s_320_coco_lcnet_train_infer_python.txt | 4 +- ...loe_crn_s_300e_coco_train_infer_python.txt | 2 +- 9 files changed, 69 insertions(+), 27 deletions(-) diff --git a/ppdet/engine/export_utils.py b/ppdet/engine/export_utils.py index 800c1faf4..882dd5af6 100644 --- a/ppdet/engine/export_utils.py +++ b/ppdet/engine/export_utils.py @@ -142,6 +142,46 @@ TO_STATIC_SPEC = { 'centerness4': paddle.static.InputSpec( name='centerness4', shape=[-1, 10, 10, 1], dtype='float32'), }], + 'picodet_s_320_coco_lcnet': [{ + 'im_id': paddle.static.InputSpec( + name='im_id', shape=[-1, 1], dtype='float32'), + 'is_crowd': paddle.static.InputSpec( + name='is_crowd', shape=[-1, -1, 1], dtype='float32'), + 'gt_class': paddle.static.InputSpec( + name='gt_class', shape=[-1, -1, 1], dtype='int32'), + 'gt_bbox': paddle.static.InputSpec( + name='gt_bbox', shape=[-1, -1, 4], dtype='float32'), + 'curr_iter': paddle.static.InputSpec( + name='curr_iter', shape=[-1], dtype='float32'), + 'image': paddle.static.InputSpec( + name='image', shape=[-1, 3, -1, -1], dtype='float32'), + 'im_shape': paddle.static.InputSpec( + name='im_shape', shape=[-1, 2], dtype='float32'), + 'scale_factor': paddle.static.InputSpec( + name='scale_factor', shape=[-1, 2], dtype='float32'), + 'pad_gt_mask': paddle.static.InputSpec( + name='pad_gt_mask', shape=[-1, -1, 1], dtype='float32'), + }], + 'ppyoloe_crn_s_300e_coco': [{ + 'im_id': paddle.static.InputSpec( + name='im_id', shape=[-1, 1], dtype='float32'), + 'is_crowd': paddle.static.InputSpec( + name='is_crowd', shape=[-1, -1, 1], dtype='float32'), + 'gt_class': paddle.static.InputSpec( + name='gt_class', shape=[-1, -1, 1], dtype='int32'), + 'gt_bbox': paddle.static.InputSpec( + name='gt_bbox', shape=[-1, -1, 4], dtype='float32'), + 'curr_iter': paddle.static.InputSpec( + name='curr_iter', shape=[-1], dtype='float32'), + 'image': paddle.static.InputSpec( + name='image', shape=[-1, 3, -1, -1], dtype='float32'), + 'im_shape': paddle.static.InputSpec( + name='im_shape', shape=[-1, 2], dtype='float32'), + 'scale_factor': paddle.static.InputSpec( + name='scale_factor', shape=[-1, 2], dtype='float32'), + 'pad_gt_mask': paddle.static.InputSpec( + name='pad_gt_mask', shape=[-1, -1, 1], dtype='float32'), + }], } diff --git a/ppdet/modeling/assigners/atss_assigner.py b/ppdet/modeling/assigners/atss_assigner.py index a1e753c94..f1aae2beb 100644 --- a/ppdet/modeling/assigners/atss_assigner.py +++ b/ppdet/modeling/assigners/atss_assigner.py @@ -169,8 +169,9 @@ class ATSSAssigner(nn.Layer): # the one with the highest iou will be selected. mask_positive_sum = mask_positive.sum(axis=-2) if mask_positive_sum.max() > 1: - mask_multiple_gts = (mask_positive_sum.unsqueeze(1) > 1).tile( - [1, num_max_boxes, 1]) + mask_multiple_gts = ( + mask_positive_sum.unsqueeze(1) > 1).astype('int32').tile( + [1, num_max_boxes, 1]).astype('bool') if self.sm_use: is_max_iou = compute_max_iou_anchor(ious * mask_positive) else: @@ -221,4 +222,4 @@ class ATSSAssigner(nn.Layer): paddle.zeros_like(gather_scores)) assigned_scores *= gather_scores.unsqueeze(-1) - return assigned_labels, assigned_bboxes, assigned_scores, mask_positive + return assigned_labels, assigned_bboxes, assigned_scores diff --git a/ppdet/modeling/assigners/task_aligned_assigner.py b/ppdet/modeling/assigners/task_aligned_assigner.py index 5a756fa67..23af79439 100644 --- a/ppdet/modeling/assigners/task_aligned_assigner.py +++ b/ppdet/modeling/assigners/task_aligned_assigner.py @@ -190,4 +190,4 @@ class TaskAlignedAssigner(nn.Layer): alignment_metrics = alignment_metrics.max(-2).unsqueeze(-1) assigned_scores = assigned_scores * alignment_metrics - return assigned_labels, assigned_bboxes, assigned_scores, mask_positive + return assigned_labels, assigned_bboxes, assigned_scores diff --git a/ppdet/modeling/assigners/task_aligned_assigner_cr.py b/ppdet/modeling/assigners/task_aligned_assigner_cr.py index 4558d6e8e..5c5097604 100644 --- a/ppdet/modeling/assigners/task_aligned_assigner_cr.py +++ b/ppdet/modeling/assigners/task_aligned_assigner_cr.py @@ -178,4 +178,4 @@ class TaskAlignedAssigner_CR(nn.Layer): alignment_metrics = alignment_metrics.max(-2).unsqueeze(-1) assigned_scores = assigned_scores * alignment_metrics - return assigned_labels, assigned_bboxes, assigned_scores, mask_positive + return assigned_labels, assigned_bboxes, assigned_scores diff --git a/ppdet/modeling/heads/pico_head.py b/ppdet/modeling/heads/pico_head.py index e52322399..adcd05fc6 100644 --- a/ppdet/modeling/heads/pico_head.py +++ b/ppdet/modeling/heads/pico_head.py @@ -651,7 +651,7 @@ class PicoHeadV2(GFLHead): # label assignment if gt_meta['epoch_id'] < self.static_assigner_epoch: - assigned_labels, assigned_bboxes, assigned_scores, _ = self.static_assigner( + assigned_labels, assigned_bboxes, assigned_scores = self.static_assigner( anchors, num_anchors_list, gt_labels, @@ -662,7 +662,7 @@ class PicoHeadV2(GFLHead): pred_bboxes=pred_bboxes.detach() * stride_tensor_list) else: - assigned_labels, assigned_bboxes, assigned_scores, _ = self.assigner( + assigned_labels, assigned_bboxes, assigned_scores = self.assigner( pred_scores.detach(), pred_bboxes.detach() * stride_tensor_list, centers, diff --git a/ppdet/modeling/heads/ppyoloe_contrast_head.py b/ppdet/modeling/heads/ppyoloe_contrast_head.py index 3b8e26e63..8732c2ce3 100644 --- a/ppdet/modeling/heads/ppyoloe_contrast_head.py +++ b/ppdet/modeling/heads/ppyoloe_contrast_head.py @@ -121,7 +121,7 @@ class PPYOLOEContrastHead(PPYOLOEHead): pad_gt_mask = gt_meta['pad_gt_mask'] # label assignment if gt_meta['epoch_id'] < self.static_assigner_epoch: - assigned_labels, assigned_bboxes, assigned_scores, _ = \ + assigned_labels, assigned_bboxes, assigned_scores = \ self.static_assigner( anchors, num_anchors_list, @@ -133,7 +133,7 @@ class PPYOLOEContrastHead(PPYOLOEHead): alpha_l = 0.25 else: if self.sm_use: - assigned_labels, assigned_bboxes, assigned_scores, _ = \ + assigned_labels, assigned_bboxes, assigned_scores = \ self.assigner( pred_scores.detach(), pred_bboxes.detach() * stride_tensor, @@ -144,7 +144,7 @@ class PPYOLOEContrastHead(PPYOLOEHead): pad_gt_mask, bg_index=self.num_classes) else: - assigned_labels, assigned_bboxes, assigned_scores, _ = \ + assigned_labels, assigned_bboxes, assigned_scores = \ self.assigner( pred_scores.detach(), pred_bboxes.detach() * stride_tensor, diff --git a/ppdet/modeling/heads/ppyoloe_head.py b/ppdet/modeling/heads/ppyoloe_head.py index 261c0c493..80f1bc479 100644 --- a/ppdet/modeling/heads/ppyoloe_head.py +++ b/ppdet/modeling/heads/ppyoloe_head.py @@ -337,7 +337,8 @@ class PPYOLOEHead(nn.Layer): # pos/neg loss if num_pos > 0: # l1 + iou - bbox_mask = mask_positive.unsqueeze(-1).tile([1, 1, 4]) + bbox_mask = mask_positive.astype('int32').unsqueeze(-1).tile( + [1, 1, 4]).astype('bool') pred_bboxes_pos = paddle.masked_select(pred_bboxes, bbox_mask).reshape([-1, 4]) assigned_bboxes_pos = paddle.masked_select( @@ -351,8 +352,8 @@ class PPYOLOEHead(nn.Layer): assigned_bboxes_pos) * bbox_weight loss_iou = loss_iou.sum() / assigned_scores_sum - dist_mask = mask_positive.unsqueeze(-1).tile( - [1, 1, self.reg_channels * 4]) + dist_mask = mask_positive.unsqueeze(-1).astype('int32').tile( + [1, 1, self.reg_channels * 4]).astype('bool') pred_dist_pos = paddle.masked_select( pred_dist, dist_mask).reshape([-1, 4, self.reg_channels]) assigned_ltrb = self._bbox2distance(anchor_points, assigned_bboxes) @@ -387,7 +388,7 @@ class PPYOLOEHead(nn.Layer): pad_gt_mask = gt_meta['pad_gt_mask'] # label assignment if gt_meta['epoch_id'] < self.static_assigner_epoch: - assigned_labels, assigned_bboxes, assigned_scores, mask_positive = \ + assigned_labels, assigned_bboxes, assigned_scores = \ self.static_assigner( anchors, num_anchors_list, @@ -400,7 +401,7 @@ class PPYOLOEHead(nn.Layer): else: if self.sm_use: # only used in smalldet of PPYOLOE-SOD model - assigned_labels, assigned_bboxes, assigned_scores, mask_positive = \ + assigned_labels, assigned_bboxes, assigned_scores = \ self.assigner( pred_scores.detach(), pred_bboxes.detach() * stride_tensor, @@ -413,7 +414,7 @@ class PPYOLOEHead(nn.Layer): else: if aux_pred is None: if not hasattr(self, "assigned_labels"): - assigned_labels, assigned_bboxes, assigned_scores, mask_positive = \ + assigned_labels, assigned_bboxes, assigned_scores = \ self.assigner( pred_scores.detach(), pred_bboxes.detach() * stride_tensor, @@ -427,15 +428,15 @@ class PPYOLOEHead(nn.Layer): self.assigned_labels = assigned_labels self.assigned_bboxes = assigned_bboxes self.assigned_scores = assigned_scores - self.mask_positive = mask_positive + else: # only used in distill assigned_labels = self.assigned_labels assigned_bboxes = self.assigned_bboxes assigned_scores = self.assigned_scores - mask_positive = self.mask_positive + else: - assigned_labels, assigned_bboxes, assigned_scores, mask_positive = \ + assigned_labels, assigned_bboxes, assigned_scores = \ self.assigner( pred_scores_aux.detach(), pred_bboxes_aux.detach() * stride_tensor, @@ -451,14 +452,12 @@ class PPYOLOEHead(nn.Layer): assign_out_dict = self.get_loss_from_assign( pred_scores, pred_distri, pred_bboxes, anchor_points_s, - assigned_labels, assigned_bboxes, assigned_scores, mask_positive, - alpha_l) + assigned_labels, assigned_bboxes, assigned_scores, alpha_l) if aux_pred is not None: assign_out_dict_aux = self.get_loss_from_assign( aux_pred[0], aux_pred[1], pred_bboxes_aux, anchor_points_s, - assigned_labels, assigned_bboxes, assigned_scores, - mask_positive, alpha_l) + assigned_labels, assigned_bboxes, assigned_scores, alpha_l) loss = {} for key in assign_out_dict.keys(): loss[key] = assign_out_dict[key] + assign_out_dict_aux[key] @@ -469,7 +468,7 @@ class PPYOLOEHead(nn.Layer): def get_loss_from_assign(self, pred_scores, pred_distri, pred_bboxes, anchor_points_s, assigned_labels, assigned_bboxes, - assigned_scores, mask_positive, alpha_l): + assigned_scores, alpha_l): # cls loss if self.use_varifocal_loss: one_hot_label = F.one_hot(assigned_labels, @@ -490,7 +489,7 @@ class PPYOLOEHead(nn.Layer): self.distill_pairs['pred_cls_scores'] = pred_scores self.distill_pairs['pos_num'] = assigned_scores_sum self.distill_pairs['assigned_scores'] = assigned_scores - self.distill_pairs['mask_positive'] = mask_positive + one_hot_label = F.one_hot(assigned_labels, self.num_classes + 1)[..., :-1] self.distill_pairs['target_labels'] = one_hot_label diff --git a/test_tipc/configs/picodet/picodet_s_320_coco_lcnet_train_infer_python.txt b/test_tipc/configs/picodet/picodet_s_320_coco_lcnet_train_infer_python.txt index f51595a9e..57e7e3c3c 100644 --- a/test_tipc/configs/picodet/picodet_s_320_coco_lcnet_train_infer_python.txt +++ b/test_tipc/configs/picodet/picodet_s_320_coco_lcnet_train_infer_python.txt @@ -57,4 +57,6 @@ repeat:25 --profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile flags:null ===========================infer_benchmark_params=========================== -numpy_infer_input:3x320x320_2.npy \ No newline at end of file +numpy_infer_input:3x320x320_2.npy +===========================to_static_train_benchmark_params=========================== +to_static_train:--to_static \ No newline at end of file diff --git a/test_tipc/configs/ppyoloe/ppyoloe_crn_s_300e_coco_train_infer_python.txt b/test_tipc/configs/ppyoloe/ppyoloe_crn_s_300e_coco_train_infer_python.txt index 0694895ea..19fa1673b 100644 --- a/test_tipc/configs/ppyoloe/ppyoloe_crn_s_300e_coco_train_infer_python.txt +++ b/test_tipc/configs/ppyoloe/ppyoloe_crn_s_300e_coco_train_infer_python.txt @@ -57,4 +57,4 @@ repeat:12 --profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile flags:null ===========================infer_benchmark_params=========================== -numpy_infer_input:3x640x640_2.npy \ No newline at end of file +numpy_infer_input:3x640x640_2.npy -- GitLab