Shortcuts

mmfewshot.classification

classification.apis

mmfewshot.classification.apis.inference_classifier(model: torch.nn.modules.module.Module, query_img: str)Dict[source]

Inference single image with the classifier.

Parameters
  • model (nn.Module) – The loaded classifier.

  • query_img (str) – The image filename.

Returns

The classification results that contains

pred_score of each class.

Return type

dict

mmfewshot.classification.apis.init_classifier(config: Union[str, mmcv.utils.config.Config], checkpoint: Optional[str] = None, device: str = 'cuda:0', options: Optional[Dict] = None)torch.nn.modules.module.Module[source]

Prepare a few shot classifier from config file.

Parameters
  • config (str or mmcv.Config) – Config file path or the config object.

  • checkpoint (str | None) – Checkpoint path. If left as None, the model will not load any weights. Default: None.

  • device (str) – Runtime device. Default: ‘cuda:0’.

  • options (dict | None) – Options to override some settings in the used config. Default: None.

Returns

The constructed classifier.

Return type

nn.Module

mmfewshot.classification.apis.multi_gpu_meta_test(model: mmcv.parallel.distributed.MMDistributedDataParallel, num_test_tasks: int, support_dataloader: torch.utils.data.dataloader.DataLoader, query_dataloader: torch.utils.data.dataloader.DataLoader, test_set_dataloader: Optional[torch.utils.data.dataloader.DataLoader] = None, meta_test_cfg: Optional[Dict] = None, eval_kwargs: Optional[Dict] = None, logger: Optional[object] = None, confidence_interval: float = 0.95, show_task_results: bool = False)Dict[source]

Distributed meta testing on multiple gpus.

During meta testing, model might be further fine-tuned or added extra parameters. While the tested model need to be restored after meta testing since meta testing can be used as the validation in the middle of training. To detach model from previous phase, the model will be copied and wrapped with MetaTestParallel. And it has full independence from the training model and will be discarded after the meta testing.

In the distributed situation, the MetaTestParallel on each GPU is also independent. The test tasks in few shot leaning usually are very small and hardly benefit from distributed acceleration. Thus, in distributed meta testing, each task is done in single GPU and each GPU is assigned a certain number of tasks. The number of test tasks for each GPU is ceil(num_test_tasks / world_size). After all GPUs finish their tasks, the results will be aggregated to get the final result.

Parameters
  • model (MMDistributedDataParallel) – Model to be meta tested.

  • num_test_tasks (int) – Number of meta testing tasks.

  • support_dataloader (DataLoader) – A PyTorch dataloader of support data.

  • query_dataloader (DataLoader) – A PyTorch dataloader of query data.

  • test_set_dataloader (DataLoader) – A PyTorch dataloader of all test data. Default: None.

  • meta_test_cfg (dict) – Config for meta testing. Default: None.

  • eval_kwargs (dict) – Any keyword argument to be used for evaluation. Default: None.

  • logger (logging.Logger | None) – Logger used for printing related information during evaluation. Default: None.

  • confidence_interval (float) – Confidence interval. Default: 0.95.

  • show_task_results (bool) – Whether to record the eval result of each task. Default: False.

Returns

Dict of meta evaluate results, containing accuracy_mean

and accuracy_std of all test tasks.

Return type

dict | None

mmfewshot.classification.apis.process_support_images(model: torch.nn.modules.module.Module, support_imgs: List[str], support_labels: List[str])None[source]

Process support images.

Parameters
  • model (nn.Module) – Classifier model.

  • support_imgs (list[str]) – The image filenames.

  • support_labels (list[str]) – The class names of support images.

mmfewshot.classification.apis.show_result_pyplot(img: str, result: Dict, fig_size: Tuple[int] = (15, 10), wait_time: int = 0, out_file: Optional[str] = None)numpy.ndarray[source]

Visualize the classification results on the image.

Parameters
  • img (str) – Image filename.

  • result (dict) – The classification result.

  • fig_size (tuple) – Figure size of the pyplot figure. Default: (15, 10).

  • wait_time (int) – How many seconds to display the image. Default: 0.

  • out_file (str | None) – Default: None

Returns

pyplot figure.

Return type

np.ndarray

mmfewshot.classification.apis.single_gpu_meta_test(model: Union[mmcv.parallel.data_parallel.MMDataParallel, torch.nn.modules.module.Module], num_test_tasks: int, support_dataloader: torch.utils.data.dataloader.DataLoader, query_dataloader: torch.utils.data.dataloader.DataLoader, test_set_dataloader: Optional[torch.utils.data.dataloader.DataLoader] = None, meta_test_cfg: Optional[Dict] = None, eval_kwargs: Optional[Dict] = None, logger: Optional[object] = None, confidence_interval: float = 0.95, show_task_results: bool = False)Dict[source]

Meta testing on single gpu.

During meta testing, model might be further fine-tuned or added extra parameters. While the tested model need to be restored after meta testing since meta testing can be used as the validation in the middle of training. To detach model from previous phase, the model will be copied and wrapped with MetaTestParallel. And it has full independence from the training model and will be discarded after the meta testing.

Parameters
  • model (MMDataParallel | nn.Module) – Model to be meta tested.

  • num_test_tasks (int) – Number of meta testing tasks.

  • support_dataloader (DataLoader) – A PyTorch dataloader of support data and it is used to fetch support data for each task.

  • query_dataloader (DataLoader) – A PyTorch dataloader of query data and it is used to fetch query data for each task.

  • test_set_dataloader (DataLoader) – A PyTorch dataloader of all test data and it is used for feature extraction from whole dataset to accelerate the testing. Default: None.

  • meta_test_cfg (dict) – Config for meta testing. Default: None.

  • eval_kwargs (dict) – Any keyword argument to be used for evaluation. Default: None.

  • logger (logging.Logger | None) – Logger used for printing related information during evaluation. Default: None.

  • confidence_interval (float) – Confidence interval. Default: 0.95.

  • show_task_results (bool) – Whether to record the eval result of each task. Default: False.

Returns

Dict of meta evaluate results, containing accuracy_mean

and accuracy_std of all test tasks.

Return type

dict

mmfewshot.classification.apis.test_single_task(model: mmfewshot.classification.utils.meta_test_parallel.MetaTestParallel, support_dataloader: torch.utils.data.dataloader.DataLoader, query_dataloader: torch.utils.data.dataloader.DataLoader, meta_test_cfg: Dict)[source]

Test a single task.

A task has two stages: handling the support set and predicting the query set. In stage one, it currently supports fine-tune based and metric based methods. In stage two, it simply forward the query set and gather all the results.

