-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisual.py
More file actions
348 lines (282 loc) · 13.9 KB
/
visual.py
File metadata and controls
348 lines (282 loc) · 13.9 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import matplotlib.pyplot as plt
import seaborn as sns
import json
import os
import numpy as np
class ResultsVisualizer:
def __init__(self, evaluation_file="evaluation_report.json", loss_file="training_loss.json", output_dir="result"):
self.output_dir = output_dir
if not os.path.exists(output_dir):
os.makedirs(output_dir)
try:
with open(evaluation_file, 'r') as f:
self.data = json.load(f)
except FileNotFoundError:
print(f"Warning: {evaluation_file} not found.")
self.data = None
try:
with open(loss_file, 'r') as f:
self.loss_data = json.load(f)
except FileNotFoundError:
print(f"Warning: {loss_file} not found.")
self.loss_data = None
sns.set_style("whitegrid")
plt.rcParams.update({'font.size': 12})
def plot_loss(self):
if not self.loss_data:
return
steps = []
losses = []
for entry in self.loss_data:
if 'loss' in entry and 'step' in entry:
steps.append(entry['step'])
losses.append(entry['loss'])
if not steps:
return
if len(steps) > 50:
skip = len(steps) // 50
steps = steps[::skip]
losses = losses[::skip]
plt.figure(figsize=(10, 6))
plt.plot(steps, losses, marker='o', linestyle='-', color='#e74c3c', linewidth=2)
plt.title('Training Loss Over Time', fontsize=14, fontweight='bold')
plt.xlabel('Training Steps')
plt.ylabel('Loss')
plt.grid(True, alpha=0.3)
output_path = os.path.join(self.output_dir, 'training_loss.png')
plt.savefig(output_path, dpi=300, bbox_inches='tight')
plt.close()
print(f"Saved {output_path}")
def plot_perplexity(self):
if not self.data:
return
plt.figure(figsize=(10, 6))
models = ['Base Model', 'Fine-tuned']
values = [self.data['base']['perplexity'], self.data['tuned']['perplexity']]
colors = ['#3498db', '#9b59b6']
bars = plt.bar(models, values, color=colors, alpha=0.8, edgecolor='black')
plt.title('Perplexity Comparison (Lower is Better)', fontsize=14, fontweight='bold')
plt.ylabel('Perplexity Score')
for bar in bars:
height = bar.get_height()
plt.text(bar.get_x() + bar.get_width()/2., height,
f'{height:.2f}', ha='center', va='bottom', fontweight='bold')
output_path = os.path.join(self.output_dir, 'perplexity.png')
plt.savefig(output_path, dpi=300, bbox_inches='tight')
plt.close()
print(f"Saved {output_path}")
def plot_similarity(self):
if not self.data:
return
plt.figure(figsize=(10, 6))
models = ['Base Model', 'Fine-tuned']
values = [self.data['base']['similarity'], self.data['tuned']['similarity']]
colors = ['#e74c3c', '#2ecc71']
bars = plt.bar(models, values, color=colors, alpha=0.8, edgecolor='black')
plt.title('Content Similarity (Higher is Better)', fontsize=14, fontweight='bold')
plt.ylabel('Jaccard Similarity Score')
plt.ylim(0, 1.0)
for bar in bars:
height = bar.get_height()
plt.text(bar.get_x() + bar.get_width()/2., height,
f'{height:.2f}', ha='center', va='bottom', fontweight='bold')
output_path = os.path.join(self.output_dir, 'similarity.png')
plt.savefig(output_path, dpi=300, bbox_inches='tight')
plt.close()
print(f"Saved {output_path}")
def plot_rouge_l(self):
"""Plot ROUGE-L scores comparison"""
if not self.data:
return
# Check if rouge_l exists in the data
if 'rouge_l' not in self.data.get('base', {}) or 'rouge_l' not in self.data.get('tuned', {}):
print("ROUGE-L data not found, skipping plot")
return
plt.figure(figsize=(10, 6))
models = ['Base Model', 'Fine-tuned']
values = [self.data['base']['rouge_l'], self.data['tuned']['rouge_l']]
colors = ['#f39c12', '#1abc9c']
bars = plt.bar(models, values, color=colors, alpha=0.8, edgecolor='black')
plt.title('ROUGE-L Score (Higher is Better)', fontsize=14, fontweight='bold')
plt.ylabel('ROUGE-L Score')
plt.ylim(0, 1.0)
for bar in bars:
height = bar.get_height()
plt.text(bar.get_x() + bar.get_width()/2., height,
f'{height:.2f}', ha='center', va='bottom', fontweight='bold')
output_path = os.path.join(self.output_dir, 'rouge_l.png')
plt.savefig(output_path, dpi=300, bbox_inches='tight')
plt.close()
print(f"Saved {output_path}")
def plot_victorian_freq(self):
"""Plot Victorian term frequency comparison"""
if not self.data:
return
# Check if victorian_freq exists in the data
if 'victorian_freq' not in self.data.get('base', {}) or 'victorian_freq' not in self.data.get('tuned', {}):
print("Victorian frequency data not found, skipping plot")
return
plt.figure(figsize=(10, 6))
models = ['Base Model', 'Fine-tuned']
values = [self.data['base']['victorian_freq'] * 100, self.data['tuned']['victorian_freq'] * 100]
colors = ['#95a5a6', '#8e44ad']
bars = plt.bar(models, values, color=colors, alpha=0.8, edgecolor='black')
plt.title('Victorian/Holmesian Term Frequency (Higher is Better)', fontsize=14, fontweight='bold')
plt.ylabel('Percentage of Victorian Terms (%)')
for bar in bars:
height = bar.get_height()
plt.text(bar.get_x() + bar.get_width()/2., height,
f'{height:.2f}%', ha='center', va='bottom', fontweight='bold')
output_path = os.path.join(self.output_dir, 'victorian_freq.png')
plt.savefig(output_path, dpi=300, bbox_inches='tight')
plt.close()
print(f"Saved {output_path}")
def plot_improvement(self):
if not self.data:
return
plt.figure(figsize=(12, 6))
base_ppl = self.data['base']['perplexity']
tuned_ppl = self.data['tuned']['perplexity']
ppl_imp = ((base_ppl - tuned_ppl) / base_ppl) * 100 if base_ppl > 0 else 0
base_sim = self.data['base']['similarity']
tuned_sim = self.data['tuned']['similarity']
sim_imp = ((tuned_sim - base_sim) / base_sim) * 100 if base_sim > 0 else (100 if tuned_sim > 0 else 0)
metrics = ['Perplexity\nReduction', 'Similarity\nIncrease']
values = [ppl_imp, sim_imp]
# Add ROUGE-L if available
if 'rouge_l' in self.data.get('base', {}) and 'rouge_l' in self.data.get('tuned', {}):
base_rouge = self.data['base']['rouge_l']
tuned_rouge = self.data['tuned']['rouge_l']
rouge_imp = ((tuned_rouge - base_rouge) / base_rouge) * 100 if base_rouge > 0 else 0
metrics.append('ROUGE-L\nIncrease')
values.append(rouge_imp)
# Add Victorian freq if available
if 'victorian_freq' in self.data.get('base', {}) and 'victorian_freq' in self.data.get('tuned', {}):
base_vf = self.data['base']['victorian_freq']
tuned_vf = self.data['tuned']['victorian_freq']
vf_imp = ((tuned_vf - base_vf) / base_vf) * 100 if base_vf > 0 else 0
metrics.append('Victorian\nTerms Increase')
values.append(vf_imp)
colors = ['#27ae60' if v > 0 else '#c0392b' for v in values]
bars = plt.barh(metrics, values, color=colors, alpha=0.8, edgecolor='black')
plt.title('Relative Model Improvement (%)', fontsize=14, fontweight='bold')
plt.xlabel('Percentage Change')
plt.axvline(0, color='black', linewidth=0.8)
for bar in bars:
width = bar.get_width()
label_x = width + (2 if width >= 0 else -2)
plt.text(label_x, bar.get_y() + bar.get_height()/2,
f'{width:.1f}%', va='center',
ha='left' if width >= 0 else 'right', fontweight='bold')
output_path = os.path.join(self.output_dir, 'improvement.png')
plt.savefig(output_path, dpi=300, bbox_inches='tight')
plt.close()
print(f"Saved {output_path}")
def create_dashboard(self):
if not self.data:
return
fig = plt.figure(figsize=(18, 14))
gs = fig.add_gridspec(3, 2, hspace=0.35, wspace=0.3)
# Perplexity comparison
ax1 = fig.add_subplot(gs[0, 0])
models = ['Base', 'Tuned']
vals1 = [self.data['base']['perplexity'], self.data['tuned']['perplexity']]
bars1 = ax1.bar(models, vals1, color=['#3498db', '#9b59b6'], alpha=0.8, edgecolor='black')
ax1.set_title('Perplexity (Lower is Better)', fontweight='bold')
for bar in bars1:
height = bar.get_height()
ax1.text(bar.get_x() + bar.get_width()/2., height, f'{height:.1f}', ha='center', va='bottom')
# Similarity comparison
ax2 = fig.add_subplot(gs[0, 1])
vals2 = [self.data['base']['similarity'], self.data['tuned']['similarity']]
bars2 = ax2.bar(models, vals2, color=['#e74c3c', '#2ecc71'], alpha=0.8, edgecolor='black')
ax2.set_title('Jaccard Similarity (Higher is Better)', fontweight='bold')
ax2.set_ylim(0, 1.0)
for bar in bars2:
height = bar.get_height()
ax2.text(bar.get_x() + bar.get_width()/2., height, f'{height:.2f}', ha='center', va='bottom')
# ROUGE-L comparison (if available)
ax3 = fig.add_subplot(gs[1, 0])
if 'rouge_l' in self.data.get('base', {}) and 'rouge_l' in self.data.get('tuned', {}):
vals3 = [self.data['base']['rouge_l'], self.data['tuned']['rouge_l']]
bars3 = ax3.bar(models, vals3, color=['#f39c12', '#1abc9c'], alpha=0.8, edgecolor='black')
ax3.set_title('ROUGE-L Score (Higher is Better)', fontweight='bold')
ax3.set_ylim(0, 1.0)
for bar in bars3:
height = bar.get_height()
ax3.text(bar.get_x() + bar.get_width()/2., height, f'{height:.2f}', ha='center', va='bottom')
else:
ax3.text(0.5, 0.5, 'ROUGE-L Data Not Available', ha='center', va='center', transform=ax3.transAxes)
ax3.set_title('ROUGE-L Score', fontweight='bold')
# Victorian term frequency (if available)
ax4 = fig.add_subplot(gs[1, 1])
if 'victorian_freq' in self.data.get('base', {}) and 'victorian_freq' in self.data.get('tuned', {}):
vals4 = [self.data['base']['victorian_freq'] * 100, self.data['tuned']['victorian_freq'] * 100]
bars4 = ax4.bar(models, vals4, color=['#95a5a6', '#8e44ad'], alpha=0.8, edgecolor='black')
ax4.set_title('Victorian Term Frequency (%)', fontweight='bold')
for bar in bars4:
height = bar.get_height()
ax4.text(bar.get_x() + bar.get_width()/2., height, f'{height:.2f}%', ha='center', va='bottom')
else:
ax4.text(0.5, 0.5, 'Victorian Freq Data Not Available', ha='center', va='center', transform=ax4.transAxes)
ax4.set_title('Victorian Term Frequency', fontweight='bold')
# Improvement metrics
ax5 = fig.add_subplot(gs[2, 0])
base_ppl = self.data['base']['perplexity']
tuned_ppl = self.data['tuned']['perplexity']
ppl_imp = ((base_ppl - tuned_ppl) / base_ppl) * 100 if base_ppl > 0 else 0
base_sim = self.data['base']['similarity']
tuned_sim = self.data['tuned']['similarity']
sim_imp = ((tuned_sim - base_sim) / base_sim) * 100 if base_sim > 0 else 0
metrics = ['Perplexity Reduction', 'Similarity Increase']
values = [ppl_imp, sim_imp]
colors = ['#27ae60' if v > 0 else '#c0392b' for v in values]
bars5 = ax5.barh(metrics, values, color=colors, alpha=0.8, edgecolor='black')
ax5.set_title('Percentage Improvement', fontweight='bold')
ax5.axvline(0, color='black')
for bar in bars5:
width = bar.get_width()
ax5.text(width, bar.get_y() + bar.get_height()/2, f' {width:.1f}%', va='center', fontweight='bold')
# Training loss curve
ax6 = fig.add_subplot(gs[2, 1])
if self.loss_data:
steps = []
losses = []
for entry in self.loss_data:
if 'loss' in entry and 'step' in entry:
steps.append(entry['step'])
losses.append(entry['loss'])
if steps:
if len(steps) > 50:
skip = len(steps) // 50
steps = steps[::skip]
losses = losses[::skip]
ax6.plot(steps, losses, marker='o', linestyle='-', color='#e74c3c', linewidth=2)
ax6.set_title('Training Loss', fontweight='bold')
ax6.set_xlabel('Steps')
ax6.set_ylabel('Loss')
ax6.grid(True, alpha=0.3)
else:
ax6.text(0.5, 0.5, 'No Loss Data Available', ha='center', va='center', transform=ax6.transAxes)
ax6.set_title('Training Loss', fontweight='bold')
plt.suptitle('Sherlock Model Evaluation Dashboard', fontsize=20, fontweight='bold')
output_path = os.path.join(self.output_dir, 'dashboard.png')
plt.savefig(output_path, dpi=300, bbox_inches='tight')
plt.close()
print(f"Saved {output_path}")
def run(self):
print(f"Generating visualizations in '{self.output_dir}'...")
if self.data:
self.plot_perplexity()
self.plot_similarity()
self.plot_rouge_l()
self.plot_victorian_freq()
self.plot_improvement()
self.plot_loss()
self.create_dashboard()
print("Visualization generation complete!")
else:
print("Skipping visualization due to missing data.")
if __name__ == "__main__":
viz = ResultsVisualizer()
viz.run()