Beispiel #1
0
    def __init__(self, num_classes, input_size=None):
        super(HSRInhandObjectsDataTransform, self).__init__()

        random.seed(1000)

        if input_size is not None:
            height, width = input_size
            self._train_transform_list.append(Resize(height, width))
            self._val_transform_list.append(Resize(height, width))

        self._train_transform_list = self._train_transform_list + [
            HorizontalFlip(p=0.5),
            Rotate(p=0.5, limit=(-15, 15)),
            GaussNoise(p=0.5),
            RandomBrightnessContrast(p=0.5),
            RandomShadow(p=0.5),
            Normalize(
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225],
            ),
            ToTensor(num_classes=num_classes),
        ]

        self._val_transform_list = self._val_transform_list + [
            Normalize(
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225],
            ),
            ToTensor(num_classes=num_classes),
        ]

        self._initialize_transform_dict()
Beispiel #2
0
    def __init__(self, to_tensor=True):
        augs = [
            RandomSizedBBoxSafeCrop(height=512, width=512),
            RandomBrightness(p=0.5),
            RandomContrast(p=0.5),

            #RandomSunFlare(p=0.5, flare_roi=(0, 0, 1, 0.5), angle_lower=0.5,src_radius= 150),
            RandomShadow(p=0.5,
                         num_shadows_lower=1,
                         num_shadows_upper=1,
                         shadow_dimension=5,
                         shadow_roi=(0, 0.5, 1, 1)),
            HorizontalFlip(p=0.5),
            GaussianBlur(p=0.5),
        ]
        if to_tensor:
            augs.append(
                ToTensor(normalize={
                    "mean": [0.485, 0.456, 0.406],
                    "std": [0.229, 0.224, 0.225]
                }))
        self.transform = Compose(augs,
                                 bbox_params={
                                     "format": "albumentations",
                                     "min_area": 0,
                                     "min_visibility": 0.2,
                                     'label_fields': ['category_id']
                                 })
def strong_aug(p=0.5, crop_size=(512, 512)):
    return Compose([
        RandomResizedCrop(crop_size[0],
                          crop_size[1],
                          scale=(0.3, 1.0),
                          ratio=(0.75, 1.3),
                          interpolation=4,
                          p=1.0),
        RandomRotate90(),
        Flip(),
        Transpose(),
        OneOf([
            IAAAdditiveGaussianNoise(),
            GaussNoise(),
        ], p=0.8),
        OneOf([
            MotionBlur(p=0.5),
            MedianBlur(blur_limit=3, p=0.5),
            Blur(blur_limit=3, p=0.5),
        ],
              p=0.3),
        ShiftScaleRotate(
            shift_limit=0.2, scale_limit=0.5, rotate_limit=180, p=0.8),
        OneOf([
            OpticalDistortion(p=0.5),
            GridDistortion(p=0.5),
            IAAPiecewiseAffine(p=0.5),
            ElasticTransform(p=0.5),
        ],
              p=0.3),
        OneOf([
            CLAHE(clip_limit=2),
            IAASharpen(),
            IAAEmboss(),
            RandomBrightnessContrast(),
        ],
              p=0.3),
        OneOf([
            GaussNoise(),
            RandomRain(
                p=0.2, brightness_coefficient=0.9, drop_width=1, blur_value=5),
            RandomSnow(p=0.4,
                       brightness_coeff=0.5,
                       snow_point_lower=0.1,
                       snow_point_upper=0.3),
            RandomShadow(p=0.2,
                         num_shadows_lower=1,
                         num_shadows_upper=1,
                         shadow_dimension=5,
                         shadow_roi=(0, 0.5, 1, 1)),
            RandomFog(
                p=0.5, fog_coef_lower=0.3, fog_coef_upper=0.5, alpha_coef=0.1)
        ],
              p=0.3),
        RGBShift(),
        HueSaturationValue(p=0.9),
    ],
                   p=p)
