def transform_v3(config): train_transforms = Compose([ ImageCompression(quality_lower=60, quality_upper=100, p=0.5), GaussNoise(p=1), GaussianBlur(blur_limit=3, p=1), HorizontalFlip(), Resize(config.image_size, config.image_size), OneOf([RandomBrightnessContrast(), FancyPCA(), HueSaturationValue()], p=1), ShiftScaleRotate(shift_limit=0.1, scale_limit=0.2, rotate_limit=10, border_mode=cv2.BORDER_CONSTANT, p=1), ToTensor() ]) test_transforms = Compose([ GaussNoise(p=1), GaussianBlur(blur_limit=3, p=1), Resize(config.image_size, config.image_size), OneOf([RandomBrightnessContrast(), FancyPCA(), HueSaturationValue()], p=1), ToTensor() ]) return train_transforms, test_transforms
def blend_original(img): img = img.copy() h, w = img.shape[:2] rect = detector(img) if len(rect) == 0: return img else: rect = rect[0] sp = predictor(img, rect) landmarks = np.array([[p.x, p.y] for p in sp.parts()]) outline = landmarks[[*range(17), *range(26, 16, -1)]] Y, X = skimage.draw.polygon(outline[:, 1], outline[:, 0]) raw_mask = np.zeros(img.shape[:2], dtype=np.uint8) raw_mask[Y, X] = 1 face = img * np.expand_dims(raw_mask, -1) # add warping h1 = random.randint(h - h // 2, h + h // 2) w1 = random.randint(w - w // 2, w + w // 2) while abs(h1 - h) < h // 3 and abs(w1 - w) < w // 3: h1 = random.randint(h - h // 2, h + h // 2) w1 = random.randint(w - w // 2, w + w // 2) face = cv2.resize(face, (w1, h1), interpolation=random.choice([cv2.INTER_LINEAR, cv2.INTER_AREA, cv2.INTER_CUBIC])) face = cv2.resize(face, (w, h), interpolation=random.choice([cv2.INTER_LINEAR, cv2.INTER_AREA, cv2.INTER_CUBIC])) raw_mask = binary_erosion(raw_mask, iterations=random.randint(4, 10)) img[raw_mask, :] = face[raw_mask, :] if random.random() < 0.2: img = OneOf([GaussianBlur(), Blur()], p=0.5)(image=img)["image"] # image compression if random.random() < 0.5: img = ImageCompression(quality_lower=40, quality_upper=95)(image=img)["image"] return img
def initialize_elements(self): self.using_roi = hasattr(self.params, "roi_crop") self.resizer = self.plain_resize if hasattr(self.params, "random_crop_scale"): self.resizer = RandomResizedCrop( height=self.params.default_height, width=self.params.default_width, scale=self.params.random_crop_scale, ratio=self.params.random_crop_ratio) if self.using_roi: self.roi_resize = Resize(height=self.params.roi_height, width=self.params.roi_width) starting_aug = [Rotate(limit=15), HorizontalFlip(p=0.5)] heavy_aug = [ # RandomGamma(p=0.1), ElasticTransform(p=0.1, alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03), GaussNoise(p=0.05), GaussianBlur(p=0.05) ] if self.params.data_augmentation == constants.heavy_augmentation: starting_aug.extend(heavy_aug) self.aug = Compose(starting_aug)
def generate_transforms(image_size): train_transform = Compose( [ Resize(height=image_size[0], width=image_size[1]), OneOf([RandomBrightness(limit=0.1, p=1), RandomContrast(limit=0.1, p=1)]), OneOf([MotionBlur(blur_limit=3), MedianBlur(blur_limit=3), GaussianBlur(blur_limit=3)], p=0.5), VerticalFlip(p=0.5), HorizontalFlip(p=0.5), ShiftScaleRotate( shift_limit=0.2, scale_limit=0.2, rotate_limit=20, interpolation=cv2.INTER_LINEAR, border_mode=cv2.BORDER_REFLECT_101, p=1, ), Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0, p=1.0), ] ) val_transform = Compose( [ Resize(height=image_size[0], width=image_size[1]), Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0, p=1.0), ] ) return {"train_transforms": train_transform, "val_transforms": val_transform}
def select_aug(atype, param, p=1): from albumentations import JpegCompression, Blur, Downscale, CLAHE, HueSaturationValue, \ RandomBrightnessContrast, IAAAdditiveGaussianNoise, GaussNoise, GaussianBlur, MedianBlur, MotionBlur if atype == 'JpegCompression': trans_aug = JpegCompression(quality_lower=param, quality_upper=param, p=p) # strong_aug_pixel() elif atype == 'Blur': trans_aug = Blur(blur_limit=param, p=p) elif atype == 'Downscale': trans_aug = Downscale(scale_min=param, scale_max=param, p=p) elif atype == 'CLAHE': trans_aug = CLAHE(clip_limit=param, p=p) elif atype == 'HueSaturationValue': trans_aug = HueSaturationValue(hue_shift_limit=param, sat_shift_limit=param, val_shift_limit=param, p=p) elif atype == 'RandomBrightnessContrast': trans_aug = RandomBrightnessContrast(brightness_limit=param, contrast_limit=param, p=p) elif atype == 'IAAAdditiveGaussianNoise': trans_aug = IAAAdditiveGaussianNoise(loc=param, p=p) elif atype == 'GaussNoise': trans_aug = GaussNoise(mean=param, p=p) elif atype == 'GaussianBlur': trans_aug = GaussianBlur(blur_limit=param, p=p) elif atype == 'MedianBlur': trans_aug = MedianBlur(blur_limit=param, p=p) elif atype == 'MotionBlur': trans_aug = MotionBlur(blur_limit=param, p=p) else: raise NotImplementedError(atype) aug = trans_aug return aug
def pixel_aug(p=.5): print('[DATA]: pixel aug') from albumentations import JpegCompression, Blur, Downscale, CLAHE, HueSaturationValue, \ RandomBrightnessContrast, IAAAdditiveGaussianNoise, GaussNoise, GaussianBlur, MedianBlur, MotionBlur, \ Compose, OneOf from random import sample, randint, uniform return Compose([ # Jpeg Compression OneOf([ JpegCompression(quality_lower=20, quality_upper=99, p=1) ], p=0.2), # Gaussian Noise OneOf([ IAAAdditiveGaussianNoise(loc=randint(1, 9), p=1), GaussNoise(mean=uniform(0, 10.0), p=1), ], p=0.3), # Blur OneOf([ GaussianBlur(blur_limit=15, p=1), MotionBlur(blur_limit=19, p=1), Downscale(scale_min=0.3, scale_max=0.99, p=1), Blur(blur_limit=15, p=1), MedianBlur(blur_limit=9, p=1) ], p=0.4), # Color OneOf([ CLAHE(clip_limit=4.0, p=1), HueSaturationValue(p=1), RandomBrightnessContrast(p=1), ], p=0.1) ], p=p)
def fer_train_aug(input_size, crop_residual_pix=16): aug = Compose([ Resize(height=input_size + crop_residual_pix, width=input_size + crop_residual_pix), OneOf([ RandomCrop(height=input_size, width=input_size), Resize(height=input_size, width=input_size) ], p=1.0), HorizontalFlip(p=0.5), OneOf([ Blur(blur_limit=7, p=0.5), GaussianBlur(blur_limit=7, p=0.5), MedianBlur(blur_limit=7, p=0.5) ]), OneOf([ HueSaturationValue(hue_shift_limit=30, sat_shift_limit=30, val_shift_limit=30, p=0.5), RandomBrightnessContrast(brightness_limit=0.3, contrast_limit=0.3) ]), Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)) ], p=1.0) return aug
def __init__(self, to_tensor=True): augs = [ RandomSizedBBoxSafeCrop(height=512, width=512), RandomBrightness(p=0.5), RandomContrast(p=0.5), #RandomSunFlare(p=0.5, flare_roi=(0, 0, 1, 0.5), angle_lower=0.5,src_radius= 150), RandomShadow(p=0.5, num_shadows_lower=1, num_shadows_upper=1, shadow_dimension=5, shadow_roi=(0, 0.5, 1, 1)), HorizontalFlip(p=0.5), GaussianBlur(p=0.5), ] if to_tensor: augs.append( ToTensor(normalize={ "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225] })) self.transform = Compose(augs, bbox_params={ "format": "albumentations", "min_area": 0, "min_visibility": 0.2, 'label_fields': ['category_id'] })
def create_train_transforms(size=300): return Compose([ ImageCompression(quality_lower=60, quality_upper=100, p=0.5), GaussNoise(p=0.1), GaussianBlur(blur_limit=3, p=0.05), HorizontalFlip(), OneOf([ IsotropicResize(max_side=size, interpolation_down=cv2.INTER_AREA, interpolation_up=cv2.INTER_CUBIC), IsotropicResize(max_side=size, interpolation_down=cv2.INTER_AREA, interpolation_up=cv2.INTER_LINEAR), IsotropicResize(max_side=size, interpolation_down=cv2.INTER_LINEAR, interpolation_up=cv2.INTER_LINEAR), ], p=1), PadIfNeeded(min_height=size, min_width=size, border_mode=cv2.BORDER_CONSTANT), OneOf([RandomBrightnessContrast(), FancyPCA(), HueSaturationValue()], p=0.7), ToGray(p=0.2), ShiftScaleRotate(shift_limit=0.1, scale_limit=0.2, rotate_limit=10, border_mode=cv2.BORDER_CONSTANT, p=0.5), ])
def aug4(p=1.0): return Compose([ Flip(p=0.75), ShiftScaleRotate( rotate_limit=0, border_mode=cv2.BORDER_CONSTANT, p=0.3), GaussianBlur(blur_limit=3, p=0.3), ], p=p)
def random_blur(image, label): limit = random.randrange(10, 50) if random.random() > 0.5: aug = GaussianBlur(blur_limit=limit, p=1) else: aug = MedianBlur(blur_limit=limit, p=1) augmented = aug(image=image) image = augmented['image'] return image, label
def aug(): return Compose_alb([ HorizontalFlip(), ShiftScaleRotate(rotate_limit=10, border_mode=0), RandomGamma(), RandomBrightness(), RandomContrast(), ElasticTransform(border_mode=0), GaussianBlur() ], p=1)
def hard_transforms(image_size): min_holes, max_holes = 1, 2 size = 30 return [ # Random shifts, stretches and turns with a 50% probability ShiftScaleRotate( shift_limit=0.2, scale_limit=0.2, rotate_limit=180, border_mode=BORDER_CONSTANT, p=0.1 ), IAAPerspective(scale=(0.02, 0.05), p=0.1), # Random brightness / contrast with a 30% probability RandomBrightnessContrast( brightness_limit=0.2, contrast_limit=0.2, p=0.1 ), OneOf([ GaussNoise(var_limit=1.0, p=1.0), MultiplicativeNoise(multiplier=(0.9, 1), p=1.0) ], p=0.1), OneOf([ GaussianBlur(blur_limit=3, p=1.0), Blur(p=1.0), ], p=0.1), # CoarseDropout( # min_holes=min_holes, # max_holes=max_holes, # # min_height=image_height // 4, # # max_height=image_height // 4, # # min_width=image_width // 4, # # max_width=image_width // 4, # min_height=size, # max_height=size, # min_width=size, # max_width=size, # fill_value=0, # p=1.0 # ), # Random gamma changes with a 30% probability RandomGamma(gamma_limit=(85, 115), p=0.1), ImageCompression( quality_lower=70, quality_upper=100, p=0.1 ), ]
def TTA(img, model, model_name, seed=88, niter=4): input_size = int(model.get_input_at(0).get_shape()[1]) AUGMENTATIONS = Compose([ HorizontalFlip(p=0.25), RandomSizedCrop(min_max_height=(int(input_size * 0.75), input_size), height=input_size, width=input_size, p=0.5), OneOf([ ShiftScaleRotate(rotate_limit=25), ElasticTransform( alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03), GridDistortion(), OpticalDistortion(distort_limit=2, shift_limit=0.5), ], p=0.5), OneOf([RandomContrast(), RandomGamma(), RandomBrightness()], p=0.5), OneOf( [Blur(), MedianBlur(), GaussNoise(), GaussianBlur()], p=0.5) ], p=0.5) np.random.seed(seed) original_img = img.copy() inverted_img = np.invert(img.copy()) hflipped_img = np.fliplr(img.copy()) original_img_array = np.empty( (niter + 1, img.shape[0], img.shape[1], img.shape[2])) inverted_img_array = original_img_array.copy() hflipped_img_array = original_img_array.copy() original_img_array[0] = original_img inverted_img_array[0] = inverted_img hflipped_img_array[0] = hflipped_img for each_iter in range(niter): original_img_array[each_iter + 1] = AUGMENTATIONS(image=original_img)['image'] inverted_img_array[each_iter + 1] = AUGMENTATIONS(image=inverted_img)['image'] hflipped_img_array[each_iter + 1] = AUGMENTATIONS(image=hflipped_img)['image'] tmp_array = np.vstack( (original_img_array, inverted_img_array, hflipped_img_array)) tmp_array = preprocess_input(tmp_array, model_name) prediction = np.mean(model.predict(tmp_array), axis=0) return prediction
def img_enhance(img): gauss = GaussianBlur() img = gauss.apply(img, ksize=5) #高斯模糊 hsv = cv.cvtColor(img, cv.COLOR_RGB2HSV) clahe = CLAHE() img = clahe.apply(img) #对比度受限的自适应直方图均衡化 s = img.shape v = 0 for i in range(s[0]): for j in range(s[1]): v += hsv[i][j][2] v = v / s[0] / s[1] if v > 120: gamma = RandomGamma() img = gamma.apply(img, gamma=1.2) elif v < 80: gamma = RandomGamma() img = gamma.apply(img, gamma=0.8) #如果图像过亮或过暗,则进行伽马变换 return img
def create_train_transforms(size=300): # defining an augmentation pipeline # this will return a transform function that will perform image augmentation. return Compose([ # Decrease Jpeg, WebP compression of an image # with the quality_lower parameter as the lower bound on the image quality # and the quality_upper as the upper bound on the image quality ImageCompression(quality_lower=60, quality_upper=100, p=0.5), # used to apply Gaussian noise to the input picture # with p as the probability of applying the transform GaussNoise(p=0.1), # used to blur the input image using a Gaussian filter with a random kernel size # with the blur_limit as the maximum Gaussian kernel size for blurring the input image GaussianBlur(blur_limit=3, p=0.05), # flips the input image horizontally around the y-axis HorizontalFlip(), # Select one of transforms to apply OneOf([ IsotropicResize(max_side=size, interpolation_down=cv2.INTER_AREA, interpolation_up=cv2.INTER_CUBIC), IsotropicResize(max_side=size, interpolation_down=cv2.INTER_AREA, interpolation_up=cv2.INTER_LINEAR), IsotropicResize(max_side=size, interpolation_down=cv2.INTER_LINEAR, interpolation_up=cv2.INTER_LINEAR), ], p=1), # Pad side of the image / max if side is less than desired number PadIfNeeded(min_height=size, min_width=size, border_mode=cv2.BORDER_CONSTANT), # Select one of the following transforms to apply: # RandomBrightnessContrast: used to randomly change brightness and contrast of the input image # FancyPCA: Augment RGB image using FancyPCA # HueSaturationValue: Randomly change hue, saturation and value of the input image OneOf([RandomBrightnessContrast(), FancyPCA(), HueSaturationValue()], p=0.7), # this converts the input RGB image to grayscale. If the mean pixel value for the resulting image is greater than 127, invert the resulting grayscale image. ToGray(p=0.2), # this randomly apply affine transforms: translate, scale and rotate the input. ShiftScaleRotate(shift_limit=0.1, scale_limit=0.2, rotate_limit=10, border_mode=cv2.BORDER_CONSTANT, p=0.5), ])
def __init__(self, root_path): self.folder_name = [ name for name in os.listdir(root_path) if os.path.isdir(os.path.join(root_path, name)) ] #os.listdir(root_path)[:-1] self.root = root_path #self.image_paths = list(Path(self.root).rglob('*.jpg')) self.json_paths = os.path.join(root_path, 'metadata.json') # 1 with open(self.json_paths) as json_file: self.json_data = json.load(json_file) self.transform = Compose([ Resize(size, size), ImageCompression(quality_lower=60, quality_upper=100, p=0.5), GaussNoise(p=0.1), GaussianBlur(blur_limit=3, p=0.05), HorizontalFlip(p=0.5), OneOf([ IsotropicResize(max_side=size, interpolation_down=cv2.INTER_AREA, interpolation_up=cv2.INTER_CUBIC), IsotropicResize(max_side=size, interpolation_down=cv2.INTER_AREA, interpolation_up=cv2.INTER_LINEAR), IsotropicResize(max_side=size, interpolation_down=cv2.INTER_LINEAR, interpolation_up=cv2.INTER_LINEAR), ], p=0.7), PadIfNeeded(min_height=size, min_width=size, border_mode=cv2.BORDER_CONSTANT), OneOf( [RandomBrightnessContrast(), FancyPCA(), HueSaturationValue()], p=0.7), ToGray(p=0.1), ShiftScaleRotate(shift_limit=0.1, scale_limit=0.2, rotate_limit=10, border_mode=cv2.BORDER_CONSTANT, p=0.5), ]) self.normalize = { "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225] } #self.len = len(self.image_paths) #folder len self.len = len(self.folder_name)
def get_random_aug(min_area=0., min_visibility=0.): aug = Compose([ RandomRotate90(p=random.uniform(0, 1)), GaussianBlur(p=random.uniform(0, 1)), GaussNoise(p=random.uniform(0, 1)), HueSaturationValue(p=random.uniform(0, 1)), RGBShift(p=random.uniform(0, 1)) ]) return Compose(aug, bbox_params={ 'format': 'pascal_voc', 'min_area': min_area, 'min_visibility': min_visibility, 'label_fields': ['category_id'] })
def build_train(self): return Compose([ OneOf([ CropNonEmptyMaskIfExists(self.height, self.width), RandomCrop(self.height, self.width) ], p=1), OneOf([ CLAHE(p=0.5), # modified source to get this to work GaussianBlur(3, p=0.3), IAASharpen(alpha=(0.2, 0.3), p=0.3), ], p=1), Flip(p=0.5), Normalize(mean=self.MEAN, std=self.STD), ToTensor(), ])
def get_augmentations(img_size): return Compose([ Resize(height=int(img_size * 1.5), width=int(img_size * 1.5), p=1), RandomSizedCrop(min_max_height=(int(img_size * 0.9), img_size), height=img_size, width=img_size, always_apply=True, p=1), RandomRotate90(), Flip(), Transpose(), OneOf([ IAAAdditiveGaussianNoise(), GaussNoise(), ], p=0.4), OneOf([ GlassBlur(p=1), GaussianBlur(p=1), MotionBlur(p=1), MedianBlur(blur_limit=3, p=1), Blur(blur_limit=3, p=1), ], p=0.4), ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=0.2), OneOf([ OpticalDistortion(p=1), ElasticTransform(), GridDistortion(p=1), IAAPiecewiseAffine(p=1), ], p=0.4), OneOf( [ CLAHE(clip_limit=2), # Histogram Equalization IAASharpen(), IAAEmboss(), RandomBrightnessContrast(), RGBShift() ], p=0.4), HueSaturationValue(p=0.3), ToSepia(p=0.2), Cutout(p=0.2), RandomScale(p=0.2) ])
def train_trasforms_standard(conf): height = conf['crop_height'] width = conf['crop_width'] return Compose([ RandomSizedCrop2x(height, width, scale_shift=0.5, p=1), OneOf([IAAPiecewiseAffine(), IAAPerspective(), OpticalDistortion(border_mode=cv2.BORDER_CONSTANT), GridDistortion(border_mode=cv2.BORDER_CONSTANT), ElasticTransform(border_mode=cv2.BORDER_CONSTANT)], p=0.3), RandomBrightnessContrast(), RandomGamma(), OneOf([MedianBlur(), GaussianBlur()], p=0.2), OneOf([IAAAdditiveGaussianNoise(per_channel=True),GaussNoise()]) ] )
def make_train_transform(mean=0, std=1): from interrater.config import SIZE, MAX_SIZE print("\ntrain transform with SIZE=",SIZE," and MAX_SIZE = ",MAX_SIZE,"\n") _train = Compose([ HorizontalFlip(), VerticalFlip(), GaussianBlur(blur_limit=3, p=.2), Rotate(45, p=.7, border_mode=cv2.BORDER_CONSTANT), OneOf([ RandomSizedCrop((MAX_SIZE, MAX_SIZE), SIZE, SIZE, p=.8), Resize(SIZE, SIZE, p=.2), ], p=1), CLAHE(always_apply=True), Normalize(mean, std, always_apply=True), ToTensor(always_apply=True) ]) return _train
def strong_aug(p=0.6): return Compose([ RandomShadow(shadow_roi=(0, 0, 1, 1), p=0.75), OneOf([MotionBlur(), GaussianBlur()]), OneOf([ToGray(), ToSepia()]), OneOf([ InvertImg(), RandomBrightnessContrast(brightness_limit=0.75, p=0.75), RandomGamma(), HueSaturationValue() ], p=0.75) ], bbox_params=BboxParams("pascal_voc", label_fields=["category_id"], min_area=0.0, min_visibility=0.0), p=p)
def _make_train_transform(mean=0, std=1): _train = Compose([ HorizontalFlip(), VerticalFlip(), Rotate(90, p=.5, border_mode=cv2.BORDER_CONSTANT, value=0), OneOf([ RandomSizedCrop( (MAX_SIZE, MAX_SIZE), PATCH_SIZE, PATCH_SIZE, p=.8), Resize(PATCH_SIZE, PATCH_SIZE, p=.2), ], p=1), # ElasticTransform(sigma=10, border_mode=cv2.BORDER_CONSTANT, value=0, p=.1), GaussianBlur(blur_limit=3, p=.2), CLAHE(always_apply=True), Normalize(mean, std, always_apply=True), ToTensor(always_apply=True) ]) return _train
def blend_original(img): img = img.copy() h, w = img.shape[:2] # detect faces in image rect = detector(img) # if there is no faces return the image if len(rect) == 0: return img else: rect = rect[0] # predict the landmarks in the image sp = predictor(img, rect) landmarks = np.array([[p.x, p.y] for p in sp.parts()]) outline = landmarks[[*range(17), *range(26, 16, -1)]] # draw the outline of the landmarks on the image Y, X = skimage.draw.polygon(outline[:, 1], outline[:, 0]) raw_mask = np.zeros(img.shape[:2], dtype=np.uint8) raw_mask[Y, X] = 1 face = img * np.expand_dims(raw_mask, -1) # add warping h1 = random.randint(h - h // 2, h + h // 2) w1 = random.randint(w - w // 2, w + w // 2) while abs(h1 - h) < h // 3 and abs(w1 - w) < w // 3: h1 = random.randint(h - h // 2, h + h // 2) w1 = random.randint(w - w // 2, w + w // 2) face = cv2.resize(face, (w1, h1), interpolation=random.choice( [cv2.INTER_LINEAR, cv2.INTER_AREA, cv2.INTER_CUBIC])) face = cv2.resize(face, (w, h), interpolation=random.choice( [cv2.INTER_LINEAR, cv2.INTER_AREA, cv2.INTER_CUBIC])) # Erosion is a mathematical morphology operation that uses a structuring element for shrinking the shapes in an image. # this function preforms multi-dimensional binary erosion with a given structuring element. raw_mask = binary_erosion(raw_mask, iterations=random.randint(4, 10)) img[raw_mask, :] = face[raw_mask, :] if random.random() < 0.2: img = OneOf([GaussianBlur(), Blur()], p=0.5)(image=img)["image"] # image compression if random.random() < 0.5: img = ImageCompression(quality_lower=40, quality_upper=95)(image=img)["image"] return img
def strong_aug(p=0.5): return Compose([ RandomGridShuffle((2, 2), p=0.75), OneOf([ ShiftScaleRotate(shift_limit=0.125), Transpose(), RandomRotate90(), VerticalFlip(), HorizontalFlip(), IAAAffine(shear=0.1) ]), OneOf([GaussNoise(), GaussianBlur(), MedianBlur(), MotionBlur()]), OneOf([RandomBrightnessContrast(), CLAHE(), IAASharpen()]), Cutout(10, 2, 2, 127), ], p=p)
def transform(image_pil, nightly=False, hard=False): aug_im = pil_2_cv(image_pil) #probabilities shadow_add_p = 0.05 sun_add_p = 0.2 rain_add_p = 0.2 speed_add_p = 0.2 g_blur_p = 0.4 g_noise_p = 1 br_con_p = 0.4 ################### first_aug = None if not nightly: if desc(shadow_add_p): aug_im = aug.add_shadow(aug_im, no_of_shadows=random.randrange(1) + 1, shadow_dimension=4) first_aug = "shadow" #get aug height = aug_im.shape[0] width = aug_im.shape[1] y = random.randrange(height // 3) x = random.randrange(width) aug_list = ['aug.add_sun_flare(aug_im, flare_center=(x, y), src_color=(255, 255, 255), src_radius=150, no_of_flare_circles=2)', 'aug.add_speed(aug_im, random.randrange(30, 60)/100)', 'aug.add_rain(aug_im, drop_length=random.randrange(10, 30))', 'aug_im'] weights = [sun_add_p, speed_add_p, rain_add_p] weights.append(1 - np.sum(weights)) to_aug = np.random.choice(aug_list, p=weights) aug_im = eval(to_aug) aug_im = GaussianBlur(p=g_blur_p, blur_limit=10)(image=aug_im)['image'] aug_im = GaussNoise(p=g_noise_p, var_limit=(0, 50))(image=aug_im)['image'] aug_im = RandomBrightnessContrast(brightness_limit=0.07, contrast_limit=0.07, p=br_con_p)(image=aug_im)['image'] return cv_2_pil(aug_im)
def strong_aug(p=0.75): return Compose( [ ShiftScaleRotate(scale_limit=0.1, rotate_limit=90), Transpose(), #IAAAffine(shear=0.1), #IAAPerspective(), Cutout(num_holes=20, max_h_size=8, max_w_size=8), HorizontalFlip(), VerticalFlip(), GaussNoise(), JpegCompression(), #RandomShadow(shadow_roi=(0, 0, 1, 1), p=0.75), OneOf([MotionBlur(), GaussianBlur()]), OneOf([ToGray(), ToSepia()]), RandomBrightnessContrast(brightness_limit=0.75, p=0.75) ], bbox_params=BboxParams("pascal_voc", label_fields=["category_id"], min_area=0.0, min_visibility=0.5), p=p)
def get_train_transforms(size=300): return Compose([ ImageCompression(quality_lower=60, quality_upper=100, p=0.5), GaussNoise(p=0.1), GaussianBlur(blur_limit=3, p=0.05), HorizontalFlip(), Resize(height=size, width=size), PadIfNeeded(min_height=size, min_width=size, border_mode=cv2.BORDER_CONSTANT), OneOf([RandomBrightnessContrast(), HueSaturationValue()], p=0.5), # FancyPCA(), OneOf([CoarseDropout(), GridDropout()], p=0.2), ToGray(p=0.2), ShiftScaleRotate(shift_limit=0.1, scale_limit=0.2, rotate_limit=10, border_mode=cv2.BORDER_CONSTANT, p=0.5), Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), ToTensorV2() ])
def MedT_preprocess_v2_image(img, train, mean=None, std=None) -> torch.Tensor: if std is None: std = [0.5, 0.5, 0.5] if mean is None: mean = [0.5, 0.5, 0.5] degrees = int(random.random() * 360) n, m = random.randint(1, 4), random.randint(2, 15) ShearTranslateAug = ShearTranslate(n, m) augmentations = [ RandomHorizontalFlip(), RandomRotation(degrees), RandomVerticalFlip(), RandomPerspective(), RandomBrightness(), RandomContrast(), RandomScale(), GaussianBlur(), RandomResizedCrop(), ShearTranslateAug ] augs_num_to_apply = random.randint(1, len(augmentations)) augs = random.sample(augmentations, augs_num_to_apply) if train == True: augment = Compose([Image.fromarray, *augs]) normilize = Compose([ToTensor(), Normalize(mean=mean, std=std)]) augmented = augment(img) preprocced = normilize(augmented).unsqueeze(0) return preprocced, augmented preprocessing = Compose([ToTensor(), Normalize(mean=mean, std=std)]) return preprocessing(img).unsqueeze(0), None