Parameters
  • model (MetaTestParallel) – Model to be meta tested.

  • support_dataloader (DataLoader) – A PyTorch dataloader of support data.

  • query_dataloader (DataLoader) – A PyTorch dataloader of query data.

  • meta_test_cfg (dict) – Config for meta testing.

Returns

  • results_list (list[np.ndarray]): Predict results.

  • gt_labels (np.ndarray): Ground truth labels.

Return type

tuple

classification.core

classification.datasets

class mmfewshot.classification.datasets.BaseFewShotDataset(data_prefix: str, pipeline: List[Dict], classes: Optional[Union[str, List[str]]] = None, ann_file: Optional[str] = None)[source]

Base few shot dataset.

Parameters
  • data_prefix (str) – The prefix of data path.

  • pipeline (list) – A list of dict, where each element represents a operation defined in mmcls.datasets.pipelines.

  • classes (str | Sequence[str] | None) – Classes for model training and provide fixed label for each class. Default: None.

  • ann_file (str | None) – The annotation file. When ann_file is str, the subclass is expected to read from the ann_file. When ann_file is None, the subclass is expected to read according to data_prefix. Default: None.

property class_to_idx: Mapping

Map mapping class name to class index.

Returns

mapping from class name to class index.

Return type

dict

static evaluate(results: List, gt_labels: numpy.array, metric: Union[str, List[str]] = 'accuracy', metric_options: Optional[dict] = None, logger: Optional[object] = None)Dict[source]

Evaluate the dataset.

Parameters
  • results (list) – Testing results of the dataset.

  • gt_labels (np.ndarray) – Ground truth labels.

  • metric (str | list[str]) – Metrics to be evaluated. Default value is accuracy.

  • metric_options (dict | None) – Options for calculating metrics. Allowed keys are ‘topk’, ‘thrs’ and ‘average_mode’. Default: None.

  • logger (logging.Logger | None) – Logger used for printing related information during evaluation. Default: None.

Returns

evaluation results

Return type

dict

classmethod get_classes(classes: Optional[Union[Sequence[str], str]] = None)Sequence[str][source]

Get class names of current dataset.

Parameters

classes (Sequence[str] | str | None) –

Three types of input will correspond to different processing logics:

  • If classes is a tuple or list, it will override the CLASSES predefined in the dataset.

  • If classes is None, we directly use pre-defined CLASSES will be used by the dataset.

  • If classes is a string, it is the path of a classes file that contains the name of all classes. Each line of the file contains a single class name.

Returns

Names of categories of the dataset.

Return type

tuple[str] or list[str]

sample_shots_by_class_id(class_id: int, num_shots: int)List[int][source]

Random sample shots of given class id.

class mmfewshot.classification.datasets.CUBDataset(classes_id_seed: Optional[int] = None, subset: typing_extensions.Literal[train, test, val] = 'train', *args, **kwargs)[source]

CUB dataset for few shot classification.

Parameters
  • classes_id_seed (int | None) – A random seed to shuffle order of classes. If seed is None, the classes will be arranged in alphabetical order. Default: None.

  • subset (str| list[str]) – The classes of whole dataset are split into three disjoint subset: train, val and test. If subset is a string, only one subset data will be loaded. If subset is a list of string, then all data of subset in list will be loaded. Options: [‘train’, ‘val’, ‘test’]. Default: ‘train’.

get_classes(classes: Optional[Union[Sequence[str], str]] = None)Sequence[str][source]

Get class names of current dataset.

Parameters

classes (Sequence[str] | str | None) –

Three types of input will correspond to different processing logics:

  • If classes is a tuple or list, it will override the CLASSES predefined in the dataset.

  • If classes is None, we directly use pre-defined CLASSES will be used by the dataset.

  • If classes is a string, it is the path of a classes file that contains the name of all classes. Each line of the file contains a single class name.

Returns

Names of categories of the dataset.

Return type

tuple[str] or list[str]

load_annotations()List[Dict][source]

Load annotation according to the classes subset.

class mmfewshot.classification.datasets.EpisodicDataset(dataset: torch.utils.data.dataset.Dataset, num_episodes: int, num_ways: int, num_shots: int, num_queries: int, episodes_seed: Optional[int] = None)[source]

A wrapper of episodic dataset.

It will generate a list of support and query images indices for each episode (support + query images). Every call of __getitem__ will fetch and return (num_ways * num_shots) support images and (num_ways * num_queries) query images according to the generated images indices. Note that all the episode indices are generated at once using a specific random seed to ensure the reproducibility for same dataset.

Parameters
  • dataset (Dataset) – The dataset to be wrapped.

  • num_episodes (int) – Number of episodes. Noted that all episodes are generated at once and will not be changed afterwards. Make sure setting the num_episodes larger than your needs.

  • num_ways (int) – Number of ways for each episode.

  • num_shots (int) – Number of support data of each way for each episode.

  • num_queries (int) – Number of query data of each way for each episode.

  • episodes_seed (int | None) – A random seed to reproduce episodic indices. If seed is None, it will use runtime random seed. Default: None.

class mmfewshot.classification.datasets.LoadImageFromBytes(to_float32=False, color_type='color', file_client_args={'backend': 'disk'})[source]

Load an image from bytes.

class mmfewshot.classification.datasets.MetaTestDataset(*args, **kwargs)[source]

A wrapper of the episodic dataset for meta testing.

During meta test, the MetaTestDataset will be copied and converted into three mode: test_set, support, and test. Each mode of dataset will be used in different dataloader, but they share the same episode and image information.

  • In test_set mode, the dataset will fetch all images from the whole test set to extract features from the fixed backbone, which can accelerate meta testing.

  • In support or query mode, the dataset will fetch images according to the episode_idxes with the same task_id. Therefore, the support and query dataset must be set to the same task_id in each test task.

cache_feats(feats: torch.Tensor, img_metas: Dict)None[source]

Cache extracted feats into dataset.

set_task_id(task_id: int)None[source]

Query and support dataset use same task id to make sure fetch data from same episode.

class mmfewshot.classification.datasets.MiniImageNetDataset(subset: typing_extensions.Literal[train, test, val] = 'train', file_format: str = 'JPEG', *args, **kwargs)[source]

MiniImageNet dataset for few shot classification.

Parameters
  • subset (str| list[str]) – The classes of whole dataset are split into three disjoint subset: train, val and test. If subset is a string, only one subset data will be loaded. If subset is a list of string, then all data of subset in list will be loaded. Options: [‘train’, ‘val’, ‘test’]. Default: ‘train’.

  • file_format (str) – The file format of the image. Default: ‘JPEG’