Beispiel #4
0
    def setUpClass(cls):
        from cral.tracking import set_experiment
        from cral.pipeline.core import ClassificationPipe
        from cral.augmentations.engine import Classification as Classification_augmentor

        zip_url = 'https://segmind-data.s3.ap-south-1.amazonaws.com/edge/data/classification/bikes_persons_dataset.zip'
        path_to_zip_file = tf.keras.utils.get_file('bikes_persons_dataset.zip',
                                                   zip_url,
                                                   cache_dir='/tmp',
                                                   cache_subdir='',
                                                   extract=False)
        directory_to_extract_to = '/tmp'
        with zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:
            zip_ref.extractall(directory_to_extract_to)

        set_experiment('6f2c48d8-970a-4e28-a609-38088cab599a')
        cls.new_pipe = ClassificationPipe()

        cls.new_pipe.add_data(train_images_dir='/tmp/bikes_persons_dataset',
                              val_images_dir=None,
                              split=0.2)

        cls.new_pipe.lock_data()

        aug = [
            OneOf([
                RandomFog(
                    fog_coef_lower=1, fog_coef_upper=1, alpha_coef=0.05,
                    p=1.0),
                RandomBrightnessContrast(
                    brightness_limit=0.2, contrast_limit=10.5, p=1.0),
                RandomShadow(shadow_roi=(0, 0.5, 1, 1),
                             num_shadows_lower=1,
                             num_shadows_upper=2,
                             shadow_dimension=5,
                             p=0.5),
                RandomSnow(),
                RandomSunFlare()
            ],
                  p=0.2)
        ]

        augmentor = Classification_augmentor(aug=aug)
        cls.new_pipe.set_aug(augmentor)

        cls.report_path = '/tmp/reports'
        if os.path.isdir(cls.report_path) is False:
            os.mkdir(cls.report_path)
Beispiel #5
0
def strong_aug(p=0.6):
    return Compose([
        RandomShadow(shadow_roi=(0, 0, 1, 1), p=0.75),
        OneOf([MotionBlur(), GaussianBlur()]),
        OneOf([ToGray(), ToSepia()]),
        OneOf([
            InvertImg(),
            RandomBrightnessContrast(brightness_limit=0.75, p=0.75),
            RandomGamma(),
            HueSaturationValue()
        ],
              p=0.75)
    ],
                   bbox_params=BboxParams("pascal_voc",
                                          label_fields=["category_id"],
                                          min_area=0.0,
                                          min_visibility=0.0),
                   p=p)
Beispiel #6
0
    def __init__(self):
        self.random_brightness_contrast = RandomBrightnessContrast()
        self.hue_saturation_value = HueSaturationValue()
        self.random_gamma = RandomGamma()
        self.clahe = CLAHE()

        self.blur = Blur()
        self.gauss_noise = GaussNoise()

        self.channel_shuffle = ChannelShuffle()
        self.rgb_shift = RGBShift()
        self.channel_dropout = ChannelDropout()

        self.random_fog = RandomFog(fog_coef_upper=0.4)
        self.random_rain = RandomRain()
        self.random_snow = RandomSnow()
        self.random_shadow = RandomShadow()
        self.random_sunflare = RandomSunFlare(angle_upper=0.2)
def train_transformations(prob=1.0):
    return Compose([
        PadIfNeeded(min_height=384,
                    min_width=1280,
                    border_mode=cv2.BORDER_CONSTANT,
                    value=(0, 0, 0),
                    always_apply=True),
        OneOf([HorizontalFlip(p=0.5),
               Rotate(limit=20, p=0.3)], p=0.5),
        OneOf([
            ToGray(p=0.3),
            RandomBrightnessContrast(p=0.5),
            CLAHE(p=0.5),
            IAASharpen(p=0.45)
        ],
              p=0.5),
        RandomShadow(p=0.4),
        HueSaturationValue(p=0.3),
        Normalize(always_apply=True)
    ],
                   p=prob)
Beispiel #8
0
def augment_data(image, steering_angle):
    h, w = image.shape[:2]

    result_image = np.copy(image)
    result_steering_angle = steering_angle

    result_image, result_steering_angle = random_flip(result_image,
                                                      result_steering_angle)
    # result_image, result_steering_angle = random_translate(result_image, result_steering_angle)
    aug = Compose(
        [
            # GaussianBlur(blur_limit=5, p=0.5),
            RandomBrightness(limit=0.1, p=0.5),
            RandomContrast(limit=0.1, p=0.5),
            # CLAHE(p=0.5),
            # Equalize(p=0.5),
            # RandomSnow(p=0.5),
            # RandomRain(p=0.5),
            # RandomFog(p=0.5),
            # RandomSunFlare(p=0.5),
            RandomShadow(p=0.5),
            CoarseDropout(
                max_holes=4,
                max_height=h // 30,
                max_width=w // 30,
                min_holes=1,
                min_height=h // 40,
                min_width=w // 40,
                p=0.5,
            ),
        ],
        p=1,
    )
    result_image = aug(image=result_image)["image"]

    return result_image, result_steering_angle
Beispiel #9
0
    def __init__(self, to_tensor=True):
        augs = [
            LongestMaxSize(640),
            PadIfNeeded(640, 640, cv.BORDER_CONSTANT, value=0),
            #RandomSizedBBoxSafeCrop(height = 300,width = 300),
            RandomBrightness(p=0.5),
            RandomContrast(p=0.5),

            #RandomSunFlare(p=0.5, flare_roi=(0, 0, 1, 0.5), angle_lower=0.5,src_radius= 150),
            RandomShadow(p=0.5,
                         num_shadows_lower=1,
                         num_shadows_upper=1,
                         shadow_dimension=5,
                         shadow_roi=(0, 0.5, 1, 1)),
            HorizontalFlip(p=0.5),
            GaussianBlur(p=0.5),
        ]
        self.transform = Compose(augs,
                                 bbox_params={
                                     "format": "albumentations",
                                     "min_area": 0,
                                     "min_visibility": 0.2,
                                     'label_fields': ['category_id']
                                 })
