forked from Cocoxili/DCASE2018Task2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
160 lines (119 loc) · 3.9 KB
/
util.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
import torch
import numpy as np
import shutil
import pickle
import librosa
import logging
import os
from network import *
from network_senet import *
from network_resnext import *
from network_MTOresnext import *
from network_dpn import *
from network_xception import *
def save_data(filename, data):
"""Save variable into a pickle file
Parameters
----------
filename: str
Path to file
data: list or dict
Data to be saved.
Returns
-------
nothing
"""
pickle.dump(data, open(filename, 'wb'), protocol=pickle.HIGHEST_PROTOCOL)
# pickle.dump(data, open(filename, 'w'))
def load_data(filename):
"""Load data from pickle file
Parameters
----------
filename: str
Path to file
Returns
-------
data: list or dict
Loaded file.
"""
return pickle.load(open(filename, "rb"), encoding='latin1')
def create_logging(log_dir, filemode):
if not os.path.exists(log_dir):
os.makedirs(log_dir)
i1 = 0
while os.path.isfile(os.path.join(log_dir, "{:04d}.log".format(i1))):
i1 += 1
log_path = os.path.join(log_dir, "{:04d}.log".format(i1))
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=log_path,
filemode=filemode)
# Print to console
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
return logging
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 accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def save_checkpoint(state, is_best, fold, config, filename='../model/checkpoint.pth.tar'):
torch.save(state, filename)
if is_best:
best_name = config.model_dir + '/model_best.' + str(fold) + '.pth.tar'
shutil.copyfile(filename, best_name)
def run_method_by_string(name):
p = globals().copy()
p.update(globals())
method = p.get(name)
if not method:
raise NotImplementedError('Method %s not implement' % name)
return method
def make_dirs():
dirs = ['../data-22050', '../prediction', '../log', '../model',
'../model', '../submission']
for dir in dirs:
if not os.path.exists(dir):
os.mkdir(dir)
print('Make dir: %s' %dir)
def make_one_hot(target, num_class=41):
"""
convert index tensor into one-hot tensor
"""
assert isinstance(target, torch.LongTensor)
return torch.zeros(target.size()[0], num_class).scatter_(1, target.view(-1, 1), 1)
def cross_entropy_onehot(input, target, size_average=True):
"""
Cross entropy that accepts soft targets (like [0, 0.1, 0.1, 0.8, 0]).
"""
assert input.size() == target.size()
if size_average:
return torch.mean(torch.sum(-target * F.log_softmax(input, dim=1), dim=1))
else:
return torch.sum(torch.sum(-target * F.log_softmax(input, dim=1), dim=1))