Exemplo n.º 1
0
def transform_img(image, iaas=iaas):
    '''
    :param image: input image
    :param image_groudtruth: ground truth of input image
    :return:
    '''
    ia.seed(1)
    images = np.array([image for _ in range(len(iaas))])

    for i in range(len(iaas)):
        seq = iaa.Sequential([iaas[i][0]], random_order=True) # apply augmenters in random order
        seq_det = seq.to_deterministic()
        images[i:i+1] = seq_det.augment_images(images[i:i+1])

    return images
Exemplo n.º 2
0
def reverse_gt(images_groundtruth):
    '''
    :param image: input image
    :param image_groudtruth: ground truth of input image
    :return:
    '''
    ia.seed(1)
    images_groundtruth_reverse = copy.copy(images_groundtruth)

    for i in range(len(iaas)):
        seq_reverse = iaa.Sequential([iaas[i][1]], random_order=True)
        seq_reverse_det = seq_reverse.to_deterministic()

        images_groundtruth_reverse[i:i+1] = seq_reverse_det.augment_images(images_groundtruth[i:i+1])

    return images_groundtruth_reverse
Exemplo n.º 3
0
def aug_img(image, image_groundtruth):
    '''
    :param image: input image
    :param image_groudtruth: ground truth of input image
    :return:
    16 augmented images of shape(16, height, width, 3) and its ground truth images of shape(16, height, width)
    '''
    ia.seed(1)
    # dst = transform.rescale(image, 0.2)
    images = np.array(
        [image for _ in range(16)]
    )

    # dst_groundtruth = transform.rescale(image_groudtruth, 0.2)
    images_groundtruth = np.array(
        [image_groundtruth for _ in range(16)]
    )

    seq = iaa.Sequential([
        iaa.Fliplr(0.5), # horizontal flips
        iaa.Flipud(0.5),
        iaa.Crop(percent=(0, 0.01)), # random crops
        # Small gaussian blur with random sigma between 0 and 0.5.
        # But we only blur about 50% of all images.
        iaa.Sometimes(0.5,
            iaa.GaussianBlur(sigma=(0, 0.5))
        ),
        # Make some images brighter and some darker.
        # In 20% of all cases, we sample the multiplier once per channel,
        # which can end up changing the color of the images.
        iaa.Multiply((0.8, 1.2), per_channel=0.2),
        # Apply affine transformations to each image.
        # Scale/zoom them, translate/move them, rotate them and shear them.
        iaa.Affine(
            scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
            # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
            rotate=(-25, 25),
            shear=(-8, 8)
        )
    ], random_order=True) # apply augmenters in random order

    seq_det = seq.to_deterministic()
    images = seq_det.augment_images(images)
    images_groundtruth = seq_det.augment_images(images_groundtruth)
    images_groundtruth[images_groundtruth > 0] = 1

    return images, images_groundtruth
Exemplo n.º 4
0
def main():
    img = data.astronaut()
    img = ia.imresize_single_image(img, (64, 64))
    aug = iaa.Fliplr(0.5)
    unseeded1 = aug.draw_grid(img, cols=8, rows=1)
    unseeded2 = aug.draw_grid(img, cols=8, rows=1)

    ia.seed(1000)
    seeded1 = aug.draw_grid(img, cols=8, rows=1)
    seeded2 = aug.draw_grid(img, cols=8, rows=1)

    ia.seed(1000)
    reseeded1 = aug.draw_grid(img, cols=8, rows=1)
    reseeded2 = aug.draw_grid(img, cols=8, rows=1)

    ia.seed(1001)
    reseeded3 = aug.draw_grid(img, cols=8, rows=1)
    reseeded4 = aug.draw_grid(img, cols=8, rows=1)

    all_rows = np.vstack([unseeded1, unseeded2, seeded1, seeded2, reseeded1, reseeded2, reseeded3, reseeded4])
    ia.imshow(all_rows)
Exemplo n.º 5
0
# -*- coding: utf-8 -*-

import os
from PIL import Image, ImageFont, ImageDraw
import sys
import random
import imageio
import imgaug as ia
from imgaug import augmenters as iaa
import random
import numpy as np
import random
import matplotlib.pyplot as plt
import matplotlib as mpl

ia.seed(4)  # set seed for consistent cropping

# CONSTANTS  ----------------

# image creation constant
width = 128
height = 128

num_lines = 2
num_grams = 100  # num copies of grams for a given font

chars = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ             '  # adding spaces to increase likelihood

# more negative moves char up
height_offset = -4  # -11 is good
Exemplo n.º 6
0
def draw_single_sequential_images():
    ia.seed(44)

    #image = misc.imresize(ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128))
    image = ia.quokka_square(size=(128, 128))

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(
        [
            # apply the following augmenters to most images
            iaa.Fliplr(0.5), # horizontally flip 50% of all images
            iaa.Flipud(0.2), # vertically flip 20% of all images
            # crop images by -5% to 10% of their height/width
            sometimes(iaa.CropAndPad(
                percent=(-0.05, 0.1),
                pad_mode=ia.ALL,
                pad_cval=(0, 255)
            )),
            sometimes(iaa.Affine(
                scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
                translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                rotate=(-45, 45), # rotate by -45 to +45 degrees
                shear=(-16, 16), # shear by -16 to +16 degrees
                order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
            )),
            # execute 0 to 5 of the following (less important) augmenters per image
            # don't execute all of them, as that would often be way too strong
            iaa.SomeOf((0, 5),
                [
                    sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation
                    iaa.OneOf([
                        iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0
                        iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7
                        iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7
                    ]),
                    iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images
                    iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images
                    # search either for all edges or for directed edges,
                    # blend the result with the original image using a blobby mask
                    iaa.SimplexNoiseAlpha(iaa.OneOf([
                        iaa.EdgeDetect(alpha=(0.5, 1.0)),
                        iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)),
                    ])),
                    iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images
                    iaa.OneOf([
                        iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
                        iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
                    ]),
                    iaa.Invert(0.05, per_channel=True), # invert color channels
                    iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value)
                    iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation
                    # either change the brightness of the whole image (sometimes
                    # per channel) or change the brightness of subareas
                    iaa.OneOf([
                        iaa.Multiply((0.5, 1.5), per_channel=0.5),
                        iaa.FrequencyNoiseAlpha(
                            exponent=(-4, 0),
                            first=iaa.Multiply((0.5, 1.5), per_channel=True),
                            second=iaa.ContrastNormalization((0.5, 2.0))
                        )
                    ]),
                    iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
                    iaa.Grayscale(alpha=(0.0, 1.0)),
                    sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)
                    sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), # sometimes move parts of the image around
                    sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))
                ],
                random_order=True
            )
        ],
        random_order=True
    )

    grid = seq.draw_grid(image, cols=8, rows=8)
    misc.imsave("examples_grid.jpg", grid)
Exemplo n.º 7
0
    def get_transformed_samples(self,
                                num_examples,
                                random_images=True,
                                save_to_dir=True,
                                out_dir='aug_3d',
                                train=False):
        """Apply selected transformations to a defined number of images from
           the dataset. 
            
           Parameters
           ----------
           num_examples : int
               Number of examples to generate.
            
           random_images : bool, optional
               Randomly select images from the dataset. If False the examples
               will be generated from the start of the dataset. 

           save_to_dir : bool, optional
               Save the images generated. The purpose of this variable is to
               check the images generated by data augmentation.

           out_dir : str, optional
               Name of the folder where the examples will be stored. 

           train : bool, optional
               To avoid drawing a grid on the generated images. This should be
               set when the samples will be used for training.
        """

        if random_images == False and num_examples > self.X.shape[0]:
            num_examples = self.X.shape[0]
            print("WARNING: More samples requested than the ones available. "
                  "'num_examples' fixed to {}".format(num_examples))

        sample_x = np.zeros((num_examples, ) + self.shape, dtype=np.float32)
        sample_y = np.zeros(
            (num_examples, ) + self.shape[:3] + (self.channels, ),
            dtype=np.uint8)

        # Generate the examples
        print("0) Creating samples of data augmentation . . .")
        for i in tqdm(range(num_examples)):
            ia.seed(i)
            if random_images or self.random_subvolumes_in_DA:
                pos = random.randint(0, self.X.shape[0] - 1)
            else:
                pos = i

            if self.random_subvolumes_in_DA:
                vol, vol_mask, ox, oy, oz,\
                s_x, s_y, s_z = random_3D_crop(
                    self.X[pos], self.Y[pos], self.shape, self.val,
                    draw_prob_map_points=True,
                    vol_prob=(self.prob_map[pos] if self.prob_map is not None else None))
            else:
                vol = np.copy(self.X[pos])
                vol_mask = np.copy(self.Y[pos])

            if not self.da:
                sample_x[i] = vol
                sample_y[i] = vol_mask
                self.trans_made = ''
            else:
                if not train:
                    self.__draw_grid(vol)
                    self.__draw_grid(vol_mask)

                extra_img = np.random.randint(0, self.X.shape[0])
                sample_x[i], sample_y[i] = self.apply_transform(
                    vol,
                    vol_mask,
                    e_im=self.X[extra_img],
                    e_mask=self.Y[extra_img])

            # Save transformed 3D volumes
            if save_to_dir:
                os.makedirs(out_dir, exist_ok=True)
                # Original image/mask
                f = os.path.join(
                    out_dir, "orig_x_" + str(pos) + self.trans_made + '.tiff')
                aux = self.X[pos].copy()
                self.__draw_grid(aux)
                aux = np.expand_dims(
                    (np.transpose(aux,
                                  (2, 0, 1, 3)) * 255).astype(np.uint8), 1)
                imsave(f, aux, imagej=True, metadata={'axes': 'ZCYXS'})
                f = os.path.join(
                    out_dir, "orig_y_" + str(pos) + self.trans_made + '.tiff')
                aux = self.Y[pos].copy()
                self.__draw_grid(aux)
                aux = np.expand_dims(
                    (np.transpose(aux,
                                  (2, 0, 1, 3)) * 255).astype(np.uint8), 1)
                imsave(f, aux, imagej=True, metadata={'axes': 'ZCYXS'})
                # Transformed
                f = os.path.join(
                    out_dir, "x_aug_" + str(pos) + self.trans_made + '.tiff')
                aux = np.expand_dims(
                    (np.transpose(sample_x[i],
                                  (2, 0, 1, 3)) * 255).astype(np.uint8), 1)
                imsave(f, aux, imagej=True, metadata={'axes': 'ZCYXS'})
                # Mask
                f = os.path.join(
                    out_dir, "y_aug_" + str(pos) + self.trans_made + '.tiff')
                aux = np.expand_dims(
                    (np.transpose(sample_y[i],
                                  (2, 0, 1, 3)) * 255).astype(np.uint8), 1)
                imsave(f, aux, imagej=True, metadata={'axes': 'ZCYXS'})

                # Save the original images with a red point and a blue square
                # that represents the point selected with the probability map
                # and the random volume extracted from the original data
                if self.random_subvolumes_in_DA and self.prob_map is not None and i == 0:
                    rc_out_dir = os.path.join(out_dir, 'rd_crop' + str(pos))
                    os.makedirs(rc_out_dir, exist_ok=True)

                    print(
                        "The selected point on the random crop was [{},{},{}]".
                        format(ox, oy, oz))

                    d = len(str(self.X[pos].shape[2]))
                    for i in range(self.X[pos].shape[2]):
                        im = Image.fromarray((self.X[pos, :, :, i,
                                                     0]).astype(np.uint8))
                        im = im.convert('RGB')
                        px = im.load()
                        mask = Image.fromarray((self.Y[pos, :, :, i,
                                                       0]).astype(np.uint8))
                        mask = mask.convert('RGB')
                        py = mask.load()

                        if i == oz:
                            # Paint the selected point in red
                            p_size = 6
                            for row in range(oy - p_size, oy + p_size):
                                for col in range(ox - p_size, ox + p_size):
                                    if col >= 0 and col < self.X[pos].shape[0] and \
                                       row >= 0 and row < self.X[pos].shape[1]:
                                        px[row, col] = (255, 0, 0)
                                        py[row, col] = (255, 0, 0)

                        if i >= s_z and i < s_z + self.shape[2]:
                            # Paint a blue square that represents the crop made
                            for col in range(s_x, s_x + self.shape[0]):
                                px[s_y, col] = (0, 0, 255)
                                px[s_y + self.shape[0] - 1, col] = (0, 0, 255)
                                py[s_y, col] = (0, 0, 255)
                                py[s_y + self.shape[0] - 1, col] = (0, 0, 255)
                            for row in range(s_y, s_y + self.shape[1]):
                                px[row, s_x] = (0, 0, 255)
                                px[row, s_x + self.shape[1] - 1] = (0, 0, 255)
                                py[row, s_x] = (0, 0, 255)
                                py[row, s_x + self.shape[1] - 1] = (0, 0, 255)

                        im.save(
                            os.path.join(rc_out_dir,
                                         'rc_x_' + str(i).zfill(d) + '.png'))
                        mask.save(
                            os.path.join(rc_out_dir,
                                         'rc_y_' + str(i).zfill(d) + '.png'))
        return sample_x, sample_y
