This repository contains a script for performing inference using a pre-trained ResNet-50 model. The model is used to classify objects in images based on the ImageNet dataset.
Before running the inference script, make sure you have the following dependencies installed:
pip install torch torchvisionobjdetection_inference.py: The main inference script.resnet50.pth: Pre-trained model weights (ensure this file is present in the directory).demo.jpg: Sample image for testing the inference script.
-
Prepare the Model and Image
- Ensure
resnet50.pthis in the same directory as the script. - Place the test image (e.g.,
demo.jpg) in the directory.
- Ensure
-
Run the Inference Script
python objdetection_inference.py
-
Expected Output The script will output the top 5 predicted classes along with their confidence scores. Example output:
Top Predictions: Labrador retriever: 85.2% Golden retriever: 7.4% Flat-coated retriever: 3.2%
-
Load the Pre-trained Model
- The script loads the ResNet-50 architecture.
- The model weights are loaded from
resnet50.pth. - The model is set to evaluation mode to disable gradient updates.
-
Preprocess the Input Image
- The image is read using
torchvision.io.read_image(). - Image resizing and normalization are applied using
torchvision.transforms. - The image is converted into a tensor suitable for ResNet.
- The image is read using
-
Perform Inference
- The processed image is passed through the model.
- Softmax is applied to get class probabilities.
- The top 5 predictions are displayed with confidence scores.
- If you encounter errors with missing model weights, ensure
resnet50.pthis downloaded correctly. - Modify
image_pathinobjdetection_inference.pyto test with different images. - The model is based on ImageNet categories.
The ResNet-50 model is provided by PyTorch under its respective license.