Beispiel #10
0
def augmentation_hardcore(size_image, p=0.8):
    '''
    Only use for second model
    About albumentation, p in compose mean the prob that all transform in Compose work
    '''
    return Compose([
        Resize(size_image, size_image),
        CenterCrop(height=200, width=200, p=0.5),
        Cutout(),
        RandomShadow(shadow_dimension=3),
        OneOf([
            Flip(),
            VerticalFlip(),
            HorizontalFlip(),
        ], p=0.5),
        OneOf([
            RandomRotate90(),
            Transpose(),
        ], p=0.5),
        OneOf([GaussNoise(), GaussianBlur(blur_limit=9),
               Blur()], p=0.5),
        OneOf([
            HueSaturationValue(
                hue_shift_limit=10, sat_shift_limit=25, val_shift_limit=20),
            RGBShift(),
            RandomBrightness(brightness_limit=0.4),
            RandomContrast(),
            RandomBrightnessContrast(),
        ],
              p=0.5),
        OneOf([ShiftScaleRotate(),
               ElasticTransform(),
               RandomGridShuffle()],
              p=0.5)
    ],
                   p=p)
Beispiel #11
0
    def __init__(self, num_classes, input_size):
        super(TuSimpleDataTransform, self).__init__()

        random.seed(1000)
        height, width = input_size

        self._train_transform_list = self._train_transform_list + [
            HorizontalFlip(p=0.5),
            GaussNoise(p=0.5),
            RandomBrightnessContrast(p=0.5),
            RandomShadow(p=0.5),
            RandomRain(rain_type="drizzle", p=0.5),
            ShiftScaleRotate(rotate_limit=10, p=0.5),
            RandomResizedCrop(
                height=height, width=width, scale=(0.8, 1), p=0.5),
        ]

        self._train_transform_list.append(Resize(height, width))
        self._val_transform_list.append(Resize(height, width))

        self._train_transform_list.append(ToTensor(num_classes=num_classes))
        self._val_transform_list.append(ToTensor(num_classes=num_classes))

        self._initialize_transform_dict()
Beispiel #12
0
                                  p=0.5),
                ElasticTransform(p=0.5,
                                 alpha=1,
                                 sigma=50,
                                 alpha_affine=0.2,
                                 border_mode=cv2.BORDER_CONSTANT)
            ]),
            OneOf([
                CLAHE(),
                Solarize(),
                RandomBrightness(),
                RandomContrast(limit=0.2),
                RandomBrightnessContrast(),
            ]),
            JpegCompression(quality_lower=20, quality_upper=100),
            RandomShadow(num_shadows_lower=1, num_shadows_upper=2),
            PadIfNeeded(min_height=64,
                        min_width=100,
                        border_mode=cv2.BORDER_CONSTANT,
                        p=0.5),
            Cutout(num_holes=8, max_h_size=16, max_w_size=16),
            InvertImg(p=0.3),
            ToGray()
            #            GridDistortion(num_steps=10, distort_limit = (0, 0.1),  border_mode = cv2.BORDER_CONSTANT)
            #               OneOf([
            #                   GridDistortion(p=0.1),
            #                   IAAPiecewiseAffine(p=0.3),
            #               ], p=1),
        ])

        charset = open('../data/characters', 'r').read()
 def __call__(self, image, boxes=None, labels=None):
     image = RandomShadow(p=0.25)(image=image)['image']
     return image.astype(np.float32), boxes, labels