def main():
    writer = SummaryWriter(max_queue=10000)
    writer_logdir = str(writer.log_dir)[5:]
    print('writer_logdir :', writer_logdir)
    os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'
    ia.seed(100)
    np.random.seed(100)
    torch.manual_seed(100)
    torch.cuda.manual_seed(100)
    torch.cuda.manual_seed_all(100)

    Model = writer_logdir
    if not os.path.isdir('./{}'.format(Model)):
        os.mkdir('./{}'.format(Model))


    ##for matplotlib absolute colorbar
    levels = [0, 1, 2, 3, 4]
    colors = ['green', 'yellow', 'blue', 'red']
    cmap, norm = matplotlib.colors.from_levels_and_colors(levels, colors)


    device = 'cuda'

    nets = [NewDensenet() for _ in range(3)]  # 3 is K-fold number
    optimizers = [optim.Adam(nets[i].parameters(), lr=0.001, weight_decay=0.00001) for i in range(3)]
    schedulers = [optim.lr_scheduler.MultiStepLR(optimizer, milestones=[30, 40, 50], gamma=0.3) for optimizer in
                  optimizers]
    criterion = nn.NLLLoss(weight=None, reduction='mean')
    dataset = make_dataset() #[(dataset_train0,dataset_val0),(dataset_train1,dataset_val),(dataset_train1,dataset_val1)]

    ##viewing some images for sanity check
    #image_show(dataset_train=dataset[0][0], dataset_val=dataset[0][1])

    for k_fold in [2, 1, 0]:
        Dataloader_train = DataLoader(dataset[k_fold][0], batch_size=2, shuffle=True, num_workers=4, drop_last=True)
        Dataloader_val = DataLoader(dataset[k_fold][1], batch_size=2, shuffle=True, num_workers=4, drop_last=True)

        total_iter_train = [40 * len(Dataloader_train), 40 * len(Dataloader_train), 40 * len(Dataloader_train)]
        total_iter_val = [40 * len(Dataloader_val), 40 * len(Dataloader_val), 40 * len(Dataloader_val)]

        net = nets[k_fold]
        net = nn.DataParallel(net)
        net.to(device)

        optimizer = optimizers[k_fold]
        scheduler = schedulers[k_fold]

        running_loss = 0
        iter_train = 0
        iter_val = 0

        train_acc_whole_epoch = [0, ]
        val_acc_whole_epoch = [0, ]
        train_acc_one_epoch = 0
        val_acc_one_epoch = 0

        best_epoch = None
        best_model_st_dct = None
        best_optimizer_st_dct = None
        best_val_acc = 0

        epoch = 0
        whole_epoch = 40
        while epoch < whole_epoch:
            scheduler.step()
            for i, (images, targets) in enumerate(Dataloader_train):
                iter_train += 1
                net.train()

                images = images.type('torch.FloatTensor')
                targets = targets.type('torch.LongTensor')

                images, targets = images.to(device), targets.to(device)

                gt_pixelwise = torch.ones((2, 64, 64)).type('torch.LongTensor').to(device)
                gt_pixelwise = torch.mul(gt_pixelwise, targets.view(-1, 1, 1))


                scores = net(images)

                loss = criterion(scores, gt_pixelwise)  # scores = (N, 4, 63, 63), gt_pixelwise = (N, 63, 63)

                optimizer.zero_grad()
                loss.backward()
                optimizer.step()

                with torch.set_grad_enabled(False):
                    ##for running loss
                    running_loss = 0.1 * running_loss + 0.9 * loss
                    if (i + 1) % 5 == 0:
                        print(
                            'train mode | epoch : [%d/%d] | #fold : [%d/3] | running_loss : [%.5f] iterations : [%d/%d]'
                            % (epoch + 1, whole_epoch, k_fold + 1, running_loss, iter_train, total_iter_train[k_fold]))

                    pred_pixelwise = torch.argmax(scores, dim=1)  # pred_pixelwise = (batch_size, 63, 63), dype = torch.LongTensor

                    ##for accuracy
                    predicts = torch.zeros((2,), dtype=torch.long).to(device)

                    for p in range(2):
                        zero_pixnum = (pred_pixelwise[p] == 0).sum()
                        one_pixnum = (pred_pixelwise[p] == 1).sum()
                        two_pixnum = (pred_pixelwise[p] == 2).sum()
                        thr_pixnum = (pred_pixelwise[p] == 3).sum()
                        predicts[p:p+1] = torch.argmax(
                            torch.IntTensor([zero_pixnum, one_pixnum, two_pixnum, thr_pixnum]), dim=0)

                    true1_false0 = targets == predicts
                    train_acc_one_epoch += true1_false0.sum().item()


                    ##for draw
                    # RGB, pred, GT
                    if i >= len(Dataloader_train) - 5:
                        print('let\'s draw!')
                        rand = np.random.randint(2)
                        rgb = images[rand].cpu().numpy()  # dtype = float, (1024, 1024, 3)
                        pred_pixelwise = pred_pixelwise[rand].cpu().numpy()  # dtype = # int64?, (64, 64)
                        GT = gt_pixelwise[rand].cpu().numpy()  # dtype = long, (64, 64) either 0,1,2,3
                        GT[0][0] = 0
                        GT[0][1] = 1
                        GT[0][2] = 2
                        GT[0][3] = 3
                        fig = plt.figure(figsize=(9, 3))
                        ax1 = fig.add_subplot(131)
                        ax2 = fig.add_subplot(132)
                        ax3 = fig.add_subplot(133)

                        im1 = ax1.imshow(np.transpose(rgb, (1, 2, 0)) / 255)
                        im2 = ax2.imshow(pred_pixelwise, cmap=cmap, norm=norm, interpolation='none')
                        im3 = ax3.imshow(GT, cmap=cmap, norm=norm, interpolation='none')

                        fig.set_constrained_layout_pads(w_pad=2. / 72., h_pad=2. / 72.,
                                                        hspace=0., wspace=0.)
                        # CB = fig.colorbar(ax2, shrink=0.8, extend='both')

                        divider = make_axes_locatable(ax2)
                        cax = divider.append_axes('right', size='3%', pad=0.03)
                        fig.colorbar(im2, cax=cax, orientation='vertical')

                        writer.add_figure('Train|{}fold|{}epoch|last5_iter_figures'.format(k_fold + 1, epoch), fig,
                                          epoch + 1)

            ##for running loss per epoch
            writer.add_scalar('{}_fold_running_loss'.format(k_fold + 1), running_loss, epoch + 1)

            ##for train accuracy per epoch
            train_acc_one_epoch /= len(Dataloader_train.dataset)
            writer.add_scalar('{}fold_train_acc'.format(k_fold + 1), train_acc_one_epoch, epoch + 1)
            train_acc_one_epoch = 0

            confusion_matrixx = torch.zeros(4, 4)

            for i, (images, targets) in enumerate(Dataloader_val):
                iter_val += 1
                net.eval()

                with torch.set_grad_enabled(False):
                    images = images.type('torch.FloatTensor')
                    targets = targets.type('torch.LongTensor')

                    images, targets = images.to(device), targets.to(device)
                    gt_pixelwise = torch.ones((2, 64, 64)).type('torch.LongTensor').to(device)
                    gt_pixelwise = torch.mul(gt_pixelwise, targets.view(-1, 1, 1))

                    scores = net(images)
                    pred_pixelwise = torch.argmax(scores, dim=1)

                    # for accuracy
                    predicts = torch.zeros((2,), dtype=torch.long).to(device)

                    for p in range(2):
                        zero_pixnum = (pred_pixelwise[p] == 0).sum()
                        one_pixnum = (pred_pixelwise[p] == 1).sum()
                        two_pixnum = (pred_pixelwise[p] == 2).sum()
                        thr_pixnum = (pred_pixelwise[p] == 3).sum()
                        predicts[p:p + 1] = torch.argmax(
                            torch.IntTensor([zero_pixnum, one_pixnum, two_pixnum, thr_pixnum]), dim=0)

                    true1_false0 = targets == predicts
                    val_acc_one_epoch += true1_false0.sum().item()

                    print('val mode | epoch : [%d/%d] | #fold : [%d/3] | iterations : [%d/%d]'
                          % (epoch + 1, whole_epoch, k_fold + 1, iter_val, total_iter_val[k_fold]))

                    ##for draw
                    # RGB, pred, GT
                    if i >= len(Dataloader_val) - 5:
                        print('let\'s draw!')
                        rand = np.random.randint(2)
                        rgb = images[rand].cpu().numpy()  # dtype = float, (1024, 1024, 3)
                        pred_pixelwise = pred_pixelwise[rand].cpu().numpy()  # dtype = # int64?, (64, 64)
                        GT = gt_pixelwise[rand].type(
                            'torch.cuda.LongTensor').cpu().numpy()  # dtype = long, (64, 64) either 0,1,2,3
                        GT[0][0] = 0
                        GT[0][1] = 1
                        GT[0][2] = 2
                        GT[0][3] = 3

                        fig = plt.figure(figsize=(9, 3))
                        ax1 = fig.add_subplot(131)
                        ax2 = fig.add_subplot(132)
                        ax3 = fig.add_subplot(133)

                        im1 = ax1.imshow(np.transpose(rgb, (1, 2, 0)) / 255)
                        im2 = ax2.imshow(pred_pixelwise, cmap=cmap, norm=norm, interpolation='none')
                        im3 = ax3.imshow(GT, cmap=cmap, norm=norm, interpolation='none')

                        fig.set_constrained_layout_pads(w_pad=2. / 72., h_pad=2. / 72.,
                                                        hspace=0., wspace=0.)
                        # CB = fig.colorbar(ax2, shrink=0.8, extend='both')

                        divider = make_axes_locatable(ax2)
                        cax = divider.append_axes('right', size='3%', pad=0.03)
                        fig.colorbar(im2, cax=cax, orientation='vertical')

                        writer.add_figure('Val|{}fold|{}epoch|last5_iter_figures'.format(k_fold + 1, epoch), fig,
                                          epoch + 1)

                    # confusion_matrix
                    for t, p in zip(targets.view(-1), predicts.view(-1)):
                        confusion_matrixx[t.long(), p.long()] += 1

            val_acc_one_epoch /= len(Dataloader_val.dataset)
            writer.add_scalar('{}fold_val_acc'.format(k_fold + 1), val_acc_one_epoch, epoch + 1)
            val_acc_whole_epoch.append(val_acc_one_epoch)

            fig = plot_confusion_matrix(confusion_matrixx, classes=['benign', 'cancer1', 'cancer2', 'cancer3'],
                                        title='ConFusionMaTrix')
            writer.add_figure('{}fold confusion_matrix'.format(k_fold + 1), fig, epoch + 1)
            val_acc_one_epoch = 0
            epoch += 1

            # Save the model
            if epoch > 50:
                if not os.path.isdir('./{}/{}fold'.format(Model, k_fold+1)):
                    os.mkdir('./{}/{}fold'.format(Model, k_fold+1))

                PATH = './{}/{}fold/epoch{} valAcc{}.tar'.format(Model, k_fold+1, epoch+1, val_acc_one_epoch)
                torch.save({
                     'epoch': epoch+1,
                     'model_state_dict': net.state_dict(),
                     'optimizer_state_dict': optimizer.state_dict(),
                     'scheduler_state_dict:':scheduler.state_dict()

                }, PATH)
def draw_single_sequential_images(img_path,idx):
    ia.seed(44)
    image = ndimage.imread(img_path)
    #image = ia.quokka_square(size=(128, 128))

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(
        [
            # apply the following augmenters to most images
            iaa.Fliplr(0.5), # horizontally flip 50% of all images
            # crop images by -5% to 10% of their height/width
            sometimes(iaa.Affine(
                scale={"x": (0.125, 0.75), "y": (0.125, 0.75)}, # scale images to 80-120% of their size, individually per axis
                translate_percent={"x": (-0.25, 0.25), "y": (-0.25, 0.25)}, # translate by -20 to +20 percent (per axis)
                rotate=(-30, 30), # rotate by -45 to +45 degrees
                shear=(-25, 25), # shear by -16 to +16 degrees
                order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                # use any of scikit-image's warping modes (see 2nd image from the top for examples)
            )),
            # execute 0 to 5 of the following (less important) augmenters per image
            # don't execute all of them, as that would often be way too strong
            iaa.SomeOf((0, 5),
                [
                    iaa.OneOf([
                        iaa.GaussianBlur((1, 2.0)), # blur images with a sigma between 0 and 3.0
                    ]),
                    iaa.Sharpen(alpha=(1.0, 1.0), lightness=(0.75, 1.5)), # sharpen images
                    iaa.AdditiveGaussianNoise(loc=0, scale=(0.05, 0.1*255), per_channel=0.5), # add gaussian noise to images
                    iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value)
                    iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation
                    # either change the brightness of the whole image (sometimes
                    # per channel) or change the brightness of subareas
                    iaa.OneOf([
                        iaa.Multiply((0.5, 1.5), per_channel=0.5),
                    ]),
                    iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast

                    sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), # sometimes move parts of the image around
                    sometimes(iaa.PerspectiveTransform(scale=(0.05, 0.1)))
                ],
                random_order=True
            )
        ],
        random_order=True
    )

    #grid = seq.draw_grid(image, cols=8, rows=8)
    #misc.imsave("examples_grid.jpg", grid)

    images = [image] * 10
    #TODO : convert xml to txt and read it in yolo format and replace the x, y coordinates with them.
    keypoints = [ia.Keypoint(x=34, y=15), ia.Keypoint(x=85, y=13), ia.Keypoint(x=63, y=73)]  # left ear, right ear, mouth

    keypointsList=[keypoints] * 10
    aug_det = seq.to_deterministic()
    images_aug = aug_det.augment_images(images)

    row_keypoints = []
    image_keypoints = []
    for idx in range(len(keypointsList)):
        onImageKeypoints = [ia.KeypointsOnImage(keypointsList[idx], shape = image.shape)]
        keypoints_aug = aug_det.augment_keypoints(onImageKeypoints)
        row_keypoints.append(keypoints_aug[0])

    for image, keypoints in zip(images_aug, row_keypoints):
        image_keypoints.append(keypoints.draw_on_image(image, size=5))

    for i, image_aug in enumerate(image_keypoints):
        misc.imsave("image_%05d_%06d.jpg" % (idx,i), image_aug)
from __future__ import print_function, division
import imgaug as ia
from imgaug import augmenters as iaa
from imgaug import parameters as iap
import numpy as np
from scipy import ndimage, misc
from skimage import data
import matplotlib.pyplot as plt
from matplotlib import gridspec
import six
import six.moves as sm

np.random.seed(44)
ia.seed(44)

def main():
    draw_single_sequential_images()
    draw_per_augmenter_images()

