mmyolo/tests/test_datasets/test_utils.py

134 lines
4.5 KiB
Python
Raw Normal View History

# Copyright (c) OpenMMLab. All rights reserved.
import unittest
import numpy as np
import torch
from mmdet.structures import DetDataSample
from mmdet.structures.bbox import HorizontalBoxes
2022-09-20 10:57:33 +08:00
from mmengine.structures import InstanceData
from mmyolo.datasets import BatchShapePolicy, yolov5_collate
def _rand_bboxes(rng, num_boxes, w, h):
cx, cy, bw, bh = rng.rand(num_boxes, 4).T
tl_x = ((cx * w) - (w * bw / 2)).clip(0, w)
tl_y = ((cy * h) - (h * bh / 2)).clip(0, h)
br_x = ((cx * w) + (w * bw / 2)).clip(0, w)
br_y = ((cy * h) + (h * bh / 2)).clip(0, h)
bboxes = np.vstack([tl_x, tl_y, br_x, br_y]).T
return bboxes
class TestYOLOv5Collate(unittest.TestCase):
def test_yolov5_collate(self):
rng = np.random.RandomState(0)
inputs = torch.randn((3, 10, 10))
data_samples = DetDataSample()
gt_instances = InstanceData()
bboxes = _rand_bboxes(rng, 4, 6, 8)
gt_instances.bboxes = HorizontalBoxes(bboxes, dtype=torch.float32)
labels = rng.randint(1, 2, size=len(bboxes))
gt_instances.labels = torch.LongTensor(labels)
data_samples.gt_instances = gt_instances
out = yolov5_collate([dict(inputs=inputs, data_samples=data_samples)])
self.assertIsInstance(out, dict)
self.assertTrue(out['inputs'].shape == (1, 3, 10, 10))
self.assertTrue(out['data_samples'].shape == (4, 6))
out = yolov5_collate([dict(inputs=inputs, data_samples=data_samples)] *
2)
self.assertIsInstance(out, dict)
self.assertTrue(out['inputs'].shape == (2, 3, 10, 10))
self.assertTrue(out['data_samples'].shape == (8, 6))
[Feature] Support PPYOLOE training (#259) * add ppyoloe backbone, neck * add ppyoloe test * add docstring * add ppyoloe m/l/x configfile * add ppyoloe_coco.py * rename config * add typehint * format code; add ut * add datapre * add datapre * add ppyoloe datapre * add ppyoloe datapre * add ppyoloe datapre * reproduce coco v0.1 * add ut * add ut, docstring * fix transforms bug * use mmdet dfloss * add non plus model config * add non plus model config * fix * add ut * produce coco v0.2 * fix config * fix config * fix eps and transforms bug * add ema * fix resize * fix transforms.py * fix transforms.py * fix transforms.py * old version * old version * old version * old version * old version * old version * fix stride loss error * add INTER_LANCZOS4 * fix crop bug * init commit * format code * format code * bgr transforms.py * add typehint and doc in transforms.py * 继承新版yolov6head写法,删除不必要的注释 * fix transforms var name bug * bbox decode use stridetensor insted of priors * add headmodule todo * add ppyoloe README.md * add ppyoloe README.md * Update tests/test_datasets/test_transforms/test_transforms.py Co-authored-by: Range King <RangeKingHZ@gmail.com> * Update tests/test_datasets/test_transforms/test_transforms.py Co-authored-by: Range King <RangeKingHZ@gmail.com> * save ckpt last 10 epochs * save_best ckpt * del ppyoloe collate * change name of ppyoloebatchrandomresize * add the reason for rewritten PPYOLOEDetDataPreprocessor * solve ppyoloerandomresize name error * rm PPYOLOERandomExpand * rm l1 loss * rm yolov6 loss_obj * add difference between yolov6 and ppyoloe * add reason for rewrite paramscheduler * change proj init way * fix error * rm proj_conv in pth * format code * add load_from * update * support fast training * add pretrained model url * update * add pretrained model url * fix error * add imagenet model convert and use init_cfg to init backbone * add plus model pretrain model * add ut * add ut * fix ut * fix withstride bug * cat in yolov5_collate * merge * fix typehint * update readme * add reason for gap * fix log in README.md * rollback yolov6 * change inherit * fix ut * fix ut Co-authored-by: Range King <RangeKingHZ@gmail.com> Co-authored-by: hha <1286304229@qq.com> Co-authored-by: huanghaian <huanghaian@sensetime.com>
2023-01-06 15:54:39 +08:00
def test_yolov5_collate_with_multi_scale(self):
rng = np.random.RandomState(0)
inputs = torch.randn((3, 10, 10))
data_samples = DetDataSample()
gt_instances = InstanceData()
bboxes = _rand_bboxes(rng, 4, 6, 8)
gt_instances.bboxes = HorizontalBoxes(bboxes, dtype=torch.float32)
labels = rng.randint(1, 2, size=len(bboxes))
gt_instances.labels = torch.LongTensor(labels)
data_samples.gt_instances = gt_instances
out = yolov5_collate([dict(inputs=inputs, data_samples=data_samples)],
use_ms_training=True)
self.assertIsInstance(out, dict)
self.assertTrue(out['inputs'][0].shape == (3, 10, 10))
print(out['data_samples'].shape)
self.assertTrue(out['data_samples'].shape == (4, 6))
self.assertIsInstance(out['inputs'], list)
self.assertIsInstance(out['data_samples'], torch.Tensor)
out = yolov5_collate(
[dict(inputs=inputs, data_samples=data_samples)] * 2,
use_ms_training=True)
self.assertIsInstance(out, dict)
self.assertTrue(out['inputs'][0].shape == (3, 10, 10))
self.assertTrue(out['data_samples'].shape == (8, 6))
self.assertIsInstance(out['inputs'], list)
self.assertIsInstance(out['data_samples'], torch.Tensor)
class TestBatchShapePolicy(unittest.TestCase):
def test_batch_shape_policy(self):
src_data_infos = [{
'height': 20,
'width': 100,
}, {
'height': 11,
'width': 100,
}, {
'height': 21,
'width': 100,
}, {
'height': 30,
'width': 100,
}, {
'height': 10,
'width': 100,
}]
expected_data_infos = [{
'height': 10,
'width': 100,
'batch_shape': np.array([96, 672])
}, {
'height': 11,
'width': 100,
'batch_shape': np.array([96, 672])
}, {
'height': 20,
'width': 100,
'batch_shape': np.array([160, 672])
}, {
'height': 21,
'width': 100,
'batch_shape': np.array([160, 672])
}, {
'height': 30,
'width': 100,
'batch_shape': np.array([224, 672])
}]
batch_shapes_policy = BatchShapePolicy(batch_size=2)
out_data_infos = batch_shapes_policy(src_data_infos)
for i in range(5):
self.assertEqual(
(expected_data_infos[i]['height'],
expected_data_infos[i]['width']),
(out_data_infos[i]['height'], out_data_infos[i]['width']))
self.assertTrue(
np.allclose(expected_data_infos[i]['batch_shape'],
out_data_infos[i]['batch_shape']))