get_classes(classes: Optional[Union[Sequence[str], str]] = None)Sequence[str][source]

Get class names of current dataset.

Parameters

classes (Sequence[str] | str | None) –

Three types of input will correspond to different processing logics:

  • If classes is a tuple or list, it will override the CLASSES predefined in the dataset.

  • If classes is None, we directly use pre-defined CLASSES will be used by the dataset.

  • If classes is a string, it is the path of a classes file that contains the name of all classes. Each line of the file contains a single class name.

Returns

Names of categories of the dataset.

Return type

tuple[str] or list[str]

load_annotations()List[source]

Load annotation according to the classes subset.

class mmfewshot.classification.datasets.TieredImageNetDataset(subset: typing_extensions.Literal[train, test, val] = 'train', *args, **kwargs)[source]

TieredImageNet dataset for few shot classification.

Parameters

subset (str| list[str]) – The classes of whole dataset are split into three disjoint subset: train, val and test. If subset is a string, only one subset data will be loaded. If subset is a list of string, then all data of subset in list will be loaded. Options: [‘train’, ‘val’, ‘test’]. Default: ‘train’.

get_classes(classes: Optional[Union[Sequence[str], str]] = None)Sequence[str][source]

Get class names of current dataset.

Parameters

classes (Sequence[str] | str | None) –

Three types of input will correspond to different processing logics:

  • If classes is a tuple or list, it will override the CLASSES predefined in the dataset.

  • If classes is None, we directly use pre-defined CLASSES will be used by the dataset.

  • If classes is a string, it is the path of a classes file that contains the name of all classes. Each line of the file contains a single class name.

Returns

Names of categories of the dataset.

Return type

tuple[str] or list[str]

get_general_classes()List[str][source]

Get general classes of each classes.

load_annotations()List[Dict][source]

Load annotation according to the classes subset.

mmfewshot.classification.datasets.build_dataloader(dataset: torch.utils.data.dataset.Dataset, samples_per_gpu: int, workers_per_gpu: int, num_gpus: int = 1, dist: bool = True, shuffle: bool = True, round_up: bool = True, seed: Optional[int] = None, pin_memory: bool = False, use_infinite_sampler: bool = False, **kwargs)torch.utils.data.dataloader.DataLoader[source]

Build PyTorch DataLoader.

In distributed training, each GPU/process has a dataloader. In non-distributed training, there is only one dataloader for all GPUs.

Parameters
  • dataset (Dataset) – A PyTorch dataset.

  • samples_per_gpu (int) – Number of training samples on each GPU, i.e., batch size of each GPU.

  • workers_per_gpu (int) – How many subprocesses to use for data loading for each GPU.

  • num_gpus (int) – Number of GPUs. Only used in non-distributed training.

  • dist (bool) – Distributed training/test or not. Default: True.

  • shuffle (bool) – Whether to shuffle the data at every epoch. Default: True.

  • round_up (bool) – Whether to round up the length of dataset by adding extra samples to make it evenly divisible. Default: True.

  • seed (int | None) – Random seed. Default:None.

  • pin_memory (bool) – Whether to use pin_memory for dataloader. Default: False.

  • use_infinite_sampler (bool) – Whether to use infinite sampler. Noted that infinite sampler will keep iterator of dataloader running forever, which can avoid the overhead of worker initialization between epochs. Default: False.

  • kwargs – any keyword argument to be used to initialize DataLoader

Returns

A PyTorch dataloader.

Return type

DataLoader

mmfewshot.classification.datasets.build_meta_test_dataloader(dataset: torch.utils.data.dataset.Dataset, meta_test_cfg: Dict, **kwargs)torch.utils.data.dataloader.DataLoader[source]

Build PyTorch DataLoader.

In distributed training, each GPU/process has a dataloader. In non-distributed training, there is only one dataloader for all GPUs.

Parameters
  • dataset (Dataset) – A PyTorch dataset.

  • meta_test_cfg (dict) – Config of meta testing.

  • kwargs – any keyword argument to be used to initialize DataLoader

Returns

support_data_loader, query_data_loader

and test_set_data_loader.

Return type

tuple[Dataloader]

mmfewshot.classification.datasets.label_wrapper(labels: Union[torch.Tensor, numpy.ndarray, List], class_ids: List[int])Union[torch.Tensor, numpy.ndarray, list][source]

Map input labels into range of 0 to numbers of classes-1.

It is usually used in the meta testing phase, in which the class ids are random sampled and discontinuous.

Parameters
  • labels (Tensor | np.ndarray | list) – The labels to be wrapped.

  • class_ids (list[int]) – All class ids of labels.

Returns

Same type as the input labels.

Return type

(Tensor | np.ndarray | list)

classification.models

classification.utils

class mmfewshot.classification.utils.MetaTestParallel(module: torch.nn.modules.module.Module, dim: int = 0)[source]

The MetaTestParallel module that supports DataContainer.

Note that each task is tested on a single GPU. Thus the data and model on different GPU should be independent. MMDistributedDataParallel always automatically synchronizes the grad in different GPUs when doing the loss backward, which can not meet the requirements. Thus we simply copy the module and wrap it with an MetaTestParallel, which will send data to the device model.

MetaTestParallel has two main differences with PyTorch DataParallel:

  • It supports a custom type DataContainer which allows more flexible control of input data during both GPU and CPU inference.

  • It implement three more APIs before_meta_test(), before_forward_support() and before_forward_query().

Parameters
  • module (nn.Module) – Module to be encapsulated.

  • dim (int) – Dimension used to scatter the data. Defaults to 0.

forward(*inputs, **kwargs)[source]

Override the original forward function.

The main difference lies in the CPU inference where the data in DataContainers will still be gathered.

mmfewshot.detection

detection.apis

mmfewshot.detection.apis.inference_detector(model: torch.nn.modules.module.Module, imgs: Union[List[str], str])List[source]

Inference images with the detector.

Parameters
  • model (nn.Module) – Detector.

  • imgs (list[str] | str) – Batch or single image file.

Returns

If imgs is a list or tuple, the same length list type results

will be returned, otherwise return the detection results directly.

Return type

list

mmfewshot.detection.apis.init_detector(config: Union[str, mmcv.utils.config.Config], checkpoint: Optional[str] = None, device: str = 'cuda:0', cfg_options: Optional[Dict] = None, classes: Optional[List[str]] = None)torch.nn.modules.module.Module[source]

Prepare a detector from config file.

