developerPushkal commited on
Commit
749e860
·
verified ·
1 Parent(s): 36deb5e

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +36 -27
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 numpy pillow
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
- # Load model architecture
34
  model = models.resnet18(pretrained=False)
 
35
 
36
- # Load weights
37
- model.load_state_dict(torch.load("resnet18_model.pth", map_location="cpu"))
 
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
- def predict_pneumonia(model, image_path):
52
- transform = transforms.Compose([
53
- transforms.Resize((224, 224)),
54
- transforms.ToTensor(),
55
- transforms.Normalize(mean=[0.5], std=[0.5])
56
- ])
57
-
58
- image = Image.open(image_path).convert("L") # Convert to grayscale
59
- image = transform(image).unsqueeze(0) # Add batch dimension
60
-
61
- with torch.no_grad():
62
- output = model(image)
63
-
64
- prediction = torch.argmax(output, dim=1).item()
65
- return "Pneumonia" if prediction == 1 else "Normal"
66
-
67
- # 🔹 **Test Prediction**
68
- image_path = "chest_xray_sample.jpg"
69
- result = predict_pneumonia(model, image_path)
70
- print(f"Prediction: {result}")
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