Showing predicted segmentation

You can add the cross entropy and the dice losses.

Okay I will try doing that. Thank you so much for your patience.

One more question, is dice loss readily available just like cross entropy?

I don’t think it is present in the main framework, but there are numerous libraries that implement it. This is a very good pytorch library for segmentation that has also implemented.

I tried searching for dice loss that was implemented in pytorch and found something in here DiceLoss-PyTorch/loss.py at master · hubutui/DiceLoss-PyTorch · GitHub do you think it is a good idea to use this code?

The code seems correct but I think it needs one hot targets. You can try this one too, from the previous library I mentioned.

You are really a great help! I’m kind of lost right now because it is my first time to get to know dice loss. Do you think this recent link might be suitable for my target?

Hi! I tried implementing IoU instead of dice because it was easier for me to understand IoU than dice. But when I tried printing the return value of IoU, the value doesnt make any sense. Isnt it should be between 0-1?

``````# PyTroch version

SMOOTH = 1e-6

# You can comment out this line if you are passing tensors of equal shape
# But if you are passing output from UNet or something it will most probably
# be with the BATCH x 1 x H x W shape
output = output.squeeze(1)  # BATCH x 1 x H x W => BATCH x H x W

intersection = (output * mask).float().sum()  # Will be zero if Truth=0 or Prediction=0
union = (output + mask).float().sum()         # Will be zzero if both are 0

iou = (intersection + SMOOTH) / (union + SMOOTH)  # We smooth our devision to avoid 0/0

thresholded = torch.clamp(20 * (iou - 0.5), 0, 10).ceil() / 10  # This is equal to comparing with thresolds

return thresholded  # Or thresholded.mean() if you are interested in average across the batch

import torch.optim as optim
optimizer = optim.Adam(model.parameters(), lr = 0.001)
loss_CE = nn.CrossEntropyLoss()

BATCH_SIZE = 4
EPOCHS = 2

def train(model):
model.train()
for epoch in range(EPOCHS):
for i in tqdm(range(0, len(img_train), BATCH_SIZE)):
batch_img_train = img_train[i:i+BATCH_SIZE].view(-1, 3, 112, 112)

outputs = model(batch_img_train)

loss = loss_iou + loss_ce

loss.backward()
optimizer.step()    # Does the update

print("Loss_IoU", loss_iou)
print("Loss_ce", loss_ce)
print(f"Epoch: {epoch}. Loss: {loss}")

train(model)``````

I think the union equation is wrong. I think it should be…

``````union = (output + mask).float().sum() - intersection
``````

Tho I made the necessary formula correction, the value I get does not make any sense still. Do I really need to add the IoU loss to Cross entropy loss? or just get their average?

Have you used softmax on the output ?

I am using a network architecture called ESNet and it goes like this:

``````###################################################################################################
#ESNet: An Efficient Symmetric Network for Real-time Semantic Segmentation
###################################################################################################

import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
from torchsummary import summary

class DownsamplerBlock(nn.Module):
def __init__(self, ninput, noutput):
super().__init__()

self.conv = nn.Conv2d(ninput, noutput-ninput, (3, 3), stride=2, padding=1, bias=True)
self.pool = nn.MaxPool2d(2, stride=2)
self.bn = nn.BatchNorm2d(noutput, eps=1e-3)
self.relu = nn.ReLU(inplace=True)

def forward(self, input):
x1 = self.pool(input)
x2 = self.conv(input)

diffY = x2.size()[2] - x1.size()[2]
diffX = x2.size()[3] - x1.size()[3]

x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2,
diffY // 2, diffY - diffY // 2])

output = torch.cat([x2, x1], 1)
output = self.bn(output)
output = self.relu(output)
return output

class UpsamplerBlock (nn.Module):
def __init__(self, ninput, noutput):
super().__init__()

self.bn = nn.BatchNorm2d(noutput, eps=1e-3)

def forward(self, input):

output = self.conv(input)
output = self.bn(output)

return F.relu(output)

class FCU(nn.Module):
def __init__(self, chann, kernel_size,dropprob, dilated):
"""
Factorized Convolution Unit
"""
super(FCU,self).__init__()

