-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_segmentation_multi_results.py
213 lines (172 loc) · 10.9 KB
/
plot_segmentation_multi_results.py
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
import json
import matplotlib.pyplot as plt
import os
# Read multi_model_results.json
with open('multi_model_results.json', 'r') as f:
results = json.load(f)
# Initialize lists to store data
model_names = []
model_sizes = []
hkcancor_accuracies = []
ud_yue_hk_f1 = []
ud_zh_hk_f1 = []
cityu_seg_f1 = []
# Function to get model size
def get_model_size(model_name):
if model_name.endswith('.gguf'):
path = f'bert.cpp/{model_name}'
else:
path = f'finetune-ckip-transformers/{model_name}/model.safetensors'
if os.path.exists(path):
return os.path.getsize(path) / (1024 * 1024) # Convert to MB
else:
print(f"Warning: File not found for {model_name}")
return None
# Function to calculate latency
def get_latency(model_data):
return model_data['total_time'] / model_data['total_tokens']
# Collect data and group models
gguf_models = {'sizes': [], 'ud_yue_hk_f1': [], 'ud_zh_hk_f1': [], 'latencies': [], 'hkcancor_accuracies': []}
electra_small_hkcancor_models = {'sizes': [], 'ud_yue_hk_f1': [], 'ud_zh_hk_f1': [], 'latencies': [], 'hkcancor_accuracies': []}
electra_small_distilled_models = {'sizes': [], 'ud_yue_hk_f1': [], 'ud_zh_hk_f1': [], 'latencies': [], 'hkcancor_accuracies': []}
other_models = {'names': [], 'sizes': [], 'ud_yue_hk_f1': [], 'ud_zh_hk_f1': [], 'latencies': [], 'hkcancor_accuracies': []}
for model_name, model_data in results.items():
model_size = get_model_size(model_name)
latency = get_latency(model_data)
if model_size is not None:
if model_name.endswith('.gguf'):
if model_name == 'electra.gguf' or model_name == 'electra-q4_1.gguf':
continue
gguf_models['sizes'].append(model_size)
gguf_models['ud_yue_hk_f1'].append(model_data['AlienKevin/ud_yue_hk']['token_f'])
gguf_models['ud_zh_hk_f1'].append(model_data['AlienKevin/ud_zh_hk']['token_f'])
gguf_models['latencies'].append(latency)
gguf_models['hkcancor_accuracies'].append(model_data['AlienKevin/hkcancor-multi']['accuracy'])
elif model_name.startswith('electra_small'):
if 'hkcancor' in model_name:
electra_small_hkcancor_models['sizes'].append(model_size)
electra_small_hkcancor_models['ud_yue_hk_f1'].append(model_data['AlienKevin/ud_yue_hk']['token_f'])
electra_small_hkcancor_models['ud_zh_hk_f1'].append(model_data['AlienKevin/ud_zh_hk']['token_f'])
electra_small_hkcancor_models['latencies'].append(latency)
electra_small_hkcancor_models['hkcancor_accuracies'].append(model_data['AlienKevin/hkcancor-multi']['accuracy'])
else:
electra_small_distilled_models['sizes'].append(model_size)
electra_small_distilled_models['ud_yue_hk_f1'].append(model_data['AlienKevin/ud_yue_hk']['token_f'])
electra_small_distilled_models['ud_zh_hk_f1'].append(model_data['AlienKevin/ud_zh_hk']['token_f'])
electra_small_distilled_models['latencies'].append(latency)
electra_small_distilled_models['hkcancor_accuracies'].append(model_data['AlienKevin/hkcancor-multi']['accuracy'])
else:
if model_name == 'electra_base_hkcancor_multi':
display_name = 'Base'
elif model_name == 'electra_large_hkcancor_multi':
display_name = 'Large'
elif model_name == 'albert_tiny_chinese_hkcancor_multi':
continue
elif model_name == 'bert_tiny_chinese_hkcancor_multi':
continue
else:
display_name = model_name
other_models['names'].append(display_name)
other_models['sizes'].append(model_size)
other_models['ud_yue_hk_f1'].append(model_data['AlienKevin/ud_yue_hk']['token_f'])
other_models['ud_zh_hk_f1'].append(model_data['AlienKevin/ud_zh_hk']['token_f'])
other_models['latencies'].append(latency)
other_models['hkcancor_accuracies'].append(model_data['AlienKevin/hkcancor-multi']['accuracy'])
# Create the plot for performance vs size
plt.figure(figsize=(6, 4))
colors = ['#e41a1c', '#377eb8', '#ff7f00', '#4daf4a', '#f781bf', '#a65628', '#984ea3', '#999999', '#dede00']
# Plot GGUF models as a line
plt.plot(gguf_models['sizes'], gguf_models['ud_yue_hk_f1'], 'o-', color=colors[0], label='Ours')
# Plot ELECTRA Small models as a line
plt.plot(electra_small_hkcancor_models['sizes'], electra_small_hkcancor_models['ud_yue_hk_f1'], 's-', color=colors[-1], label='Small (Layer Dropped)')
# Plot ELECTRA Small Distilled models as a line
plt.plot(electra_small_distilled_models['sizes'], electra_small_distilled_models['ud_yue_hk_f1'], '^-', color=colors[-2], label='Small (Distilled)')
# Plot other models with different shapes and colors
markers = ['D', 'v', '<', '>', 'p', '*', 'h', 'H', '+', 'x', 'd', '|', '_']
for i, (name, size, f1) in enumerate(zip(other_models['names'], other_models['sizes'], other_models['ud_yue_hk_f1'])):
plt.scatter(size, f1, marker=markers[i % len(markers)], c=colors[1:-2][i % len(colors)], label=name)
plt.xlabel('Model Size (MB)')
plt.ylabel('F1 Score')
plt.xscale('log') # Use log scale for x-axis due to potentially large size differences
plt.grid(True, which="both", ls="-", alpha=0.2)
plt.gcf().set_size_inches(7.5, 4) # Increase width from 6 to 10 inches, keep height at 4 inches
# Move legend to the top and reorganize
handles, labels = plt.gca().get_legend_handles_labels()
order = ['Ours', 'Small (Distilled)', 'Small (Layer Dropped)', 'Base', 'Large']
handles_dict = dict(zip(labels, handles))
ordered_handles = [handles_dict[label] for label in order if label in handles_dict]
ordered_labels = [label for label in order if label in handles_dict]
plt.legend(ordered_handles, ordered_labels, bbox_to_anchor=(0.5, 1.2), loc='upper center', ncol=5, frameon=False)
plt.tight_layout()
plt.savefig('multi_model_performance_vs_size.png', dpi=300, bbox_inches='tight')
plt.close()
# Create the plot for performance vs latency
plt.figure(figsize=(6, 4))
# Plot GGUF models as a line
plt.plot(gguf_models['latencies'], gguf_models['ud_yue_hk_f1'], 'o-', color=colors[0], label='Ours')
# Plot ELECTRA Small models as a line
plt.plot(electra_small_hkcancor_models['latencies'], electra_small_hkcancor_models['ud_yue_hk_f1'], 's-', color=colors[-1], label='Small (Layer Dropped)')
# Plot ELECTRA Small Distilled models as a line
plt.plot(electra_small_distilled_models['latencies'], electra_small_distilled_models['ud_yue_hk_f1'], '^-', color=colors[-2], label='Small (Distilled)')
# Plot other models with different shapes and colors
for i, (name, latency, f1) in enumerate(zip(other_models['names'], other_models['latencies'], other_models['ud_yue_hk_f1'])):
plt.scatter(latency, f1, marker=markers[i % len(markers)], c=colors[1:-2][i % len(colors)], label=name)
plt.xlabel('Latency (seconds/token)')
plt.ylabel('F1 Score')
plt.xscale('log') # Use log scale for x-axis due to potentially large latency differences
plt.grid(True, which="both", ls="-", alpha=0.2)
plt.gcf().set_size_inches(7.5, 4)
# Move legend to the top and reorganize
handles, labels = plt.gca().get_legend_handles_labels()
order = ['Ours', 'Small (Distilled)', 'Small (Layer Dropped)', 'Base', 'Large']
handles_dict = dict(zip(labels, handles))
ordered_handles = [handles_dict[label] for label in order if label in handles_dict]
ordered_labels = [label for label in order if label in handles_dict]
plt.legend(ordered_handles, ordered_labels, bbox_to_anchor=(0.5, 1.2), loc='upper center', ncol=5, frameon=False)
plt.tight_layout()
plt.savefig('multi_model_performance_vs_latency.png', dpi=300, bbox_inches='tight')
plt.close()
# Create the plot for HKCanCor accuracy vs size
plt.figure(figsize=(6, 4))
# Plot GGUF models as a line
plt.plot(gguf_models['sizes'], gguf_models['hkcancor_accuracies'], 'o-', color=colors[0], label='Ours')
# Plot ELECTRA Small models as a line
plt.plot(electra_small_hkcancor_models['sizes'], electra_small_hkcancor_models['hkcancor_accuracies'], 's-', color=colors[-1], label='Small (Layer Dropped)')
# Plot ELECTRA Small Distilled models as a line
plt.plot(electra_small_distilled_models['sizes'], electra_small_distilled_models['hkcancor_accuracies'], '^-', color=colors[-2], label='Small (Distilled)')
# Plot other models with different shapes and colors
for i, (name, size, accuracy) in enumerate(zip(other_models['names'], other_models['sizes'], other_models['hkcancor_accuracies'])):
plt.scatter(size, accuracy, marker=markers[i % len(markers)], c=colors[1:-2][i % len(colors)], label=name)
plt.xlabel('Model Size (MB)')
plt.ylabel('Accuracy')
plt.xscale('log') # Use log scale for x-axis due to potentially large size differences
plt.grid(True, which="both", ls="-", alpha=0.2)
plt.gcf().set_size_inches(7.5, 4)
# Move legend to the top and reorganize
handles, labels = plt.gca().get_legend_handles_labels()
order = ['Ours', 'Small (Distilled)', 'Small (Layer Dropped)', 'Base', 'Large']
handles_dict = dict(zip(labels, handles))
ordered_handles = [handles_dict[label] for label in order if label in handles_dict]
ordered_labels = [label for label in order if label in handles_dict]
plt.legend(ordered_handles, ordered_labels, bbox_to_anchor=(0.5, 1.2), loc='upper center', ncol=5, frameon=False)
plt.tight_layout()
plt.savefig('multi_model_hkcancor_accuracy_vs_size.png', dpi=300, bbox_inches='tight')
plt.close()
# Create a Markdown table with model information
print("| Model | Size (MB) | Latency (ms/token) | UD Yue F1 | UD Zh F1 |")
print("|-------|-----------|---------------------|-----------|----------|")
# Function to format float values
def format_float(value):
return f"{value:.4f}"
# GGUF models
for size, latency, ud_yue_f1, ud_zh_f1 in zip(gguf_models['sizes'], gguf_models['latencies'], gguf_models['ud_yue_hk_f1'], gguf_models['ud_zh_hk_f1']):
print(f"| Ours | {size:.2f} | {latency*1000:.2f} | {format_float(ud_yue_f1)} | {format_float(ud_zh_f1)} |")
# ELECTRA Small (Layer Dropped) models
for size, latency, ud_yue_f1, ud_zh_f1 in zip(electra_small_hkcancor_models['sizes'], electra_small_hkcancor_models['latencies'], electra_small_hkcancor_models['ud_yue_hk_f1'], electra_small_hkcancor_models['ud_zh_hk_f1']):
print(f"| Small (Layer Dropped) | {size:.2f} | {latency*1000:.2f} | {format_float(ud_yue_f1)} | {format_float(ud_zh_f1)} |")
# ELECTRA Small (Distilled) models
for size, latency, ud_yue_f1, ud_zh_f1 in zip(electra_small_distilled_models['sizes'], electra_small_distilled_models['latencies'], electra_small_distilled_models['ud_yue_hk_f1'], electra_small_distilled_models['ud_zh_hk_f1']):
print(f"| Small (Distilled) | {size:.2f} | {latency*1000:.2f} | {format_float(ud_yue_f1)} | {format_float(ud_zh_f1)} |")
# Other models
for name, size, latency, ud_yue_f1, ud_zh_f1 in zip(other_models['names'], other_models['sizes'], other_models['latencies'], other_models['ud_yue_hk_f1'], other_models['ud_zh_hk_f1']):
print(f"| {name} | {size:.2f} | {latency*1000:.2f} | {format_float(ud_yue_f1)} | {format_float(ud_zh_f1)} |")