-
Notifications
You must be signed in to change notification settings - Fork 0
/
nni_main_classification_1.py
134 lines (107 loc) · 5.79 KB
/
nni_main_classification_1.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
from comet_ml import experiment
from data_loader.uts_classification_data_loader import UtsClassificationDataLoader
from models.uts_classification_model_2 import UtsClassificationModel
from trainers.uts_classification_trainer import UtsClassificationTrainer
from evaluater.uts_classification_evaluater import UtsClassificationEvaluater
from utils.config import process_config_UtsClassification
from utils.dirs import create_dirs
from utils.utils import get_args
import os
import time
import nni
import logging
LOG = logging.getLogger('main_classification')
from utils.config import get_config_from_json
def process_config_UtsClassification_bayes_optimization(json_file,params):
config, _ = get_config_from_json(json_file)
config.model.learning_rate = params['learning_rate']
config.trainer.batch_size = params['batch_size']
config.model.type = params['type']
config.model.nb_filters = params['nb_filters']
config.model.depth = params['depth']
config.model.kernel_size = params['kernel_size']
config.model.params = params
config.callbacks.tensorboard_log_dir = os.path.join("experiments",time.strftime("%Y-%m-%d/", time.localtime()),
config.exp.name, config.dataset.name,
config.model.name, "tensorboard_logs",
"lr=%s,epoch=%s,batch=%s,type=%s,nb_filters=%s,depth=%s,kernel_size=%s" % (
config.model.learning_rate, config.trainer.num_epochs,
config.trainer.batch_size, config.model.type, config.model.nb_filters,
config.model.depth,config.model.kernel_size)
)
config.callbacks.checkpoint_dir = os.path.join("experiments", time.strftime("%Y-%m-%d/", time.localtime()),
config.exp.name, config.dataset.name,
config.model.name, "%s-%s-%s-%s-%s-%s-%s" % (
config.model.learning_rate, config.trainer.num_epochs,
config.trainer.batch_size,config.model.type, config.model.nb_filters,
config.model.depth,config.model.kernel_size),
"checkpoints/")
config.log_dir = os.path.join("experiments", time.strftime("%Y-%m-%d/", time.localtime()),
config.exp.name, config.dataset.name,
config.model.name, "%s-%s-%s-%s-%s-%s-%s" % (
config.model.learning_rate, config.trainer.num_epochs,
config.trainer.batch_size,config.model.type, config.model.nb_filters,
config.model.depth,config.model.kernel_size),
"training_logs/")
config.result_dir = os.path.join("experiments", time.strftime("%Y-%m-%d/", time.localtime()),
config.exp.name, config.dataset.name,
config.model.name, "%s-%s-%s-%s-%s-%s-%s" % (
config.model.learning_rate, config.trainer.num_epochs,
config.trainer.batch_size,config.model.type, config.model.nb_filters,
config.model.depth,config.model.kernel_size),
"result/")
return config
def generate_default_params():
'''
Generate default hyper parameters
'''
return {
'learning_rate': 0.001,
'batch_size':64,
'type':'inceptiontime',
'nb_filters':32,
'depth':6,
'kernel_size':41
# 'learning_rate': 0.001,
# 'batch_size': 64,
# 'type' :1,
# 'convfilt' : 64,
# 'ksize' : 16,
# 'depth' : 15,
# 'drop' : 0.5
}
def main():
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
try:
RECEIVED_PARAMS = nni.get_next_parameter()
LOG.debug(RECEIVED_PARAMS)
PARAMS = generate_default_params()
PARAMS.update(RECEIVED_PARAMS)
args = get_args()
config = process_config_UtsClassification_bayes_optimization(args.config,PARAMS)
# except:
# print("missing or invalid arguments")
# exit(0)
# create the experiments dirs
create_dirs([config.callbacks.tensorboard_log_dir, config.callbacks.checkpoint_dir,
config.log_dir, config.result_dir])
print('Create the data generator.')
data_loader = UtsClassificationDataLoader(config)
print('Create the model.')
model = UtsClassificationModel(config, data_loader.get_inputshape(), data_loader.get_nbclasses())
print('Create the trainer')
trainer = UtsClassificationTrainer(model.model, data_loader.get_train_data(), config)
print('Start training the model.')
trainer.train()
print('Create the evaluater.')
evaluater = UtsClassificationEvaluater(trainer.best_model, data_loader.get_test_data(), data_loader.get_nbclasses(),
config)
print('Start evaluating the model.')
evaluater.evluate()
nni.report_final_result(evaluater.f1)
print('done')
except Exception as e:
LOG.exception(e)
raise
if __name__ == '__main__':
main()