def test_transform_pipeline_serialization_with_bboxes(seed, image, bboxes,
                                                      bbox_format, labels):
    aug = A.Compose(
        [
            A.OneOrOther(
                A.Compose([
                    A.RandomRotate90(),
                    A.OneOf([A.HorizontalFlip(p=0.5),
                             A.VerticalFlip(p=0.5)])
                ]),
                A.Compose([
                    A.Rotate(p=0.5),
                    A.OneOf([A.HueSaturationValue(p=0.5),
                             A.RGBShift(p=0.7)],
                            p=1)
                ]),
            ),
            A.HorizontalFlip(p=1),
            A.RandomBrightnessContrast(p=0.5),
        ],
        bbox_params={
            "format": bbox_format,
            "label_fields": ["labels"]
        },
    )
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    set_seed(seed)
    aug_data = aug(image=image, bboxes=bboxes, labels=labels)
    set_seed(seed)
    deserialized_aug_data = deserialized_aug(image=image,
                                             bboxes=bboxes,
                                             labels=labels)
    assert np.array_equal(aug_data["image"], deserialized_aug_data["image"])
    assert np.array_equal(aug_data["bboxes"], deserialized_aug_data["bboxes"])
Ejemplo n.º 2
0
def test_transform_pipeline_serialization(seed, image, mask):
    aug = A.Compose([
        A.OneOrOther(
            A.Compose([
                A.Resize(1024, 1024),
                A.RandomSizedCrop(min_max_height=(256, 1024), height=512, width=512, p=1),
                A.OneOf([
                    A.RandomSizedCrop(min_max_height=(256, 512), height=384, width=384, p=0.5),
                    A.RandomSizedCrop(min_max_height=(256, 512), height=512, width=512, p=0.5),
                ])
            ]),
            A.Compose([
                A.Resize(1024, 1024),
                A.RandomSizedCrop(min_max_height=(256, 1025), height=256, width=256, p=1),
                A.OneOf([
                    A.HueSaturationValue(p=0.5),
                    A.RGBShift(p=0.7)
                ], p=1),
            ])
        ),
        A.HorizontalFlip(p=1),
        A.RandomBrightnessContrast(p=0.5)
    ])
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    random.seed(seed)
    aug_data = aug(image=image, mask=mask)
    random.seed(seed)
    deserialized_aug_data = deserialized_aug(image=image, mask=mask)
    assert np.array_equal(aug_data['image'], deserialized_aug_data['image'])
    assert np.array_equal(aug_data['mask'], deserialized_aug_data['mask'])
Ejemplo n.º 3
0
def test_lambda_serialization(image, mask, bboxes, keypoints, seed, p):

    def vflip_image(image, **kwargs):
        return F.vflip(image)

    def vflip_mask(mask, **kwargs):
        return F.vflip(mask)

    def vflip_bbox(bbox, **kwargs):
        return F.bbox_vflip(bbox, **kwargs)

    def vflip_keypoint(keypoint, **kwargs):
        return F.keypoint_vflip(keypoint, **kwargs)

    aug = A.Lambda(name='vflip', image=vflip_image, mask=vflip_mask, bbox=vflip_bbox, keypoint=vflip_keypoint, p=p)

    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug, lambda_transforms={'vflip': aug})
    random.seed(seed)
    aug_data = aug(image=image, mask=mask, bboxes=bboxes, keypoints=keypoints)
    random.seed(seed)
    deserialized_aug_data = deserialized_aug(image=image, mask=mask, bboxes=bboxes, keypoints=keypoints)
    assert np.array_equal(aug_data['image'], deserialized_aug_data['image'])
    assert np.array_equal(aug_data['mask'], deserialized_aug_data['mask'])
    assert np.array_equal(aug_data['bboxes'], deserialized_aug_data['bboxes'])
    assert np.array_equal(aug_data['keypoints'], deserialized_aug_data['keypoints'])
