Tutorial: Use on images

1. A simple augmentation operator

Take an example of Rotation in “imgaug” library

[2]:
import beacon_aug as BA
import cv2
from skimage import data
import matplotlib.pyplot as plt
import numpy as np

image = data.astronaut()

aug = BA.Rotate(p=1,library="imgaug", limit = (45,45))
image_auged = aug(image=image)["image"].copy()
print("Parameter dictionary:\n\t", BA.to_dict(aug))

plt.figure()
plt.imshow(np.hstack([ image, image_auged]))

Parameter dictionary:
         {'__version__': '1.0.0', 'transform': {'__class_fullname__': 'BA_Rotate', 'always_apply': False, 'p': 1, 'deterministic': False, 'library': 'imgaug', 'avail_libraries': ['albumentations', 'imgaug', 'torchvision', 'keras', 'augly']}}
[2]:
<matplotlib.image.AxesImage at 0x7fb74fcedf10>
../_images/ipynbs_tutorial_img_2_2.svg

2. Result comparision for all backend libraries

result different of imgarray V.S. PIL

[5]:

from skimage import data
import matplotlib.pyplot as plt
import beacon_aug as BA
image = data.astronaut()
fig,__ = BA.screenshot.screenshot_library(BA.Brightness, image_data=image,  limit = (0.5,0.5),save_fig_path="./figs")


/Users/xiaoli/miniconda3/envs/aug/lib/python3.7/site-packages/albumentations/augmentations/transforms.py:1701: FutureWarning: This class has been deprecated. Please use RandomBrightnessContrast
  FutureWarning,
/Users/xiaoli/OneDrive - Adobe/Projs/adobe-research/augmenter/beacon_aug/screenshot.py:123: UserWarning: This figure was using constrained_layout==True, but that is incompatible with subplots_adjust and or tight_layout: setting constrained_layout==False.
  fig.subplots_adjust(wspace=0)

...screenshot figure save as :  ./figs/Brightnessaug_library-screenshot.png
../_images/ipynbs_tutorial_img_4_1.svg

3. Run an augmentation pipeline

[5]:
import matplotlib.pyplot as plt
import beacon_aug as BA
import numpy as np
from skimage import data


image = data.astronaut()
aug_pipeline = BA.Compose([
    BA.Rotate(p=0.5,limit=(-90,90)),
    BA.Brightness(p=0.5, limit=(-0.5,0.5) ),     #image only
    BA.VerticalFlip(p=0.5)
])

# Optional: if need to control the random seed to fix the results
BA.properties.set_seed(100)
augmented_image = aug_pipeline(image=image)['image']

# Display a result
plt.figure();plt.title("An example of the result of an augmentation pipeline")
plt.imshow(np.hstack([ image, augmented_image]))


[5]:
<matplotlib.image.AxesImage at 0x7fd2b2536990>
../_images/ipynbs_tutorial_img_6_1.svg

4. Display a fast screenshot of the pipeline

[6]:
import matplotlib.pyplot as plt
import beacon_aug as BA
from skimage import data

image = data.astronaut()

aug_pipeline = BA.Compose([
    BA.Rotate(p=0.5,limit=(-90,90)) ,
    BA.Brightness(p=0.5, limit=(-0.5,0.5) ),
    BA.VerticalFlip(p=0.5)
])

fig = BA.screenshot.screenshot_pipeline(aug_pipeline , image)
fig.show()
plt.savefig("figs/screenshot_pipeline.png")
ipykernel_launcher:16: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.
../_images/ipynbs_tutorial_img_8_1.svg

5. Same augmentation for image, bbox, masks at the same time

Check the function definition for detail

[1]:

import __init__
from skimage import data
import matplotlib.pyplot as plt
import beacon_aug as BA
import numpy as np
import random

# assign target values
image = data.astronaut()

bboxes = [[90, 151, 380, 280, "o"], [178, 30, 290, 160, "0"]]  # [x_min, y_min, x_max, y_max]
label_ids = [0] * len(bboxes)          # label_names = {0:"bbox"}  # can be optional

mask = np.zeros(image.shape[:2])
mask[ 370:420,155:225] = 1

kps = [[400, 104], [400, 50]]
gray = image[:,:,0]  #an example of image0


# A simple example of pair augmentation
aug_pipeline = BA.Compose([BA.Rotate(p=1)],
                         bbox_params=A.BboxParams(format="pascal_voc"),
                         keypoint_params=A.KeypointParams(format='xy'),
                         additional_targets={'image0': 'image'},
                         p=1, )
augmented = aug_pipeline(image=image, image0=gray, bboxes=bboxes,
                         mask=mask, keypoints=kps)


# visualize
vis_original = BA.screenshot.visualize_targets(image,  mask, bboxes, kps, gray)
vis_augmented = BA.screenshot.visualize_targets(augmented["image"], augmented["mask"] ,augmented["bboxes"] , augmented["keypoints"],augmented["image0"] )

compare_vis = np.vstack( [vis_original,vis_augmented])
plt.imshow(compare_vis)

[1]:
<matplotlib.image.AxesImage at 0x7fd6669d3b10>
../_images/ipynbs_tutorial_img_10_1.svg

6. Export pipeline paramters to jason file

[4]:
import random
import matplotlib.pyplot as plt
from skimage import data
import beacon_aug as BA

# Load an example image (uint8, 128x128x3).
image = data.astronaut()

