Search code examples
pythontensorflowsemantic-segmentation

Read image in tensorflow with indexed color value for semantic segmentation task


I'm creating FCN for semantic segmentation. I'm having a difficult time with converting the labeled png image to indexed color value on PascalVOC dataset. I want the value to be range between 0 and 20. As I can achieve such operation with PIL in the following code

with Image.open(image_path) as img:
    label = np.array(img)

it outputs what I want. But for tensorflow implementation, I want it to be same value with code like the following

file = tf.read_file(image_path)
label = tf.image.decode_png(file, channels=0)

But the tensorflow implementation results the value from 0 to 255. Is there any way that I can achieve the PIL implementation in tensorflow as well? Thank you.


Solution

  • The SegmentationClass file has color map component in it and so when using tf.decode_png() you need to specify as :

    label = tf.image.decode_png(file, channels=3)
    

    Now you have got the RGB values you can use create_pascal_label_colormap() function to convert to class Id's.