<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shivam Shah on Medium]]></title>
        <description><![CDATA[Stories by Shivam Shah on Medium]]></description>
        <link>https://medium.com/@shivam.shah2509?source=rss-2fedd47e0dbf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*XzQa6aUS4iwHmwbe.jpg</url>
            <title>Stories by Shivam Shah on Medium</title>
            <link>https://medium.com/@shivam.shah2509?source=rss-2fedd47e0dbf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 00:30:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shivam.shah2509/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding Nuclei Segmentation using U-Net]]></title>
            <link>https://medium.com/@shivam.shah2509/understanding-nuclei-segmentation-using-u-net-e633184602c8?source=rss-2fedd47e0dbf------2</link>
            <guid isPermaLink="false">https://medium.com/p/e633184602c8</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[keras]]></category>
            <category><![CDATA[biomedical-imaging]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[image-segmentation]]></category>
            <dc:creator><![CDATA[Shivam Shah]]></dc:creator>
            <pubDate>Sun, 17 Nov 2019 19:49:24 GMT</pubDate>
            <atom:updated>2019-11-17T19:49:24.459Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, I will be sharing the implementation details of the <strong>U-Net</strong> model for image segmentation tasks. U-Net was first introduced by <a href="https://arxiv.org/abs/1505.04597">Ronneberger et. al</a> for biomedical image segmentation. U-Net model is able to perform very well even when there is no large dataset to start with. Today U-Net is widely used for image segmentation task especially in biomedical image-based tasks where availability of annotated images is lesser.</p><p><strong>Data</strong></p><p>Nuclei Dataset: Include 52 images of 200x200 pixels. These images contain severely clustered nuclei. You can download this dataset from <a href="http://www.viplab.cs.nott.ac.uk/download/Nott-Nuclei.zip">Nuclei DATA</a>.</p><p><strong>Network Architecture</strong></p><p>U-Net is comprised of two parts, encoder, and decoder. Here Encoder decreases the input size while increasing the channel (depth) size as we move forward whereas decoder works vice versa. For the classification task, most of the deep neural network architecture is encoder kind off where they start with an image and the final output is just a couple of nodes that represent the classes for classification.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*MKQRRlLjULoS8Ef4ZSNlfA.png" /><figcaption>Basic U-Net Architecture</figcaption></figure><p>For all the experiment I have used Keras framework with Tensorflow as backend.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b5d0e92b345761be24562b50b84e2cc/href">https://medium.com/media/5b5d0e92b345761be24562b50b84e2cc/href</a></iframe><p><strong>Loss function</strong></p><p>The loss function is the most important aspect of training any task using a neural network. At any time during training, loss function helps in deciding how well the network is able to learn the task in hand. Lower the loss, a higher chance of network correctly doing the task (classification or segmentation). As the parameters of the network are updated based on loss function only (back-propagation) it is very important to select the correct loss function. For the case of image segmentation, commonly two kinds of loss functions are considered.</p><ol><li>Cross-entropy</li><li>Dice loss</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*_Y9-S_jRrW1bBq0oSmTt5g.png" /><figcaption>A mask is nothing but a 2D matrix with value 1 at object annotated locations and the rest pixel values as 0.</figcaption></figure><p>Cross-entropy while being the frequent choice for the classification task, it can be used for segmentation task as well because in simpler terms a segmentation mask is nothing but a set of data points (equivalent to the number of pixels in the image) assigned to either class 0 or 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yChZPqYMwz9DC-GBhYYHYg.png" /><figcaption>Dice Coefficient quantifies segmentation irrespective of the size of the mask. If the mask is just 5% of the total image part, with cross-entropy we can be 95% accurate by predicting blank values, whereas Dice Coefficient value will be zero in such cases.</figcaption></figure><p>Dice loss is a bit different one which works for image segmentation task mainly defined as negative of Dice coefficient. See below for the Dice coefficient formula.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/204/1*Ongs3Slhjc6PihFsGRSnyA.png" /></figure><p>Here X is the ground truth mask and Y is the predicted mask. For measuring dice coefficient which is a common metric for reporting segmentation task accuracy, we assume X and Y as discrete image arrays with the value of each pixel only being 0 or 1. This definition of dice coefficient makes it non-differentiable and hence we cannot directly use the negative dice coefficient as the loss function. To make the dice coefficient differentiable, instead of using the predicted mask with the image array of value 0 or 1, we use the predicted probabilities (continuous parameter). By doing this, the function becomes continuous and hence differentiable. So finally the dice loss is defined as negative of dice coefficient but with prediction mask in terms of predicted probabilities, not the binary values. Please see below the implementation of the loss function in Keras.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f443daf6ecdb2ed838ef178954cfc0a/href">https://medium.com/media/6f443daf6ecdb2ed838ef178954cfc0a/href</a></iframe><p><strong>Training and Model Performance</strong></p><p>For training, the segmentation task network is mostly similar to how we train networks for classification tasks except for the data generators aspect. While I was playing around for the first time with image segmentation models, it was very difficult for me to understand how exactly I can use a pre-defined Keras image generator for image segmentation tasks. The Keras documentation has been very helpful in making me understand the concept, but you need to really get your hands dirty to completely understand them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d455e1d16d2adf1a1af9c7f95ee63b5/href">https://medium.com/media/2d455e1d16d2adf1a1af9c7f95ee63b5/href</a></iframe><p>For image segmentation tasks, while applying augmentations it is important to consider the same on masks as well. Like if you are rotating the image by 45 degrees, the corresponding mask will also need to be rotated by 45 degrees. Hence if you refer to the generator code, you will see that I have defined the data generator for the image as well as for mask. Also if you are aware of generators, they randomly output an image from a given directory, so to ensure the same image and corresponding mask is considered from two different generators, we provide the same seed to the generators.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7c46f4c83d77553ce26b1ff09d84b41/href">https://medium.com/media/e7c46f4c83d77553ce26b1ff09d84b41/href</a></iframe><p>For model training, we need to provide a single generator that outputs image as well as its mask for which need to combine the generators. Also as here, I need the masks with values of pixels as either 0 or 1. Because of augmentation values could have been changed, so to ensure I am applying a threshold (0.5) on the mask.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6bd2fa049aeaffb26509df869cb5be0/href">https://medium.com/media/c6bd2fa049aeaffb26509df869cb5be0/href</a></iframe><p>The next step is to train the model. For that, we are first defining the optimizer, loss function as well as metrics to be reported. Then training the model with training data coming from train generator and validation from validation generator.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZASZ7aUsOHvzMK3dLzr81Q.png" /><figcaption>training logs (You can define Tensorboard callbacks as well in the model to keep a better track of loss)</figcaption></figure><p>I have trained the model for 10 epochs only but you can increase the number of epoch to achieve better performing models.</p><p>Now let&#39;s see how the model performs on test images.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c02ef2f86400b3ddff8494c5dd67472f/href">https://medium.com/media/c02ef2f86400b3ddff8494c5dd67472f/href</a></iframe><p>Below are some examples of predictions. The first image is the input image, the second one is the ground truth mask and the third one is the predicted mask.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*APw3ttHPcM-0NHEh93sE0w.png" /><figcaption>Here we see there are some miss predictions. But also some corner cells that are not present in ground truth are getting detected by the trained model.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CJ_7YUzVKvYUoxCHl073UQ.png" /></figure><p><strong>Summary</strong></p><p>In this post, I explained the basics of U-Net architecture and the loss functions used for it. Also how to use Keras generator for training an image segmentation task.</p><p>We also observed that with mere 20-30 training images, we are able to get very good results on the test set. I am planning to deep dive further to understand the root cause reasons for the model’s ability to learn this quickly and what are its limitations.</p><p>For the complete code for the above please refer jupyter notebook <a href="https://github.com/shivamshah/UNet-Basics/blob/master/UNetBasic.ipynb">link</a>.</p><p>Do you have any questions?<br>Ask your questions in the comments below and I will do my best to answer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e633184602c8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>