-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_ensemble.py
More file actions
345 lines (262 loc) · 11.7 KB
/
Copy pathtest_ensemble.py
File metadata and controls
345 lines (262 loc) · 11.7 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from __future__ import print_function
import os
from torch.autograd import Variable
import torch.nn.functional as F
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from transform import *
from Utils import *
from cdimage import *
from torch.utils.data.sampler import RandomSampler
import operator
from tqdm import tqdm
import label_category_transform
# --------------------------------------------------------
from net.resnet101 import ResNet101 as Net
TTA_list = [fix_center_crop, random_shift_scale_rotate]
# TTA_list = [fix_center_crop]
transform_num = len(TTA_list)
use_cuda = True
IDENTIFIER = "resnet"
SEED = 123456
PROJECT_PATH = './project'
CDISCOUNT_HEIGHT = 180
CDISCOUNT_WIDTH = 180
CDISCOUNT_NUM_CLASSES = 5270
csv_dir = './data/'
root_dir = '../output/'
test_data_filename = 'test.csv'
validation_data_filename = 'validation_small.csv'
initial_checkpoint = "./latest/" + IDENTIFIER + "/latest.pth"
# initial_checkpoint = "../trained_models/resnet_00243000_model.pth"
res_path = "./test_res/" + IDENTIFIER + "_test_TTA.res"
validation_batch_size = 64
def ensemble_predict(cur_procuct_probs, num):
candidates = list(set(np.argmax(cur_procuct_probs, axis=1))) # remove dups
if len(candidates) == 1:
return candidates[0]
print("candidates: ", candidates)
probs_means = np.mean(cur_procuct_probs, axis=0)
winner_score = 0.0
winner = None
for candidate in candidates:
# Adopt criteria to abandan some instances
print("=> candidate: ", candidate)
print("=> prob_mean: ", probs_means[candidate])
candidate_score = probs_means[candidate] * num
abandan_cnt = 0
for probs in cur_procuct_probs: # iterate each product instance
print("prob: ", probs[candidate])
if probs[candidate] < probs_means[candidate] * 0.6:
# abandan this instance
candidate_score -= probs[candidate]
abandan_cnt += 1
candidate_score = float(candidate_score) / (num - abandan_cnt)
if candidate_score > winner_score:
winner = candidate
winner_score = candidate_score
return winner
def TTA(images):
images_TTA_list = []
for transform in TTA_list:
cur_images = []
for image in images:
cur_images.append(pytorch_image_to_tensor_transform(transform(image)))
images_TTA_list.append(torch.stack(cur_images))
return images_TTA_list
def evaluate_sequential_average_val(net, loader, path):
cur_procuct_probs = np.zeros((1, CDISCOUNT_NUM_CLASSES))
cur_product_id = None
cur_product_label = None
correct_product_cnt = 0
total_product_cnt = 0
for iter, (images, labels, image_ids) in enumerate(tqdm(loader), 0):
labels = labels.numpy()
image_ids = np.array(image_ids)
# transforms
images_list = TTA(images.numpy()) # a list of image batch using different transforms
probs_list = []
for images in images_list:
images = Variable(images.type(torch.FloatTensor)).cuda()
logits = net(images)
probs = (((F.softmax(logits)).cpu().data.numpy()).astype(float))
probs_list.append(probs)
i = 0
cnt = 0;
for image_id in image_ids:
product_id = imageid_to_productid(image_id)
if cur_product_id == None:
cur_product_id = product_id
cur_product_label = labels[i]
if product_id != cur_product_id:
# a new product
print("------------------------- cur product: " + str(cur_product_id) + "-------------------------")
# find winner for previous product
num = cnt # total number of instances for current product
print("Number of instances: ", num)
# do predictions
cur_procuct_probs = np.array(cur_procuct_probs)
winner = np.argmax(cur_procuct_probs)
if winner == cur_product_label:
correct_product_cnt += 1
print("winner: ", str(winner))
print("label: ", str(cur_product_label))
total_product_cnt += 1
print("Acc: ", str(float(correct_product_cnt) / total_product_cnt))
# update
# start = end
cur_product_id = product_id
cur_product_label = labels[i]
cnt = 0
cur_procuct_probs = np.zeros((1, CDISCOUNT_NUM_CLASSES))
# add up probs
for probs in probs_list:
cur_procuct_probs += probs[i]
cnt += 1
i += 1
# find winner for current product
# do predictions
cur_procuct_probs = np.array(cur_procuct_probs)
winner = np.argmax(cur_procuct_probs)
if winner == cur_product_label:
correct_product_cnt += 1
total_product_cnt += 1
print("Acc: ", str(float(correct_product_cnt) / total_product_cnt))
def evaluate_sequential_ensemble_val(net, loader, path):
cur_procuct_probs = []
cur_product_id = None
cur_product_label = None
correct_product_cnt = 0
total_product_cnt = 0
for iter, (images, labels, image_ids) in enumerate(tqdm(loader), 0):
# if total_product_cnt > 10:
# break
labels = labels.numpy()
image_ids = np.array(image_ids)
# transforms
images_list = TTA(images.numpy()) # a list of image batch using different transforms
probs_list = []
for images in images_list:
images = Variable(images.type(torch.FloatTensor)).cuda()
logits = net(images)
probs = (((F.softmax(logits)).cpu().data.numpy()).astype(float))
probs_list.append(probs)
i = 0
for image_id in image_ids:
product_id = imageid_to_productid(image_id)
if cur_product_id == None:
cur_product_id = product_id
cur_product_label = labels[i]
if product_id != cur_product_id:
# a new product
print("------------------------- cur product: " + str(cur_product_id) + "-------------------------")
# find winner for previous product
num = len(cur_procuct_probs) * transform_num # total number of instances for current product
print("Number of instances: ", num)
# do predictions
cur_procuct_probs = np.array(cur_procuct_probs)
winner = ensemble_predict(cur_procuct_probs, num)
if winner == cur_product_label:
correct_product_cnt += 1
print("winner: ", str(winner))
print("label: ", str(cur_product_label))
total_product_cnt += 1
print("Acc: ", str(float(correct_product_cnt) / total_product_cnt))
# update
cur_product_id = product_id
cur_product_label = labels[i]
cur_procuct_probs = []
for probs in probs_list:
cur_procuct_probs.append(probs[i])
i += 1
# find winner for current product
num = len(cur_procuct_probs) * transform_num # total number of instances for current product
# do predictions
winner = ensemble_predict(np.array(cur_procuct_probs), num)
if winner == cur_product_label:
correct_product_cnt += 1
total_product_cnt += 1
print("Acc: ", str(float(correct_product_cnt) / total_product_cnt))
def evaluate_sequential_ensemble_test(net, loader, path):
product_to_prediction_map = {}
cur_procuct_probs = []
cur_product_id = None
with open(path, "a") as file:
file.write("_id,category_id\n")
for iter, (images, image_ids) in enumerate(tqdm(loader), 0):
image_ids = np.array(image_ids)
# transforms
images_list = TTA(images.numpy()) # a list of image batch using different transforms
probs_list = []
for images in images_list:
images = Variable(images.type(torch.FloatTensor)).cuda()
logits = net(images)
probs = ((F.softmax(logits)).cpu().data.numpy()).astype(float)
probs_list.append(probs)
i = 0
for image_id in image_ids:
product_id = imageid_to_productid(image_id)
if cur_product_id == None:
cur_product_id = product_id
if product_id != cur_product_id:
# a new product
print("------------------------- cur product: " + str(cur_product_id) + "-------------------------")
# find winner for previous product
num = len(cur_procuct_probs) * transform_num # total number of instances for current product
print("Number of instances: ", num)
# do predictions
cur_procuct_probs = np.array(cur_procuct_probs)
winner = ensemble_predict(cur_procuct_probs, num)
# save winner
product_to_prediction_map[cur_product_id] = winner
# update
cur_product_id = product_id
cur_procuct_probs = []
for probs in probs_list:
cur_procuct_probs.append(probs[i])
i += 1
# a new product
print("------------------------- cur product: " + str(cur_product_id) + "-------------------------")
# find winner for previous product
num = len(cur_procuct_probs) * transform_num # total number of instances for current product
print("Number of instances: ", num)
# do predictions
cur_procuct_probs = np.array(cur_procuct_probs)
winner = ensemble_predict(cur_procuct_probs, num)
# save winner
product_to_prediction_map[cur_product_id] = winner
for product_id, prediction in product_to_prediction_map.items():
file.write(str(product_id) + "," + str(label_to_category_id[prediction]) + "\n")
def write_test_result(path, product_to_prediction_map):
with open(path, "a") as file:
file.write("_id,category_id\n")
for product_id, prediction in product_to_prediction_map.items():
print(product_id)
print(prediction)
file.write(str(product_id) + "," + str(prediction) + "\n")
# main #################################################################
if __name__ == '__main__':
print( '%s: calling main function ... ' % os.path.basename(__file__))
net = Net(in_shape = (3, CDISCOUNT_HEIGHT, CDISCOUNT_WIDTH), num_classes=CDISCOUNT_NUM_CLASSES)
net.cuda()
net.eval()
if os.path.isfile(initial_checkpoint):
print("=> loading checkpoint '{}'".format(initial_checkpoint))
# load checkpoint
checkpoint = torch.load(initial_checkpoint)
net.load_state_dict(checkpoint['state_dict']) # load model weights from the checkpoint
# # load pretrained model
# net.load_state_dict(torch.load(initial_checkpoint, map_location=lambda storage, loc: storage))
print("=> loaded checkpoint '{}'".format(initial_checkpoint))
else:
print("=> no checkpoint found at '{}'".format(initial_checkpoint))
exit(0)
dataset = CDiscountDataset(csv_dir + test_data_filename, root_dir)
loader = DataLoader(
dataset,
sampler=SequentialSampler(dataset),
batch_size = validation_batch_size,
drop_last = False,
num_workers = 4,
pin_memory = False)
product_to_prediction_map = evaluate_sequential_ensemble_test(net, loader, res_path)
print('\nsucess!')