def chapter_augmenters_coarsesaltandpepper():
    fn_start = "arithmetic/coarsesaltandpepper"

    aug = iaa.CoarseSaltAndPepper(0.05, size_percent=(0.01, 0.1))
    run_and_save_augseq(fn_start + ".jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2,
                        quality=95)

    aug = iaa.CoarseSaltAndPepper(0.05, size_px=(4, 16))
    run_and_save_augseq(fn_start + "_pixels.jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2,
                        quality=95)

    aug = iaa.CoarseSaltAndPepper(0.05,
                                  size_percent=(0.01, 0.1),
                                  per_channel=True)
    run_and_save_augseq(fn_start + "_per_channel.jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2,
                        quality=95)
def augmentation_pipeline(level):
    if level == 'resize_only':
        list_augmentations = [iaa.Resize(512)]

    elif level == 'light':
        list_augmentations = [
            iaa.Resize(512),
            iaa.Affine(
                scale=1.1,
                shear=(2.5, 2.5),
                rotate=(-5, 5),
            ),
        ]

    elif level == 'heavy':  #no rotation included
        list_augmentations = [
            iaa.Resize(512),
            iaa.Affine(
                scale=1.15,
                shear=(4.0, 4.0),
            ),
            iaa.Fliplr(0.2),  # horizontally flip 20% of the images
            iaa.Sometimes(
                0.1,
                iaa.CoarseSaltAndPepper(p=(0.01, 0.01),
                                        size_percent=(0.1, 0.2))),
            iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0.0, 2.0))),
            iaa.Sometimes(0.5,
                          iaa.AdditiveGaussianNoise(scale=(0, 0.04 * 255))),
        ]

    elif level == 'heavy_with_rotations':
        list_augmentations = [
            iaa.Resize(512),
            iaa.Affine(
                scale=1.15,
                shear=(4.0, 4.0),
                rotate=(-6, 6),
            ),
            iaa.Fliplr(0.2),  # horizontally flip 20% of the images
            iaa.Sometimes(
                0.1,
                iaa.CoarseSaltAndPepper(p=(0.01, 0.01),
                                        size_percent=(0.1, 0.2))),
            iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0.0, 2.0))),
            iaa.Sometimes(0.5,
                          iaa.AdditiveGaussianNoise(scale=(0, 0.04 * 255))),
        ]

    return list_augmentations
Exemplo n.º 3
0
    def train(self, sess, data, labels, learning_rate):
        ### do hiding?
        if len(self.do_hide) > 0:  # do_hide is num of grid
            N = np.random.choice(self.do_hide, 1)[0]

            ### if N == 0: use full image
            if N != 0:
                n, w, h, _ = data.shape
                mask = net.gen_random_patch(shape=(n, w, h), N=N)
                mask = np.expand_dims(mask, axis=3)

                data = data * mask + (1 - mask) * self.image_mean

        ### do augmentation?
        if self.do_augmentation == 1:
            data = iaa.Sequential([
                iaa.Fliplr(0.25),
                iaa.Flipud(0.25),
                iaa.Sometimes(0.25, iaa.Affine(rotate=(-180, 180))),
                iaa.Sometimes(
                    0.2,
                    iaa.Affine(translate_percent={
                        'x': (-0.15, 0.15),
                        'y': (-0.15, 0.15)
                    }))
            ]).augment_images(data)
        elif self.do_augmentation == 2:
            data = iaa.Sequential([
                iaa.Fliplr(0.25),
                iaa.Flipud(0.25),
                iaa.Sometimes(0.25, iaa.Affine(rotate=(-180, 180))),
                iaa.Sometimes(
                    0.2,
                    iaa.Affine(translate_percent={
                        'x': (-0.1, 0.1),
                        'y': (-0.1, 0.1)
                    })),
                iaa.Sometimes(
                    0.2,
                    iaa.OneOf([
                        iaa.CoarseDropout(0.2, size_percent=(0.05, 0.1)),
                        iaa.CoarseSalt(0.2, size_percent=(0.05, 0.1)),
                        iaa.CoarsePepper(0.2, size_percent=(0.05, 0.1)),
                        iaa.CoarseSaltAndPepper(0.2, size_percent=(0.05, 0.1))
                    ]))
            ]).augment_images(data)

        _, loss, scores, hits, summary = sess.run(
            [
                self.train_op, self.loss_op, self.score_op, self.hit_op,
                self.summary_op
            ],
            feed_dict={
                self.inputs: data,
                self.labels: labels,
                self.learning_rate: learning_rate,
                self.is_training: True
            })

        return loss, scores, hits, summary
Exemplo n.º 4
0
def get_augmentation_sequence():
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential([
        sometimes(iaa.Fliplr(0.5)),
        iaa.Sometimes(0.1, iaa.Add((-70, 70))),
        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
                       )),
        sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.01))),
        iaa.Sometimes(
            0.1,
            iaa.SimplexNoiseAlpha(iaa.OneOf(
                [iaa.Add((150, 255)),
                 iaa.Add((-100, 100))]),
                                  sigmoid_thresh=5)),
        iaa.Sometimes(
            0.1,
            iaa.OneOf([
                iaa.CoarseDropout((0.01, 0.15), size_percent=(0.02, 0.08)),
                iaa.CoarseSaltAndPepper(p=0.2, size_percent=0.01),
                iaa.CoarseSalt(p=0.2, size_percent=0.02)
            ])),
        iaa.Sometimes(0.25, slice_thickness_augmenter)
    ])
    return seq
def train_transform(image, segmentation_maps=None):
    image_aug = iaa.Sequential(
        [iaa.CoarseSaltAndPepper(0.05, size_percent=(0.01, 0.1))],
        random_order=False)

    geom_aug = iaa.Sequential([
        iaa.flip.Fliplr(p=0.5),
        iaa.flip.Flipud(p=0.5),
        iaa.Affine(scale={
            "x": (0.8, 1.2),
            "y": (0.8, 1.2)
        },
                   translate_percent={
                       "x": (-0.25, 0.25),
                       "y": (-0.25, 0.25)
                   },
                   rotate=(-180, 180),
                   shear=(20, 20),
                   mode='reflect'),
    ],
                              random_order=False)

    geom_aug_deterministic = geom_aug.to_deterministic()
    image = geom_aug_deterministic.augment(image=image)
    image = image_aug(image=image)

    if segmentation_maps is None:
        return image

    segmentation_maps = geom_aug_deterministic.augment(image=segmentation_maps)
    return image, segmentation_maps
Exemplo n.º 6
0
def aug_image(image, is_infer=False, augment = 1):
    if is_infer:
        flip_code = augment

        if flip_code == 1:
            seq = iaa.Sequential([iaa.Fliplr(1.0)])
        elif flip_code == 2:
            seq = iaa.Sequential([iaa.Flipud(1.0)])
        elif flip_code == 3:
            seq = iaa.Sequential([iaa.Flipud(1.0),
                                  iaa.Fliplr(1.0)])
        elif flip_code ==0:
            return image

    else:

        seq = iaa.Sequential([
            iaa.Affine(rotate= (-15, 15),
                       shear = (-15, 15),
                       mode='edge'),

            iaa.SomeOf((0, 2),
                       [
                           iaa.GaussianBlur((0, 1.5)),
                           iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.01 * 255), per_channel=0.5),
                           iaa.AddToHueAndSaturation((-5, 5)),
                           iaa.EdgeDetect(alpha=(0, 0.5)), 
                           iaa.CoarseSaltAndPepper(0.2, size_percent=(0.05, 0.1)), 
                       ],
                       random_order=True
                       )
        ])

    image = seq.augment_image(image)
    return image
Exemplo n.º 7
0
 def logic(self, image):
     for param in self.augmentation_params:
         self.augmentation_data.append([
             str(param.augmentation_value),
             iaa.CoarseSaltAndPepper(
                 p=0.2, size_percent=param.augmentation_value,
                 min_size=2).to_deterministic().augment_image(image),
             param.detection_tag
         ])
Exemplo n.º 8
0
def heavy_augmentation():
    augmenter = iaa.Sequential(
        [
            iaa.Sometimes(
                0.1,
                iaa.OneOf([
                    iaa.CoarseSaltAndPepper(0.02, size_percent=(0.001, 0.02)),
                    iaa.CoarseSaltAndPepper(
                        0.02, size_percent=(0.001, 0.02), per_channel=True)
                ])),
            iaa.Sometimes(0.5, iaa.GaussianBlur(
                sigma=(0, 3.0))),  # blur images with a sigma of 0 to 3.0
            iaa.OneOf([
                iaa.Sometimes(0.33, iaa.SaltAndPepper(p=(0, 0.05))),
                iaa.Sometimes(0.33,
                              iaa.SaltAndPepper(p=(0, 0.05), per_channel=True))
            ]),
            iaa.Sometimes(
                0.6,
                iaa.OneOf([
                    iaa.Multiply((0.6, 1.4), per_channel=0.5),
                    iaa.Multiply((0.8, 1.2), per_channel=0.5),
                ])),
            iaa.Sometimes(0.6, iaa.LinearContrast((0.6, 1.4))),

            # Too slow to do all the time
            iaa.Sometimes(
                0.2,
                iaa.CropAndPad(
                    percent=(-0.2, 0.2), pad_mode="constant", pad_cval=0)),
            iaa.Sometimes(0.1, iaa.imgcorruptlike.ShotNoise(severity=1)),
            iaa.Sometimes(
                0.2,
                iaa.OneOf([
                    iaa.imgcorruptlike.Pixelate(severity=1),
                    iaa.imgcorruptlike.GlassBlur(severity=1),
                    iaa.imgcorruptlike.ZoomBlur(severity=1),
                    iaa.imgcorruptlike.Fog(severity=1),
                    iaa.imgcorruptlike.Frost(severity=1),
                    iaa.imgcorruptlike.Spatter(severity=1)
                ])),
        ],
        random_order=True)
    return augmenter
