I am using PyTorch for semantic segmentation, But I am facing a problem, because I am use images , and their masks/labels . I want to perform data augmentation such as RandomHorizontalFlip, and RandomCrop, etc.
Here is my code, please check and let me know, how I can embed the following operations in the provided code.
import torchvision.transforms.functional as F
class ToTensor(object):
def call(self, sample):
image, label = sample[‘image’], sample[‘label’]
return {‘image’: F.to_tensor(image), ‘label’: F.to_tensor(label)}my_transform = transforms.Compose([ ToTensor() ])
dataset = Mydataset(image_dir, label_dir, transform = my_transform)
Print dataset output
dataset[1]
Output
{‘image’: tensor([[[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
…,
[0.0902, 0.0902, 0.0902, …, 0.0824, 0.0824, 0.0824],
[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
[0.0745, 0.0745, 0.0745, …, 0.0824, 0.0824, 0.0824]],[[0.0824, 0.0824, 0.0824, ..., 0.0824, 0.0824, 0.0824], [0.0824, 0.0824, 0.0824, ..., 0.0824, 0.0824, 0.0824], [0.0824, 0.0824, 0.0824, ..., 0.0824, 0.0824, 0.0824], ..., [0.0902, 0.0902, 0.0902, ..., 0.0824, 0.0824, 0.0824], [0.0824, 0.0824, 0.0824, ..., 0.0824, 0.0824, 0.0824], [0.0745, 0.0745, 0.0745, ..., 0.0824, 0.0824, 0.0824]], [[0.0824, 0.0824, 0.0824, ..., 0.0824, 0.0824, 0.0824], [0.0824, 0.0824, 0.0824, ..., 0.0824, 0.0824, 0.0824], [0.0824, 0.0824, 0.0824, ..., 0.0824, 0.0824, 0.0824], ..., [0.0902, 0.0902, 0.0902, ..., 0.0824, 0.0824, 0.0824], [0.0824, 0.0824, 0.0824, ..., 0.0824, 0.0824, 0.0824], [0.0745, 0.0745, 0.0745, ..., 0.0824, 0.0824, 0.0824]]]),
‘label’: tensor([[[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
…,
[0.0902, 0.0902, 0.0902, …, 0.0824, 0.0824, 0.0824],
[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
[0.0745, 0.0745, 0.0745, …, 0.0824, 0.0824, 0.0824]]])}