forked from thuiar/MMSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
300 lines (283 loc) · 11 KB
/
run.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import os
import gc
import time
import random
import logging
import torch
import pynvml
import argparse
import numpy as np
import pandas as pd
import multiprocessing as mp
from multiprocessing import Pool
from models.AMIO import AMIO
from trains.ATIO import ATIO
from data.load_data import MMDataLoader
from config.config_tune import ConfigTune
from config.config_regression import ConfigRegression
from config.config_classification import ConfigClassification
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
def run(args):
if not os.path.exists(args.model_save_dir):
os.makedirs(args.model_save_dir)
args.model_save_path = os.path.join(args.model_save_dir,\
f'{args.modelName}-{args.datasetName}-{args.train_mode}.pth')
# indicate used gpu
if len(args.gpu_ids) == 0 and torch.cuda.is_available():
# load free-most gpu
pynvml.nvmlInit()
dst_gpu_id, min_mem_used = 0, 1e16
for g_id in [0, 1, 2, 3]:
handle = pynvml.nvmlDeviceGetHandleByIndex(g_id)
meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
mem_used = meminfo.used
if mem_used < min_mem_used:
min_mem_used = mem_used
dst_gpu_id = g_id
print(f'Find gpu: {dst_gpu_id}, use memory: {min_mem_used}!')
logger.info(f'Find gpu: {dst_gpu_id}, with memory: {min_mem_used} left!')
args.gpu_ids.append(dst_gpu_id)
# device
using_cuda = len(args.gpu_ids) > 0 and torch.cuda.is_available()
logger.info("Let's use %d GPUs!" % len(args.gpu_ids))
device = torch.device('cuda:%d' % int(args.gpu_ids[0]) if using_cuda else 'cpu')
args.device = device
# add tmp tensor to increase the temporary consumption of GPU
tmp_tensor = torch.zeros((100, 100)).to(args.device)
# load data and models
dataloader = MMDataLoader(args)
model = AMIO(args).to(device)
del tmp_tensor
def count_parameters(model):
answer = 0
for p in model.parameters():
if p.requires_grad:
answer += p.numel()
# print(p)
return answer
logger.info(f'The model has {count_parameters(model)} trainable parameters')
# exit()
# using multiple gpus
# if using_cuda and len(args.gpu_ids) > 1:
# model = torch.nn.DataParallel(model,
# device_ids=args.gpu_ids,
# output_device=args.gpu_ids[0])
atio = ATIO().getTrain(args)
# do train
atio.do_train(model, dataloader)
# load pretrained model
assert os.path.exists(args.model_save_path)
model.load_state_dict(torch.load(args.model_save_path))
model.to(device)
# do test
if args.is_tune:
# using valid dataset to tune hyper parameters
results = atio.do_test(model, dataloader['valid'], mode="VALID")
else:
results = atio.do_test(model, dataloader['test'], mode="TEST")
del model
torch.cuda.empty_cache()
gc.collect()
time.sleep(5)
return results
def run_tune(args, tune_times=50):
args.res_save_dir = os.path.join(args.res_save_dir, 'tunes')
init_args = args
has_debuged = [] # save used paras
save_file_path = os.path.join(args.res_save_dir, \
f'{args.datasetName}-{args.modelName}-{args.train_mode}-tune.csv')
if not os.path.exists(os.path.dirname(save_file_path)):
os.makedirs(os.path.dirname(save_file_path))
for i in range(tune_times):
# cancel random seed
setup_seed(int(time.time()))
args = init_args
config = ConfigTune(args)
args = config.get_config()
print(args)
# print debugging params
logger.info("#"*40 + '%s-(%d/%d)' %(args.modelName, i+1, tune_times) + '#'*40)
for k,v in args.items():
if k in args.d_paras:
logger.info(k + ':' + str(v))
logger.info("#"*90)
logger.info('Start running %s...' %(args.modelName))
# restore existed paras
if i == 0 and os.path.exists(save_file_path):
df = pd.read_csv(save_file_path)
for i in range(len(df)):
has_debuged.append([df.loc[i,k] for k in args.d_paras])
# check paras
cur_paras = [args[v] for v in args.d_paras]
if cur_paras in has_debuged:
logger.info('These paras have been used!')
time.sleep(3)
continue
has_debuged.append(cur_paras)
results = []
for j, seed in enumerate([1111]):
args.cur_time = j + 1
setup_seed(seed)
results.append(run(args))
# save results to csv
logger.info('Start saving results...')
if os.path.exists(save_file_path):
df = pd.read_csv(save_file_path)
else:
df = pd.DataFrame(columns = [k for k in args.d_paras] + [k for k in results[0].keys()])
# stat results
tmp = [args[c] for c in args.d_paras]
for col in results[0].keys():
values = [r[col] for r in results]
tmp.append(round(sum(values) * 100 / len(values), 2))
df.loc[len(df)] = tmp
df.to_csv(save_file_path, index=None)
logger.info('Results are saved to %s...' %(save_file_path))
def run_normal(args):
args.res_save_dir = os.path.join(args.res_save_dir, 'normals')
init_args = args
model_results = []
seeds = args.seeds
# run results
for i, seed in enumerate(seeds):
args = init_args
# load config
if args.train_mode == "regression":
config = ConfigRegression(args)
else:
config = ConfigClassification(args)
args = config.get_config()
setup_seed(seed)
args.seed = seed
logger.info('Start running %s...' %(args.modelName))
logger.info(args)
# runnning
args.cur_time = i+1
test_results = run(args)
# restore results
model_results.append(test_results)
criterions = list(model_results[0].keys())
# load other results
save_path = os.path.join(args.res_save_dir, \
f'{args.datasetName}-{args.train_mode}.csv')
if not os.path.exists(args.res_save_dir):
os.makedirs(args.res_save_dir)
if os.path.exists(save_path):
df = pd.read_csv(save_path)
else:
df = pd.DataFrame(columns=["Model"] + criterions)
# save results
res = [args.modelName]
for c in criterions:
values = [r[c] for r in model_results]
mean = round(np.mean(values)*100, 2)
std = round(np.std(values)*100, 2)
res.append((mean, std))
df.loc[len(df)] = res
df.to_csv(save_path, index=None)
logger.info('Results are added to %s...' %(save_path))
def set_log(args):
log_file_path = f'logs/{args.modelName}-{args.datasetName}.log'
# set logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
for ph in logger.handlers:
logger.removeHandler(ph)
# add FileHandler to log file
formatter_file = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s', datefmt='%Y-%m-%d %H:%M:%S')
fh = logging.FileHandler(log_file_path)
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter_file)
logger.addHandler(fh)
# add StreamHandler to terminal outputs
# formatter_stream = logging.Formatter('%(message)s')
# ch = logging.StreamHandler()
# ch.setLevel(logging.DEBUG)
# ch.setFormatter(formatter_stream)
# logger.addHandler(ch)
return logger
def worker(cur_task=None):
args = parse_args()
global logger
if cur_task:
stime = random.random()*60
print(f"{os.getpid()} process will wait: {stime} seconds...")
time.sleep(stime) # avoid use the same gpu at first
args.is_tune = True if cur_task['is_tune'] else False
args.train_mode = cur_task['train_mode']
args.modelName = cur_task['modelName']
args.datasetName = cur_task['datasetName']
try:
logger = set_log(args)
args.seeds = [1111,1112, 1113, 1114, 1115]
if args.is_tune:
run_tune(args, tune_times=cur_task['tune_times'])
else:
run_normal(args)
df = pd.read_csv('tasks.csv')
df.loc[cur_task['index'], 'state'] = 1 # 任务完成
except Exception as e:
logger.error(e)
df = pd.read_csv('tasks.csv')
df.loc[cur_task['index'], 'state'] = -1 # 任务出错
df.loc[cur_task['index'], 'error_info'] = str(e)
finally:
df.to_csv('tasks.csv', index=None)
else:
logger = set_log(args)
args.seeds = [1111,1112, 1113, 1114, 1115]
if args.is_tune:
# run_tune(args, tune_times=cur_task['tune_times'])
run_tune(args, tune_times=50)
else:
run_normal(args)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--need_task_scheduling', type=bool, default=False,
help='use the task scheduling module.')
parser.add_argument('--is_tune', type=bool, default=False,
help='tune parameters ?')
parser.add_argument('--train_mode', type=str, default="regression",
help='regression / classification')
parser.add_argument('--modelName', type=str, default='bert_mag',
help='support lf_dnn/ef_lstm/tfn/lmf/mfn/graph_mfn/mult/misa/mlf_dnn/mtfn/mlmf/self_mm')
parser.add_argument('--datasetName', type=str, default='mosi',
help='support mosi/mosei/sims')
parser.add_argument('--num_workers', type=int, default=8,
help='num workers of loading data')
parser.add_argument('--model_save_dir', type=str, default='results/models',
help='path to save results.')
parser.add_argument('--res_save_dir', type=str, default='results/20200506',
help='path to save results.')
parser.add_argument('--gpu_ids', type=list, default=[1],
help='indicates the gpus will be used. If none, the most-free gpu will be used!')
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
if args.need_task_scheduling:
mp.set_start_method('spawn')
# load uncompleted tasks
df = pd.read_csv('tasks.csv')
left_tasks = []
for index in range(len(df)):
if df.loc[index, 'state'] == 0:
cur_task = {name:df.loc[index,name] for name in df.columns}
cur_task['index'] = index
left_tasks.append(cur_task)
# create process pools
po = Pool(4)
for cur_task in left_tasks:
po.apply_async(worker, (cur_task,))
# close and wait
print('-----start--------')
po.close()
po.join()
print('-----end--------')
else:
worker()