self.conv3x1_1 = nn.Conv2d(chann, chann, (kernel_size,1), stride=1, padding=(int((kernel_size-1)//2)*1,0), bias=True)

self.conv1x3_1 = nn.Conv2d(chann, chann, (1,kernel_size), stride=1, padding=(0,int((kernel_size-1)//2)*1), bias=True)

self.bn1 = nn.BatchNorm2d(chann, eps=1e-03)

self.conv1x3_2 = nn.Conv2d(chann, chann, (1,kernel_size), stride=1, padding=(0,padding), bias=True, dilation = (1, dilated))

self.bn2 = nn.BatchNorm2d(chann, eps=1e-03)

self.relu = nn.ReLU(inplace = True)
self.dropout = nn.Dropout2d(dropprob)

def forward(self, input):
residual = input
output = self.conv3x1_1(input)
output = self.relu(output)
output = self.conv1x3_1(output)
output = self.bn1(output)
output = self.relu(output)

output = self.conv3x1_2(output)
output = self.relu(output)
output = self.conv1x3_2(output)
output = self.bn2(output)

if (self.dropout.p != 0):
output = self.dropout(output)

return F.relu(residual+output,inplace=True)

class PFCU(nn.Module):
def __init__(self,chann):
"""
Parallel Factorized Convolution Unit
"""

super(PFCU,self).__init__()

self.conv3x1_1 = nn.Conv2d(chann, chann, (3,1), stride=1, padding=(1,0), bias=True)

self.conv1x3_1 = nn.Conv2d(chann, chann, (1,3), stride=1, padding=(0,1), bias=True)

self.bn1 = nn.BatchNorm2d(chann, eps=1e-03)

self.conv3x1_22 = nn.Conv2d(chann, chann, (3,1), stride=1, padding=(2,0), bias=True, dilation = (2,1))
self.conv1x3_22 = nn.Conv2d(chann, chann, (1,3), stride=1, padding=(0,2), bias=True, dilation = (1,2))

self.conv3x1_25 = nn.Conv2d(chann, chann, (3,1), stride=1, padding=(5,0), bias=True, dilation = (5,1))
self.conv1x3_25 = nn.Conv2d(chann, chann, (1,3), stride=1, padding=(0,5), bias=True, dilation = (1,5))

self.conv3x1_29 = nn.Conv2d(chann, chann, (3,1), stride=1, padding=(9,0), bias=True, dilation = (9,1))
self.conv1x3_29 = nn.Conv2d(chann, chann, (1,3), stride=1, padding=(0,9), bias=True, dilation = (1,9))

self.bn2 = nn.BatchNorm2d(chann, eps=1e-03)

self.dropout = nn.Dropout2d(0.3)

def forward(self, input):
residual = input
output = self.conv3x1_1(input)
output = F.relu(output)
output = self.conv1x3_1(output)
output = self.bn1(output)
output = F.relu(output)

output2 = self.conv3x1_22(output)
output2 = F.relu(output2)
output2 = self.conv1x3_22(output2)
output2 = self.bn2(output2)
if (self.dropout.p != 0):
output2 = self.dropout(output2)

output5 = self.conv3x1_25(output)
output5 = F.relu(output5)
output5 = self.conv1x3_25(output5)
output5 = self.bn2(output5)
if (self.dropout.p != 0):
output5 = self.dropout(output5)

output9 = self.conv3x1_29(output)
output9 = F.relu(output9)
output9 = self.conv1x3_29(output9)
output9 = self.bn2(output9)
if (self.dropout.p != 0):
output9 = self.dropout(output9)

return F.relu(residual+output2+output5+output9,inplace=True)

class ESNet(nn.Module):
def __init__(self, classes):
super().__init__()
#-----ESNET---------#
self.initial_block = DownsamplerBlock(3,16)

self.layers = nn.ModuleList()

for x in range(0, 3):
self.layers.append(FCU(16, 3, 0.03, 1))

self.layers.append(DownsamplerBlock(16,64))

for x in range(0, 2):
self.layers.append(FCU(64, 5, 0.03, 1))

self.layers.append(DownsamplerBlock(64,128))

for x in range(0, 3):
self.layers.append(PFCU(chann=128))

self.layers.append(UpsamplerBlock(128,64))
self.layers.append(FCU(64, 5, 0, 1))
self.layers.append(FCU(64, 5, 0, 1))

self.layers.append(UpsamplerBlock(64,32))
self.layers.append(FCU(32, 3, 0, 1))
self.layers.append(FCU(32, 3, 0, 1))

def forward(self, input):
output = self.initial_block(input)

for layer in self.layers:
output = layer(output)

output = self.output_conv(output)

return output

"""print layers and params of network"""
if __name__ == '__main__':
model = ESNet(classes=61)
summary(model,(3,224,224))``````

Hi, I have recently used U-Net for a segmentation task where I used a mixture of cross-entropy and dice loss. You can find the code here. I hope it would be helpful.