 # Calculate train accuracy of the model in segmentation task

I think I don’t have a good understanding of train accuracy. This is the snippet for train the model and calculates the loss and train accuracy for segmentation task.

`````` for epoch in range(2):  # loop over the dataset multiple times

running_loss = 0
total_train = 0
correct_train = 0
for i, data in enumerate(train_loader, 0):
# get the inputs

# zeroes the gradient buffers of all parameters
# forward + backward + optimize
outputs = model(t_image) # forward
loss = criterion(outputs, mask) # calculate the loss
loss.backward() # back propagation
running_loss += loss.item()

# accuracy
_, predicted = torch.max(outputs.data, 1)
train_accuracy = 100 * correct_train / total_train

print('Epoch {}, train Loss: {:.3f}'.format(epoch, loss.item()), "Training Accuracy: %d %%" % (train_accuracy))
``````

I am getting a number like below for the training accuracy which I assume is the amount of pixel which corresponds with the ground truth (mask). is that correct?

``````Epoch 0, train Loss: 0.721 Training Accuracy: 500300 %
Epoch 0, train Loss: 0.707 Training Accuracy: 676000 %
``````

How can I calculate the accuracy of the network that shows the performance of the model? (ex. The accuracy of the network on the 10000 test images: 54 % in pytorch tutorial but this is for the classification task )

1 Like

You are currently summing all correctly predicted pixels and divide it by the batch size. To get a valid accuracy between 0 and 100% you should divide `correct_train` by the number of pixels in your batch.
Try to calculate `total_train` as `total_train += mask.nelement()`.

3 Likes

@ptrblck yes it works. Thanks a lot

1 Like

I this piece of code of yours

``````# accuracy
_, predicted = torch.max(outputs.data, 1)
``````

what are you trying to do in first line with torch.max especially why 1 and not 0. In 3rd line with predicted.eq()

1 Like

first line: return the indices of max values along rows in `softmax` probability output (`torch.max` returns a tuple containing the maximum value and the index of the maximum value within the tensor. Since the index in our case represents the classified category itself, we will only take that ignoring the actual probability).

second line: number of pixel in the batch

third line: By summing the output of the `.eq()` function, we get a count of the number of times the neural network has produced a correct output, and we take an accumulating sum of these correct predictions so that we can determine the accuracy of the network.

1 Like

I am getting 100% accuracy. I don’t know how to correct it. I am using the code of following link on my own dataset. The dataset set 400 training images and 120 validation images. Please help me.

Are you concerned about possible errors in the calculation of the accuracy or are you concerned you might have another error in the code, e.g. data leakage?

Yes i’m concerned about possible errors in the calculation of the accuracy. On each model i’m getting 100% accuracy after some epochs i think there some over fitting or some issue with accuracy calculation but i don’t know how to correct.

Could you post your code to calculate the accuracy by wrapping it in three backticks ``` so that we could have a look?

I’m using the same code as in the link.

// Code To Calculate Accuarcy.

import numpy as np

class runningScore(object):
def init(self, n_classes):
self.n_classes = n_classes
self.confusion_matrix = np.zeros((n_classes, n_classes))

``````def _fast_hist(self, label_true, label_pred, n_class):
mask = (label_true >= 0) & (label_true < n_class)
hist = np.bincount(
).reshape(n_class, n_class)
return hist

def update(self, label_trues, label_preds):
for lt, lp in zip(label_trues, label_preds):
self.confusion_matrix += self._fast_hist(lt.flatten(), lp.flatten(), self.n_classes)

def get_scores(self):
"""Returns accuracy score evaluation result.
- overall accuracy
- mean accuracy
- mean IU
- fwavacc
"""
hist = self.confusion_matrix
acc = np.diag(hist).sum() / hist.sum()
acc_cls = np.diag(hist) / hist.sum(axis=1)
acc_cls = np.nanmean(acc_cls)
iu = np.diag(hist) / (hist.sum(axis=1) + hist.sum(axis=0) - np.diag(hist))
mean_iu = np.nanmean(iu)
freq = hist.sum(axis=1) / hist.sum()
fwavacc = (freq[freq > 0] * iu[freq > 0]).sum()
cls_iu = dict(zip(range(self.n_classes), iu))

return (
{
"Overall Acc: \t": acc,
"Mean Acc : \t": acc_cls,
"FreqW Acc : \t": fwavacc,
"Mean IoU : \t": mean_iu,
},
cls_iu,
)

def reset(self):
self.confusion_matrix = np.zeros((self.n_classes, self.n_classes))
``````

class averageMeter(object):
“”“Computes and stores the average and current value”""

``````def __init__(self):
self.reset()

def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0

def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count``````

Hello can you tell me how should i calculate accuracy against MSELoss function ?

How would you like to define your accuracy?
If you are dealing with a multi-class classification use case, you could compare the predictions to the targets via:

``````output = torch.randn(10, 10)
target = torch.randint(0, 10, (10,))
preds = torch.argmax(output, 1)
acc = preds.eq(target).float().mean()
``````

Usually you wouldn’t use `nn.MSELoss` for a classification use case, so we would need some information about your use case.

I have a dataset of wheat Crop n which i have two clases Ear and Spikelet. when i use crossEntropyLoss function i will get the following error.

when i change
target_var = Variable(target.float())
to
target_var = Variable(target.long())

i will get the following error

when i tried to use your code. i will get the following error.

hello can you mention your input size if you are having the problem right now.it is a like an 6 months after completing this thread