Spaces:
Sleeping

File size: 5,685 Bytes
2431a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from keras.models import load_model
from sklearn.metrics import classification_report, precision_score, recall_score, f1_score, accuracy_score
import numpy as np
from keras.preprocessing.image import img_to_array, ImageDataGenerator
from sklearn.metrics import classification_report
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np


# Path to your saved model
model_path = "./Model/optimized_model_LAST.hdf5"  # Replace with your model's name
model = load_model(model_path)

# Dataset Parameters
dataset_path = "./Dataset/FireNet_Augmented/"  # Replace with your dataset path
norm_size = 224
batch_size = 32

# Data Generator for Validation
datagen = ImageDataGenerator(rescale=1.0 / 255, validation_split=0.3)
validation_generator = datagen.flow_from_directory(
    dataset_path,
    target_size=(norm_size, norm_size),
    batch_size=batch_size,
    class_mode="categorical",
    subset="validation",
    shuffle=False  # Ensure labels and predictions align
)

y_true = validation_generator.classes  # True labels from the generator
y_pred_prob = model.predict(validation_generator, verbose=1)  # Predicted probabilities
y_pred = np.argmax(y_pred_prob, axis=1)  # Predicted class indices

# Compute metrics
accuracy = accuracy_score(y_true, y_pred)
precision = precision_score(y_true, y_pred, average='weighted', zero_division=1)
recall = recall_score(y_true, y_pred, average='weighted', zero_division=1)
f1 = f1_score(y_true, y_pred, average='weighted', zero_division=1)

# Print metrics
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1 Score:", f1)

# Detailed classification report
report = classification_report(
    y_true, y_pred, target_names=['NoFire', 'Fire'], output_dict=True, zero_division=1
)
metrics_data = {
    'Metric': ['F1 Score', 'Precision', 'Recall'],
    'Fire': [
        round(report['Fire']['f1-score'], 3),
        round(report['Fire']['precision'], 3),
        round(report['Fire']['recall'], 3)
    ],
    'No Fire': [
        round(report['NoFire']['f1-score'], 3),
        round(report['NoFire']['precision'], 3),
        round(report['NoFire']['recall'], 3)
    ]
}

metrics_df = pd.DataFrame(metrics_data)

# Prepare data for the table
cell_text = [["Labels", "F1 Score", "Precision", "Recall"],  # Column Headers
             ["Fire", metrics_df['Fire'][0], metrics_df['Fire'][1], metrics_df['Fire'][2]],  # Fire Data Row
             ["No Fire", metrics_df['No Fire'][0], metrics_df['No Fire'][1], metrics_df['No Fire'][2]]]  # No Fire Data Row

# Set up figure and axis
fig, ax = plt.subplots(figsize=(8, 3))  # Adjust size as needed
ax.axis('off')

# Add the title text just above the table with minimal spacing
ax.text(0.50, 0.75, 'Classification Report', ha='center', va='top', fontsize=14, weight='bold', transform=ax.transAxes)

# Create the table
table = ax.table(
    cellText=cell_text,
    cellLoc='center',
    colWidths=[0.3, 0.2, 0.2, 0.2],
    loc='center'
)

# Adjust the main table styling
table.auto_set_font_size(False)
table.set_fontsize(12)
table.scale(1.2, 1.2)  # Scale up for readability

# Set bold for header row in the main table
for (i, j), cell in table.get_celld().items():
    if i == 0:  # Header Row Styling
        cell.set_text_props(weight='bold')

plt.show()

name_file = "optimized_model"
from keras.models import load_model
import matplotlib.pyplot as plt
import cv2
import numpy as np

# Define the getLabel function
def getLabel(id):
    return ['NoFire', 'Fire'][id]

# Helper function to get distinct balanced samples from the validation generator
def get_balanced_samples(generator, num_samples_per_class=6):
    """
    Collect balanced samples from the generator.
    Args:
        generator: Validation generator.
        num_samples_per_class: Number of samples to select per class.
    Returns:
        images: Array of balanced images.
        labels: Array of corresponding labels.
    """
    images, labels = [], []
    class_counts = {0: 0, 1: 0}  # Track counts for 'NoFire' and 'Fire'
    while class_counts[0] < num_samples_per_class or class_counts[1] < num_samples_per_class:
        batch_data, batch_labels = next(generator)  # Get a batch
        for i in range(len(batch_data)):
            label = np.argmax(batch_labels[i])  # Convert one-hot encoding to class index
            if class_counts[label] < num_samples_per_class:
                images.append(batch_data[i])
                labels.append(label)
                class_counts[label] += 1
                if class_counts[0] == num_samples_per_class and class_counts[1] == num_samples_per_class:
                    break
    return np.array(images), np.array(labels)

# Load the best model
# model_path = "./Model/" + name_file + ".hdf5"
# model = load_model(model_path, compile=False)

# Get a balanced batch of validation data
X_test, y_test = get_balanced_samples(validation_generator, num_samples_per_class=6)

# Make predictions
res = model.predict(X_test).argmax(axis=-1)

# Visualize predictions for the best model
plt.figure(figsize=(12, 12))
for i in range(12):  # Display all 12 images
    plt.subplot(3, 4, i + 1)  # Create a 3x4 grid of plots

    # Convert data to a valid range for imshow
    image = (X_test[i] * 255).astype(np.uint8)  # Denormalize
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # Convert to RGB

    plt.imshow(image)
    plt.gca().get_xaxis().set_ticks([])
    plt.gca().get_yaxis().set_ticks([])
    plt.ylabel('Prediction = %s' % getLabel(res[i]), fontsize=10)

# Save and display the plot
plt.tight_layout()  # Adjust spacing for the 3x4 grid
plt.savefig('./Plot/' + name_file + '_Best.png', dpi=300)
plt.show()