Exemplo n.º 9
0
    def __getitem__(self, idx):
        patient_id = self.patient_ids[idx]
        if self.verbose:
            print(patient_id)

        img = self.load_image(patient_id)

        img_source_h, img_source_w = img.shape[:2]
        img_h, img_w = img.shape[:2]

        if self.is_training:
            cfg = utils.TransformCfg(
                crop_size=self.img_size,
                src_center_x=img_w / 2 + np.random.uniform(-32, 32),
                src_center_y=img_h / 2 + np.random.uniform(-32, 32),
                scale_x=self.img_size / img_source_w *
                (2**np.random.normal(0, 0.25)),
                scale_y=self.img_size / img_source_h *
                (2**np.random.normal(0, 0.25)),
                angle=np.random.normal(0, 8.0),
                shear=np.random.normal(0, 4.0),
                hflip=np.random.choice([True, False]),
                vflip=False)
        else:
            cfg = utils.TransformCfg(crop_size=self.img_size,
                                     src_center_x=img_w / 2,
                                     src_center_y=img_h / 2,
                                     scale_x=self.img_size / img_source_w,
                                     scale_y=self.img_size / img_source_h,
                                     angle=0,
                                     shear=0,
                                     hflip=False,
                                     vflip=False)

        crop = cfg.transform_image(img)
        if self.is_training:
            crop = np.power(crop, 2.0**np.random.normal(0, 0.2))
            aug = iaa.Sequential([
                iaa.Sometimes(
                    0.1,
                    iaa.CoarseSaltAndPepper(p=(0.01, 0.01),
                                            size_percent=(0.1, 0.2))),
                iaa.Sometimes(0.2, iaa.GaussianBlur(sigma=(0.0, 2.0))),
                iaa.Sometimes(0.2,
                              iaa.AdditiveGaussianNoise(scale=(0, 0.04 * 255)))
            ])
            crop = aug.augment_image(
                np.clip(np.stack([crop, crop, crop], axis=2) * 255, 0,
                        255).astype(np.uint8))[:, :, 0].astype(
                            np.float32) / 255.0

        # soft_label = 1e-4
        # labels = self.patient_categories[patient_id] * (1.0 - soft_label * 2) + soft_label
        labels = self.patient_categories[patient_id].astype(np.float32)
        sample = {'img': crop, 'categories': labels}
        return sample
Exemplo n.º 10
0
def data_aug2(image):

    seq = iaa.Sometimes(
        0.5, iaa.Identity(),
        iaa.OneOf([
            iaa.CoarseDropout((0.1, 0.2), size_percent=(0.01, 0.02)),
            iaa.CoarseSaltAndPepper(0.1, size_percent=(0.01, 0.02))
        ]))

    image = seq(image=image)

    return image
Exemplo n.º 11
0
    def setup_augmentation(self):
        # Augmentation
        # aug = iaa.Sequential([
        #     #iaa.Sometimes(0.5, iaa.PerspectiveTransform(0.05)),
        #     #iaa.Sometimes(0.5, iaa.CropAndPad(percent=(-0.05, 0.1))),
        #     #iaa.Sometimes(0.5, iaa.Affine(scale=(1.0, 1.2))),
        #     iaa.Sometimes(0.5, iaa.CoarseDropout( p=0.05, size_percent=0.01) ),F
        #     iaa.Sometimes(0.5, iaa.GaussianBlur(1.2*np.random.rand())),
        #     iaa.Sometimes(0.5, iaa.Add((-0.1, 0.1), per_channel=0.3)),
        #     iaa.Sometimes(0.3, iaa.Invert(0.2, per_channel=True)),
        #     iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4), per_channel=0.5)),
        #     iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4))),
        #     iaa.Sometimes(0.5, iaa.ContrastNormalization((0.5, 2.2), per_channel=0.3))],
        #                      random_order=False)
        # aug = iaa.Sequential([
        #     #iaa.Sometimes(0.5, iaa.CoarseDropout( p=0.25, size_percent=0.02) ),
        #     iaa.Sometimes(0.5, iaa.GaussianBlur(1.2*np.random.rand())),
        #     iaa.Sometimes(0.5, iaa.Add((-60, 60), per_channel=0.3)),
        #     iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4), per_channel=0.5)),
        #     iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4))),
        #     iaa.Sometimes(0.5, iaa.ContrastNormalization((0.5, 2.2), per_channel=0.3))],
        #                      random_order=False)

        aug = iaa.Sequential(
            [
                #iaa.Sometimes(0.5, PerspectiveTransform(0.05)),
                #iaa.Sometimes(0.5, CropAndPad(percent=(-0.05, 0.1))),
                iaa.Sometimes(0.5, iaa.Affine(scale=(1.0, 1.2))),
                #iaa.Sometimes(0.5, iaa.CoarseDropout( p=0.2, size_percent=0.05) ),
                iaa.Sometimes(
                    0.5,
                    iaa.SomeOf(2, [
                        iaa.CoarseDropout(p=0.2, size_percent=0.05),
                        iaa.Cutout(fill_mode="constant",
                                   cval=(0, 255),
                                   fill_per_channel=0.5),
                        iaa.Cutout(fill_mode="constant", cval=(255)),
                        iaa.CoarseSaltAndPepper(0.05, size_px=(4, 16)),
                        iaa.CoarseSalt(0.05, size_percent=(0.01, 0.1))
                    ])),
                iaa.Sometimes(0.5, iaa.GaussianBlur(1.2 * np.random.rand())),
                iaa.Sometimes(0.5, iaa.Add((-25, 25), per_channel=0.3)),
                iaa.Sometimes(0.3, iaa.Invert(0.2, per_channel=True)),
                iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4), per_channel=0.5)),
                iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4))),
                iaa.Sometimes(
                    0.5, iaa.ContrastNormalization(
                        (0.5, 2.2), per_channel=0.3))
            ],
            random_order=False)
        return aug
Exemplo n.º 12
0
def DA_CoarseSaltAndPepper(inputimg):
    assert inputimg.ndim in [2, 3], "input invalid! Please check input"
    values = np.arange(0.0, 0.21, 0.02)
    p_channels = np.arange(0.0, 1.01, 0.1)
    ret = []
    for i in np.arange(len(values)):
        for j in np.arange(len(p_channels)):
            Name = "DA_CoarseSalt" + str(values[i]) + "_" + str(p_channels[j])
            VALUE = str(values[i]) + "_" + str(p_channels[j])
            aug_img = iaa.CoarseSalt(p=values[i],size_percent=(0.5, 1.0), per_channel=p_channels[j]).augment_image(inputimg)
            ret.append((Name, VALUE, aug_img))
            Name = "DA_CoarseSaltAndPepper" + str(values[i]) + "_" + str(p_channels[j])
            VALUE = str(values[i]) + "_" + str(p_channels[j])
            aug_img1 = iaa.CoarseSaltAndPepper(p=values[i], size_percent=(0.5, 1.0), per_channel=p_channels[j]).augment_image(inputimg)
            ret.append((Name, VALUE, aug_img1))
    assert len(ret) == 242, "DA_CoarseDropout output size not match!"
    return ret
Exemplo n.º 13
0
    def __init__(self, size, train):
        self.seq = iaa.Sequential([
            iaa.OneOf(
                [iaa.CropToFixedSize(size, size),
                 iaa.Resize((size, size))],
                random_state=63),  # end of OneOf
            iaa.Fliplr(0.5),
            iaa.PerspectiveTransform(0.01)
        ])

        if train:
            self.seq.append(iaa.CoarseSaltAndPepper(0.2, size_percent=0.01))

        #self.seq = self.seq.to_deterministic()

        self.mean = np.array([123.15163084, 115.90288257, 103.0626238],
                             dtype=np.float32).reshape(3, 1, 1)
Exemplo n.º 14
0
    def cpu_augment(self, imgs, boxes):
        # for bx in boxes:
        #     self.assert_bboxes(bx)
        ia_bb = []
        for n in range(len(imgs)):
            c_boxes = []
            for i in boxes[n]:
                try:
                    c_boxes.append(
                        ia.BoundingBox(x1=i[0], y1=i[1], x2=i[2], y2=i[3]))
                except AssertionError:
                    print('Assertion Error: ', i)
            ia_bb.append(ia.BoundingBoxesOnImage(c_boxes, shape=imgs[n].shape))

        seq = iaa.Sequential([
            iaa.Sometimes(0.5, iaa.AddElementwise((-20, 20), per_channel=1)),
            iaa.Sometimes(0.5,
                          iaa.AdditiveGaussianNoise(scale=(0, 0.10 * 255))),
            iaa.Sometimes(0.5, iaa.Multiply((0.75, 1.25), per_channel=1)),
            iaa.Sometimes(0.5, iaa.MultiplyElementwise((0.75, 1.25))),
            iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0.0, 1.0))),
            iaa.Fliplr(0.5),
            iaa.Sometimes(
                0.95,
                iaa.SomeOf(1, [
                    iaa.CoarseDropout(p=(0.10, 0.25),
                                      size_percent=(0.25, 0.5)),
                    iaa.CoarseDropout(p=(0.0, 0.15), size_percent=(0.1, 0.25)),
                    iaa.Dropout(p=(0, 0.25)),
                    iaa.CoarseSaltAndPepper(p=(0, 0.25),
                                            size_percent=(0.1, 0.2))
                ])),
            iaa.Affine(scale=iap.Choice(
                [iap.Uniform(0.4, 1), iap.Uniform(1, 3)]),
                       rotate=(-180, 180))
        ])
        seq_det = seq.to_deterministic()
        image_b_aug = seq_det.augment_images(imgs)
        bbs_b_aug = seq_det.augment_bounding_boxes(ia_bb)
        bbs_b_aug = [
            b.remove_out_of_image().cut_out_of_image() for b in bbs_b_aug
        ]
        return image_b_aug, [
            np.array([self.bbox_r(j) for j in i.bounding_boxes])
            for i in bbs_b_aug
        ]
Exemplo n.º 15
0
    def __init__(self, args, split, input_size=224, scale=0.05, shift=0.05, flip=0.5, rand_warp=False, skip_warp=0.1, warp_scale=0.05, rotate=20):
        self.args = args
        self.split = split

        self.input_size = input_size
        self.scale = scale
        self.shift = shift
        self.flip = flip
        self.rand_warp = rand_warp
        self.skip_warp = skip_warp
        self.warp_scale = warp_scale

        self.mean = np.array([0.485, 0.456, 0.406], dtype=np.float32).reshape(1, 1, 3)
        self.std = np.array([0.229, 0.224, 0.225], dtype=np.float32).reshape(1, 1, 3)
        self.data_rng = np.random.RandomState(123)
        self.eig_val = np.array([0.2141788, 0.01817699, 0.00341571], dtype=np.float32)
        self.eig_vec = np.array([[-0.58752847, -0.69563484, 0.41340352], [-0.5832747, 0.00994535, -0.81221408], [-0.56089297, 0.71832671, 0.41158938]], dtype=np.float32)

        self.samples = []

        self.seq = iaa.Sequential([
            iaa.Sometimes(0.5, [
                iaa.Fliplr(flip),
                iaa.Affine(scale={"x": (1.0 - shift, 1.0 + shift), "y": (1.0 - shift, 1.0 + shift)}, translate_percent={"x": (-scale, scale), "y": (-scale, scale)}, rotate=(-rotate, rotate)),
                iaa.GaussianBlur(sigma=(0, 1.0)),
                iaa.Sometimes(0.5, iaa.CoarseDropout(0.2, size_percent=0.1)),
                iaa.Sometimes(0.5, iaa.PiecewiseAffine(scale=(0.01, warp_scale), nb_cols=3, nb_rows=3)),
                iaa.Sometimes(0.5,
                              iaa.OneOf([
                                  iaa.SaltAndPepper(0.1),
                                  iaa.CoarseSaltAndPepper(0.05, size_percent=(0.01, 0.1)), ]))]),
            ])

        self.split = ['train', 'val'] if self.split == 'all' else [self.split]
        for spl in self.split:
            data_path = os.path.join(args.data, spl)
            for path, dir, files in os.walk(data_path):
                for filename in files:
                    ext = os.path.splitext(filename)[-1].lower()
                    if ext in ('.png', '.jpg', '.jpeg'):
                        label_name = path.split('/')[-1]
                        self.samples.append((os.path.join(path, filename), int(label_name)))

        print('Loaded {} total: {}'.format(self.split[0], len(self.samples)))
