Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Intersection over Union Loss Using Tensorflow

This may be more of a Tensorflow gradient question. I have been attempting to implement Intersection over Union (IoU) as losses and have been running into some problems. To the point, here is the snippet of my code that computes the IoU:

def get_iou(masks, predictions):
    ious = []
    for i in range(batch_size):
        mask = masks[i]
        pred = predictions[i]
        masks_sum = tf.reduce_sum(mask)
        predictions_sum = tf.reduce_mean(pred)
        intersection = tf.reduce_sum(tf.multiply(mask, pred))
        union = masks_sum + predictions_sum - intersection
        iou = intersection / union
        ious.append(iou)
    return ious

iou = get_iou(masks, predictions)
mean_iou_loss = -tf.log(tf.reduce_sum(iou))
train_op = tf.train.AdamOptimizer(0.001).minimize(mean_iou_loss)

It works as predicted. However, the issue that I am having is the losses do not decrease. The model does train, though the results are less than ideal so I am wondering if I am implementing it correctly. Do I have to compute the gradients myself? I can compute the gradients for this IoU loss derived by this paper using tf.gradients(), though I am not sure how to incorporate that with the tf.train.AdamOptimizer(). Reading the documentation, I feel like compute_gradients and apply_gradients are the commands that I need to use, but I can't find any examples on how to use them. My understanding is that the Tensorflow graph should be able to come up with the gradient itself via chain rule. So is a custom gradient even necessary in this problem? If the custom gradient is not necessary then I may just have an ill-posed problem and need to adjust some hyperparameters.

Note: I have tried Tensorflow's implementation of the IoU, tf.metrics.mean_iou(), but it spits out inf every time so I have abandoned that.

like image 864
MasterYoda Avatar asked Mar 29 '19 21:03

MasterYoda


People also ask

How is IoU calculated in Tensorflow?

To compute IoUs, the predictions are accumulated in a confusion matrix, weighted by sample_weight and the metric is then calculated from it. If sample_weight is None , weights default to 1. Use sample_weight of 0 to mask values.

How do you implement IoU in Python?

Coding a function for IOU in python:The function IOU takes in 2 boxes, box1 and box2 as input. The data in each box is a list containing[x1, y1, x2, y2], which is the top left, and bottom right coordinates. We find the area of the intersection, followed by the area of the union, as described earlier.

What is intersection over union object detection?

In object detection,Intersection of Union is a unit of measurement for checking the level of overlap between an object's predicted box and its actual bounding box in a particular dataset.


1 Answers

Gradient computation occurs inside optimizer.minimize function, so, no explicit use inside loss function is needed. However, your implementation simply lacks an optimizable, trainable variable.

iou = get_iou(masks, predictions)
mean_iou_loss = tf.Variable(initial_value=-tf.log(tf.reduce_sum(iou)), name='loss', trainable=True)
train_op = tf.train.AdamOptimizer(0.001).minimize(mean_iou_loss)

Numerical stability, differentiability and particular implementation aside, this should be enough to use it as a loss function, which will change with iterations.

Also take a look:

https://arxiv.org/pdf/1902.09630.pdf

Why does one not use IOU for training?

like image 79
Sharky Avatar answered Oct 24 '22 10:10

Sharky