Exemplo n.º 1
0
def open_rgby(path, id):  # a function that reads RGBY image
    colors = ['red', 'green', 'blue', 'yellow']
    flags = cv2.IMREAD_GRAYSCALE
    img = [
        cv2.imread(os.path.join(path, id + '_' + color + '.png'),
                   flags).astype(np.float32) / 255 for color in colors
    ]
    return np.stack(img, axis=-1)


TARGET_SIZE = 512

aug = Compose([
    HorizontalFlip(p=0.7),
    VerticalFlip(p=0.7),
    Transpose(p=0.7),
    Rotate(30, p=0.7),
    # RandomGamma(p=0.3),
    # GridDistortion(p=0.3),
    # Resize(height=TARGET_SIZE, width=TARGET_SIZE)
])

val_aug = None  #Resize(height=TARGET_SIZE, width=TARGET_SIZE)


class ProteinDataset:
    def __init__(self, names, path, aug=aug):
        self.names = names
        self.aug = aug
        self.path = path
        self.labels = pd.read_csv(LABELS).set_index('Id')
Exemplo n.º 2
0
                            RandomRotate90, OneOf, CLAHE, RandomGamma,
                            HueSaturationValue, IAAAdditiveGaussianNoise,
                            GaussNoise, RandomBrightnessContrast, IAASharpen,
                            IAAEmboss)

from models.unet import UNet
from models.discriminator import FCDiscriminator
from dataset.refuge_Vmiccai import REFUGE
from pytorch_utils import (adjust_learning_rate, adjust_learning_rate_D,
                           calc_mse_loss, Weighted_Jaccard_loss, dice_loss)
from models import optim_weight_ema
from arguments import get_arguments

