MATLAB Answers

How to visualize the predicted values of segmentation from softmax output?

8 views (last 30 days)
I want to know (visualize) what value, network has predicted during training.
i used Dice pixel classfication layer to observe this but instead of predicted value it shows the original image. Its written that T is target which means T is ground truth and Y is predicted which means predicted pixels.
Code:
classdef dicePixelClassificationLayer < nnet.layer.ClassificationLayer
% This layer implements the generalized Dice loss function for training
% semantic segmentation networks.
properties(Constant)
% Small constant to prevent division by zero.
Epsilon = 1e-8;
end
methods
function layer = dicePixelClassificationLayer(name)
% layer = dicePixelClassificationLayer(name) creates a Dice
% pixel classification layer with the specified name.
% Set layer name.
layer.Name = name;
% Set layer description.
layer.Description = 'Dice loss';
end
function loss = forwardLoss(layer, Y, T)
% loss = forwardLoss(layer, Y, T) returns the Dice loss between
% the predictions Y and the training targets T.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
code used to visualize the predictions Y and the training targets T
T1=T(:,:,1);
Y1=Y(:,:,1);
subplot(1,2,1)
imshow(T1)
subplot(1,2,2)
imshow(Y1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Weights by inverse of region size.
W = 1 ./ sum(sum(T,1),2).^2;
intersection = sum(sum(Y.*T,1),2);
union = sum(sum(Y.^2 + T.^2, 1),2);
numer = 2*sum(W.*intersection,3) + layer.Epsilon;
denom = sum(W.*union,3) + layer.Epsilon;
% Compute Dice score.
dice = numer./denom;
% Return average Dice loss.
N = size(Y,4);
loss = sum((1-dice))/N;
end
function dLdY = backwardLoss(layer, Y, T)
% dLdY = backwardLoss(layer, Y, T) returns the derivatives of
% the Dice loss with respect to the predictions Y.
% Weights by inverse of region size.
W = 1 ./ sum(sum(T,1),2).^2;
intersection = sum(sum(Y.*T,1),2);
union = sum(sum(Y.^2 + T.^2, 1),2);
numer = 2*sum(W.*intersection,3) + layer.Epsilon;
denom = sum(W.*union,3) + layer.Epsilon;
N = size(Y,4);
dLdY = (2*W.*Y.*numer./denom.^2 - 2*W.*T./denom)./N;
end
end
end
left is groundtruth and right side is origianl image, which supoosed to be predicted one. now how can i see the predicted one?

  0 Comments

Sign in to comment.

Accepted Answer

Mahesh Taparia
Mahesh Taparia on 15 Sep 2020
Hi
The softmax layer gives the probability of the predicted class. To get the segmented result from that, you can put the probabilistic threshold of 0.5. For example:
P=T1(:,:,1)>0.5;
Else, in order to evaluate the segmented result, you can use the 'semanticseg' function. For more information, you can refer this documentation. Hope it wil help!

  2 Comments

Raza Ali
Raza Ali on 16 Sep 2020
Thank you for the answer.
What is Y and T?
defined in loss fucntion? Are they ground truth (T) and predicted values (Y)?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by