def draw_single_sequential_images():
    image = ndimage.imread("/home/ahmed/Downloads/test/brut_image/number.png")

    """
    rarely = lambda aug: iaa.Sometimes(0.1, aug)
    sometimes = lambda aug: iaa.Sometimes(0.25, aug)
    often = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential([
<<<<<<< HEAD
            iaa.Flipud(0.5),
            iaa.GaussianBlur(3.0),
            st(iaa.Sharpen(alpha=1.0, lightness=0.25)),
from class_weights_calculations import get_class_weights

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

fs = gcsfs.GCSFileSystem(project='inlaid-marker-222600')

# Any results you write to the current directory are saved as output.
BATCH_SIZE = 128
SEED = 777
SHAPE = (192, 192, 4)

DATA_DIR = ''
VAL_RATIO = 0.1
THRESHOLD = 0.05

ia.seed(SEED)

def trainDataset():
    path_to_train = DATA_DIR + 'data/'
    data = pd.read_csv(DATA_DIR +'train.csv')
    
    names_with_path = []
    labels = []
    
    for name, label in zip(data['Id'], data['Target'].str.split(' ')):
        y = np.zeros(28) # 28 is the number of the labels
        for lbl in label:
            y[int(lbl)] = 1
            
        names_with_path.append(os.path.join(path_to_train, name))
        labels.append(y)
Exemplo n.º 12
0
def reseed(seed=0):
    ia.seed(seed)
    np.random.seed(seed)
    random.seed(seed)
import imgaug as ia
from imgaug import augmenters as iaa
import numpy as np

# This seed can be changed - random seed
ia.seed(1)

# Example batch of 100 images
images = np.array(
    [ia.quokka(size=(64, 64)) for _ in range(100)],
    dtype=np.uint8
)

# Create the transformer function by specifying the different augmentations
seq = iaa.Sequential([
    # Horizontal Flips
    iaa.Fliplr(0.5), 

    # Random Crops
    iaa.Crop(percent=(0, 0.1)), 

    # Gaussian blur for 50% of the images
    iaa.Sometimes(0.5,
        iaa.GaussianBlur(sigma=(0, 0.5))
    ),
    # Strengthen or weaken the contrast in each image.
    iaa.ContrastNormalization((0.75, 1.5)),

    # Add gaussian noise.
    iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
Exemplo n.º 14
0
def draw_single_sequential_images():
    ia.seed(44)

    #image = misc.imresize(ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128))
    image = ia.quokka_square(size=(128, 128))

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(
        [
            # apply the following augmenters to most images
            iaa.Fliplr(0.5), # horizontally flip 50% of all images
            iaa.Flipud(0.2), # vertically flip 20% of all images
            # crop images by -5% to 10% of their height/width
            sometimes(iaa.CropAndPad(
                percent=(-0.05, 0.1),
                pad_mode=ia.ALL,
                pad_cval=(0, 255)
            )),
            sometimes(iaa.Affine(
                scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
                translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                rotate=(-45, 45), # rotate by -45 to +45 degrees
                shear=(-16, 16), # shear by -16 to +16 degrees
                order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
            )),
            # execute 0 to 5 of the following (less important) augmenters per image
            # don't execute all of them, as that would often be way too strong
            iaa.SomeOf((0, 5),
                [
                    sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation
                    iaa.OneOf([
                        iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0
                        iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7
                        iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7
                    ]),
                    iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images
                    iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images
                    # search either for all edges or for directed edges,
                    # blend the result with the original image using a blobby mask
                    iaa.SimplexNoiseAlpha(iaa.OneOf([
                        iaa.EdgeDetect(alpha=(0.5, 1.0)),
                        iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)),
                    ])),
                    iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images
                    iaa.OneOf([
                        iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
                        iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
                    ]),
                    iaa.Invert(0.05, per_channel=True), # invert color channels
                    iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value)
                    iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation
                    # either change the brightness of the whole image (sometimes
                    # per channel) or change the brightness of subareas
                    iaa.OneOf([
                        iaa.Multiply((0.5, 1.5), per_channel=0.5),
                        iaa.FrequencyNoiseAlpha(
                            exponent=(-4, 0),
                            first=iaa.Multiply((0.5, 1.5), per_channel=True),
                            second=iaa.ContrastNormalization((0.5, 2.0))
                        )
                    ]),
                    iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
                    iaa.Grayscale(alpha=(0.0, 1.0)),
                    sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)
                    sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), # sometimes move parts of the image around
                    sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))
                ],
                random_order=True
            )
        ],
        random_order=True
    )

    grid = seq.draw_grid(image, cols=8, rows=8)
    misc.imsave("examples_grid.jpg", grid)
import sys, os, cv2
from sklearn.utils import shuffle
from scipy.misc import imread, imresize
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder
from skimage.transform import resize
from imgaug import augmenters as iaa
import imgaug as ia
from skimage.color import rgba2rgb
from tensorflow.examples.tutorials.mnist import input_data

plt.style.use('seaborn-white')
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
np.random.seed(6278)
tf.set_random_seed(6728)
ia.seed(6278)


def tf_elu(x):
    return tf.nn.elu(x)


def d_tf_elu(x):
    return tf.cast(tf.greater(x, 0), tf.float32) + (
        tf_elu(tf.cast(tf.less_equal(x, 0), tf.float32) * x) + 1.0)


def tf_softmax(x):
    return tf.nn.softmax(x)

Exemplo n.º 16
0
def useAllAugmentations(path):
    rowsToAdd = []
    with open(path + "/dataset.csv") as csvfile:
        csvreader = csv.reader(csvfile)
        next(csvreader)
        for row in csvreader:
            new_row = row
            image_name = row[0]
            new_name = image_name.split(".")

            # Apply JPEG Compression
            jpeg_augmentor = iaa.imgcorruptlike.JpegCompression(severity=random.randrange(1,5))
            jpeg_name = new_name[0] + "_jpegCompression.jpg"
            new_row[0] = jpeg_name
            image = imageio.imread(path + "/" + image_name)
            ia.seed(1)
            image_aug = jpeg_augmentor(image = image)
            rowsToAdd.append(new_row)
            im_name = path + "/" + jpeg_name
            imageio.imwrite(im_name, image_aug[:, :, 0])

            # Apply GaussianNoise
            gaussian_augmentor = iaa.imgcorruptlike.GaussianNoise(severity=random.randrange(1,5))
            gaussian_name = new_name[0] + "_gaussian.jpg"
            new_row[0] = gaussian_name
            image = imageio.imread(path + "/" + image_name)
            ia.seed(1)
            image_aug = gaussian_augmentor(image = image)
            rowsToAdd.append(new_row)
            im_name = path + "/" + gaussian_name
            imageio.imwrite(im_name, image_aug[:, :, 0])

            # Motion blur
            mb_augmentaor = iaa.MotionBlur(k=random.randrange(4,25))
            mb_name = new_name[0] + "_motionBlur.jpeg"
            new_row = row
            new_row[0] = mb_name
            image = imageio.imread(path + "/" + image_name)
            ia.seed(1)
            image_aug = mb_augmentaor(image = image)
            rowsToAdd.append(new_row)
            im_name = path + "/" + mb_name
            imageio.imwrite(im_name, image_aug[:, :, 0])

            # Affine
            affine_augmentor = iaa.Affine(scale={"x":(0.5, 1.5), "y":(0.5, 1.5)}, rotate=(-5, 5))
            affine_name = new_name[0] + "_affine.jpeg"
            new_row = row
            new_row[0] = affine_name
            image = imageio.imread(path + "/" + image_name)
            image_aug, modified_row = updateBoundingBoxes(new_row, affine_augmentor, image)
            rowsToAdd.append(modified_row)
            im_name = path + "/" + affine_name
            imageio.imwrite(im_name, image_aug[:, :, 0])

        with open(path + "/dataset.csv", 'a') as fd:
            for row in rowsToAdd:
                stringToWrite = ""
                for entry in row:
                    stringToWrite = stringToWrite + entry + ","
                fd.write(stringToWrite)
                fd.write("\n")
Exemplo n.º 17
0
def main():
    # CMD args parser
    parser = argparse.ArgumentParser(description='Augment image datasets')
    parser.add_argument("--input",   action = "store", help = "Input images dir")
    parser.add_argument("--output",  action = "store", help = "Output images dir")
    parser.add_argument("--count",   action = "store", help = "Number of augmented sets to make", type=int, default=1)

    # Parse CMD args
    args = parser.parse_args()
    if (args.input == None or args.output == None):
        parser.print_help()
        sys.exit(1)

    ia.seed(1)
    paths = os.listdir(args.input)

    for x in range(args.count):
        seq = iaa.Sequential([
            iaa.Fliplr(0.5), # horizontal flips

            # Small gaussian blur with random sigma between 0 and 0.5.
            # But we only blur about 50% of all images.
            iaa.Sometimes(0.5,
                iaa.GaussianBlur(sigma=(0, 0.2))
            ),

            # Add gaussian noise.
            # For 50% of all images, we sample the noise once per pixel.
            # For the other 50% of all images, we sample the noise per pixel AND
            # channel. This can change the color (not only brightness) of the pixels.
            iaa.Sometimes(0.5,
                iaa.AdditiveGaussianNoise(
                    loc=0, scale=(0.0, 0.005*255), per_channel=0.5
                )
            ),

            # Make some images brighter and some darker.
            # In 20% of all cases, we sample the multiplier once per channel,
            # which can end up changing the color of the images.
            iaa.Sometimes(0.5,
                iaa.Multiply((0.8, 1.2), per_channel=0.0),
            ),

            # Apply affine transformations to each image.
            # Scale/zoom images.
            iaa.Sometimes(0.5,
                iaa.Affine(
                     rotate=(-20, 20),
                ),
            ),

            # Translate/move images.
            iaa.Sometimes(0.5,
                iaa.Affine(
                     scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
                ),
            ),

            # Rotate images.
            iaa.Sometimes(0.5,
                iaa.Affine(
                    translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)},
                ),
            ),
        ], random_order=True) # apply augmenters in random order
        
        print("Augmenting images set %d/%d"%(x+1, args.count))
        for i in tqdm(xrange(len(paths))):
            img = cv2.imread(args.input+'/'+paths[i], cv2.IMREAD_GRAYSCALE)
            img = seq.augment_image(img)
            f = os.path.splitext(paths[i])
            cv2.imwrite(args.output+'/'+f[0] + '_aug%d'%(x) + f[1], img)

    print('Finished processing all images\n')
import imgaug  #https://github.com/aleju/imgaug
from imgaug import augmenters as iaa
import imgaug as ia
ia.seed(100)
#data_size = [3150,3150]
input_size = [2048, 2048]


def train_augementors():
    shape_augs = [

        # iaa.Affine(
        #     # scale images to 80-120% of their size, individually per axis
        #     scale={"x": (0.8, 1.2),
        #            "y": (0.8, 1.2)},
        #     # translate by -A to +A percent (per axis)
        #     translate_percent={"x": (-0.01, 0.01),
        #                        "y": (-0.01, 0.01)},
        #     rotate=(-179, 179),  # rotate by -179 to +179 degrees
        #     shear=(-5, 5),  # shear by -5 to +5 degrees
        #     order=[0],  # use nearest neighbour
        #     backend='cv2'  # opencv for fast processing
        # ),
        # iaa.Fliplr(0.5),  # horizontally flip 50% of all images
        # iaa.Flipud(0.2),  # vertically flip 20% of all images
        # iaa.CropToFixedSize(input_size[0],
        #                     input_size[1],
        #                     position='center',
        #                     deterministic=True)
        # iaa.Affine(
        #     # scale images to 80-120% of their size, individually per axis
Exemplo n.º 19
0
def ImageAug(data):
    try:
        print("\n\n\n")
        Augs = data.POST.get("augs")
        numImages = int(data.POST.get("numImages"))
        print(Augs, type(Augs))
        print(numImages, type(numImages))

        # defaults
        multiplier = int(defaultSetter(data.POST.get("multiplier"), 20))
        hflip = int(defaultSetter(data.POST.get("hflip"), 0))
        vflip = int(defaultSetter(data.POST.get("vflip"), 0))
        crop = float(defaultSetter(data.POST.get("crop"), 0.05))
        blur = float(defaultSetter(data.POST.get("blur"), 0.1))
        contrast = float(defaultSetter(data.POST.get("contrast"), 0))
        scale = float(defaultSetter(data.POST.get("scale"), 0.1))
        translate = float(defaultSetter(data.POST.get("translate"), 0.1))
        rotate = float(defaultSetter(data.POST.get("rotate"), 15))
        shear = float(defaultSetter(data.POST.get("shear"), 0))

        className = int(defaultSetter(data.POST.get("class"), 1))
        if className == None:
            rtn = "Please provide class"
            print(rtn)
            return JsonResponse(rtn, safe=False)

        #storage location
        uploadedStorage = "Images/Uploaded/" + str(className) + "/"
        storageLoc_aug = "Images/Augmented/classBased/" + str(className) + "/"
        commonStorage = "Images/Augmented/mixed/"
        num_of_files_aug = len(glob.glob(storageLoc_aug + '*'))
        num_of_files_uploaded = len(glob.glob(uploadedStorage + '*'))
        nameFile = int(num_of_files_aug) + 1
        nameUploaded = int(num_of_files_uploaded) + 1

        #augmentation
        ia.seed(1)
        seq = iaa.Sequential(
            [
                iaa.Sometimes(
                    0.2,
                    iaa.Flipud(vflip),  # vertical flips
                ),
                iaa.Sometimes(
                    0.2,
                    iaa.Fliplr(hflip),  # horizontal flips
                ),
                iaa.Crop(percent=(0, crop)),  # random crops
                # Small gaussian blur with random sigma between 0 and 0.5.
                # But we only blur about 20% of all images.
                iaa.Sometimes(0.2, iaa.GaussianBlur(sigma=(0, blur))),
                # Strengthen or weaken the contrast in each image.
                iaa.LinearContrast((1 - contrast, 1 + contrast)),
                # Add gaussian noise.
                iaa.AdditiveGaussianNoise(
                    loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
                # Apply affine transformations to each image.
                # Scale/zoom them, translate/move them, rotate them and shear them.
                iaa.Affine(scale={
                    "x": (1 - scale, 1 + scale),
                    "y": (1 - scale, 1 + scale)
                },
                           translate_percent={
                               "x": (-translate, translate),
                               "y": (-translate, translate)
                           },
                           rotate=(-rotate, rotate),
                           shear=(-shear, shear))
            ],
            random_order=True)  # apply augmenters in random order

        rtn = ""
        rtn_json = []

        try:
            print(data.FILES)
            for imageName in data.FILES:
                image = data.FILES[imageName]
                print("Image : ", image)
                im = imageio.imread(image)

                imageio.imwrite(uploadedStorage + str(nameUploaded) + ".png",
                                im)
                nameUploaded += 1

                images = np.array([im for _ in range(multiplier)],
                                  dtype=np.uint8)
                images_aug = seq(images=images)
                nameFile = int(num_of_files_aug) + 1
                for j in images_aug:
                    imageio.imwrite(storageLoc_aug + str(nameFile) + ".png", j)
                    location = commonStorage + str(className) + "_" + str(
                        nameFile) + ".png"
                    imageio.imwrite(location, j)
                    rtn_json.append({str(image): location})
                    nameFile += 1
                    num_of_files_aug += 1
            rtn += "\n Successfully stored and augmented image " + imageName + "th " + str(
                multiplier) + " number of times!"
            print(rtn)
        except:
            rtn = "Error Happened!"
            print(rtn)
        return JsonResponse(rtn_json, safe=False)
    except ValueError as e:
        return Response(e.args[0], status.HTTP_400_BAD_REQUEST)
Exemplo n.º 20
0
def draw_per_augmenter_images():
    print("[draw_per_augmenter_images] Loading image...")
    #image = misc.imresize(ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128))
    image = ia.quokka_square(size=(128, 128))

    keypoints = [ia.Keypoint(x=34, y=15), ia.Keypoint(x=85, y=13), ia.Keypoint(x=63, y=73)] # left ear, right ear, mouth
    keypoints = [ia.KeypointsOnImage(keypoints, shape=image.shape)]

    print("[draw_per_augmenter_images] Initializing...")
    rows_augmenters = [
        (0, "Noop", [("", iaa.Noop()) for _ in sm.xrange(5)]),
        (0, "Crop\n(top, right,\nbottom, left)", [(str(vals), iaa.Crop(px=vals)) for vals in [(2, 0, 0, 0), (0, 8, 8, 0), (4, 0, 16, 4), (8, 0, 0, 32), (32, 64, 0, 0)]]),
        (0, "Pad\n(top, right,\nbottom, left)", [(str(vals), iaa.Pad(px=vals)) for vals in [(2, 0, 0, 0), (0, 8, 8, 0), (4, 0, 16, 4), (8, 0, 0, 32), (32, 64, 0, 0)]]),
        (0, "Fliplr", [(str(p), iaa.Fliplr(p)) for p in [0, 0, 1, 1, 1]]),
        (0, "Flipud", [(str(p), iaa.Flipud(p)) for p in [0, 0, 1, 1, 1]]),
        (0, "Superpixels\np_replace=1", [("n_segments=%d" % (n_segments,), iaa.Superpixels(p_replace=1.0, n_segments=n_segments)) for n_segments in [25, 50, 75, 100, 125]]),
        (0, "Superpixels\nn_segments=100", [("p_replace=%.2f" % (p_replace,), iaa.Superpixels(p_replace=p_replace, n_segments=100)) for p_replace in [0, 0.25, 0.5, 0.75, 1.0]]),
        (0, "Invert", [("p=%d" % (p,), iaa.Invert(p=p)) for p in [0, 0, 1, 1, 1]]),
        (0, "Invert\n(per_channel)", [("p=%.2f" % (p,), iaa.Invert(p=p, per_channel=True)) for p in [0.5, 0.5, 0.5, 0.5, 0.5]]),
        (0, "Add", [("value=%d" % (val,), iaa.Add(val)) for val in [-45, -25, 0, 25, 45]]),
        (0, "Add\n(per channel)", [("value=(%d, %d)" % (vals[0], vals[1],), iaa.Add(vals, per_channel=True)) for vals in [(-55, -35), (-35, -15), (-10, 10), (15, 35), (35, 55)]]),
        (0, "AddToHueAndSaturation", [("value=%d" % (val,), iaa.AddToHueAndSaturation(val)) for val in [-45, -25, 0, 25, 45]]),
        (0, "Multiply", [("value=%.2f" % (val,), iaa.Multiply(val)) for val in [0.25, 0.5, 1.0, 1.25, 1.5]]),
        (1, "Multiply\n(per channel)", [("value=(%.2f, %.2f)" % (vals[0], vals[1],), iaa.Multiply(vals, per_channel=True)) for vals in [(0.15, 0.35), (0.4, 0.6), (0.9, 1.1), (1.15, 1.35), (1.4, 1.6)]]),
        (0, "GaussianBlur", [("sigma=%.2f" % (sigma,), iaa.GaussianBlur(sigma=sigma)) for sigma in [0.25, 0.50, 1.0, 2.0, 4.0]]),
        (0, "AverageBlur", [("k=%d" % (k,), iaa.AverageBlur(k=k)) for k in [1, 3, 5, 7, 9]]),
        (0, "MedianBlur", [("k=%d" % (k,), iaa.MedianBlur(k=k)) for k in [1, 3, 5, 7, 9]]),
        (0, "BilateralBlur\nsigma_color=250,\nsigma_space=250", [("d=%d" % (d,), iaa.BilateralBlur(d=d, sigma_color=250, sigma_space=250)) for d in [1, 3, 5, 7, 9]]),
        (0, "Sharpen\n(alpha=1)", [("lightness=%.2f" % (lightness,), iaa.Sharpen(alpha=1, lightness=lightness)) for lightness in [0, 0.5, 1.0, 1.5, 2.0]]),
        (0, "Emboss\n(alpha=1)", [("strength=%.2f" % (strength,), iaa.Emboss(alpha=1, strength=strength)) for strength in [0, 0.5, 1.0, 1.5, 2.0]]),
        (0, "EdgeDetect", [("alpha=%.2f" % (alpha,), iaa.EdgeDetect(alpha=alpha)) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        (0, "DirectedEdgeDetect\n(alpha=1)", [("direction=%.2f" % (direction,), iaa.DirectedEdgeDetect(alpha=1, direction=direction)) for direction in [0.0, 1*(360/5)/360, 2*(360/5)/360, 3*(360/5)/360, 4*(360/5)/360]]),
        (0, "AdditiveGaussianNoise", [("scale=%.2f*255" % (scale,), iaa.AdditiveGaussianNoise(scale=scale * 255)) for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]),
        (0, "AdditiveGaussianNoise\n(per channel)", [("scale=%.2f*255" % (scale,), iaa.AdditiveGaussianNoise(scale=scale * 255, per_channel=True)) for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]),
        (0, "Dropout", [("p=%.2f" % (p,), iaa.Dropout(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        (0, "Dropout\n(per channel)", [("p=%.2f" % (p,), iaa.Dropout(p=p, per_channel=True)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        (3, "CoarseDropout\n(p=0.2)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseDropout(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        (0, "CoarseDropout\n(p=0.2, per channel)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseDropout(p=0.2, size_percent=size_percent, per_channel=True, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        (0, "SaltAndPepper", [("p=%.2f" % (p,), iaa.SaltAndPepper(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        (0, "Salt", [("p=%.2f" % (p,), iaa.Salt(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        (0, "Pepper", [("p=%.2f" % (p,), iaa.Pepper(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        (0, "CoarseSaltAndPepper\n(p=0.2)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseSaltAndPepper(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        (0, "CoarseSalt\n(p=0.2)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseSalt(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        (0, "CoarsePepper\n(p=0.2)", [("size_percent=%.2f" % (size_percent,), iaa.CoarsePepper(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        (0, "ContrastNormalization", [("alpha=%.1f" % (alpha,), iaa.ContrastNormalization(alpha=alpha)) for alpha in [0.5, 0.75, 1.0, 1.25, 1.50]]),
        (0, "ContrastNormalization\n(per channel)", [("alpha=(%.2f, %.2f)" % (alphas[0], alphas[1],), iaa.ContrastNormalization(alpha=alphas, per_channel=True)) for alphas in [(0.4, 0.6), (0.65, 0.85), (0.9, 1.1), (1.15, 1.35), (1.4, 1.6)]]),
        (0, "Grayscale", [("alpha=%.1f" % (alpha,), iaa.Grayscale(alpha=alpha)) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        (6, "PerspectiveTransform", [("scale=%.3f" % (scale,), iaa.PerspectiveTransform(scale=scale)) for scale in [0.025, 0.05, 0.075, 0.10, 0.125]]),
        (0, "PiecewiseAffine", [("scale=%.3f" % (scale,), iaa.PiecewiseAffine(scale=scale)) for scale in [0.015, 0.03, 0.045, 0.06, 0.075]]),
        (0, "Affine: Scale", [("%.1fx" % (scale,), iaa.Affine(scale=scale)) for scale in [0.1, 0.5, 1.0, 1.5, 1.9]]),
        (0, "Affine: Translate", [("x=%d y=%d" % (x, y), iaa.Affine(translate_px={"x": x, "y": y})) for x, y in [(-32, -16), (-16, -32), (-16, -8), (16, 8), (16, 32)]]),
        (0, "Affine: Rotate", [("%d deg" % (rotate,), iaa.Affine(rotate=rotate)) for rotate in [-90, -45, 0, 45, 90]]),
        (0, "Affine: Shear", [("%d deg" % (shear,), iaa.Affine(shear=shear)) for shear in [-45, -25, 0, 25, 45]]),
        (0, "Affine: Modes", [(mode, iaa.Affine(translate_px=-32, mode=mode)) for mode in ["constant", "edge", "symmetric", "reflect", "wrap"]]),
        (0, "Affine: cval", [("%d" % (int(cval*255),), iaa.Affine(translate_px=-32, cval=int(cval*255), mode="constant")) for cval in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        (
            2, "Affine: all", [
                (
                    "",
                    iaa.Affine(
                        scale={"x": (0.5, 1.5), "y": (0.5, 1.5)},
                        translate_px={"x": (-32, 32), "y": (-32, 32)},
                        rotate=(-45, 45),
                        shear=(-32, 32),
                        mode=ia.ALL,
                        cval=(0.0, 1.0)
                    )
                )
                for _ in sm.xrange(5)
            ]
        ),
        (1, "ElasticTransformation\n(sigma=0.2)", [("alpha=%.1f" % (alpha,), iaa.ElasticTransformation(alpha=alpha, sigma=0.2)) for alpha in [0.1, 0.5, 1.0, 3.0, 9.0]]),
        (0, "Alpha\nwith EdgeDetect(1.0)", [("factor=%.1f" % (factor,), iaa.Alpha(factor=factor, first=iaa.EdgeDetect(1.0))) for factor in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        (4, "Alpha\nwith EdgeDetect(1.0)\n(per channel)", [("factor=(%.2f, %.2f)" % (factor[0], factor[1]), iaa.Alpha(factor=factor, first=iaa.EdgeDetect(1.0), per_channel=0.5)) for factor in [(0.0, 0.2), (0.15, 0.35), (0.4, 0.6), (0.65, 0.85), (0.8, 1.0)]]),
        (15, "SimplexNoiseAlpha\nwith EdgeDetect(1.0)", [("", iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0))) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        (9, "FrequencyNoiseAlpha\nwith EdgeDetect(1.0)", [("exponent=%.1f" % (exponent,), iaa.FrequencyNoiseAlpha(exponent=exponent, first=iaa.EdgeDetect(1.0), size_px_max=16, upscale_method="linear", sigmoid=False)) for exponent in [-4, -2, 0, 2, 4]])
    ]

    print("[draw_per_augmenter_images] Augmenting...")
    rows = []
    for (row_seed, row_name, augmenters) in rows_augmenters:
        ia.seed(row_seed)
        #for img_title, augmenter in augmenters:
        #    #aug.reseed(1000)
        #    pass

        row_images = []
        row_keypoints = []
        row_titles = []
        for img_title, augmenter in augmenters:
            aug_det = augmenter.to_deterministic()
            row_images.append(aug_det.augment_image(image))
            row_keypoints.append(aug_det.augment_keypoints(keypoints)[0])
            row_titles.append(img_title)
        rows.append((row_name, row_images, row_keypoints, row_titles))

    # matplotlib drawin routine
    """
    print("[draw_per_augmenter_images] Plotting...")
    width = 8
    height = int(1.5 * len(rows_augmenters))
    fig = plt.figure(figsize=(width, height))
    grid_rows = len(rows)
    grid_cols = 1 + 5
    gs = gridspec.GridSpec(grid_rows, grid_cols, width_ratios=[2, 1, 1, 1, 1, 1])
    axes = []
    for i in sm.xrange(grid_rows):
        axes.append([plt.subplot(gs[i, col_idx]) for col_idx in sm.xrange(grid_cols)])
    fig.tight_layout()
    #fig.subplots_adjust(bottom=0.2 / grid_rows, hspace=0.22)
    #fig.subplots_adjust(wspace=0.005, hspace=0.425, bottom=0.02)
    fig.subplots_adjust(wspace=0.005, hspace=0.005, bottom=0.02)

    for row_idx, (row_name, row_images, row_keypoints, row_titles) in enumerate(rows):
        axes_row = axes[row_idx]

        for col_idx in sm.xrange(grid_cols):
            ax = axes_row[col_idx]

            ax.cla()
            ax.axis("off")
            ax.get_xaxis().set_visible(False)
            ax.get_yaxis().set_visible(False)

            if col_idx == 0:
                ax.text(0, 0.5, row_name, color="black")
            else:
                cell_image = row_images[col_idx-1]
                cell_keypoints = row_keypoints[col_idx-1]
                cell_image_kp = cell_keypoints.draw_on_image(cell_image, size=5)
                ax.imshow(cell_image_kp)
                x = 0
                y = 145
                #ax.text(x, y, row_titles[col_idx-1], color="black", backgroundcolor="white", fontsize=6)
                ax.text(x, y, row_titles[col_idx-1], color="black", fontsize=7)


    fig.savefig("examples.jpg", bbox_inches="tight")
    #plt.show()
    """

    # simpler and faster drawing routine
    """
    output_image = ExamplesImage(128, 128, 128+64, 32)
    for (row_name, row_images, row_keypoints, row_titles) in rows:
        row_images_kps = []
        for image, keypoints in zip(row_images, row_keypoints):
            row_images_kps.append(keypoints.draw_on_image(image, size=5))
        output_image.add_row(row_name, row_images_kps, row_titles)
    misc.imsave("examples.jpg", output_image.draw())
    """

    # routine to draw many single files
    seen = defaultdict(lambda: 0)
    markups = []
    for (row_name, row_images, row_keypoints, row_titles) in rows:
        output_image = ExamplesImage(128, 128, 128+64, 32)
        row_images_kps = []
        for image, keypoints in zip(row_images, row_keypoints):
            row_images_kps.append(keypoints.draw_on_image(image, size=5))
        output_image.add_row(row_name, row_images_kps, row_titles)
        if "\n" in row_name:
            row_name_clean = row_name[0:row_name.find("\n")+1]
        else:
            row_name_clean = row_name
        row_name_clean = re.sub(r"[^a-z0-9]+", "_", row_name_clean.lower())
        row_name_clean = row_name_clean.strip("_")
        if seen[row_name_clean] > 0:
            row_name_clean = "%s_%d" % (row_name_clean, seen[row_name_clean] + 1)
        fp = os.path.join(IMAGES_DIR, "examples_%s.jpg" % (row_name_clean,))
        #misc.imsave(fp, output_image.draw())
        save(fp, output_image.draw())
        seen[row_name_clean] += 1

        markup_descr = row_name.replace('"', '') \
                               .replace("\n", " ") \
                               .replace("(", "") \
                               .replace(")", "")
        markup = '![%s](%s?raw=true "%s")' % (markup_descr, fp, markup_descr)
        markups.append(markup)

    for markup in markups:
        print(markup)
Exemplo n.º 21
0
def make_EliceiriP2P_train(src_dir, target_dir):
    '''
    Create folder `/path/to/data` with subfolders `A` and `B`. 
    `A` and `B` should each have their own subfolders `train`, `val`, `test`, etc. 
    In `/path/to/data/A/train`, put training images in style A. 
    In `/path/to/data/B/train`, put the corresponding images in style B. 
    '''
    #    src_dir='./Datasets/HighRes_Splits'
    #    target_dir='./Datasets/Eliceiri_temp'
    #    is_test=False

    data_folder = 'CNN_Training_Crops'

    dset = SlideDataset([
        f'{src_dir}/{data_folder}/*_SHG.tif',
        f'{src_dir}/{data_folder}/*_BF.tif'
    ],
                        transform=ImgAugTransform())
    #    dset_test = ZurichDataset([], transform=ImgAugTransform(testing=is_test))

    for modality in ['A', 'B']:
        for folder in ['train', 'val', 'test']:
            if not os.path.exists(f'{target_dir}/{modality}/{folder}'):
                os.makedirs(f'{target_dir}/{modality}/{folder}')

    epochs = 10
    batch_size = 16
    steps_per_epoch = 32
    samples_per_epoch = steps_per_epoch * batch_size

    # DATASET RELATED
    def worker_init_fn(worker_id):
        base_seed = int(torch.randint(2**32, (1, )).item())
        lib_seed = (base_seed + worker_id) % (2**32)
        imgaug.seed(lib_seed)
        np.random.seed(lib_seed)

    dataloader_args = {
        "batch_size": batch_size,
        "shuffle": False,
        "num_workers": 0,
        "pin_memory": True,
        "worker_init_fn": worker_init_fn,
    }

    loader = torch.utils.data.DataLoader(dset,
                                         sampler=OverSampler(
                                             dset, samples_per_epoch),
                                         **dataloader_args)

    for epoch in tqdm(range(epochs)):
        for batch_idx, data in enumerate(loader):  # 32 batches
            imgaug.seed(batch_idx)
            ABs = data.numpy()  # data.shape = (16, 128, 128, 4)
            ABs = cv2.normalize(src=ABs,
                                dst=None,
                                alpha=0,
                                beta=255,
                                norm_type=cv2.NORM_MINMAX,
                                dtype=cv2.CV_8U)
            As = ABs[..., 0]
            Bs = ABs[..., 1:]
            for j in range(batch_size):
                skio.imsave(
                    f'{target_dir}/A/train/e{epoch}_b{batch_idx}_{j}.tiff',
                    As[j])
                skio.imsave(
                    f'{target_dir}/B/train/e{epoch}_b{batch_idx}_{j}.tiff',
                    Bs[j])
    return
Exemplo n.º 22
0
warnings.filterwarnings("ignore")
SIZE = 256
NUM_CLASSES = 5

#%% [markdown]
# ### Setting the seed

#%%
os.environ['PYTHONHASHSEED'] = '0'
np.random.seed(444)
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1,
                              inter_op_parallelism_threads=1)
tf.set_random_seed(444)
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)
aug.seed(444)

#%%
df_train = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')
df_test = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')

#%%
x = df_train['id_code']
y = df_train['diagnosis']

x, y = shuffle(x, y, random_state=8)
y.hist()

#%%
y = to_categorical(y, num_classes=NUM_CLASSES)
train_x, valid_x, train_y, valid_y = train_test_split(x,
Exemplo n.º 23
0
 def worker_init_fn(worker_id):
     base_seed = int(torch.randint(2**32, (1, )).item())
     lib_seed = (base_seed + worker_id) % (2**32)
     imgaug.seed(lib_seed)
     np.random.seed(lib_seed)
Exemplo n.º 24
0
    def __init__(self,
                 X,
                 Y,
                 random_subvolumes_in_DA=False,
                 subvol_shape=None,
                 seed=42,
                 shuffle_each_epoch=False,
                 batch_size=32,
                 da=True,
                 da_prob=0.5,
                 rotation90=False,
                 rand_rot=False,
                 rnd_rot_range=(-180, 180),
                 shear=False,
                 shear_range=(-20, 20),
                 zoom=False,
                 zoom_range=(0.8, 1.2),
                 shift=False,
                 shift_range=(0.1, 0.2),
                 flip=False,
                 elastic=False,
                 e_alpha=(240, 250),
                 e_sigma=25,
                 e_mode='constant',
                 g_blur=False,
                 g_sigma=(1.0, 2.0),
                 median_blur=False,
                 mb_kernel=(3, 7),
                 motion_blur=False,
                 motb_k_range=(3, 8),
                 gamma_contrast=False,
                 gc_gamma=(1.25, 1.75),
                 dropout=False,
                 drop_range=(0, 0.2),
                 cutout=False,
                 cout_nb_iterations=(1, 3),
                 cout_size=0.2,
                 cout_fill_mode='constant',
                 cutblur=False,
                 cblur_size=0.4,
                 cblur_down_range=(2, 8),
                 cblur_inside=True,
                 cutmix=False,
                 cmix_size=0.4,
                 n_classes=1,
                 out_number=1,
                 val=False,
                 prob_map=None,
                 extra_data_factor=1):
        """ImageDataGenerator constructor. Based on transformations from 
           `imgaug <https://github.com/aleju/imgaug>`_ library. Here a brief
           description of each transformation parameter is made. Find a complete
           explanation of the library `documentation <https://imgaug.readthedocs.io/en/latest/index.html>`_. 
           
                                                                                
           Parameters
           ----------
           X : Numpy 5D array
               Data. E.g. ``(num_of_images, x, y, z, channels)``.

           Y : Numpy 5D array
               Mask data. E.g. ``(num_of_images, x, y, z, channels)``.

           random_subvolumes_in_DA : bool, optional
               To extract random subvolumes from the given data. If not, the 
               data must be 5D and is assumed that the subvolumes are prepared. 
    
           subvol_shape : 4D tuple of ints, optional
               Shape of the subvolume to be extracted randomly from the data. 
               E. g. ``(x, y, z, channels)``.
            
           seed : int, optional
               Seed for random functions.
                
           shuffle_each_epoch : bool, optional
               To shuffle data after each epoch.

           batch_size : int, optional
               Size of the batches.
            
           da : bool, optional
               To activate the data augmentation.
            
           da_prob : float, optional
               Probability of doing each transformation.
            
           rotation90 : bool, optional                                          
               To make square (90, 180,270) degree rotations.
        
           rand_rot : bool, optional                                            
               To make random degree range rotations.                  
           
           rnd_rot_range : tuple of float, optional
               Range of random rotations. E. g. ``(-180, 180)``.

           shear : bool, optional
               To make shear transformations. 

           shear_range : tuple of int, optional
               Degree range to make shear. E. g. ``(-20, 20)``. 

           zoom : bool, optional
               To make zoom on images.
        
           zoom_range : tuple of floats, optional
               Zoom range to apply. E. g. ``(0.8, 1.2)``. 
            
           shift : float, optional 
               To make shifts.
         
           shift_range : tuple of float, optional
               Range to make a shift. E. g. ``(0.1, 0.2)``.

           flip : bool, optional
               To activate flips (both horizontal and vertical).
        
           elastic : bool, optional
               To make elastic deformations.

           e_alpha : tuple of ints, optional
                Strength of the distortion field. E. g. ``(240, 250)``.
               
           e_sigma : int, optional
               Standard deviation of the gaussian kernel used to smooth the 
               distortion fields. 

           e_mode : str, optional
               Parameter that defines the handling of newly created pixels with 
               the elastic transformation. 
            
           g_blur : bool, optional
               To insert gaussian blur on the images.
        
           g_sigma : tuple of floats, optional
               Standard deviation of the gaussian kernel. E. g. ``(1.0, 2.0)``.

           median_blur : bool, optional                                      
               To blur an image by computing median values over neighbourhoods.
                                                                                
           mb_kernel : tuple of ints, optional                                  
               Median blur kernel size. E. g. ``(3, 7)``.                                   

           motion_blur : bool, optional
               Blur images in a way that fakes camera or object movements.

           motb_k_range : int, optional
               Kernel size to use in motion blur. 
           
           gamma_contrast : bool, optional
               To insert gamma constrast changes on images. 

           gc_gamma : tuple of floats, optional                                  
               Exponent for the contrast adjustment. Higher values darken the 
               image. E. g. ``(1.25, 1.75)``. 

           dropout : bool, optional
               To set a certain fraction of pixels in images to zero.

           drop_range : tuple of floats, optional
               Range to take a probability ``p`` to drop pixels. E.g. ``(0, 0.2)``
               will take a ``p`` folowing ``0<=p<=0.2`` and then drop ``p``
               percent of all pixels in the image (i.e. convert them to black
               pixels).

           cutout : bool, optional                                      
               To fill one or more rectangular areas in an image using a fill 
               mode.

           cout_nb_iterations : tuple of ints, optional
               Range of number of areas to fill the image with. E. g. ``(1, 3)``. 

           cout_size : float, optional                         
               Size of the areas in % of the corresponding image size. Value 
               between ``0`` and ``1``.

           cout_fill_mode : str, optional                                      
               Parameter that defines the handling of newly created pixels with
               cutout.

           cutblur : boolean, optional
               Blur a rectangular area of the image by downsampling and upsampling
               it again. 

           cblur_size : float, optional
               Size of the area to apply cutblur on.
        
           cblur_inside : boolean, optional
               If ``True`` only the region inside will be modified (cut LR into HR
               image). If ``False`` the ``50%`` of the times the region inside will
               be modified (cut LR into HR image) and the other ``50%`` the inverse
               will be done (cut HR into LR image). See Figure 1 of the official
               `paper <https://arxiv.org/pdf/2004.00448.pdf>`_.

           cutmix : boolean, optional
               Combine two images pasting a region of one image to another.

           cmix_size : float, optional
               Size of the area to paste one image into another. 

           n_classes : int, optional
               Number of classes. If ``> 1`` one-hot encoding will be done on 
               the ground truth.

           out_number : int, optional                                               
               Number of output returned by the network. Used to produce same 
               number of ground truth data on each batch. 

           val : bool, optional
               Advice the generator that the volumes will be used to validate
               the model to not make random crops (as the validation data must
               be the same on each epoch). Valid when ``random_subvolumes_in_DA`` 
               is set.

           prob_map : 5D Numpy array, optional
               Probability map used to make random crops when
               ``random_subvolumes_in_DA`` is set.
            
           extra_data_factor : int, optional
               Factor to multiply the batches yielded in a epoch. It acts as if
               ``X`` and ``Y``` where concatenated ``extra_data_factor`` times.
        """

        if X.ndim != 5 or Y.ndim != 5:
            raise ValueError("X and Y must be a 5D Numpy array")
        if X.shape[:4] != Y.shape[:4]:
            raise ValueError(
                "The shape of X and Y must be the same. {} != {}".format(
                    X.shape[:4], Y.shape[:4]))
        if random_subvolumes_in_DA:
            if subvol_shape is None:
                raise ValueError("'subvol_shape' must be provided when "
                                 "'random_subvolumes_in_DA is enabled")
            if subvol_shape[0] > X.shape[1] or subvol_shape[1] > X.shape[2] or \
               subvol_shape[2] > X.shape[3]:
                raise ValueError(
                    "Given 'subvol_shape' is bigger than the data "
                    "provided")

        if rotation90 and rand_rot:
            print(
                "Warning: you selected double rotation type. Maybe you should"
                " set only 'rand_rot'?")

        self.X = (X / 255).astype(np.float32) if np.max(X) > 100 else X.astype(
            np.float32)
        self.X_c = self.X.shape[-1]
        self.X_z = self.X.shape[-2]
        self.Y = (Y / 255).astype(np.uint8) if np.max(Y) > 100 else Y.astype(
            np.uint8)
        self.Y_c = self.Y.shape[-1]
        self.Y_z = self.Y.shape[-2]
        self.n_classes = n_classes
        self.out_number = out_number
        self.channels = Y.shape[-1]
        self.random_subvolumes_in_DA = random_subvolumes_in_DA
        self.seed = seed
        self.shuffle_each_epoch = shuffle_each_epoch
        self.da = da
        self.da_prob = da_prob
        self.flip = flip
        self.cutblur = cutblur
        self.cblur_size = cblur_size
        self.cblur_down_range = cblur_down_range
        self.cblur_inside = cblur_inside
        self.cutmix = cutmix
        self.cmix_size = cmix_size
        self.val = val
        self.batch_size = batch_size
        self.o_indexes = np.arange(len(self.X))
        if extra_data_factor > 1:
            self.extra_data_factor = extra_data_factor
            self.o_indexes = np.concatenate([self.o_indexes] *
                                            extra_data_factor)
        else:
            self.extra_data_factor = 1
        self.prob_map = prob_map
        if random_subvolumes_in_DA:
            self.shape = subvol_shape
        else:
            self.shape = X.shape[1:]
        self.total_batches_seen = 0

        self.da_options = []
        self.trans_made = ''
        if rotation90:
            self.da_options.append(iaa.Sometimes(da_prob, iaa.Rot90((1, 3))))
            self.trans_made += '_rot[90,180,270]'
        if rand_rot:
            self.da_options.append(
                iaa.Sometimes(da_prob, iaa.Affine(rotate=rnd_rot_range)))
            self.trans_made += '_rrot' + str(rnd_rot_range)
        if shear:
            self.da_options.append(
                iaa.Sometimes(da_prob, iaa.Affine(rotate=shear_range)))
            self.trans_made += '_shear' + str(shear_range)
        if zoom:
            self.da_options.append(
                iaa.Sometimes(
                    da_prob,
                    iaa.Affine(scale={
                        "x": zoom_range,
                        "y": zoom_range
                    })))
            self.trans_made += '_zoom' + str(zoom_range)
        if shift:
            self.da_options.append(
                iaa.Sometimes(da_prob,
                              iaa.Affine(translate_percent=shift_range)))
            self.trans_made += '_shift' + str(shift_range)
        if flip:
            self.da_options.append(iaa.Flipud(0.5))
            self.da_options.append(iaa.Fliplr(0.5))
            self.trans_made += '_flip'
        if elastic:
            self.da_options.append(
                iaa.Sometimes(
                    da_prob,
                    iaa.ElasticTransformation(alpha=e_alpha,
                                              sigma=e_sigma,
                                              mode=e_mode)))
            self.trans_made += '_elastic' + str(e_alpha) + '+' + str(
                e_sigma) + '+' + str(e_mode)
        if g_blur:
            self.da_options.append(
                iaa.Sometimes(da_prob, iaa.GaussianBlur(g_sigma)))
            self.trans_made += '_gblur' + str(g_sigma)
        if median_blur:
            self.da_options.append(
                iaa.Sometimes(da_prob, iaa.MedianBlur(k=mb_kernel)))
            self.trans_made += '_mblur' + str(mb_kernel)
        if motion_blur:
            self.da_options.append(
                iaa.Sometimes(da_prob, iaa.MotionBlur(k=motb_k_range)))
            self.trans_made += '_motb' + str(motb_k_range)
        if gamma_contrast:
            self.da_options.append(
                iaa.Sometimes(da_prob, iaa.GammaContrast(gc_gamma)))
            self.trans_made += '_gcontrast' + str(gc_gamma)
        if dropout:
            self.da_options.append(
                iaa.Sometimes(da_prob, iaa.Dropout(p=drop_range)))
            self.trans_made += '_drop' + str(drop_range)
        if cutout:
            self.da_options.append(
                iaa.Sometimes(
                    da_prob,
                    iaa.Cutout(nb_iterations=cout_nb_iterations,
                               size=cout_size,
                               fill_mode=cout_fill_mode,
                               squared=False)))
            self.trans_made += '_cout' + str(cout_nb_iterations) + '+' + str(
                cout_size) + '+' + str(cout_fill_mode)
        if cutblur:
            self.trans_made += '_cblur' + str(cblur_size) + '+' + str(
                cblur_down_range) + '+' + str(cblur_inside)
        if cutmix: self.trans_made += '_cmix' + str(cmix_size)

        self.trans_made = self.trans_made.replace(" ", "")
        self.seq = iaa.Sequential(self.da_options)
        ia.seed(seed)
        self.on_epoch_end()
# encoding:utf-8
import os, sys, numpy as np, random, time, cv2
import torch

# import jpeg4py as jpeg
from PIL import Image
import torch.utils.data as data
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
import imgaug as ia
from imgaug import augmenters as iaa
from glob import glob

from random import randint
from tqdm import tqdm
ia.seed(random.randint(1, 10000))


class ClassifyDataset(data.Dataset):
    def __init__(self,
                 base_data_path,
                 train,
                 transform,
                 id_name_path,
                 device,
                 little_train=False,
                 read_mode='jpeg4py',
                 input_size=224,
                 C=2048,
                 test_mode=False):
        print('data init')
Exemplo n.º 26
0
def draw_per_augmenter_images():
    print("[draw_per_augmenter_images] Loading image...")
    #image = misc.imresize(ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128))
    image = ia.quokka_square(size=(128, 128))

    keypoints = [ia.Keypoint(x=34, y=15), ia.Keypoint(x=85, y=13), ia.Keypoint(x=63, y=73)] # left ear, right ear, mouth
    keypoints = [ia.KeypointsOnImage(keypoints, shape=image.shape)]

    print("[draw_per_augmenter_images] Initializing...")
    rows_augmenters = [
        (0, "Noop", [("", iaa.Noop()) for _ in sm.xrange(5)]),
        (0, "Crop\n(top, right,\nbottom, left)", [(str(vals), iaa.Crop(px=vals)) for vals in [(2, 0, 0, 0), (0, 8, 8, 0), (4, 0, 16, 4), (8, 0, 0, 32), (32, 64, 0, 0)]]),
        (0, "Pad\n(top, right,\nbottom, left)", [(str(vals), iaa.Pad(px=vals)) for vals in [(2, 0, 0, 0), (0, 8, 8, 0), (4, 0, 16, 4), (8, 0, 0, 32), (32, 64, 0, 0)]]),
        (0, "Fliplr", [(str(p), iaa.Fliplr(p)) for p in [0, 0, 1, 1, 1]]),
        (0, "Flipud", [(str(p), iaa.Flipud(p)) for p in [0, 0, 1, 1, 1]]),
        (0, "Superpixels\np_replace=1", [("n_segments=%d" % (n_segments,), iaa.Superpixels(p_replace=1.0, n_segments=n_segments)) for n_segments in [25, 50, 75, 100, 125]]),
        (0, "Superpixels\nn_segments=100", [("p_replace=%.2f" % (p_replace,), iaa.Superpixels(p_replace=p_replace, n_segments=100)) for p_replace in [0, 0.25, 0.5, 0.75, 1.0]]),
        (0, "Invert", [("p=%d" % (p,), iaa.Invert(p=p)) for p in [0, 0, 1, 1, 1]]),
        (0, "Invert\n(per_channel)", [("p=%.2f" % (p,), iaa.Invert(p=p, per_channel=True)) for p in [0.5, 0.5, 0.5, 0.5, 0.5]]),
        (0, "Add", [("value=%d" % (val,), iaa.Add(val)) for val in [-45, -25, 0, 25, 45]]),
        (0, "Add\n(per channel)", [("value=(%d, %d)" % (vals[0], vals[1],), iaa.Add(vals, per_channel=True)) for vals in [(-55, -35), (-35, -15), (-10, 10), (15, 35), (35, 55)]]),
        (0, "AddToHueAndSaturation", [("value=%d" % (val,), iaa.AddToHueAndSaturation(val)) for val in [-45, -25, 0, 25, 45]]),
        (0, "Multiply", [("value=%.2f" % (val,), iaa.Multiply(val)) for val in [0.25, 0.5, 1.0, 1.25, 1.5]]),
        (1, "Multiply\n(per channel)", [("value=(%.2f, %.2f)" % (vals[0], vals[1],), iaa.Multiply(vals, per_channel=True)) for vals in [(0.15, 0.35), (0.4, 0.6), (0.9, 1.1), (1.15, 1.35), (1.4, 1.6)]]),
        (0, "GaussianBlur", [("sigma=%.2f" % (sigma,), iaa.GaussianBlur(sigma=sigma)) for sigma in [0.25, 0.50, 1.0, 2.0, 4.0]]),
        (0, "AverageBlur", [("k=%d" % (k,), iaa.AverageBlur(k=k)) for k in [1, 3, 5, 7, 9]]),
        (0, "MedianBlur", [("k=%d" % (k,), iaa.MedianBlur(k=k)) for k in [1, 3, 5, 7, 9]]),
        (0, "BilateralBlur\nsigma_color=250,\nsigma_space=250", [("d=%d" % (d,), iaa.BilateralBlur(d=d, sigma_color=250, sigma_space=250)) for d in [1, 3, 5, 7, 9]]),
        (0, "Sharpen\n(alpha=1)", [("lightness=%.2f" % (lightness,), iaa.Sharpen(alpha=1, lightness=lightness)) for lightness in [0, 0.5, 1.0, 1.5, 2.0]]),
        (0, "Emboss\n(alpha=1)", [("strength=%.2f" % (strength,), iaa.Emboss(alpha=1, strength=strength)) for strength in [0, 0.5, 1.0, 1.5, 2.0]]),
        (0, "EdgeDetect", [("alpha=%.2f" % (alpha,), iaa.EdgeDetect(alpha=alpha)) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        (0, "DirectedEdgeDetect\n(alpha=1)", [("direction=%.2f" % (direction,), iaa.DirectedEdgeDetect(alpha=1, direction=direction)) for direction in [0.0, 1*(360/5)/360, 2*(360/5)/360, 3*(360/5)/360, 4*(360/5)/360]]),
        (0, "AdditiveGaussianNoise", [("scale=%.2f*255" % (scale,), iaa.AdditiveGaussianNoise(scale=scale * 255)) for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]),
        (0, "AdditiveGaussianNoise\n(per channel)", [("scale=%.2f*255" % (scale,), iaa.AdditiveGaussianNoise(scale=scale * 255, per_channel=True)) for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]),
        (0, "Dropout", [("p=%.2f" % (p,), iaa.Dropout(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        (0, "Dropout\n(per channel)", [("p=%.2f" % (p,), iaa.Dropout(p=p, per_channel=True)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        (3, "CoarseDropout\n(p=0.2)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseDropout(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        (0, "CoarseDropout\n(p=0.2, per channel)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseDropout(p=0.2, size_percent=size_percent, per_channel=True, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        (0, "SaltAndPepper", [("p=%.2f" % (p,), iaa.SaltAndPepper(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        (0, "Salt", [("p=%.2f" % (p,), iaa.Salt(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        (0, "Pepper", [("p=%.2f" % (p,), iaa.Pepper(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        (0, "CoarseSaltAndPepper\n(p=0.2)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseSaltAndPepper(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        (0, "CoarseSalt\n(p=0.2)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseSalt(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        (0, "CoarsePepper\n(p=0.2)", [("size_percent=%.2f" % (size_percent,), iaa.CoarsePepper(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        (0, "ContrastNormalization", [("alpha=%.1f" % (alpha,), iaa.ContrastNormalization(alpha=alpha)) for alpha in [0.5, 0.75, 1.0, 1.25, 1.50]]),
        (0, "ContrastNormalization\n(per channel)", [("alpha=(%.2f, %.2f)" % (alphas[0], alphas[1],), iaa.ContrastNormalization(alpha=alphas, per_channel=True)) for alphas in [(0.4, 0.6), (0.65, 0.85), (0.9, 1.1), (1.15, 1.35), (1.4, 1.6)]]),
        (0, "Grayscale", [("alpha=%.1f" % (alpha,), iaa.Grayscale(alpha=alpha)) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        (6, "PerspectiveTransform", [("scale=%.3f" % (scale,), iaa.PerspectiveTransform(scale=scale)) for scale in [0.025, 0.05, 0.075, 0.10, 0.125]]),
        (0, "PiecewiseAffine", [("scale=%.3f" % (scale,), iaa.PiecewiseAffine(scale=scale)) for scale in [0.015, 0.03, 0.045, 0.06, 0.075]]),
        (0, "Affine: Scale", [("%.1fx" % (scale,), iaa.Affine(scale=scale)) for scale in [0.1, 0.5, 1.0, 1.5, 1.9]]),
        (0, "Affine: Translate", [("x=%d y=%d" % (x, y), iaa.Affine(translate_px={"x": x, "y": y})) for x, y in [(-32, -16), (-16, -32), (-16, -8), (16, 8), (16, 32)]]),
        (0, "Affine: Rotate", [("%d deg" % (rotate,), iaa.Affine(rotate=rotate)) for rotate in [-90, -45, 0, 45, 90]]),
        (0, "Affine: Shear", [("%d deg" % (shear,), iaa.Affine(shear=shear)) for shear in [-45, -25, 0, 25, 45]]),
        (0, "Affine: Modes", [(mode, iaa.Affine(translate_px=-32, mode=mode)) for mode in ["constant", "edge", "symmetric", "reflect", "wrap"]]),
        (0, "Affine: cval", [("%d" % (int(cval*255),), iaa.Affine(translate_px=-32, cval=int(cval*255), mode="constant")) for cval in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        (
            2, "Affine: all", [
                (
                    "",
                    iaa.Affine(
                        scale={"x": (0.5, 1.5), "y": (0.5, 1.5)},
                        translate_px={"x": (-32, 32), "y": (-32, 32)},
                        rotate=(-45, 45),
                        shear=(-32, 32),
                        mode=ia.ALL,
                        cval=(0.0, 1.0)
                    )
                )
                for _ in sm.xrange(5)
            ]
        ),
        (1, "ElasticTransformation\n(sigma=0.2)", [("alpha=%.1f" % (alpha,), iaa.ElasticTransformation(alpha=alpha, sigma=0.2)) for alpha in [0.1, 0.5, 1.0, 3.0, 9.0]]),
        (0, "Alpha\nwith EdgeDetect(1.0)", [("factor=%.1f" % (factor,), iaa.Alpha(factor=factor, first=iaa.EdgeDetect(1.0))) for factor in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        (4, "Alpha\nwith EdgeDetect(1.0)\n(per channel)", [("factor=(%.2f, %.2f)" % (factor[0], factor[1]), iaa.Alpha(factor=factor, first=iaa.EdgeDetect(1.0), per_channel=0.5)) for factor in [(0.0, 0.2), (0.15, 0.35), (0.4, 0.6), (0.65, 0.85), (0.8, 1.0)]]),
        (15, "SimplexNoiseAlpha\nwith EdgeDetect(1.0)", [("", iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0))) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        (9, "FrequencyNoiseAlpha\nwith EdgeDetect(1.0)", [("exponent=%.1f" % (exponent,), iaa.FrequencyNoiseAlpha(exponent=exponent, first=iaa.EdgeDetect(1.0), size_px_max=16, upscale_method="linear", sigmoid=False)) for exponent in [-4, -2, 0, 2, 4]])
    ]

    print("[draw_per_augmenter_images] Augmenting...")
    rows = []
    for (row_seed, row_name, augmenters) in rows_augmenters:
        ia.seed(row_seed)
        #for img_title, augmenter in augmenters:
        #    #aug.reseed(1000)
        #    pass

        row_images = []
        row_keypoints = []
        row_titles = []
        for img_title, augmenter in augmenters:
            aug_det = augmenter.to_deterministic()
            row_images.append(aug_det.augment_image(image))
            row_keypoints.append(aug_det.augment_keypoints(keypoints)[0])
            row_titles.append(img_title)
        rows.append((row_name, row_images, row_keypoints, row_titles))

    # matplotlib drawin routine
    """
    print("[draw_per_augmenter_images] Plotting...")
    width = 8
    height = int(1.5 * len(rows_augmenters))
    fig = plt.figure(figsize=(width, height))
    grid_rows = len(rows)
    grid_cols = 1 + 5
    gs = gridspec.GridSpec(grid_rows, grid_cols, width_ratios=[2, 1, 1, 1, 1, 1])
    axes = []
    for i in sm.xrange(grid_rows):
        axes.append([plt.subplot(gs[i, col_idx]) for col_idx in sm.xrange(grid_cols)])
    fig.tight_layout()
    #fig.subplots_adjust(bottom=0.2 / grid_rows, hspace=0.22)
    #fig.subplots_adjust(wspace=0.005, hspace=0.425, bottom=0.02)
    fig.subplots_adjust(wspace=0.005, hspace=0.005, bottom=0.02)

    for row_idx, (row_name, row_images, row_keypoints, row_titles) in enumerate(rows):
        axes_row = axes[row_idx]

        for col_idx in sm.xrange(grid_cols):
            ax = axes_row[col_idx]

            ax.cla()
            ax.axis("off")
            ax.get_xaxis().set_visible(False)
            ax.get_yaxis().set_visible(False)

            if col_idx == 0:
                ax.text(0, 0.5, row_name, color="black")
            else:
                cell_image = row_images[col_idx-1]
                cell_keypoints = row_keypoints[col_idx-1]
                cell_image_kp = cell_keypoints.draw_on_image(cell_image, size=5)
                ax.imshow(cell_image_kp)
                x = 0
                y = 145
                #ax.text(x, y, row_titles[col_idx-1], color="black", backgroundcolor="white", fontsize=6)
                ax.text(x, y, row_titles[col_idx-1], color="black", fontsize=7)


    fig.savefig("examples.jpg", bbox_inches="tight")
    #plt.show()
    """

    # simpler and faster drawing routine
    """
    output_image = ExamplesImage(128, 128, 128+64, 32)
    for (row_name, row_images, row_keypoints, row_titles) in rows:
        row_images_kps = []
        for image, keypoints in zip(row_images, row_keypoints):
            row_images_kps.append(keypoints.draw_on_image(image, size=5))
        output_image.add_row(row_name, row_images_kps, row_titles)
    misc.imsave("examples.jpg", output_image.draw())
    """

    # routine to draw many single files
    seen = defaultdict(lambda: 0)
    markups = []
    for (row_name, row_images, row_keypoints, row_titles) in rows:
        output_image = ExamplesImage(128, 128, 128+64, 32)
        row_images_kps = []
        for image, keypoints in zip(row_images, row_keypoints):
            row_images_kps.append(keypoints.draw_on_image(image, size=5))
        output_image.add_row(row_name, row_images_kps, row_titles)
        if "\n" in row_name:
            row_name_clean = row_name[0:row_name.find("\n")+1]
        else:
            row_name_clean = row_name
        row_name_clean = re.sub(r"[^a-z0-9]+", "_", row_name_clean.lower())
        row_name_clean = row_name_clean.strip("_")
        if seen[row_name_clean] > 0:
            row_name_clean = "%s_%d" % (row_name_clean, seen[row_name_clean] + 1)
        fp = os.path.join(IMAGES_DIR, "examples_%s.jpg" % (row_name_clean,))
        #misc.imsave(fp, output_image.draw())
        save(fp, output_image.draw())
        seen[row_name_clean] += 1

        markup_descr = row_name.replace('"', '') \
                               .replace("\n", " ") \
                               .replace("(", "") \
                               .replace(")", "")
        markup = '![%s](%s?raw=true "%s")' % (markup_descr, fp, markup_descr)
        markups.append(markup)

    for markup in markups:
        print(markup)
Exemplo n.º 27
0
    def generate_data(self, indexs):
        src_dir = self.src_dir + '/images'
        src_dir1 = self.src_dir + '/out'
        images_batch = []
        labels_batch = []

        for i in indexs:
            image_name = os.path.join(src_dir, self.images[i])
            image = cv2.imread(image_name, cv2.IMREAD_COLOR)
            labels_strings = open(os.path.join(src_dir1, self.labels[i])).read().splitlines()
            # 1 0.716797 0.395833 0.216406 0.147222
            # cl xcenter ycenter width height
            # img.shape = H, W, C
            bboxes = []

            for line in labels_strings:
                line = line.split(" ")
                xc = float(line[1]) * image.shape[1]
                yc = float(line[2]) * image.shape[0]
                w = float(line[3]) * image.shape[1]
                h = float(line[4]) * image.shape[0]
                label = int(line[0])
                x1 = xc - (w / 2)
                x2 = xc + (w / 2)
                y1 = yc - (h / 2)
                y2 = yc + (h / 2)
                bboxes.append([x1, y1, x2, y2, label])

            ia.seed(1)

            #image = ia.quokka(size=(image.shape[1], image.shape[0]))
            bbs = BoundingBoxesOnImage([
                BoundingBox(x1=bbox[0], y1=bbox[1], x2=bbox[2], y2=bbox[3], label=label) for bbox in bboxes
            ], shape=image.shape)

            seq = iaa.Sequential([
                iaa.Multiply((1.2, 1.5)),  # change brightness, doesn't affect BBs
                iaa.LinearContrast((0.75, 1.5)),
                iaa.AdditiveGaussianNoise(loc=0, scale=(0, 0.05 * 255), per_channel=0.5),
                iaa.Fliplr(0.5)
                #iaa.Crop(percent=(0, 0.src_dir 1)),
            ])

            # Augment BBs and images.
            image_aug, bboxes_aug = seq(image=image, bounding_boxes=bbs)
            labels_strings_ = []
            for j, _ in enumerate(bboxes_aug):
                bbox = bboxes_aug.bounding_boxes[j]
                x1, y1, x2, y2, label = bbox.x1, bbox.y1, bbox.x2, bbox.y2, bbox.label
                xc = (x1 + x2) / 2
                yc = (y1 + y2) / 2
                h = y2 - y1
                w = x2 - x1
                string1 = '{0} {1} {2} {3} {4}'.format(str(label), str(xc), str(yc), str(h), str(w))
                #st = str(xc) + str(yc) + str(h) + str(w)
                #print('st: ', st)
                #string1 = labels_strings[i][0] + st

                print('string1: ', string1)
                labels_strings_.append(string1)


            # seq = iaa.Sequential(
            #     [
            #         iaa.Fliplr(0.5),
            #         iaa.Crop(percent=(0, 0.1)),
            #         iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.3))),
            #         iaa.LinearContrast((0.75, 1.5)),
            #         iaa.AdditiveGaussianNoise(loc=0, scale=(0, 0.05 * 255), per_channel=0.5),
            #         iaa.Multiply((0.8, 1.2), per_channel=0.2)
            #     ], random_order=True
            # )
            #seq_det = seq.to_deterministic()
            #augmented_image = seq_det.augment_images([image])
            #augmented_image = augmented_image[0]
            augmented_image = cv2.resize(image_aug, self.img_shape)
            images_batch.append(augmented_image)
            labels_batch.append(labels_strings_)
            #labels_batch.append(to_categorical(self.labels[i], len(self.uniq_classes)))

        images_batch = np.array(images_batch)
        labels_batch = np.array(labels_batch)
        return images_batch, labels_batch
Exemplo n.º 28
0
from __future__ import print_function, division
import imgaug as ia
from imgaug import augmenters as iaa
import numpy as np

ia.seed(3)


def main():
    image = ia.quokka(size=0.5)
    print(image.shape)
    kps = [
        ia.KeypointsOnImage(
            [
                ia.Keypoint(x=123, y=102),
                ia.Keypoint(x=182, y=98),
                ia.Keypoint(x=155, y=134),

                #ia.Keypoint(x=255, y=213),
                #ia.Keypoint(x=375, y=205),
                #ia.Keypoint(x=323, y=279),

                #ia.Keypoint(x=265, y=223),
                #ia.Keypoint(x=385, y=215),
                #ia.Keypoint(x=333, y=289),

                #ia.Keypoint(x=275, y=233),
                #ia.Keypoint(x=395, y=225),
                #ia.Keypoint(x=343, y=299),
                ia.Keypoint(x=-20, y=20)
            ],
    def __init__(self,
                 X,
                 Y,
                 random_subvolumes_in_DA=False,
                 subvol_shape=None,
                 seed=42,
                 shuffle_each_epoch=False,
                 batch_size=32,
                 da=True,
                 shift_range=0,
                 flip=False,
                 rotation=False,
                 elastic=False,
                 g_blur=False,
                 gamma_contrast=False,
                 n_classes=1,
                 out_number=1,
                 val=False,
                 prob_map=None,
                 extra_data_factor=1):
        """ImageDataGenerator constructor. Based on transformations from 
           https://github.com/aleju/imgaug.
                                                                                
           Parameters
           ----------
           X : Numpy 5D array
               Data. E.g. ``(num_of_images, x, y, z, channels)``.

           Y : Numpy 5D array
               Mask data. E.g. ``(num_of_images, x, y, z, channels)``.

           random_subvolumes_in_DA : bool, optional
               To extract random subvolumes from the given data. If not, the 
               data must be 5D and is assumed that the subvolumes are prepared. 
    
           subvol_shape : 4D tuple of ints, optional
               Shape of the subvolume to be extracted randomly from the data. 
               E. g. ``(x, y, z, channels)``.
            
           seed : int, optional
               Seed for random functions.
                
           shuffle_each_epoch : bool, optional
               To shuffle data after each epoch.

           batch_size : int, optional
               Size of the batches.
            
           da : bool, optional
               To activate the data augmentation.
            
           shift_range : float, optional
               Range to make a shift. It must be a number between ``0`` and ``1``. 

           flip : bool, optional
               To activate flips.
        
           rotation : bool, optional
               To make ``[-180, 180]`` degree range rotations.

           elastic : bool, optional
               To make elastic deformations.
            
           g_blur : bool, optional
               To insert gaussian blur on the images.

           gamma_contrast : bool, optional
               To insert gamma constrast changes on images. 

           n_classes : int, optional
               Number of classes. If ``> 1`` one-hot encoding will be done on 
               the ground truth.

           out_number : int, optional                                               
               Number of output returned by the network. Used to produce same 
               number of ground truth data on each batch. 

           val : bool, optional
               Advice the generator that the volumes will be used to validate
               the model to not make random crops (as the validation data must
               be the same on each epoch). Valid when ``random_subvolumes_in_DA`` 
               is set.

           prob_map : 5D Numpy array, optional
               Probability map used to make random crops when
               ``random_subvolumes_in_DA`` is set.
            
           extra_data_factor : int, optional
               Factor to multiply the batches yielded in a epoch. It acts as if
               ``X`` and ``Y``` where concatenated ``extra_data_factor`` times.
        """

        if X.ndim != 5 or Y.ndim != 5:
            raise ValueError("X and Y must be a 5D Numpy array")
        if X.shape[:4] != Y.shape[:4]:
            raise ValueError(
                "The shape of X and Y must be the same. {} != {}".format(
                    X.shape[:4], Y.shape[:4]))
        if random_subvolumes_in_DA:
            if subvol_shape is None:
                raise ValueError("'subvol_shape' must be provided when "
                                 "'random_subvolumes_in_DA is enabled")
            if subvol_shape[0] > X.shape[1] or subvol_shape[1] > X.shape[2] or \
               subvol_shape[2] > X.shape[3]:
                raise ValueError(
                    "Given 'subvol_shape' is bigger than the data "
                    "provided")

        self.X = (X / 255).astype(np.float32) if np.max(X) > 100 else X.astype(
            np.float32)
        self.Y = (Y / 255).astype(np.uint8) if np.max(Y) > 100 else Y.astype(
            np.uint8)
        self.rgb = True if self.X.shape[-1] != 1 else False
        self.n_classes = n_classes
        self.out_number = out_number
        self.channels = Y.shape[-1]
        self.random_subvolumes_in_DA = random_subvolumes_in_DA
        self.seed = seed
        self.shuffle_each_epoch = shuffle_each_epoch
        self.da = da
        self.val = val
        self.batch_size = batch_size
        self.o_indexes = np.arange(len(self.X))
        if extra_data_factor > 1:
            self.extra_data_factor = extra_data_factor
            self.o_indexes = np.concatenate([self.o_indexes] *
                                            extra_data_factor)
        else:
            self.extra_data_factor = 1
        self.prob_map = prob_map
        if random_subvolumes_in_DA:
            self.shape = subvol_shape
        else:
            self.shape = X.shape[1:]
        self.flip = flip
        self.rotation = rotation
        self.shift_range = shift_range
        self.total_batches_seen = 0
        self.imgaug = False

        self.da_options = []
        self.trans_made = ''
        if elastic:
            self.da_options.append(
                iaa.Sometimes(
                    0.5,
                    iaa.ElasticTransformation(alpha=(240, 250),
                                              sigma=25,
                                              mode="reflect")))
            self.trans_made += '_elastic'
            self.imgaug = True
        if g_blur:
            self.da_options.append(
                iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(1.0, 2.0))))
            self.trans_made += '_gblur'
            self.imgaug = True
        if gamma_contrast:
            self.da_options.append(
                iaa.Sometimes(0.5, iaa.GammaContrast((1.25, 1.75))))
            self.trans_made += '_gcontrast'
            self.imgaug = True
        self.seq = iaa.Sequential(self.da_options)
        ia.seed(seed)
        self.on_epoch_end()
Exemplo n.º 30
0
import tensorflow as tf
import numpy as np
import sys, os, cv2
from sklearn.utils import shuffle
from scipy.misc import imread
from scipy.misc import imresize
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder
from skimage.transform import resize
from imgaug import augmenters as iaa
import imgaug as ia

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
np.random.seed(678)
tf.set_random_seed(678)
ia.seed(678)


def tf_elu(x):
    return tf.nn.elu(x)


def d_tf_elu(x):
    return tf.cast(tf.greater_equal(x, 0), tf.float32) + (
        tf_elu(tf.cast(tf.less(x, 0), tf.float32) * x) + 1.0)


def tf_relu(x):
    return tf.nn.relu(x)

Exemplo n.º 31
0
def show_img_augs(imgs, imgaug_seq):
    ia.seed(1)
    imgs_aug = seq.augment_images(imgs)
    show_nine(imgs_aug)
Exemplo n.º 32
0
def main(args):
    seed = args.seed  # random seed for imgaug
    num_augment = args.num_augment  # 20
    image_path = args.image_path  # input path that contains all the images
    imglab_xml = args.imglab_xml  # xml files
    data_out = args.data_out  # output path

    if not os.path.exists(data_out):
        os.mkdir(data_out)

    ia.seed(seed)

    xmldoc = minidom.parse(imglab_xml)  # After labeling on imgaug
    itemlist = xmldoc.getElementsByTagName('image')
    data = ET.Element('dataset')
    element1 = ET.SubElement(data, 'images')

    for index, s in enumerate(itemlist):
        image_url = itemlist[index].attributes['file'].value
        newPath = os.path.join(image_path, image_url)  # if image is found
        imageList = xmldoc.getElementsByTagName('part')
        box = xmldoc.getElementsByTagName('box')
        part = box[index].getElementsByTagName('part')
        for j in range(num_augment):
            try:
                image = imageio.imread(newPath)
                kps, bbs = get_kps_bbs(part, box, image, index)
                seq = augment()
                image_aug, kps_aug, bbs_aug = seq(image=image,
                                                  keypoints=kps,
                                                  bounding_boxes=bbs)

                for i in range(len(kps.keypoints)):
                    kps_before = kps.keypoints[i]
                    kps_after = kps_aug.keypoints[i]

                for i in range(len(bbs.bounding_boxes)):
                    bbs_before = bbs.bounding_boxes[i]
                    bbs_after = bbs_aug.bounding_boxes[i]

                image_after = bbs_aug.draw_on_image(image_aug, size=5)
                image_after_2 = kps_aug.draw_on_image(image_after, size=5)
                img_url = str(image_url + "_augmented_" + str(j) +
                              ".jpg").replace(".jpg_augmented", "_augmented")
                augmented_img = os.path.join(data_out, img_url)

                cv2.imwrite(augmented_img,
                            cv2.cvtColor(image_aug, cv2.COLOR_RGB2BGR))

                box_top = str(int(bbs_aug.bounding_boxes[0].x1))
                box_left = str(int(bbs_aug.bounding_boxes[0].y1))
                box_width = str(int(bbs_aug.bounding_boxes[0].x2))
                box_height = str(int(bbs_aug.bounding_boxes[0].y2))
                bbs_box = [box_top, box_left, box_width, box_height]

                new_keypoints = []
                for keypoint in range(0, 4):
                    keypoints_x = int(float(kps_aug.keypoints[keypoint].x))
                    keypoints_y = int(float(kps_aug.keypoints[keypoint].y))
                    new_keypoints.append(keypoints_x)
                    new_keypoints.append(keypoints_y)

                save_xml(img_url, bbs_box, new_keypoints, data, element1)

            except FileNotFoundError:
                print(image_url + " is not found.")
Exemplo n.º 33
0
from __future__ import print_function, division

import numpy as np

import imgaug as ia
from imgaug import augmenters as iaa

ia.seed(3)


def main():
    image = ia.quokka(size=0.5)
    print(image.shape)
    kps = [
        ia.KeypointsOnImage(
            [
                ia.Keypoint(x=123, y=102),
                ia.Keypoint(x=182, y=98),
                ia.Keypoint(x=155, y=134),
                ia.Keypoint(x=-20, y=20)
            ],
            shape=(image.shape[0], image.shape[1])
        )
    ]
    print("image shape:", image.shape)

    augs = [
        iaa.PiecewiseAffine(scale=0.05),
        iaa.PiecewiseAffine(scale=0.1),
        iaa.PiecewiseAffine(scale=0.2)
    ]
Exemplo n.º 34
0
def image_augmentation(img_path, save_path):
    try:
        ia.seed(1)

        w = 1920
        h = 1080

        label = ''

        image = cv2.imread(img_path, 1)
        print(img_path.split('/')[4])
        for index in range(10):
            box_pos = []
            with open(img_path.replace('png', 'txt'), 'r') as f:
                line = f.readline()
                label = line.split(" ")[0]
                box_pos = line.split(" ")[1:]

            box_pos = list(map(lambda x: int(x), box_pos))

            bbs = BoundingBoxesOnImage([
                BoundingBox(x1=box_pos[0], y1=box_pos[1], x2=box_pos[2], y2=box_pos[3]),
            ], shape=image.shape)

            # Augmentation parameter
            seq = iaa.Sequential([
                iaa.Crop(percent=(0, 0.1)),  # random crops, 이미지 랜덤하게 크롭
                # Small gaussian blur with random sigma between 0 and 0.5.
                # But we only blur about 50% of all images.
                # 50%로 확률로 블러 넣기
                iaa.Sometimes(
                    0.5,
                    iaa.GaussianBlur(sigma=(0, 0.5))
                ),
                # Strengthen or weaken the contrast in each image.
                # 선형 대비
                iaa.LinearContrast((0.75, 1.5)),
                # Add gaussian noise.
                # For 50% of all images, we sample the noise once per pixel.
                # For the other 50% of all images, we sample the noise per pixel AND
                # channel. This can change the color (not only brightness) of the
                # pixels.
                # 노이즈 추가
                iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
                # Make some images brighter and some darker.
                # In 20% of all cases, we sample the multiplier once per channel,
                # which can end up changing the color of the images.카
                # 명암 추가
                iaa.Multiply((0.8, 1.2), per_channel=0.2),
                # Apply affine transformations to each image.
                # Scale/zoom them, translate/move them, rotate them and shear them.
                # 아핀 변환
                iaa.Affine(
                    scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
                    translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
                    rotate=(-25, 25),
                    shear=(-8, 8)
                )
            ], random_order=True)  # apply augmenters in random order

            images_aug, bbs_aug = seq(image=image, bounding_boxes=bbs)

            cv2.imwrite(f'{save_path}_aug{index}.jpg', images_aug)
            after = bbs_aug.bounding_boxes[0]
            yolo_format = convert((w, h), (after.x1, after.x2, after.y1, after.y2))
            with open(f'{save_path}_aug{index}.txt', 'w') as f:
                f.write(f'{label} {yolo_format[0]} {yolo_format[1]} {yolo_format[2]} {yolo_format[3]}')

    except Exception as e:
        print(e)
        print(img_path)
Exemplo n.º 35
0
def chapter_parameters_arithmetic():
    ia.seed(1)

    # -----------------------
    # Add
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Uniform(0, 1) + 1,  # identical to: Add(Uniform(0, 1), 1)
        iap.Add(iap.Uniform(0, 1), iap.Choice([0, 1], p=[0.7, 0.3])),
        iap.Normal(0, 1) + iap.Uniform(-5.5, -5) + iap.Uniform(5, 5.5),
        iap.Normal(0, 1) + iap.Uniform(-7, 5) + iap.Poisson(3),
        iap.Add(iap.Normal(-3, 1), iap.Normal(3, 1)),
        iap.Add(iap.Normal(-3, 1), iap.Normal(3, 1), elementwise=True)
    ]
    gridarr = draw_distributions_grid(
        params,
        rows=2,
        sample_sizes=[  # (iterations, samples per iteration)
            (1000, 1000), (1000, 1000), (1000, 1000), (1000, 1000),
            (1, 100000), (1, 100000)
        ])
    save("parameters", "arithmetic_add.jpg", gridarr)

    # -----------------------
    # Multiply
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Uniform(0, 1) * 2,  # identical to: Multiply(Uniform(0, 1), 2)
        iap.Multiply(iap.Uniform(0, 1), iap.Choice([0, 1], p=[0.7, 0.3])),
        (iap.Normal(0, 1) * iap.Uniform(-5.5, -5)) * iap.Uniform(5, 5.5),
        (iap.Normal(0, 1) * iap.Uniform(-7, 5)) * iap.Poisson(3),
        iap.Multiply(iap.Normal(-3, 1), iap.Normal(3, 1)),
        iap.Multiply(iap.Normal(-3, 1), iap.Normal(3, 1), elementwise=True)
    ]
    gridarr = draw_distributions_grid(
        params,
        rows=2,
        sample_sizes=[  # (iterations, samples per iteration)
            (1000, 1000), (1000, 1000), (1000, 1000), (1000, 1000),
            (1, 100000), (1, 100000)
        ])
    save("parameters", "arithmetic_multiply.jpg", gridarr)

    # -----------------------
    # Divide
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Uniform(0, 1) / 2,  # identical to: Divide(Uniform(0, 1), 2)
        iap.Divide(iap.Uniform(0, 1), iap.Choice([0, 2], p=[0.7, 0.3])),
        (iap.Normal(0, 1) / iap.Uniform(-5.5, -5)) / iap.Uniform(5, 5.5),
        (iap.Normal(0, 1) * iap.Uniform(-7, 5)) / iap.Poisson(3),
        iap.Divide(iap.Normal(-3, 1), iap.Normal(3, 1)),
        iap.Divide(iap.Normal(-3, 1), iap.Normal(3, 1), elementwise=True)
    ]
    gridarr = draw_distributions_grid(
        params,
        rows=2,
        sample_sizes=[  # (iterations, samples per iteration)
            (1000, 1000), (1000, 1000), (1000, 1000), (1000, 1000),
            (1, 100000), (1, 100000)
        ])
    save("parameters", "arithmetic_divide.jpg", gridarr)

    # -----------------------
    # Power
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Uniform(0, 1)**2,  # identical to: Power(Uniform(0, 1), 2)
        iap.Clip(iap.Uniform(-1, 1)**iap.Normal(0, 1), -4, 4)
    ]
    gridarr = draw_distributions_grid(params, rows=1)
    save("parameters", "arithmetic_power.jpg", gridarr)
Exemplo n.º 36
0
from mlxtend.plotting import plot_confusion_matrix
from sklearn.metrics import confusion_matrix
import cv2
from keras import backend as K
color = sns.color_palette()
import tensorflow as tf

# Enable multi-threading in tensorflow
session_conf = tf.ConfigProto(intra_op_parallelism_threads=64,
                              inter_op_parallelism_threads=64)

# Set seeds
os.environ['PYTHONHASHSEED'] = '0'
np.random.seed(111)
tf.set_random_seed(111)
aug.seed(111)

# Define a tensorflow session with above session configs
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)

# Define a session in keras
K.set_session(sess)

# Define path to the image data directory (use ubuntu if computing on AWS)
data_dir = Path("/home/ubuntu/xray/chest_xray/")

# Path to train, val, test directory
train_dir = data_dir / 'train'
val_dir = data_dir / 'val'
test_dir = data_dir / 'test'
Exemplo n.º 37
0
def chapter_parameters_special():
    ia.seed(1)

    # -----------------------
    # Choice
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Choice([0, 1, 2]),
        iap.Choice([0, 1, 2], p=[0.15, 0.5, 0.35]),
        iap.Choice([iap.Normal(-3, 1), iap.Normal(3, 1)]),
        iap.Choice([iap.Normal(-3, 1), iap.Poisson(3)])
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "special_choice.jpg", gridarr)

    # -----------------------
    # Clip
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Clip(iap.Normal(0, 1), -2, 2),
        iap.Clip(iap.Normal(0, 1), -2, None)
    ]
    gridarr = draw_distributions_grid(params, rows=1)
    save("parameters", "special_clip.jpg", gridarr)

    # -----------------------
    # Discretize
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Discretize(iap.Normal(0, 1)),
        iap.Discretize(iap.ChiSquare(3))
    ]
    gridarr = draw_distributions_grid(params, rows=1)
    save("parameters", "special_discretize.jpg", gridarr)

    # -----------------------
    # Absolute
    # -----------------------
    from imgaug import parameters as iap
    params = [iap.Absolute(iap.Normal(0, 1)), iap.Absolute(iap.Laplace(0, 1))]
    gridarr = draw_distributions_grid(params, rows=1)
    save("parameters", "special_absolute.jpg", gridarr)

    # -----------------------
    # RandomSign
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.ChiSquare(3),
        iap.RandomSign(iap.ChiSquare(3)),
        iap.RandomSign(iap.ChiSquare(3), p_positive=0.75),
        iap.RandomSign(iap.ChiSquare(3), p_positive=0.9)
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "special_randomsign.jpg", gridarr)

    # -----------------------
    # ForceSign
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.ForceSign(iap.Normal(0, 1), positive=True),
        iap.ChiSquare(3) - 3.0,
        iap.ForceSign(iap.ChiSquare(3) - 3.0, positive=True, mode="invert"),
        iap.ForceSign(iap.ChiSquare(3) - 3.0, positive=True, mode="reroll")
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "special_forcesign.jpg", gridarr)
Exemplo n.º 38
0
import glob
import os
import sys
from copy import deepcopy

import tqdm
import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa
from PIL import Image, ImageOps
import matplotlib.pyplot as plt
from xml.etree import ElementTree as ET
import random
# from keras.preprocessing.image import load_img,img_to_array,array_to_img,save_img

ia.seed(1)
# # Example batch of images.
# # The array has shape (32, 64, 64, 3) and dtype uint8.
# images = np.array(
#     [np.array(data) for _ in range(32)],
#     dtype=np.uint8
# )


def plot_img(images):
    col = np.sqrt(len(images))
    rows, res = divmod(len(images), col)
    if res:
        rows += 1
    fig, axes = plt.subplots(rows, col, constrained_layout=True)
    for img, ax in zip(images, axes.flatten()):
Exemplo n.º 39
0
def chapter_parameters_continuous():
    ia.seed(1)

    # -----------------------
    # Normal
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Normal(0, 1),
        iap.Normal(5, 3),
        iap.Normal(iap.Choice([-3, 3]), 1),
        iap.Normal(iap.Uniform(-3, 3), 1)
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "continuous_normal.jpg", gridarr)

    # -----------------------
    # Laplace
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Laplace(0, 1),
        iap.Laplace(5, 3),
        iap.Laplace(iap.Choice([-3, 3]), 1),
        iap.Laplace(iap.Uniform(-3, 3), 1)
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "continuous_laplace.jpg", gridarr)

    # -----------------------
    # ChiSquare
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.ChiSquare(1),
        iap.ChiSquare(3),
        iap.ChiSquare(iap.Choice([1, 5])),
        iap.RandomSign(iap.ChiSquare(3))
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "continuous_chisquare.jpg", gridarr)

    # -----------------------
    # Weibull
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Weibull(0.5),
        iap.Weibull(1),
        iap.Weibull(1.5),
        iap.Weibull((0.5, 1.5))
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "continuous_weibull.jpg", gridarr)

    # -----------------------
    # Uniform
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Uniform(0, 1),
        iap.Uniform(iap.Normal(-3, 1), iap.Normal(3, 1)),
        iap.Uniform([-1, 0], 1),
        iap.Uniform((-1, 0), 1)
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "continuous_uniform.jpg", gridarr)

    # -----------------------
    # Beta
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Beta(0.5, 0.5),
        iap.Beta(2.0, 2.0),
        iap.Beta(1.0, 0.5),
        iap.Beta(0.5, 1.0)
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "continuous_beta.jpg", gridarr)
from utils.generals import make_batch
from torchvision.transforms import transforms

EMOTION_DICT = {
    0: 'angry',
    1: 'disgust',
    2: 'fear',
    3: 'happy',
    4: 'sad',
    5: 'surprise',
    6: 'neutral'
}

seed = 1234
random.seed(seed)
imgaug.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False


def main(model_name, idx):
    model = getattr(models, model_name)
    model = model(in_channels=3, num_classes=7)
    state = torch.load(
        '/home/aditya/Downloads/checkpoints2/cbam_resnet50__n_2020Jun24_13.32')
    model.load_state_dict(state['net'])
    model.cuda()
    model.eval()
Exemplo n.º 41
0
import numpy as np
from scipy import ndimage, misc
from skimage import data
import matplotlib.pyplot as plt
import six.moves as sm
import re
import os
from collections import defaultdict
import PIL.Image
try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO

np.random.seed(44)
ia.seed(44)

IMAGES_DIR = "images"

def main():
    draw_single_sequential_images()
    draw_per_augmenter_images()

def draw_single_sequential_images():
    ia.seed(44)

    #image = misc.imresize(ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128))
    image = ia.quokka_square(size=(128, 128))

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(