示例#1
0
 def _augment(self, img, _):
     h, w = img.shape[:2]
     area = h * w
     for _ in range(10):
         targetArea = self.rng.uniform(self.crop_area_fraction, 1.0) * area
         aspectR = self.rng.uniform(self.aspect_ratio_low,
                                    self.aspect_ratio_high)
         ww = int(np.sqrt(targetArea * aspectR) + 0.5)
         hh = int(np.sqrt(targetArea / aspectR) + 0.5)
         if self.rng.uniform() < 0.5:
             ww, hh = hh, ww
         if hh <= h and ww <= w:
             x1 = 0 if w == ww else self.rng.randint(0, w - ww)
             y1 = 0 if h == hh else self.rng.randint(0, h - hh)
             out = img[y1:y1 + hh, x1:x1 + ww]
             out = cv2.resize(out, (self.target_shape, self.target_shape),
                              interpolation=cv2.INTER_CUBIC)
             return out
     out = imgaug.ResizeShortestEdge(self.target_shape,
                                     interp=cv2.INTER_CUBIC).augment(img)
     out = imgaug.CenterCrop(self.target_shape).augment(out)
     return out
示例#2
0
def get_data(datadir, size=IMAGESIZE, isTrain=True, zmin=-1, zmax=1):
    if isTrain:
        augs = [
            imgaug.ResizeShortestEdge(int(size * 1.143)),
            imgaug.RandomCrop(size),
            imgaug.Flip(horiz=True),
        ]
    else:
        augs = [
            imgaug.ResizeShortestEdge(int(size * 1.143)),
            imgaug.CenterCrop(size)
        ]

    def get_images(dir):
        files = sorted(glob.glob(os.path.join(dir, "*.jpg")))
        df = ImageFromFile(files, channel=3, shuffle=isTrain)
        random_df = RandomZData([size, size, 3], zmin, zmax)
        return JoinData([random_df, AugmentImageComponent(df, augs)])

    names = ['train'] if isTrain else ['test']
    df = get_images(*[os.path.join(datadir, n) for n in names])
    df = BatchData(df, BATCH if isTrain else TEST_BATCH)
    return df
示例#3
0
def fbresnet_augmentor(isTrain):
    """
    Augmentor used in fb.resnet.torch, for BGR images in range [0,255].
    """
    if isTrain:
        """
        Sec 5.1:
        We use scale and aspect ratio data augmentation [35] as
        in [12]. The network input image is a 224×224 pixel random
        crop from an augmented image or its horizontal flip.
        """
        augmentors = [
            imgaug.GoogleNetRandomCropAndResize(interp=cv2.INTER_LINEAR),
            # It's OK to remove the following augs if your CPU is not fast enough.
            # Removing brightness/contrast/saturation does not have a significant effect on accuracy.
            # Removing lighting leads to a tiny drop in accuracy.
            imgaug.RandomOrderAug([
                imgaug.BrightnessScale((0.6, 1.4), clip=False),
                imgaug.Contrast((0.6, 1.4), rgb=False, clip=False),
                imgaug.Saturation(0.4, rgb=False),
                # rgb-bgr conversion for the constants copied from fb.resnet.torch
                imgaug.Lighting(
                    0.1,
                    eigval=np.asarray([0.2175, 0.0188, 0.0045][::-1]) * 255.0,
                    eigvec=np.array([[-0.5675, 0.7192, 0.4009],
                                     [-0.5808, -0.0045, -0.8140],
                                     [-0.5836, -0.6948, 0.4203]],
                                    dtype='float32')[::-1, ::-1])
            ]),
            imgaug.Flip(horiz=True),
        ]
    else:
        augmentors = [
            imgaug.ResizeShortestEdge(256, cv2.INTER_LINEAR),
            imgaug.CenterCrop((224, 224)),
        ]
    return augmentors
示例#4
0
def get_data(datadir,
             size=IMAGE_SIZE,
             isTrain=True,
             zmin=-1,
             zmax=1,
             batch=BATCH,
             shuffle_read=False):
    if isTrain:
        augs = [
            imgaug.ResizeShortestEdge(int(size * 1.143)),
            imgaug.RandomCrop(size),
            imgaug.Flip(horiz=True),
        ]
    else:
        augs = [
            imgaug.ResizeShortestEdge(int(size * 1.143)),
            imgaug.CenterCrop(size),
        ]

    def get_images(dir):
        files = glob.glob(os.path.join(dir, "*.jpg"))
        if shuffle_read:
            import random
            random.seed(1)
            random.shuffle(files)
        else:
            files = sorted(files)
        image_df = ImageFromFile(files, channel=3, shuffle=isTrain)
        image_df = AugmentImageComponent(image_df, augs)
        random_df = RandomZData([size, size, 3], zmin, zmax)
        return JoinData([random_df, image_df])

    names = ['train'] if isTrain else ['test']
    df = get_images(*[os.path.join(datadir, n) for n in names])
    df = BatchData(df, batch)
    return df
