This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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): | |
| ''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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),[ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 | |
| ]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |