YAML Metadata
Warning:
empty or missing yaml metadata in repo card
(https://huggingface.co/docs/hub/model-cards#model-card-metadata)
π©Ί ResNet-18 Pneumonia Detection Model
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.
π Model Details
- Model Architecture: ResNet-18
- Task: Pneumonia Detection
- Dataset: Chest X-ray Pneumonia Dataset (Kaggle)
- Framework: PyTorch
- Input Image Size: 224x224
- Number of Classes: 2 (Normal, Pneumonia)
- Quantization: FP16 (for efficiency)
π Usage
Installation
pip install torch torchvision pillow
Loading the Model
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!")
π Perform Pneumonia Detection
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')}")
π Evaluation Results
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) |
π§ Fine-Tuning Details
Dataset
The model was trained on Chest X-ray images with labeled cases of Normal and Pneumonia patients.
Training Configuration
- Number of epochs: 10
- Batch size: 16
- Optimizer: Adam
- Learning rate: 1e-4
- Loss Function: Cross-Entropy
- Evaluation Strategy: Validation at each epoch
Quantization
The model was quantized using FP16 precision, reducing latency and memory usage while maintaining high accuracy.
β οΈ Limitations
- Misclassification risk: The model may produce false positives or false negatives. Always verify results with a radiologist.
- Dataset bias: Performance may be affected by dataset distribution. It may not generalize well to different populations.
- Black-box nature: Like all deep learning models, it does not explain why a prediction was made.
Inference Providers
NEW
This model isn't deployed by any Inference Provider.
π
Ask for provider support