fakedata.py 2.51 KB
Newer Older
1
import torch
Philip Meier's avatar
Philip Meier committed
2
from typing import Any, Callable, Optional, Tuple
3
from .vision import VisionDataset
4
5
from .. import transforms

Soumith Chintala's avatar
Soumith Chintala committed
6

7
class FakeData(VisionDataset):
8
9
10
11
12
    """A fake dataset that returns randomly generated images and returns them as PIL images

    Args:
        size (int, optional): Size of the dataset. Default: 1000 images
        image_size(tuple, optional): Size if the returned images. Default: (3, 224, 224)
t-rutten's avatar
t-rutten committed
13
        num_classes(int, optional): Number of classes in the dataset. Default: 10
14
15
16
17
        transform (callable, optional): A function/transform that  takes in an PIL image
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
18
19
        random_offset (int): Offsets the index-based random seed used to
            generate each image. Default: 0
20
21
22

    """

Philip Meier's avatar
Philip Meier committed
23
24
25
26
27
28
29
30
31
32
    def __init__(
            self,
            size: int = 1000,
            image_size: Tuple[int, int, int] = (3, 224, 224),
            num_classes: int = 10,
            transform: Optional[Callable] = None,
            target_transform: Optional[Callable] = None,
            random_offset: int = 0,
    ) -> None:
        super(FakeData, self).__init__(None, transform=transform,  # type: ignore[arg-type]
Zhiqiang Wang's avatar
Zhiqiang Wang committed
33
                                       target_transform=target_transform)
34
35
36
        self.size = size
        self.num_classes = num_classes
        self.image_size = image_size
37
        self.random_offset = random_offset
38

Philip Meier's avatar
Philip Meier committed
39
    def __getitem__(self, index: int) -> Tuple[Any, Any]:
40
41
42
43
44
45
46
47
        """
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is class_index of the target class.
        """
        # create random image that is consistent with the index id
Will Frey's avatar
Will Frey committed
48
49
        if index >= len(self):
            raise IndexError("{} index out of range".format(self.__class__.__name__))
50
        rng_state = torch.get_rng_state()
51
        torch.manual_seed(index + self.random_offset)
52
        img = torch.randn(*self.image_size)
vfdev's avatar
vfdev committed
53
        target = torch.randint(0, self.num_classes, size=(1,), dtype=torch.long)[0]
54
55
56
57
58
59
60
61
62
        torch.set_rng_state(rng_state)

        # convert to PIL Image
        img = transforms.ToPILImage()(img)
        if self.transform is not None:
            img = self.transform(img)
        if self.target_transform is not None:
            target = self.target_transform(target)

63
        return img, target.item()
64

Philip Meier's avatar
Philip Meier committed
65
    def __len__(self) -> int:
66
        return self.size