2
$\begingroup$

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

$\endgroup$
2
$\begingroup$
_, 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.

$\endgroup$
  • $\begingroup$ Also, I think there is a simple edit in torch.max(output.data,1). 1 is the dimension across which the maximum value has to be found $\endgroup$ – Mark Dec 11 '19 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.