Parameters
  • config (str | mmcv.Config) – Config file path or the config object.

  • checkpoint (str | None) – Checkpoint path. If left as None, the model will not load any weights.

  • device (str) – Runtime device. Default: ‘cuda:0’.

  • cfg_options (dict | None) – Options to override some settings in the used config.

  • classes (list[str] | None) – Options to override classes name of model. Default: None.

Returns

The constructed detector.

Return type

nn.Module

mmfewshot.detection.apis.multi_gpu_model_init(model: torch.nn.modules.module.Module, data_loader: torch.utils.data.dataloader.DataLoader)List[source]

Forward support images for meta-learning based detector initialization.

The function usually will be called before single_gpu_test in QuerySupportEvalHook. It firstly forwards support images with mode=model_init and the features will be saved in the model. Then it will call :func:model_init to process the extracted features of support images to finish the model initialization.

Noted that the data_loader should NOT use distributed sampler, all the models in different gpus should be initialized with same images.

Parameters
  • model (nn.Module) – Model used for extracting support template features.

  • data_loader (nn.Dataloader) – Pytorch data loader.

Returns

Extracted support template features.

Return type

list[Tensor]

mmfewshot.detection.apis.multi_gpu_test(model: torch.nn.modules.module.Module, data_loader: torch.utils.data.dataloader.DataLoader, tmpdir: Optional[str] = None, gpu_collect: bool = False)List[source]

Test model with multiple gpus for meta-learning based detector.

The model forward function requires mode, while in mmdet it requires return_loss. And the encode_mask_results is removed. This method tests model with multiple gpus and collects the results under two different modes: gpu and cpu modes. By setting ‘gpu_collect=True’ it encodes results to gpu tensors and use gpu communication for results collection. On cpu mode it saves the results on different gpus to ‘tmpdir’ and collects them by the rank 0 worker.

Parameters
  • model (nn.Module) – Model to be tested.

  • data_loader (Dataloader) – Pytorch data loader.

  • tmpdir (str) – Path of directory to save the temporary results from different gpus under cpu mode. Default: None.

  • gpu_collect (bool) – Option to use either gpu or cpu to collect results. Default: False.

Returns

The prediction results.

Return type

list

mmfewshot.detection.apis.process_support_images(model: torch.nn.modules.module.Module, support_imgs: List[str], support_labels: List[List[str]], support_bboxes: Optional[List[List[float]]] = None, classes: Optional[List[str]] = None)None[source]

Process support images for query support detector.

Parameters
  • model (nn.Module) – Detector.

  • support_imgs (list[str]) – Support image filenames.

  • support_labels (list[list[str]]) – Support labels of each bbox.

  • support_bboxes (list[list[list[float]]] | None) – Bbox in support images. If it set to None, it will use the [0, 0, image width, image height] as bbox. Default: None.

  • classes (list[str] | None) – Options to override classes name of model. Default: None.

mmfewshot.detection.apis.single_gpu_model_init(model: torch.nn.modules.module.Module, data_loader: torch.utils.data.dataloader.DataLoader)List[source]

Forward support images for meta-learning based detector initialization.

The function usually will be called before single_gpu_test in QuerySupportEvalHook. It firstly forwards support images with mode=model_init and the features will be saved in the model. Then it will call :func:model_init to process the extracted features of support images to finish the model initialization.

Parameters
  • model (nn.Module) – Model used for extracting support template features.

  • data_loader (nn.Dataloader) – Pytorch data loader.

Returns

Extracted support template features.

Return type

list[Tensor]

mmfewshot.detection.apis.single_gpu_test(model: torch.nn.modules.module.Module, data_loader: torch.utils.data.dataloader.DataLoader, show: bool = False, out_dir: Optional[str] = None, show_score_thr: float = 0.3)List[source]

Test model with single gpu for meta-learning based detector.

The model forward function requires mode, while in mmdet it requires return_loss. And the encode_mask_results is removed.

Parameters
  • model (nn.Module) – Model to be tested.

  • data_loader (DataLoader) – Pytorch data loader.

  • show (bool) – Whether to show the image. Default: False.

  • out_dir (str | None) – The directory to write the image. Default: None.

  • show_score_thr (float) – Minimum score of bboxes to be shown. Default: 0.3.

Returns

The prediction results.

Return type

list

detection.core

detection.datasets

class mmfewshot.detection.datasets.BaseFewShotDataset(ann_cfg: List[Dict], classes: Optional[Union[str, Sequence[str]]], pipeline: Optional[List[Dict]] = None, multi_pipelines: Optional[Dict[str, List[Dict]]] = None, data_root: Optional[str] = None, img_prefix: str = '', seg_prefix: Optional[str] = None, proposal_file: Optional[str] = None, test_mode: bool = False, filter_empty_gt: bool = True, min_bbox_size: Optional[Union[float, int]] = None, ann_shot_filter: Optional[Dict] = None, instance_wise: bool = False, dataset_name: Optional[str] = None)[source]

Base dataset for few shot detection.

The main differences with normal detection dataset fall in two aspects.

  • It allows to specify single (used in normal dataset) or multiple

    (used in query-support dataset) pipelines for data processing.

  • It supports to control the maximum number of instances of each class

    when loading the annotation file.

The annotation format is shown as follows. The ann field is optional for testing.

