Kaggle competition: Quick, Draw! Doodle Recognition Challenge
<SOS> metric: MAP@{K} Kaggle data source: quick,-draw!-doodle-recognition-challenge<choose_model_class>
import os
import cv2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from skimage.draw import draw
from glob import glob
from sklearn.preprocessing import LabelEncoder
from keras.utils.np_utils import to_categorical
from multiprocessing.dummy import Pool
from keras.models import load_model
import time
import ast
import keras
import random
import glob
import math
input_layer = Input(( img_size_target, img_size_target, 1))
output_layer = build_model(input_layer, 16,0.5)
 model1 = Model(input_layer, output_layer)
 c = optimizers.adam(lr = 0.01)
 model1.compile(loss="binary_crossentropy", optimizer=c, metrics=[my_iou_metric])
<train_model>
ALL_FILES = glob.glob('.. /input/shuffle-csvs*/*.csv.gz')
 VALIDATION_FILE = '.. /input/shuffle-csvs-75000-100000/train_k0.csv.gz'
ALL_FILES.remove(VALIDATION_FILE)
 INPUT_DIR = '.. /input/quickdraw-doodle-recognition/'
BASE_SIZE = 256
NCATS = 340
np.random.seed(seed=1987)
 def apk(actual, predicted, k=3):

 if len(predicted)> k:
predicted = predicted[:k]
score = 0.0
num_hits = 0.0
for i, p in enumerate(predicted):
if p in actual and p not in predicted[:i]:
num_hits += 1.0
score += num_hits /(i + 1.0)
 if not actual:
return 0.0
return score / min(len(actual), k)
 def mapk(actual, predicted, k=3):

 return np.mean([apk(a, p, k)for a, p in zip(actual, predicted)])
 def preds2catids(predictions):
return pd.DataFrame(np.argsort(-predictions, axis=1)[:, :3], columns=['a', 'b', 'c'])
 def f2cat(filename: str)-> str:
return filename.split('.')[0]
def list_all_categories() :
files = os.listdir(os.path.join(INPUT_DIR, 'train_simplified'))
return sorted([f2cat(f)for f in files], key=str.lower)
 def plot_batch(x):
cols = 4
rows = 6
fig, axs = plt.subplots(nrows=rows, ncols=cols, sharex=True, sharey=True, figsize=(18, 18))
for i in range(rows):
for k in range(0,3):
ax = axs[i, k]
ax.imshow(x[i, :, :, k], cmap=plt.cm.gray)
 ax.axis('off')
 ax = axs[i, 3]
ax.imshow(x[i, :, :],)
 ax.axis('off')
 fig.tight_layout()
plt.show() ;
model_checkpoint = ModelCheckpoint(save_model_name,monitor='my_iou_metric',
mode = 'max', save_best_only=True, verbose=1)
 reduce_lr = ReduceLROnPlateau(monitor='my_iou_metric', mode = 'max',factor=0.5, patience=5, min_lr=0.0001, verbose=1)
 epochs = 50
batch_size = 32
history = model1.fit(x_train, y_train,
validation_data=[x_valid, y_valid],
epochs=epochs,
batch_size=batch_size,
callbacks=[ model_checkpoint,reduce_lr],
verbose=2 )<choose_model_class>
AUGMENTATION = True
STEPS = 500
BATCH_SIZE = 448
EPOCHS = 0
LEARNING_RATE = 0.002
IMG_SHAPE =(128,128,3)
 IMG_SIZE = IMG_SHAPE[0]
model1 = load_model(save_model_name,custom_objects={'my_iou_metric': my_iou_metric})
 input_x = model1.layers[0].input
output_layer = model1.layers[-1].input
model = Model(input_x, output_layer)
 c = optimizers.adam(lr = 0.01)
 model.compile(loss=lovasz_loss, optimizer=c, metrics=[my_iou_metric_2])
<train_model>
def image_generator(size, batchsize, lw=6, augmentation = False):
while True:
for filename in ALL_FILES:
for df in pd.read_csv(filename, chunksize=batchsize):
df['drawing'] = df['drawing'].apply(eval)
 x = np.zeros(( len(df), size, size,3))
for i, raw_strokes in enumerate(df.drawing.values):
x[i] = draw_cv2(raw_strokes, size=size, lw=lw, augmentation = augmentation)
 x = x / 255.
 x = x.reshape(( len(df), size, size, 3)).astype(np.float32)
 y = keras.utils.to_categorical(df.y, num_classes=NCATS)
 yield x, y
def valid_generator(valid_df, size, batchsize, lw=6):
while(True):
for i in range(0,len(valid_df),batchsize):
chunk = valid_df[i:i+batchsize]
x = np.zeros(( len(chunk), size, size,3))
for i, raw_strokes in enumerate(chunk.drawing.values):
x[i] = draw_cv2(raw_strokes, size=size, lw=lw)
 x = x / 255.
 x = x.reshape(( len(chunk), size, size,3)).astype(np.float32)
 y = keras.utils.to_categorical(chunk.y, num_classes=NCATS)
 yield x,y
