Kaggle competition: Quick, Draw! Doodle Recognition Challenge
<SOS> metric: MAP@{K} Kaggle data source: quick,-draw!-doodle-recognition-challenge<save_to_csv>
%matplotlib inline
InteractiveShell.ast_node_interactivity = "all"
plt.rcParams['figure.figsize'] = [16, 10]
plt.rcParams['font.size'] = 14
sub = pd.DataFrame.from_dict(pred_dict,orient='index')
 sub.index.names = ['id']
sub.columns = ['rle_mask']
sub.to_csv(submission_file )<train_model>
DP_DIR = '.. /input/shuffle-csvs/'
INPUT_DIR = '.. /input/quickdraw-doodle-recognition/'
BASE_SIZE = 256
NCSVS = 100
NCATS = 340
np.random.seed(seed=1987)
 tf.set_random_seed(seed=1987)
 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 )
t_finish = time.time()
print(f"Kernel run time = {(t_finish-t_start)/3600} hours" )<set_options>
size = 256
batchsize = 512
plt.style.use('seaborn-white')
 sns.set_style("white")
 %matplotlib inline 
<define_variables>
train_datagen = image_generator_xd(size=size, batchsize=batchsize, ks=range(NCSVS - 1))
im_width = 101
im_height = 101
im_chan = 1
basicpath = '.. /input/'
path_train = basicpath + 'train/'
path_test = basicpath + 'test/'
path_train_images = path_train + 'images/'
path_train_masks = path_train + 'masks/'
path_test_images = path_test + 'images/' 
<train_model>
color = [(187,255,255),(106, 90, 205),(0, 255, 127),(255,255,0),(255,193,37),(205,92,92), 
(244, 164, 96),(255, 105, 180),(218, 112, 214),(255, 165, 0),(139, 134, 130),(24, 116, 205), 
(187,255,255),(106, 90, 205),(0, 255, 127),(255,255,0),(255,193,37),(205,92,92), 
(244, 164, 96),(255, 105, 180),(218, 112, 214),(255, 165, 0),(139, 134, 130),(24, 116, 205), 
(187,255,255),(106, 90, 205),(0, 255, 127),(255,255,0),(255,193,37),(205,92,92), 
(244, 164, 96),(255, 105, 180),(218, 112, 214),(255, 165, 0),(139, 134, 130),(24, 116, 205)]
def draw_cv2_strokes(raw_strokes, size=256, lw=4, color = color):
img = np.zeros(( size, size, 3), np.uint8)
 for t, stroke in enumerate(raw_strokes):
cl = color[t]
for i in range(len(stroke[0])- 1):
_ = cv2.line(img,(stroke[0][i] * size // BASE_SIZE, stroke[1][i] * size // BASE_SIZE), 
(stroke[0][i + 1] * size // BASE_SIZE, stroke[1][i + 1] * size // BASE_SIZE), cl, lw)
 return img
def image_generator_xd(size, batchsize, ks, lw=6):
while True:
for k in np.random.permutation(ks):
filename = os.path.join(DP_DIR, 'train_k{}.csv.gz'.format(k))
for df in pd.read_csv(filename, chunksize=batchsize):
df['drawing'] = df['drawing'].apply(ast.literal_eval)
 x = np.zeros(( len(df), size, size, 3))
for i, raw_strokes in enumerate(df.drawing.values):
num_of_strokes = len(raw_strokes)
 if num_of_strokes == 1:
x[i, :, :, 0] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 0]
x[i, :, :, 1] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 1]
x[i, :, :, 2] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 2]
elif num_of_strokes == 2:
x[i, :, :, 0] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 0]
x[i, :, :, 1] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 1]
x[i, :, :, 2] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 2]
else:
x[i, :, :, 0] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 0]
x[i, :, :, 1] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 1]
x[i, :, :, 2] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 2]
x = preprocess_input(x ).astype(np.float32)
 y = keras.utils.to_categorical(df.y, num_classes=NCATS)
 yield x, y
def df_to_image_array_xd(df, size, lw=6):
df['drawing'] = df['drawing'].apply(ast.literal_eval)
 x = np.zeros(( len(df), size, size, 3))
for i, raw_strokes in enumerate(df.drawing.values):
num_of_strokes = len(raw_strokes)
 if num_of_strokes == 1:
x[i, :, :, 0] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 0]
x[i, :, :, 1] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 1]
x[i, :, :, 2] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 2]
elif num_of_strokes == 2:
x[i, :, :, 0] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 0]
x[i, :, :, 1] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 1]
x[i, :, :, 2] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 2]
else:
x[i, :, :, 0] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 0]
x[i, :, :, 1] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 1]
x[i, :, :, 2] = draw_cv2_strokes(raw_strokes, size=size, lw=lw 
)[:, :, 2]
x = preprocess_input(x ).astype(np.float32)
 return x
<load_from_csv><EOS> 
train_datagen = image_generator_xd(size=size, batchsize=batchsize, ks=range(NCSVS - 1))