Views
No views yet
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
1import tensorflow as tf
2import numpy as np
3import cv2
4
5model = tf.saved_model.load("pneumonia_cnn_saved_model")
6infer = model.signatures['serving_default']
7class_names = ['PNEUMONIA', 'NORMAL']
8
9def preprocess_image(image_path):
10 img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
11 img = cv2.resize(img, (150, 150)) / 255.0
12 img = img.reshape(1, 150, 150, 1).astype(np.float32)
13 return img
14
15image = preprocess_image("path/to/xray.jpg")
16prediction = infer(tf.convert_to_tensor(image))['dense_1'].numpy()
17class_id = (prediction > 0.5).astype("int32")[0][0]
18print("Prediction: ", class_names[class_id], " Probability: "prediction[0][0]:.4f)