Explanation behind the calculation of accuracy in deep learning model
-
20-10-2020 - |
Question
I am trying to model an image segmentation problem using convolutional neural network. I came across code in Github which I am not able to understand the meaning of following lines of codes for calculation of accuracy -
def new_test(loaders,model,criterion,use_cuda):
for batch_idx, (data,target) in enumerate(loaders):
output = model(data)
###Accuracy
_, predicted = torch.max(output.data, 1)
total_train += target.nelement()
correct_train += predicted.eq(target.data).sum().item()
model(data)
outputs a tensor of shape B * N * H * W
B = Batch Size
N = Number of segmentated classes
H,W = Height,Width of an image
Solution
_, predicted = torch.max(output.data, 1)
On the first line above, it uses torch.max()
to find all cases in output.data
along the dimension - 1(i.e along each row). In addition, torch.max()
returns (values, indices)
, so only the indices are stored in a variable called predicted
while the values are not stored.
total_train += target.nelement()
The second line above just tracks the total size of the train set into a variable called total_train
based on the number of elements in target
correct_train += predicted.eq(target.data).sum().item()
The third line above takes the predicted
variable, which contains all indices of when the output is greater than 1, checks it against target.data
, and sums up all items that are equal. In other words, it is counting how many of the predicted values are equal to the target data.