Exemplo n.º 16
0
def get_augmentation_sequence():
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    slice_thickness_augmenter = iaa.Lambda(
        func_images=slice_thickness_func_images,
        func_keypoints=slice_thickness_func_keypoints)

    seq = iaa.Sequential([
        sometimes(iaa.Fliplr(0.5)),
        iaa.Sometimes(0.1, iaa.Add((-70, 70))),
        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
                       )),
        # sometimes(iaa.Multiply((0.5, 1.5))),
        # sometimes(iaa.ContrastNormalization((0.5, 2.0))),
        #     sometimes(iaa.Affine(
        #     translate_percent={"x": (-0.02, 0.02), "y": (-0.02, 0.02)}, # translate by -20 to +20 percent (per axis)
        #     rotate=(-2, 2), # rotate by -45 to +45 degrees
        #     shear=(-2, 2), # 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='constant' # use any of scikit-image's warping modes (see 2nd image from the top for examples)
        # )),
        #     sometimes(iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05))),
        sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.01))),
        iaa.Sometimes(
            0.1,
            iaa.SimplexNoiseAlpha(iaa.OneOf(
                [iaa.Add((150, 255)),
                 iaa.Add((-100, 100))]),
                                  sigmoid_thresh=5)),
        iaa.Sometimes(
            0.1,
            iaa.OneOf([
                iaa.CoarseDropout((0.01, 0.15), size_percent=(0.02, 0.08)),
                iaa.CoarseSaltAndPepper(p=0.2, size_percent=0.01),
                iaa.CoarseSalt(p=0.2, size_percent=0.02)
            ])),
        iaa.Sometimes(0.25, slice_thickness_augmenter)
    ])
    return seq
Exemplo n.º 17
0
def get_augmentation_sequence():
    sometimes = lambda aug: iaa.Sometimes(0.1, aug)
    seq = iaa.Sequential([
        iaa.Sometimes(
            0.01,
            iaa.OneOf([
                iaa.CoarseDropout((0.01, 0.15), size_percent=(0.02, 0.08)),
                iaa.CoarseSaltAndPepper(p=0.2, size_percent=0.01),
                iaa.CoarseSalt(p=0.2, size_percent=0.02)
            ])),
        iaa.Sometimes(0.2, iaa.LinearContrast((0.25, 0.8))),
        iaa.Sometimes(0.2, iaa.Add((-20, 20))),
    ])
    seq2 = iaa.Sequential([
        sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.01), cval=0)),
        iaa.Sometimes(
            0.2, iaa.ElasticTransformation(alpha=(15, 25), sigma=6, cval=0))
    ])

    return seq, seq2
Exemplo n.º 18
0
 def noise_tsfm(self, conf):
     seq = iaa.Sequential([
         iaa.OneOf(children=[
             iaa.GaussianBlur((0., 1.2)),
             iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255)),
             iaa.Emboss(alpha=(0, 0.5), strength=(0, 1.0)),
             iaa.ElasticTransformation(0.8),
             iaa.OneOf([
                 iaa.CoarseSaltAndPepper(p=(0., 0.15), size_percent=0.3),
                 iaa.Dropout(p=0.15)
             ])
         ])
     ])
     transform = trans.Compose([
         trans.Lambda(lambda x: imgaug_on_PIL(seq, x)),
         trans.RandomApply([trans.ColorJitter(0.1, 0.15, 0.15)]),
         trans.RandomApply([trans.ColorJitter(hue=0.1)]),
         trans.ToTensor(),
         trans.Normalize(conf.mean, conf.std)
     ])
     return transform
Exemplo n.º 19
0
def augment_images(np_img_array, img_dir, img_list):
    seq = iaa.Sequential(
        [
            iaa.Sometimes(
                0.8,
                iaa.CropAndPad(
                    percent=(0.1, 0.3),
                    pad_mode=["edge", "reflect"],
                )),
            iaa.Sometimes(
                0.35,
                iaa.WithColorspace(to_colorspace="HSV",
                                   from_colorspace="RGB",
                                   children=iaa.WithChannels(
                                       0, iaa.Add((10, 50))))),
            iaa.Sometimes(
                0.35, iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5)),
            iaa.Sometimes(0.35,
                          iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.25))),
            iaa.Sometimes(
                0.35,
                iaa.OneOf([
                    iaa.CoarseDropout((0.15, 0.2),
                                      size_percent=(0.001, 0.02),
                                      per_channel=0.1),
                    iaa.CoarseSaltAndPepper(
                        (0.15, 0.2), size_percent=(0.001, 0.02)),
                    #iaa.Superpixels(p_replace=(0.15, 0.2), n_segments=(128, 256))
                ]))
        ],
        random_order=True)
    images_aug = seq.augment_images(np_img_array)
    for image, filepath in zip(images_aug, image_list):
        global image_num
        image_num += 1
        im = Image.fromarray(image)
        new_filename = split(filepath)[-1]
        new_filename.replace(image_extension, '')
        new_filename = new_filename + str(image_num) + image_extension
        im.save(join(image_dir, new_filename))
def grayback_gaia():
    sequence = iaa.Sequential([
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.Sometimes(
            0.9,
            iaa.Affine(scale=[1, 2.5],
                       translate_percent={
                           "x": (-0.2, 0.2),
                           "y": (-0.2, 0.2)
                       },
                       rotate=(-10, 10),
                       order=1,
                       cval=(0, 0),
                       mode="constant")),
        iaa.Sometimes(0.5, iaa.ContrastNormalization((0.5, 1.5))),
        iaa.Sometimes(
            0.5, iaa.OneOf([iaa.Add(
                (-50, 50)), iaa.Multiply((0.5, 1.5))])),
        iaa.Sometimes(
            0.2,
            iaa.OneOf([
                iaa.GaussianBlur(sigma=(0.0, 4.0)),
                iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0))
            ])),
        iaa.Sometimes(0.1, iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255))),
        iaa.Sometimes(
            0.1,
            iaa.OneOf([
                iaa.Dropout(p=(0, 0.2)),
                iaa.CoarseDropout((0.0, 0.025), size_percent=(0.08)),
                iaa.SaltAndPepper(p=(0, 0.2)),
                iaa.CoarseSaltAndPepper((0.0, 0.025), size_percent=(0.08))
            ]))
    ],
                              random_order=False)

    return sequence
