Repository navigation
Expand file tree
/
Copy pathplot.py
More file actions
138 lines (109 loc) · 4.8 KB
/
Copy pathplot.py
File metadata and controls
138 lines (109 loc) · 4.8 KB
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
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import json
import os
def save_figure(fig, directory, filename):
"""Save the figure in the specified directory."""
os.makedirs(directory, exist_ok=True)
fig_path = os.path.join(directory, filename)
fig.savefig(fig_path)
plt.close(fig)
# Function to plot the training and validation metrics for a given model
def plot_training_metrics(data, model_name, output_dir):
epochs = data["epoch"]
train_loss = data["train_loss"]
val_loss = data["val_loss"]
train_acc = data["train_accuracy"]
val_acc = data["val_accuracy"]
fig, axs = plt.subplots(1, 2, figsize=(12, 5))
# Plot loss evolution
axs[0].plot(epochs, train_loss, label="Train Loss", color="blue")
axs[0].plot(epochs, val_loss, label="Validation Loss", color="orange")
axs[0].set_title(f"{model_name} - Loss Evolution")
axs[0].set_xlabel("Epochs")
axs[0].set_ylabel("Loss")
axs[0].legend()
axs[0].grid(True)
# Plot accuracy evolution
axs[1].plot(epochs, train_acc, label="Train Accuracy", color="blue")
axs[1].plot(epochs, val_acc, label="Validation Accuracy", color="orange")
axs[1].set_title(f"{model_name} - Accuracy Evolution")
axs[1].set_xlabel("Epochs")
axs[1].set_ylabel("Accuracy")
axs[1].legend()
axs[1].grid(True)
plt.tight_layout()
save_figure(fig, output_dir, f"{model_name}_metrics.png")
# Function to plot confusion matrix
def plot_confusion_matrix(conf_matrix, model_name, output_dir, class_names=None):
if conf_matrix.shape[0] > 20: # Simplify for large matrices
fig, ax = plt.subplots(figsize=(12, 12))
else:
fig, ax = plt.subplots(figsize=(8, 8))
cax = ax.matshow(conf_matrix, cmap="coolwarm")
fig.colorbar(cax)
# Add values inside the matrix for smaller matrices
if conf_matrix.shape[0] <= 20:
for (i, j), val in np.ndenumerate(conf_matrix):
ax.text(j, i, f"{val}", ha="center", va="center", color="black")
ax.set_title(f"{model_name} - Confusion Matrix")
ax.set_xlabel("Predicted")
ax.set_ylabel("Actual")
# Add class names for Deepfake
if class_names and len(class_names) <= 10: # Check for Deepfake
ax.set_xticks(range(len(class_names)))
ax.set_yticks(range(len(class_names)))
ax.set_xticklabels(class_names, rotation=90, ha="center")
ax.set_yticklabels(class_names)
save_figure(fig, output_dir, f"{model_name}_confusion_matrix.png")
def save_most_confused_classes(conf_matrix, model_name, output_dir, class_names):
# Check if the number of class names matches the size of the confusion matrix
if len(class_names) != conf_matrix.shape[0]:
raise ValueError(
f"Mismatch between number of classes ({len(class_names)}) and confusion matrix size ({conf_matrix.shape[0]})."
)
confused_pairs = []
for i in range(conf_matrix.shape[0]):
for j in range(conf_matrix.shape[1]):
if i != j and conf_matrix[i, j] > 0:
confused_pairs.append(
(class_names[i], class_names[j], conf_matrix[i, j])
)
confused_pairs = sorted(confused_pairs, key=lambda x: x[2], reverse=True)
most_confused_path = os.path.join(
output_dir, f"{model_name}_most_confused_classes.txt"
)
with open(most_confused_path, "w") as f:
for cls1, cls2, count in confused_pairs[:10]: # Save top 10 most confused pairs
f.write(f"{cls1} <-> {cls2}: {count} times\n")
def main():
# File paths for DEEPFAKE
datasets = {
"DEEPFAKE": [
("./checkpoints/deepfake_custom/", 2),
("./checkpoints/deepfake_resnet50/", 2),
],
}
deepfake_classes = ["FAKE", "REAL"]
for dataset, paths in datasets.items():
for path, num_classes in paths:
model_name = path.split("/")[-2]
output_dir = os.path.join(path, "figures")
# Load training logs
training_log_path = os.path.join(path, "training_log.csv")
training_data = pd.read_csv(training_log_path)
plot_training_metrics(training_data, model_name, output_dir)
# Load confusion matrix
conf_matrix_path = os.path.join(path, "confusion_matrix.npy")
conf_matrix = np.load(conf_matrix_path)
# Select correct class names based on dataset
class_names = deepfake_classes
# Plot confusion matrix and save most confused classes
if len(class_names) <= 10:
plot_confusion_matrix(conf_matrix, model_name, output_dir, class_names)
else:
plot_confusion_matrix(conf_matrix, model_name, output_dir)
save_most_confused_classes(conf_matrix, model_name, output_dir, class_names)
if __name__ == "__main__":
main()