def transform(image, mask, image_name, mask_name):

    x, y = image, mask

    rand = random.uniform(0, 1)
    if (rand > 0.5):

        images_name = [f"{image_name}"]
        masks_name = [f"{mask_name}"]
        images_aug = [x]
        masks_aug = [y]

        it = iter(images_name)
        it2 = iter(images_aug)
        imagedict = dict(zip(it, it2))

        it = iter(masks_name)
        it2 = iter(masks_aug)
        masksdict = dict(zip(it, it2))

        return imagedict, masksdict

    mask_density = np.count_nonzero(y)

    ## Augmenting only images with Gloms
    if (mask_density > 0):
        try:
            h, w, c = x.shape
        except Exception as e:
            image = image[:-1]
            x, y = image, mask
            h, w, c = x.shape

        aug = Blur(p=1, blur_limit=3)
        augmented = aug(image=x, mask=y)
        x0 = augmented['image']
        y0 = augmented['mask']

        #    aug = CenterCrop(p=1, height=32, width=32)
        #    augmented = aug(image=x, mask=y)
        #    x1 = augmented['image']
        #    y1 = augmented['mask']

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

        aug = VerticalFlip(p=1)
        augmented = aug(image=x, mask=y)
        x3 = augmented['image']
        y3 = augmented['mask']

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

        aug = Transpose(p=1)
        augmented = aug(image=x, mask=y)
        x5 = augmented['image']
        y5 = augmented['mask']

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

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

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

        aug = RandomGridShuffle(p=1)
        augmented = aug(image=x, mask=y)
        x9 = augmented['image']
        y9 = augmented['mask']

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

        #        aug = PadIfNeeded(p=1)
        #        augmented = aug(image=x, mask=y)
        #        x11 = augmented['image']
        #        y11 = augmented['mask']

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

        ## Random Brightness
        aug = RandomBrightness(p=1)
        augmented = aug(image=x, mask=y)
        x13 = augmented['image']
        y13 = augmented['mask']

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

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

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

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

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

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

        aug = CLAHE(clip_limit=1.0,
                    tile_grid_size=(8, 8),
                    always_apply=False,
                    p=1)
        augmented = aug(image=x, mask=y)
        x20 = augmented['image']
        y20 = augmented['mask']

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

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

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

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

        aug = ImageCompression(p=1)
        augmented = aug(image=x, mask=y)
        x25 = augmented['image']
        y25 = augmented['mask']

        aug = Cutout(p=1)
        augmented = aug(image=x, mask=y)
        x26 = augmented['image']
        y26 = augmented['mask']

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

        #       aug = ToFloat(p=1)
        #       augmented = aug(image=x, mask=y)
        #       x28 = augmented['image']
        #       y28 = augmented['mask']

        aug = FromFloat(p=1)
        augmented = aug(image=x, mask=y)
        x29 = augmented['image']
        y29 = augmented['mask']

        ## Random Brightness and Contrast
        aug = RandomBrightnessContrast(p=1)
        augmented = aug(image=x, mask=y)
        x30 = augmented['image']
        y30 = augmented['mask']

        aug = RandomSnow(p=1)
        augmented = aug(image=x, mask=y)
        x31 = augmented['image']
        y31 = augmented['mask']

        aug = RandomRain(p=1)
        augmented = aug(image=x, mask=y)
        x32 = augmented['image']
        y32 = augmented['mask']

        aug = RandomFog(p=1)
        augmented = aug(image=x, mask=y)
        x33 = augmented['image']
        y33 = augmented['mask']

        aug = RandomSunFlare(p=1)
        augmented = aug(image=x, mask=y)
        x34 = augmented['image']
        y34 = augmented['mask']

        aug = RandomShadow(p=1)
        augmented = aug(image=x, mask=y)
        x35 = augmented['image']
        y35 = augmented['mask']

        aug = Lambda(p=1)
        augmented = aug(image=x, mask=y)
        x36 = augmented['image']
        y36 = augmented['mask']

        aug = ChannelDropout(p=1)
        augmented = aug(image=x, mask=y)
        x37 = augmented['image']
        y37 = augmented['mask']

        aug = ISONoise(p=1)
        augmented = aug(image=x, mask=y)
        x38 = augmented['image']
        y38 = augmented['mask']

        aug = Solarize(p=1)
        augmented = aug(image=x, mask=y)
        x39 = augmented['image']
        y39 = augmented['mask']

        aug = Equalize(p=1)
        augmented = aug(image=x, mask=y)
        x40 = augmented['image']
        y40 = augmented['mask']

        aug = Posterize(p=1)
        augmented = aug(image=x, mask=y)
        x41 = augmented['image']
        y41 = augmented['mask']

        aug = Downscale(p=1)
        augmented = aug(image=x, mask=y)
        x42 = augmented['image']
        y42 = augmented['mask']

        aug = MultiplicativeNoise(p=1)
        augmented = aug(image=x, mask=y)
        x43 = augmented['image']
        y43 = augmented['mask']

        aug = FancyPCA(p=1)
        augmented = aug(image=x, mask=y)
        x44 = augmented['image']
        y44 = augmented['mask']

        #       aug = MaskDropout(p=1)
        #       augmented = aug(image=x, mask=y)
        #       x45 = augmented['image']
        #       y45 = augmented['mask']

        aug = GridDropout(p=1)
        augmented = aug(image=x, mask=y)
        x46 = augmented['image']
        y46 = augmented['mask']

        aug = ColorJitter(p=1)
        augmented = aug(image=x, mask=y)
        x47 = augmented['image']
        y47 = augmented['mask']

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

        aug = CropNonEmptyMaskIfExists(p=1, height=22, width=32)
        augmented = aug(image=x, mask=y)
        x51 = augmented['image']
        y51 = augmented['mask']

        aug = IAAAffine(p=1)
        augmented = aug(image=x, mask=y)
        x52 = augmented['image']
        y52 = augmented['mask']

        #        aug = IAACropAndPad(p=1)
        #        augmented = aug(image=x, mask=y)
        #        x53 = augmented['image']
        #        y53 = augmented['mask']

        aug = IAAFliplr(p=1)
        augmented = aug(image=x, mask=y)
        x54 = augmented['image']
        y54 = augmented['mask']

        aug = IAAFlipud(p=1)
        augmented = aug(image=x, mask=y)
        x55 = augmented['image']
        y55 = augmented['mask']

        aug = IAAPerspective(p=1)
        augmented = aug(image=x, mask=y)
        x56 = augmented['image']
        y56 = augmented['mask']

        aug = IAAPiecewiseAffine(p=1)
        augmented = aug(image=x, mask=y)
        x57 = augmented['image']
        y57 = augmented['mask']

        aug = LongestMaxSize(p=1)
        augmented = aug(image=x, mask=y)
        x58 = augmented['image']
        y58 = augmented['mask']

        aug = NoOp(p=1)
        augmented = aug(image=x, mask=y)
        x59 = augmented['image']
        y59 = augmented['mask']

        #       aug = RandomCrop(p=1, height=22, width=22)
        #       augmented = aug(image=x, mask=y)
        #       x61 = augmented['image']
        #       y61 = augmented['mask']

        #      aug = RandomResizedCrop(p=1, height=22, width=20)
        #      augmented = aug(image=x, mask=y)
        #      x63 = augmented['image']
        #      y63 = augmented['mask']

        aug = RandomScale(p=1)
        augmented = aug(image=x, mask=y)
        x64 = augmented['image']
        y64 = augmented['mask']

        #      aug = RandomSizedCrop(p=1, height=22, width=20, min_max_height = [32,32])
        #      augmented = aug(image=x, mask=y)
        #      x66 = augmented['image']
        #      y66 = augmented['mask']

        #      aug = Resize(p=1, height=22, width=20)
        #      augmented = aug(image=x, mask=y)
        #      x67 = augmented['image']
        #      y67 = augmented['mask']

        aug = Rotate(p=1)
        augmented = aug(image=x, mask=y)
        x68 = augmented['image']
        y68 = augmented['mask']

        aug = ShiftScaleRotate(p=1)
        augmented = aug(image=x, mask=y)
        x69 = augmented['image']
        y69 = augmented['mask']

        aug = SmallestMaxSize(p=1)
        augmented = aug(image=x, mask=y)
        x70 = augmented['image']
        y70 = augmented['mask']

        images_aug.extend([
            x, x0, x2, x3, x5, x6, x7, x8, x9, x10, x12, x13, x14, x16, x17,
            x18, x19, x20, x21, x22, x23, x24, x25, x26, x29, x30, x31, x32,
            x33, x34, x35, x36, x37, x38, x39, x40, x41, x42, x43, x44, x46,
            x47, x50, x51, x52, x54, x55, x56, x57, x58, x59, x64, x68, x69,
            x70
        ])

        masks_aug.extend([
            y, y0, y2, y3, y5, y6, y7, y8, y9, y10, y12, y13, y14, y16, y17,
            y18, y19, y20, y21, y22, y23, y24, y25, y26, y29, y30, y31, y32,
            y33, y34, y35, y36, y37, y38, y39, y40, y41, y42, y43, y44, y46,
            y47, y50, y51, y52, y54, y55, y56, y57, y58, y59, y64, y68, y69,
            y70
        ])

        idx = -1
        images_name = []
        masks_name = []
        for i, m in zip(images_aug, masks_aug):
            if idx == -1:
                tmp_image_name = f"{image_name}"
                tmp_mask_name = f"{mask_name}"
            else:
                tmp_image_name = f"{image_name}_{smalllist[idx]}"
                tmp_mask_name = f"{mask_name}_{smalllist[idx]}"
            images_name.extend(tmp_image_name)
            masks_name.extend(tmp_mask_name)
            idx += 1

        it = iter(images_name)
        it2 = iter(images_aug)
        imagedict = dict(zip(it, it2))

        it = iter(masks_name)
        it2 = iter(masks_aug)
        masksdict = dict(zip(it, it2))

    return imagedict, masksdict