示例#5
0
def get_train_augmentors(self, input_shape, output_shape, view=False):
    print(input_shape, output_shape)
    if self.model_mode == "class_rotmnist":
        shape_augs = [
            imgaug.Affine(rotate_max_deg=359,
                          interp=cv2.INTER_NEAREST,
                          border=cv2.BORDER_CONSTANT),
        ]

        input_augs = []

    else:
        shape_augs = [
            imgaug.Affine(
                rotate_max_deg=359,
                translate_frac=(0.01, 0.01),
                interp=cv2.INTER_NEAREST,
                border=cv2.BORDER_REFLECT,
            ),
            imgaug.Flip(vert=True),
            imgaug.Flip(horiz=True),
            imgaug.CenterCrop(input_shape),
        ]

        input_augs = [
            imgaug.RandomApplyAug(
                imgaug.RandomChooseAug([
                    GaussianBlur(),
                    MedianBlur(),
                    imgaug.GaussianNoise(),
                ]),
                0.5,
            ),
            # Standard colour augmentation
            imgaug.RandomOrderAug([
                imgaug.Hue((-8, 8), rgb=True),
                imgaug.Saturation(0.2, rgb=True),
                imgaug.Brightness(26, clip=True),
                imgaug.Contrast((0.75, 1.25), clip=True),
            ]),
            imgaug.ToUint8(),
        ]

    if self.model_mode == "seg_gland":
        label_augs = []
        label_augs = [GenInstanceContourMap(mode=self.model_mode)]
        label_augs.append(BinarizeLabel())

        if not view:
            label_augs.append(imgaug.CenterCrop(output_shape))
        else:
            label_augs.append(imgaug.CenterCrop(input_shape))

        return shape_augs, input_augs, label_augs
    elif self.model_mode == "seg_nuc":
        label_augs = []
        label_augs = [GenInstanceMarkerMap()]
        label_augs.append(BinarizeLabel())
        if not view:
            label_augs.append(imgaug.CenterCrop(output_shape))
        else:
            label_augs.append(imgaug.CenterCrop(input_shape))

        return shape_augs, input_augs, label_augs

    else:
        return shape_augs, input_augs
示例#6
0
def fbresnet_augmentor(isTrain, crop_method, color_augmentation):
    """
    Augmentor used in fb.resnet.torch, for BGR images in range [0,255].
    """
    execution_lst = []

    if isTrain:
        augmentors = [
            # 1. crop_method
            # a) GoogleNetResize
            GoogleNetResize(),
            # b) ShortestEdgeResize
            imgaug.ResizeShortestEdge(256),
            # c) GlobalWarp
            imgaug.Resize(226),  # NOTE: for CAM generation
            imgaug.RandomCrop((224, 224)),
            # d) CAMCrop
            # (when CAMCrop is set, the output from the original DataFlow has already been cropped)
            # 2. color_augmentation
            imgaug.RandomOrderAug([
                imgaug.BrightnessScale((0.6, 1.4), clip=False),
                imgaug.Contrast((0.6, 1.4), clip=False),
                imgaug.Saturation(0.4, rgb=False),
                # rgb-bgr conversion for the constants copied from fb.resnet.torch
                imgaug.Lighting(
                    0.1,
                    eigval=np.asarray([0.2175, 0.0188, 0.0045][::-1]) * 255.0,
                    eigvec=np.array([[-0.5675, 0.7192, 0.4009],
                                     [-0.5808, -0.0045, -0.8140],
                                     [-0.5836, -0.6948, 0.4203]],
                                    dtype='float32')[::-1, ::-1])
            ]),
            imgaug.Flip(horiz=True),
        ]

        #
        if crop_method == 'GoogleNetResize':
            print(
                '--> perform GoogleNetResize cropping method during the training pipeline'
            )
            execution_lst.extend([0])
        elif crop_method == 'ShortestEdgeResize':
            print(
                '--> perform ShortestEdgeResize cropping method during the training pipeline'
            )
            execution_lst.extend([1, 3])
        elif crop_method == 'GlobalWarp':
            print(
                '--> perform GlobalWarp cropping method during the training pipeline'
            )
            execution_lst.extend([2, 3])
        elif crop_method == 'CAMCrop':
            # enable CAMCrop @ 20171124
            print(
                '*** Perform CAMCrop to better the training dynamics and the results ***'
            )

        if color_augmentation:
            print(
                '--> perform color augmentation during the training pipeline')
            execution_lst.extend([4])
        else:
            print(
                '--> discard the color jittering process during the training pipeline'
            )

        # perform mirror reflection augmentation anyway
        execution_lst.extend([5])

    else:
        augmentors = [
            imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC),
            imgaug.CenterCrop((224, 224)),
            imgaug.RandomCrop((224, 224)),
        ]

        if crop_method == 'RandomCrop':
            execution_lst.extend([0, 2])

        elif crop_method == 'CenterCrop':
            execution_lst.extend([0, 1])

    return [
        item_ for id_, item_ in enumerate(augmentors) if id_ in execution_lst
    ]
