Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

fakedata.py 2.4 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  1. from typing import Any, Callable, Optional, Tuple
  2. import torch
  3. from .. import transforms
  4. from .vision import VisionDataset
  5. class FakeData(VisionDataset):
  6. """A fake dataset that returns randomly generated images and returns them as PIL images
  7. Args:
  8. size (int, optional): Size of the dataset. Default: 1000 images
  9. image_size(tuple, optional): Size if the returned images. Default: (3, 224, 224)
  10. num_classes(int, optional): Number of classes in the dataset. Default: 10
  11. transform (callable, optional): A function/transform that takes in a PIL image
  12. and returns a transformed version. E.g, ``transforms.RandomCrop``
  13. target_transform (callable, optional): A function/transform that takes in the
  14. target and transforms it.
  15. random_offset (int): Offsets the index-based random seed used to
  16. generate each image. Default: 0
  17. """
  18. def __init__(
  19. self,
  20. size: int = 1000,
  21. image_size: Tuple[int, int, int] = (3, 224, 224),
  22. num_classes: int = 10,
  23. transform: Optional[Callable] = None,
  24. target_transform: Optional[Callable] = None,
  25. random_offset: int = 0,
  26. ) -> None:
  27. super().__init__(transform=transform, target_transform=target_transform)
  28. self.size = size
  29. self.num_classes = num_classes
  30. self.image_size = image_size
  31. self.random_offset = random_offset
  32. def __getitem__(self, index: int) -> Tuple[Any, Any]:
  33. """
  34. Args:
  35. index (int): Index
  36. Returns:
  37. tuple: (image, target) where target is class_index of the target class.
  38. """
  39. # create random image that is consistent with the index id
  40. if index >= len(self):
  41. raise IndexError(f"{self.__class__.__name__} index out of range")
  42. rng_state = torch.get_rng_state()
  43. torch.manual_seed(index + self.random_offset)
  44. img = torch.randn(*self.image_size)
  45. target = torch.randint(0, self.num_classes, size=(1,), dtype=torch.long)[0]
  46. torch.set_rng_state(rng_state)
  47. # convert to PIL Image
  48. img = transforms.ToPILImage()(img)
  49. if self.transform is not None:
  50. img = self.transform(img)
  51. if self.target_transform is not None:
  52. target = self.target_transform(target)
  53. return img, target.item()
  54. def __len__(self) -> int:
  55. return self.size
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...