How to find test accuracy after training

Hi,
I’m a beginner and I trained a image classification model with training data and validation data using resnet50. But now I want to test my model performance on test data. Can anyone help me with this?

If you have the test data including the targets, you could reuse the validation approach and just swap the test data for the validation set.
Since the validation code should neither update the model not use any data augmentation techniques, it should be OK to use the same workflow for the test case.

The first thing you need to do is to separate your dataset into a training, validation and test set. For each of these you create a loader exactly like you’ve done for the training loader. Assuming you’ve done that and have a training_loader, validation_loader, and test_loader, you could then define a separate function to check the accuracy which will be general in the way that you just need to send in the loader you’ve created.

This could look something like this:

def check_accuracy(loader, model):
    num_correct = 0
    num_samples = 0
    model.eval()
    
    with torch.no_grad():
        for x, y in loader:
            x = x.to(device=device)
            y = y.to(device=device)
            
            scores = model(x)
            _, predictions = scores.max(1)
            num_correct += (predictions == y).sum()
            num_samples += predictions.size(0)
        
        print(f'Got {num_correct} / {num_samples} with accuracy {float(num_correct)/float(num_samples)*100:.2f}') 
    
    model.train()

Where you need to have defined device, but you could also send this to the function.

1 Like

yeah, Thank you so much @ptrblck

Thank so much @AladdinPerzon for sharing the code