-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
219 lines (192 loc) · 7.05 KB
/
utils.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
import torch
from easydict import EasyDict as edict
import torch.backends.cudnn as cudnn
import os
import numpy as np
import torch
import random
import logging
import yaml
from datetime import datetime
import conf_graph_au
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def statistics(pred, y, thresh):
batch_size = pred.size(0)
class_nb = pred.size(1)
pred = pred >= thresh
pred = pred.long()
statistics_list = []
for j in range(class_nb):
TP = 0
FP = 0
FN = 0
TN = 0
for i in range(batch_size):
if pred[i][j] == 1:
if y[i][j] == 1:
TP += 1
elif y[i][j] == 0:
FP += 1
else:
assert False
elif pred[i][j] == 0:
if y[i][j] == 1:
FN += 1
elif y[i][j] == 0:
TN += 1
else:
assert False
else:
assert False
statistics_list.append({'TP': TP, 'FP': FP, 'TN': TN, 'FN': FN})
return statistics_list
def calc_f1_score(statistics_list):
f1_score_list = []
for i in range(len(statistics_list)):
TP = statistics_list[i]['TP']
FP = statistics_list[i]['FP']
FN = statistics_list[i]['FN']
precise = TP / (TP + FP + 1e-20)
recall = TP / (TP + FN + 1e-20)
f1_score = 2 * precise * recall / (precise + recall + 1e-20)
f1_score_list.append(f1_score)
mean_f1_score = sum(f1_score_list) / len(f1_score_list)
return mean_f1_score, f1_score_list
def calc_acc(statistics_list):
acc_list = []
for i in range(len(statistics_list)):
TP = statistics_list[i]['TP']
FP = statistics_list[i]['FP']
FN = statistics_list[i]['FN']
TN = statistics_list[i]['TN']
acc = (TP+TN)/(TP+TN+FP+FN)
acc_list.append(acc)
mean_acc_score = sum(acc_list) / len(acc_list)
return mean_acc_score, acc_list
def update_statistics_list(old_list, new_list):
if not old_list:
return new_list
assert len(old_list) == len(new_list)
for i in range(len(old_list)):
old_list[i]['TP'] += new_list[i]['TP']
old_list[i]['FP'] += new_list[i]['FP']
old_list[i]['TN'] += new_list[i]['TN']
old_list[i]['FN'] += new_list[i]['FN']
return old_list
def BP4D_infolist(list):
infostr = {'AU1: {:.2f} AU2: {:.2f} AU4: {:.2f} AU6: {:.2f} AU7: {:.2f} AU10: {:.2f} AU12: {:.2f} AU14: {:.2f} AU15: {:.2f} AU17: {:.2f} AU23: {:.2f} AU24: {:.2f} '.format(100.*list[0],100.*list[1],100.*list[2],100.*list[3],100.*list[4],100.*list[5],100.*list[6],100.*list[7],100.*list[8],100.*list[9],100.*list[10],100.*list[11])}
return infostr
def DISFA_infolist(list):
infostr = {'AU1: {:.2f} AU2: {:.2f} AU4: {:.2f} AU6: {:.2f} AU9: {:.2f} AU12: {:.2f} AU25: {:.2f} AU26: {:.2f} '.format(100.*list[0],100.*list[1],100.*list[2],100.*list[3],100.*list[4],100.*list[5],100.*list[6],100.*list[7])}
return infostr
def str2bool(v):
return v.lower() in ('true', '1')
def print_conf(opt):
message = ''
message += '----------------- Options ---------------\n'
for k, v in sorted(vars(opt).items()):
comment = ''
message += '{:>25}: {:<30}{}\n'.format(str(k), str(v), comment)
message += '----------------- End -------------------'
return message
def get_config_graphau():
cfg = conf_graph_au.parser2dict()
if cfg.dataset == 'BP4D':
with open('config/BP4D_config.yaml', 'r') as f:
datasets_cfg = yaml.safe_load(f)
datasets_cfg = edict(datasets_cfg)
elif cfg.dataset == 'DISFA':
with open('config/DISFA_config.yaml', 'r') as f:
datasets_cfg = yaml.safe_load(f)
datasets_cfg = edict(datasets_cfg)
else:
raise Exception("Unkown Datsets:",cfg.dataset)
cfg.update(datasets_cfg)
return cfg
def set_env(cfg):
random.seed(cfg.seed)
np.random.seed(cfg.seed)
torch.manual_seed(cfg.seed)
torch.cuda.manual_seed(cfg.seed)
torch.cuda.manual_seed_all(cfg.seed)
if 'cudnn' in cfg:
torch.backends.cudnn.benchmark = cfg.cudnn
else:
torch.backends.cudnn.benchmark = False
cudnn.deterministic = True
os.environ["NUMEXPR_MAX_THREADS"] = '16'
os.environ["CUDA_VISIBLE_DEVICES"] = cfg.gpu_ids
def set_outdir(conf):
default_outdir = 'results'
if 'timedir' in conf:
timestr = datetime.now().strftime('%d-%m-%Y_%I_%M-%S_%p')
outdir = os.path.join(default_outdir,conf.exp_name,timestr)
else:
outdir = os.path.join(default_outdir,conf.exp_name)
prefix = 'bs_'+str(conf.batch_size)+'_seed_'+str(conf.seed)+'_lr_'+str(conf.init_lr)
outdir = os.path.join(outdir,prefix)
ensure_dir(outdir)
conf.outdir = outdir
return conf
def ensure_dir(dir_name):
if not os.path.exists(dir_name):
os.makedirs(dir_name)
print('{} is created'.format(dir_name))
def set_logger(cfg):
if 'loglevel' in cfg:
loglevel = eval('logging.'+loglevel)
else:
loglevel = logging.INFO
if cfg.evaluate:
outname = 'test.log'
else:
outname = 'train.log'
outdir = cfg['outdir']
log_path = os.path.join(outdir,outname)
logger = logging.getLogger()
logger.setLevel(loglevel)
if not logger.handlers:
file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s: %(message)s'))
logger.addHandler(file_handler)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.Formatter('%(message)s'))
logger.addHandler(stream_handler)
logging.info(print_conf(cfg))
logging.info('writting logs to file {}'.format(log_path))
def set_linear(conf):
default_outdir = 'results'
outdir = os.path.join(default_outdir,conf.exp_name)
prefix = 'bs_'+str(conf.batch_size)+'_seed_'+str(conf.seed)+'_lr_'+str(conf.init_lr)
outdir = os.path.join(outdir, prefix)
ensure_dir(outdir)
loglevel = logging.INFO
if conf.linear:
outname = 'linear_test.log'
else:
outname = 'train.log'
log_path = os.path.join(outdir,outname)
logger = logging.getLogger()
logger.setLevel(loglevel)
if not logger.handlers:
file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s: %(message)s'))
logger.addHandler(file_handler)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.Formatter('%(message)s'))
logger.addHandler(stream_handler)
logging.info(print_conf(conf))
logging.info('writting logs to file {}'.format(log_path))