Deep Learning pour les images avec 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])

Documentation de l'ensemble de données :
Annotations de pixels : 1 : premier plan 2 : arrière-plan 3 : non classé
Valeurs uniques du masque :
mask_tensor.unique()
tensor([0.0039, 0.0078, 0.0118])
Les valeurs de pixels sont divisées par 255 :
1 / 255 = 0.0039 - objet2 / 255 = 0.0078 - arrière-plan3 / 255 = 0.0118 - non classébinary_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 pour les images avec PyTorch