Exemplo n.º 21
0
def simple_imgaug_example():
    image_dir_path = dataset_home_dir_path + '/phenotyping/cvppp2017_lsc_lcc_challenge/package/CVPPP2017_LSC_training/training/A1'
    label_dir_path = dataset_home_dir_path + '/phenotyping/cvppp2017_lsc_lcc_challenge/package/CVPPP2017_LSC_training/training/A1'
    images, labels = prepare_dataset(image_dir_path, label_dir_path)

    image_width, image_height = 200, 200

    # FIXME [decide] >> Before or after random transformation?
    # Preprocessing (normalization, standardization, etc).
    images_pp = images.astype(np.float)
    #images_pp /= 255.0
    images_pp = standardize_samplewise(images_pp)
    #images_pp = standardize_featurewise(images_pp)

    if True:
        augmenter = iaa.SomeOf(
            (1, 2),
            [
                iaa.OneOf([
                    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.1, 0.1),
                            'y': (-0.1, 0.1)
                        },  # Translate by -10 to +10 percent (per axis).
                        rotate=(-10, 10),  # Rotate by -10 to +10 degrees.
                        shear=(-5, 5),  # Shear by -5 to +5 degrees.
                        #order=[0, 1],  # Use nearest neighbour or bilinear interpolation (fast).
                        order=
                        0,  # 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).
                        #mode='edge'  # Use any of scikit-image's warping modes (see 2nd image from the top for examples).
                    ),
                    #iaa.PiecewiseAffine(scale=(0.01, 0.05)),  # Move parts of the image around. Slow.
                    iaa.PerspectiveTransform(scale=(0.01, 0.1)),
                    iaa.ElasticTransformation(
                        alpha=(20.0, 50.0), sigma=(6.5, 8.5)
                    ),  # Move pixels locally around (with random strengths).
                ]),
                iaa.OneOf([
                    iaa.GaussianBlur(sigma=(
                        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.MotionBlur(k=(5, 11),
                                   angle=(0, 360),
                                   direction=(-1.0, 1.0),
                                   order=1),
                ]),
                iaa.OneOf([
                    iaa.AdditiveGaussianNoise(
                        loc=0, scale=(0.1 * 255, 0.5 * 255),
                        per_channel=False),  # Add Gaussian noise to images.
                    iaa.AdditiveLaplaceNoise(loc=0,
                                             scale=(0.1 * 255, 0.4 * 255),
                                             per_channel=False),
                    iaa.AdditivePoissonNoise(lam=(32, 96), per_channel=False),
                    iaa.CoarseSaltAndPepper(p=(0.1, 0.3),
                                            size_percent=(0.2, 0.9),
                                            per_channel=False),
                    iaa.CoarseSalt(p=(0.1, 0.3),
                                   size_percent=(0.2, 0.9),
                                   per_channel=False),
                    iaa.CoarsePepper(p=(0.1, 0.3),
                                     size_percent=(0.2, 0.9),
                                     per_channel=False),
                    iaa.CoarseDropout(p=(0.1, 0.3),
                                      size_percent=(0.05, 0.3),
                                      per_channel=False),
                ]),
                iaa.OneOf([
                    iaa.MultiplyHueAndSaturation(mul=(-10, 10),
                                                 per_channel=False),
                    iaa.AddToHueAndSaturation(value=(-255, 255),
                                              per_channel=False),
                    iaa.LinearContrast(
                        alpha=(0.5, 1.5),
                        per_channel=False),  # Improve or worsen the contrast.
                    iaa.Invert(p=1,
                               per_channel=False),  # Invert color channels.
                    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.
                ]),
            ],
            random_order=True)
    elif False:
        augmenter = 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.
                iaa.Sometimes(
                    0.5,
                    iaa.CropAndPad(percent=(-0.05, 0.1),
                                   pad_mode=ia.ALL,
                                   pad_cval=(0, 255))),
                iaa.Sometimes(
                    0.5,
                    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),
                    [
                        iaa.Sometimes(
                            0.5,
                            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)),
                        iaa.Sometimes(
                            0.5,
                            iaa.ElasticTransformation(alpha=(0.5, 3.5),
                                                      sigma=0.25)
                        ),  # Move pixels locally around (with random strengths).
                        iaa.Sometimes(
                            0.5, iaa.PiecewiseAffine(scale=(0.01, 0.05))
                        ),  # Sometimes move parts of the image around.
                        iaa.Sometimes(
                            0.5, iaa.PerspectiveTransform(scale=(0.01, 0.1)))
                    ],
                    random_order=True)
            ],
            random_order=True)
    else:
        augmenter = iaa.Sequential([
            iaa.SomeOf(
                1,
                [
                    #iaa.Sometimes(0.5, iaa.Crop(px=(0, 100))),  # Crop images from each side by 0 to 16px (randomly chosen).
                    iaa.Sometimes(0.5, iaa.Crop(percent=(
                        0,
                        0.1))),  # Crop images by 0-10% of their height/width.
                    iaa.Fliplr(0.5),  # Horizontally flip 50% of the images.
                    iaa.Flipud(0.5),  # Vertically flip 50% of the images.
                    iaa.Sometimes(
                        0.5,
                        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).
                            order=
                            0,  # 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).
                            #mode='edge'  # Use any of scikit-image's warping modes (see 2nd image from the top for examples).
                        )),
                    iaa.Sometimes(0.5, iaa.GaussianBlur(
                        sigma=(0,
                               3.0)))  # Blur images with a sigma of 0 to 3.0.
                ]),
            iaa.Scale(size={
                'height': image_height,
                'width': image_width
            })  # Resize.
        ])

    for idx in range(images.shape[0]):
        images_pp[idx] = (images_pp[idx] - np.min(images_pp[idx])) / (
            np.max(images_pp[idx]) - np.min(images_pp[idx])) * 255
    images_pp = images_pp.astype(np.uint8)

    # Test 1 (good).
    augmenter_det = augmenter.to_deterministic(
    )  # Call this for each batch again, NOT only once at the start.
    #images_aug1 = augmenter_det.augment_images(images)
    images_aug1 = augmenter_det.augment_images(images_pp)
    labels_aug1 = augmenter_det.augment_images(labels)
    augmenter_det = augmenter.to_deterministic(
    )  # Call this for each batch again, NOT only once at the start.
    #images_aug2 = augmenter_det.augment_images(images)
    images_aug2 = augmenter_det.augment_images(images_pp)
    labels_aug2 = augmenter_det.augment_images(labels)

    #export_images(images, labels, './augmented1/img', '')
    export_images(images_pp, labels, './augmented1/img', '')
    export_images(images_aug1, labels_aug1, './augmented1/img', '_aug1')
    export_images(images_aug2, labels_aug2, './augmented1/img', '_aug2')

    # Test 2 (bad).
    augmenter_det = augmenter.to_deterministic(
    )  # Call this for each batch again, NOT only once at the start.
    #images_aug1 = augmenter_det.augment_images(images)
    images_aug1 = augmenter_det.augment_images(images_pp)
    labels_aug1 = augmenter_det.augment_images(labels)
    #images_aug2 = augmenter_det.augment_images(images)
    images_aug2 = augmenter_det.augment_images(images_pp)
    labels_aug2 = augmenter_det.augment_images(labels)

    #export_images(images, labels, './augmented2/img', '')
    export_images(images_pp, labels, './augmented2/img', '')
    export_images(images_aug1, labels_aug1, './augmented2/img', '_aug1')
    export_images(images_aug2, labels_aug2, './augmented2/img', '_aug2')

    print('*********************************', images_pp.dtype)
Exemplo n.º 22
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)
    def __getitem__(self, idx):

        patient_id = self.patient_ids[idx]

        img = self.load_image(patient_id)

        if self.crop_source != 1024:
            img_source_w = self.crop_source
            img_source_h = self.crop_source
        else:
            img_source_h, img_source_w = img.shape[:2]

        img_h, img_w = img.shape[:2]

        augmentation_sigma = {
            10:
            dict(scale=0.1, angle=5.0, shear=2.5, gamma=0.2, hflip=False),
            15:
            dict(scale=0.15,
                 angle=6.0,
                 shear=4.0,
                 gamma=0.2,
                 hflip=np.random.choice([True, False])),
            20:
            dict(scale=0.15,
                 angle=6.0,
                 shear=4.0,
                 gamma=0.25,
                 hflip=np.random.choice([True, False])),
        }[self.augmentation_level]

        if self.is_training:
            cfg = utils.TransformCfg(
                crop_size=self.img_size,
                src_center_x=img_w / 2 + np.random.uniform(-32, 32),
                src_center_y=img_h / 2 + np.random.uniform(-32, 32),
                scale_x=self.img_size / img_source_w *
                (2**np.random.normal(0, augmentation_sigma['scale'])),
                scale_y=self.img_size / img_source_h *
                (2**np.random.normal(0, augmentation_sigma['scale'])),
                angle=np.random.normal(0, augmentation_sigma['angle']),
                shear=np.random.normal(0, augmentation_sigma['shear']),
                hflip=augmentation_sigma['hflip'],
                vflip=False)
        else:
            cfg = utils.TransformCfg(crop_size=self.img_size,
                                     src_center_x=img_w / 2,
                                     src_center_y=img_h / 2,
                                     scale_x=self.img_size / img_source_w,
                                     scale_y=self.img_size / img_source_h,
                                     angle=0,
                                     shear=0,
                                     hflip=False,
                                     vflip=False)

        crop = cfg.transform_image(img)
        if self.is_training:
            crop = np.power(
                crop, 2.0**np.random.normal(0, augmentation_sigma['gamma']))
            if self.augmentation_level == 20:
                aug = iaa.Sequential([
                    iaa.Sometimes(
                        0.1,
                        iaa.CoarseSaltAndPepper(p=(0.01, 0.01),
                                                size_percent=(0.1, 0.2))),
                    iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0.0, 2.0))),
                    iaa.Sometimes(
                        0.5, iaa.AdditiveGaussianNoise(scale=(0, 0.04 * 255)))
                ])
                crop = aug.augment_image(
                    np.clip(
                        np.stack([crop, crop, crop], axis=2) * 255, 0,
                        255).astype(np.uint8))[:, :, 0].astype(
                            np.float32) / 255.0
            if self.augmentation_level == 15:
                aug = iaa.Sequential([
                    iaa.Sometimes(0.25, iaa.GaussianBlur(sigma=(0.0, 1.0))),
                    iaa.Sometimes(
                        0.25, iaa.AdditiveGaussianNoise(scale=(0, 0.02 * 255)))
                ])
                crop = aug.augment_image(
                    np.clip(
                        np.stack([crop, crop, crop], axis=2) * 255, 0,
                        255).astype(np.uint8))[:, :, 0].astype(
                            np.float32) / 255.0

        annotations = []
        # print('patient_id', patient_id)

        for annotation in self.annotations[patient_id]:
            points = cfg.transform().inverse(annotation)

            res = np.zeros((1, 5))
            p0 = np.min(points, axis=0)
            p1 = np.max(points, axis=0)

            res[0, 0:2] = p0
            res[0, 2:4] = p1
            res[0, 4] = 0
            annotations.append(res)

        if len(annotations):
            annotations = np.row_stack(annotations)
        else:
            annotations = np.zeros((0, 5))

        sample = {
            'img': crop,
            'annot': annotations,
            'scale': 1.0,
            'category': self.patient_categories[patient_id]
        }
        return sample
Exemplo n.º 24
0
class NeuralNet:
    # History of accuracies on train set
    accs = []

    # History of accuracies on test set
    val_accs = []

    # Image augmenters
    augmenters = [
        ia.Noop(),
        ia.CoarseSaltAndPepper(p=0.2, size_percent=0.30),
        ia.CoarseSaltAndPepper(p=0.4, size_percent=0.30),
        ia.Pad(px=(3, 0, 0, 0)),
        ia.Pad(px=(0, 3, 0, 0)),
        ia.Pad(px=(0, 0, 3, 0)),
        ia.Pad(px=(0, 0, 0, 3)),
        ia.GaussianBlur(sigma=0.25),
        ia.GaussianBlur(sigma=0.5),
        ia.GaussianBlur(sigma=1),
        ia.GaussianBlur(sigma=2),
        ia.Affine(rotate=-2),
        ia.Affine(rotate=2),
        ia.PiecewiseAffine(scale=0.007)
    ]

    def __init__(
        self,
        experiment_name: str,
        # Input shape
        input_shape: Tuple[int, int, int],
        # Mini batch size
        mb_size: Optional = 32,
        # Number of filters in each convolutional layer
        filters_count: Optional[List[int]] = None,
        # Size of kernel, common for each convolutional layer
        kernel_size: Optional[List[int]] = None,
        # Neurons count in each dense layer
        dense_layers: Optional[List[int]] = None,
        # Learning rate
        learning_rate: float = 0.005,
        # Number of epochs
        nb_epochs: int = 50000,
        # Steps per epoch. Each |steps_per_epoch| epochs net is evaluated on val set.
        steps_per_epoch: int = 1000,
        # Dropout after each dense layer (excluding last)
        dropout_rate: float = 0.5,
        # Whether or not augmentation should be performed when choosing next
        # batch (as opposed to augmenting the entire
        augment_on_the_fly: bool = True,
        augmenters: Optional[List[ia.Augmenter]] = None,
        min_label: int = 0,
        max_label: int = NUM_CLASSES,
        # Whether or not classification should be in binary mode. If yes,
        # *please* provide the |positive_class| parameter.
        binary_classification: bool = False,
        # ID of the subject that is considered "positive" in case of
        # binary classification.
        positive_class: int = 0,
        # If provided, will store checkpoints to ckpt_file
        ckpt_file: Optional[str] = None,
    ):
        self.experiment_name = experiment_name
        self.input_shape = input_shape
        self.mb_size = mb_size
        self.learning_rate = learning_rate
        self.nb_epochs = nb_epochs
        self.steps_per_epoch = steps_per_epoch
        self.dropout = dropout_rate
        self.augment_on_the_fly = augment_on_the_fly
        self.ckpt_file = ckpt_file
        self.binary_classification = binary_classification
        self.positive_class = positive_class
        self.num_classes = NUM_CLASSES if not binary_classification else 1
        if dense_layers is None:
            dense_layers = [32, self.num_classes]
        self.dense_layers = dense_layers
        if filters_count is None:
            filters_count = [32, 64]
        self.filters_count = filters_count
        if kernel_size is None:
            kernel_size = [5, 5]
        self.kernel_size = kernel_size
        if binary_classification:
            self._confusion_matrix = np.zeros((2, 2))
        else:
            self._confusion_matrix = np.zeros(
                (self.num_classes, self.num_classes))
        if augmenters is not None:
            self.augmenters = augmenters

        self._get_data(range_beg=min_label, range_end=max_label)

        # Initialize logging.
        self.logger = logging.Logger("main_logger", level=logging.INFO)
        log_file = 'log.txt'
        formatter = logging.Formatter(fmt='{levelname:<7} {message}',
                                      style='{')
        console_handler = logging.StreamHandler()
        console_handler.setFormatter(formatter)
        file_handler = logging.FileHandler(log_file)
        file_handler.setFormatter(formatter)
        self.logger.addHandler(console_handler)
        self.logger.addHandler(file_handler)

    def _augment_single_input(self, inp_x: np.ndarray):
        """
        Augments single input with given augmenter.
        :param inp_x: single input
        :return: augmented input
        """
        augmenter = choice(self.augmenters)
        inp_x = inp_x.reshape([1] + list(inp_x.shape))
        augmented = np.ndarray.astype(
            augmenter.augment_images(np.ndarray.astype(inp_x * 256, np.uint8)),
            np.float32)
        augmented = augmented * (1 / 256)
        augmented = augmented.reshape(inp_x.shape[1:])
        return augmented

    def _augment_train_set(self) -> None:
        """
        Augments entire training set with all augmenters.
        :return: None, appends augmented images to the train set.
        """
        train_augs = []
        for augmenter in self.augmenters:
            cur_aug = np.ndarray.astype(
                augmenter.augment_images(
                    np.ndarray.astype(self.x_train * 256, np.uint8)),
                np.float32)
            cur_aug = cur_aug * (1 / 256)
            # Display augmented input, if you want
            # show_image(cur_aug[0].reshape(NN_INPUT_SIZE))
            train_augs.append(cur_aug)
        self.x_train = np.concatenate([self.x_train] + train_augs)
        self.y_train = np.concatenate([self.y_train] * (1 + len(train_augs)))

    def _get_data(self, range_beg: int = 0, range_end: int = 52) -> None:
        """
        :param range_beg, range_end: only samples such that label \in [range_beg, range_end) will be
            used. Sensible values for (range_beg, range_end) would be:
            * 00, 52 -> to use eurecom only
            * 52, 78 -> to use ias_lab_rgbd_only
            * 78, 98 -> to use superface_dataset only
        :return: self.(x|y)_(train|test) are set as a result
        """

        # Load stored numpy arrays from files.
        logging.info("Loading data..")
        self.x_train = np.load(DB_LOCATION + '/gen/' + self.experiment_name +
                               '_X_train.npy')
        self.y_train = np.load(DB_LOCATION + '/gen/' + self.experiment_name +
                               '_Y_train.npy')
        self.x_test = np.load(DB_LOCATION + '/gen/' + self.experiment_name +
                              '_X_test.npy')
        self.y_test = np.load(DB_LOCATION + '/gen/' + self.experiment_name +
                              '_Y_test.npy')
        train_indices = []
        test_indices = []

        # Filter out samples out of [range_beg, range_end).
        for i in range(len(self.y_train)):
            if range_end > np.argmax(self.y_train[i]) >= range_beg:
                train_indices.append(i)
        for i in range(len(self.y_test)):
            if range_end > np.argmax(self.y_test[i]) >= range_beg:
                test_indices.append(i)
        shuffle(train_indices)
        self.x_train = self.x_train[train_indices]
        self.y_train = self.y_train[train_indices]
        self.x_test = self.x_test[test_indices]
        self.y_test = self.y_test[test_indices]

        if self.binary_classification:

            def to_binary(row):
                return np.array([
                    1.
                ]) if np.argmax(row) == self.positive_class else np.array([0.])

            self.y_train = np.apply_along_axis(to_binary, 1, self.y_train)
            self.y_test = np.apply_along_axis(to_binary, 1, self.y_test)

        # Show first input if you want
        show_image(self.x_train[0].reshape(
            [self.input_shape[0], self.input_shape[1] * self.input_shape[2]]))

        # Image augmentation.
        if not self.augment_on_the_fly:
            self._augment_train_set()

        logging.info("Loaded data..")

    def _visualize_kernels(self):
        """
        For each convolutional layer, visualizes filters and convolved images.
        """
        for layer_no in range(len(self.conv_layers)):
            num_filters = self.filters_count[layer_no]
            kernels = []
            applied_kernels = []
            for filter_no in range(num_filters):
                inp_x = self.input_shape[0] // (2**layer_no)
                inp_y = self.input_shape[1] // (2**layer_no)
                if layer_no == 0:
                    tmp_str = 'conv2d/kernel:0'
                else:
                    tmp_str = 'conv2d_%d/kernel:0' % layer_no
                kernel = [
                    v for v in tf.global_variables() if v.name == tmp_str
                ][0]
                kernel = kernel[:, :, :, filter_no]
                cur_conv_layer = self.conv_layers[layer_no]
                if layer_no == 0:
                    kernel = tf.reshape(kernel, [
                        1, self.kernel_size[0] * self.input_shape[-1],
                        self.kernel_size[1], 1
                    ])
                else:
                    kernel = tf.reshape(kernel, [1] +\
                                        [self.kernel_size[0] * self.filters_count[layer_no - 1], self.kernel_size[1]] +
                                        [1])
                kernels.append(kernel)
                applied = tf.reshape(cur_conv_layer[0, :, :, filter_no],
                                     [1, inp_x, inp_y, 1])
                tf.summary.image('conv{0}_filter{1}_kernel'.format(
                    layer_no, filter_no),
                                 kernel,
                                 family='kernels_layer{0}'.format(layer_no),
                                 max_outputs=1)
                tf.summary.image('conv{0}_filter{1}_applied'.format(
                    layer_no, filter_no),
                                 applied,
                                 family='convolved_layer_{0}'.format(layer_no),
                                 max_outputs=1)
                applied_kernels.append(applied)
            # Write concatenated patches to summary.
            concatenated_kernels = tf.concat(kernels, axis=2)
            kernels_name = "kernels_layer{0}".format(layer_no)
            tf.summary.image(kernels_name,
                             concatenated_kernels,
                             family='kernels_all_layers')
            concatenated_applieds = tf.concat(applied_kernels, axis=2)
            applieds_name = "convolved_layer{0}".format(layer_no)
            tf.summary.image(applieds_name,
                             concatenated_applieds,
                             family='convolved_all_layers')

        if self.conv_layers:
            # Merge all visualizations of kernels.
            self.merged_summary = tf.summary.merge_all()

    def _visualize_exciting_patches(self):
        """
        For each convolutional layer, visualizes patches that excite each filter the most.
        """
        # Initialize fetch handles for exciting patches and their respective responses.
        self.exciting_patches = [[None] * k for k in self.filters_count]
        self.patches_responses = [[None] * k for k in self.filters_count]
        self.flattened_exciting_patches = [[None] * k
                                           for k in self.filters_count]
        self.all_exciting_patches_at_layer = [None for _ in self.filters_count]

        for layer_no in range(len(self.conv_layers)):
            num_filters = self.filters_count[layer_no]
            cur_conv_layer = self.conv_layers[layer_no]

            for filter_no in range(num_filters):
                # Find top 10 responses to current filter, in the current mini-batch.
                inp_x = self.input_shape[0] // (2**layer_no)
                inp_y = self.input_shape[1] // (2**layer_no)
                single_filtered_flattened = tf.reshape(
                    cur_conv_layer[:, :, :, filter_no],
                    [self.eff_mb_size * inp_x * inp_y])
                top10_vals, top10_indices = tf.nn.top_k(
                    single_filtered_flattened, k=10, sorted=True)
                top10_reshaped = tf.map_fn(
                    lambda sxy: [
                        sxy // (inp_x * inp_y),
                        (sxy // inp_y) % inp_x, sxy % inp_y
                    ],
                    top10_indices,
                    dtype=[tf.int32, tf.int32, tf.int32])

                def safe_cut_patch(sxy, size, img, layer_no):
                    """
                    :param (sample_no, x, y)@sxy
                    :param size: size of patch to cut out
                    :param img: image to cut it from
                    :param layer_no: current layer number
                    :return: Cuts out a patch of size (|size|) located at (x, y) on
                        input #sample_no in current batch.
                    """
                    sample_no, x, y = sxy
                    x *= 2**layer_no
                    y *= 2**layer_no
                    pad_marg_x = size[0] // 2
                    pad_marg_y = size[1] // 2
                    padding = [[0, 0], [pad_marg_x, pad_marg_x],
                               [pad_marg_y, pad_marg_y], [0, 0]]
                    padded = tf.pad(img, padding)
                    return padded[sample_no, x:x + size[0], y:y + size[1], :]

                # Find patches corresponding to the top 10 responses.
                # Store patches and responses in class-visible array to be retrieved later.
                self.exciting_patches[layer_no][filter_no] = \
                    tf.map_fn(lambda sxy: safe_cut_patch(sxy,
                                                         size=(self.kernel_size[0] * (2 ** layer_no),
                                                               self.kernel_size[1] * (2 ** layer_no)),
                                                         img=tf.expand_dims(self.x[:, :, :, 0], axis=-1),
                                                         layer_no=layer_no),
                              top10_reshaped,
                              dtype=tf.float32)
                self.patches_responses[layer_no][filter_no] = top10_vals

                # Flatten and concatenate the 10 patches to 2 dimensions for visualization.
                flattened_patches_shape = [1] + \
                                          [10 * self.kernel_size[0] * (2 ** layer_no),
                                           self.kernel_size[1] * (2 ** layer_no)] + \
                                          [1]
                # Write patches to summary.
                patch_name = "exciting_patches_filter{0}".format(filter_no)
                flattened_exciting_patches = tf.reshape(
                    self.exciting_patches[layer_no][filter_no],
                    flattened_patches_shape,
                    name=patch_name)
                self.flattened_exciting_patches[layer_no][
                    filter_no] = flattened_exciting_patches
            self.all_exciting_patches_at_layer[layer_no] = tf.concat(
                self.flattened_exciting_patches[layer_no], axis=2)
            # Write concatenated patches to summary.
            all_patches_name = "exciting_patches_layer{0}".format(layer_no)
            tf.summary.image(all_patches_name,
                             self.all_exciting_patches_at_layer[layer_no],
                             family='exciting_all_layers')

            # Merge all summaries.
            self.merged_summary = tf.summary.merge_all()

    def _visualize_incorrect_answer_images(self):
        correct = tf.boolean_mask(self.x, self.correct)
        correct = tf.transpose(correct, perm=[0, 1, 3, 2])
        correct = tf.reshape(
            correct,
            shape=[1, -1, self.input_shape[1] * self.input_shape[2], 1])
        correct = tf.concat([
            correct,
            tf.zeros(
                shape=[1, 1, self.input_shape[1] * self.input_shape[2], 1])
        ],
                            axis=1)
        tf.summary.image('correct', correct)
        incorrect = tf.boolean_mask(self.x, tf.logical_not(self.correct))
        incorrect = tf.transpose(incorrect, perm=[0, 1, 3, 2])
        incorrect = tf.reshape(
            incorrect,
            shape=[1, -1, self.input_shape[1] * self.input_shape[2], 1])
        incorrect = tf.concat([
            incorrect,
            tf.zeros(
                shape=[1, 1, self.input_shape[1] * self.input_shape[2], 1])
        ],
                              axis=1)
        tf.summary.image('incorrect', incorrect)

        # Merge all summaries.
        self.merged_summary = tf.summary.merge_all()

    def _create_convolutional_layers(self) -> None:
        signal = self.x

        for layer_no in range(len(self.filters_count)):
            num_filters = self.filters_count[layer_no]
            signal = tf.layers.batch_normalization(signal)

            # Init weights with std.dev = sqrt(2 / N)
            #
            input_size = int(signal.get_shape()[1]) * int(
                signal.get_shape()[2]) * int(signal.get_shape()[3])
            w_init = tf.initializers.random_normal(stddev=sqrt(2 / input_size))

            # Convolutional layer
            cur_conv_layer = tf.layers.conv2d(inputs=signal,
                                              filters=num_filters,
                                              kernel_size=self.kernel_size,
                                              kernel_initializer=w_init,
                                              padding='same')

            # Reduce image dimensions in half.
            cur_pool_layer = tf.layers.max_pooling2d(inputs=cur_conv_layer,
                                                     pool_size=[2, 2],
                                                     strides=2,
                                                     padding='valid')

            self.conv_layers.append(cur_conv_layer)
            self.pool_layers.append(cur_pool_layer)

            # Set pooled image as current signal
            signal = cur_pool_layer

        return signal

    def _create_dense_layers(self) -> None:
        signal = self.x if not self.pool_layers else self.pool_layers[-1]
        input_size = int(signal.get_shape()[1]) * int(
            signal.get_shape()[2]) * int(signal.get_shape()[3])
        signal = tf.reshape(signal, [self.eff_mb_size, input_size])

        for num_neurons in self.dense_layers[:-1]:
            signal = tf.layers.batch_normalization(signal)

            # Init weights with std.dev = sqrt(2 / N)
            # https://www.cv-foundation.org/openaccess/content_iccv_2015/papers/He_Delving_Deep_into_ICCV_2015_paper.pdf?spm=5176.100239.blogcont55892.28.pm8zm1&file=He_Delving_Deep_into_ICCV_2015_paper.pdf
            input_size = int(signal.get_shape()[1])
            w_init = tf.initializers.random_normal(stddev=sqrt(2 / input_size))

            cur_dense_layer = tf.layers.dense(inputs=signal,
                                              units=num_neurons,
                                              activation=tf.nn.leaky_relu,
                                              kernel_initializer=w_init)

            signal = cur_dense_layer

            # Apply dropout
            cur_dropout_layer = tf.layers.dropout(inputs=signal,
                                                  rate=self.dropout)

            signal = cur_dropout_layer

        # Init weights with std.dev = sqrt(2 / N)
        input_size = int(signal.get_shape()[1])
        w_init = tf.initializers.random_normal(
            stddev=tf.sqrt(tf.constant(2.) / input_size))
        cur_layer = tf.layers.dense(inputs=signal,
                                    activation=tf.nn.sigmoid,
                                    units=self.dense_layers[-1],
                                    kernel_initializer=w_init)
        self.output_layer = cur_layer

    def _create_training_objectives(self) -> None:
        if self.binary_classification:
            self.preds = tf.cast(tf.round(self.output_layer), dtype=tf.int64)
            self.y_sparse = tf.cast(self.y, dtype=tf.int64)
        else:
            self.preds = tf.argmax(self.output_layer, axis=1)
            self.y_sparse = tf.argmax(self.y, axis=1)
        self.loss = tf.losses.log_loss(self.y, self.output_layer)
        self.correct = tf.reshape(tf.equal(self.y_sparse, self.preds),
                                  shape=[self.eff_mb_size])
        self.accuracy = tf.reduce_mean(tf.cast(self.correct, tf.float32))
        self.train_op = tf.train.GradientDescentOptimizer(
            self.learning_rate).minimize(self.loss)

        self.logger.info('list of variables {0}'.format(
            list(map(lambda x: x.name, tf.global_variables()))))

    def _create_model(self):
        self.x = tf.placeholder(dtype=tf.float32,
                                shape=[None] + list(self.input_shape))
        self.y = tf.placeholder(dtype=tf.float32,
                                shape=[None, self.num_classes])
        self.eff_mb_size = tf.shape(self.x)[0]  # Effective batch size
        self.conv_layers = []
        self.pool_layers = []

        self._create_convolutional_layers()
        self._create_dense_layers()
        self._create_training_objectives()

    def train_on_batch(self, batch_x, batch_y):
        """
        :return: [loss, accuracy]
        """
        results = self.sess.run([self.loss, self.accuracy, self.train_op],
                                feed_dict={
                                    self.x: batch_x,
                                    self.y: batch_y
                                })
        self.accs.append(results[1])
        return results[:2]

    def test_on_batch(self,
                      batch_x,
                      batch_y,
                      global_step=1) -> Tuple[float, float, List[float]]:
        """
        Note that this function does not fetch |self.train_op|, so that the weights
        are not updated.
        :param batch_x:
        :param batch_y:
        :param global_step:
        :return: (loss, accuracy, probs)
        """
        if self.conv_layers:
            # Write summary
            results = self.sess.run([
                self.loss, self.accuracy, self.output_layer, self.preds,
                self.merged_summary
            ],
                                    feed_dict={
                                        self.x: batch_x,
                                        self.y: batch_y
                                    })
            msum = results[4]
            self.writer.add_summary(msum, global_step=global_step)
            self.writer.flush()
        else:
            results = self.sess.run([self.loss, self.accuracy, self.preds],
                                    feed_dict={
                                        self.x: batch_x,
                                        self.y: batch_y
                                    })
        self.val_accs.append(results[1])
        # Update confusion matrix
        preds = results[3]
        for i in range(len(batch_x)):
            self._confusion_matrix[np.argmax(batch_y[i]), preds[i]] += 1.

        return results[0], results[1], list(results[2])

    def validate(self, global_step) -> ClassificationResults:
        """
        :return: (loss, accuracy, auc_roc)
        Note that if self.binary_classification is False, auc_roc may be anything
        """
        losses = []
        accs = []
        all_pred_probs = []
        all_labels = []
        for batch_no in range(self.x_test.shape[0] // self.mb_size + 1):
            inputs = self.x_test[batch_no * self.mb_size:(batch_no + 1) *
                                 self.mb_size]
            labels = self.y_test[batch_no * self.mb_size:(batch_no + 1) *
                                 self.mb_size]
            loss, acc, probs = self.test_on_batch(inputs,
                                                  labels,
                                                  global_step=global_step)
            losses.append(loss)
            accs.append(acc)
            all_pred_probs += probs
            all_labels += list(labels)
        all_pred_probs = np.array(all_pred_probs)
        all_labels = np.array(all_labels)
        all_labels = all_labels.astype(dtype=np.bool)
        loss = np.mean(losses)
        acc = np.mean(accs)
        return ClassificationResults(loss=loss,
                                     acc=acc,
                                     pred_probs=all_pred_probs,
                                     labels=all_labels,
                                     binary=self.binary_classification)

    def _next_training_batch(self) -> (np.ndarray, np.ndarray):
        batch = sample(list(range(self.x_train.shape[0])), self.mb_size)
        batch_x, batch_y = self.x_train[batch], self.y_train[batch]
        if self.augment_on_the_fly:
            for sample_no in range(self.mb_size):
                batch_x[sample_no] = self._augment_single_input(
                    batch_x[sample_no])
        return batch_x, batch_y

    def train_and_evaluate(self) -> ClassificationResults:
        """
        Train and evaluate model.
        """
        with tf.Session() as self.sess:
            # Initialize computation graph.
            self._create_model()
            # Add visualizations to computation graph.
            self._visualize_kernels()
            self._visualize_exciting_patches()
            self._visualize_incorrect_answer_images()

            # Initialize variables.
            if self.ckpt_file:
                saver = tf.train.Saver()
                try:
                    saver.restore(self.sess, self.ckpt_file)
                except (tf.errors.InvalidArgumentError,
                        tf.errors.NotFoundError):
                    tf.global_variables_initializer().run()
            else:
                tf.global_variables_initializer().run()

            # Initialize summary writer.
            self.writer = tf.summary.FileWriter(logdir='conv_vis')

            # Initialize progress bar.
            bar = Bar('',
                      max=self.steps_per_epoch,
                      suffix='%(index)d/%(max)d ETA: %(eta)ds')

            for epoch_no in range(self.nb_epochs):
                self.logger.info("Epoch {epoch_no}/{nb_epochs}".format(
                    epoch_no=epoch_no, nb_epochs=self.nb_epochs))
                for step_no in range(self.steps_per_epoch):
                    # Train model on next batch
                    batch_x, batch_y = self._next_training_batch()
                    results = self.train_on_batch(batch_x, batch_y)

                    # Update bar
                    bar.message = 'loss: {0[0]:.8f} acc: {0[1]:.3f} mean_acc: {1:.3f}'. \
                        format(results, np.mean(self.accs[-1000:]), )
                    bar.next()

                # Re-initialize progress bar
                bar.finish()
                bar = Bar('',
                          max=self.steps_per_epoch,
                          suffix='%(index)d/%(max)d ETA: %(eta)ds')

                # Store model
                if self.ckpt_file:
                    saver.save(self.sess, self.ckpt_file)

                # Validate
                val_results = self.validate(global_step=epoch_no)
                loss, acc, auc_roc = val_results.loss, val_results.acc, val_results.get_auc_roc(
                )
                if self.binary_classification:
                    self.logger.info(
                        "Validation results: Loss: {0}, accuracy: {1}, auc_roc: {2}"
                        .format(loss, acc, auc_roc))
                else:
                    self.logger.info(
                        "Validation results: Loss: {0}, accuracy: {1}".format(
                            loss, acc))
                # Dipslay confusion matrix
                show_image(self._confusion_matrix)

            return val_results
Exemplo n.º 25
0
        scale={
            'x': TruncatedNormal(1, .1, low=.8, high=1.2),
            'y': TruncatedNormal(1, .1, low=.8, high=1.2),
        },
        translate_percent={
            'x': TruncatedNormal(0, .1, low=-.2, high=.2),
            'y': TruncatedNormal(0, .1, low=-.2, high=.2),
        },
        rotate=(-180, 180),
        shear={
            'x': TruncatedNormal(0, 10, low=-30, high=30),
            'y': TruncatedNormal(0, 10, low=-30, high=30),
        },
        cval=(0, 255),
    ),
    aug.CoarseSaltAndPepper((.01, .1), size_percent=(5E-3, 5E-2)),
])


