Dice coefficient, IOU
#day7 of #100daysofcode
Recently I was working on Image Segmentation. More specifically Semantic Segmentation. Where I used IoU, Dice Coefficient metrics to evaluate my model. At that time I had a vague idea about the working of these metrics. So today I decided to explore them in detail. Though the concept is simple it is interesting to code it and know how it is working deep down. The best way to understand them is to pick an example and solve them on the paper. There are other various metrics but these are used mostly.
I use Keras for implementation. As in deep learning, we are dealing with tensors. NumPy is a good option but it becomes a little cumbersome. As I implement my deep learning models in Keras that’s why it is easy and efficient to implement any metrics in it. Keras is a great library, it provides an upper layer to Tensorflow. As I use Tensorflow at the backend, there are other options also. It is just my personal preference and it is also my first one so I am kind of attached to it.
I will briefly explain both the metrics. If you have any doubt, let me know in the comment section. More doubt you have and more you clear more you will learn. In my opinion, learning starts with doubts. Of course, you should clear them then only you will learn.
Dice Coefficient
The idea is simple we count the similar pixels (taking intersection, present in both the images) in the both images we are comparing and multiple it by 2. And divide it by the total pixels in both the images. The below diagrams will make the picture more clear.
Formula:-
IoU (Intersection over Union)
As the name suggest we divide the intersection means the number of similar pixels by the union means total number of uniques pixels in both the images. The below diagrams will clear it more. Both these metrics are positively correlated means if one says the model A is better than other will also say the same.
Code
The implementation is very easy in Keras. Let’s get into code and discuss it.
First, we need to import backend which indicates what is used at the backend. I am using Tensorflow. Many use Theano at the backend. Used flatten() to get the values in a one-dimensional array. The rest of the code seems simple and self-explaining but still you have any doubts please do ask. Metrics are very important in training. If we can’t measure the performance of the model then how we can know that our model is improving and we have trained right model. These metrics give confidence that our model can solve real-world problems. Do invest some time in understanding these metrics.
Happy Learning !!!