-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess_folds.py
128 lines (99 loc) · 4.22 KB
/
process_folds.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
import os
import json
import numpy as np
from collections import defaultdict
# Function to load JSON data from a file
def load_json(filepath):
with open(filepath, 'r') as f:
return json.load(f)
def check_seed_existence(results):
seeds_exist = []
for seed in SEEDS:
if f'seed_{seed}' in results.keys(): seeds_exist.append(seed)
return seeds_exist
# Function to check if all seeds exist in results
def check_seed_results(dataset):
json_path = f"{os.path.join(results_folder, dataset)}.json"
if os.path.exists(json_path):
results = load_json(json_path)
seeds_exist = check_seed_existence(results)
if len(seeds_exist) != len(SEEDS):
raise ValueError(f"Seeds {set(SEEDS)-set(seeds_exist)} not found in {json_path} file. Get results of '{dataset}' on all seeds.")
else:
raise ValueError(f"File {json_path} does not exist. Get results of Dataset='{dataset}'.")
return results
# Function to check if all folds exist for a dataset
def get_folds_results(dataset, folds):
results = {}
for fold in range(1,folds+1):
json_path = f"{os.path.join(results_folder, dataset+'-FOLD'+str(fold))}.json"
if os.path.exists(json_path):
results_fold = load_json(json_path)
results['FOLD'+str(fold)] = results_fold
seeds_exist = check_seed_existence(results_fold)
if len(seeds_exist) != len(SEEDS):
raise ValueError(f"Seeds {set(SEEDS)-set(seeds_exist)} not found in {json_path} file. Get results '{dataset}-FOLD{fold}' on all seeds.")
else:
raise ValueError(f"File {json_path} does not exist. Get results for all folds of Dataset='{dataset}'.")
return results
# Function to calculate average metrics for all folds of dataset
def average_folds(dataset, folds):
results = get_folds_results(dataset, folds)
results_avg = {}
for seed in SEEDS:
for fold in range(1,folds+1):
results_fold = results['FOLD'+str(fold)]
results_seed = results_fold[f'seed_{seed}']
if fold == 1:
results_avg[f'seed_{seed}'] = results_seed
else:
for metric in results_seed.keys():
if metric != 'epoch':
results_avg[f'seed_{seed}'][metric] += results_seed[metric]
for seed in SEEDS:
for metric in results_avg[f'seed_{seed}'].keys():
if metric != 'epoch':
results_avg[f'seed_{seed}'][metric] /= folds
results_avg[f'seed_{seed}'][metric] = float(f"{results_avg[f'seed_{seed}'][metric]:0.4f}")
json_path = f'{os.path.join(results_folder,dataset)}.json'
with open(json_path, 'w') as f:
json.dump(results_avg, f, indent=2)
return results_avg
def get_results():
for dataset, folds in DATASETS.items():
print(f"Calculating average metrics for {dataset} dataset...")
if folds>1:
results_avg = average_folds(dataset, folds)
print(f"Average metrics for {dataset} dataset calculated successfully.")
print(f"Results saved in {os.path.join(results_folder,dataset)}.json")
print(results_avg)
print()
else:
print(f"Only one fold exists for {dataset} dataset. No need to calculate average metrics.")
check_seed_results(dataset)
print()
if __name__ == "__main__":
# Datasets and number of folds
DATASETS = {
'Beijing-Opera':5,
'CREMA-D':1,
'ESC50-Actions':5,
'ESC50':5,
'GT-Music-Genre':1,
'NS-Instruments':1,
'RAVDESS':1,
'SESA':1,
'TUT2017':4,
'UrbanSound8K':10,
'VocalSound':1
}
methods = ['zeroshot', 'coop', 'cocoop', 'palm']
for method in methods:
# Folder containing the JSON files
results_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), method)
if method == 'zeroshot':
SEEDS = [0]
else:
SEEDS = [0,1,2]
get_results()
print("All average metrics calculated successfully.")