class Trainer(DefaultTrainer):
    @classmethod
    def build_train_loader(cls, cfg):
        return build_detection_train_loader(cfg, mapper=augment)


def augment(record):
    record = deepcopy(record)
    image = plt.imread(record["filepath"])
    annotations = record["annotations"]

    boxes = [annotation["bbox"] for annotation in annotations]
Exemplo n.º 26
0
    (gir_face, depth_face) = (face.gir_img, face.depth_img)
    if gir_face is None or depth_face is None:
        return None
    if np.isnan(gir_face).any() or np.isnan(depth_face).any():
        return None
    try:
        face = normalized(face, rotate=False)
        face = hog_and_entropy(face)
    except ValueError:
        return None
    return face.get_fd_desc()


augmenters = [
    ia.Noop(),
    ia.CoarseSaltAndPepper(p=0.2, size_percent=0.30),
    ia.CoarseSaltAndPepper(p=0.4, size_percent=0.30),
    ia.Pad(px=(3, 0, 0, 0)),
    ia.Pad(px=(0, 3, 0, 0)),
    ia.Pad(px=(0, 0, 3, 0)),
    ia.Pad(px=(0, 0, 0, 3)),
    ia.GaussianBlur(sigma=0.25),
    ia.GaussianBlur(sigma=0.5),
    ia.GaussianBlur(sigma=1),
    ia.GaussianBlur(sigma=2),
    ia.Affine(rotate=-2),
    ia.Affine(rotate=2)
]