transform = BA.Compose([
    BA.RandomCrop(height=64,width=64,  library= "A"),
    BA.OneOf([  BA.RGBShift(library= "A"),
            BA.HueSaturationValue(library= "A")  ])])
# Save .Json
BA.save(transform, 'transform.json')
# Load .Json
loaded_transform = BA.load('transform.json')
# Change transformation
transform_dict = BA.to_dict(loaded_transform)
transform_dict["transform"]["transforms"][0]["height"] = 76
transform_dict["transform"]["transforms"][0]["width"] = 76
loaded_transform_revised =BA.from_dict(transform_dict)
# print parameters
BA.to_dict( transform)
[4]:
{'__version__': '1.0.3',
 'transform': {'__class_fullname__': 'Compose',
  'p': 1.0,
  'transforms': [{'__class_fullname__': 'A_RandomCrop',
    'always_apply': False,
    'p': 1.0,
    'height': 64,
    'width': 64},
   {'__class_fullname__': 'OneOf',
    'p': 0.5,
    'transforms': [{'__class_fullname__': 'A_RGBShift',
      'always_apply': False,
      'p': 0.5,
      'r_shift_limit': (-20, 20),
      'g_shift_limit': (-20, 20),
      'b_shift_limit': (-20, 20)},
     {'__class_fullname__': 'A_HueSaturationValue',
      'always_apply': False,
      'p': 0.5,
      'hue_shift_limit': (-20, 20),
      'sat_shift_limit': (-30, 30),
      'val_shift_limit': (-20, 20)}]}],
  'bbox_params': None,
  'keypoint_params': None,
  'additional_targets': {}}}

7. Example of AutoAugment

[1]:
from skimage import data
import matplotlib.pyplot as plt
import numpy as np
import random
import beacon_aug as BA

# Load an example image (uint8, 128x128x3).
image = data.astronaut()


ls = []
for i in range(5):
    auto_pipeline = BA.AutoAugment(policy = "imagenet")
    image_auged = auto_pipeline(image=image)["image"]
    ls.append(image_auged)

vis = np.hstack(ls)
plt.imshow(vis)
plt.imsave("figs/autoaugment.png",np.hstack(ls))
transform id= 0 Rotate 0.8 8
transform id= 1 Saturation 1.0 2
transform id= 0 Posterize 0.8 5
transform id= 1 Equalize 1.0 None
../_images/ipynbs_tutorial_img_14_1.svg

8. Example of RandAugment

[4]:
from skimage import data
import matplotlib.pyplot as plt
import numpy as np
import random
import beacon_aug as BA


image = data.astronaut()
ls = []
for i in range(5):
    auto_pipeline = BA.RandAugment(policy = "COCO")
    image_auged = auto_pipeline(image=image)["image"]
    ls.append(image_auged)

vis = np.hstack(ls)
plt.imshow(vis)
plt.imsave("figs/RandAugment.png",vis)
../_images/ipynbs_tutorial_img_16_0.svg

9. Example of Collections

[17]:
from skimage import data
import matplotlib.pyplot as plt
import numpy as np
import random
import beacon_aug as BA

# Load an example image (uint8, 128x128x3).
image = data.astronaut()


ls = []
for i in range(5):
    auto_pipeline = BA.Collections(tag = "color")
    image_auged = auto_pipeline(image=image)["image"]
    ls.append(image_auged)

vis = np.hstack(ls)
plt.imshow(vis)
plt.imsave("figs/collections-color.png",vis)
../_images/ipynbs_tutorial_img_18_0.svg
[2]:
from skimage import data
import matplotlib.pyplot as plt
import numpy as np
import random
import beacon_aug as BA

# Load an example image (uint8, 128x128x3).
image = data.astronaut()


ls = []
for i in range(5):
    auto_pipeline = BA.Collections(tag = "geometric")
    image_auged = auto_pipeline(image=image)["image"]
    ls.append(image_auged)

vis = np.hstack(ls)
plt.imshow(vis)
plt.imsave("figs/collections-geometric.png",vis)
/Users/xiaoli/miniconda3/envs/aug/lib/python3.7/site-packages/albumentations-1.0.0-py3.7.egg/albumentations/augmentations/transforms.py:645: FutureWarning: This class has been deprecated. Please use CoarseDropout
  FutureWarning,
../_images/ipynbs_tutorial_img_19_1.svg

10. Overlay Text

[12]:
# Imgaug
from skimage import data
import matplotlib.pyplot as plt
import numpy as np
import random
import beacon_aug as BA

image = data.astronaut()

# # text overlap in random place of image
# aug = BA.OverlayText(p=1, text="Text", library="custom")
# image_auged = aug(image=image)["image"]
# plt.imshow(image_auged)

# a list of text overlap in limit place of image
aug = BA.TextFlow(p=1, text=[ "Text1"] ,  x= [100,200], y= [100,500],
                size=200,    library="custom")
image_auged = aug(image=image)["image"]
plt.imshow(image_auged)

[12]:
<matplotlib.image.AxesImage at 0x7ff649850050>
../_images/ipynbs_tutorial_img_21_1.svg
[2]:
# augly
from skimage import data
import matplotlib.pyplot as plt
import numpy as np
import random
import beacon_aug as BA

image = data.astronaut()

auto_pipeline = BA.OverlayText(library="augly", text= [3,6])
image_auged = auto_pipeline(image=image)["image"]
plt.imshow(image_auged)
[2]:
<matplotlib.image.AxesImage at 0x7fb904432510>
../_images/ipynbs_tutorial_img_22_1.png
[ ]: