Spaces:
Build error
Build error
File size: 34,021 Bytes
28c256d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 |
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import copy
import functools
import gc
import logging
import pickle
from typing import Any, Callable, List, Optional, Sequence, Tuple, Union
import numpy as np
from torch.utils.data import Dataset
from mmengine.fileio import join_path, list_from_file, load
from mmengine.logging import print_log
from mmengine.registry import TRANSFORMS
from mmengine.utils import is_abs
class Compose:
"""Compose multiple transforms sequentially.
Args:
transforms (Sequence[dict, callable], optional): Sequence of transform
object or config dict to be composed.
"""
def __init__(self, transforms: Optional[Sequence[Union[dict, Callable]]]):
self.transforms: List[Callable] = []
if transforms is None:
transforms = []
for transform in transforms:
# `Compose` can be built with config dict with type and
# corresponding arguments.
if isinstance(transform, dict):
transform = TRANSFORMS.build(transform)
if not callable(transform):
raise TypeError(f'transform should be a callable object, '
f'but got {type(transform)}')
self.transforms.append(transform)
elif callable(transform):
self.transforms.append(transform)
else:
raise TypeError(
f'transform must be a callable object or dict, '
f'but got {type(transform)}')
def __call__(self, data: dict) -> Optional[dict]:
"""Call function to apply transforms sequentially.
Args:
data (dict): A result dict contains the data to transform.
Returns:
dict: Transformed data.
"""
for t in self.transforms:
data = t(data)
# The transform will return None when it failed to load images or
# cannot find suitable augmentation parameters to augment the data.
# Here we simply return None if the transform returns None and the
# dataset will handle it by randomly selecting another data sample.
if data is None:
return None
return data
def __repr__(self):
"""Print ``self.transforms`` in sequence.
Returns:
str: Formatted string.
"""
format_string = self.__class__.__name__ + '('
for t in self.transforms:
format_string += '\n'
format_string += f' {t}'
format_string += '\n)'
return format_string
def force_full_init(old_func: Callable) -> Any:
"""Those methods decorated by ``force_full_init`` will be forced to call
``full_init`` if the instance has not been fully initiated.
Args:
old_func (Callable): Decorated function, make sure the first arg is an
instance with ``full_init`` method.
Returns:
Any: Depends on old_func.
"""
@functools.wraps(old_func)
def wrapper(obj: object, *args, **kwargs):
# The instance must have `full_init` method.
if not hasattr(obj, 'full_init'):
raise AttributeError(f'{type(obj)} does not have full_init '
'method.')
# If instance does not have `_fully_initialized` attribute or
# `_fully_initialized` is False, call `full_init` and set
# `_fully_initialized` to True
if not getattr(obj, '_fully_initialized', False):
print_log(
f'Attribute `_fully_initialized` is not defined in '
f'{type(obj)} or `type(obj)._fully_initialized is '
'False, `full_init` will be called and '
f'{type(obj)}._fully_initialized will be set to True',
logger='current',
level=logging.WARNING)
obj.full_init() # type: ignore
obj._fully_initialized = True # type: ignore
return old_func(obj, *args, **kwargs)
return wrapper
class BaseDataset(Dataset):
r"""BaseDataset for open source projects in OpenMMLab.
The annotation format is shown as follows.
.. code-block:: none
{
"metainfo":
{
"dataset_type": "test_dataset",
"task_name": "test_task"
},
"data_list":
[
{
"img_path": "test_img.jpg",
"height": 604,
"width": 640,
"instances":
[
{
"bbox": [0, 0, 10, 20],
"bbox_label": 1,
"mask": [[0,0],[0,10],[10,20],[20,0]],
"extra_anns": [1,2,3]
},
{
"bbox": [10, 10, 110, 120],
"bbox_label": 2,
"mask": [[10,10],[10,110],[110,120],[120,10]],
"extra_anns": [4,5,6]
}
]
},
]
}
Args:
ann_file (str, optional): Annotation file path. Defaults to ''.
metainfo (dict, optional): Meta information for dataset, such as class
information. Defaults to None.
data_root (str, optional): The root directory for ``data_prefix`` and
``ann_file``. Defaults to ''.
data_prefix (dict): Prefix for training data. Defaults to
dict(img_path='').
filter_cfg (dict, optional): Config for filter data. Defaults to None.
indices (int or Sequence[int], optional): Support using first few
data in annotation file to facilitate training/testing on a smaller
serialize_data (bool, optional): Whether to hold memory using
serialized objects, when enabled, data loader workers can use
shared RAM from master process instead of making a copy. Defaults
to True.
pipeline (list, optional): Processing pipeline. Defaults to [].
test_mode (bool, optional): ``test_mode=True`` means in test phase.
Defaults to False.
lazy_init (bool, optional): Whether to load annotation during
instantiation. In some cases, such as visualization, only the meta
information of the dataset is needed, which is not necessary to
load annotation file. ``Basedataset`` can skip load annotations to
save time by set ``lazy_init=True``. Defaults to False.
max_refetch (int, optional): If ``Basedataset.prepare_data`` get a
None img. The maximum extra number of cycles to get a valid
image. Defaults to 1000.
Note:
BaseDataset collects meta information from ``annotation file`` (the
lowest priority), ``BaseDataset.METAINFO``(medium) and ``metainfo
parameter`` (highest) passed to constructors. The lower priority meta
information will be overwritten by higher one.
Note:
Dataset wrapper such as ``ConcatDataset``, ``RepeatDataset`` .etc.
should not inherit from ``BaseDataset`` since ``get_subset`` and
``get_subset_`` could produce ambiguous meaning sub-dataset which
conflicts with original dataset.
Examples:
>>> # Assume the annotation file is given above.
>>> class CustomDataset(BaseDataset):
>>> METAINFO: dict = dict(task_name='custom_task',
>>> dataset_type='custom_type')
>>> metainfo=dict(task_name='custom_task_name')
>>> custom_dataset = CustomDataset(
>>> 'path/to/ann_file',
>>> metainfo=metainfo)
>>> # meta information of annotation file will be overwritten by
>>> # `CustomDataset.METAINFO`. The merged meta information will
>>> # further be overwritten by argument `metainfo`.
>>> custom_dataset.metainfo
{'task_name': custom_task_name, dataset_type: custom_type}
"""
METAINFO: dict = dict()
_fully_initialized: bool = False
def __init__(self,
ann_file: Optional[str] = '',
metainfo: Optional[dict] = None,
data_root: Optional[str] = '',
data_prefix: dict = dict(img_path=''),
filter_cfg: Optional[dict] = None,
indices: Optional[Union[int, Sequence[int]]] = None,
serialize_data: bool = True,
pipeline: List[Union[dict, Callable]] = [],
test_mode: bool = False,
lazy_init: bool = False,
max_refetch: int = 1000):
self.ann_file = ann_file
self._metainfo = self._load_metainfo(copy.deepcopy(metainfo))
self.data_root = data_root
self.data_prefix = copy.copy(data_prefix)
self.filter_cfg = copy.deepcopy(filter_cfg)
self._indices = indices
self.serialize_data = serialize_data
self.test_mode = test_mode
self.max_refetch = max_refetch
self.data_list: List[dict] = []
self.data_bytes: np.ndarray
# Join paths.
self._join_prefix()
# Build pipeline.
self.pipeline = Compose(pipeline)
# Full initialize the dataset.
if not lazy_init:
self.full_init()
@force_full_init
def get_data_info(self, idx: int) -> dict:
"""Get annotation by index and automatically call ``full_init`` if the
dataset has not been fully initialized.
Args:
idx (int): The index of data.
Returns:
dict: The idx-th annotation of the dataset.
"""
if self.serialize_data:
start_addr = 0 if idx == 0 else self.data_address[idx - 1].item()
end_addr = self.data_address[idx].item()
bytes = memoryview(
self.data_bytes[start_addr:end_addr]) # type: ignore
data_info = pickle.loads(bytes) # type: ignore
else:
data_info = copy.deepcopy(self.data_list[idx])
# Some codebase needs `sample_idx` of data information. Here we convert
# the idx to a positive number and save it in data information.
if idx >= 0:
data_info['sample_idx'] = idx
else:
data_info['sample_idx'] = len(self) + idx
return data_info
def full_init(self):
"""Load annotation file and set ``BaseDataset._fully_initialized`` to
True.
If ``lazy_init=False``, ``full_init`` will be called during the
instantiation and ``self._fully_initialized`` will be set to True. If
``obj._fully_initialized=False``, the class method decorated by
``force_full_init`` will call ``full_init`` automatically.
Several steps to initialize annotation:
- load_data_list: Load annotations from annotation file.
- filter data information: Filter annotations according to
filter_cfg.
- slice_data: Slice dataset according to ``self._indices``
- serialize_data: Serialize ``self.data_list`` if
``self.serialize_data`` is True.
"""
if self._fully_initialized:
return
# load data information
self.data_list = self.load_data_list()
# filter illegal data, such as data that has no annotations.
self.data_list = self.filter_data()
# Get subset data according to indices.
if self._indices is not None:
self.data_list = self._get_unserialized_subset(self._indices)
# serialize data_list
if self.serialize_data:
self.data_bytes, self.data_address = self._serialize_data()
self._fully_initialized = True
@property
def metainfo(self) -> dict:
"""Get meta information of dataset.
Returns:
dict: meta information collected from ``BaseDataset.METAINFO``,
annotation file and metainfo argument during instantiation.
"""
return copy.deepcopy(self._metainfo)
def parse_data_info(self, raw_data_info: dict) -> Union[dict, List[dict]]:
"""Parse raw annotation to target format.
This method should return dict or list of dict. Each dict or list
contains the data information of a training sample. If the protocol of
the sample annotations is changed, this function can be overridden to
update the parsing logic while keeping compatibility.
Args:
raw_data_info (dict): Raw data information load from ``ann_file``
Returns:
list or list[dict]: Parsed annotation.
"""
for prefix_key, prefix in self.data_prefix.items():
assert prefix_key in raw_data_info, (
f'raw_data_info: {raw_data_info} dose not contain prefix key'
f'{prefix_key}, please check your data_prefix.')
raw_data_info[prefix_key] = join_path(prefix,
raw_data_info[prefix_key])
return raw_data_info
def filter_data(self) -> List[dict]:
"""Filter annotations according to filter_cfg. Defaults return all
``data_list``.
If some ``data_list`` could be filtered according to specific logic,
the subclass should override this method.
Returns:
list[int]: Filtered results.
"""
return self.data_list
def get_cat_ids(self, idx: int) -> List[int]:
"""Get category ids by index. Dataset wrapped by ClassBalancedDataset
must implement this method.
The ``ClassBalancedDataset`` requires a subclass which implements this
method.
Args:
idx (int): The index of data.
Returns:
list[int]: All categories in the image of specified index.
"""
raise NotImplementedError(f'{type(self)} must implement `get_cat_ids` '
'method')
def __getitem__(self, idx: int) -> dict:
"""Get the idx-th image and data information of dataset after
``self.pipeline``, and ``full_init`` will be called if the dataset has
not been fully initialized.
During training phase, if ``self.pipeline`` get ``None``,
``self._rand_another`` will be called until a valid image is fetched or
the maximum limit of refetech is reached.
Args:
idx (int): The index of self.data_list.
Returns:
dict: The idx-th image and data information of dataset after
``self.pipeline``.
"""
# Performing full initialization by calling `__getitem__` will consume
# extra memory. If a dataset is not fully initialized by setting
# `lazy_init=True` and then fed into the dataloader. Different workers
# will simultaneously read and parse the annotation. It will cost more
# time and memory, although this may work. Therefore, it is recommended
# to manually call `full_init` before dataset fed into dataloader to
# ensure all workers use shared RAM from master process.
if not self._fully_initialized:
print_log(
'Please call `full_init()` method manually to accelerate '
'the speed.',
logger='current',
level=logging.WARNING)
self.full_init()
if self.test_mode:
data = self.prepare_data(idx)
if data is None:
raise Exception('Test time pipline should not get `None` '
'data_sample')
return data
for _ in range(self.max_refetch + 1):
data = self.prepare_data(idx)
# Broken images or random augmentations may cause the returned data
# to be None
if data is None:
idx = self._rand_another()
continue
return data
raise Exception(f'Cannot find valid image after {self.max_refetch}! '
'Please check your image path and pipeline')
def load_data_list(self) -> List[dict]:
"""Load annotations from an annotation file named as ``self.ann_file``
If the annotation file does not follow `OpenMMLab 2.0 format dataset
<https://mmengine.readthedocs.io/en/latest/advanced_tutorials/basedataset.html>`_ .
The subclass must override this method for load annotations. The meta
information of annotation file will be overwritten :attr:`METAINFO`
and ``metainfo`` argument of constructor.
Returns:
list[dict]: A list of annotation.
""" # noqa: E501
# `self.ann_file` denotes the absolute annotation file path if
# `self.root=None` or relative path if `self.root=/path/to/data/`.
annotations = load(self.ann_file)
if not isinstance(annotations, dict):
raise TypeError(f'The annotations loaded from annotation file '
f'should be a dict, but got {type(annotations)}!')
if 'data_list' not in annotations or 'metainfo' not in annotations:
raise ValueError('Annotation must have data_list and metainfo '
'keys')
metainfo = annotations['metainfo']
raw_data_list = annotations['data_list']
# Meta information load from annotation file will not influence the
# existed meta information load from `BaseDataset.METAINFO` and
# `metainfo` arguments defined in constructor.
for k, v in metainfo.items():
self._metainfo.setdefault(k, v)
# load and parse data_infos.
data_list = []
for raw_data_info in raw_data_list:
# parse raw data information to target format
data_info = self.parse_data_info(raw_data_info)
if isinstance(data_info, dict):
# For image tasks, `data_info` should information if single
# image, such as dict(img_path='xxx', width=360, ...)
data_list.append(data_info)
elif isinstance(data_info, list):
# For video tasks, `data_info` could contain image
# information of multiple frames, such as
# [dict(video_path='xxx', timestamps=...),
# dict(video_path='xxx', timestamps=...)]
for item in data_info:
if not isinstance(item, dict):
raise TypeError('data_info must be list of dict, but '
f'got {type(item)}')
data_list.extend(data_info)
else:
raise TypeError('data_info should be a dict or list of dict, '
f'but got {type(data_info)}')
return data_list
@classmethod
def _load_metainfo(cls, metainfo: dict = None) -> dict:
"""Collect meta information from the dictionary of meta.
Args:
metainfo (dict): Meta information dict. If ``metainfo``
contains existed filename, it will be parsed by
``list_from_file``.
Returns:
dict: Parsed meta information.
"""
# avoid `cls.METAINFO` being overwritten by `metainfo`
cls_metainfo = copy.deepcopy(cls.METAINFO)
if metainfo is None:
return cls_metainfo
if not isinstance(metainfo, dict):
raise TypeError(
f'metainfo should be a dict, but got {type(metainfo)}')
for k, v in metainfo.items():
if isinstance(v, str):
# If type of value is string, and can be loaded from
# corresponding backend. it means the file name of meta file.
try:
cls_metainfo[k] = list_from_file(v)
except (TypeError, FileNotFoundError):
print_log(
f'{v} is not a meta file, simply parsed as meta '
'information',
logger='current',
level=logging.WARNING)
cls_metainfo[k] = v
else:
cls_metainfo[k] = v
return cls_metainfo
def _join_prefix(self):
"""Join ``self.data_root`` with ``self.data_prefix`` and
``self.ann_file``.
Examples:
>>> # self.data_prefix contains relative paths
>>> self.data_root = 'a/b/c'
>>> self.data_prefix = dict(img='d/e/')
>>> self.ann_file = 'f'
>>> self._join_prefix()
>>> self.data_prefix
dict(img='a/b/c/d/e')
>>> self.ann_file
'a/b/c/f'
>>> # self.data_prefix contains absolute paths
>>> self.data_root = 'a/b/c'
>>> self.data_prefix = dict(img='/d/e/')
>>> self.ann_file = 'f'
>>> self._join_prefix()
>>> self.data_prefix
dict(img='/d/e')
>>> self.ann_file
'a/b/c/f'
"""
# Automatically join annotation file path with `self.root` if
# `self.ann_file` is not an absolute path.
if self.ann_file and not is_abs(self.ann_file) and self.data_root:
self.ann_file = join_path(self.data_root, self.ann_file)
# Automatically join data directory with `self.root` if path value in
# `self.data_prefix` is not an absolute path.
for data_key, prefix in self.data_prefix.items():
if not isinstance(prefix, str):
raise TypeError('prefix should be a string, but got '
f'{type(prefix)}')
if not is_abs(prefix) and self.data_root:
self.data_prefix[data_key] = join_path(self.data_root, prefix)
else:
self.data_prefix[data_key] = prefix
@force_full_init
def get_subset_(self, indices: Union[Sequence[int], int]) -> None:
"""The in-place version of ``get_subset`` to convert dataset to a
subset of original dataset.
This method will convert the original dataset to a subset of dataset.
If type of indices is int, ``get_subset_`` will return a subdataset
which contains the first or last few data information according to
indices is positive or negative. If type of indices is a sequence of
int, the subdataset will extract the data information according to
the index given in indices.
Examples:
>>> dataset = BaseDataset('path/to/ann_file')
>>> len(dataset)
100
>>> dataset.get_subset_(90)
>>> len(dataset)
90
>>> # if type of indices is sequence, extract the corresponding
>>> # index data information
>>> dataset.get_subset_([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> len(dataset)
10
>>> dataset.get_subset_(-3)
>>> len(dataset) # Get the latest few data information.
3
Args:
indices (int or Sequence[int]): If type of indices is int, indices
represents the first or last few data of dataset according to
indices is positive or negative. If type of indices is
Sequence, indices represents the target data information
index of dataset.
"""
# Get subset of data from serialized data or data information sequence
# according to `self.serialize_data`.
if self.serialize_data:
self.data_bytes, self.data_address = \
self._get_serialized_subset(indices)
else:
self.data_list = self._get_unserialized_subset(indices)
@force_full_init
def get_subset(self, indices: Union[Sequence[int], int]) -> 'BaseDataset':
"""Return a subset of dataset.
This method will return a subset of original dataset. If type of
indices is int, ``get_subset_`` will return a subdataset which
contains the first or last few data information according to
indices is positive or negative. If type of indices is a sequence of
int, the subdataset will extract the information according to the index
given in indices.
Examples:
>>> dataset = BaseDataset('path/to/ann_file')
>>> len(dataset)
100
>>> subdataset = dataset.get_subset(90)
>>> len(sub_dataset)
90
>>> # if type of indices is list, extract the corresponding
>>> # index data information
>>> subdataset = dataset.get_subset([0, 1, 2, 3, 4, 5, 6, 7,
>>> 8, 9])
>>> len(sub_dataset)
10
>>> subdataset = dataset.get_subset(-3)
>>> len(subdataset) # Get the latest few data information.
3
Args:
indices (int or Sequence[int]): If type of indices is int, indices
represents the first or last few data of dataset according to
indices is positive or negative. If type of indices is
Sequence, indices represents the target data information
index of dataset.
Returns:
BaseDataset: A subset of dataset.
"""
# Get subset of data from serialized data or data information list
# according to `self.serialize_data`. Since `_get_serialized_subset`
# will recalculate the subset data information,
# `_copy_without_annotation` will copy all attributes except data
# information.
sub_dataset = self._copy_without_annotation()
# Get subset of dataset with serialize and unserialized data.
if self.serialize_data:
data_bytes, data_address = \
self._get_serialized_subset(indices)
sub_dataset.data_bytes = data_bytes.copy()
sub_dataset.data_address = data_address.copy()
else:
data_list = self._get_unserialized_subset(indices)
sub_dataset.data_list = copy.deepcopy(data_list)
return sub_dataset
def _get_serialized_subset(self, indices: Union[Sequence[int], int]) \
-> Tuple[np.ndarray, np.ndarray]:
"""Get subset of serialized data information list.
Args:
indices (int or Sequence[int]): If type of indices is int,
indices represents the first or last few data of serialized
data information list. If type of indices is Sequence, indices
represents the target data information index which consist of
subset data information.
Returns:
Tuple[np.ndarray, np.ndarray]: subset of serialized data
information.
"""
sub_data_bytes: Union[List, np.ndarray]
sub_data_address: Union[List, np.ndarray]
if isinstance(indices, int):
if indices >= 0:
assert indices < len(self.data_address), \
f'{indices} is out of dataset length({len(self)}'
# Return the first few data information.
end_addr = self.data_address[indices - 1].item() \
if indices > 0 else 0
# Slicing operation of `np.ndarray` does not trigger a memory
# copy.
sub_data_bytes = self.data_bytes[:end_addr]
# Since the buffer size of first few data information is not
# changed,
sub_data_address = self.data_address[:indices]
else:
assert -indices <= len(self.data_address), \
f'{indices} is out of dataset length({len(self)}'
# Return the last few data information.
ignored_bytes_size = self.data_address[indices - 1]
start_addr = self.data_address[indices - 1].item()
sub_data_bytes = self.data_bytes[start_addr:]
sub_data_address = self.data_address[indices:]
sub_data_address = sub_data_address - ignored_bytes_size
elif isinstance(indices, Sequence):
sub_data_bytes = []
sub_data_address = []
for idx in indices:
assert len(self) > idx >= -len(self)
start_addr = 0 if idx == 0 else \
self.data_address[idx - 1].item()
end_addr = self.data_address[idx].item()
# Get data information by address.
sub_data_bytes.append(self.data_bytes[start_addr:end_addr])
# Get data information size.
sub_data_address.append(end_addr - start_addr)
# Handle indices is an empty list.
if sub_data_bytes:
sub_data_bytes = np.concatenate(sub_data_bytes)
sub_data_address = np.cumsum(sub_data_address)
else:
sub_data_bytes = np.array([])
sub_data_address = np.array([])
else:
raise TypeError('indices should be a int or sequence of int, '
f'but got {type(indices)}')
return sub_data_bytes, sub_data_address # type: ignore
def _get_unserialized_subset(self, indices: Union[Sequence[int],
int]) -> list:
"""Get subset of data information list.
Args:
indices (int or Sequence[int]): If type of indices is int,
indices represents the first or last few data of data
information. If type of indices is Sequence, indices represents
the target data information index which consist of subset data
information.
Returns:
Tuple[np.ndarray, np.ndarray]: subset of data information.
"""
if isinstance(indices, int):
if indices >= 0:
# Return the first few data information.
sub_data_list = self.data_list[:indices]
else:
# Return the last few data information.
sub_data_list = self.data_list[indices:]
elif isinstance(indices, Sequence):
# Return the data information according to given indices.
sub_data_list = []
for idx in indices:
sub_data_list.append(self.data_list[idx])
else:
raise TypeError('indices should be a int or sequence of int, '
f'but got {type(indices)}')
return sub_data_list
def _serialize_data(self) -> Tuple[np.ndarray, np.ndarray]:
"""Serialize ``self.data_list`` to save memory when launching multiple
workers in data loading. This function will be called in ``full_init``.
Hold memory using serialized objects, and data loader workers can use
shared RAM from master process instead of making a copy.
Returns:
Tuple[np.ndarray, np.ndarray]: Serialized result and corresponding
address.
"""
def _serialize(data):
buffer = pickle.dumps(data, protocol=4)
return np.frombuffer(buffer, dtype=np.uint8)
# Serialize data information list avoid making multiple copies of
# `self.data_list` when iterate `import torch.utils.data.dataloader`
# with multiple workers.
data_list = [_serialize(x) for x in self.data_list]
address_list = np.asarray([len(x) for x in data_list], dtype=np.int64)
data_address: np.ndarray = np.cumsum(address_list)
# TODO Check if np.concatenate is necessary
data_bytes = np.concatenate(data_list)
# Empty cache for preventing making multiple copies of
# `self.data_info` when loading data multi-processes.
self.data_list.clear()
gc.collect()
return data_bytes, data_address
def _rand_another(self) -> int:
"""Get random index.
Returns:
int: Random index from 0 to ``len(self)-1``
"""
return np.random.randint(0, len(self))
def prepare_data(self, idx) -> Any:
"""Get data processed by ``self.pipeline``.
Args:
idx (int): The index of ``data_info``.
Returns:
Any: Depends on ``self.pipeline``.
"""
data_info = self.get_data_info(idx)
return self.pipeline(data_info)
@force_full_init
def __len__(self) -> int:
"""Get the length of filtered dataset and automatically call
``full_init`` if the dataset has not been fully init.
Returns:
int: The length of filtered dataset.
"""
if self.serialize_data:
return len(self.data_address)
else:
return len(self.data_list)
def _copy_without_annotation(self, memo=dict()) -> 'BaseDataset':
"""Deepcopy for all attributes other than ``data_list``,
``data_address`` and ``data_bytes``.
Args:
memo: Memory dict which used to reconstruct complex object
correctly.
"""
cls = self.__class__
other = cls.__new__(cls)
memo[id(self)] = other
for key, value in self.__dict__.items():
if key in ['data_list', 'data_address', 'data_bytes']:
continue
super(BaseDataset, other).__setattr__(key,
copy.deepcopy(value, memo))
return other
|