示例#7
0
        def prediction_incorrect(logits,
                                 label,
                                 topk=1,
                                 name='incorrect_vector'):
            with tf.name_scope('prediction_incorrect'):
                x = tf.logical_not(tf.nn.in_top_k(logits, label, topk))
            return tf.cast(x, tf.float32, name=name)

        wrong = prediction_incorrect(logits, label, 1, name='wrong-top1')
        add_moving_summary(tf.reduce_mean(wrong, name='train-error-top1'))

        wrong = prediction_incorrect(logits, label, 5, name='wrong-top5')
        add_moving_summary(tf.reduce_mean(wrong, name='train-error-top5'))
        return loss


if __name__ == '__main__':
    import argparse
    from tensorpack.dataflow import TestDataSpeed
    parser = argparse.ArgumentParser()
    parser.add_argument('--data', required=True)
    parser.add_argument('--batch', type=int, default=32)
    args = parser.parse_args()

    augs = fbresnet_augmentor(False)
    augs = [imgaug.ResizeShortestEdge(256), imgaug.CenterCrop(224)]
    df = get_imagenet_dataflow(args.data, 'train', args.batch, augs)

    TestDataSpeed(df).start()
示例#8
0
def simple_augmentor():
    augmentors = [
        imgaug.MapImage(lazy_resize),
        imgaug.CenterCrop((224, 224)),
    ]
    return augmentors
示例#9
0
    def get_train_augmentors(self, input_shape, output_shape, view=False):
        print(input_shape, output_shape)
        shape_augs = [
            imgaug.Affine(
                        shear=5, # in degree
                        scale=(0.8, 1.2),
                        rotate_max_deg=179,
                        translate_frac=(0.01, 0.01),
                        interp=cv2.INTER_NEAREST,
                        border=cv2.BORDER_CONSTANT),
            imgaug.Flip(vert=True),
            imgaug.Flip(horiz=True),
            imgaug.CenterCrop(input_shape),
        ]
        input_augs = [
            imgaug.RandomApplyAug(
                imgaug.RandomChooseAug(
                    [
                    GaussianBlur(),
                    MedianBlur(),
                    imgaug.GaussianNoise(),
                    ]
                ), 0.5),
            # standard color augmentation
            imgaug.RandomOrderAug(
                [imgaug.Hue((-8, 8), rgb=True), 
                imgaug.Saturation(0.2, rgb=True),
                imgaug.Brightness(26, clip=True),  
                imgaug.Contrast((0.75, 1.25), clip=True),
                ]),
            CoarseDropout(),
            SaltPepperNoise(),
            imgaug.ToUint8(),

        ]  
        #input_augs = [
        #        [imgaug.Hue((-8, 8), rgb=True), 
         #       imgaug.Saturation(0.2, rgb=True),
          #      imgaug.Brightness(26, clip=True),  
           #     imgaug.Contrast((0.75, 1.25), clip=True),
            #    ]),
           # CoarseDropout(),
           # SaltPepperNoise(),
           # imgaug.ToUint8(),]

        label_augs = []
        if self.model_type == 'unet' or self.model_type == 'micronet':
            label_augs =[GenInstanceUnetMap(crop_shape=output_shape)]
        if self.model_type == 'dcan':
            label_augs =[GenInstanceContourMap(crop_shape=output_shape)]
        if self.model_type == 'dist':
            label_augs = [GenInstanceDistance(crop_shape=output_shape, inst_norm=False)]
        if self.model_type == 'np_hv':
            label_augs = [GenInstanceHV(crop_shape=output_shape)]
        if self.model_type == 'np_dist':
            label_augs = [GenInstanceDistance(crop_shape=output_shape, inst_norm=True)]

        if not self.type_classification:            
            label_augs.append(BinarizeLabel())

        if not view:
            label_augs.append(imgaug.CenterCrop(output_shape))        

        return shape_augs, input_augs, label_augs