0

I am working on a multi-class image segmentation problem but am having difficulty appropriately converting my .png mask files to arrays. Instead of values corresponding to the number of classes (13), the masks have seemingly random values between 30 and 215. The images and corresponding masks are stored in two separate directories. Below are the utility functions I'm using to manipulate the data and the values I'm getting in the masks. What can I do so that the values in the mask arrays accurately depict the classes they belong to?

# Size that images will be scaled to
img_size  = (128, 128)

# Get total number of images
num_imgs = len(input_img_paths)

# Shuffle file paths. Use same seed to ensure that the order of images and masks remains the same
random.Random(42).shuffle(input_img_paths)
random.Random(42).shuffle(mask_paths)

# Utility functions to convert image/mask to array
def path_to_input(path):
    return img_to_array(load_img(path, target_size=img_size))

def path_to_mask(path):
    mask = img_to_array(load_img(path, target_size=img_size, color_mode='grayscale'))
    mask = mask.astype('uint8')-1
    return mask

# Utility function to resize image/mask
def resize(input, img_size):
   input = tf.image.resize(input, img_size, method="nearest")
   return input

# Utility function to normalize images
def normalize_img(input_image):
   input_image = tf.cast(input_image, tf.float32) / 255.0
   return input_image

# Initialize arrays to hold images/masks
input_imgs = np.zeros((num_imgs,) + img_size + (3,), dtype='float32')
masks = np.zeros((num_imgs,) + img_size + (1,), dtype='uint8')

# Fill arrays with images/masks
for i in range(num_imgs):
  img = path_to_input(input_img_paths[i])
  mask= path_to_mask(mask_paths[i])

  img = resize(img, img_size)
  img = normalize_img(img)
  mask = resize(mask, img_size)

  input_imgs[i] = img
  masks[i] = mask  
print(np.min(masks), np.max(masks))
30 215

For what its worth, I'm working in Colab with Tensorflow and can verify that the images and masks display correctly.

I'm not sure what else to do differently. I'm using the keras.utils img_to_array() function to convert the masks to arrays, but there aren't a lot of args to experiment with.

aiden
  • 1
  • https://www.kaggle.com/code/ipythonx/stanford-background-scene-understanding-starter https://www.kaggle.com/code/ipythonx/retinal-vessel-segmentation-starter https://www.kaggle.com/code/ipythonx/carvana-image-semantic-segmentation-starter – Innat Feb 27 '23 at 02:42

0 Answers0