Exemple #1
0
def main(img):
    """Applies static transformations to img and plots for visual comparison"""
    # set up augmentation parameters
    hw = (img.shape[0], img.shape[1])
    crop_percentage = 0.10
    crop_hw = (hw[0] - int(hw[0] * crop_percentage),
               hw[1] - int(hw[1] * crop_percentage))

    augmentation_params = dict(
        rotation=45,
        zoom=(0.5, 2.0),  # zoom (x, y) = (col, row)
        shear=15,
        translation=(int(hw[0] * 0.1), -int(hw[1] * 0.2)),
        flip_lr=True,
        flip_ud=True)
    nAugmentations = len(augmentation_params)

    # set up plotting.
    # nPlots = original image + len(augs) + all_augment + all_augment_crop
    nPlots = nAugmentations + 3
    nrows = int(np.ceil(np.sqrt(nPlots)))
    ncols = int(np.ceil(nPlots / float(nrows)))

    fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(16, 12))
    axes = axes.flatten()

    # Plot the original image
    axes[0].imshow(img)
    axes[0].set_title("Original Image")

    # Plot each augmentation parameter, isolated
    for it, (key, param) in enumerate(augmentation_params.iteritems()):
        ax = axes[it + 1]
        augmentation_param = {key: param}

        # transform image
        img_wf = dtf.transform_image(img, **augmentation_param)

        # plot image
        ax.imshow(img_wf.astype(np.uint8))
        ax.set_title("Augmentation Param: %s = %s" % (key, param))
        ax.set_xticks([])
        ax.set_yticks([])

    # plot image with all augmentations at once
    ax = axes[it + 2]
    img_wf_og = dtf.transform_image(img, **augmentation_params)
    ax.imshow(img_wf_og.astype(np.uint8))
    ax.set_title("All Augmentations")
    ax.set_xticks([])
    ax.set_yticks([])

    # plot image with all augmentations & center crop
    ax = axes[it + 3]
    img_wf = dtf.transform_image(img,
                                 output_shape=crop_hw,
                                 **augmentation_params)
    ax.imshow(img_wf.astype(np.uint8))
    ax.set_title("All Augmentation + Crop")
def main(img):
    """Applies static transformations to img and plots for visual comparison"""
    # set up augmentation parameters
    hw = (img.shape[0], img.shape[1])
    crop_percentage = 0.10
    crop_hw = (hw[0] - int(hw[0]*crop_percentage),
               hw[1] - int(hw[1]*crop_percentage))

    augmentation_params = dict(rotation=45,
                               zoom=(0.5, 2.0),  # zoom (x, y) = (col, row)
                               shear=15,
                               translation=(int(hw[0]*0.1), -int(hw[1]*0.2)),
                               flip_lr=True,
                               flip_ud=True)
    nAugmentations = len(augmentation_params)

    # set up plotting.
    # nPlots = original image + len(augs) + all_augment + all_augment_crop
    nPlots = nAugmentations + 3
    nrows = int(np.ceil(np.sqrt(nPlots)))
    ncols = int(np.ceil(nPlots/float(nrows)))

    fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(16, 12))
    axes = axes.flatten()

    # Plot the original image
    axes[0].imshow(img)
    axes[0].set_title("Original Image")

    # Plot each augmentation parameter, isolated
    for it, (key, param) in enumerate(augmentation_params.iteritems()):
        ax = axes[it + 1]
        augmentation_param = {key: param}

        # transform image
        img_wf = dtf.transform_image(img, **augmentation_param)

        # plot image
        ax.imshow(img_wf.astype(np.uint8))
        ax.set_title("Augmentation Param: %s = %s" % (key, param))
        ax.set_xticks([])
        ax.set_yticks([])

    # plot image with all augmentations at once
    ax = axes[it + 2]
    img_wf_og = dtf.transform_image(img, **augmentation_params)
    ax.imshow(img_wf_og.astype(np.uint8))
    ax.set_title("All Augmentations")
    ax.set_xticks([])
    ax.set_yticks([])

    # plot image with all augmentations & center crop
    ax = axes[it + 3]
    img_wf = dtf.transform_image(img, output_shape=crop_hw,
                                 **augmentation_params)
    ax.imshow(img_wf.astype(np.uint8))
    ax.set_title("All Augmentation + Crop")
    if title is None:
        title = ''

    ax.imshow(img, **imshow_kwargs)
    ax.set_title(title, fontsize=20)

    if axis_off:
        ax.set_xticks([])
        ax.set_yticks([])


# load test img
img = np.array(Image.open('../tests/test_data/cat.jpg', 'r'))

# rotate image by 20 deg CCW
img_rot = dtf.transform_image(img, rotation=20)
img_rot = img_rot.astype(np.uint8)  # by default, transform returns float32

# zoom in the center of image by 2x (not upscale). zoom = (col, row)
img_zoom = dtf.transform_image(img, zoom=(2.0, 2.0)).astype(np.uint8)

# translate x,y: -50 in x direction and 200 in y direction
img_txy = dtf.transform_image(img, translation=(-50, 200)).astype(np.uint8)

# Take a 200x200 center-crop of image
img_crop = dtf.transform_image(img, output_shape=(600, 600)).astype(np.uint8)

# rotate, translate, and zoom all at once
img_all = dtf.transform_image(img,
                              rotation=20,
                              zoom=(2.0, 2.0),
    if title is None:
        title = ''

    ax.imshow(img, **imshow_kwargs)
    ax.set_title(title, fontsize=20)

    if axis_off:
        ax.set_xticks([])
        ax.set_yticks([])

# load test img
img = np.array(Image.open('../tests/test_data/cat.jpg', 'r'))

# rotate image by 20 deg CCW
img_rot = dtf.transform_image(img, rotation=20)
img_rot = img_rot.astype(np.uint8)  # by default, transform returns float32

# zoom in the center of image by 2x (not upscale). zoom = (col, row)
img_zoom = dtf.transform_image(img, zoom=(2.0, 2.0)).astype(np.uint8)

# translate x,y: -50 in x direction and 200 in y direction
img_txy = dtf.transform_image(img, translation=(-50, 200)).astype(np.uint8)

# Take a 200x200 center-crop of image
img_crop = dtf.transform_image(img, output_shape=(600, 600)).astype(np.uint8)

# rotate, translate, and zoom all at once
img_all = dtf.transform_image(img, rotation=20, zoom=(2.0, 2.0),
                              translation=(-50, 200)).astype(np.uint8)