Adaptive selects a channel or two channels. Args: probability: The probability that the Random Erasing operation will be performed. sl: Minimum proportion of erased area against input image. sh: Maximum proportion of erased area against input image. r1:
| 49 | |
| 50 | |
| 51 | class ChannelAdapGray(object): |
| 52 | """ Adaptive selects a channel or two channels. |
| 53 | Args: |
| 54 | probability: The probability that the Random Erasing operation will be performed. |
| 55 | sl: Minimum proportion of erased area against input image. |
| 56 | sh: Maximum proportion of erased area against input image. |
| 57 | r1: Minimum aspect ratio of erased area. |
| 58 | mean: Erasing value. |
| 59 | """ |
| 60 | |
| 61 | def __init__(self, probability = 0.5): |
| 62 | self.probability = probability |
| 63 | |
| 64 | |
| 65 | def __call__(self, img): |
| 66 | |
| 67 | # if random.uniform(0, 1) > self.probability: |
| 68 | # return img |
| 69 | |
| 70 | idx = random.randint(0, 3) |
| 71 | |
| 72 | if idx ==0: |
| 73 | # random select R Channel |
| 74 | img[1, :,:] = img[0,:,:] |
| 75 | img[2, :,:] = img[0,:,:] |
| 76 | elif idx ==1: |
| 77 | # random select B Channel |
| 78 | img[0, :,:] = img[1,:,:] |
| 79 | img[2, :,:] = img[1,:,:] |
| 80 | elif idx ==2: |
| 81 | # random select G Channel |
| 82 | img[0, :,:] = img[2,:,:] |
| 83 | img[1, :,:] = img[2,:,:] |
| 84 | else: |
| 85 | if random.uniform(0, 1) > self.probability: |
| 86 | # return img |
| 87 | img = img |
| 88 | else: |
| 89 | tmp_img = 0.2989 * img[0,:,:] + 0.5870 * img[1,:,:] + 0.1140 * img[2,:,:] |
| 90 | img[0,:,:] = tmp_img |
| 91 | img[1,:,:] = tmp_img |
| 92 | img[2,:,:] = tmp_img |
| 93 | return img |
| 94 | |
| 95 | class ChannelRandomErasing(object): |
| 96 | """ Randomly selects a rectangle region in an image and erases its pixels. |