Trying to calculate Mean IoU for Semantic Segmentation Task

Viewed 284

I am trying to calculate loss and mean IoU for semantic segmentation task. I created a dummy variable, seg_gt_cl5, which shows the ground truth value of class-5, and assume that I have 21 classes in my dataset, which is the same as PascalVOC.

import tensorflow as tf
import numpy as np


seg_gt_cl5 = tf.Variable([[[5, 5, 0], [0, 0, 0], [0, 0, 0]]]) # Its shape is 3x3
logits = tf.random_uniform([1,3,3,21], maxval=255, dtype=tf.float32, seed=0)
logits = logits / tf.norm(logits)


mask = seg_gt_cl5 <= 21
seg_logits = tf.boolean_mask(logits, mask)
seg_gt = tf.boolean_mask(seg_gt_cl5, mask)


seg_loss_local = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=seg_logits, labels=seg_gt)
seg_loss = tf.reduce_mean(seg_loss_local)
seg_predictions = tf.argmax(seg_logits, axis=1)

iou, conf_mat = tf.metrics.mean_iou(seg_gt, seg_logits, num_classes=21)
with tf.Session() as sess:
    sess.run(tf.local_variables_initializer())
    sess.run([conf_mat])
    miou = sess.run([iou])
    print(miou)

When I run the code snip, getting the following error:

 File "....segmantation_mean_IoU_analysis.py", line 19, in <module>
    iou, conf_mat = tf.metrics.mean_iou(seg_gt, seg_logits, num_classes=21)

raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (?, 21) and (?,) are incompatible

Line 19 points to iou, conf_mat = tf.metrics.mean_iou(seg_gt, seg_logits, num_classes=21).

I am new to TensorFlow and couldn't solve the problem. Thanks a lot in advance.

Edit: By changing these lines, I can get get the result:

...
...
++ seg_logits = tf.argmax(logits, axis=-1)
-- iou, conf_mat = tf.metrics.mean_iou(seg_gt, seg_logits, num_classes=21)
++ iou, conf_mat = tf.metrics.mean_iou(seg_gt_cl5, seg_logits, num_classes=21)

It gives the mean IoU as 0.03125. I think the result is wrong because there are no intersecting pixels between the gt and logits matrix. I illustrated my gt matrix and logits output after applying tf.argmax(logits, axis=-1). Images are shown below:

seg_gt_cl5 matrix

enter image description here

logits matrix after applying tf.argmax(logits, axis=-1): enter image description here

0 Answers
Related