def run_preprocess():
Exemplo n.º 27
0
    # Adds poisson noise (similar to gaussian but different distribution) to an image, sampled once per pixel from
    # a poisson distribution Poisson(s), where s is sampled per image and varies between lo and hi for percent of
    # all images (sampled once for all channels) and sampled three (RGB) times (channel-wise)
    # for the rest from the same poisson distribution:
    "Additive_Poisson_Noise": lambda lo, hi, percent:
    iaa.AdditivePoissonNoise(lam=(lo, hi), per_channel=percent),

    # Adds salt and pepper noise to an image, i.e. some white-ish and black-ish pixels.
    # Replaces percent of all pixels with salt and pepper noise
    "Salt_And_Pepper": lambda percent: iaa.SaltAndPepper(percent),

    # Adds coarse salt and pepper noise to image, i.e. rectangles that contain noisy white-ish and black-ish pixels
    # Replaces percent of all pixels with salt/pepper in an image that has lo to hi percent of the input image size,
    # then upscales the results to the input image size, leading to large rectangular areas being replaced.
    "Coarse_Salt_And_Pepper": lambda percent, lo, hi: iaa.CoarseSaltAndPepper(percent, size_percent=(lo, hi)),

    # Adds salt noise to an image, i.e white-ish pixels
    # Replaces percent of all pixels with salt noise
    "Salt": lambda percent: iaa.Salt(percent),

    # Adds coarse salt noise to image, i.e. rectangles that contain noisy white-ish pixels
    # Replaces percent of all pixels with salt in an image that has lo to hi percent of the input image size,
    # then upscales the results to the input image size, leading to large rectangular areas being replaced.
    "Coarse_Salt": lambda percent, lo, hi: iaa.CoarseSalt(percent, size_percent=(lo, hi)),

    # Adds Pepper noise to an image, i.e Black-ish pixels
    # Replaces percent of all pixels with Pepper noise
    "Pepper": lambda percent: iaa.Pepper(percent),

    # Adds coarse pepper noise to image, i.e. rectangles that contain noisy black-ish pixels
Exemplo n.º 28
0
def transform(aug_type, magnitude, X):
    if aug_type == "crop":
        X_aug = iaa.Crop(px=(0, int(magnitude * 32))).augment_images(X)
    elif aug_type == "gaussian-blur":
        X_aug = iaa.GaussianBlur(sigma=(0, magnitude * 25.0)).augment_images(X)
    elif aug_type == "rotate":
        X_aug = iaa.Affine(rotate=(-180 * magnitude, 180 * magnitude)).augment_images(X)
    elif aug_type == "shear":
        X_aug = iaa.Affine(shear=(-90 * magnitude, 90 * magnitude)).augment_images(X)
    elif aug_type == "translate-x":
        X_aug = iaa.Affine(
            translate_percent={"x": (-magnitude, magnitude), "y": (0, 0)}
        ).augment_images(X)
    elif aug_type == "translate-y":
        X_aug = iaa.Affine(
            translate_percent={"x": (0, 0), "y": (-magnitude, magnitude)}
        ).augment_images(X)
    elif aug_type == "horizontal-flip":
        X_aug = iaa.Fliplr(magnitude).augment_images(X)
    elif aug_type == "vertical-flip":
        X_aug = iaa.Flipud(magnitude).augment_images(X)
    elif aug_type == "sharpen":
        X_aug = iaa.Sharpen(
            alpha=(0, 1.0), lightness=(0.50, 5 * magnitude)
        ).augment_images(X)
    elif aug_type == "emboss":
        X_aug = iaa.Emboss(
            alpha=(0, 1.0), strength=(0.0, 20.0 * magnitude)
        ).augment_images(X)
    elif aug_type == "additive-gaussian-noise":
        X_aug = iaa.AdditiveGaussianNoise(
            loc=0, scale=(0.0, magnitude * 255), per_channel=0.5
        ).augment_images(X)
    elif aug_type == "dropout":
        X_aug = iaa.Dropout(
            (0.01, max(0.011, magnitude)), per_channel=0.5
        ).augment_images(
            X
        )  # Dropout first argument should be smaller than second one
    elif aug_type == "coarse-dropout":
        X_aug = iaa.CoarseDropout(
            (0.03, 0.15), size_percent=(0.30, np.log10(magnitude * 3)), per_channel=0.2
        ).augment_images(X)
    elif aug_type == "gamma-contrast":
        X_norm = normalize(X)
        X_aug_norm = iaa.GammaContrast(magnitude * 1.75).augment_images(
            X_norm
        )  # needs 0-1 values
        X_aug = denormalize(X_aug_norm)
    elif aug_type == "brighten":
        X_aug = iaa.Add(
            (int(-40 * magnitude), int(40 * magnitude)), per_channel=0.5
        ).augment_images(
            X
        )  # brighten
    elif aug_type == "invert":
        X_aug = iaa.Invert(1.0).augment_images(X)  # magnitude not used
    elif aug_type == "fog":
        X_aug = iaa.Fog().augment_images(X)  # magnitude not used
    elif aug_type == "clouds":
        X_aug = iaa.Clouds().augment_images(X)  # magnitude not used
    elif aug_type == "histogram-equalize":
        X_aug = iaa.AllChannelsHistogramEqualization().augment_images(
            X
        )  # magnitude not used
    elif aug_type == "super-pixels":  # deprecated
        X_norm = normalize(X)
        X_norm2 = (X_norm * 2) - 1
        X_aug_norm2 = iaa.Superpixels(
            p_replace=(0, magnitude), n_segments=(100, 100)
        ).augment_images(X_norm2)
        X_aug_norm = (X_aug_norm2 + 1) / 2
        X_aug = denormalize(X_aug_norm)
    elif aug_type == "perspective-transform":
        X_norm = normalize(X)
        X_aug_norm = iaa.PerspectiveTransform(
            scale=(0.01, max(0.02, magnitude))
        ).augment_images(
            X_norm
        )  # first scale param must be larger
        np.clip(X_aug_norm, 0.0, 1.0, out=X_aug_norm)
        X_aug = denormalize(X_aug_norm)
    elif aug_type == "elastic-transform":  # deprecated
        X_norm = normalize(X)
        X_norm2 = (X_norm * 2) - 1
        X_aug_norm2 = iaa.ElasticTransformation(
            alpha=(0.0, max(0.5, magnitude * 300)), sigma=5.0
        ).augment_images(X_norm2)
        X_aug_norm = (X_aug_norm2 + 1) / 2
        X_aug = denormalize(X_aug_norm)
    elif aug_type == "add-to-hue-and-saturation":
        X_aug = iaa.AddToHueAndSaturation(
            (int(-45 * magnitude), int(45 * magnitude))
        ).augment_images(X)
    elif aug_type == "coarse-salt-pepper":
        X_aug = iaa.CoarseSaltAndPepper(p=0.2, size_percent=magnitude).augment_images(X)
    elif aug_type == "grayscale":
        X_aug = iaa.Grayscale(alpha=(0.0, magnitude)).augment_images(X)
    else:
        raise ValueError
    return X_aug
