1pip install torch torchvision albumentations scikit-learn matplotlib seaborn
2import torch
3from torchvision import transforms
4from PIL import Image
5from model import ImprovedPneumoniaCNN # make sure model is defined/imported
6
7# Load model
8model = ImprovedPneumoniaCNN()
9model.load_state_dict(torch.load("improved_pneumonia_cnn.pth", map_location=torch.device('cpu')))
10model.eval()
11
12# Preprocess image
13transform = transforms.Compose([
14 transforms.Grayscale(),
15 transforms.Resize((224, 224)),
16 transforms.ToTensor(),
17])
18
19img = Image.open("path_to_chest_xray.jpg")
20img_tensor = transform(img).unsqueeze(0)
21
22# Predict
23with torch.no_grad():
24 output = model(img_tensor)
25 prediction = torch.sigmoid(output).item()
26 print("Pneumonia" if prediction > 0.5 else "Normal")
This model is licensed under
CC BY-NC 3.0.
For research and educational use only.