-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_logs.py
75 lines (66 loc) · 2.66 KB
/
parse_logs.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
'''
Copyright (c) Haowei Zhu, 2024
'''
import os
import re
import numpy as np
import argparse
import yaml
def extract_performance(file_path):
with open(file_path, 'r') as file:
content = file.read()
matches = re.findall(r'The best performance:(\d+\.\d+)', content)
if matches:
return float(matches[-1])
else:
return None
def main(directory_path, multi_exp=False):
performances = []
if multi_exp:
for exp in os.listdir(directory_path):
file_path = os.path.join(directory_path, exp, "results.yaml")
if os.path.exists(file_path):
with open(file_path, 'r') as file:
data = yaml.safe_load(file)
performance = data.get('best_accuracy')
print(f"Accuracy of {file_path} is {performance}")
if performance is not None:
performances.append(performance)
else:
file_path = os.path.join(directory_path, "results.yaml")
if os.path.exists(file_path):
with open(file_path, 'r') as file:
data = yaml.safe_load(file)
performance = data.get('best_accuracy')
print(f"Accuracy of {file_path} is {performance}")
if performance is not None:
performances.append(performance)
# if multi_exp:
# for exp in os.listdir(directory_path):
# file_path = os.path.join(directory_path, exp, "log.txt")
# if os.path.exists(file_path):
# performance = float(extract_performance(file_path))
# print(f"Accuracy of {file_path} is {performance}")
# if performance is not None:
# performances.append(performance)
# else:
# file_path = os.path.join(directory_path, "log.txt")
# if os.path.exists(file_path):
# performance = float(extract_performance(file_path))
# print(f"Accuracy of {file_path} is {performance}")
# if performance is not None:
# performances.append(performance)
# 计算平均值和方差
if performances:
average_performance = np.mean(performances)
variance_performance = np.std(performances)
print(f"Average of {len(performances)} files is {average_performance:.2f} +- {variance_performance:.2f}")
else:
print("No valid performances found in the specified directory.")
parser = argparse.ArgumentParser()
parser.add_argument('exp', type=str)
parser.add_argument('--multi', action='store_true')
args = parser.parse_args()
# 指定包含txt文件的目录路径
directory_path = args.exp
main(directory_path, args.multi)