-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
207 lines (162 loc) · 6.08 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
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
import torch.nn as nn
from torch.autograd import Variable
from sklearn.metrics import fbeta_score
from torch.nn import functional as F
from matplotlib import pyplot as plt
import pandas as pds
from datasets import *
import torch
import os
from data.kgdataset import CLASS_NAMES, KAGGLE_DATA_DIR
import pandas as pd
def name_idx():
return {name: idx for idx, name in enumerate(CLASS_NAMES)}
def idx_name():
return {idx: name for idx, name in enumerate(CLASS_NAMES)}
def predict(net, dataloader):
num = dataloader.dataset.num
probs = np.empty(num, 17)
current = 0
for batch_idx, (images, im_ids) in enumerate(dataloader):
num = images.size(0)
previous = current
current = previous + num
logits = net(Variable(images.cuda(), volatile=True))
prob = F.sigmoid(logits)
probs[previous:current, :] = prob.data.cpu().numpy()
print('Batch Index ', batch_idx)
return probs
def pred_csv(predictions, threshold, name):
"""
predictions: numpy array of predicted probabilities
"""
csv_name = os.path.join(KAGGLE_DATA_DIR, 'sample_submission.csv')
submission = pd.read_csv(csv_name)
print(submission)
for i, pred in enumerate(predictions):
labels = (pred > threshold).astype(int)
labels = np.where(labels == 1)[0]
labels = ' '.join(idx_name()[index] for index in labels)
submission['tags'][i] = labels
print('Index ', i)
submission.to_csv(os.path.join('submissions', '{}.csv'.format(name)), index=False)
def multi_criterion(logits, labels):
loss = nn.MultiLabelSoftMarginLoss()(logits, Variable(labels))
return loss
def multi_f_measure(probs, labels, threshold=0.235, beta=2):
batch_size = probs.size()[0]
SMALL = 1e-12
l = labels
p = (probs > threshold).float()
num_pos = torch.sum(p, 1)
num_pos_hat = torch.sum(l, 1)
tp = torch.sum(l*p,1)
precise = tp/(num_pos+ SMALL)
recall = tp/(num_pos_hat + SMALL)
fs = (1+beta*beta)*precise*recall/(beta*beta*precise + recall + SMALL)
f = fs.sum()/batch_size
return f
def evaluate(net, test_loader):
test_num = 0
test_loss = 0
test_acc = 0
for iter, (images, labels, indices) in enumerate(test_loader, 0):
# forward
logits = net(Variable(images.cuda(), volatile=True))
probs = F.sigmoid(logits)
loss = multi_criterion(logits, labels.cuda())
batch_size = len(images)
test_acc += batch_size*multi_f_measure(probs.data, labels.cuda())
test_loss += batch_size*loss.data[0]
test_num += batch_size
assert(test_num == test_loader.dataset.num)
test_acc = test_acc/test_num
test_loss = test_loss/test_num
return test_loss, test_acc
def get_learning_rate(optimizer):
lr=[]
for param_group in optimizer.param_groups:
lr +=[param_group['lr']]
return lr
def lr_schedule(epoch, optimizer, pretrained=False):
if pretrained:
if 0 <= epoch < 10:
lr = 1e-2
elif 10 <= epoch < 25:
lr = 5e-3
elif 25 <= epoch < 40:
lr = 1e-3
else:
lr = 1e-4
else:
if 0 <= epoch < 10:
lr = 1e-1
elif 10 <= epoch < 25:
lr = 5e-2
elif 25 <= epoch < 40:
lr = 1e-2
else:
lr = 1e-3
for para_group in optimizer.param_groups:
para_group['lr'] = lr
def split_train_validation(num_val=3000):
"""
Save train image names and validation image names to csv files
"""
train_image_idx = np.sort(np.random.choice(40479, 40479-num_val, replace=False))
all_idx = np.arange(40479)
validation_image_idx = np.zeros(num_val, dtype=np.int32)
val_idx = 0
train_idx = 0
for i in all_idx:
if i not in train_image_idx:
validation_image_idx[val_idx] = i
val_idx += 1
else:
train_idx += 1
# save train
train = []
for name in train_image_idx:
train.append('train-<ext>/train_%s.<ext>' % name)
eval = []
for name in validation_image_idx:
eval.append('train-<ext>/train_%s.<ext>' % name)
df = pds.DataFrame(train)
df.to_csv('dataset/train-%s' % (40479 - num_val), index=False, header=False)
df = pds.DataFrame(eval)
df.to_csv('dataset/validation-%s' % num_val, index=False, header=False)
def f2_score(y_true, y_pred):
return fbeta_score(y_true, y_pred, beta=2, average='samples')
class Logger(object):
def __init__(self, save_dir, name):
self.save_dir = save_dir
self.name = name
if not os.path.exists(self.save_dir):
os.makedirs(self.save_dir)
self.save_dict = {'train_loss': [], "evaluation_loss": [], 'f2_score': []}
def add_record(self, key, value):
self.save_dict[key].append(value)
def save(self):
df = pd.DataFrame.from_dict(self.save_dict)
df.to_csv(os.path.join(self.save_dir, '%s.csv' % self.name), header=True, index=False)
def save_plot(self):
train_loss = self.save_dict['train_loss']
eval_loss = self.save_dict['evaluation_loss']
f2_scores = self.save_dict['f2_score']
plt.figure()
plt.plot(np.arange(len(train_loss)), train_loss, color='red', label='train_loss')
plt.plot(np.arange(len(eval_loss)), eval_loss, color='blue', label='eval_loss')
plt.legend(loc='best')
plt.savefig(os.path.join(self.save_dir, 'loss.jpg'))
plt.figure()
plt.plot(np.arange(len(f2_scores)), f2_scores)
plt.savefig(os.path.join(self.save_dir, 'f2_score.jpg'))
plt.close('all')
def save_time(self, start_time, end_time):
with open(os.path.join(self.save_dir, 'time.txt'), 'w') as f:
f.write('start time, end time, duration\n')
f.write('{}, {}, {}'.format(start_time, end_time, (end_time - start_time)/60))
if __name__ == '__main__':
files = ['probs/densenet121.txt', 'probs/densenet161.txt', 'probs/densenet169.txt', 'probs/resnet18_planet.txt',
'probs/resnet34_planet.txt', 'probs/resnet50_planet.txt']
pred_csv(np.random.randn(2, 6), 0, 0)