Beispiel #15
0
    elif augmentation == 'iso_noise':
        transform = ISONoise(always_apply=True, color_shift=(0.08, 0.1), 
                                                intensity=(0.5, 0.8))
        transformed_image = transform(image=image)['image']

    elif augmentation == 'shot_noise':
        transform = iaa.imgcorruptlike.ShotNoise(severity=2)
        transformed_image = transform(image=image)
    
    elif augmentation == 'speckle_noise':
        transform = iaa.imgcorruptlike.SpeckleNoise(severity=2)
        transformed_image = transform(image=image)
    
    elif augmentation == 'random_shadow':
        transform = RandomShadow(always_apply=True)
        transformed_image = transform(image=image)['image']

    elif augmentation == 'random_sun_flare':
        transform = RandomSunFlare(always_apply=True)
        transformed_image = transform(image=image)['image']
        
    elif augmentation == 'spatter':
        transform = iaa.imgcorruptlike.Spatter(severity=2)
        transformed_image = transform(image=image)
    
    ## Edges

    elif augmentation == 'canny':
        transform = iaa.Canny(alpha=(0.0, 0.9))
        transformed_image = transform(image=image)
    def __init__(self, opt):
        #        super(Batch_Balanced_Dataset, self).__init__()
        self.tbaug = TenBrightAug()
        self.incbaug = IncBrightAug()
        self.colaug = ColorAug()
        self.distortaug = DistortAug()
        self.grayimg = GrayImg()
        self.binimg = BinImg()
        self.jpgaug = JPEGAug(0.8)
        self.resizeimg = ResizeAug(opt.imgW, opt.imgH)
        self.resizeimg_test = ResizeAug(opt.imgW, opt.imgH, rand_scale=False)
        l1 = len(opt.train_data.strip().split(','))
        if l1 == 1:
            self.batch_sizes = [int(opt.batch_size)]
        elif l1 == 3:
            self.batch_sizes = [
                int(opt.batch_size * opt.batch_ratio),
                opt.batch_size - int(opt.batch_size * opt.batch_ratio)
            ]
        elif l1 == 4:
            b1 = int(opt.batch_size * opt.batch_ratio2)
            self.batch_sizes = [int(b1 * opt.batch_ratio), 0, 0]
            self.batch_sizes[1] = b1 - self.batch_sizes[0]
            self.batch_sizes[
                2] = opt.batch_size - self.batch_sizes[0] - self.batch_sizes[1]

        if not opt.alldata:
            self.test_books = [
                '200021660', '200005598', 'hnsd006', 'umgy003', '100249416',
                'umgy011', 'umgy010'
            ]
        else:
            print('use all data')
            self.test_books = [
                #                '200021660',
                #               '200005598',
                'hnsd006',
                'umgy003',
                '100249416',
                'umgy011',
                'umgy010'
            ]

        self.train_tf1 = transforms.Compose([
            self.distortaug, self.colaug, self.tbaug, self.incbaug,
            self.grayimg, self.binimg, self.tbaug, self.incbaug, self.jpgaug,
            self.resizeimg,
            transforms.ToTensor()
        ])

        self.train_tf0 = transforms.Compose(
            [self.grayimg, self.resizeimg,
             transforms.ToTensor()])

        self.train_tf00 = Compose([
            OneOf([
                OpticalDistortion(distort_limit=0.05,
                                  shift_limit=10,
                                  border_mode=cv2.BORDER_WRAP,
                                  p=0.5),
                ElasticTransform(p=0.5,
                                 alpha=1,
                                 sigma=50,
                                 alpha_affine=0.2,
                                 border_mode=cv2.BORDER_CONSTANT)
            ]),
            OneOf([
                CLAHE(),
                Solarize(),
                RandomBrightness(),
                RandomContrast(limit=0.2),
                RandomBrightnessContrast(),
            ]),
            JpegCompression(quality_lower=20, quality_upper=100),
            RandomShadow(num_shadows_lower=1, num_shadows_upper=2),
            PadIfNeeded(min_height=64,
                        min_width=100,
                        border_mode=cv2.BORDER_CONSTANT,
                        p=0.5),
            Cutout(num_holes=8, max_h_size=16, max_w_size=16),
            InvertImg(p=0.3),
            ToGray()
        ])

        self.train_tf01 = Compose([
            JpegCompression(quality_lower=20, quality_upper=100),
            Cutout(num_holes=4, max_h_size=8, max_w_size=8),
            InvertImg(p=0.3)
        ])

        self.randtf = transforms.RandomApply([self.distortaug])

        self.train_tf2 = transforms.Compose(
            [self.resizeimg_test, transforms.ToTensor()])

        self.data_loader_list = []
        self.datasets = []
        self.data_samplers = []
        self.opt = opt

        self.train_data = opt.train_data.strip().split(',')

        if not 'txt' in self.train_data[0]:
            self.datasets.append(
                RealImageLMDB(self.train_data[0],
                              transform=self.train_tf0,
                              transform2=self.train_tf00,
                              testBooks=self.test_books,
                              character=opt.character,
                              max_batch_length=opt.batch_max_length))
        else:
            self.datasets.append(
                RealImageTxtLMDB(self.train_data[0],
                                 transform=self.train_tf0,
                                 transform2=self.train_tf00,
                                 testBooks=self.test_books,
                                 max_batch_length=opt.batch_max_length))

        if len(self.train_data) == 3:
            self.datasets.append(
                SynthImageLMDB(self.train_data[1],
                               self.train_data[2],
                               transform=self.train_tf2,
                               transform2=self.train_tf01,
                               size=(opt.imgH, opt.imgW),
                               gray_bin_ratio=0.5,
                               max_label_length=opt.batch_max_length))

        if len(self.train_data) == 4:
            self.datasets.append(
                RealImageTxtLMDB(self.train_data[3],
                                 transform=self.train_tf0,
                                 transform2=self.train_tf00,
                                 testBooks=self.test_books,
                                 max_batch_length=opt.batch_max_length))

        for i in range(len(self.datasets)):
            train_sampler = torch.utils.data.distributed.DistributedSampler(
                self.datasets[i])
            self.data_samplers.append(train_sampler)
            self.data_loader_list.append(
                DataLoader(self.datasets[i],
                           num_workers=int(opt.workers),
                           shuffle=False,
                           sampler=train_sampler,
                           batch_size=self.batch_sizes[i],
                           collate_fn=collate_fn,
                           pin_memory=True,
                           drop_last=True))

        self.dataloader_iter_list = [iter(i) for i in self.data_loader_list]

        self.test_tf = transforms.Compose(
            [self.grayimg, self.resizeimg_test,
             transforms.ToTensor()])

        self.test_dataset = RealImageLMDB(self.train_data[0],
                                          self.test_tf,
                                          testBooks=self.test_books,
                                          isTest=True,
                                          character=opt.character)
        self.test_sampler = torch.utils.data.distributed.DistributedSampler(
            self.test_dataset)
        self.test_loader = DataLoader(self.test_dataset,
                                      num_workers=int(opt.workers),
                                      shuffle=False,
                                      sampler=self.test_sampler,
                                      batch_size=max(2,
                                                     int(opt.batch_size / 8)),
                                      collate_fn=collate_fn,
                                      drop_last=True)
def main():
    dt_config = Config()
    dt_config.display()

    train_aug = Compose(
        [
            GaussNoise(p=1.0),
            RandomShadow(p=0.5),
            RandomRain(p=0.5, rain_type="drizzle"),
            RandomContrast(limit=0.2, p=0.5),
            RandomGamma(gamma_limit=(80, 120), p=0.5),
            RandomBrightness(limit=0.2, p=0.5),
            HueSaturationValue(
                hue_shift_limit=5, sat_shift_limit=20, val_shift_limit=10, p=0.5
            ),
            CLAHE(p=0.5, clip_limit=2.0),
            Normalize(p=1.0),
        ]
    )

    val_aug = Compose([Normalize(p=1.0)])
    train_set = KittiStixelDataset(
        data_path=dt_config.DATA_PATH,
        ground_truth_path=dt_config.GROUND_TRUTH_PATH,
        batch_size=parsed_args.batch_size,
        phase="train",
        transform=train_aug,
        customized_transform=utility.HorizontalFlip(p=0.5),
    )

    val_set = KittiStixelDataset(
        data_path=dt_config.DATA_PATH,
        ground_truth_path=dt_config.GROUND_TRUTH_PATH,
        phase="val",
        transform=val_aug,
    )

    model = build_stixel_net()
    loss_func = StixelLoss()
    opt = optimizers.Adam(0.0001)
    callbacks = [
        ModelCheckpoint(
            os.path.join(dt_config.SAVED_MODELS_PATH, "model-{epoch:03d}.h5"),
            monitor="val_loss",
            verbose=1,
            save_best_only=True,
            mode="auto",
            period=1,
        ),
        ReduceLROnPlateau(
            monitor="val_loss",
            factor=0.1,
            patience=7,
            verbose=0,
            mode="auto",
            min_lr=0.000001,
        ),
        EarlyStopping(
            monitor="val_loss", min_delta=0, patience=10, verbose=0, mode="auto"
        ),
    ]

    model.compile(loss=loss_func, optimizer=opt)
    model.summary()

    history = model.fit_generator(
        train_set,
        steps_per_epoch=len(train_set),
        validation_data=val_set,
        validation_steps=len(val_set),
        epochs=parsed_args.num_epoch,
        callbacks=callbacks,
        shuffle=True,
    )

    history_path = os.path.join(dt_config.SAVED_MODELS_PATH, "history.pkl")
    np.save(history_path, history.history)
Beispiel #18
0
def ImageAugument():
    imgs_save_dir = 'data/albu_imgs/'
    if not os.path.exists(imgs_save_dir):
        os.makedirs(imgs_save_dir)
    xmls_save_dir = 'data/albu_xmls/'
    if not os.path.exists(xmls_save_dir):
        os.makedirs(xmls_save_dir)
    path = "data/img"  # 文件夹目录
    xml_path = "data/xml"
    files = os.listdir(path)  # 得到文件夹下的所有文件名称
    # 遍历文件夹
    prefix = path + '/'
    print("begin>>>")
    for file in tqdm(files):
        image = cv2.imread(prefix + file)
        # cv2.imwrite("origin.jpg",image)
        xml = xml_path + "/" + file[:-4] + ".xml"

        #示例:使用具有随机孔径线性大小的中值滤波器来模糊输入图像
        aug = MedianBlur(p=1)

        aug_image = aug(image=image)['image']
        cv2.imwrite(imgs_save_dir + file[:-4] + 'mb' + '.jpg', aug_image)
        new_name = xmls_save_dir + "/" + file[:-4] + "mb" + ".xml"  # 为文件赋予新名字
        shutil.copyfile(xml, new_name)

        #随机大小的内核模糊输入图像
        aug = Blur(p=1)

        aug_image = aug(image=image)['image']
        cv2.imwrite(imgs_save_dir + file[:-4] + 'blur' + '.jpg', aug_image)
        new_name = xmls_save_dir + "/" + file[:-4] + "blur" + ".xml"  # 为文件赋予新名字
        shutil.copyfile(xml, new_name)

        #高斯模糊
        aug = GaussNoise(p=1)

        aug_image = aug(image=image)['image']
        cv2.imwrite(imgs_save_dir + file[:-4] + 'gau' + '.jpg', aug_image)
        new_name = xmls_save_dir + "/" + file[:-4] + "gau" + ".xml"  # 为文件赋予新名字
        shutil.copyfile(xml, new_name)

        #随机雨
        aug = RandomRain(p=1)

        aug_image = aug(image=image)['image']
        cv2.imwrite(imgs_save_dir + file[:-4] + 'rain' + '.jpg', aug_image)
        new_name = xmls_save_dir + "/" + file[:-4] + "rain" + ".xml"  # 为文件赋予新名字
        shutil.copyfile(xml, new_name)

        #随机雾
        aug = RandomFog(fog_coef_lower=0.2,
                        fog_coef_upper=0.5,
                        alpha_coef=0.1,
                        p=1)

        aug_image = aug(image=image)['image']
        cv2.imwrite(imgs_save_dir + file[:-4] + 'fog' + '.jpg', aug_image)
        new_name = xmls_save_dir + "/" + file[:-4] + "fog" + ".xml"  # 为文件赋予新名字
        shutil.copyfile(xml, new_name)

        #太阳耀斑RandomSunFlare
        aug = RandomSunFlare(p=1)

        aug_image = aug(image=image)['image']
        cv2.imwrite(imgs_save_dir + file[:-4] + 'sun' + '.jpg', aug_image)
        new_name = xmls_save_dir + "/" + file[:-4] + "sun" + ".xml"  # 为文件赋予新名字
        shutil.copyfile(xml, new_name)

        #阴影RandomShadow
        aug = RandomShadow(p=1)

        aug_image = aug(image=image)['image']
        cv2.imwrite(imgs_save_dir + file[:-4] + 'shadow' + '.jpg', aug_image)
        new_name = xmls_save_dir + "/" + file[:-4] + "shadow" + ".xml"  # 为文件赋予新名字
        shutil.copyfile(xml, new_name)

        #随机雪RandomSnow
        aug = RandomSnow(p=1)

        aug_image = aug(image=image)['image']
        cv2.imwrite(imgs_save_dir + file[:-4] + 'snow' + '.jpg', aug_image)
        new_name = xmls_save_dir + "/" + file[:-4] + "snow" + ".xml"  # 为文件赋予新名字
        shutil.copyfile(xml, new_name)

        #随机CoarseDropout
        aug = CoarseDropout(p=1)

        aug_image = aug(image=image)['image']
        cv2.imwrite(imgs_save_dir + file[:-4] + 'drop' + '.jpg', aug_image)
        new_name = xmls_save_dir + "/" + file[:-4] + "drop" + ".xml"  # 为文件赋予新名字
        shutil.copyfile(xml, new_name)

        #随机cutout
        aug = Cutout(p=1)

        aug_image = aug(image=image)['image']
        cv2.imwrite(imgs_save_dir + file[:-4] + 'cut' + '.jpg', aug_image)
        new_name = xmls_save_dir + "/" + file[:-4] + "cut" + ".xml"  # 为文件赋予新名字
        shutil.copyfile(xml, new_name)

    print("Done")