Exemplo n.º 29
0
def main():
    parser = argparse.ArgumentParser(description="Check augmenters visually.")
    parser.add_argument(
        "--only",
        default=None,
        help=
        "If this is set, then only the results of an augmenter with this name will be shown. "
        "Optionally, comma-separated list.",
        required=False)
    args = parser.parse_args()

    images = [
        ia.quokka_square(size=(128, 128)),
        ia.imresize_single_image(data.astronaut(), (128, 128))
    ]

    keypoints = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=50, y=40),
            ia.Keypoint(x=70, y=38),
            ia.Keypoint(x=62, y=52)
        ],
                            shape=images[0].shape),
        ia.KeypointsOnImage([
            ia.Keypoint(x=55, y=32),
            ia.Keypoint(x=42, y=95),
            ia.Keypoint(x=75, y=89)
        ],
                            shape=images[1].shape)
    ]

    bounding_boxes = [
        ia.BoundingBoxesOnImage([
            ia.BoundingBox(x1=10, y1=10, x2=20, y2=20),
            ia.BoundingBox(x1=40, y1=50, x2=70, y2=60)
        ],
                                shape=images[0].shape),
        ia.BoundingBoxesOnImage([
            ia.BoundingBox(x1=10, y1=10, x2=20, y2=20),
            ia.BoundingBox(x1=40, y1=50, x2=70, y2=60)
        ],
                                shape=images[1].shape)
    ]

    augmenters = [
        iaa.Sequential([
            iaa.CoarseDropout(p=0.5, size_percent=0.05),
            iaa.AdditiveGaussianNoise(scale=0.1 * 255),
            iaa.Crop(percent=0.1)
        ],
                       name="Sequential"),
        iaa.SomeOf(2,
                   children=[
                       iaa.CoarseDropout(p=0.5, size_percent=0.05),
                       iaa.AdditiveGaussianNoise(scale=0.1 * 255),
                       iaa.Crop(percent=0.1)
                   ],
                   name="SomeOf"),
        iaa.OneOf(children=[
            iaa.CoarseDropout(p=0.5, size_percent=0.05),
            iaa.AdditiveGaussianNoise(scale=0.1 * 255),
            iaa.Crop(percent=0.1)
        ],
                  name="OneOf"),
        iaa.Sometimes(0.5,
                      iaa.AdditiveGaussianNoise(scale=0.1 * 255),
                      name="Sometimes"),
        iaa.WithColorspace("HSV",
                           children=[iaa.Add(20)],
                           name="WithColorspace"),
        iaa.WithChannels([0], children=[iaa.Add(20)], name="WithChannels"),
        iaa.AddToHueAndSaturation((-20, 20),
                                  per_channel=True,
                                  name="AddToHueAndSaturation"),
        iaa.Noop(name="Noop"),
        iaa.Resize({
            "width": 64,
            "height": 64
        }, name="Resize"),
        iaa.CropAndPad(px=(-8, 8), name="CropAndPad-px"),
        iaa.Pad(px=(0, 8), name="Pad-px"),
        iaa.Crop(px=(0, 8), name="Crop-px"),
        iaa.Crop(percent=(0, 0.1), name="Crop-percent"),
        iaa.Fliplr(0.5, name="Fliplr"),
        iaa.Flipud(0.5, name="Flipud"),
        iaa.Superpixels(p_replace=0.75, n_segments=50, name="Superpixels"),
        iaa.Grayscale(0.5, name="Grayscale0.5"),
        iaa.Grayscale(1.0, name="Grayscale1.0"),
        iaa.GaussianBlur((0, 3.0), name="GaussianBlur"),
        iaa.AverageBlur(k=(3, 11), name="AverageBlur"),
        iaa.MedianBlur(k=(3, 11), name="MedianBlur"),
        iaa.BilateralBlur(d=10, name="BilateralBlur"),
        iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0, 2.0), name="Sharpen"),
        iaa.Emboss(alpha=(0.1, 1.0), strength=(0, 2.0), name="Emboss"),
        iaa.EdgeDetect(alpha=(0.1, 1.0), name="EdgeDetect"),
        iaa.DirectedEdgeDetect(alpha=(0.1, 1.0),
                               direction=(0, 1.0),
                               name="DirectedEdgeDetect"),
        iaa.Add((-50, 50), name="Add"),
        iaa.Add((-50, 50), per_channel=True, name="AddPerChannel"),
        iaa.AddElementwise((-50, 50), name="AddElementwise"),
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.0, 0.1 * 255),
                                  name="AdditiveGaussianNoise"),
        iaa.Multiply((0.5, 1.5), name="Multiply"),
        iaa.Multiply((0.5, 1.5), per_channel=True, name="MultiplyPerChannel"),
        iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"),
        iaa.Dropout((0.0, 0.1), name="Dropout"),
        iaa.CoarseDropout(p=0.05,
                          size_percent=(0.05, 0.5),
                          name="CoarseDropout"),
        iaa.Invert(p=0.5, name="Invert"),
        iaa.Invert(p=0.5, per_channel=True, name="InvertPerChannel"),
        iaa.ContrastNormalization(alpha=(0.5, 2.0),
                                  name="ContrastNormalization"),
        iaa.SaltAndPepper(p=0.05, name="SaltAndPepper"),
        iaa.Salt(p=0.05, name="Salt"),
        iaa.Pepper(p=0.05, name="Pepper"),
        iaa.CoarseSaltAndPepper(p=0.05,
                                size_percent=(0.01, 0.1),
                                name="CoarseSaltAndPepper"),
        iaa.CoarseSalt(p=0.05, size_percent=(0.01, 0.1), name="CoarseSalt"),
        iaa.CoarsePepper(p=0.05, size_percent=(0.01, 0.1),
                         name="CoarsePepper"),
        iaa.Affine(scale={
            "x": (0.8, 1.2),
            "y": (0.8, 1.2)
        },
                   translate_px={
                       "x": (-16, 16),
                       "y": (-16, 16)
                   },
                   rotate=(-45, 45),
                   shear=(-16, 16),
                   order=ia.ALL,
                   cval=(0, 255),
                   mode=ia.ALL,
                   name="Affine"),
        iaa.PiecewiseAffine(scale=0.03,
                            nb_rows=(2, 6),
                            nb_cols=(2, 6),
                            name="PiecewiseAffine"),
        iaa.PerspectiveTransform(scale=0.1, name="PerspectiveTransform"),
        iaa.ElasticTransformation(alpha=(0.5, 8.0),
                                  sigma=1.0,
                                  name="ElasticTransformation"),
        iaa.Alpha(factor=(0.0, 1.0),
                  first=iaa.Add(100),
                  second=iaa.Dropout(0.5),
                  per_channel=False,
                  name="Alpha"),
        iaa.Alpha(factor=(0.0, 1.0),
                  first=iaa.Add(100),
                  second=iaa.Dropout(0.5),
                  per_channel=True,
                  name="AlphaPerChannel"),
        iaa.Alpha(factor=(0.0, 1.0),
                  first=iaa.Affine(rotate=(-45, 45)),
                  per_channel=True,
                  name="AlphaAffine"),
        iaa.AlphaElementwise(factor=(0.0, 1.0),
                             first=iaa.Add(50),
                             second=iaa.ContrastNormalization(2.0),
                             per_channel=False,
                             name="AlphaElementwise"),
        iaa.AlphaElementwise(factor=(0.0, 1.0),
                             first=iaa.Add(50),
                             second=iaa.ContrastNormalization(2.0),
                             per_channel=True,
                             name="AlphaElementwisePerChannel"),
        iaa.AlphaElementwise(factor=(0.0, 1.0),
                             first=iaa.Affine(rotate=(-45, 45)),
                             per_channel=True,
                             name="AlphaElementwiseAffine"),
        iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0),
                              per_channel=False,
                              name="SimplexNoiseAlpha"),
        iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0),
                                per_channel=False,
                                name="FrequencyNoiseAlpha")
    ]

    augmenters.append(
        iaa.Sequential([iaa.Sometimes(0.2, aug.copy()) for aug in augmenters],
                       name="Sequential"))
    augmenters.append(
        iaa.Sometimes(0.5, [aug.copy() for aug in augmenters],
                      name="Sometimes"))

    for augmenter in augmenters:
        if args.only is None or augmenter.name in [
                v.strip() for v in args.only.split(",")
        ]:
            print("Augmenter: %s" % (augmenter.name, ))
            grid = []
            for image, kps, bbs in zip(images, keypoints, bounding_boxes):
                aug_det = augmenter.to_deterministic()
                imgs_aug = aug_det.augment_images(
                    np.tile(image[np.newaxis, ...], (16, 1, 1, 1)))
                kps_aug = aug_det.augment_keypoints([kps] * 16)
                bbs_aug = aug_det.augment_bounding_boxes([bbs] * 16)
                imgs_aug_drawn = [
                    kps_aug_one.draw_on_image(img_aug)
                    for img_aug, kps_aug_one in zip(imgs_aug, kps_aug)
                ]
                imgs_aug_drawn = [
                    bbs_aug_one.draw_on_image(img_aug)
                    for img_aug, bbs_aug_one in zip(imgs_aug_drawn, bbs_aug)
                ]
                grid.append(np.hstack(imgs_aug_drawn))
            ia.imshow(np.vstack(grid))
Exemplo n.º 30
0
    def transform(self, image: np.ndarray, target: str,
                  condition: int) -> Tuple[torch.Tensor, torch.Tensor, int]:
        """Transforms and normalizes the data. If in training mode the data is augmentated.

        Args:
            image (np.ndarray): Image to transform
            target (str): Training target
            condition (int): Condition

        Returns:
            Tuple[torch.Tensor, torch.Tensor, int]: Augmented image, target and condition
        """
        # Resize
        resize = iaa.Resize({"height": 224, "width": 224})
        image = resize.augment_image(image)

        # Random horizontal flipping and erase
        if self.train:

            if random.random() > 0.5:
                # flip image
                flip = iaa.HorizontalFlip(1.0)
                image = flip.augment_image(image)

                # flip class
                if target == "a":
                    target = "d"
                elif target == "d":
                    target = "a"

                # flip condition
                if condition == 2:
                    condition = 4
                elif condition == 4:
                    condition = 2

            #imgaug
            seq = iaa.Sequential([
                iaa.Sometimes(0.5, iaa.Affine(rotate=(-15, 15))),
                iaa.Sometimes(0.3, iaa.EdgeDetect(alpha=(0.3, 0.8))),
                iaa.Sometimes(0.5, iaa.MotionBlur(k=iap.Choice([3, 5, 7]))),
                iaa.OneOf([
                    iaa.Dropout(p=(0, 0.3), per_channel=0.5),
                    iaa.CoarseSaltAndPepper(0.05, size_percent=(0.01, 0.09))
                ]),
                iaa.Sometimes(0.5, iaa.AllChannelsCLAHE(clip_limit=(1, 10)))
            ])
            image = seq.augment_image(image)

        # Transform to tensor
        image = TF.to_tensor(image)

        # Transform to one hot encoding
        target = torch.tensor(self.target_dict[target])

        #normalize image to fit pretrained vgg model
        normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                         std=[0.229, 0.224, 0.225])
        image = normalize(image)

        return image, target, condition