[
    {
        'id': '0000001'
        'filename': 'a.jpg',
        'width': 1280,
        'height': 720,
        'ann': {
            'bboxes': <np.ndarray> (n, 4) in (x1, y1, x2, y2) order.
            'labels': <np.ndarray> (n, ),
            'bboxes_ignore': <np.ndarray> (k, 4), (optional field)
            'labels_ignore': <np.ndarray> (k, 4) (optional field)
        }
    },
    ...
]
Parameters
  • ann_cfg (list[dict]) –

    Annotation config support two type of config.

    • loading annotation from common ann_file of dataset with or without specific classes. example:dict(type=’ann_file’, ann_file=’path/to/ann_file’, ann_classes=[‘dog’, ‘cat’])

    • loading annotation from a json file saved by dataset. example:dict(type=’saved_dataset’, ann_file=’path/to/ann_file’)

  • classes (str | Sequence[str] | None) – Classes for model training and provide fixed label for each class.

  • pipeline (list[dict] | None) – Config to specify processing pipeline. Used in normal dataset. Default: None.

  • multi_pipelines (dict[list[dict]]) –

    Config to specify data pipelines for corresponding data flow. For example, query and support data can be processed with two different pipelines, the dict should contain two keys like:

    • query (list[dict]): Config for query-data process pipeline.

    • support (list[dict]): Config for support-data process pipeline.

  • data_root (str | None) – Data root for ann_cfg, img_prefix`, seg_prefix, proposal_file if specified. Default: None.

  • test_mode (bool) – If set True, annotation will not be loaded. Default: False.

  • filter_empty_gt (bool) – If set true, images without bounding boxes of the dataset’s classes will be filtered out. This option only works when test_mode=False, i.e., we never filter images during tests. Default: True.

  • min_bbox_size (int | float | None) – The minimum size of bounding boxes in the images. If the size of a bounding box is less than min_bbox_size, it would be added to ignored field. Default: None.

  • ann_shot_filter (dict | None) – Used to specify the class and the corresponding maximum number of instances when loading the annotation file. For example: {‘dog’: 10, ‘person’: 5}. If set it as None, all annotation from ann file would be loaded. Default: None.

  • instance_wise (bool) – If set true, self.data_infos would change to instance-wise, which means if the annotation of single image has more than one instance, the annotation would be split to num_instances items. Often used in support datasets, Default: False.

  • dataset_name (str | None) – Name of dataset to display. For example: ‘train_dataset’ or ‘query_dataset’. Default: None.

ann_cfg_parser(ann_cfg: List[Dict])List[Dict][source]

Parse annotation config to annotation information.

Parameters

ann_cfg (list[dict]) –

Annotation config support two type of config.

  • ’ann_file’: loading annotation from common ann_file of

    dataset. example: dict(type=’ann_file’, ann_file=’path/to/ann_file’, ann_classes=[‘dog’, ‘cat’])

  • ’saved_dataset’: loading annotation from saved dataset.

    example:dict(type=’saved_dataset’, ann_file=’path/to/ann_file’)

Returns

Annotation information.

Return type

list[dict]

get_ann_info(idx: int)Dict[source]

Get annotation by index.

When override this function please make sure same annotations are used during the whole training.

Parameters

idx (int) – Index of data.

Returns

Annotation info of specified index.

Return type

dict

load_annotations_saved(ann_file: str)List[Dict][source]

Load data_infos from saved json.

prepare_train_img(idx: int, pipeline_key: Optional[str] = None, gt_idx: Optional[List[int]] = None)Dict[source]

Get training data and annotations after pipeline.

Parameters
  • idx (int) – Index of data.

  • pipeline_key (str) – Name of pipeline

  • gt_idx (list[int]) – Index of used annotation.

Returns

Training data and annotation after pipeline with new keys introduced by pipeline.

Return type

dict

save_data_infos(output_path: str)None[source]

Save data_infos into json.

class mmfewshot.detection.datasets.CropResizeInstance(num_context_pixels: int = 16, target_size: Tuple[int] = (320, 320))[source]

Crop and resize instance according to bbox form image.

Parameters
  • num_context_pixels (int) – Padding pixel around instance. Default: 16.

  • target_size (tuple[int, int]) – Resize cropped instance to target size. Default: (320, 320).

class mmfewshot.detection.datasets.FewShotCocoDataset(classes: Optional[Union[Sequence[str], str]] = None, num_novel_shots: Optional[int] = None, num_base_shots: Optional[int] = None, ann_shot_filter: Optional[Dict[str, int]] = None, min_bbox_area: Optional[Union[float, int]] = None, dataset_name: Optional[str] = None, test_mode: bool = False, **kwargs)[source]

COCO dataset for few shot detection.

Parameters
  • classes (str | Sequence[str] | None) – Classes for model training and provide fixed label for each class. When classes is string, it will load pre-defined classes in FewShotCocoDataset. For example: ‘BASE_CLASSES’, ‘NOVEL_CLASSES` or ALL_CLASSES.

  • num_novel_shots (int | None) – Max number of instances used for each novel class. If is None, all annotation will be used. Default: None.

  • num_base_shots (int | None) – Max number of instances used for each base class. If is None, all annotation will be used. Default: None.

  • ann_shot_filter (dict | None) – Used to specify the class and the corresponding maximum number of instances when loading the annotation file. For example: {‘dog’: 10, ‘person’: 5}. If set it as None, ann_shot_filter will be created according to num_novel_shots and num_base_shots.

  • min_bbox_area (int | float | None) – Filter images with bbox whose area smaller min_bbox_area. If set to None, skip this filter. Default: None.

  • dataset_name (str | None) – Name of dataset to display. For example: ‘train dataset’ or ‘query dataset’. Default: None.

  • test_mode (bool) – If set True, annotation will not be loaded. Default: False.

evaluate(results: List[Sequence], metric: Union[str, List[str]] = 'bbox', logger: Optional[object] = None, jsonfile_prefix: Optional[str] = None, classwise: bool = False, proposal_nums: Sequence[int] = (100, 300, 1000), iou_thrs: Optional[Union[float, Sequence[float]]] = None, metric_items: Optional[Union[str, List[str]]] = None, class_splits: Optional[List[str]] = None)Dict[source]

Evaluation in COCO protocol and summary results of different splits of classes.

Parameters
  • results (list[list | tuple]) – Testing results of the dataset.

  • metric (str | list[str]) – Metrics to be evaluated. Options are ‘bbox’, ‘proposal’, ‘proposal_fast’. Default: ‘bbox’

  • logger (logging.Logger | None) – Logger used for printing related information during evaluation. Default: None.

  • jsonfile_prefix (str | None) – The prefix of json files. It includes the file path and the prefix of filename, e.g., “a/b/prefix”. If not specified, a temp file will be created. Default: None.

  • classwise (bool) – Whether to evaluating the AP for each class.

  • proposal_nums (Sequence[int]) – Proposal number used for evaluating recalls, such as recall@100, recall@1000. Default: (100, 300, 1000).

  • iou_thrs (Sequence[float] | float | None) – IoU threshold used for evaluating recalls/mAPs. If set to a list, the average of all IoUs will also be computed. If not specified, [0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95] will be used. Default: None.

  • metric_items (list[str] | str | None) – Metric items that will be returned. If not specified, ['AR@100', 'AR@300', 'AR@1000', 'AR_s@1000', 'AR_m@1000', 'AR_l@1000' ] will be used when metric=='proposal', ['mAP', 'mAP_50', 'mAP_75', 'mAP_s', 'mAP_m', 'mAP_l'] will be used when metric=='bbox'.

  • class_splits – (list[str] | None): Calculate metric of classes split in COCO_SPLIT. For example: [‘BASE_CLASSES’, ‘NOVEL_CLASSES’]. Default: None.

Returns

COCO style evaluation metric.

Return type

dict[str, float]

get_cat_ids(idx: int)List[int][source]

Get category ids by index.

Overwrite the function in CocoDataset.

Parameters

idx (int) – Index of data.

Returns

All categories in the image of specified index.

Return type

list[int]

get_classes(classes: Union[str, Sequence[str]])List[str][source]

Get class names.

It supports to load pre-defined classes splits. The pre-defined classes splits are: [‘ALL_CLASSES’, ‘NOVEL_CLASSES’, ‘BASE_CLASSES’]

Parameters

classes (str | Sequence[str]) – Classes for model training and provide fixed label for each class. When classes is string, it will load pre-defined classes in FewShotCocoDataset. For example: ‘NOVEL_CLASSES’.

Returns

list of class names.

Return type

list[str]

load_annotations(ann_cfg: List[Dict])List[Dict][source]

Support to Load annotation from two type of ann_cfg.

  • type of ‘ann_file’: COCO-style annotation file.

  • type of ‘saved_dataset’: Saved COCO dataset json.

Parameters

ann_cfg (list[dict]) – Config of annotations.

Returns

Annotation infos.

Return type

list[dict]

load_annotations_coco(ann_file: str)List[Dict][source]

Load annotation from COCO style annotation file.

Parameters

ann_file (str) – Path of annotation file.

Returns

Annotation info from COCO api.

Return type

list[dict]

class mmfewshot.detection.datasets.FewShotVOCDataset(classes: Optional[Union[Sequence[str], str]] = None, num_novel_shots: Optional[int] = None, num_base_shots: Optional[int] = None, ann_shot_filter: Optional[Dict] = None, use_difficult: bool = False, min_bbox_area: Optional[Union[float, int]] = None, dataset_name: Optional[str] = None, test_mode: bool = False, coordinate_offset: List[int] = [- 1, - 1, 0, 0], **kwargs)[source]

VOC dataset for few shot detection.

Parameters
  • classes (str | Sequence[str]) – Classes for model training and provide fixed label for each class. When classes is string, it will load pre-defined classes in FewShotVOCDataset. For example: ‘NOVEL_CLASSES_SPLIT1’.

  • num_novel_shots (int | None) – Max number of instances used for each novel class. If is None, all annotation will be used. Default: None.

  • num_base_shots (int | None) – Max number of instances used for each base class. When it is None, all annotations will be used. Default: None.

  • ann_shot_filter (dict | None) – Used to specify the class and the corresponding maximum number of instances when loading the annotation file. For example: {‘dog’: 10, ‘person’: 5}. If set it as None, ann_shot_filter will be created according to num_novel_shots and num_base_shots. Default: None.

  • use_difficult (bool) – Whether use the difficult annotation or not. Default: False.

  • min_bbox_area (int | float | None) – Filter images with bbox whose area smaller min_bbox_area. If set to None, skip this filter. Default: None.

  • dataset_name (str | None) – Name of dataset to display. For example: ‘train dataset’ or ‘query dataset’. Default: None.

  • test_mode (bool) – If set True, annotation will not be loaded. Default: False.

  • coordinate_offset (list[int]) – The bbox annotation will add the coordinate offsets which corresponds to [x_min, y_min, x_max, y_max] during training. For testing, the gt annotation will not be changed while the predict results will minus the coordinate offsets to inverse data loading logic in training. Default: [-1, -1, 0, 0].

evaluate(results: List[Sequence], metric: Union[str, List[str]] = 'mAP', logger: Optional[object] = None, proposal_nums: Sequence[int] = (100, 300, 1000), iou_thr: Optional[Union[float, Sequence[float]]] = 0.5, class_splits: Optional[List[str]] = None)Dict[source]

Evaluation in VOC protocol and summary results of different splits of classes.

Parameters
  • results (list[list | tuple]) – Predictions of the model.

  • metric (str | list[str]) – Metrics to be evaluated. Options are ‘mAP’, ‘recall’. Default: mAP.

  • logger (logging.Logger | None) – Logger used for printing related information during evaluation. Default: None.

  • proposal_nums (Sequence[int]) – Proposal number used for evaluating recalls, such as recall@100, recall@1000. Default: (100, 300, 1000).

  • iou_thr (float | list[float]) – IoU threshold. Default: 0.5.

  • class_splits – (list[str] | None): Calculate metric of classes split defined in VOC_SPLIT. For example: [‘BASE_CLASSES_SPLIT1’, ‘NOVEL_CLASSES_SPLIT1’]. Default: None.

Returns

AP/recall metrics.

Return type

dict[str, float]

get_classes(classes: Union[str, Sequence[str]])List[str][source]

Get class names.

It supports to load pre-defined classes splits. The pre-defined classes splits are: [‘ALL_CLASSES_SPLIT1’, ‘ALL_CLASSES_SPLIT2’, ‘ALL_CLASSES_SPLIT3’,

‘BASE_CLASSES_SPLIT1’, ‘BASE_CLASSES_SPLIT2’, ‘BASE_CLASSES_SPLIT3’, ‘NOVEL_CLASSES_SPLIT1’,’NOVEL_CLASSES_SPLIT2’,’NOVEL_CLASSES_SPLIT3’]

Parameters

classes (str | Sequence[str]) – Classes for model training and provide fixed label for each class. When classes is string, it will load pre-defined classes in FewShotVOCDataset. For example: ‘NOVEL_CLASSES_SPLIT1’.

Returns

List of class names.

Return type

list[str]

load_annotations(ann_cfg: List[Dict])List[Dict][source]

Support to load annotation from two type of ann_cfg.

Parameters
  • ann_cfg (list[dict]) – Support two type of config.

  • loading annotation from common ann_file of dataset (-) – with or without specific classes. example:dict(type=’ann_file’, ann_file=’path/to/ann_file’, ann_classes=[‘dog’, ‘cat’])

  • loading annotation from a json file saved by dataset. (-) – example:dict(type=’saved_dataset’, ann_file=’path/to/ann_file’)

Returns

Annotation information.

Return type

list[dict]

load_annotations_xml(ann_file: str, classes: Optional[List[str]] = None)List[Dict][source]

Load annotation from XML style ann_file.

It supports using image id or image path as image names to load the annotation file.

Parameters
  • ann_file (str) – Path of annotation file.

  • classes (list[str] | None) – Specific classes to load form xml file. If set to None, it will use classes of whole dataset. Default: None.

Returns

Annotation info from XML file.

Return type

list[dict]

class mmfewshot.detection.datasets.GenerateMask(target_size: Tuple[int] = (224, 224))[source]

Resize support image and generate a mask.

Parameters

target_size (tuple[int, int]) – Crop and resize to target size. Default: (224, 224).

class mmfewshot.detection.datasets.NWayKShotDataloader(query_data_loader: torch.utils.data.dataloader.DataLoader, support_data_loader: torch.utils.data.dataloader.DataLoader)[source]

A dataloader wrapper.

It Create a iterator to generate query and support batch simultaneously. Each batch contains query data and support data, and the lengths are batch_size and (num_support_ways * num_support_shots) respectively.

Parameters
  • query_data_loader (DataLoader) – DataLoader of query dataset

  • support_data_loader (DataLoader) – DataLoader of support datasets.

class mmfewshot.detection.datasets.NWayKShotDataset(query_dataset: mmfewshot.detection.datasets.base.BaseFewShotDataset, support_dataset: Optional[mmfewshot.detection.datasets.base.BaseFewShotDataset], num_support_ways: int, num_support_shots: int, one_support_shot_per_image: bool = False, num_used_support_shots: int = 200, repeat_times: int = 1)[source]

A dataset wrapper of NWayKShotDataset.

Building NWayKShotDataset requires query and support dataset, the behavior of NWayKShotDataset is determined by mode. When dataset in ‘query’ mode, dataset will return regular image and annotations. While dataset in ‘support’ mode, dataset will build batch indices firstly and each batch indices contain (num_support_ways * num_support_shots) samples. In other words, for support mode every call of __getitem__ will return a batch of samples, therefore the outside dataloader should set batch_size to 1. The default mode of NWayKShotDataset is ‘query’ and by using convert function convert_query_to_support the mode will be converted into ‘support’.

Parameters
  • query_dataset (BaseFewShotDataset) – Query dataset to be wrapped.

  • support_dataset (BaseFewShotDataset | None) – Support dataset to be wrapped. If support dataset is None, support dataset will copy from query dataset.

  • num_support_ways (int) – Number of classes for support in mini-batch.

  • num_support_shots (int) – Number of support shot for each class in mini-batch.

  • one_support_shot_per_image (bool) – If True only one annotation will be sampled from each image. Default: False.

  • num_used_support_shots (int | None) – The total number of support shots sampled and used for each class during training. If set to None, all shots in dataset will be used as support shot. Default: 200.

  • shuffle_support (bool) – If allow generate new batch indices for each epoch. Default: False.

  • repeat_times (int) – The length of repeated dataset will be times larger than the original dataset. Default: 1.

convert_query_to_support(support_dataset_len: int)None[source]

Convert query dataset to support dataset.

Parameters

support_dataset_len (int) – Length of pre sample batch indices.

generate_support_batch_indices(dataset_len: int)List[List[Tuple[int]]][source]

Generate batch indices from support dataset.

Batch indices is in the shape of [length of datasets * [support way * support shots]]. And the dataset_len will be the length of support dataset.

Parameters

dataset_len (int) – Length of batch indices.

Returns

Pre-sample batch indices.

Return type

list[list[(data_idx, gt_idx)]]

get_support_data_infos()List[Dict][source]

Get support data infos from batch indices.

save_data_infos(output_path: str)None[source]

Save data infos of query and support data.

save_support_data_infos(support_output_path: str)None[source]

Save support data infos.

class mmfewshot.detection.datasets.NumpyEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Save numpy array obj to json.

default(obj: object)object[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
class mmfewshot.detection.datasets.QueryAwareDataset(query_dataset: mmfewshot.detection.datasets.base.BaseFewShotDataset, support_dataset: Optional[mmfewshot.detection.datasets.base.BaseFewShotDataset], num_support_ways: int, num_support_shots: int, repeat_times: int = 1)[source]

A wrapper of QueryAwareDataset.

Building QueryAwareDataset requires query and support dataset. Every call of __getitem__ will firstly sample a query image and its annotations. Then it will use the query annotations to sample a batch of positive and negative support images and annotations. The positive images share same classes with query, while the annotations of negative images don’t have any category from query.

Parameters
  • query_dataset (BaseFewShotDataset) – Query dataset to be wrapped.

  • support_dataset (BaseFewShotDataset | None) – Support dataset to be wrapped. If support dataset is None, support dataset will copy from query dataset.

  • num_support_ways (int) – Number of classes for support in mini-batch, the first one always be the positive class.

  • num_support_shots (int) – Number of support shots for each class in mini-batch, the first K shots always from positive class.

  • repeat_times (int) – The length of repeated dataset will be times larger than the original dataset. Default: 1.

generate_support(idx: int, query_class: int, support_classes: List[int])List[Tuple[int]][source]

Generate support indices of query images.

Parameters
  • idx (int) – Index of query data.

  • query_class (int) – Query class.

  • support_classes (list[int]) – Classes of support data.

Returns

A mini-batch (num_support_ways *

num_support_shots) of support data (idx, gt_idx).

Return type

list[tuple(int)]

get_support_data_infos()List[Dict][source]

Return data_infos of support dataset.

sample_support_shots(idx: int, class_id: int, allow_same_image: bool = False)List[Tuple[int]][source]

Generate support indices according to the class id.

Parameters
  • idx (int) – Index of query data.

  • class_id (int) – Support class.

  • allow_same_image (bool) – Allow instance sampled from same image as query image. Default: False.

Returns

Support data (num_support_shots)

of specific class.

Return type

list[tuple[int]]

save_data_infos(output_path: str)None[source]

Save data_infos into json.

mmfewshot.detection.datasets.build_dataloader(dataset: torch.utils.data.dataset.Dataset, samples_per_gpu: int, workers_per_gpu: int, num_gpus: int = 1, dist: bool = True, shuffle: bool = True, seed: Optional[int] = None, data_cfg: Optional[Dict] = None, use_infinite_sampler: bool = False, **kwargs)torch.utils.data.dataloader.DataLoader[source]

Build PyTorch DataLoader.

In distributed training, each GPU/process has a dataloader. In non-distributed training, there is only one dataloader for all GPUs.

Parameters
  • dataset (Dataset) – A PyTorch dataset.

  • samples_per_gpu (int) – Number of training samples on each GPU, i.e., batch size of each GPU.

  • workers_per_gpu (int) – How many subprocesses to use for data loading for each GPU.

  • num_gpus (int) – Number of GPUs. Only used in non-distributed training. Default:1.

  • dist (bool) – Distributed training/test or not. Default: True.

  • shuffle (bool) – Whether to shuffle the data at every epoch. Default: True.

  • seed (int) – Random seed. Default:None.

  • data_cfg (dict | None) – Dict of data configure. Default: None.

  • use_infinite_sampler (bool) – Whether to use infinite sampler. Noted that infinite sampler will keep iterator of dataloader running forever, which can avoid the overhead of worker initialization between epochs. Default: False.

  • kwargs – any keyword argument to be used to initialize DataLoader

Returns

A PyTorch dataloader.

Return type

DataLoader

mmfewshot.detection.datasets.get_copy_dataset_type(dataset_type: str)str[source]

Return corresponding copy dataset type.

detection.models

mmfewshot.detection.models.build_backbone(cfg)[source]

Build backbone.

mmfewshot.detection.models.build_detector(cfg: mmcv.utils.config.ConfigDict, logger: Optional[object] = None)[source]

Build detector.

mmfewshot.detection.models.build_head(cfg)[source]

Build head.

mmfewshot.detection.models.build_loss(cfg)[source]

Build loss.

mmfewshot.detection.models.build_neck(cfg)[source]

Build neck.

mmfewshot.detection.models.build_roi_extractor(cfg)[source]

Build roi extractor.

mmfewshot.detection.models.build_shared_head(cfg)[source]

Build shared head.

detection.utils

class mmfewshot.detection.utils.ContrastiveLossDecayHook(decay_steps: Sequence[int], decay_rate: float = 0.5)

Hook for contrast loss weight decay used in FSCE.

Parameters
  • decay_steps (list[int] | tuple[int]) – Each item in the list is the step to decay the loss weight.

  • decay_rate (float) – Decay rate. Default: 0.5.

mmfewshot.utils

class mmfewshot.utils.DistributedInfiniteGroupSampler(dataset: Iterable, samples_per_gpu: int = 1, num_replicas: Optional[int] = None, rank: Optional[int] = None, seed: int = 0, shuffle: bool = True)[source]

Similar to InfiniteGroupSampler but in distributed version.

The length of sampler is set to the actual length of dataset, thus the length of dataloader is still determined by the dataset. The implementation logic is referred to https://github.com/facebookresearch/detectron2/blob/main/detectron2/data/samplers/grouped_batch_sampler.py

Parameters
  • dataset (Iterable) – The dataset.

  • samples_per_gpu (int) – Number of training samples on each GPU, i.e., batch size of each GPU. Default: 1.

  • num_replicas (int | None) – Number of processes participating in distributed training. Default: None.

  • rank (int | None) – Rank of current process. Default: None.

  • seed (int) – Random seed. Default: 0.

  • shuffle (bool) – Whether shuffle the indices of a dummy epoch, it should be noted that shuffle can not guarantee that you can generate sequential indices because it need to ensure that all indices in a batch is in a group. Default: True.

class mmfewshot.utils.DistributedInfiniteSampler(dataset: Iterable, num_replicas: Optional[int] = None, rank: Optional[int] = None, seed: int = 0, shuffle: bool = True)[source]

Similar to InfiniteSampler but in distributed version.

The length of sampler is set to the actual length of dataset, thus the length of dataloader is still determined by the dataset. The implementation logic is referred to https://github.com/facebookresearch/detectron2/blob/main/detectron2/data/samplers/grouped_batch_sampler.py

Parameters
  • dataset (Iterable) – The dataset.

  • num_replicas (int | None) – Number of processes participating in distributed training. Default: None.

  • rank (int | None) – Rank of current process. Default: None.

  • seed (int) – Random seed. Default: 0.

  • shuffle (bool) – Whether shuffle the dataset or not. Default: True.

class mmfewshot.utils.InfiniteEpochBasedRunner(model, batch_processor=None, optimizer=None, work_dir=None, logger=None, meta=None, max_iters=None, max_epochs=None)[source]

Epoch-based Runner supports dataloader with InfiniteSampler.

The workers of dataloader will re-initialize, when the iterator of dataloader is created. InfiniteSampler is designed to avoid these time consuming operations, since the iterator with InfiniteSampler will never reach the end.

class mmfewshot.utils.InfiniteGroupSampler(dataset: Iterable, samples_per_gpu: int = 1, seed: int = 0, shuffle: bool = True)[source]

Similar to InfiniteSampler, but all indices in a batch should be in the same group of flag.

The length of sampler is set to the actual length of dataset, thus the length of dataloader is still determined by the dataset. The implementation logic is referred to https://github.com/facebookresearch/detectron2/blob/main/detectron2/data/samplers/grouped_batch_sampler.py

Parameters
  • dataset (Iterable) – The dataset.

  • samples_per_gpu (int) – Number of training samples on each GPU, i.e., batch size of each GPU. Default: 1.

  • seed (int) – Random seed. Default: 0.

  • shuffle (bool) – Whether shuffle the indices of a dummy epoch, it should be noted that shuffle can not guarantee that you can generate sequential indices because it need to ensure that all indices in a batch is in a group. Default: True.

class mmfewshot.utils.InfiniteSampler(dataset: Iterable, seed: int = 0, shuffle: bool = True)[source]

Return a infinite stream of index.

The length of sampler is set to the actual length of dataset, thus the length of dataloader is still determined by the dataset. The implementation logic is referred to https://github.com/facebookresearch/detectron2/blob/main/detectron2/data/samplers/grouped_batch_sampler.py

Parameters
  • dataset (Iterable) – The dataset.

  • seed (int) – Random seed. Default: 0.

  • shuffle (bool) – Whether shuffle the dataset or not. Default: True.

mmfewshot.utils.local_numpy_seed(seed: Optional[int] = None)None[source]

Run numpy codes with a local random seed.

If seed is None, the default random state will be used.

mmfewshot.utils.multi_pipeline_collate_fn(batch, samples_per_gpu: int = 1)[source]

Puts each data field into a tensor/DataContainer with outer dimension batch size. This is designed to support the case that the __getitem__() of dataset return more than one images, such as query_support dataloader. The main difference with the collate_fn() in mmcv is it can process list[list[DataContainer]].

Extend default_collate to add support for :type:`~mmcv.parallel.DataContainer`. There are 3 cases:

  1. cpu_only = True, e.g., meta data.

  2. cpu_only = False, stack = True, e.g., images tensors.

  3. cpu_only = False, stack = False, e.g., gt bboxes.

:param batch (list[list[mmcv.parallel.DataContainer]] |: list[mmcv.parallel.DataContainer]): Data of

single batch.

Parameters

samples_per_gpu (int) – The number of samples of single GPU.

Read the Docs v: stable
Versions
latest
stable
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.