| 184 | self.r1 = r1 |
| 185 | |
| 186 | def __call__(self, img): |
| 187 | if random.uniform(0, 1) >= self.probability: |
| 188 | return img |
| 189 | |
| 190 | for attempt in range(100): |
| 191 | area = img.size()[1] * img.size()[2] |
| 192 | |
| 193 | target_area = random.uniform(self.sl, self.sh) * area |
| 194 | aspect_ratio = random.uniform(self.r1, 1 / self.r1) |
| 195 | |
| 196 | h = int(round(np.sqrt(target_area * aspect_ratio))) |
| 197 | w = int(round(np.sqrt(target_area / aspect_ratio))) |
| 198 | |
| 199 | if w < img.size()[2] and h < img.size()[1]: |
| 200 | x1 = random.randint(0, img.size()[1] - h) |
| 201 | y1 = random.randint(0, img.size()[2] - w) |
| 202 | |
| 203 | img[0, x1:x1 + h, y1:y1 + w] = random.uniform(0, 1) |
| 204 | img[1, x1:x1 + h, y1:y1 + w] = random.uniform(0, 1) |
| 205 | img[2, x1:x1 + h, y1:y1 + w] = random.uniform(0, 1) |
| 206 | |
| 207 | return img |
| 208 | |
| 209 | return img |