Segmantation with FCN8s

username: David_Jitca
cooked: <p>I have a model, FCN8s, that makes a binary segmentation for a dataset with random objects, person. I obtain the segmantation I have like, 59% Intersection over Union accuracy.<br>
After this i take the weights and try to segmentate brain tumors.<br>
The train loss is decreasing, test loss is decreasing and accuracy (intersection over union) is decreasing (it doesnt exist to say ) 0.03% …Anyone have an idea?</p>

username: Geeks_Sid
cooked: <p>Accuracy shouldn’t decrease. Do you mind posting your loss function?</p>

username: David_Jitca
cooked: <p>I am using CrossEntropy loss.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/5/54a0352044314dabb6cd43e56af2520279a94883.png" data-download-href="https://discuss.pytorch.org/uploads/default/54a0352044314dabb6cd43e56af2520279a94883" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/5/54a0352044314dabb6cd43e56af2520279a94883_2_690x388.png" alt="image" data-base62-sha1="c4DlJwPL2Gq1lqkz6YPbZFPNiF5" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/5/54a0352044314dabb6cd43e56af2520279a94883_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/2X/5/54a0352044314dabb6cd43e56af2520279a94883_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/5/54a0352044314dabb6cd43e56af2520279a94883_2_1380x776.png 2x" data-dominant-color="EFEFF0"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1920×1080 153 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
First plot test loss, accuracy and last plot train loss. After three epochs</p>

username: Geeks_Sid
cooked: <p>Change your metrics and loss.</p>
<p>Metric - Dice or IOU<br>
loss - Dice</p>
<p>Give it a try.</p>

username: David_Jitca
cooked: <p>As I say, im using IOU accuracy… But on the other dataset i managed to obtain 59%, i dont understand why on this doesnt work</p>

username: Geeks_Sid
cooked: <p>Different problems require different metrics.</p>

username: David_Jitca
cooked: <p>Hi, i found this los</p>
<pre><code class="lang-auto">def dice_loss(logits, true, eps=1e-7):
    """Computes the Sørensen–Dice loss.
    Note that PyTorch optimizers minimize a loss. In this
    case, we would like to maximize the dice loss so we
    return the negated dice loss.
    Args:
        true: a tensor of shape [B, 1, H, W].
        logits: a tensor of shape [B, C, H, W]. Corresponds to
            the raw output or logits of the model.
        eps: added to the denominator for numerical stability.
    Returns:
        dice_loss: the Sørensen–Dice loss.
    """
    num_classes = logits.shape[1]
    if num_classes == 1:
        true_1_hot = torch.eye(num_classes + 1)[true.squeeze(1)]
        true_1_hot = true_1_hot.permute(0, 3, 1, 2).float()
        true_1_hot_f = true_1_hot[:, 0:1, :, :]
        true_1_hot_s = true_1_hot[:, 1:2, :, :]
        true_1_hot = torch.cat([true_1_hot_s, true_1_hot_f], dim=1)
        pos_prob = torch.sigmoid(logits)
        neg_prob = 1 - pos_prob
        probas = torch.cat([pos_prob, neg_prob], dim=1)
    else:
        true_1_hot = torch.eye(num_classes)[true.squeeze(1)]
        true_1_hot = true_1_hot.permute(0, 3, 1, 2).float()
        probas = F.softmax(logits, dim=1)
    true_1_hot = true_1_hot.type(logits.type())
    dims = (0,) + tuple(range(2, true.ndimension()))
    intersection = torch.sum(probas * true_1_hot, dims)
    cardinality = torch.sum(probas + true_1_hot, dims)
    dice_loss = (2. * intersection / (cardinality + eps)).mean()
    return (1 - dice_loss)
</code></pre>
<p>And …my network doesnt improve…:(( the loss comes down from 0.64 to 0.50 after first batch and its decreasing very slow, (but after the third decimal 0,50x ), in conclusion the mean of loss its 0,5 …</p>

username: Geeks_Sid
cooked: <p>How long have you let your network train?</p>

username: David_Jitca
cooked: <p>I have trained for six epochs, but after the first iteration the predicted pixels are 0, and accuracy 0 (this happend also with cross). I have tried Adam with 0,001 … 0,000001 lr. With sgd after 6 epochs i have had an accuracy of 0,07 very small but wasnt improving the loss decrease very slow. I have rised the lr to 0.1 and the the accuracy become 0.</p>

username: Geeks_Sid
cooked: <p>Try it for a longer period of time. Because maybe your network is just learning that slow.</p>
<p>Try adam : 0.005 with learning rate decay<br>
Try SGD : 0.05 with learning rate decay</p>
<p>for lets say 30 epochs. If it doesn’t work, then maybe we gotta debug the problem more.</p>

username: David_Jitca
cooked: <p>Also my images are 512x512 and i use a batch of 10 images(it uses the full colab gpu)and it takes like 7 minute per epoch.<br>
So i reduce image to 256x256 or 128x128… but not the resize from pytorch which use interpolation, i take pixel from 4 to 4 on colums and rows (that for 128x128) or 2 by 2 (for 256x256) to train with a batch of 30. I dont think this is a big problem, this was recommended by a teacher.</p>
<p>And i used scheduler for adam lr, but if i use scheduler after every epcoch it doenst change the lr, but if i use after iterations its changes too much like lr = 0.0000005</p>

username: Geeks_Sid
cooked: <p>use a scheduler with a validation metric</p>
<blockquote>
<h1>Setting up an optimizer lr reducer</h1>
</blockquote>
<pre><code>scheduler = ReduceLROnPlateau(optimizer, mode='max', factor=0.5,
                              patience=int(params['patience']),
                              verbose=True)
# Making sure that the learning rate scheduler keeps track
def testfunc():
    """
    .................
    """
    scheduler.step(test_average_loss)
</code></pre>
<p>Try this for lr decay.</p>

username: David_Jitca
cooked: <aside class="quote no-group" data-username="Geeks_Sid" data-post="12" data-topic="46350">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/geeks_sid/48/10013_2.png" class="avatar"> Geeks_Sid:</div>
<blockquote>
<p>ReduceLROnPlateau</p>
</blockquote>
</aside>
<p>Hmm I’m using pytorch, how to use a scheduler with validation metric?</p>

username: Geeks_Sid
cooked: <p>This is in PyTorch</p>
<pre><code class="lang-auto">scheduler.step(validation_metric_in_here_as_a_number_probably)
</code></pre>
<p>Just type your validation metric in here.</p>