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

#807 Feature/sg 747 add full pipeline with preprocessing

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:feature/SG-747-add_full_pipeline_with_preprocessing
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  1. import numpy as np
  2. import torch
  3. from torchvision.transforms import RandomErasing
  4. from super_gradients.common.object_names import Transforms
  5. from super_gradients.common.registry.registry import register_transform
  6. class DataAugmentation:
  7. @staticmethod
  8. def to_tensor():
  9. def _to_tensor(image):
  10. if len(image.shape) == 3:
  11. return torch.from_numpy(image.transpose(2, 0, 1).astype(np.float32))
  12. else:
  13. return torch.from_numpy(image[None, :, :].astype(np.float32))
  14. return _to_tensor
  15. @staticmethod
  16. def normalize(mean, std):
  17. mean = np.array(mean)
  18. std = np.array(std)
  19. def _normalize(image):
  20. image = np.asarray(image).astype(np.float32) / 255.0
  21. image = (image - mean) / std
  22. return image
  23. return _normalize
  24. @staticmethod
  25. def cutout(mask_size, p=1, cutout_inside=False, mask_color=(0, 0, 0)):
  26. mask_size_half = mask_size // 2
  27. offset = 1 if mask_size % 2 == 0 else 0
  28. def _cutout(image):
  29. image = np.asarray(image).copy()
  30. if np.random.random() > p:
  31. return image
  32. h, w = image.shape[:2]
  33. if cutout_inside:
  34. cxmin, cxmax = mask_size_half, w + offset - mask_size_half
  35. cymin, cymax = mask_size_half, h + offset - mask_size_half
  36. else:
  37. cxmin, cxmax = 0, w + offset
  38. cymin, cymax = 0, h + offset
  39. cx = np.random.randint(cxmin, cxmax)
  40. cy = np.random.randint(cymin, cymax)
  41. xmin = cx - mask_size_half
  42. ymin = cy - mask_size_half
  43. xmax = xmin + mask_size
  44. ymax = ymin + mask_size
  45. xmin = max(0, xmin)
  46. ymin = max(0, ymin)
  47. xmax = min(w, xmax)
  48. ymax = min(h, ymax)
  49. image[ymin:ymax, xmin:xmax] = mask_color
  50. return image
  51. return _cutout
  52. IMAGENET_PCA = {
  53. "eigval": torch.Tensor([0.2175, 0.0188, 0.0045]),
  54. "eigvec": torch.Tensor([[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]]),
  55. }
  56. @register_transform(Transforms.Lighting)
  57. class Lighting(object):
  58. """
  59. Lighting noise(AlexNet - style PCA - based noise)
  60. Taken from fastai Imagenet training -
  61. https://github.com/fastai/imagenet-fast/blob/faa0f9dfc9e8e058ffd07a248724bf384f526fae/imagenet_nv/fastai_imagenet.py#L103
  62. To use:
  63. - training_params = {"imagenet_pca_aug": 0.1}
  64. - Default training_params arg is 0.0 ("don't use")
  65. - 0.1 is that default in the original paper
  66. """
  67. def __init__(self, alphastd, eigval=IMAGENET_PCA["eigval"], eigvec=IMAGENET_PCA["eigvec"]):
  68. self.alphastd = alphastd
  69. self.eigval = eigval
  70. self.eigvec = eigvec
  71. def __call__(self, img):
  72. if self.alphastd == 0:
  73. return img
  74. alpha = img.new().resize_(3).normal_(0, self.alphastd)
  75. rgb = self.eigvec.type_as(img).clone().mul(alpha.view(1, 3).expand(3, 3)).mul(self.eigval.view(1, 3).expand(3, 3)).sum(1).squeeze()
  76. return img.add(rgb.view(3, 1, 1).expand_as(img))
  77. @register_transform(Transforms.RandomErase)
  78. class RandomErase(RandomErasing):
  79. """
  80. A simple class that translates the parameters supported in SuperGradient's code base
  81. """
  82. def __init__(self, probability: float, value: str):
  83. # value might be a string representing a float. First we try to convert to float and if fails,
  84. # pass it as-is to super
  85. try:
  86. value = float(value)
  87. except ValueError:
  88. pass
  89. super().__init__(p=probability, value=value)
Discard
Tip!

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