|
|
|
|
|
--- |
|
|
title: Pneumonia CNN Inference |
|
|
emoji: 🩺 |
|
|
colorFrom: blue |
|
|
colorTo: indigo |
|
|
sdk: gradio |
|
|
sdk_version: 4.44.0 |
|
|
app_file: app.py |
|
|
pinned: false |
|
|
python_version: 3.10 |
|
|
short_description: Classify chest X-ray images as Pneumonia or Normal. |
|
|
tags: |
|
|
- medical-imaging |
|
|
- pneumonia-detection |
|
|
- tensorflow |
|
|
- gradio |
|
|
--- |
|
|
|
|
|
# Pneumonia CNN Inference Space |
|
|
This Hugging Face Space uses a Keras CNN to classify chest X-ray images as Pneumonia or Normal. |
|
|
|
|
|
## How to Use |
|
|
1. Upload a chest X-ray image (JPEG) using the Gradio interface. |
|
|
2. View the prediction (PNEUMONIA or NORMAL) and probability. |
|
|
|
|
|
## Model Details |
|
|
- **Dataset**: chest-xray-pneumonia |
|
|
- **Test Accuracy**: ~90.54% |
|
|
- **Model Format**: TensorFlow SavedModel |
|
|
- **Classification Report**: |
|
|
``` |
|
|
|
|
|
precision recall f1-score support |
|
|
|
|
|
Pneumonia (Class 0) 0.95 0.89 0.92 390 |
|
|
Normal (Class 1) 0.84 0.92 0.88 234 |
|
|
|
|
|
accuracy 0.91 624 |
|
|
macro avg 0.90 0.91 0.90 624 |
|
|
weighted avg 0.91 0.91 0.91 624 |
|
|
|
|
|
``` |
|
|
|
|
|
## Local Inference |
|
|
```python |
|
|
import tensorflow as tf |
|
|
import numpy as np |
|
|
import cv2 |
|
|
|
|
|
model = tf.saved_model.load("pneumonia_cnn_saved_model") |
|
|
infer = model.signatures['serving_default'] |
|
|
class_names = ['PNEUMONIA', 'NORMAL'] |
|
|
|
|
|
def preprocess_image(image_path): |
|
|
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) |
|
|
img = cv2.resize(img, (150, 150)) / 255.0 |
|
|
img = img.reshape(1, 150, 150, 1).astype(np.float32) |
|
|
return img |
|
|
|
|
|
image = preprocess_image("path/to/xray.jpg") |
|
|
prediction = infer(tf.convert_to_tensor(image))['dense_1'].numpy() |
|
|
class_id = (prediction > 0.5).astype("int32")[0][0] |
|
|
print("Prediction: ", class_names[class_id], " Probability: "prediction[0][0]:.4f) |
|
|
``` |
|
|
|