|
a |
|
b/ensemble_methods.py |
|
|
1 |
import numpy as np |
|
|
2 |
from tensorflow.keras.models import load_model |
|
|
3 |
from tensorflow.keras.layers import Input |
|
|
4 |
from tensorflow.keras.preprocessing import image |
|
|
5 |
|
|
|
6 |
test_image_path = 'X-ray Images/train/Pneumonia-Bacterial/Pneumonia-Bacterial (1).jpg' |
|
|
7 |
img = image.load_img(test_image_path, target_size=(224, 224)) |
|
|
8 |
img_array = image.img_to_array(img) |
|
|
9 |
img_array = np.expand_dims(img_array, axis=0) |
|
|
10 |
|
|
|
11 |
# Load the trained models |
|
|
12 |
cnn_model = load_model('Deep Learning Models/CNN-CUSTOM.h5') |
|
|
13 |
vgg16_model = load_model('Deep Learning Models/Feature-Extraction-VGG16.h5') |
|
|
14 |
vgg19_model = load_model('Deep Learning Models/VGG19.h5') |
|
|
15 |
resnet50_model = load_model('Deep Learning Models/ResNet50.h5') |
|
|
16 |
|
|
|
17 |
# Create input tensor |
|
|
18 |
input_tensor = Input(shape=(224, 224, 3)) |
|
|
19 |
|
|
|
20 |
# Preprocess the image |
|
|
21 |
img_array = img_array / 255.0 # Normalize pixel values |
|
|
22 |
|
|
|
23 |
# Make predictions with each model |
|
|
24 |
cnn_predictions = cnn_model.predict(img_array) |
|
|
25 |
vgg16_predictions = vgg16_model.predict(img_array) |
|
|
26 |
vgg19_predictions = vgg19_model.predict(img_array) |
|
|
27 |
resnet50_predictions = resnet50_model.predict(img_array) |
|
|
28 |
|
|
|
29 |
# Combine predictions using majority voting |
|
|
30 |
ensemble_predictions = np.argmax(cnn_predictions + vgg16_predictions + vgg19_predictions + resnet50_predictions, axis=1) |
|
|
31 |
|
|
|
32 |
class_name = ['Bacterial Pneumonia', 'Normal', 'Viral Pneumonia'] |
|
|
33 |
predicted_label = class_name[ensemble_predictions[0]] |
|
|
34 |
|
|
|
35 |
print("Predicted Label: ", predicted_label) |
|
|
36 |
|
|
|
37 |
img.show() |