Collect data from the loader relevant to the specific task. This keeps the items in `keys` as it is, and collect items in `meta_keys` into a meta item called `meta_name`.This is usually the last stage of the data loader pipeline. For example, when keys='imgs', meta_keys=('filename',
| 857 | |
| 858 | |
| 859 | class Collect: |
| 860 | """Collect data from the loader relevant to the specific task. |
| 861 | |
| 862 | This keeps the items in `keys` as it is, and collect items in `meta_keys` |
| 863 | into a meta item called `meta_name`.This is usually the last stage of the |
| 864 | data loader pipeline. |
| 865 | For example, when keys='imgs', meta_keys=('filename', 'label', |
| 866 | 'original_shape'), meta_name='img_metas', the results will be a dict with |
| 867 | keys 'imgs' and 'img_metas', where 'img_metas' is a DataContainer of |
| 868 | another dict with keys 'filename', 'label', 'original_shape'. |
| 869 | |
| 870 | Args: |
| 871 | keys (Sequence[str|tuple]): Required keys to be collected. If a tuple |
| 872 | (key, key_new) is given as an element, the item retrived by key will |
| 873 | be renamed as key_new in collected data. |
| 874 | meta_name (str): The name of the key that contains meta infomation. |
| 875 | This key is always populated. Default: "img_metas". |
| 876 | meta_keys (Sequence[str|tuple]): Keys that are collected under |
| 877 | meta_name. The contents of the `meta_name` dictionary depends |
| 878 | on `meta_keys`. |
| 879 | """ |
| 880 | |
| 881 | def __init__(self, keys, meta_keys, meta_name='img_metas'): |
| 882 | self.keys = keys |
| 883 | self.meta_keys = meta_keys |
| 884 | self.meta_name = meta_name |
| 885 | |
| 886 | def __call__(self, results): |
| 887 | """Performs the Collect formating. |
| 888 | |
| 889 | Args: |
| 890 | results (dict): The resulting dict to be modified and passed |
| 891 | to the next transform in pipeline. |
| 892 | """ |
| 893 | if 'ann_info' in results: |
| 894 | results.update(results['ann_info']) |
| 895 | |
| 896 | data = {} |
| 897 | for key in self.keys: |
| 898 | if isinstance(key, tuple): |
| 899 | assert len(key) == 2 |
| 900 | key_src, key_tgt = key[:2] |
| 901 | else: |
| 902 | key_src = key_tgt = key |
| 903 | data[key_tgt] = results[key_src] |
| 904 | |
| 905 | meta = {} |
| 906 | if len(self.meta_keys) != 0: |
| 907 | for key in self.meta_keys: |
| 908 | if isinstance(key, tuple): |
| 909 | assert len(key) == 2 |
| 910 | key_src, key_tgt = key[:2] |
| 911 | else: |
| 912 | key_src = key_tgt = key |
| 913 | meta[key_tgt] = results[key_src] |
| 914 | if 'bbox_id' in results: |
| 915 | meta['bbox_id'] = results['bbox_id'] |
| 916 | data[self.meta_name] = DataContainer(meta, cpu_only=True) |