| |
| |
| |
| |
| |
|
|
| import cv2 |
| import torch |
| import numpy as np |
| import albumentations as A |
| def gaussian_noise(img, mean, sigma): |
| return img + torch.FloatTensor(img.shape).normal_(mean=mean, std=sigma) |
| |
|
|
| def GammaInterference(img): |
| |
| gamma = np.random.random() * 1.5 + 0.25 |
| |
| img = gamma_concern(img, gamma) |
|
|
| |
| choose = np.random.randint(0, 2) |
| direction = np.random.randint(0, 2) |
|
|
| if choose == 0: |
| gamma = 0.2 + np.random.random() * 2.3 |
| img = gamma_power(img, gamma, direction) |
| else: |
| gamma = np.random.random() * 2.3 + 0.6 |
| img = gamma_exp(img, gamma, direction) |
|
|
| return img |
|
|
|
|
|
|
| def get_resize_transforms(img_size = (192, 192)): |
| |
| return A.Compose([ |
| A.Resize(img_size[0], img_size[1]) |
| ], p=1.0, additional_targets={'image2': 'image', "mask2": "mask"}) |
|
|
|
|
| def get_albu_transforms(type="train", img_size = (192, 192)): |
| if type == 'train': |
| compose = [ |
| A.VerticalFlip(p=0.5), |
| A.HorizontalFlip(p=0.5), |
| A.ShiftScaleRotate(shift_limit=0.2, scale_limit=(-0.2, 0.2), |
| rotate_limit=5, p=0.5), |
|
|
| |
| |
|
|
| |
| |
|
|
| |
| |
|
|
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
| A.OneOf([ |
| A.GridDistortion(num_steps=1, distort_limit=0.3, p=1.0), |
| A.ElasticTransform(alpha=3, sigma=15, alpha_affine=10, p=1.0) |
| ], p=0.5), |
|
|
| A.Resize(img_size[0], img_size[1])] |
| else: |
| compose = [A.Resize(img_size[0], img_size[1])] |
|
|
| return A.Compose(compose, p=1.0, additional_targets={'image2': 'image', "mask2": "mask"}) |
|
|
|
|
|
|
|
|
| |
| def gamma_concern(img, gamma): |
| mean = torch.mean(img) |
|
|
| img = (img - mean) * gamma |
| img = img + mean |
| img = torch.clip(img, 0, 1) |
|
|
| return img |
|
|
| def gamma_power(img, gamma, direction=0): |
| if direction == 1: |
| img = 1 - img |
| img = torch.pow(img, gamma) |
|
|
| img = img / torch.max(img) |
| if direction == 1: |
| img = 1 - img |
|
|
| return img |
|
|
| def gamma_exp(img, gamma, direction=0): |
| if direction == 1: |
| img = 1 - img |
|
|
| img = torch.exp(img * gamma) |
| img = img / torch.max(img) |
|
|
| if direction == 1: |
| img = 1 - img |
| return img |
|
|
|
|
|
|
|
|
|
|