aug_student = Compose([
    OneOf([
        Transpose(p=0.5),
        HorizontalFlip(p=0.5),
        VerticalFlip(p=0.5),
        RandomRotate90(p=0.5)
    ],
          p=0.2),
    OneOf([
        IAAAdditiveGaussianNoise(p=0.5),
        GaussNoise(p=0.5),
    ], p=0.2),
    OneOf([
        CLAHE(clip_limit=2),
        IAASharpen(p=0.5),
        IAAEmboss(p=0.5),
        RandomBrightnessContrast(p=0.5),
    ],
Exemplo n.º 3
0
def compose_augmentations(img_height,
                          img_width,
                          flip_p=0.5,
                          translate_p=0.5,
                          distort_p=0.5,
                          color_p=0.5,
                          overlays_p=0.15,
                          blur_p=0.25,
                          noise_p=0.25):
    # Resize
    resize_p = 1 if img_height != 1024 else 0

    # Random sized crop
    if img_height == 1024:
        min_max_height = (896, 960)
    elif img_height == 512:
        min_max_height = (448, 480)
    elif img_height == 256:
        min_max_height = (224, 240)
    else:
        raise NotImplementedError

    return Compose([
        Resize(p=resize_p, height=img_height, width=img_width),
        OneOf([
            HorizontalFlip(p=0.5),
            VerticalFlip(p=0.5),
            Transpose(p=0.5),
            RandomRotate90(p=0.5),
        ],
              p=flip_p),
        OneOf([
            Rotate(p=0.25, limit=10),
            RandomSizedCrop(p=0.5,
                            min_max_height=min_max_height,
                            height=img_height,
                            width=img_width),
            OneOrOther(IAAAffine(p=0.1, translate_percent=0.05),
                       IAAPerspective(p=0.1)),
        ],
              p=translate_p),
        OneOf([
            ElasticTransform(p=0.5,
                             alpha=10,
                             sigma=img_height * 0.05,
                             alpha_affine=img_height * 0.03,
                             approximate=True),
            GridDistortion(p=0.5),
            OpticalDistortion(p=0.5),
            IAAPiecewiseAffine(p=0.25, scale=(0.01, 0.03)),
        ],
              p=distort_p),
        OneOrOther(
            OneOf([
                CLAHE(p=0.5),
                RandomGamma(p=0.5),
                RandomContrast(p=0.5),
                RandomBrightness(p=0.5),
                RandomBrightnessContrast(p=0.5),
            ],
                  p=color_p),
            OneOf([IAAEmboss(p=0.1),
                   IAASharpen(p=0.1),
                   IAASuperpixels(p=0)],
                  p=overlays_p)),
        OneOrOther(
            OneOf([
                Blur(p=0.2),
                MedianBlur(p=0.1),
                MotionBlur(p=0.1),
                GaussianBlur(p=0.1),
            ],
                  p=blur_p),
            OneOf([GaussNoise(p=0.2),
                   IAAAdditiveGaussianNoise(p=0.1)],
                  p=noise_p)),
        Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
        ToTensor(sigmoid=False),
    ])
Exemplo n.º 4
0
        transformed_image = transform(image=image)['image']
        transform = iaa.CenterPadToSquare()
        transformed_image = transform(image=transformed_image)

    ## Rotate

    elif augmentation == 'rotate':
        transform = Rotate(always_apply=True)
        transformed_image = transform(image=image)['image']

    elif augmentation == 'random_rotate90':
        transform = RandomRotate90(always_apply=True)
        transformed_image = transform(image=image)['image']
   
    elif augmentation == 'transpose':
        transform = Transpose(always_apply=True)
        transformed_image = transform(image=image)['image']

    ## Size

    elif augmentation == 'resize':
        transform = Resize(always_apply=True, height=100, width=100)
        transformed_image = transform(image=image)['image']

    elif augmentation == 'longest_max_size':
        transform = LongestMaxSize(always_apply=True)
        transformed_image = transform(image=image)['image']
          
    elif augmentation == 'smallest_max_size':
        transform = SmallestMaxSize(always_apply=True)
        transformed_image = transform(image=image)['image']
Exemplo n.º 5
0
from albumentations import Compose, Resize, RandomCrop, Flip, HorizontalFlip, VerticalFlip, Transpose, RandomRotate90, \
    ShiftScaleRotate, OneOf, OpticalDistortion, Blur, MotionBlur, GaussianBlur
from albumentations.pytorch import ToTensor

train_aug = Compose([
    RandomCrop(height=96, width=96, p=0.2),
    OneOf([
        VerticalFlip(p=0.2),
        HorizontalFlip(p=0.3),
        Transpose(p=0.2),
        RandomRotate90(p=0.2),
    ],
          p=0.3),
    ShiftScaleRotate(p=0.2),
    OneOf([
        Blur(p=0.2),
        MotionBlur(p=0.2),
        GaussianBlur(p=0.2),
        OpticalDistortion(p=0.2),
    ],
          p=0.3),
    Resize(128, 128, always_apply=True),
    ToTensor()
])

valid_aug = Compose([Resize(128, 128, always_apply=True), ToTensor()])
Exemplo n.º 6
0
def torch_rules(config, dataset_type, **params):
    print('torch_rules', params)
    assert 'rules' in params or config['transform']['rules']

    size = _check_param(config, 'size')
    size = 128 if size is None else size

    if 'rules' in params:
        rules_file = params['rules']
    else:
        rules_file = config['transform']['rules']
    with open(rules_file, 'r') as fid:
        rules = eval(fid.read())
        rules = itertools.chain.from_iterable(rules)
        print(rules)
    means = np.array([127.5, 127.5, 127.5, 127.5])
    stds = np.array([255.0, 255.0, 255.0, 255.0])

    base_aug = Compose([
        RandomRotate90(),
        Flip(),
        Transpose(),
    ])
    aug_list = []
    for rule in rules:

        op_1, params_1 = rule[0]
        op_2, params_2 = rule[1]
        print(op_1, params_1)
        aug = Compose([
            globals().get(op_1)(**params_1),
            globals().get(op_2)(**params_2),
        ])
        aug_list.append(aug)
    print('len(aug_list):', len(aug_list), 'image size', size)
    resize = Resize(height=size, width=size, always_apply=True)

    def transform(image):
        if dataset_type == 'train':
            image = base_aug(image=image)['image']
            if len(aug_list) > 0:
                aug = random.choice(aug_list)
                image = aug(image=image)['image']
            image = resize(image=image)['image']
        else:
            if size != image.shape[0]:
                image = resize(image=image)['image']

        image = image.astype(np.float32)
        if  _check_param(config, 'per_image_norm'):
            mean = np.mean(image.reshape(-1, 4), axis=0)
            std = np.std(image.reshape(-1, 4), axis=0)
            image -= mean
            image /= (std + 0.0000001)
        else:
            image -= means
            image /= stds
        image = np.transpose(image, (2, 0, 1))

        return image

    return transform
Exemplo n.º 7
0
mask_rot90 = augmented['mask']

visualize(image_rot90, mask_rot90, original_image=image, original_mask=mask)

#%%
aug = RandomRotate90(p=1)

augmented = aug(image=image, mask=mask)

image_rot90 = augmented['image']
mask_rot90 = augmented['mask']

visualize(image_rot90, mask_rot90, original_image=image, original_mask=mask)

#%%
aug = Transpose(p=1)

augmented = aug(image=image, mask=mask)

image_transposed = augmented['image']
mask_transposed = augmented['mask']

visualize(image_transposed, mask_transposed, original_image=image, original_mask=mask)

#%%
aug = ElasticTransform(p=1, alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03)

augmented = aug(image=image, mask=mask)

image_elastic = augmented['image']
mask_elastic = augmented['mask']
Exemplo n.º 8
0
def augment_data(images, masks, augment=True):
    """ Performing data augmentation. """
    crop_size = (192 - 32, 256 - 32)
    size = (512, 384)
    list_of_img = []
    list_of_mask = []
    for image, mask in zip(images, masks):
        image_name = image.split("/")[-1].split(".")[0]
        mask_name = mask.split("/")[-1].split(".")[0]

        x, y = read_data(image, mask)
        try:
            h, w, c = x.shape
        except Exception as e:
            image = image[:-1]
            x, y = read_data(image, mask)
            h, w, c = x.shape

        if augment == True:
            # Center Crop
            aug = CenterCrop(p=1, height=crop_size[0], width=crop_size[1])
            augmented = aug(image=x, mask=y)
            x1 = augmented['image']
            y1 = augmented['mask']

            # Crop
            x_min = 0
            y_min = 0
            x_max = x_min + size[0]
            y_max = y_min + size[1]

            aug = Crop(p=1, x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max)
            augmented = aug(image=x, mask=y)
            x2 = augmented['image']
            y2 = augmented['mask']

            # Random Rotate 90 degree
            aug = RandomRotate90(p=1)
            augmented = aug(image=x, mask=y)
            x3 = augmented['image']
            y3 = augmented['mask']

            # Transpose
            aug = Transpose(p=1)
            augmented = aug(image=x, mask=y)
            x4 = augmented['image']
            y4 = augmented['mask']

            # ElasticTransform
            aug = ElasticTransform(p=1,
                                   alpha=120,
                                   sigma=120 * 0.05,
                                   alpha_affine=120 * 0.03)
            augmented = aug(image=x, mask=y)
            x5 = augmented['image']
            y5 = augmented['mask']

            # Grid Distortion
            aug = GridDistortion(p=1)
            augmented = aug(image=x, mask=y)
            x6 = augmented['image']
            y6 = augmented['mask']

            # Optical Distortion
            aug = OpticalDistortion(p=1, distort_limit=2, shift_limit=0.5)
            augmented = aug(image=x, mask=y)
            x7 = augmented['image']
            y7 = augmented['mask']

            # Vertical Flip
            aug = VerticalFlip(p=1)
            augmented = aug(image=x, mask=y)
            x8 = augmented['image']
            y8 = augmented['mask']

            # Horizontal Flip
            aug = HorizontalFlip(p=1)
            augmented = aug(image=x, mask=y)
            x9 = augmented['image']
            y9 = augmented['mask']
            """## Grayscale
            x10 = cv2.cvtColor(x, cv2.COLOR_RGB2GRAY)
            y10 = y

                       ## Grayscale Vertical Flip
            aug = VerticalFlip(p=1)
            augmented = aug(image=x10, mask=y10)
            x11 = augmented['image']
            y11 = augmented['mask']

            ## Grayscale Horizontal Flip
            aug = HorizontalFlip(p=1)
            augmented = aug(image=x10, mask=y10)
            x12 = augmented['image']
            y12 = augmented['mask']

            ## Grayscale Center Crop
            aug = CenterCrop(p=1, height=crop_size[0], width=crop_size[1])
            augmented = aug(image=x10, mask=y10)
            x13 = augmented['image']
            y13 = augmented['mask']
            """
            ##
            aug = RandomBrightnessContrast(p=1)
            augmented = aug(image=x, mask=y)
            x14 = augmented['image']
            y14 = augmented['mask']

            aug = RandomGamma(p=1)
            augmented = aug(image=x, mask=y)
            x15 = augmented['image']
            y15 = augmented['mask']

            aug = HueSaturationValue(p=1)
            augmented = aug(image=x, mask=y)
            x16 = augmented['image']
            y16 = augmented['mask']

            aug = RGBShift(p=1)
            augmented = aug(image=x, mask=y)
            x17 = augmented['image']
            y17 = augmented['mask']

            aug = RandomBrightness(p=1)
            augmented = aug(image=x, mask=y)
            x18 = augmented['image']
            y18 = augmented['mask']

            aug = RandomContrast(p=1)
            augmented = aug(image=x, mask=y)
            x19 = augmented['image']
            y19 = augmented['mask']

            aug = MotionBlur(p=1, blur_limit=7)
            augmented = aug(image=x, mask=y)
            x20 = augmented['image']
            y20 = augmented['mask']

            aug = MedianBlur(p=1, blur_limit=9)
            augmented = aug(image=x, mask=y)
            x21 = augmented['image']
            y21 = augmented['mask']

            aug = GaussianBlur(p=1, blur_limit=9)
            augmented = aug(image=x, mask=y)
            x22 = augmented['image']
            y22 = augmented['mask']

            aug = GaussNoise(p=1)
            augmented = aug(image=x, mask=y)
            x23 = augmented['image']
            y23 = augmented['mask']

            aug = ChannelShuffle(p=1)
            augmented = aug(image=x, mask=y)
            x24 = augmented['image']
            y24 = augmented['mask']

            aug = CoarseDropout(p=1, max_holes=8, max_height=32, max_width=32)
            augmented = aug(image=x, mask=y)
            x25 = augmented['image']
            y25 = augmented['mask']

            images = [
                x,
                x1,
                x2,
                x3,
                x4,
                x5,
                x6,
                x7,
                x8,
                x9,  # x10,x11, x12, x13,
                x14,
                x15,
                x16,
                x17,
                x18,
                x19,
                x20,
                x21,
                x22,
                x23,
                x24,
                x25
            ]
            masks = [
                y,
                y1,
                y2,
                y3,
                y4,
                y5,
                y6,
                y7,
                y8,
                y9,  # y10, y11, y12, y13,
                y14,
                y15,
                y16,
                y17,
                y18,
                y19,
                y20,
                y21,
                y22,
                y23,
                y24,
                y25
            ]
            images = list(map(lambda a: cv2.resize(a, size), images))
            masks = list(map(lambda a: cv2.resize(a, size), masks))
        else:
            images = [cv2.resize(x, size)]
            masks = [cv2.resize(y, size)]

        list_of_img = list_of_img + images
        list_of_mask = list_of_mask + masks
    return list_of_img, list_of_mask
Exemplo n.º 9
0
        "2017-07-15",
        "2017-08-04",
        "2017-08-19",
    )
    rgb_file_path = f"{args.root}/train_rgb.csv"
    nir_file_path = f"{args.root}/train_b08.csv"

    additional_targets = {
        f"image{n}": "image"
        for n, _ in enumerate(dates[:-1])
    }
    data_transform = Compose(
        [
            RandomCrop(128, 128),
            VerticalFlip(p=0.3),
            Transpose(p=0.3),
            ShiftScaleRotate(p=0.3),
            RandomRotate90(p=0.3),
            GridDistortion(p=0.3),
            ElasticTransform(p=0.3),
            Normalize(),
        ],
        additional_targets=additional_targets,
    )

    val_transform = Compose(
        [RandomCrop(128, 128), Normalize()],
        additional_targets=additional_targets)

    #fold_sets = [[(0, 1, 2), (0, 1, 2)]]
    fold_sets = [[(1, 2), (0, )], [(0, 2), (1, )], [(0, 1), (2, )]]
Exemplo n.º 10
0
        augmented = aug(image=image, mask=gt_image)
        image_cropped = augmented['image']
        image_cropped = resize(image_cropped, (target_size, target_size))
        gt_cropped = augmented['mask']
        gt_cropped = resize(gt_cropped, (target_size, target_size))
        imsave(
            output_dir.joinpath(settings.image_subdir_name,
                                settings.dummycls_name,
                                '{}_{}.png'.format(file_stem, i)).as_posix(),
            image_cropped)
        imsave(output_dir.joinpath(settings.gt_subdir_name,
                                   settings.dummycls_name,
                                   '{}_{}.png'.format(file_stem,
                                                      i)).as_posix(),
               gt_cropped,
               check_contrast=False)


"""
augmentation pipeline for runtime
"""
runtime_augmentor = Compose([
    OneOf([Transpose(p=0.5), RandomRotate90(p=0.5)], p=0.5),
    OneOf([
        RGBShift(r_shift_limit=10, g_shift_limit=10, b_shift_limit=10),
        HueSaturationValue(
            hue_shift_limit=10, sat_shift_limit=15, val_shift_limit=10)
    ],
          p=0.3)
])
Exemplo n.º 11
0
valdf = train[train['fold'] == fold].reset_index(drop=True)
trndf = train[train['fold'] != fold].reset_index(drop=True)

# Data loaders
mean_img = [0.22363983, 0.18190407, 0.2523437]
std_img = [0.32451536, 0.2956294, 0.31335256]
transform_train = Compose([
    #ShiftScaleRotate(),
    #CenterCrop(height = SIZE//10, width = SIZE//10, p=0.3),
    HorizontalFlip(p=0.5),
    ShiftScaleRotate(shift_limit=0.05,
                     scale_limit=0.05,
                     rotate_limit=20,
                     p=0.3,
                     border_mode=cv2.BORDER_REPLICATE),
    Transpose(p=0.5),
    Normalize(mean=mean_img, std=std_img, max_pixel_value=255.0, p=1.0),
    ToTensor()
])

HFLIPVAL = 1.0 if HFLIP == 'T' else 0.0
TRANSPOSEVAL = 1.0 if TRANSPOSE == 'P' else 0.0
transform_test = Compose([
    HorizontalFlip(p=HFLIPVAL),
    Transpose(p=TRANSPOSEVAL),
    Normalize(mean=mean_img, std=std_img, max_pixel_value=255.0, p=1.0),
    ToTensor()
])

trndataset = IntracranialDataset(trndf,
                                 path=dir_train_img,
Exemplo n.º 12
0
 def imgClassificationfolder(self,
                             img_path,
                             model_list=None,
                             tfms=True,
                             advance_augmentation=False,
                             size=224,
                             bs=16,
                             epochs=1,
                             test_size=0.3):
     """
     RETURNS MODEL
 
     model_list: list of names of all models that need to be trained 
       Call Model_Names() function to get the list of available models
     img_path: path to the images root folder containing the individual image folders
     tfms: augmentation transforms. Possible methods are True or False
     advance_augmentation: Should we apply advance data augmentation?
     size: individual image size in dataloader. Default size set to 224
     bs:batch size
     epochs:number of epochs for which the individual models need to be trained
     test_size:test size for ranking the models
     """
     dictionary = {}
     dictionary["models"] = [
         resnet18, resnet34, resnet50, resnet101, resnet152, densenet121,
         densenet169, densenet201, densenet161, vgg16_bn, vgg19_bn
     ]
     dictionary["model_names"] = [
         'resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152',
         'densenet121', 'densenet169', 'densenet201', 'densenet161',
         'vgg16_bn', 'vgg19_bn'
     ]
     dictionary["efficientnets"] = [
         "efficientnet-b{}".format(i) for i in range(0, 8)
     ]
     accuracy = []
     roc_auc = []
     list_names = []
     path = ''
     if model_list is None:
         model_list = dictionary["model_names"]
     if tfms == True:
         if advance_augmentation:
             tfms = get_transforms(do_flip=True,
                                   max_lighting=0.2,
                                   max_zoom=1.1,
                                   max_warp=0.15,
                                   max_rotate=45,
                                   xtra_tfms=[
                                       alb_tfm2fastai(ElasticTransform()),
                                       alb_tfm2fastai(GridDistortion()),
                                       alb_tfm2fastai(OpticalDistortion()),
                                       alb_tfm2fastai(MedianBlur()),
                                       alb_tfm2fastai(ToGray()),
                                       alb_tfm2fastai(JpegCompression()),
                                       alb_tfm2fastai(Transpose()),
                                       alb_tfm2fastai(ShiftScaleRotate()),
                                       alb_tfm2fastai(Normalize()),
                                       alb_tfm2fastai(Blur()),
                                       alb_tfm2fastai(CLAHE()),
                                       alb_tfm2fastai(RGBShift()),
                                       alb_tfm2fastai(ChannelShuffle()),
                                       alb_tfm2fastai(RandomContrast()),
                                       alb_tfm2fastai(RandomBrightness())
                                   ])
         else:
             tfms = get_transforms(do_flip=True,
                                   max_lighting=0.2,
                                   max_zoom=1.1,
                                   max_warp=0.15,
                                   max_rotate=45)
     else:
         tfms = False
     data = ImageDataBunch.from_folder(
         img_path,
         valid_pct=test_size,
         ds_tfms=tfms,
         size=224,
         num_workers=4).normalize(imagenet_stats)
     print("Loaded dataset\n")
     print("Labels classified from the source are {}\n".format(
         data.classes))
     max_score = 0
     for model in model_list:
         if model in dictionary["model_names"]:
             print("Started training {}\n".format(model))
             acc, auroc, names, model = self.res_dense_vgg(
                 data, eval(model), epochs)
             accuracy.append(acc)
             roc_auc.append(auroc)
             list_names.append(names)
         if model in dictionary['efficientnets']:
             print("Started training {}\n".format(model))
             acc, auroc, names, model = self.eff(data, model, epochs)
             accuracy.append(acc)
             roc_auc.append(auroc)
             list_names.append(names)
         if acc > max_score:
             best_model = model
             max_score = acc
         else:
             del model
     df = pd.DataFrame(list(zip(model_list, accuracy, roc_auc)),
                       columns=['Model', 'Accuracy', 'ROAUC'])
     df.sort_values('Accuracy', inplace=True, ascending=False)
     self.plot_table(df)
     return best_model
Exemplo n.º 13
0
    def __init__(self, opt, img_mask_dirs, train=True, loader=default_loader):
        self.annotations = img_mask_dirs
        self.train = train
        self.loader = loader

        if opt.debug:
            self.annotations = self.annotations[:8]
        curr_size = opt.img_size
        min_max_height = opt.crop_sizes

        self.Transforms = {
            "train":
            transforms.Compose([
                transforms.Resize(opt.img_size),
                transforms.RandomHorizontalFlip(),  # 水平翻转
                transforms.RandomVerticalFlip(),
                transforms.RandomRotation(opt.rotation),  # 旋转up to 30 degree
                transforms.ToTensor(),  # for PIL image, 0-1 range
                transforms.Normalize([0.485, 0.456, 0.406],
                                     [0.229, 0.224, 0.225])
            ]),
            "val":
            transforms.Compose([
                transforms.Resize(opt.img_size),
                transforms.ToTensor(),
                transforms.Normalize([0.485, 0.456, 0.406],
                                     [0.229, 0.224, 0.225])
            ]),
            "tgt_train":
            transforms.Compose([
                transforms.Resize(opt.img_size),
                transforms.RandomHorizontalFlip(),  # 水平翻转
                transforms.RandomVerticalFlip(),
                transforms.RandomRotation(opt.rotation),  # 旋转up to 30 degree
                transforms.ToTensor(),  # for PIL image, 0-1 range
                transforms.Normalize([0.485, 0.456, 0.406],
                                     [0.229, 0.224, 0.225])
            ]),
        }
        self.transform_basic = Compose([
            RandomSizedCrop(min_max_height=min_max_height,
                            height=curr_size,
                            width=curr_size,
                            always_apply=True,
                            p=1.0),
            OneOf([
                Transpose(p=0.5),
                HorizontalFlip(p=0.5),
                VerticalFlip(p=0.5),
                Rotate(p=0.5),
            ],
                  p=1.0),
        ])

        self.gt_transform = transforms.Compose([
            lambda img: np.array(img)[np.newaxis, ...],
            #lambda nd: nd / 255,  # max <= 1
            lambda nd: torch.tensor(nd, dtype=torch.long)
        ])

        self.img_transform = transforms.Compose([
            lambda nd: torch.tensor(nd, dtype=torch.float32),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
        ])

        self.opt = opt
Exemplo n.º 14
0
def augment_data(images, masks, save_path, augment=True):
    """ Performing data augmentation. """
    crop_size = (192 - 32, 256 - 32)
    size = (256, 192)

    for image, mask in tqdm(zip(images, masks), total=len(images)):
        image_name = image.split("/")[-1].split(".")[0]
        mask_name = mask.split("/")[-1].split(".")[0]

        x, y = read_data(image, mask)
        try:
            h, w, c = x.shape
        except Exception as e:
            image = image[:-1]
            x, y = read_data(image, mask)
            h, w, c = x.shape

        if augment == True:
            ## Center Crop
            aug = CenterCrop(p=1, height=crop_size[0], width=crop_size[1])
            augmented = aug(image=x, mask=y)
            x1 = augmented['image']
            y1 = augmented['mask']

            ## Crop
            x_min = 0
            y_min = 0
            x_max = x_min + size[0]
            y_max = y_min + size[1]

            aug = Crop(p=1, x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max)
            augmented = aug(image=x, mask=y)
            x2 = augmented['image']
            y2 = augmented['mask']

            ## Random Rotate 90 degree
            aug = RandomRotate90(p=1)
            augmented = aug(image=x, mask=y)
            x3 = augmented['image']
            y3 = augmented['mask']

            ## Transpose
            aug = Transpose(p=1)
            augmented = aug(image=x, mask=y)
            x4 = augmented['image']
            y4 = augmented['mask']

            ## ElasticTransform
            aug = ElasticTransform(p=1,
                                   alpha=120,
                                   sigma=120 * 0.05,
                                   alpha_affine=120 * 0.03)
            augmented = aug(image=x, mask=y)
            x5 = augmented['image']
            y5 = augmented['mask']

            ## Grid Distortion
            aug = GridDistortion(p=1)
            augmented = aug(image=x, mask=y)
            x6 = augmented['image']
            y6 = augmented['mask']

            ## Optical Distortion
            aug = OpticalDistortion(p=1, distort_limit=2, shift_limit=0.5)
            augmented = aug(image=x, mask=y)
            x7 = augmented['image']
            y7 = augmented['mask']

            ## Vertical Flip
            aug = VerticalFlip(p=1)
            augmented = aug(image=x, mask=y)
            x8 = augmented['image']
            y8 = augmented['mask']

            ## Horizontal Flip
            aug = HorizontalFlip(p=1)
            augmented = aug(image=x, mask=y)
            x9 = augmented['image']
            y9 = augmented['mask']

            ## Grayscale
            x10 = cv2.cvtColor(x, cv2.COLOR_RGB2GRAY)
            y10 = y

            ## Grayscale Vertical Flip
            aug = VerticalFlip(p=1)
            augmented = aug(image=x10, mask=y10)
            x11 = augmented['image']
            y11 = augmented['mask']

            ## Grayscale Horizontal Flip
            aug = HorizontalFlip(p=1)
            augmented = aug(image=x10, mask=y10)
            x12 = augmented['image']
            y12 = augmented['mask']

            ## Grayscale Center Crop
            aug = CenterCrop(p=1, height=crop_size[0], width=crop_size[1])
            augmented = aug(image=x10, mask=y10)
            x13 = augmented['image']
            y13 = augmented['mask']

            ##
            aug = RandomBrightnessContrast(p=1)
            augmented = aug(image=x, mask=y)
            x14 = augmented['image']
            y14 = augmented['mask']

            aug = RandomGamma(p=1)
            augmented = aug(image=x, mask=y)
            x15 = augmented['image']
            y15 = augmented['mask']

            aug = HueSaturationValue(p=1)
            augmented = aug(image=x, mask=y)
            x16 = augmented['image']
            y16 = augmented['mask']

            aug = RGBShift(p=1)
            augmented = aug(image=x, mask=y)
            x17 = augmented['image']
            y17 = augmented['mask']

            aug = RandomBrightness(p=1)
            augmented = aug(image=x, mask=y)
            x18 = augmented['image']
            y18 = augmented['mask']

            aug = RandomContrast(p=1)
            augmented = aug(image=x, mask=y)
            x19 = augmented['image']
            y19 = augmented['mask']

            aug = MotionBlur(p=1, blur_limit=7)
            augmented = aug(image=x, mask=y)
            x20 = augmented['image']
            y20 = augmented['mask']

            aug = MedianBlur(p=1, blur_limit=11)
            augmented = aug(image=x, mask=y)
            x21 = augmented['image']
            y21 = augmented['mask']

            aug = GaussianBlur(p=1, blur_limit=11)
            augmented = aug(image=x, mask=y)
            x22 = augmented['image']
            y22 = augmented['mask']

            aug = GaussNoise(p=1)
            augmented = aug(image=x, mask=y)
            x23 = augmented['image']
            y23 = augmented['mask']

            aug = ChannelShuffle(p=1)
            augmented = aug(image=x, mask=y)
            x24 = augmented['image']
            y24 = augmented['mask']

            aug = CoarseDropout(p=1, max_holes=8, max_height=32, max_width=32)
            augmented = aug(image=x, mask=y)
            x25 = augmented['image']
            y25 = augmented['mask']

            images = [
                x, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14,
                x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25
            ]
            masks = [
                y, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12, y13, y14,
                y15, y16, y17, y18, y19, y20, y21, y22, y23, y24, y25
            ]

        else:
            images = [x]
            masks = [y]

        idx = 0
        for i, m in zip(images, masks):
            i = cv2.resize(i, size)
            m = cv2.resize(m, size)

            tmp_image_name = f"{image_name}_{idx}.bmp"
            tmp_mask_name = f"{mask_name}_{idx}.bmp"

            image_path = os.path.join(save_path, "image/", tmp_image_name)
            mask_path = os.path.join(save_path, "mask/", tmp_mask_name)

            cv2.imwrite(image_path, i)
            cv2.imwrite(mask_path, m)

            idx += 1
Exemplo n.º 15
0
def generate_transforms(image_size):

    # train_transform = Compose(
    #     [
    #         Resize(height=int(image_size[0]), width=int(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),
    #         # Transpose(p=0.5),
    #         HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
    #         CoarseDropout(p=0.5),
    #         Cutout(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),
    #     ]
    # )
    train_transform = Compose(
        [
            RandomResizedCrop(int(image_size[0]),
                              int(image_size[1]),
                              scale=(0.08, 1.0),
                              ratio=(0.75, 1.3333333333333333)),
            Transpose(p=0.5),
            HorizontalFlip(p=0.5),
            VerticalFlip(p=0.5),
            ShiftScaleRotate(p=0.5),
            HueSaturationValue(hue_shift_limit=0.2,
                               sat_shift_limit=0.2,
                               val_shift_limit=0.2,
                               p=0.5),
            RandomBrightnessContrast(brightness_limit=(-0.1, 0.1),
                                     contrast_limit=(-0.1, 0.1),
                                     p=0.5),
            Normalize(mean=[0.485, 0.456, 0.406],
                      std=[0.229, 0.224, 0.225],
                      max_pixel_value=255.0,
                      p=1.0),
            # CoarseDropout(p=0.5),
            # Cutout(p=0.5),
            # ToTensorV2(p=1.0),
        ],
        p=1.)

    val_transform = Compose([
        Resize(height=int(image_size[0]), width=int(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
    }
Exemplo n.º 16
0
 def __init__(self, prob):
     self.horizontal = HorizontalFlip(p=prob)
     self.vertical = VerticalFlip(p=prob)
     self.rotate = RandomRotate90(p=prob)
     self.transpose = Transpose(p=prob)
import pandas as pd
import numpy as np
from torch.utils.data import Dataset

from albumentations import (CLAHE, RandomRotate90, Transpose, ShiftScaleRotate,
                            Blur, OpticalDistortion, GridDistortion,
                            HueSaturationValue, IAAAdditiveGaussianNoise,
                            GaussNoise, MotionBlur, MedianBlur,
                            IAAPiecewiseAffine, IAASharpen, IAAEmboss,
                            RandomContrast, RandomBrightness, Flip, OneOf,
                            Compose)

aug = Compose([
    RandomRotate90(),
    Flip(),
    Transpose(),
    OneOf([
        IAAAdditiveGaussianNoise(),
        GaussNoise(),
    ], p=0.2),
    OneOf([
        MotionBlur(p=.2),
        MedianBlur(blur_limit=3, p=.1),
        Blur(blur_limit=3, p=.1),
    ],
          p=0.2),
    ShiftScaleRotate(
        shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=.2),
    OneOf([
        OpticalDistortion(p=0.3),
        GridDistortion(p=.1),