def test_generator(test_df, size, batchsize, lw=6):
for i in range(0,len(test_df),batchsize):
chunk = test_df[i:i+batchsize]
x = np.zeros(( len(chunk), size, size,3))
for i, raw_strokes in enumerate(chunk.drawing.values):
x[i] = draw_cv2(raw_strokes, size=size, lw=lw)
 x = x / 255.
 x = x.reshape(( len(chunk), size, size, 3)).astype(np.float32)
 yield x
train_datagen = image_generator(size=IMG_SIZE, batchsize=BATCH_SIZE, augmentation = AUGMENTATION)
 valid_df = pd.read_csv(VALIDATION_FILE)
 valid_df['drawing'] = valid_df['drawing'].apply(eval)
 validation_steps = len(valid_df)//BATCH_SIZE
valid_datagen = valid_generator(valid_df, size=IMG_SIZE, batchsize=BATCH_SIZE )
early_stopping = EarlyStopping(monitor='val_my_iou_metric_2', mode = 'max',patience=20, verbose=1)
 model_checkpoint = ModelCheckpoint(save_model_name,monitor='val_my_iou_metric_2',
mode = 'max', save_best_only=True, verbose=1)
 reduce_lr = ReduceLROnPlateau(monitor='val_my_iou_metric_2', mode = 'max',factor=0.5, patience=5, min_lr=0.0001, verbose=1)
 epochs = 50
batch_size = 32
history = model.fit(x_train, y_train,
validation_data=[x_valid, y_valid],
epochs=epochs,
batch_size=batch_size,
callbacks=[ model_checkpoint,reduce_lr,early_stopping],
verbose=2 )<load_pretrained>
def top_3_accuracy(y_true, y_pred):
return keras.metrics.top_k_categorical_accuracy(y_true, y_pred, k=3)
 reducer = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=10)
 checkpointer = ModelCheckpoint(filepath='mobileNet-best.hdf5', verbose=0, save_best_only=True)
 model = load_model('.. /input/mobilenet-126x126x3-100k-per-class/mobileNet.hdf5', custom_objects = {'top_3_accuracy':top_3_accuracy})
 opt = Adam(lr = LEARNING_RATE)
 model.compile(optimizer = opt, loss = 'categorical_crossentropy', metrics = ['accuracy', top_3_accuracy])
 model.summary()
model = load_model(save_model_name,custom_objects={'my_iou_metric_2': my_iou_metric_2,
'lovasz_loss': lovasz_loss})
<predict_on_test>
history = model.fit_generator(train_datagen, steps_per_epoch=STEPS, epochs=EPOCHS , validation_data=valid_datagen, validation_steps = validation_steps, callbacks= [checkpointer, reducer])
 model.save('mobileNet.hdf5')
def predict_result(model,x_test,img_size_target):
x_test_reflect = np.array([np.fliplr(x)for x in x_test])
 preds_test = model.predict(x_test ).reshape(-1, img_size_target, img_size_target)
 preds_test2_refect = model.predict(x_test_reflect ).reshape(-1, img_size_target, img_size_target)
 preds_test += np.array([ np.fliplr(x)for x in preds_test2_refect])
 return preds_test/2<predict_on_test>
log_df = pd.read_csv('.. /input/whole-training-log/log.csv' )
preds_valid = predict_result(model,x_valid,img_size_target )<compute_train_metric>
log = pd.DataFrame.from_dict(history.history)
 log.to_csv('train_log.csv', index=False )
thresholds_ori = np.linspace(0.3, 0.7, 31)
 thresholds = np.log(thresholds_ori/(1-thresholds_ori))
ious = np.array([iou_metric_batch(y_valid, preds_valid > threshold)for threshold in tqdm_notebook(thresholds)])
 print(ious )<categorify>
gen = test_generator(valid_df, size=IMG_SIZE, batchsize=BATCH_SIZE)
 valid_predictions = model.predict_generator(gen, steps = validation_steps, verbose=1)
 map3 = mapk(valid_df[['y']].values, preds2catids(valid_predictions ).values)
 print('Map3: {:.3f}'.format(map3))
<predict_on_test><EOS> 
submission_df = pd.read_csv(os.path.join(INPUT_DIR, 'test_simplified.csv'))
submission_df['drawing'] = submission_df['drawing'].apply(eval)
 submission_datagen = test_generator(submission_df, size=IMG_SIZE, batchsize=BATCH_SIZE)
 submission_predictions = model.predict_generator(submission_datagen, math.ceil(len(submission_df)/BATCH_SIZE))
cats = list_all_categories()
id2cat = {k: cat.replace(' ', '_')for k, cat in enumerate(cats)}
top3 = preds2catids(submission_predictions)
 top3cats = top3.replace(id2cat)
 submission_df['word'] = top3cats['a'] + ' ' + top3cats['b'] + ' ' + top3cats['c']
submission = submission_df[['key_id', 'word']]
submission.to_csv('submission.csv', index=False )