Ejemplo n.º 4
0
def test_transform_pipeline_serialization_with_keypoints(seed, image, keypoints, keypoint_format, labels):
    aug = A.Compose([
        A.OneOrOther(
            A.Compose([
                A.RandomRotate90(),
                A.OneOf([
                    A.HorizontalFlip(p=0.5),
                    A.VerticalFlip(p=0.5),
                ])
            ]),
            A.Compose([
                A.Rotate(p=0.5),
                A.OneOf([
                    A.HueSaturationValue(p=0.5),
                    A.RGBShift(p=0.7)
                ], p=1),
            ])
        ),
        A.HorizontalFlip(p=1),
        A.RandomBrightnessContrast(p=0.5)
    ], keypoint_params={'format': keypoint_format, 'label_fields': ['labels']})
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    random.seed(seed)
    aug_data = aug(image=image, keypoints=keypoints, labels=labels)
    random.seed(seed)
    deserialized_aug_data = deserialized_aug(image=image, keypoints=keypoints, labels=labels)
    assert np.array_equal(aug_data['image'], deserialized_aug_data['image'])
    assert np.array_equal(aug_data['keypoints'], deserialized_aug_data['keypoints'])
def test_from_float_serialization(float_image):
    aug = A.FromFloat(p=1, dtype="uint8")
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    aug_data = aug(image=float_image)
    deserialized_aug_data = deserialized_aug(image=float_image)
    assert np.array_equal(aug_data["image"], deserialized_aug_data["image"])
def test_lambda_serialization(image, mask, albumentations_bboxes, keypoints, seed, p):
    def vflip_image(image, **kwargs):
        return F.vflip(image)

    def vflip_mask(mask, **kwargs):
        return F.vflip(mask)

    def vflip_bbox(bbox, **kwargs):
        return F.bbox_vflip(bbox, **kwargs)

    def vflip_keypoint(keypoint, **kwargs):
        return F.keypoint_vflip(keypoint, **kwargs)

    aug = A.Lambda(name="vflip", image=vflip_image, mask=vflip_mask, bbox=vflip_bbox, keypoint=vflip_keypoint, p=p)

    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug, lambda_transforms={"vflip": aug})
    set_seed(seed)
    aug_data = aug(image=image, mask=mask, bboxes=albumentations_bboxes, keypoints=keypoints)
    set_seed(seed)
    deserialized_aug_data = deserialized_aug(image=image, mask=mask, bboxes=albumentations_bboxes, keypoints=keypoints)
    assert np.array_equal(aug_data["image"], deserialized_aug_data["image"])
    assert np.array_equal(aug_data["mask"], deserialized_aug_data["mask"])
    assert np.array_equal(aug_data["bboxes"], deserialized_aug_data["bboxes"])
    assert np.array_equal(aug_data["keypoints"], deserialized_aug_data["keypoints"])
def test_subtract_is_serializable(image):
    aug = Subtract(subtract_value=10)
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    set_seed(42)
    aug_data = aug.apply(image)
    set_seed(42)
    deserialized_aug_data = deserialized_aug.apply(image)
    assert np.array_equal(aug_data, deserialized_aug_data)
    def gen_json(self):
        transform = self._get_transform_from_code()
        if transform is None:
            self.textBrowser_json.setText('')

        data = albumentations.to_dict(transform, on_not_implemented_error='warn')
        data = json.dumps(data, indent=' ' * 4)

        self.textBrowser_json.setText(data)
Ejemplo n.º 9
0
def test_augmentations_for_keypoints_serialization(augmentation_cls, params, p, seed, image, keypoints, always_apply):
    aug = augmentation_cls(p=p, always_apply=always_apply, **params)
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    random.seed(seed)
    aug_data = aug(image=image, keypoints=keypoints)
    random.seed(seed)
    deserialized_aug_data = deserialized_aug(image=image, keypoints=keypoints)
    assert np.array_equal(aug_data['image'], deserialized_aug_data['image'])
    assert np.array_equal(aug_data['keypoints'], deserialized_aug_data['keypoints'])
Ejemplo n.º 10
0
def test_image_only_crop_around_bbox_augmentation_serialization(p, seed, image, always_apply):
    aug = A.RandomCropNearBBox(p=p, always_apply=always_apply, max_part_shift=0.15)
    annotations = {'image': image, 'cropping_bbox': [-59, 77, 177, 231]}
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    random.seed(seed)
    aug_data = aug(**annotations)
    random.seed(seed)
    deserialized_aug_data = deserialized_aug(**annotations)
    assert np.array_equal(aug_data['image'], deserialized_aug_data['image'])
