Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. # Import pytorch utilities from albumentations
  2. from albumentations.pytorch import ToTensor
  3.  
  4. # Define the augmentation pipeline
  5. augmentation_pipeline = A.Compose(
  6. [
  7. A.HorizontalFlip(p = 0.5), # apply horizontal flip to 50% of images
  8. A.OneOf(
  9. [
  10. # apply one of transforms to 50% of images
  11. A.RandomContrast(), # apply random contrast
  12. A.RandomGamma(), # apply random gamma
  13. A.RandomBrightness(), # apply random brightness
  14. ],
  15. p = 0.5
  16. ),
  17.  
  18. A.Normalize(
  19. mean=[0.485, 0.456, 0.406],
  20. std=[0.229, 0.224, 0.225]),
  21.  
  22. ToTensor() # convert the image to PyTorch tensor
  23. ],
  24. p = 1
  25. )
  26.  
  27. # Load the augmented data
  28.  
  29. # Define the demo dataset
  30. class DogDataset2(Dataset):
  31. '''
  32. Sample dataset for Albumentations demonstration.
  33. The dataset will consist of just one sample image.
  34. '''
  35.  
  36. def __init__(self, image, augmentations = None):
  37. self.image = image
  38. self.augmentations = augmentations # save the augmentations
  39.  
  40. def __len__(self):
  41. return 1 # return 1 as we have only one image
  42.  
  43. def __getitem__(self, idx):
  44. # return the augmented image
  45. # no need to convert to tensor, because image is converted to tensor already by the pipeline
  46. augmented = self.augmentations(image = self.image)
  47. return augmented['image']
  48.  
  49. # Initialize the dataset, pass the augmentation pipeline as an argument to init function
  50. train_ds = DogDataset2(image, augmentations = augmentation_pipeline)
  51.  
  52. # Initilize the dataloader
  53. trainloader = DataLoader(train_ds, batch_size=1, shuffle=True, num_workers=0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement