A callable which takes a dataset dict in Detectron2 Dataset format, and map it into a format used by the model. This is the default callable to be used to map your dataset dict into training data. You may need to follow it to implement your own one for customized logic, such as
| 17 | |
| 18 | |
| 19 | class Mapper: |
| 20 | """ |
| 21 | A callable which takes a dataset dict in Detectron2 Dataset format, |
| 22 | and map it into a format used by the model. |
| 23 | |
| 24 | This is the default callable to be used to map your dataset dict into training data. |
| 25 | You may need to follow it to implement your own one for customized logic, |
| 26 | such as a different way to read or transform images. |
| 27 | See :doc:`/tutorials/data_loading` for details. |
| 28 | |
| 29 | The callable currently does the following: |
| 30 | |
| 31 | 1. Read the image from "file_name" |
| 32 | 2. Applies cropping/geometric transforms to the image and annotations |
| 33 | 3. Prepare data and annotations to Tensor and :class:`Instances` |
| 34 | """ |
| 35 | |
| 36 | def __init__(self, cfg, is_train=True): |
| 37 | |
| 38 | self.tfm_gens = build_transform_gen(cfg, is_train) |
| 39 | # fmt: off |
| 40 | self.img_format = cfg.INPUT.FORMAT |
| 41 | self.mask_on = False |
| 42 | self.mask_format = cfg.INPUT.MASK_FORMAT |
| 43 | self.keypoint_on = False |
| 44 | self.load_proposals = False |
| 45 | self.keypoint_hflip_indices = None |
| 46 | # fmt: on |
| 47 | |
| 48 | self.is_train = is_train |
| 49 | |
| 50 | def __call__(self, dataset_dict): |
| 51 | |
| 52 | dataset_dict = copy.deepcopy(dataset_dict) |
| 53 | image = utils.read_image(dataset_dict["file_name"], format=self.img_format) |
| 54 | utils.check_image_size(dataset_dict, image) |
| 55 | |
| 56 | image, transforms = T.apply_transform_gens(self.tfm_gens, image) |
| 57 | image_shape = image.shape[:2] # h, w |
| 58 | |
| 59 | dataset_dict["image"] = torch.as_tensor(np.ascontiguousarray(image.transpose(2, 0, 1))) |
| 60 | |
| 61 | |
| 62 | if not self.is_train: |
| 63 | dataset_dict.pop("annotations", None) |
| 64 | return dataset_dict |
| 65 | |
| 66 | if "annotations" in dataset_dict: |
| 67 | # USER: Modify this if you want to keep them for some reason. |
| 68 | for anno in dataset_dict["annotations"]: |
| 69 | anno.pop("segmentation", None) |
| 70 | anno.pop("keypoints", None) |
| 71 | |
| 72 | # USER: Implement additional transformations if you have other types of data |
| 73 | annos = [ |
| 74 | utils.transform_instance_annotations( |
| 75 | obj, transforms, image_shape |
| 76 | ) |
no outgoing calls
no test coverage detected