[Fix] fix random may generate same value (#85)

This commit is contained in:
liukuikun 2022-03-05 15:02:54 +08:00 committed by GitHub
parent 43dcc5648f
commit 11b38b12d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 98 deletions

View File

@ -25,27 +25,26 @@ class BaseDataElement:
types of ground truth labels or predictions. types of ground truth labels or predictions.
They are used as interfaces between different commopenets. They are used as interfaces between different commopenets.
The attributes in ``BaseDataElement`` are divided into two parts, The attributes in ``BaseDataElement`` are divided into two parts,
the ``metainfo`` and the ``data`` respectively. the ``metainfo`` and the ``data`` respectively.
- ``metainfo``: Usually contains the - ``metainfo``: Usually contains the
information about the image such as filename, information about the image such as filename,
image_shape, pad_shape, etc. The attributes can be accessed or image_shape, pad_shape, etc. The attributes can be accessed or
modified by dict-like or object-like operations, such as modified by dict-like or object-like operations, such as
``.``(for data access and modification) , ``in``, ``del``, ``.``(for data access and modification) , ``in``, ``del``,
``pop(str)``, ``get(str)``, ``metainfo_keys()``, ``pop(str)``, ``get(str)``, ``metainfo_keys()``,
``metainfo_values()``, ``metainfo_items()``, ``set_metainfo()``(for ``metainfo_values()``, ``metainfo_items()``, ``set_metainfo()``(for
set or change key-value pairs in metainfo). set or change key-value pairs in metainfo).
- ``data``: Annotations or model predictions are - ``data``: Annotations or model predictions are
stored. The attributes can be accessed or modified by stored. The attributes can be accessed or modified by
dict-like or object-like operations, such as dict-like or object-like operations, such as
``.`` , ``in``, ``del``, ``pop(str)`` ``get(str)``, ``data_keys()``, ``.`` , ``in``, ``del``, ``pop(str)`` ``get(str)``, ``data_keys()``,
``data_values()``, ``data_items()``. Users can also apply tensor-like ``data_values()``, ``data_items()``. Users can also apply tensor-like
methods to all obj:``torch.Tensor`` in the ``data_fileds``, methods to all obj:``torch.Tensor`` in the ``data_fileds``,
such as ``.cuda()``, ``.cpu()``, ``.numpy()``, , ``.to()`` such as ``.cuda()``, ``.cpu()``, ``.numpy()``, , ``.to()``
``to_tensor()``, ``.detach()``, ``.numpy()`` ``to_tensor()``, ``.detach()``, ``.numpy()``
Args: Args:
meta_info (dict, optional): A dict contains the meta information meta_info (dict, optional): A dict contains the meta information
@ -57,24 +56,24 @@ class BaseDataElement:
Examples: Examples:
>>> from mmengine.data import BaseDataElement >>> from mmengine.data import BaseDataElement
>>> gt_instances = BaseDataElement() >>> gt_instances = BaseDataElement()
>>> bboxes = torch.rand((5, 4)) >>> bboxes = torch.rand((5, 4))
>>> scores = torch.rand((5,)) >>> scores = torch.rand((5,))
>>> img_id = 0 >>> img_id = 0
>>> img_shape = (800, 1333) >>> img_shape = (800, 1333)
>>> gt_instances = BaseDataElement( >>> gt_instances = BaseDataElement(
metainfo=dict(img_id=img_id, img_shape=img_shape), ... metainfo=dict(img_id=img_id, img_shape=img_shape),
data=dict(bboxes=bboxes, scores=scores)) ... data=dict(bboxes=bboxes, scores=scores))
>>> gt_instances = BaseDataElement(dict(img_id=img_id, >>> gt_instances = BaseDataElement(dict(img_id=img_id,
img_shape=(H, W))) ... img_shape=(H, W)))
# new
>>> # new
>>> gt_instances1 = gt_instance.new( >>> gt_instances1 = gt_instance.new(
metainfo=dict(img_id=1, img_shape=(640, 640)), ... metainfo=dict(img_id=1, img_shape=(640, 640)),
data=dict(bboxes=torch.rand((5, 4)), ... data=dict(bboxes=torch.rand((5, 4)),
scores=torch.rand((5,)))) ... scores=torch.rand((5,))))
>>> gt_instances2 = gt_instances1.new() >>> gt_instances2 = gt_instances1.new()
# add and process property >>> # add and process property
>>> gt_instances = BaseDataElement() >>> gt_instances = BaseDataElement()
>>> gt_instances.set_metainfo(dict(img_id=9, img_shape=(100, 100)) >>> gt_instances.set_metainfo(dict(img_id=9, img_shape=(100, 100))
>>> assert 'img_shape' in gt_instances.metainfo_keys() >>> assert 'img_shape' in gt_instances.metainfo_keys()
@ -82,14 +81,12 @@ class BaseDataElement:
>>> assert 'img_shape' not in gt_instances.data_keys() >>> assert 'img_shape' not in gt_instances.data_keys()
>>> assert 'img_shape' in gt_instances.keys() >>> assert 'img_shape' in gt_instances.keys()
>>> print(gt_instances.img_shape) >>> print(gt_instances.img_shape)
>>> gt_instances.scores = torch.rand((5,)) >>> gt_instances.scores = torch.rand((5,))
>>> assert 'scores' in gt_instances.data_keys() >>> assert 'scores' in gt_instances.data_keys()
>>> assert 'scores' in gt_instances >>> assert 'scores' in gt_instances
>>> assert 'scores' in gt_instances.keys() >>> assert 'scores' in gt_instances.keys()
>>> assert 'scores' not in gt_instances.metainfo_keys() >>> assert 'scores' not in gt_instances.metainfo_keys()
>>> print(gt_instances.scores) >>> print(gt_instances.scores)
>>> gt_instances.bboxes = torch.rand((5, 4)) >>> gt_instances.bboxes = torch.rand((5, 4))
>>> assert 'bboxes' in gt_instances.data_keys() >>> assert 'bboxes' in gt_instances.data_keys()
>>> assert 'bboxes' in gt_instances >>> assert 'bboxes' in gt_instances
@ -97,14 +94,14 @@ class BaseDataElement:
>>> assert 'bboxes' not in gt_instances.metainfo_keys() >>> assert 'bboxes' not in gt_instances.metainfo_keys()
>>> print(gt_instances.bboxes) >>> print(gt_instances.bboxes)
# delete and change property >>> # delete and change property
>>> gt_instances = BaseDataElement( >>> gt_instances = BaseDataElement(
metainfo=dict(img_id=0, img_shape=(640, 640)) ... metainfo=dict(img_id=0, img_shape=(640, 640)),
data=dict(bboxes=torch.rand((6, 4)), scores=torch.rand((6,)))) ... data=dict(bboxes=torch.rand((6, 4)), scores=torch.rand((6,))))
>>> gt_instances.img_shape = (1280, 1280) >>> gt_instances.img_shape = (1280, 1280)
>>> gt_instances.img_shape # (1280, 1280) >>> gt_instances.img_shape # (1280, 1280)
>>> gt_instances.bboxes = gt_instances.bboxes * 2 >>> gt_instances.bboxes = gt_instances.bboxes * 2
>>> gt_instances.get('img_shape', None) # (640 640) >>> gt_instances.get('img_shape', None) # (640, 640)
>>> gt_instances.get('bboxes', None) # 6x4 tensor >>> gt_instances.get('bboxes', None) # 6x4 tensor
>>> del gt_instances.img_shape >>> del gt_instances.img_shape
>>> del gt_instances.bboxes >>> del gt_instances.bboxes
@ -113,18 +110,18 @@ class BaseDataElement:
>>> gt_instances.pop('img_shape', None) # None >>> gt_instances.pop('img_shape', None) # None
>>> gt_instances.pop('bboxes', None) # None >>> gt_instances.pop('bboxes', None) # None
# Tensor-like >>> # Tensor-like
>>> cuda_instances = gt_instances.cuda() >>> cuda_instances = gt_instances.cuda()
>>> cuda_instances = gt_instancess.to('cuda:0') >>> cuda_instances = gt_instancess.to('cuda:0')
>>> cpu_instances = cuda_instances.cpu() >>> cpu_instances = cuda_instances.cpu()
>>> cpu_instances = cuda_instances.to('cpu') >>> cpu_instances = cuda_instances.to('cpu')
>>> fp16_instances = cuda_instances.to( >>> fp16_instances = cuda_instances.to(
device=None, dtype=torch.float16, non_blocking=False, copy=False, ... device=None, dtype=torch.float16, non_blocking=False, copy=False,
memory_format=torch.preserve_format) ... memory_format=torch.preserve_format)
>>> cpu_instances = cuda_instances.detach() >>> cpu_instances = cuda_instances.detach()
>>> np_instances = cpu_instances.numpy() >>> np_instances = cpu_instances.numpy()
# print >>> # print
>>> img_meta = dict(img_shape=(800, 1196, 3), pad_shape=(800, 1216, 3)) >>> img_meta = dict(img_shape=(800, 1196, 3), pad_shape=(800, 1216, 3))
>>> instance_data = BaseDataElement(metainfo=img_meta) >>> instance_data = BaseDataElement(metainfo=img_meta)
>>> instance_data.det_labels = torch.LongTensor([0, 1, 2, 3]) >>> instance_data.det_labels = torch.LongTensor([0, 1, 2, 3])

View File

@ -54,55 +54,52 @@ class BaseDataSample:
Examples: Examples:
>>> from mmengine.data import BaseDataElement, BaseDataSample >>> from mmengine.data import BaseDataElement, BaseDataSample
>>> gt_instances = BaseDataSample() >>> gt_instances = BaseDataSample()
>>> bboxes = torch.rand((5, 4)) >>> bboxes = torch.rand((5, 4))
>>> scores = torch.rand((5,)) >>> scores = torch.rand((5,))
>>> img_id = 0 >>> img_id = 0
>>> img_shape = (800, 1333) >>> img_shape = (800, 1333)
>>> gt_instances = BaseDataElement( >>> gt_instances = BaseDataElement(
metainfo=dict(img_id=img_id, img_shape=img_shape), ... metainfo=dict(img_id=img_id, img_shape=img_shape),
data=dict(bboxes=bboxes, scores=scores)) ... data=dict(bboxes=bboxes, scores=scores))
>>> data = dict(gt_instances=gt_instances) >>> data = dict(gt_instances=gt_instances)
>>> sample = BaseDataSample( >>> sample = BaseDataSample(
metainfo=dict(img_id=img_id, img_shape=img_shape), ... metainfo=dict(img_id=img_id, img_shape=img_shape),
data=data) ... data=data)
>>> sample = BaseDataSample(dict(img_id=img_id, >>> sample = BaseDataSample(dict(img_id=img_id,
img_shape=(H, W))) ... img_shape=(H, W)))
# new
>>> # new
>>> data1 = dict(bboxes=torch.rand((5, 4)), >>> data1 = dict(bboxes=torch.rand((5, 4)),
scores=torch.rand((5,))) scores=torch.rand((5,)))
>>> metainfo1 = dict(img_id=1, img_shape=(640, 640)), >>> metainfo1 = dict(img_id=1, img_shape=(640, 640)),
>>> gt_instances1 = BaseDataElement( >>> gt_instances1 = BaseDataElement(
metainfo=metainfo1, ... metainfo=metainfo1,
data=data1) ... data=data1)
>>> sample1 = sample.new( >>> sample1 = sample.new(
metainfo=metainfo1 ... metainfo=metainfo1
data=dict(gt_instances1=gt_instances1)), ... data=dict(gt_instances1=gt_instances1)),
>>> gt_instances2 = gt_instances1.new() >>> gt_instances2 = gt_instances1.new()
# property add and access >>> # property add and access
>>> sample = BaseDataSample() >>> sample = BaseDataSample()
>>> gt_instances = BaseDataElement( >>> gt_instances = BaseDataElement(
metainfo=dict(img_id=9, img_shape=(100, 100)), ... metainfo=dict(img_id=9, img_shape=(100, 100)),
data=dict(bboxes=torch.rand((5, 4)), scores=torch.rand((5,))) ... data=dict(bboxes=torch.rand((5, 4)), scores=torch.rand((5,)))
>>> sample.set_metainfo(dict(img_id=9, img_shape=(100, 100)) >>> sample.set_metainfo(dict(img_id=9, img_shape=(100, 100))
>>> assert 'img_shape' in sample.metainfo_keys() >>> assert 'img_shape' in sample.metainfo_keys()
>>> assert 'img_shape' in sample >>> assert 'img_shape' in sample
>>> assert 'img_shape' not in sample.data_keys() >>> assert 'img_shape' not in sample.data_keys()
>>> assert 'img_shape' in sample.keys() >>> assert 'img_shape' in sample.keys()
>>> print(sample.img_shape) >>> print(sample.img_shape)
>>> gt_instances.gt_instances = gt_instances >>> gt_instances.gt_instances = gt_instances
>>> assert 'gt_instances' in sample.data_keys() >>> assert 'gt_instances' in sample.data_keys()
>>> assert 'gt_instances' in sample >>> assert 'gt_instances' in sample
>>> assert 'gt_instances' in sample.keys() >>> assert 'gt_instances' in sample.keys()
>>> assert 'gt_instances' not in sample.metainfo_keys() >>> assert 'gt_instances' not in sample.metainfo_keys()
>>> print(sample.gt_instances) >>> print(sample.gt_instances)
>>> pred_instances = BaseDataElement( >>> pred_instances = BaseDataElement(
metainfo=dict(img_id=9, img_shape=(100, 100)), ... metainfo=dict(img_id=9, img_shape=(100, 100)),
data=dict(bboxes=torch.rand((5, 4)), scores=torch.rand((5,)) ... data=dict(bboxes=torch.rand((5, 4)), scores=torch.rand((5,))
>>> sample.pred_instances = pred_instances >>> sample.pred_instances = pred_instances
>>> assert 'pred_instances' in sample.data_keys() >>> assert 'pred_instances' in sample.data_keys()
>>> assert 'pred_instances' in sample >>> assert 'pred_instances' in sample
@ -110,17 +107,17 @@ class BaseDataSample:
>>> assert 'pred_instances' not in sample.metainfo_keys() >>> assert 'pred_instances' not in sample.metainfo_keys()
>>> print(sample.pred_instances) >>> print(sample.pred_instances)
# property delete and change >>> # property delete and change
>>> metainfo=dict(img_id=0, img_shape=(640, 640) >>> metainfo=dict(img_id=0, img_shape=(640, 640)
>>> gt_instances = BaseDataElement( >>> gt_instances = BaseDataElement(
metainfo=metainfo) ... metainfo=metainfo),
data=dict(bboxes=torch.rand((6, 4)), scores=torch.rand((6,)))) ... data=dict(bboxes=torch.rand((6, 4)), scores=torch.rand((6,))))
>>> sample = BaseDataSample(metainfo=metainfo, >>> sample = BaseDataSample(metainfo=metainfo,
data=dict(gt_instances=gt_instances)) ... data=dict(gt_instances=gt_instances))
>>> sample.img_shape = (1280, 1280) >>> sample.img_shape = (1280, 1280)
>>> sample.img_shape # (1280, 1280) >>> sample.img_shape # (1280, 1280)
>>> sample.gt_instances = gt_instances >>> sample.gt_instances = gt_instances
>>> sample.get('img_shape', None) # (640 640) >>> sample.get('img_shape', None) # (640, 640)
>>> sample.get('gt_instances', None) >>> sample.get('gt_instances', None)
>>> del sample.img_shape >>> del sample.img_shape
>>> del sample.gt_instances >>> del sample.gt_instances
@ -129,23 +126,22 @@ class BaseDataSample:
>>> sample.pop('img_shape', None) # None >>> sample.pop('img_shape', None) # None
>>> sample.pop('gt_instances', None) # None >>> sample.pop('gt_instances', None) # None
# Tensor-like >>> # Tensor-like
>>> cuda_sample = gt_instasamplences.cuda() >>> cuda_sample = gt_instasamplences.cuda()
>>> cuda_sample = gt_sample.to('cuda:0') >>> cuda_sample = gt_sample.to('cuda:0')
>>> cpu_sample = cuda_sample.cpu() >>> cpu_sample = cuda_sample.cpu()
>>> cpu_sample = cuda_sample.to('cpu') >>> cpu_sample = cuda_sample.to('cpu')
>>> fp16_sample = cuda_sample.to( >>> fp16_sample = cuda_sample.to(
device=None, dtype=torch.float16, non_blocking=False, copy=False, ... device=None, dtype=torch.float16, non_blocking=False, copy=False,
memory_format=torch.preserve_format) ... memory_format=torch.preserve_format)
>>> cpu_sample = cuda_sample.detach() >>> cpu_sample = cuda_sample.detach()
>>> np_sample = cpu_sample.numpy() >>> np_sample = cpu_sample.numpy()
# print >>> # print
>>> metainfo = dict(img_shape=(800, 1196, 3)) >>> metainfo = dict(img_shape=(800, 1196, 3))
>>> gt_instances = BaseDataElement( >>> gt_instances = BaseDataElement(
metainfo=metainfo, ... metainfo=metainfo,
data=dict(det_labels=torch.LongTensor([0, 1, 2, 3]))) ... data=dict(det_labels=torch.LongTensor([0, 1, 2, 3])))
>>> data = dict(gt_instances=gt_instances) >>> data = dict(gt_instances=gt_instances)
>>> sample = BaseDataSample(metainfo=metainfo, data=data) >>> sample = BaseDataSample(metainfo=metainfo, data=data)
>>> print(sample) >>> print(sample)
@ -161,37 +157,36 @@ class BaseDataSample:
) at 0x7f9705daecd0>' ) at 0x7f9705daecd0>'
) at 0x7f981e41c550>' ) at 0x7f981e41c550>'
# inheritance >>> # inheritance
>>> class DetDataSample(BaseDataSample): >>> class DetDataSample(BaseDataSample):
>>> proposals = property( ... proposals = property(
>>> fget=partial(BaseDataSample.get_field, name='_proposals'), ... fget=partial(BaseDataSample.get_field, name='_proposals'),
>>> fset=partial( ... fset=partial(
>>> BaseDataSample.set_field, ... BaseDataSample.set_field,
>>> name='_proposals', ... name='_proposals',
>>> dtype=BaseDataElement), ... dtype=BaseDataElement),
>>> fdel=partial(BaseDataSample.del_field, name='_proposals'), ... fdel=partial(BaseDataSample.del_field, name='_proposals'),
>>> doc='Region proposals of an image') ... doc='Region proposals of an image')
>>> gt_instances = property( ... gt_instances = property(
>>> fget=partial(BaseDataSample.get_field, ... fget=partial(BaseDataSample.get_field,
name='_gt_instances'), ... name='_gt_instances'),
>>> fset=partial( ... fset=partial(
>>> BaseDataSample.set_field, ... BaseDataSample.set_field,
>>> name='_gt_instances', ... name='_gt_instances',
>>> dtype=BaseDataElement), ... dtype=BaseDataElement),
>>> fdel=partial(BaseDataSample.del_field, ... fdel=partial(BaseDataSample.del_field,
name='_gt_instances'), ... name='_gt_instances'),
>>> doc='Ground truth instances of an image') ... doc='Ground truth instances of an image')
>>> pred_instances = property( ... pred_instances = property(
>>> fget=partial( ... fget=partial(
>>> BaseDataSample.get_field, name='_pred_instances'), ... BaseDataSample.get_field, name='_pred_instances'),
>>> fset=partial( ... fset=partial(
>>> BaseDataSample.set_field, ... BaseDataSample.set_field,
>>> name='_pred_instances', ... name='_pred_instances',
>>> dtype=BaseDataElement), ... dtype=BaseDataElement),
>>> fdel=partial( ... fdel=partial(
>>> BaseDataSample.del_field, name='_pred_instances'), ... BaseDataSample.del_field, name='_pred_instances'),
>>> doc='Predicted instances of an image') ... doc='Predicted instances of an image')
>>> det_sample = DetDataSample() >>> det_sample = DetDataSample()
>>> proposals = BaseDataElement(data=dict(bboxes=torch.rand((5, 4)))) >>> proposals = BaseDataElement(data=dict(bboxes=torch.rand((5, 4))))
>>> det_sample.proposals = proposals >>> det_sample.proposals = proposals
@ -200,7 +195,7 @@ class BaseDataSample:
>>> del det_sample.proposals >>> del det_sample.proposals
>>> assert 'proposals' not in det_sample >>> assert 'proposals' not in det_sample
>>> with self.assertRaises(AssertionError): >>> with self.assertRaises(AssertionError):
det_sample.proposals = torch.rand((5, 4)) ... det_sample.proposals = torch.rand((5, 4))
""" """
def __init__(self, def __init__(self,

View File

@ -185,6 +185,14 @@ class TestBaseDataSample(TestCase):
instances = BaseDataSample(metainfo, data) instances = BaseDataSample(metainfo, data)
new_metainfo, new_data = self.setup_data() new_metainfo, new_data = self.setup_data()
# avoid generating same metainfo
while True:
if new_metainfo['img_id'] == metainfo['img_id'] or new_metainfo[
'img_shape'] == metainfo['img_shape']:
new_metainfo, new_data = self.setup_data()
else:
break
instances.gt_instances = new_data['gt_instances'] instances.gt_instances = new_data['gt_instances']
instances.pred_instances = new_data['pred_instances'] instances.pred_instances = new_data['pred_instances']