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

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with datascience.stackexchange
scroll top