Ejemplo n.º 11
0
def test_augmentations_serialization(augmentation_cls, params, p, seed, image, mask, always_apply):
    aug = augmentation_cls(p=p, always_apply=always_apply, **params)
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    set_seed(seed)
    aug_data = aug(image=image, mask=mask)
    set_seed(seed)
    deserialized_aug_data = deserialized_aug(image=image, mask=mask)
    assert np.array_equal(aug_data["image"], deserialized_aug_data["image"])
    assert np.array_equal(aug_data["mask"], deserialized_aug_data["mask"])
Ejemplo n.º 12
0
def test_serialization_v2_to_dict():
    transform = A.Compose([A.HorizontalFlip()])
    transform_dict = A.to_dict(transform)["transform"]
    assert transform_dict == {
        "__class_fullname__": "Compose",
        "p": 1.0,
        "transforms": [{"__class_fullname__": "HorizontalFlip", "always_apply": False, "p": 0.5}],
        "bbox_params": None,
        "keypoint_params": None,
        "additional_targets": {},
    }
Ejemplo n.º 13
0
def test_additional_targets_for_image_only_serialization(augmentation_cls, params, image, seed):
    aug = A.Compose([augmentation_cls(always_apply=True, **params)], additional_targets={"image2": "image"})
    image2 = image.copy()

    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    set_seed(seed)
    aug_data = aug(image=image, image2=image2)
    set_seed(seed)
    deserialized_aug_data = deserialized_aug(image=image, image2=image2)
    assert np.array_equal(aug_data["image"], deserialized_aug_data["image"])
    assert np.array_equal(aug_data["image2"], deserialized_aug_data["image2"])
Ejemplo n.º 14
0
def test_augmentations_serialization_with_call_params(
    augmentation_cls, params, call_params, p, seed, image, always_apply
):
    aug = augmentation_cls(p=p, always_apply=always_apply, **params)
    annotations = {"image": image, **call_params}
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    set_seed(seed)
    aug_data = aug(**annotations)
    set_seed(seed)
    deserialized_aug_data = deserialized_aug(**annotations)
    assert np.array_equal(aug_data["image"], deserialized_aug_data["image"])
Ejemplo n.º 15
0
def test_augmentations_for_bboxes_serialization(
    augmentation_cls, params, p, seed, image, albumentations_bboxes, always_apply
):
    aug = augmentation_cls(p=p, always_apply=always_apply, **params)
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    set_seed(seed)
    aug_data = aug(image=image, bboxes=albumentations_bboxes)
    set_seed(seed)
    deserialized_aug_data = deserialized_aug(image=image, bboxes=albumentations_bboxes)
    assert np.array_equal(aug_data["image"], deserialized_aug_data["image"])
    assert np.array_equal(aug_data["bboxes"], deserialized_aug_data["bboxes"])
Ejemplo n.º 16
0
def test_imgaug_augmentations_for_bboxes_serialization(augmentation_cls, params, p, seed, image, bboxes, always_apply):
    aug = augmentation_cls(p=p, always_apply=always_apply, **params)
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    random.seed(seed)
    ia.seed(seed)
    aug_data = aug(image=image, bboxes=bboxes)
    random.seed(seed)
    ia.seed(seed)
    deserialized_aug_data = deserialized_aug(image=image, bboxes=bboxes)
    assert np.array_equal(aug_data['image'], deserialized_aug_data['image'])
    assert np.array_equal(aug_data['bboxes'], deserialized_aug_data['bboxes'])
    def gen_yaml(self):
        transform = self._get_transform_from_code()
        if transform is None:
            self.textBrowser_json.setText('')

        data = albumentations.to_dict(transform, on_not_implemented_error='warn')
        with io.StringIO() as stream:
            yaml.safe_dump(data, stream)

            stream.seek(0)
            data = stream.read()

        self.textBrowser_yaml.setText(data)
