-
Notifications
You must be signed in to change notification settings - Fork 2
/
method2.py
506 lines (413 loc) · 17.6 KB
/
method2.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# Author: Yang Liu
# Date: 07/28/2016
# This is for classifying driving behaviors based on the in-car images provided by State Farm Kaggle competition.
import numpy as np
import os
import glob
import cv2
import pickle
import datetime
import pandas as pd
import time
from shutil import copy2
import warnings
warnings.filterwarnings("ignore")
from numpy.random import permutation
np.random.seed(2016)
from sklearn.cross_validation import train_test_split
from sklearn.cross_validation import KFold
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
from keras.callbacks import EarlyStopping, ModelCheckpoint, Callback
from keras.utils import np_utils
from keras.models import model_from_json
from sklearn.metrics import log_loss
import h5py
use_cache = 1
def get_im_cv2(path):
img = cv2.imread(path)
resized = cv2.resize(img, (224, 224), cv2.INTER_LINEAR)
return resized
def get_driver_data():
dr = dict()
clss = dict()
path = os.path.join('..', 'input', 'driver_imgs_list.csv')
print('Read drivers data')
f = open(path, 'r')
line = f.readline()
while (1):
line = f.readline()
if line == '':
break
arr = line.strip().split(',')
dr[arr[2]] = arr[0]
if arr[0] not in clss.keys():
clss[arr[0]] = [(arr[1], arr[2])]
else:
clss[arr[0]].append((arr[1], arr[2]))
f.close()
return dr, clss
def normalize_image(img):
img[:, :, 0] -= 103.939
img[:, :, 1] -= 116.779
img[:, :, 2] -= 123.68
return img
def load_train():
X_train = []
X_train_id = []
y_train = []
driver_id = []
start_time = time.time()
driver_data, dr_class = get_driver_data()
print('Read train images')
for j in range(10):
print('Load folder c{}'.format(j))
path = os.path.join('..', 'input', 'train', 'c' + str(j), '*.jpg')
files = glob.glob(path)
for fl in files:
flbase = os.path.basename(fl)
img = get_im_cv2(fl)
X_train.append(img)
X_train_id.append(flbase)
y_train.append(j)
driver_id.append(driver_data[flbase])
print('Read train data time: {} seconds'.format(round(time.time() - start_time, 2)))
unique_drivers = sorted(list(set(driver_id)))
print('Unique drivers: {}'.format(len(unique_drivers)))
print(unique_drivers)
return X_train, y_train, X_train_id, driver_id, unique_drivers
def split_list(l, wanted_parts=1):
length = len(l)
return [l[i*length // wanted_parts: (i+1)*length // wanted_parts] for i in range(wanted_parts)]
def load_test(part):
path = os.path.join('..', 'input', 'test', '*.jpg')
files = sorted(glob.glob(path))
ch = split_list(files, 5)
X_test = []
X_test_id = []
print('Start image: ' + str(ch[part][0]))
print('Last image: ' + str(ch[part][-1]))
for fl in ch[part]:
flbase = os.path.basename(fl)
img = get_im_cv2(fl)
X_test.append(img)
X_test_id.append(flbase)
return X_test, X_test_id
def cache_data(data, path):
if os.path.isdir(os.path.dirname(path)):
file = open(path, 'wb')
pickle.dump(data, file)
file.close()
else:
print('Directory doesnt exists')
def restore_data(path):
data = dict()
if os.path.isfile(path):
file = open(path, 'rb')
data = pickle.load(file)
return data
def save_model(model):
json_string = model.to_json()
if not os.path.isdir('cache'):
os.mkdir('cache')
open(os.path.join('cache', 'architecture_vgg16.json'), 'w').write(json_string)
model.save_weights(os.path.join('cache', 'model_weights_vgg16.h5'), overwrite=True)
def read_model():
model = model_from_json(open(os.path.join('cache', 'architecture_vgg16.json')).read())
model.load_weights(os.path.join('cache', 'model_weights_vgg16.h5'))
return model
def split_validation_set(train, target, test_size):
random_state = 51
X_train, X_test, y_train, y_test = train_test_split(train, target, test_size=test_size, random_state=random_state)
return X_train, X_test, y_train, y_test
def create_submission(predictions, test_id, info):
result1 = pd.DataFrame(predictions, columns=['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9'])
result1.loc[:, 'img'] = pd.Series(test_id, index=result1.index)
now = datetime.datetime.now()
if not os.path.isdir('subm'):
os.mkdir('subm')
suffix = info + '_' + str(now.strftime("%Y-%m-%d-%H-%M"))
sub_file = os.path.join('subm', 'submission_' + suffix + '.csv')
result1.to_csv(sub_file, index=False)
def save_useful_data(predictions_valid, valid_ids, model, info):
result1 = pd.DataFrame(predictions_valid, columns=['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9'])
result1.loc[:, 'img'] = pd.Series(valid_ids, index=result1.index)
now = datetime.datetime.now()
if not os.path.isdir(os.path.join('subm', 'data')):
os.mkdir(os.path.join('subm', 'data'))
suffix = info + '_' + str(now.strftime("%Y-%m-%d-%H-%M"))
# Save predictions
pred_file = os.path.join('subm', 'data', 's_' + suffix + '_train_predictions.csv')
result1.to_csv(pred_file, index=False)
# Save model
json_string = model.to_json()
model_file = os.path.join('subm', 'data', 's_' + suffix + '_model.json')
open(model_file, 'w').write(json_string)
# Save code
cur_code = os.path.realpath(__file__)
code_file = os.path.join('subm', 'data', 's_' + suffix + '_code.py')
copy2(cur_code, code_file)
def read_and_normalize_train_data():
cache_path = os.path.join('cache', 'train_r_' + str(224) + '_c_' + str(224) + '_t_' + str(3) + '.dat')
if not os.path.isfile(cache_path) or use_cache == 0:
train_data, train_target, train_id, driver_id, unique_drivers = load_train()
cache_data((train_data, train_target, train_id, driver_id, unique_drivers), cache_path)
else:
print('Restore train from cache!')
(train_data, train_target, train_id, driver_id, unique_drivers) = restore_data(cache_path)
print('Convert to numpy...')
train_data = np.array(train_data, dtype=np.uint8)
train_target = np.array(train_target, dtype=np.uint8)
print('Reshape...')
train_data = train_data.transpose((0, 3, 1, 2))
print('Convert to float...')
train_data = train_data.astype('float16')
mean_pixel = [103.939, 116.779, 123.68]
print('Substract 0...')
train_data[:, 0, :, :] -= mean_pixel[0]
print('Substract 1...')
train_data[:, 1, :, :] -= mean_pixel[1]
print('Substract 2...')
train_data[:, 2, :, :] -= mean_pixel[2]
train_target = np_utils.to_categorical(train_target, 10)
# Shuffle experiment START !!!
perm = permutation(len(train_target))
train_data = train_data[perm]
train_target = train_target[perm]
# Shuffle experiment END !!!
print('Train shape:', train_data.shape)
print(train_data.shape[0], 'train samples')
return train_data, train_target, train_id, driver_id, unique_drivers
def read_and_normalize_test_data(part):
start_time = time.time()
cache_path = os.path.join('cache', 'test_r_' + str(224) +
'_c_' + str(224) +
'_part_' + str(part) +
'.dat')
if not os.path.isfile(cache_path) or use_cache == 0:
test_data, test_id = load_test(part)
cache_data((test_data, test_id), cache_path)
else:
print('Restore test from cache [{}]!'.format(part))
(test_data, test_id) = restore_data(cache_path)
test_data = np.array(test_data, dtype=np.uint8)
test_data = test_data.transpose((0, 3, 1, 2))
test_data = test_data.astype('float16')
mean_pixel = [103.939, 116.779, 123.68]
test_data[:, 0, :, :] -= mean_pixel[0]
test_data[:, 1, :, :] -= mean_pixel[1]
test_data[:, 2, :, :] -= mean_pixel[2]
print('Test shape:', test_data.shape)
print(test_data.shape[0], 'test samples')
print('Read and process test data time: {} seconds'.format(round(time.time() - start_time, 2)))
return test_data, test_id
def dict_to_list(d):
ret = []
for i in d.items():
ret.append(i[1])
return ret
def merge_several_folds_mean(data, nfolds):
a = np.array(data[0])
for i in range(1, nfolds):
a += np.array(data[i])
a /= nfolds
return a.tolist()
def copy_selected_drivers(train_data, train_target, driver_id, driver_list):
data = []
target = []
index = []
for i in range(len(driver_id)):
if driver_id[i] in driver_list:
data.append(train_data[i])
target.append(train_target[i])
index.append(i)
data = np.array(data)
target = np.array(target)
index = np.array(index)
return data, target, index
def VGG_16():
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(3, 224, 224)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), stride=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), stride=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), stride=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), stride=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), stride=(2,2)))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
f = h5py.File('weights/vgg16_weights.h5')
for k in range(f.attrs['nb_layers']):
if k >= len(model.layers):
# we don't look at the last (fully-connected) layers in the savefile
break
g = f['layer_{}'.format(k)]
weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
model.layers[k].set_weights(weights)
f.close()
print('Model loaded.')
model.add(Dense(10, activation='softmax'))
sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy')
return model
def get_validation_predictions(train_data, predictions_valid):
pv = []
for i in range(len(train_data)):
pv.append(predictions_valid[i])
return pv
class EarlyStoppingByLossVal(Callback):
def __init__(self, monitor='val_loss', value=0.00001, verbose=0):
super(Callback, self).__init__()
self.monitor = monitor
self.value = value
self.verbose = verbose
def on_epoch_end(self, epoch, logs={}):
current = logs.get(self.monitor)
if current is None:
warnings.warn("Early stopping requires %s available!" % self.monitor, RuntimeWarning)
if current < self.value:
if self.verbose > 0:
print("Epoch %05d: early stopping THR" % epoch)
self.model.stop_training = True
def run_cross_validation_create_models(nfolds=10):
# input image dimensions
batch_size = 16
nb_epoch = 25
random_state = 51
restore_from_last_checkpoint = 1
train_data, train_target, train_id, driver_id, unique_drivers = read_and_normalize_train_data()
yfull_train = dict()
kf = KFold(len(unique_drivers), n_folds=nfolds, shuffle=True, random_state=random_state)
num_fold = 0
sum_score = 0
for train_drivers, test_drivers in kf:
model = VGG_16()
unique_list_train = [unique_drivers[i] for i in train_drivers]
X_train, Y_train, train_index = copy_selected_drivers(train_data, train_target, driver_id, unique_list_train)
unique_list_valid = [unique_drivers[i] for i in test_drivers]
X_valid, Y_valid, test_index = copy_selected_drivers(train_data, train_target, driver_id, unique_list_valid)
num_fold += 1
print('Start KFold number {} from {}'.format(num_fold, nfolds))
print('Split train: ', len(X_train), len(Y_train))
print('Split valid: ', len(X_valid), len(Y_valid))
print('Train drivers: ', unique_list_train)
print('Test drivers: ', unique_list_valid)
kfold_weights_path = os.path.join('cache', 'weights_kfold_vgg16_' + str(num_fold) + '.h5')
if not os.path.isfile(kfold_weights_path) or restore_from_last_checkpoint == 0:
callbacks = [
EarlyStoppingByLossVal(monitor='val_loss', value=0.00001, verbose=1),
EarlyStopping(monitor='val_loss', patience=5, verbose=0),
ModelCheckpoint(kfold_weights_path, monitor='val_loss', save_best_only=True, verbose=0),
]
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
shuffle=True, verbose=1, validation_data=(X_valid, Y_valid),
callbacks=callbacks)
if os.path.isfile(kfold_weights_path):
model.load_weights(kfold_weights_path)
# score = model.evaluate(X_valid, Y_valid, show_accuracy=True, verbose=0)
# print('Score log_loss: ', score[0])
predictions_valid = model.predict(X_valid.astype('float32'), batch_size=batch_size, verbose=1)
score = log_loss(Y_valid, predictions_valid)
print('Score log_loss: ', score)
sum_score += score*len(test_index)
# Store valid predictions
for i in range(len(test_index)):
yfull_train[test_index[i]] = predictions_valid[i]
score = sum_score/len(train_data)
print("Log_loss train independent avg: ", score)
predictions_valid = get_validation_predictions(train_data, yfull_train)
print('Final log_loss: {}, nfolds: {} epoch: {}'.format(score, nfolds, nb_epoch))
info_string = 'loss_' + str(score) \
+ '_folds_' + str(nfolds) \
+ '_ep_' + str(nb_epoch)
save_useful_data(predictions_valid, train_id, model, info_string)
score1 = log_loss(train_target, predictions_valid)
if abs(score1 - score) > 0.0001:
print('Check error: {} != {}'.format(score, score1))
def append_chunk(main, part):
for p in part:
main.append(p)
return main
def run_cross_validation_process_test(nfolds = 10):
batch_size = 16
num_fold = 0
yfull_test = []
test_id = []
for i in range(nfolds):
model = VGG_16()
num_fold += 1
print('Start KFold number {} from {}'.format(num_fold, nfolds))
kfold_weights_path = os.path.join('cache', 'weights_kfold_vgg16_' + str(num_fold) + '.h5')
model.load_weights(kfold_weights_path)
kfold_test_validation_path = os.path.join('cache', 'test_kfold_vgg16_' + str(num_fold) + '.pickle.dat')
kfold_test_ids_path = os.path.join('cache', 'test_kfold_vgg16_ids.pickle.dat')
if not os.path.isfile(kfold_test_validation_path):
test_prediction = []
for part in range(5):
print('Reading test data part {}...'.format(part))
test_data_chunk, test_id_chunk = read_and_normalize_test_data(part)
test_prediction_chunk = model.predict(test_data_chunk, batch_size=batch_size, verbose=1)
test_prediction = append_chunk(test_prediction, test_prediction_chunk)
if i == 0:
test_id = append_chunk(test_id, test_id_chunk)
cache_data(test_prediction, kfold_test_validation_path)
if i == 0:
cache_data(test_id, kfold_test_ids_path)
else:
print('Restore data from cache...')
test_prediction = restore_data(kfold_test_validation_path)
if i == 0:
test_id = restore_data(kfold_test_ids_path)
yfull_test.append(test_prediction)
test_res = merge_several_folds_mean(yfull_test, nfolds)
info_string = 'loss_' \
+ '_r_' + str(224) \
+ '_c_' + str(224) \
+ '_folds_' + str(nfolds)
suffix = info_string + '_' + str(datetime.datetime.now().strftime("%Y-%m-%d-%H-%M"))
cache_data((yfull_test, test_id), os.path.join("subm", "full_array_" + suffix + ".pickle.dat"))
create_submission(test_res, test_id, info_string)
# Store debug submissions
for i in range(nfolds):
info_string1 = info_string + '_debug_' + str(i)
create_submission(yfull_test[i], test_id, info_string1)
if __name__ == '__main__':
num_folds = 10
if not os.path.isdir("subm"):
os.mkdir("subm")
if not os.path.isdir("cache"):
os.mkdir("cache")
if not os.path.isfile("weights/vgg16_weights.h5"):
print('Please put VGG16 pretrained weights in weights/vgg16_weights.h5')
exit()
run_cross_validation_create_models(num_folds)
run_cross_validation_process_test(num_folds)