Skip to main content
In this guide, we evaluate multiple trained models to identify the best performer for deployment. We cover every step—from loading and preprocessing unseen data to executing real-time and batch inferences, and finally computing performance metrics.

Data Preparation and Transformation

Begin by importing the necessary modules and defining an image preprocessing transformation. These transformations ensure the image data is in the required input format during inference.

Defining the Dataset

Create a custom dataset class to load images along with their corresponding labels. In this example, the label encoding maps “malignant” to 0 and “benign” to 1.
After defining the dataset, instantiate it and create a DataLoader. The DataLoader processes the test data in batches (batch size of 32) without shuffling, which is standard for evaluation purposes.
Ensure that your CSV file and image directory path are correctly set to avoid file not found errors.

Loading the Model

Load your pre-trained model by defining its network architecture and then loading the checkpoint containing the trained weights. Below is a sample implementation of a breast cancer classification model.
After verifying that the model keys match, the model is ready for evaluation.

Real-Time Model Inference

Real-time inference processes a single prediction request instantly. The example below shows how to load an image, apply the transformation, and then perform inference to obtain a class prediction.

Batch Inference

Batch inference processes multiple images simultaneously. This approach mirrors the method used during training. The following code demonstrates batch processing using the DataLoader.
Since the dataset contains 100 images and the batch size is 32, the DataLoader returns three full batches and a final batch of 4 images. It is good practice to perform inference without tracking gradients to speed up processing and lower memory consumption:
You can also time these loops using magic commands like %%time in a Jupyter Notebook to compare performance.

Evaluating Model Performance with TorchMetrics

TorchMetrics is an excellent library for tracking various performance metrics during model evaluation. In the example below, we compute the accuracy based on simulated predictions for a three-class classification setup.

Evaluating on the Test Dataset with TorchMetrics

Integrate TorchMetrics directly into your test loop to evaluate the model on the entire test dataset. The code below sets the model to evaluation mode, disables gradient tracking, and then computes the overall accuracy.
If you run multiple evaluations in succession, reset the metric using the reset() method before each new update to ensure accurate tracking.

Custom Metric Calculation

In addition to TorchMetrics, you can implement a custom metric. The following example demonstrates how to calculate accuracy manually by comparing model predictions with true labels.

Summary

In this guide, we demonstrated the comprehensive process of model evaluation:
  • Data Preparation: Importing modules, setting up the transformation pipeline, and creating a custom dataset.
  • Model Loading: Initializing the network architecture and loading pre-trained weights.
  • Inference Techniques: Running both real-time single-image inference and batch inference.
  • Performance Metrics: Computing accuracy using both TorchMetrics and custom metric implementations to assess model performance.
While the demonstration produced an accuracy of around 49%—likely due to limited data and training epochs—the evaluation process detailed here can be extended to larger, more diverse datasets to ensure robust model performance prior to production deployment. Happy evaluating!

Watch Video

Practice Lab