Update README.md
Browse files
README.md
CHANGED
|
@@ -21,7 +21,7 @@ This repository hosts a fine-tuned **ResNet-18-based** model optimized for **pne
|
|
| 21 |
### **Installation**
|
| 22 |
|
| 23 |
```bash
|
| 24 |
-
pip install torch torchvision
|
| 25 |
```
|
| 26 |
|
| 27 |
### **Loading the Model**
|
|
@@ -30,14 +30,19 @@ pip install torch torchvision numpy pillow
|
|
| 30 |
import torch
|
| 31 |
import torchvision.models as models
|
| 32 |
|
| 33 |
-
#
|
| 34 |
model = models.resnet18(pretrained=False)
|
|
|
|
| 35 |
|
| 36 |
-
# Load weights
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
# Set to evaluation mode
|
| 40 |
model.eval()
|
|
|
|
|
|
|
|
|
|
| 41 |
```
|
| 42 |
|
| 43 |
---
|
|
@@ -45,30 +50,34 @@ model.eval()
|
|
| 45 |
### **🔍 Perform Pneumonia Detection**
|
| 46 |
|
| 47 |
```python
|
| 48 |
-
from torchvision import transforms
|
| 49 |
from PIL import Image
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
---
|
| 74 |
|
|
|
|
| 21 |
### **Installation**
|
| 22 |
|
| 23 |
```bash
|
| 24 |
+
pip install torch torchvision pillow
|
| 25 |
```
|
| 26 |
|
| 27 |
### **Loading the Model**
|
|
|
|
| 30 |
import torch
|
| 31 |
import torchvision.models as models
|
| 32 |
|
| 33 |
+
# Step 1: Define the model architecture (Must match the trained model)
|
| 34 |
model = models.resnet18(pretrained=False)
|
| 35 |
+
model.fc = torch.nn.Linear(in_features=512, out_features=2) # Ensure output matches 2 classes
|
| 36 |
|
| 37 |
+
# Step 2: Load the fine-tuned model weights
|
| 38 |
+
model_path = "/content/chest_xray_pneumonia_model.pth" # Ensure the file is in the same directory
|
| 39 |
+
model.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
|
| 40 |
|
| 41 |
+
# Step 3: Set model to evaluation mode
|
| 42 |
model.eval()
|
| 43 |
+
|
| 44 |
+
print("✅ Model loaded successfully and ready for inference!")
|
| 45 |
+
|
| 46 |
```
|
| 47 |
|
| 48 |
---
|
|
|
|
| 50 |
### **🔍 Perform Pneumonia Detection**
|
| 51 |
|
| 52 |
```python
|
|
|
|
| 53 |
from PIL import Image
|
| 54 |
+
import torchvision.transforms as transforms
|
| 55 |
|
| 56 |
+
# Load the image
|
| 57 |
+
image_path = "/content/Screenshot 2025-03-04 104637.png" # Replace with your test image
|
| 58 |
+
image = Image.open(image_path).convert("RGB") # Ensure 3-channel format
|
| 59 |
+
|
| 60 |
+
# Define preprocessing (same as used during training)
|
| 61 |
+
transform = transforms.Compose([
|
| 62 |
+
transforms.Resize((224, 224)), # Resize to match model input
|
| 63 |
+
transforms.ToTensor(),
|
| 64 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 65 |
+
])
|
| 66 |
+
|
| 67 |
+
# Apply transformations
|
| 68 |
+
image = transform(image).unsqueeze(0) # Add batch dimension
|
| 69 |
+
|
| 70 |
+
# Perform inference
|
| 71 |
+
with torch.no_grad():
|
| 72 |
+
output = model(image)
|
| 73 |
+
|
| 74 |
+
# Convert output to class prediction
|
| 75 |
+
predicted_class = torch.argmax(output, dim=1).item()
|
| 76 |
+
|
| 77 |
+
# Map the predicted class to labels (Modify if needed)
|
| 78 |
+
class_labels = {0: "Normal (Healthy)", 1: "Pneumonia"}
|
| 79 |
+
|
| 80 |
+
print(f"✅ Predicted Class: {class_labels.get(predicted_class, 'Unknown')}")
|
| 81 |
|
| 82 |
---
|
| 83 |
|