def __init__(self):

        self.img_h = 560
        self.img_w = 1280

        ia.seed(2)
        self.seq = iaa.Sequential([
            iaa.CropToFixedSize(width=500, height=500,
                                position='uniform'),  #crop
            iaa.PerspectiveTransform(scale=(0, 0.40), keep_size=True,
                                     cval=0),  #perspective
            iaa.Affine(
                scale={
                    "x": (0.5, 2.0),
                    "y": (0.5, 2.0)
                },  #scale, rotation
                rotate=(-180, 180),
                cval=0),
            iaa.CropToFixedSize(width=256, height=256,
                                position='center'),  #set size
            iaa.Fliplr(0.5),  #flip
            iaa.Multiply((0.5, 1.5)),  #brightness
            iaa.Sharpen(alpha=(0.0, 0.4), lightness=(0.8, 1.2)),
            sometimes(
                iaa.OneOf([
                    iaa.AverageBlur(k=(1, 3)),
                    iaa.MedianBlur(k=(1, 3)),
                    iaa.GaussianBlur(sigma=(0, 0.2))
                ])),
            sometimes(
                iaa.AdditiveGaussianNoise(loc=0,
                                          scale=(0.0, 0.05 * 255),
                                          per_channel=0.5)),
            sometimes(iaa.Grayscale((0.0, 1.0)))
        ])

        self.examples = []
        for train_dir in train_data_path:

            file_dir = os.path.join(train_dir, "*.png")
            file_list = glob.glob(file_dir)

            for file_path in file_list:
                img_path = file_path.replace('Labels_', 'ColorImage_resize_')
                img_path = img_path.replace('Label', 'ColorImage')
                img_path = img_path.replace('_bin.png', '.jpg')
                label_path = file_path.replace('Labels_', 'Trainid_')

                if os.path.exists(img_path) and os.path.exists(label_path):
                    example = {}
                    example["img_path"] = img_path
                    example["label_img_path"] = label_path
                    self.examples.append(example)

        self.num_examples = len(self.examples)
示例#2
0
def sroie_refine():
    aug_list = []
    stage_0, stage_1, stage_2, stage_3 = 1536, 2048, 768, 512
    # Pad the height to stage_0
    aug_list.append(
        augmenters.PadToFixedSize(width=1, height=stage_0, pad_cval=255))
    # Resize its height to stage_1, note that stage_0 is smaller than stage_1
    # so that the font size could be increased for most cases.
    aug_list.append(
        augmenters.Resize(size={
            "height": stage_1,
            "width": "keep-aspect-ratio"
        }))
    # Crop a stage_2 x stage_2 area
    aug_list.append(augmenters.CropToFixedSize(width=stage_2, height=stage_2))
    # In case the width is not enough, pad it to stage_2 x stage_2
    aug_list.append(
        augmenters.PadToFixedSize(width=stage_2, height=stage_2, pad_cval=255))
    # Resize to stage_3 x stage_3
    aug_list.append(
        augmenters.Resize(size={
            "height": stage_3,
            "width": stage_3
        }))
    # Perform Flip
    aug_list.append(augmenters.Fliplr(0.33, name="horizontal_flip"))
    aug_list.append(augmenters.Flipud(0.33, name="vertical_flip"))
    return aug_list
示例#3
0
def aug_sroie(args, bg_color=255):
    aug_list = []
    stage_0, stage_1, stage_2, stage_3 = 1536, 2048, 768, 512
    # Pad the height to stage_0
    aug_list.append(
        augmenters.PadToFixedSize(width=1, height=stage_0, pad_cval=bg_color))
    # Resize its height to stage_1, note that stage_0 is smaller than stage_1
    # so that the font size could be increased for most cases.
    aug_list.append(
        augmenters.Resize(size={
            "height": stage_1,
            "width": "keep-aspect-ratio"
        }))
    # increase the aspect ratio
    aug_list.append(
        augmenters.Sometimes(
            args.augment_zoom_probability,
            augmenters.Affine(scale=(args.augment_zoom_lower_bound,
                                     args.augment_zoom_higher_bound))))
    # Crop a stage_2 x stage_2 area
    aug_list.append(augmenters.CropToFixedSize(width=stage_2, height=stage_2))
    # In case the width is not enough, pad it to stage_2 x stage_2
    aug_list.append(
        augmenters.PadToFixedSize(width=stage_2,
                                  height=stage_2,
                                  pad_cval=bg_color))
    # Resize to stage_3 x stage_3
    #aug_list.append(augmenters.Resize(size={"height": stage_3, "width": stage_3}))
    # Perform Flip
    aug_list.append(augmenters.Fliplr(0.33, name="horizontal_flip"))
    aug_list.append(augmenters.Flipud(0.33, name="vertical_flip"))
    return aug_list
