コード例 #1
0
    def test_build_dict_field_transform_default_imagenet(self):
        dataset = self.get_test_image_dataset(SampleType.DICT)

        # should apply the transform in the config
        config = [{"name": "ToTensor"}]
        default_transform = transforms.Compose(
            [transforms.CenterCrop(100), transforms.ToTensor()]
        )
        transform = build_field_transform_default_imagenet(
            config, default_transform=default_transform
        )
        sample = dataset[0]
        expected_sample = _apply_transform_to_key_and_copy(
            sample, transforms.ToTensor(), "input"
        )
        self.transform_checks(sample, transform, expected_sample)

        # should apply default_transform
        config = None
        transform = build_field_transform_default_imagenet(
            config, default_transform=default_transform
        )
        expected_sample = _apply_transform_to_key_and_copy(
            sample, default_transform, "input"
        )
        self.transform_checks(sample, transform, expected_sample)

        # should apply the transform for a test split
        transform = build_field_transform_default_imagenet(config, split="test")
        expected_sample = _apply_transform_to_key_and_copy(
            sample, ImagenetNoAugmentTransform(), "input"
        )
        self.transform_checks(sample, transform, expected_sample)
コード例 #2
0
 def from_config(cls, config):
     assert all(key in config
                for key in ["crop_size", "class_ratio", "seed"])
     length = config.get("length")
     crop_size = config["crop_size"]
     class_ratio = config["class_ratio"]
     seed = config["seed"]
     (
         transform_config,
         batchsize_per_replica,
         shuffle,
         num_samples,
     ) = cls.parse_config(config)
     default_transform = transforms.Compose([
         transforms.ToTensor(),
         transforms.Normalize(mean=ImagenetConstants.MEAN,
                              std=ImagenetConstants.STD),
     ])
     transform = build_field_transform_default_imagenet(
         transform_config, default_transform=default_transform)
     return cls(
         batchsize_per_replica,
         shuffle,
         transform,
         num_samples,
         crop_size,
         class_ratio,
         seed,
         length=length,
     )
コード例 #3
0
    def test_imagenet_autoaugment_transform_no_errors(self):
        """
        Tests that the imagenet autoaugment transform runs without any errors.
        """
        dataset = self.get_test_image_dataset()

        config = [{"name": "imagenet_autoaugment"}]
        transform = build_field_transform_default_imagenet(config)
        sample = dataset[0]
        # test that imagenet autoaugment has been registered and runs without errors
        transform(sample)
コード例 #4
0
    def create_image_dataset(
        self,
        image_paths: Union[List[str], str],
        targets: Optional[List[Any]] = None,
        batchsize_per_replica: int = 32,
        shuffle: bool = True,
        transform: Optional[Callable] = None,
        num_samples: Optional[int] = None,
        phase_type: str = "train",
    ) -> ClassyDataset:
        """Create a ClassyDataset which reads images from image_paths.

        Args:
            image_paths: Can be
                - A single directory location, in which case the data is expected to be
                    arranged in a format similar to
                    `:class:torchvision.datasets.ImageFolder`. The targets will
                    be inferred from the directory structure.
                - A list of paths, in which case the list will contain the paths
                    to all the images. In this situation, the targets can be specified
                    by using the targets argument.
            targets: A list containing the target classes for each image
            batchsize_per_replica: Minibatch size per replica (i.e. samples per GPU)
            shuffle: If true, data is shuffled between epochs
            transform: Transform to apply to sample. If left as None, the dataset's
                phase_type is used to determine the transform to apply. The transform
                for the phase_type is searched for in self.task, falling back to
                imagenet transformations if it is not found there.
            num_samples: If specified, limits the number of samples returned by
                the dataset
            phase_type: String specifying the phase_type, e.g. "train" or "test"
        """
        if transform is None:
            if self.task is not None and phase_type in self.task.datasets:
                # use the transform from the dataset for the phase_type
                dataset = self.task.datasets[phase_type]
                transform = dataset.transform
                assert transform is not None, "Cannot infer transform from the task"
            else:
                transform = build_field_transform_default_imagenet(
                    config=None, split=phase_type
                )
        return ImagePathDataset(
            batchsize_per_replica,
            shuffle,
            transform,
            num_samples,
            image_paths,
            targets=targets,
        )
    def test_lighting_transform_no_errors(self):
        """
        Tests that the lighting transform runs without any errors.
        """
        dataset = self.get_test_image_dataset()

        config = [{"name": "ToTensor"}, {"name": "lighting"}]
        transform = build_field_transform_default_imagenet(config)
        sample = dataset[0]
        try:
            # test that lighting has been registered and runs without errors
            transform(sample)
        except Exception:
            self.fail("LightingTransform raised an exception")
        return
コード例 #6
0
    def create_image_dataset(
        self,
        batchsize_per_replica: int = 32,
        shuffle: bool = True,
        transform: Optional[Union[ClassyTransform, Callable]] = None,
        num_samples: Optional[int] = None,
        image_folder: Optional[str] = None,
        image_files: Optional[List[str]] = None,
        phase_type: str = "train",
    ) -> ClassyDataset:
        """Create a ClassyDataset which reads images from image_paths.

        See :class:`dataset.ImagePathDataset` for documentation on image_folder and
        image_files

        Args:
            batchsize_per_replica: Minibatch size per replica (i.e. samples per GPU)
            shuffle: If true, data is shuffled between epochs
            transform: Transform to apply to sample. If left as None, the dataset's
                phase_type is used to determine the transform to apply. The transform
                for the phase_type is searched for in self.task, falling back to
                imagenet transformations if it is not found there.
            num_samples: If specified, limits the number of samples returned by
                the dataset
            phase_type: String specifying the phase_type, e.g. "train" or "test"
        """
        if transform is None:
            if self.task is not None and phase_type in self.task.datasets:
                # use the transform from the dataset for the phase_type
                dataset = self.task.datasets[phase_type]
                transform = dataset.transform
                assert transform is not None, "Cannot infer transform from the task"
            else:
                transform = build_field_transform_default_imagenet(
                    config=None, split=phase_type, key_map_transform=None
                )
        return ImagePathDataset(
            batchsize_per_replica,
            shuffle,
            transform,
            num_samples,
            image_folder=image_folder,
            image_files=image_files,
        )