Ejemplo n.º 18
0
class PlotOptions(Config):
    """Config related to plotting."""
    transform: Optional[dict] = Field(
        A.to_dict(MinMaxNormalize()),
        description='An Albumentations transform serialized as a dict that '
        'will be applied to each image before it is plotted. Mainly useful '
        'for undoing any data transformation that you do not want included in '
        'the plot, such as normalization. The default value will shift and scale the '
        'image so the values range from 0.0 to 1.0 which is the expected range for '
        'the plotting function. This default is useful for cases where the values after '
        'normalization are close to zero which makes the plot difficult to see.'
    )

    # validators
    _tf = validator(
        'transform', allow_reuse=True)(validate_albumentation_transform)
Ejemplo n.º 19
0
def test_template_transform_serialization(image, template, seed, p):
    template_transform = A.TemplateTransform(name="template",
                                             templates=template,
                                             p=p)

    aug = A.Compose([A.Flip(), template_transform, A.Blur()])

    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(
        serialized_aug, lambda_transforms={"template": template_transform})

    set_seed(seed)
    aug_data = aug(image=image)
    set_seed(seed)
    deserialized_aug_data = deserialized_aug(image=image)

    assert np.array_equal(aug_data["image"], deserialized_aug_data["image"])
Ejemplo n.º 20
0
def test_augmentations_serialization_with_custom_parameters(
    augmentation_cls,
    params,
    p,
    seed,
    image,
    mask,
    always_apply,
):
    aug = augmentation_cls(p=p, always_apply=always_apply, **params)
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    random.seed(seed)
    aug_data = aug(image=image, mask=mask)
    random.seed(seed)
    deserialized_aug_data = deserialized_aug(image=image, mask=mask)
    assert np.array_equal(aug_data['image'], deserialized_aug_data['image'])
    assert np.array_equal(aug_data['mask'], deserialized_aug_data['mask'])
Ejemplo n.º 21
0
def main():
    image = cv2.imread("cuiyan.png")
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    def visualize(image):
        plt.figure(figsize=(6, 6))
        plt.axis("off")
        plt.imshow(image)
        # plt.show()

    transform = A.Compose([
        A.RandomCrop(111, 222),
        A.OneOf([A.RGBShift(), A.HueSaturationValue()]),
    ])

    random.seed(42)
    transformed = transform(image=image)
    visualize(transformed["image"])

    A.save(transform, "./transform.json")
    A.save(transform, "./transform.yml", data_format="yaml")
    pprint.pprint(A.to_dict(transform))
Ejemplo n.º 22
0
    def state_dict(self, epoch):
        state = {
            'epoch': epoch,
            'state_dict': self.model.module.state_dict(),
            'optimizer': self.optimizer.state_dict(),
            'score_metric': self.best_metric,
            'test_transforms': A.to_dict(self.test_transforms),
            'transform': self.transform_args,
            'model_params': self.model_params,
            'class_order': self.classes,
            'final_output': self.final_output,
        }

        if self.lr_scheduler is not None:
            state['lr_scheduler'] = self.lr_scheduler.state_dict()

        if self.plateau_scheduler is not None:
            state['plateau_scheduler'] = self.plateau_scheduler.state_dict()

        if self.stopper is not None:
            state['stopper'] = self.stopper.state_dict()

        return state
