Downloads · 30 days
0
AventIQ-AI/ResNet-18-pneumonia-detection
ResNet-18-pneumonia-detection is a machine learning model from AventIQ-AI. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
This repository hosts a fine-tuned ResNet-18-based model optimized for pneumonia detection from chest X-ray images. The model classifies images into two categories: Normal and Pneumonia.
Downloads · 30 days
0
Access
Public
Updated Mar 4, 2025
Repo size
44.8 MB
Likes
4
Public
Click a slice to open those files.
.pth44.8 MB · 100%
From the Hugging Face model README
This repository hosts a fine-tuned ResNet-18-based model optimized for pneumonia detection from chest X-ray images. The model classifies images into two categories: Normal and Pneumonia.
pip install torch torchvision pillow
import torch
import torchvision.models as models
# Step 1: Define the model architecture (Must match the trained model)
model = models.resnet18(pretrained=False)
model.fc = torch.nn.Linear(in_features=512, out_features=2) # Ensure output matches 2 classes
# Step 2: Load the fine-tuned model weights
model_path = "/content/chest_xray_pneumonia_model.pth" # Ensure the file is in the same directory
model.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
# Step 3: Set model to evaluation mode
model.eval()
print("✅ Model loaded successfully and ready for inference!")
from PIL import Image
import torchvision.transforms as transforms
# Load the image
image_path = "/content/Screenshot 2025-03-04 104637.png" # Replace with your test image
image = Image.open(image_path).convert("RGB") # Ensure 3-channel format
# Define preprocessing (same as used during training)
transform = transforms.Compose([
transforms.Resize((224, 224)), # Resize to match model input
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# Apply transformations
image = transform(image).unsqueeze(0) # Add batch dimension
# Perform inference
with torch.no_grad():
output = model(image)
# Convert output to class prediction
predicted_class = torch.argmax(output, dim=1).item()
# Map the predicted class to labels (Modify if needed)
class_labels = {0: "Normal (Healthy)", 1: "Pneumonia"}
print(f"✅ Predicted Class: {class_labels.get(predicted_class, 'Unknown')}")
After fine-tuning, the model was evaluated on the Chest X-ray Pneumonia Dataset, achieving the following performance:
| Metric | Score |
|---|---|
| Accuracy | 80.4% |
| Precision | 78.2% |
| Recall | 75.8% |
| F1-Score | 79.5% |
| Inference Speed | Fast (Optimized with FP16) |
The model was trained on Chest X-ray images with labeled cases of Normal and Pneumonia patients.
The model was quantized using FP16 precision, reducing latency and memory usage while maintaining high accuracy.