示例#4
0
def chapter_augmenters_padtofixedsize():
    fn_start = "size/padtofixedsize"
    aug_cls = iaa.PadToFixedSize

    aug = aug_cls(width=100, height=100)
    run_and_save_augseq(fn_start + ".jpg",
                        aug, [ia.quokka(size=(80, 80)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)

    aug = aug_cls(width=100, height=100, position="center")
    run_and_save_augseq(fn_start + "_center.jpg",
                        aug, [ia.quokka(size=(80, 80)) for _ in range(4 * 1)],
                        cols=4,
                        rows=1)

    aug = aug_cls(width=100, height=100, pad_mode=ia.ALL)
    run_and_save_augseq(fn_start + "_pad_mode.jpg",
                        aug, [ia.quokka(size=(80, 80)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)

    aug = iaa.Sequential([
        iaa.PadToFixedSize(width=100, height=100),
        iaa.CropToFixedSize(width=100, height=100)
    ])
    run_and_save_augseq(fn_start + "_with_croptofixedsize.jpg",
                        aug, [ia.quokka(size=(80, 120)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)
示例#5
0
 def __init__(self):
     sometimes = lambda aug: iaa.Sometimes(0.3, aug)
     self.seq = iaa.Sequential([
         sometimes(iaa.CropToFixedSize(width=640, height=640)),
         iaa.Fliplr(0.5),
         iaa.Flipud(0.5),
         iaa.MultiplyAndAddToBrightness(mul=(0.9, 1.1), add=(-5, 5)),
         iaa.Affine(
             rotate=(-380, 380),
             scale=(0.7, 1.3),
             translate_percent={
                 'x': (-0.2, 0.2),
                 'y': (-0.2, 0.2)
             },
             #mode=['symmetric', 'reflect'], # bbox는 reflect 되지 않음
             cval=(0, 0)),
         sometimes(
             iaa.SomeOf(1, [
                 iaa.GaussianBlur(sigma=(0.6, 1.4)),
                 iaa.AverageBlur(k=(1, 3)),
                 iaa.MedianBlur(k=(1, 3)),
                 iaa.BilateralBlur(d=(5, 7), sigma_space=(10, 250))
             ])),
         sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.11))),
         sometimes(iaa.Grayscale(alpha=(0.0, 0.3))),
     ])
示例#6
0
def load_pic(path):
    pic = imread(path)
    class_name = get_class(path)
    if class_name == "malaria":
        pic = color.rgb2grey(pic)
        pic = invert(pic)
    elif class_name == "Human_HT29_colon-cancer" or class_name == "dna":
        #pic = color.rgb2grey(pic)
        pass
    elif class_name == "dp":
        pic = color.rgba2rgb(pic)
        pic = color.rgb2grey(pic)

    if (pic.dtype == 'uint16'):
        #print("anything else than double!!")
        if (pic.max() < 32768):
            pic = pic.astype('int16')
        else:
            pic = pic.astype('int32')
    if (False):
        pic = pic.astype('float')

    #if (len(pic.shape)==2):
    #pic = pic.reshape((pic.shape[0], pic.shape[1],1))
    #pic = np.repeat(pic, 3, axis=-1)
    if (pic.shape[0] == 3):
        #print("**************3 IS BACK: ",pic.shape)
        pic = pic.transpose(
        )  #Not sure this is correct to get from (y,x,3) to (3,y,x)

    #==========RESHAPING=============
    shorter_side = min(pic.shape[0], pic.shape[1])
    if (class_name == "Hela"):
        shorter_side = shorter_side // 8
    if (class_name == "mSar"):
        shorter_side = shorter_side // 6
    if (class_name == "malaria"):
        shorter_side = shorter_side // 4
    if (class_name == "Human_Hepatocyte_Murine_Fibroblast"):
        shorter_side = int(shorter_side / 2)
    else:
        #shorter_side = 256
        pass
    scale = iaa.Resize({
        "shorter-side": shorter_side,
        "longer-side": "keep-aspect-ratio"
    }).augment_image
    pic = scale(pic)

    #===========CROP==================
    crop = iaa.CropToFixedSize(width=256,
                               height=256,
                               position='center',
                               seed=0).augment_image
    pic = crop(pic)
    #==========TO [0,1]=============
    pic = pic / pic.max()

    return pic
示例#7
0
 def __init__(self, input_size=320):
     """Input shape always stay the same after the augmentation, while value be change for a same Augmenter object"""
     self.just_crop = iaa.CropToFixedSize(position='center', width=input_size, height=input_size)
     self.seq_shape = self.get_seq_shape(input_size).to_deterministic()  # iaa.Noop()
     self.seq_val = self.get_seq_val()  # iaa.Noop() self.get_seq_val()
     self.seq_val1 = self.get_seq_val()
     self.seq_val2 = self.get_seq_val()
     self.seq_noop = iaa.Sequential([iaa.Noop(), iaa.Noop()])
    def fit(self, X: Dict[str, Any], y: Any = None) -> BaseImageAugmenter:
        self.check_requirements(X, y)
        self.pad_augmenter = iaa.Pad(percent=self.percent, keep_size=False)
        self.crop_augmenter = iaa.CropToFixedSize(height=X['image_height'], width=X['image_width'])
        self.augmenter: Augmenter = iaa.Sequential([
            self.pad_augmenter,
            self.crop_augmenter
        ], name=self.get_properties()['name'])

        return self