Deep Learning for Images with PyTorch
Michal Oleszak
Machine Learning Engineer



image = Image.open("images/British_Shorthair_36.jpg") mask = Image.open("annots/British_Shorthair_36.png")transform = transforms.Compose([ transforms.ToTensor() ]) image_tensor = transform(image) mask_tensor = transform(mask)print(f"""Image shape: {image_tensor.shape} Mask shape: {mask_tensor.shape}""")
Image shape: torch.Size([3, 333, 500])
Mask shape: torch.Size([1, 333, 500])

Dataset documentation:
Pixel Annotations: 1: Foreground 2: Background 3: Not classified
Unique mask values:
mask_tensor.unique()
tensor([0.0039, 0.0078, 0.0118])
Pixel values are divided by 255:
1 / 255 = 0.0039 - object2 / 255 = 0.0.0078 - background3 / 255 = 0.0118 - unclassifiedbinary_mask = torch.where( mask_tensor == 1/255, torch.tensor(1.0), torch.tensor(0.0), )to_pil_image = transforms.ToPILImage() mask = to_pil_image(binary_mask)plt.imshow(mask)

torch.where():object_tensor = image_tensor * binary_maskto_pil_image = transforms.ToPILImage() object_image = to_pil_image(object_tensor)plt.imshow(object_image)

Deep Learning for Images with PyTorch