!pip install kornia
!pip install kornia-rs
Augmentation Sequential
Basic
2D
Data augmentation
kornia.augmentation
In this tutorial we will show how we can quickly perform data augmentation for various tasks (segmentation, detection, regression) using the features provided by the
kornia.augmentation.AugmentationSequential
API.
Install and get data
We install Kornia and some dependencies, and download a simple data sample
import io
import requests
def download_image(url: str, filename: str = "") -> str:
= url.split("/")[-1] if len(filename) == 0 else filename
filename # Download
= io.BytesIO(requests.get(url).content)
bytesio # Save file
with open(filename, "wb") as outfile:
outfile.write(bytesio.getbuffer())
return filename
= "https://raw.githubusercontent.com/kornia/data/main/panda.jpg"
url download_image(url)
import cv2
import kornia as K
import numpy as np
import torch
from kornia.augmentation import AugmentationSequential
from kornia.geometry import bbox_to_mask
from matplotlib import pyplot as plt
def plot_resulting_image(img, bbox, keypoints, mask):
= img * mask
img = K.tensor_to_image(img.mul(255).byte()).copy()
img_array = cv2.polylines(img_array, bbox.numpy(), isClosed=True, color=(255, 0, 0))
img_draw for k in keypoints[0]:
= cv2.circle(img_draw, tuple(k.numpy()[:2]), radius=6, color=(255, 0, 0), thickness=-1)
img_draw return img_draw
= K.io.load_image("panda.jpg", K.io.ImageLoadType.RGB32)[None, ...] # BxCxHxW
img_tensor = img_tensor.shape[-2:]
h, w
"off")
plt.axis(
plt.imshow(K.tensor_to_image(img_tensor)) plt.show()
Define Augmentation Sequential and Different Labels
= AugmentationSequential(
aug_list 0.1, 0.1, 0.1, 0.1, p=1.0),
K.augmentation.ColorJitter(360, [0.1, 0.1], [0.7, 1.2], [30.0, 50.0], p=1.0),
K.augmentation.RandomAffine(0.5, p=1.0),
K.augmentation.RandomPerspective(=["input", "bbox", "keypoints", "mask"],
data_keys=False,
same_on_batch
)
= torch.tensor([[[[355, 10], [660, 10], [660, 250], [355, 250]]]])
bbox = torch.tensor([[[465, 115], [545, 116]]])
keypoints = bbox_to_mask(torch.tensor([[[155, 0], [900, 0], [900, 400], [155, 400]]]), w, h)[None].float()
mask
= plot_resulting_image(img_tensor, bbox[0], keypoints, mask[0])
img_out
"off")
plt.axis(
plt.imshow(img_out) plt.show()
Forward Computations
= aug_list(img_tensor, bbox.float(), keypoints.float(), mask)
out_tensor = plot_resulting_image(
img_out 0][0],
out_tensor[1].int(),
out_tensor[2].int(),
out_tensor[3][0],
out_tensor[
)
"off")
plt.axis(
plt.imshow(img_out) plt.show()
Inverse Transformations
= aug_list.inverse(*out_tensor)
out_tensor_inv = plot_resulting_image(
img_out 0][0],
out_tensor_inv[1].int(),
out_tensor_inv[2].int(),
out_tensor_inv[3][0],
out_tensor_inv[
)
"off")
plt.axis(
plt.imshow(img_out) plt.show()