-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
137 lines (115 loc) · 3.84 KB
/
main.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
import os
import argparse
import logging
from datetime import datetime
from mgc.experiments import bayes, deep, tree, svm, lstm
from mgc.persistence import (SklearnModelPersistence,
KerasModelPersistence)
from mgc.evaluation import (SklearnModelEvaluator,
KerasModelEvaluator)
from mgc.dataloading import (NumpyMusicGenreSetLoader,
TFMusicGenreSetLoader)
# Configuration of EXPERIMENTS
EXPERIMENTS = {
'bayes': lambda args: bayes.BayesExperiment(
data_loader=NumpyMusicGenreSetLoader(setup_datadir()),
evaluator=SklearnModelEvaluator(
get_output_filepath(args, extension='csv')),
persistence=SklearnModelPersistence(
saved_model_filepath('bayes.joblib', args)),
balanced=args.balanced
),
'deep': lambda args: deep.DeepExperiment(
data_loader=TFMusicGenreSetLoader(setup_datadir()),
evaluator=KerasModelEvaluator(
get_output_filepath(args, extension='csv')),
persistence=KerasModelPersistence(
saved_model_filepath('deep_wt.h5', args)),
balanced=args.balanced,
epochs=args.epochs
),
'lstm': lambda args: lstm.LSTMExperiment(
data_loader=TFMusicGenreSetLoader(setup_datadir()),
evaluator=KerasModelEvaluator(
get_output_filepath(args, extension='csv')),
persistence=KerasModelPersistence(
saved_model_filepath('lstm_wt.h5', args)),
balanced=args.balanced,
epochs=args.epochs
),
'svm': lambda args: svm.SVMExperiment(
data_loader=NumpyMusicGenreSetLoader(setup_datadir()),
evaluator=SklearnModelEvaluator(
get_output_filepath(args, extension='csv')),
persistence=SklearnModelPersistence(
saved_model_filepath('svm.joblib', args)),
balanced=args.balanced
),
'tree': lambda args: tree.DecisionTreeExperiment(
data_loader=NumpyMusicGenreSetLoader(setup_datadir()),
evaluator=SklearnModelEvaluator(
get_output_filepath(args, extension='csv')),
persistence=SklearnModelPersistence(
saved_model_filepath('tree.joblib', args)),
balanced=args.balanced
)
}
# Support functions
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'experiment',
help='The experiment to be executed',
choices=EXPERIMENTS.keys()
)
parser.add_argument(
'--balanced',
action='store_true'
)
parser.add_argument(
'--epochs',
type=int,
default=50
)
return parser.parse_args()
def setup_logging(args):
logfile = get_output_filepath(args)
logging.basicConfig(
level=logging.INFO,
filename=logfile,
format='%(asctime)s %(message)s')
logging.info(args)
def get_output_filepath(args, extension='log'):
balanced = 'bal' if args.balanced else 'unbal'
logfile = 'logs/{}_{}_{}.{}'.format(
args.experiment,
balanced,
datetime.now().strftime("%Y-%m-%d_%H.%M.%S"),
extension
)
return logfile
def setup_datadir():
datadir = os.environ.get(
'DATA_DIR',
'./downloads/audioset/audioset_v1_embeddings/'
)
datadir = os.path.abspath(datadir)
logging.info('Data dir: {}'.format(datadir))
return datadir
def saved_model_filepath(filename, args):
final_filename = filename
if (args.balanced):
final_filename = 'bal_{}'.format(filename)
else:
final_filename = 'unbal_{}'.format(filename)
return os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'saved_models',
final_filename
)
# Run main program
if __name__ == "__main__":
args = parse_args()
setup_logging(args)
experiment = EXPERIMENTS[args.experiment](args)
experiment.run()