From 58f83f6621acbd2140a2578936fc1d07a597fd98 Mon Sep 17 00:00:00 2001 From: yuedongli1 Date: Thu, 7 Sep 2023 15:53:06 +0800 Subject: [PATCH] fit new COCODataset --- GETTING_STARTED.md | 2 +- configs/yolov3/hyp.scratch.yaml | 16 ++++-- configs/yolov4/hyp.scratch.yaml | 16 ++++-- configs/yolov5/hyp.scratch-high.yaml | 18 ++++-- configs/yolov5/hyp.scratch-low.yaml | 11 ++-- configs/yolov7/hyp.scratch.p5.yaml | 16 ++++-- configs/yolov7/hyp.scratch.p6.yaml | 16 ++++-- configs/yolov7/hyp.scratch.tiny.yaml | 16 ++++-- configs/yolov8/hyp.scratch.high.yaml | 21 ++++--- configs/yolov8/hyp.scratch.low.yaml | 14 ++--- configs/yolov8/hyp.scratch.med.yaml | 21 ++++--- configs/yolox/hyp.scratch.yaml | 15 +++-- mindyolo/data/dataset.py | 82 ++++++++++++++++------------ mindyolo/data/utils.py | 6 +- 14 files changed, 164 insertions(+), 106 deletions(-) diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index d3b37008..363aa1f9 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -64,7 +64,7 @@ to understand their behavior. Some common arguments are: ``` * To evaluate a model's performance 8 NPUs/GPUs: ``` - mpirun --allow-run-as-root -n 8 python test.py --config ./configs/yolov7/yolov7.yaml --weight /path_to_ckpt/WEIGHT.ckpt --is_parallel True ``` + mpirun --allow-run-as-root -n 8 python test.py --config ./configs/yolov7/yolov7.yaml --weight /path_to_ckpt/WEIGHT.ckpt --is_parallel True ``` *Notes: (1) The default hyper-parameter is used for 8-card training, and some parameters need to be adjusted in the case of a single card. (2) The default device is Ascend, and you can modify it by specifying 'device_target' as Ascend/GPU/CPU, as these are currently supported.* * For more options, see `train/test.py -h`. diff --git a/configs/yolov3/hyp.scratch.yaml b/configs/yolov3/hyp.scratch.yaml index 4b70bff2..c9ce0077 100644 --- a/configs/yolov3/hyp.scratch.yaml +++ b/configs/yolov3/hyp.scratch.yaml @@ -29,19 +29,23 @@ data: num_parallel_workers: 4 train_transforms: - - { func_name: mosaic, prob: 1.0, mosaic9_prob: 0.0, translate: 0.1, scale: 0.9 } - - { func_name: mixup, prob: 0.1, alpha: 8.0, beta: 8.0, needed_mosaic: True } + - { func_name: mosaic, prob: 1.0 } + - { func_name: resample_segments } + - { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.9, shear: 0.0 } + - { func_name: mixup, alpha: 8.0, beta: 8.0, prob: 0.1, pre_transform: [ + { func_name: mosaic, prob: 1.0 }, + { func_name: resample_segments }, + { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.9, shear: 0.0 }, ] + } - { func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4 } - - { func_name: label_norm, xyxy2xywh_: True } - { func_name: albumentations } - { func_name: fliplr, prob: 0.5 } + - { func_name: label_norm, xyxy2xywh_: True } - { func_name: label_pad, padding_size: 160, padding_value: -1 } - { func_name: image_norm, scale: 255. } - { func_name: image_transpose, bgr2rgb: True, hwc2chw: True } test_transforms: - - { func_name: letterbox, scaleup: False } - - { func_name: label_norm, xyxy2xywh_: True } - - { func_name: label_pad, padding_size: 160, padding_value: -1 } + - { func_name: letterbox, scaleup: False, only_image: True } - { func_name: image_norm, scale: 255. } - { func_name: image_transpose, bgr2rgb: True, hwc2chw: True } diff --git a/configs/yolov4/hyp.scratch.yaml b/configs/yolov4/hyp.scratch.yaml index 9c29f11d..0e24de88 100644 --- a/configs/yolov4/hyp.scratch.yaml +++ b/configs/yolov4/hyp.scratch.yaml @@ -26,19 +26,23 @@ data: num_parallel_workers: 4 train_transforms: - - { func_name: mosaic, prob: 1.0, mosaic9_prob: 0.0, translate: 0.1, scale: 0.9 } - - { func_name: mixup, prob: 0.1, alpha: 8.0, beta: 8.0, needed_mosaic: True } + - { func_name: mosaic, prob: 1.0 } + - { func_name: resample_segments } + - { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.9, shear: 0.0 } + - { func_name: mixup, alpha: 8.0, beta: 8.0, prob: 0.1, pre_transform: [ + { func_name: mosaic, prob: 1.0 }, + { func_name: resample_segments }, + { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.9, shear: 0.0 }, ] + } - { func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4 } - - { func_name: label_norm, xyxy2xywh_: True } - { func_name: albumentations } - { func_name: fliplr, prob: 0.5 } + - { func_name: label_norm, xyxy2xywh_: True } - { func_name: label_pad, padding_size: 160, padding_value: -1 } - { func_name: image_norm, scale: 255. } - { func_name: image_transpose, bgr2rgb: True, hwc2chw: True } test_transforms: - - { func_name: letterbox, scaleup: False } - - { func_name: label_norm, xyxy2xywh_: True } - - { func_name: label_pad, padding_size: 160, padding_value: -1 } + - { func_name: letterbox, scaleup: False, only_image: True } - { func_name: image_norm, scale: 255. } - { func_name: image_transpose, bgr2rgb: True, hwc2chw: True } diff --git a/configs/yolov5/hyp.scratch-high.yaml b/configs/yolov5/hyp.scratch-high.yaml index 2b87bfef..ef7de65c 100644 --- a/configs/yolov5/hyp.scratch-high.yaml +++ b/configs/yolov5/hyp.scratch-high.yaml @@ -33,19 +33,25 @@ data: num_parallel_workers: 4 train_transforms: - - {func_name: mosaic, prob: 1.0, mosaic9_prob: 0.0, translate: 0.1, scale: 0.9, copy_paste_prob: 0.1} - - {func_name: mixup, prob: 0.1, alpha: 32.0, beta: 32.0, needed_mosaic: True} - - {func_name: label_norm, xyxy2xywh_: True} + - { func_name: mosaic, prob: 1.0 } + - { func_name: copy_paste, prob: 0.1 } + - { func_name: resample_segments } + - { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.9, shear: 0.0 } + - { func_name: mixup, alpha: 32.0, beta: 32.0, prob: 0.1, pre_transform: [ + { func_name: mosaic, prob: 1.0 }, + { func_name: copy_paste, prob: 0.1 }, + { func_name: resample_segments }, + { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.9, shear: 0.0 }, ] + } - {func_name: albumentations} - {func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4} - {func_name: fliplr, prob: 0.5} + - {func_name: label_norm, xyxy2xywh_: True} - {func_name: label_pad, padding_size: 160, padding_value: -1} - {func_name: image_norm, scale: 255.} - {func_name: image_transpose, bgr2rgb: True, hwc2chw: True} test_transforms: - - {func_name: letterbox, scaleup: False} - - {func_name: label_norm, xyxy2xywh_: True} - - {func_name: label_pad, padding_size: 160, padding_value: -1} + - {func_name: letterbox, scaleup: False, only_image: True} - {func_name: image_norm, scale: 255. } - {func_name: image_transpose, bgr2rgb: True, hwc2chw: True } diff --git a/configs/yolov5/hyp.scratch-low.yaml b/configs/yolov5/hyp.scratch-low.yaml index b777fcaa..ac0758e0 100644 --- a/configs/yolov5/hyp.scratch-low.yaml +++ b/configs/yolov5/hyp.scratch-low.yaml @@ -29,19 +29,18 @@ data: num_parallel_workers: 4 train_transforms: - - {func_name: mosaic, prob: 1.0, mosaic9_prob: 0.0, translate: 0.1, scale: 0.5} - - {func_name: mixup, prob: 0.0, alpha: 32.0, beta: 32.0, needed_mosaic: True} - - {func_name: label_norm, xyxy2xywh_: True} + - { func_name: mosaic, prob: 1.0 } + - { func_name: resample_segments } + - { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.5, shear: 0.0 } - {func_name: albumentations} - {func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4} - {func_name: fliplr, prob: 0.5} + - {func_name: label_norm, xyxy2xywh_: True} - {func_name: label_pad, padding_size: 160, padding_value: -1} - {func_name: image_norm, scale: 255.} - {func_name: image_transpose, bgr2rgb: True, hwc2chw: True} test_transforms: - - {func_name: letterbox, scaleup: False} - - {func_name: label_norm, xyxy2xywh_: True} - - {func_name: label_pad, padding_size: 160, padding_value: -1} + - {func_name: letterbox, scaleup: False, only_image: True} - {func_name: image_norm, scale: 255. } - {func_name: image_transpose, bgr2rgb: True, hwc2chw: True } diff --git a/configs/yolov7/hyp.scratch.p5.yaml b/configs/yolov7/hyp.scratch.p5.yaml index 1ae3f332..54dec978 100644 --- a/configs/yolov7/hyp.scratch.p5.yaml +++ b/configs/yolov7/hyp.scratch.p5.yaml @@ -28,19 +28,23 @@ data: num_parallel_workers: 4 train_transforms: - - {func_name: mosaic, prob: 1.0, mosaic9_prob: 0.2, translate: 0.2, scale: 0.9} - - {func_name: mixup, prob: 0.15, alpha: 8.0, beta: 8.0, needed_mosaic: True} + - { func_name: mosaic, prob: 1.0, mosaic9_prob: 0.2 } + - { func_name: resample_segments } + - { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.2, scale: 0.9, shear: 0.0 } + - { func_name: mixup, alpha: 8.0, beta: 8.0, prob: 0.15, pre_transform: [ + { func_name: mosaic, prob: 1.0, mosaic9_prob: 0.2 }, + { func_name: resample_segments }, + { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.2, scale: 0.9, shear: 0.0 }, ] + } - {func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4} - {func_name: pastein, prob: 0.15, num_sample: 30} - - {func_name: label_norm, xyxy2xywh_: True} - {func_name: fliplr, prob: 0.5} + - {func_name: label_norm, xyxy2xywh_: True} - {func_name: label_pad, padding_size: 160, padding_value: -1} - {func_name: image_norm, scale: 255.} - {func_name: image_transpose, bgr2rgb: True, hwc2chw: True} test_transforms: - - {func_name: letterbox, scaleup: False} - - {func_name: label_norm, xyxy2xywh_: True} - - {func_name: label_pad, padding_size: 160, padding_value: -1} + - {func_name: letterbox, scaleup: False, only_image: True} - {func_name: image_norm, scale: 255. } - {func_name: image_transpose, bgr2rgb: True, hwc2chw: True } diff --git a/configs/yolov7/hyp.scratch.p6.yaml b/configs/yolov7/hyp.scratch.p6.yaml index 331d3843..2ab423da 100644 --- a/configs/yolov7/hyp.scratch.p6.yaml +++ b/configs/yolov7/hyp.scratch.p6.yaml @@ -28,19 +28,23 @@ data: num_parallel_workers: 4 train_transforms: - - {func_name: mosaic, prob: 1.0, mosaic9_prob: 0.2, translate: 0.2, scale: 0.9} - - {func_name: mixup, prob: 0.15, alpha: 8.0, beta: 8.0, needed_mosaic: True} + - { func_name: mosaic, prob: 1.0, mosaic9_prob: 0.2 } + - { func_name: resample_segments } + - { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.2, scale: 0.9, shear: 0.0 } + - { func_name: mixup, alpha: 8.0, beta: 8.0, prob: 0.15, pre_transform: [ + { func_name: mosaic, prob: 1.0, mosaic9_prob: 0.2 }, + { func_name: resample_segments }, + { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.2, scale: 0.9, shear: 0.0 }, ] + } - {func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4} - {func_name: pastein, prob: 0.15, num_sample: 30} - - {func_name: label_norm, xyxy2xywh_: True} - {func_name: fliplr, prob: 0.5} + - {func_name: label_norm, xyxy2xywh_: True} - {func_name: label_pad, padding_size: 160, padding_value: -1} - {func_name: image_norm, scale: 255.} - {func_name: image_transpose, bgr2rgb: True, hwc2chw: True} test_transforms: - - {func_name: letterbox, scaleup: False} - - {func_name: label_norm, xyxy2xywh_: True} - - {func_name: label_pad, padding_size: 160, padding_value: -1} + - {func_name: letterbox, scaleup: False, only_image: True} - {func_name: image_norm, scale: 255. } - {func_name: image_transpose, bgr2rgb: True, hwc2chw: True } diff --git a/configs/yolov7/hyp.scratch.tiny.yaml b/configs/yolov7/hyp.scratch.tiny.yaml index 6c6321ce..b77ba4d4 100644 --- a/configs/yolov7/hyp.scratch.tiny.yaml +++ b/configs/yolov7/hyp.scratch.tiny.yaml @@ -28,19 +28,23 @@ data: num_parallel_workers: 4 train_transforms: - - {func_name: mosaic, prob: 1.0, mosaic9_prob: 0.2, translate: 0.1, scale: 0.5} - - {func_name: mixup, prob: 0.05, alpha: 8.0, beta: 8.0, needed_mosaic: True} + - { func_name: mosaic, prob: 1.0, mosaic9_prob: 0.2 } + - { func_name: resample_segments } + - { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.5, shear: 0.0 } + - { func_name: mixup, alpha: 8.0, beta: 8.0, prob: 0.05, pre_transform: [ + { func_name: mosaic, prob: 1.0, mosaic9_prob: 0.2 }, + { func_name: resample_segments }, + { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.5, shear: 0.0 }, ] + } - {func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4} - {func_name: pastein, prob: 0.05, num_sample: 30} - - {func_name: label_norm, xyxy2xywh_: True} - {func_name: fliplr, prob: 0.5} + - {func_name: label_norm, xyxy2xywh_: True} - {func_name: label_pad, padding_size: 160, padding_value: -1} - {func_name: image_norm, scale: 255.} - {func_name: image_transpose, bgr2rgb: True, hwc2chw: True} test_transforms: - - {func_name: letterbox, scaleup: False} - - {func_name: label_norm, xyxy2xywh_: True} - - {func_name: label_pad, padding_size: 160, padding_value: -1} + - {func_name: letterbox, scaleup: False, only_image: True} - {func_name: image_norm, scale: 255. } - {func_name: image_transpose, bgr2rgb: True, hwc2chw: True } diff --git a/configs/yolov8/hyp.scratch.high.yaml b/configs/yolov8/hyp.scratch.high.yaml index 17f43a67..1d66833b 100644 --- a/configs/yolov8/hyp.scratch.high.yaml +++ b/configs/yolov8/hyp.scratch.high.yaml @@ -28,23 +28,32 @@ data: stage_epochs: [ 490, 10 ], trans_list: [ [ - {func_name: mosaic, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.9, shear: 0.0, copy_paste_prob: 0.3}, - {func_name: mixup, prob: 0.15, alpha: 32.0, beta: 32.0, needed_mosaic: True}, - {func_name: label_norm, xyxy2xywh_: True}, + { func_name: mosaic, prob: 1.0 }, + { func_name: copy_paste, prob: 0.3 }, + {func_name: resample_segments}, + { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.9, shear: 0.0 }, + { func_name: mixup, alpha: 32.0, beta: 32.0, prob: 0.15, pre_transform: [ + { func_name: mosaic, prob: 1.0 }, + { func_name: copy_paste, prob: 0.3 }, + { func_name: resample_segments }, + { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.9, shear: 0.0 }, ] + }, {func_name: albumentations}, {func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4}, {func_name: fliplr, prob: 0.5}, + {func_name: label_norm, xyxy2xywh_: True}, {func_name: label_pad, padding_size: 160, padding_value: -1}, {func_name: image_norm, scale: 255.}, {func_name: image_transpose, bgr2rgb: True, hwc2chw: True} ], [ {func_name: letterbox, scaleup: True}, + {func_name: resample_segments}, {func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.9, shear: 0.0}, - {func_name: label_norm, xyxy2xywh_: True}, {func_name: albumentations}, {func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4}, {func_name: fliplr, prob: 0.5}, + {func_name: label_norm, xyxy2xywh_: True}, {func_name: label_pad, padding_size: 160, padding_value: -1}, {func_name: image_norm, scale: 255.}, {func_name: image_transpose, bgr2rgb: True, hwc2chw: True} @@ -52,9 +61,7 @@ data: } test_transforms: [ - {func_name: letterbox, scaleup: False}, - {func_name: label_norm, xyxy2xywh_: True}, - {func_name: label_pad, padding_size: 160, padding_value: -1}, + {func_name: letterbox, scaleup: False, only_image: True}, {func_name: image_norm, scale: 255.}, {func_name: image_transpose, bgr2rgb: True, hwc2chw: True} ] diff --git a/configs/yolov8/hyp.scratch.low.yaml b/configs/yolov8/hyp.scratch.low.yaml index f075ec50..ba309b96 100644 --- a/configs/yolov8/hyp.scratch.low.yaml +++ b/configs/yolov8/hyp.scratch.low.yaml @@ -30,23 +30,25 @@ data: stage_epochs: [ 490, 10 ], trans_list: [ [ - {func_name: mosaic, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.5, shear: 0.0}, - {func_name: mixup, prob: 0.0, alpha: 32.0, beta: 32.0, needed_mosaic: True}, - {func_name: label_norm, xyxy2xywh_: True}, + { func_name: mosaic, prob: 1.0 }, + { func_name: resample_segments }, + { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.5, shear: 0.0 }, {func_name: albumentations}, {func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4}, {func_name: fliplr, prob: 0.5}, + {func_name: label_norm, xyxy2xywh_: True}, {func_name: label_pad, padding_size: 160, padding_value: -1}, {func_name: image_norm, scale: 255.}, {func_name: image_transpose, bgr2rgb: True, hwc2chw: True} ], [ {func_name: letterbox, scaleup: True}, + {func_name: resample_segments}, {func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.5, shear: 0.0}, - {func_name: label_norm, xyxy2xywh_: True}, {func_name: albumentations}, {func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4}, {func_name: fliplr, prob: 0.5}, + {func_name: label_norm, xyxy2xywh_: True}, {func_name: label_pad, padding_size: 160, padding_value: -1}, {func_name: image_norm, scale: 255.}, {func_name: image_transpose, bgr2rgb: True, hwc2chw: True} @@ -54,9 +56,7 @@ data: } test_transforms: [ - {func_name: letterbox, scaleup: False}, - {func_name: label_norm, xyxy2xywh_: True}, - {func_name: label_pad, padding_size: 160, padding_value: -1}, + {func_name: letterbox, scaleup: False, only_image: True}, {func_name: image_norm, scale: 255.}, {func_name: image_transpose, bgr2rgb: True, hwc2chw: True} ] diff --git a/configs/yolov8/hyp.scratch.med.yaml b/configs/yolov8/hyp.scratch.med.yaml index 9d1f69d1..e810f8b9 100644 --- a/configs/yolov8/hyp.scratch.med.yaml +++ b/configs/yolov8/hyp.scratch.med.yaml @@ -28,23 +28,32 @@ data: stage_epochs: [ 490, 10 ], trans_list: [ [ - {func_name: mosaic, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.9, shear: 0.0, copy_paste_prob: 0.1}, - {func_name: mixup, prob: 0.1, alpha: 32.0, beta: 32.0, needed_mosaic: True}, - {func_name: label_norm, xyxy2xywh_: True}, + { func_name: mosaic, prob: 1.0 }, + { func_name: copy_paste, prob: 0.1 }, + {func_name: resample_segments}, + { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.9, shear: 0.0 }, + { func_name: mixup, alpha: 32.0, beta: 32.0, prob: 0.1, pre_transform: [ + { func_name: mosaic, prob: 1.0 }, + { func_name: copy_paste, prob: 0.1 }, + { func_name: resample_segments }, + { func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.9, shear: 0.0 }, ] + }, {func_name: albumentations}, {func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4}, {func_name: fliplr, prob: 0.5}, + {func_name: label_norm, xyxy2xywh_: True}, {func_name: label_pad, padding_size: 160, padding_value: -1}, {func_name: image_norm, scale: 255.}, {func_name: image_transpose, bgr2rgb: True, hwc2chw: True} ], [ {func_name: letterbox, scaleup: True}, + {func_name: resample_segments}, {func_name: random_perspective, prob: 1.0, degrees: 0.0, translate: 0.1, scale: 0.9, shear: 0.0}, - {func_name: label_norm, xyxy2xywh_: True}, {func_name: albumentations}, {func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4}, {func_name: fliplr, prob: 0.5}, + {func_name: label_norm, xyxy2xywh_: True}, {func_name: label_pad, padding_size: 160, padding_value: -1}, {func_name: image_norm, scale: 255.}, {func_name: image_transpose, bgr2rgb: True, hwc2chw: True} @@ -52,9 +61,7 @@ data: } test_transforms: [ - {func_name: letterbox, scaleup: False}, - {func_name: label_norm, xyxy2xywh_: True}, - {func_name: label_pad, padding_size: 160, padding_value: -1}, + {func_name: letterbox, scaleup: False, only_image: True}, {func_name: image_norm, scale: 255.}, {func_name: image_transpose, bgr2rgb: True, hwc2chw: True} ] diff --git a/configs/yolox/hyp.scratch.yaml b/configs/yolox/hyp.scratch.yaml index 190d6058..f3567c15 100644 --- a/configs/yolox/hyp.scratch.yaml +++ b/configs/yolox/hyp.scratch.yaml @@ -39,20 +39,27 @@ data: stage_epochs: [ 285, 15 ], trans_list: [ [ - { func_name: mosaic, prob: 1.0, mosaic9_prob: 0.0, translate: 0.1, scale: 0.9, degrees: 10.0, shear: 2.0 }, - { func_name: mixup, prob: 1.0, alpha: 8.0, beta: 8.0, needed_mosaic: True }, + { func_name: mosaic, prob: 1.0 }, + { func_name: resample_segments }, + { func_name: random_perspective, prob: 1.0, degrees: 10.0, translate: 0.1, scale: 0.9, shear: 2.0 }, + { func_name: mixup, alpha: 8.0, beta: 8.0, prob: 1.0, pre_transform: [ + { func_name: mosaic, prob: 1.0 }, + { func_name: resample_segments }, + { func_name: random_perspective, prob: 1.0, degrees: 10.0, translate: 0.1, scale: 0.9, shear: 2.0 }, ] + }, { func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4 }, - { func_name: label_norm, xyxy2xywh_: True }, { func_name: fliplr, prob: 0.5 }, + { func_name: label_norm, xyxy2xywh_: True }, { func_name: label_pad, padding_size: 160, padding_value: -1 }, { func_name: image_norm, scale: 255. }, { func_name: image_transpose, bgr2rgb: True, hwc2chw: True }, ], [ { func_name: letterbox, scaleup: False }, + { func_name: resample_segments }, { func_name: hsv_augment, prob: 1.0, hgain: 0.015, sgain: 0.7, vgain: 0.4 }, - { func_name: label_norm, xyxy2xywh_: True }, { func_name: fliplr, prob: 0.5 }, + { func_name: label_norm, xyxy2xywh_: True }, { func_name: label_pad, padding_size: 160, padding_value: -1 }, { func_name: image_norm, scale: 255. }, { func_name: image_transpose, bgr2rgb: True, hwc2chw: True }, diff --git a/mindyolo/data/dataset.py b/mindyolo/data/dataset.py index f4537398..4026631c 100644 --- a/mindyolo/data/dataset.py +++ b/mindyolo/data/dataset.py @@ -641,17 +641,19 @@ def random_perspective( n = len(targets) if n: use_segments = len(segments) + new_bboxes = np.zeros((n, 4)) if use_segments: # warp segments - n, num = segments.shape[:2] - xy = np.ones((n * num, 3), dtype=segments.dtype) - segments = segments.reshape(-1, 2) - xy[:, :2] = segments - xy = xy @ M.T # transform - xy = xy[:, :2] / xy[:, 2:3] - segments = xy.reshape(n, -1, 2) - segments[..., 0] = segments[..., 0].clip(0, width) - segments[..., 1] = segments[..., 1].clip(0, height) - new_bboxes = np.stack([segment2box(xy) for xy in segments], 0) + point_num = segments[0].shape[0] + new_segments = np.zeros((n, point_num, 2)) + for i, segment in enumerate(segments): + xy = np.ones((len(segment), 3)) + xy[:, :2] = segment + xy = xy @ M.T # transform + xy = xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2] # perspective rescale or affine + + # clip + new_segments[i] = xy + new_bboxes[i] = segment2box(xy, width, height) else: # warp boxes xy = np.ones((n * 4, 3)) @@ -949,7 +951,7 @@ def fliplr(self, sample): return sample - def letterbox(self, sample, new_shape=None, xywhn2xyxy_=True, scaleup=False, color=(114, 114, 114)): + def letterbox(self, sample, new_shape=None, xywhn2xyxy_=True, scaleup=False, only_image=False, color=(114, 114, 114)): # Resize and pad image while meeting stride-multiple constraints if sample['bbox_format'] == 'ltrb': @@ -987,34 +989,42 @@ def letterbox(self, sample, new_shape=None, xywhn2xyxy_=True, scaleup=False, col left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) image = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border - # convert bboxes - if len(bboxes): - if xywhn2xyxy_: - bboxes = xywhn2xyxy(bboxes, r * w, r * h, padw=dw, padh=dh) - else: - bboxes *= r - bboxes[:, [0, 2]] += dw - bboxes[:, [1, 3]] += dh - - # convert segments - if 'segments' in sample: - segments, segment_format = sample['segments'], sample['segment_format'] - assert segment_format == 'polygon', f'The segment format should be polygon, but got {segment_format}' - assert isinstance(segments, np.ndarray), \ - f"LetterBox: segments type expect numpy.ndarray, but got {type(segments)}; " \ - f"maybe you should resample_segments before that." - - if len(segments): + if not only_image: + # convert bboxes + if len(bboxes): if xywhn2xyxy_: - segments[..., 0] *= w - segments[..., 1] *= h + bboxes = xywhn2xyxy(bboxes, r * w, r * h, padw=dw, padh=dh) else: - segments *= r - segments[..., 0] += dw - segments[..., 1] += dh - sample['segments'] = segments + bboxes *= r + bboxes[:, [0, 2]] += dw + bboxes[:, [1, 3]] += dh + sample['bboxes'] = bboxes + + # convert segments + if 'segments' in sample: + segments, segment_format = sample['segments'], sample['segment_format'] + assert segment_format == 'polygon', f'The segment format should be polygon, but got {segment_format}' + + if len(segments): + if isinstance(segments, np.ndarray): + if xywhn2xyxy_: + segments[..., 0] *= w + segments[..., 1] *= h + else: + segments *= r + segments[..., 0] += dw + segments[..., 1] += dh + elif isinstance(segments, list): + for segment in segments: + if xywhn2xyxy_: + segment[..., 0] *= w + segment[..., 1] *= h + else: + segment *= r + segment[..., 0] += dw + segment[..., 1] += dh + sample['segments'] = segments - sample['bboxes'] = bboxes sample['img'] = image sample['hw_scale'] = hw_scale sample['hw_pad'] = hw_pad diff --git a/mindyolo/data/utils.py b/mindyolo/data/utils.py index a0211f2c..2ac8ee24 100644 --- a/mindyolo/data/utils.py +++ b/mindyolo/data/utils.py @@ -115,10 +115,12 @@ def segments2boxes(segments): return xyxy2xywh(np.array(boxes)) # cls, xywh -def segment2box(segment): +def segment2box(segment, width=640, height=640): # Convert 1 segment label to 1 box label, applying inside-image constraint, i.e. (xy1, xy2, ...) to (xyxy) x, y = segment.T # segment xy - return np.array([x.min(), y.min(), x.max(), y.max()]) if any(x) else np.zeros(4) # xyxy + inside = (x >= 0) & (y >= 0) & (x <= width) & (y <= height) + x, y = x[inside], y[inside] + return np.array([x.min(), y.min(), x.max(), y.max()], dtype=segment.dtype) if any(x) else np.zeros(4, dtype=segment.dtype) # xyxy def box_candidates(box1, box2, wh_thr=2, ar_thr=20, area_thr=0.1, eps=1e-16): # box1(4,n), box2(4,n)