Randomly mask out one or more patches from an image.
| 92 | |
| 93 | |
| 94 | class Cutout(object): |
| 95 | """Randomly mask out one or more patches from an image.""" |
| 96 | |
| 97 | def __init__(self, n_holes=2, length=32, prob=0.5): |
| 98 | self.n_holes = n_holes |
| 99 | self.length = length |
| 100 | self.prob = prob |
| 101 | |
| 102 | def __call__(self, img): |
| 103 | if np.random.rand() < self.prob: |
| 104 | h = img.size(1) |
| 105 | w = img.size(2) |
| 106 | mask = np.ones((h, w), np.float32) |
| 107 | for n in range(self.n_holes): |
| 108 | y = np.random.randint(h) |
| 109 | x = np.random.randint(w) |
| 110 | y1 = np.clip(y - self.length // 2, 0, h) |
| 111 | y2 = np.clip(y + self.length // 2, 0, h) |
| 112 | x1 = np.clip(x - self.length // 2, 0, w) |
| 113 | x2 = np.clip(x + self.length // 2, 0, w) |
| 114 | mask[y1:y2, x1:x2] = 0. |
| 115 | |
| 116 | mask = torch.from_numpy(mask) |
| 117 | mask = mask.expand_as(img) |
| 118 | img = img * mask |
| 119 | |
| 120 | return img |
| 121 | |
| 122 | |
| 123 | class RandomOrientationRotation(object): |
nothing calls this directly
no outgoing calls
no test coverage detected