BLOG Computer Vision

Loss Functions for Computer Vision Models

Prerak Mody
April 6, 2018
Choosing an appropriate loss function is important as it affects the ability of the algorithm to produce optimum results as fast as possible. Here we study losses in popular models like MaskRCNN and PSPNet.

Machine learning algorithms are designed so that they can "learn" from their mistakes and "update" themselves using the training data we provide them. But how do they quantify these mistakes? This is done via the usage of "loss functions" that help an algorithm get a sense of how erroneous its predictions are when compared to the ground truth. Choosing an appropriate loss function is important as it affects the ability of the algorithm to produce optimum results as fast as possible.

Basic Definitions

L2 LOSS

CROSS-ENTROPY LOSS

SIGMOID FUNCTION

SOFTMAX FUNCTION

Examples of Loss Functions in Popular Semantic Segmentation Models

Semantic Segmentation - PSPNet


# Step1 :

raw_prediction = tf.reshape(raw_output, [-1, args.num_classes])

label_proc = prepare_label(label_batch, tf.stack(raw_output.get_shape()[1:3]), num_classes=args.num_classes, one_hot=False) # [batch_size, h, w]

raw_gt = tf.reshape(label_proc, [-1,])

indices = tf.squeeze(tf.where(tf.less_equal(raw_gt, args.num_classes - 1)), 1)



# Ste2 :

gt = tf.cast(tf.gather(raw_gt, indices), tf.int32)    

prediction = tf.gather(raw_prediction, indices)



# Step3 : Pixel-wise softmax loss.

loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction, labels=gt)

Dimensions of various layers using Cityscapes
Inference in PSPNet

Instance Semantic Segmentation - MaskRCNN

[caption id="attachment_59958" align="aligncenter" width="479"]

Architecture of MaskRCNN (post Region Proposal Network)

Figure3: Architecture of MaskRCNN (post Region Proposal Network)[/caption]

Sample region of interest passing through class and mask layers
Getting the mask of region of interest using the class and mask layers

Conclusion

Here we learned about some basic loss functions and how their complex variants are used in state-of-the-art networks. Go ahead and play around with the repositories in the links above!