0

I am using Unet for segmentation in python and my unet's output is a mask with this shape [512,512,1].

After predicted a mask I want to do f1 score between the predicted mask and the real mask of the test image. I need to convert the real mask from [512,512,3] to [512,512,1] and I just can convert to [512,512].

Can anyone help me? Image with my outputs

Nick ODell
  • 15,465
  • 3
  • 32
  • 66

2 Answers2

0

You can use Pillow

from PIL import Image
img = Image.open('image.png').convert('LA')
img.save('greyscale.png')
          • OR

Using matplotlib and the formula

Y' = 0.2989 R + 0.5870 G + 0.1140 B

you could do:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])

img = mpimg.imread('image.png')
gray = rgb2gray(img)
plt.imshow(gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show()

You can refer this answer for more info

Kuldeep Singh Sidhu
  • 3,748
  • 2
  • 12
  • 22
0

I discover the answer. I had to remove one axis

predicted.shape [512,512,1]

predicted = predicted[:, :,:, 0]

predicted.shape [512,512]