| 29 | |
| 30 | |
| 31 | def randomErasing(image, |
| 32 | probability=0.5, |
| 33 | sl=0.02, |
| 34 | sh=0.2, |
| 35 | r1=0.3, |
| 36 | mean=[0.4914, 0.4822, 0.4465]): |
| 37 | expand_batch_dim = len(image.size()) == 3 |
| 38 | if expand_batch_dim: |
| 39 | image = image.unsqueeze(0) |
| 40 | |
| 41 | batch_size = image.size(0) |
| 42 | width = image.size(2) |
| 43 | height = image.size(3) |
| 44 | area = width * height |
| 45 | |
| 46 | for index in range(batch_size): |
| 47 | erase_flag = False |
| 48 | |
| 49 | while not erase_flag: |
| 50 | |
| 51 | target_area = random.uniform(sl, sh) * area |
| 52 | aspect_ratio = random.uniform(r1, 1 / r1) |
| 53 | h = int(round(math.sqrt(target_area * aspect_ratio))) |
| 54 | w = int(round(math.sqrt(target_area / aspect_ratio))) |
| 55 | |
| 56 | if w < width and h < height: |
| 57 | x1 = random.randint(0, height - h) |
| 58 | y1 = random.randint(0, width - w) |
| 59 | image[index, 0, x1:x1 + h, y1:y1 + w] = mean[0] |
| 60 | image[index, 1, x1:x1 + h, y1:y1 + w] = mean[1] |
| 61 | image[index, 2, x1:x1 + h, y1:y1 + w] = mean[2] |
| 62 | |
| 63 | erase_flag = True |
| 64 | |
| 65 | return image |
| 66 | |
| 67 | |
| 68 | def solarize(tensor, threshold=0.5, apply_prob=0.2): |