Skip to content

Instantly share code, notes, and snippets.

View Lexie88rus's full-sized avatar
🏡
WFH

Aleksandra Deis Lexie88rus

🏡
WFH
View GitHub Profile
@Lexie88rus
Lexie88rus / albumentations_image_with_mask_transform.py
Created August 8, 2019 18:01
Transform image with mask with Albumentations
# Compose an augmentation pipeline
aug_pipeline = A.Compose([
A.ShiftScaleRotate(),
A.RGBShift(),
A.Blur(),
A.GaussNoise()
],p=1)
# Load the mask
mask = imageio.imread('mask1.png')
@Lexie88rus
Lexie88rus / albumentations_simple_example.py
Created August 8, 2019 17:49
Example of using Albumentations package augmenters
# import Albumentations package
import albumentations as A
# Initialize an augmentation
gaus_noise = A.GaussNoise() # gaussian noise
# Apply augmentation
img_gaus = gaus_noise(image = image)
# Access the augmented image by the 'image' key
@Lexie88rus
Lexie88rus / imgaug_pytorch_sample.py
Last active August 8, 2019 18:44
Using imgaug with PyTorch
# Import PyTorch
import torchvision.transforms.functional as TF
from torch.utils.data import Dataset, DataLoader
# Define the augmentations
AUG_TRAIN = aug_pipeline # use our pipeline as train augmentations
# Define the demo dataset
class DogDataset(Dataset):
'''
@Lexie88rus
Lexie88rus / pipeline_imgaug.py
Created August 8, 2019 17:31
Augmentation pipeline with imgaug
# Define an augmentation pipeline
aug_pipeline = iaa.Sequential([
iaa.Sometimes(0.5, iaa.GaussianBlur((0, 3.0))), # apply Gaussian blur with a sigma between 0 and 3 to 50% of the images
# apply one of the augmentations: Dropout or CoarseDropout
iaa.OneOf([
iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
]),
# apply from 0 to 3 of the augmentations from the list
iaa.SomeOf((0, 3),[
@Lexie88rus
Lexie88rus / visualize_bbox_imgaug.py
Created August 8, 2019 17:21
Plot augmented image with box with imgaug
# Plot the initial and the augmented images with bounding boxes
# using helpers from imgaug package
side_by_side = np.hstack([
bbs.draw_on_image(image, size=2), # blend the original image with bounding box
bbs_aug.draw_on_image(image_aug, size=2) # blend the augmented image with bounding box
])
# Plot with matplotlib imshow()
fig, ax = plt.subplots(figsize=(10, 7))
ax.axis('off')
@Lexie88rus
Lexie88rus / imgaug_bounding_boxes.py
Created August 8, 2019 17:13
Augmenting images with bounding boxes with imgaug
# Import bounding boxes from imgaug
from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage
# Initialize the bounding box for the original image
# using helpers from imgaug package
bbs = BoundingBoxesOnImage([
BoundingBox(x1=1, x2=980, y1=9, y2=535)
], shape=image.shape)
# Define a simple augmentations pipeline for the image with bounding box
@Lexie88rus
Lexie88rus / img_aug_visualize_mask.py
Created August 8, 2019 16:32
Visualize augmented image and segmentation mask with imgaug
# Import matplotlib for visualization
from matplotlib import pyplot as plt
# Visualize original image, augmented image, and segmentation map
side_by_side = np.hstack([
segmap.draw_on_image(image), # show blend of original image and segmentation map
segmap_aug.draw_on_image(image_aug), # show blend of augmented image and segmentation map
segmap_aug.draw() # show only the augmented segmentation map
])
@Lexie88rus
Lexie88rus / imgaug_mask_augmentation.py
Last active October 10, 2019 07:24
Augment image with binary segmentation mask
# Import PIL
from PIL import Image
# Import segmentation maps from imgaug
from imgaug.augmentables.segmaps import SegmentationMapOnImage
# Open image with mask
pil_mask = Image.open('../input/mask1.png')
# Convert mask to binary map
@Lexie88rus
Lexie88rus / imgaug_augmentation.py
Created August 8, 2019 16:11
Augment image with imgaug
# import imageio to open images
import imageio
# import augmenters from imgaug
from imgaug import augmenters as iaa
# use imageio library to read the image (alternatively you can use OpenCV cv2.imread() function)
image = imageio.imread('image1.jpg')
# initialize the augmenter
rotate = iaa.Affine(rotate=(-25, 25)) # rotate image
@Lexie88rus
Lexie88rus / silu_inplace_implementation.py
Created July 10, 2019 08:16
Example of implementation of in-place SiLU activation function
def silu_inplace_2(input):
'''
Example of implementation of in-place SiLU activation function using torch.sigmoid_
https://arxiv.org/pdf/1606.08415.pdf
'''
result = input.clone()
torch.sigmoid_(input)
input *= result
return input