Ejemplo n.º 23
0
def get_config(runner,
               raw_uri,
               processed_uri,
               root_uri,
               test=False,
               external_model=False,
               external_loss=False,
               augment=False):
    debug = False
    train_scene_info = get_scene_info(join(processed_uri, 'train-scenes.csv'))
    val_scene_info = get_scene_info(join(processed_uri, 'val-scenes.csv'))
    log_tensorboard = True
    run_tensorboard = True
    class_config = ClassConfig(names=['no_building', 'building'])

    if test:
        debug = True
        train_scene_info = train_scene_info[0:1]
        val_scene_info = val_scene_info[0:1]

    def make_scene(scene_info):
        (raster_uri, label_uri) = scene_info
        raster_uri = join(raw_uri, raster_uri)
        label_uri = join(processed_uri, label_uri)
        aoi_uri = join(raw_uri, aoi_path)

        if test:
            crop_uri = join(processed_uri, 'crops',
                            os.path.basename(raster_uri))
            label_crop_uri = join(processed_uri, 'crops',
                                  os.path.basename(label_uri))

            save_image_crop(raster_uri,
                            crop_uri,
                            label_uri=label_uri,
                            label_crop_uri=label_crop_uri,
                            size=600,
                            min_features=20,
                            class_config=class_config)
            raster_uri = crop_uri
            label_uri = label_crop_uri

        id = os.path.splitext(os.path.basename(raster_uri))[0]
        raster_source = RasterioSourceConfig(channel_order=[0, 1, 2],
                                             uris=[raster_uri])
        label_source = ChipClassificationLabelSourceConfig(
            vector_source=GeoJSONVectorSourceConfig(uri=label_uri,
                                                    default_class_id=1,
                                                    ignore_crs_field=True),
            ioa_thresh=0.5,
            use_intersection_over_cell=False,
            pick_min_class_id=False,
            background_class_id=0,
            infer_cells=True)

        return SceneConfig(id=id,
                           raster_source=raster_source,
                           label_source=label_source,
                           aoi_uris=[aoi_uri])

    chip_sz = 200
    train_scenes = [make_scene(info) for info in train_scene_info]
    val_scenes = [make_scene(info) for info in val_scene_info]
    dataset = DatasetConfig(class_config=class_config,
                            train_scenes=train_scenes,
                            validation_scenes=val_scenes)

    if external_model:
        model = ClassificationModelConfig(external_def=ExternalModuleConfig(
            github_repo='lukemelas/EfficientNet-PyTorch',
            # uri='s3://raster-vision-ahassan/models/EfficientNet-PyTorch.zip',
            name='efficient_net',
            entrypoint='efficientnet_b0',
            force_reload=False,
            entrypoint_kwargs={
                'num_classes': len(class_config.names),
                'pretrained': 'imagenet'
            }))
    else:
        model = ClassificationModelConfig(backbone=Backbone.resnet50)

    if external_loss:
        external_loss_def = ExternalModuleConfig(
            github_repo='AdeelH/pytorch-multi-class-focal-loss',
            name='focal_loss',
            entrypoint='focal_loss',
            force_reload=False,
            entrypoint_kwargs={
                'alpha': [.75, .25],
                'gamma': 2
            })
    else:
        external_loss_def = None

    solver = SolverConfig(lr=1e-4,
                          num_epochs=20,
                          test_num_epochs=4,
                          batch_sz=32,
                          one_cycle=True,
                          external_loss_def=external_loss_def)

    if augment:
        mu = np.array((0.485, 0.456, 0.406))
        std = np.array((0.229, 0.224, 0.225))

        aug_transform = A.Compose([
            A.Flip(),
            A.Transpose(),
            A.RandomRotate90(),
            A.ShiftScaleRotate(),
            A.OneOf([
                A.ChannelShuffle(),
                A.CLAHE(),
                A.FancyPCA(),
                A.HueSaturationValue(),
                A.RGBShift(),
                A.ToGray(),
                A.ToSepia(),
            ]),
            A.OneOf([
                A.RandomBrightness(),
                A.RandomGamma(),
            ]),
            A.OneOf([
                A.GaussNoise(),
                A.ISONoise(),
                A.RandomFog(),
            ]),
            A.OneOf([
                A.Blur(),
                A.MotionBlur(),
                A.ImageCompression(),
                A.Downscale(),
            ]),
            A.CoarseDropout(max_height=32, max_width=32, max_holes=5)
        ])
        base_transform = A.Normalize(mean=mu.tolist(), std=std.tolist())
        plot_transform = A.Normalize(mean=(-mu / std).tolist(),
                                     std=(1 / std).tolist(),
                                     max_pixel_value=1.)
    else:
        aug_transform = None
        base_transform = None
        plot_transform = None

    backend = PyTorchChipClassificationConfig(
        model=model,
        solver=solver,
        log_tensorboard=log_tensorboard,
        run_tensorboard=run_tensorboard,
        test_mode=test,
        base_transform=A.to_dict(base_transform),
        aug_transform=A.to_dict(aug_transform),
        plot_options=PlotOptions(transform=A.to_dict(plot_transform)))

    config = ChipClassificationConfig(root_uri=root_uri,
                                      dataset=dataset,
                                      backend=backend,
                                      train_chip_sz=chip_sz,
                                      predict_chip_sz=chip_sz)
    return config