CoolFace
Modelpublic

AventIQ-AI/resnet18-cataract-detection-system

sourceHugging Faceupdated 2y agoView on Hugging Face
2likes
Model Card

๐Ÿฉบ ResNet-18 Cataract Detection System

This repository hosts a quantized version of ResNet-18-based model optimized for cataract detection having two labels either normal or cataract. The model detects images into these 2 labels.


๐Ÿ“Œ Model Details

  • โ€”Model Architecture: ResNet-18
  • โ€”Task: Cataract Detection System
  • โ€”Dataset: Cataract Dataset (Kaggle)
  • โ€”Framework: PyTorch
  • โ€”Input Image Size: 224x224
  • โ€”Number of Classes: 2 ---

๐Ÿš€ Usage

Installation

bash
pip install torch torchvision pillow

Loading the Model

python
import torch
import torchvision.models as models
from huggingface_hub import hf_hub_download
import json
from PIL import Image
import torchvision.transforms as transforms

device = "cuda" if torch.cuda.is_available() else "cpu"

weights_path = hf_hub_download(repo_id="AventIQ-AI/resnet18-cataract-detection-system", filename="cataract_detection_resnet18_quantized.pth")
labels_path = hf_hub_download(repo_id="AventIQ-AI/resnet18-cataract-detection-system", filename="class_names.json")

with open(labels_path, "r") as f:
    class_labels = json.load(f)

model = models.resnet18(pretrained=False)

num_classes = len(class_labels)
model.fc = torch.nn.Linear(in_features=512, out_features=num_classes)

model.load_state_dict(torch.load(weights_path, map_location=torch.device('cpu')))

model.eval()

print("Model loaded successfully!")

๐Ÿ” Perform Classification

python

transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

def predict_image(image_path):
    image = Image.open(image_path).convert("RGB")
    image = transform(image).unsqueeze(0).to(device)  # Add batch dimension

    with torch.no_grad():
        outputs = model(image)
        _, predicted_class = torch.max(outputs, 1)
    
    predicted_label = class_labels[predicted_class.item()]
    print(f"Predicted Class: {predicted_label}")

# Example usage:
image_path = "your_image_path"  
predict_image(image_path)

๐Ÿ“Š Evaluation Results

After fine-tuning, the model was evaluated on the Chest X-ray Pneumonia Dataset, achieving the following performance:

**Metric****Score**
Accuracy97.52%
Precision98.31%
Recall96.67%
F1-Score97.48%

๐Ÿ”ง Fine-Tuning Details

Dataset

The model was trained on Cataract Dataset having two labels.

Training Configuration

  • โ€”Number of epochs: 10
  • โ€”Batch size: 32
  • โ€”Optimizer: Adam
  • โ€”Learning rate: 1e-4
  • โ€”Loss Function: Cross-Entropy
  • โ€”Evaluation Strategy: Validation at each epoch ---

โš ๏ธ 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.