{"cells":[{"metadata":{},"cell_type":"markdown","source":"<h1 style=\"color:#ff355e; font-family:Ewert; font-size:150%;\" class=\"font-effect-3d\">Code Library, Style, & Links</h1>","execution_count":null},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"%%html\n<style>\n@import url('https://fonts.googleapis.com/css?family=Ewert|Roboto&effect=3d');\nspan {font-family:'Roboto'; color:black; text-shadow:4px 4px 4px slategray;}  \ndiv.output_area pre{font-family:'Roboto'; font-size:110%; color:#ff355e;}      \n</style>","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"import numpy as np,pandas as pd\nimport os,h5py,warnings\nimport tensorflow as tf,pylab as pl\nfrom tensorflow.keras import layers as tkl\nfrom sklearn.metrics import confusion_matrix,\\\nclassification_report\nfrom IPython.core.magic import register_line_magic\nwarnings.filterwarnings('ignore')\npl.style.use('seaborn-whitegrid')\nstyle_dict={'background-color':'silver','color':'#ff355e', \n            'border-color':'white','font-family':'Roboto'}\nfpath='../input/quick-draw-images-from-key-points/'\nfpath2='../input/quick-draw-images-from-key-points-2/'\nfpath3='../input/quick-draw-images-from-key-points-3/'\nfpath4='../input/quick-draw-images-from-key-points-4/'\nfpath5='../input/quick-draw-images-from-key-points-5/'\nfpath6='../input/quick-draw-images-from-key-points-6/'\nfpath7='../input/quick-draw-images-from-key-points-7/'\nfiles=sorted(os.listdir(fpath))\nfiles2=sorted(os.listdir(fpath2))\nfiles3=sorted(os.listdir(fpath3))\nfiles4=sorted(os.listdir(fpath4))\nfiles5=sorted(os.listdir(fpath5))\nfiles6=sorted(os.listdir(fpath6))\nfiles7=sorted(os.listdir(fpath7))\nfiles2=files2[1:]+[files2[0]]\n[files,files2,files3,files4,files5,files6,files7]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h1 style=\"color:#ff355e; font-family:Ewert; font-size:150%;\" class=\"font-effect-3d\">Data Construction</h1>","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"labels=os.listdir('../input/quickdraw-doodle-recognition/'+\\\n                  'train_simplified/')\nlabels=np.array(sorted([l[:-4] for l in labels]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"D=700; x=[]; y=[]\n@register_line_magic\ndef load_img(n):\n    global D,x,y\n    if n=='1': fp=fpath; fns=files; m=5\n    if n=='2': fp=fpath2; fns=files2; m=5\n    if n=='3': fp=fpath3; fns=files3; m=5\n    if n=='4': fp=fpath4; fns=files4; m=5\n    if n=='5': fp=fpath5; fns=files5; m=5\n    if n=='6': fp=fpath6; fns=files6; m=5\n    if n=='7': fp=fpath7; fns=files7; m=4\n    for i in range(m):\n        f=h5py.File(fp+fns[i],'r')\n        keys=list(f.keys())\n        x+=[f[keys[0]][i*10000:i*10000+D] \n            for i in range(10)]\n        y+=[f[keys[1]][i*10000:i*10000+D]\n            for i in range(10)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%load_img 1\n%load_img 2\n%load_img 3\n%load_img 4\n%load_img 5\n%load_img 6\n%load_img 7","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"x=np.array(x)\nnum_classes=x.shape[0]\nx=x.reshape(num_classes*D,96,96)\ny=np.array(y).reshape(num_classes*D)\nN=y.shape[0]; n=int(.1*N)\nshuffle_ids=np.arange(N)\nnp.random.RandomState(12).shuffle(shuffle_ids)\nx,y=x[shuffle_ids],y[shuffle_ids]\nx.shape,y.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"nn=np.random.randint(0,N,5)\nll=[labels[y[nn[i]]] for i in range(5)]\npl.figure(figsize=(10,3))\nfor i in range(5):\n    pl.subplot(1,5,i+1)\n    pl.imshow(x[nn[i]],cmap='Pastel1')\npl.suptitle('Key Points to Lines: \\n%s'%ll);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x=x.reshape(-1,96,96,1)\nx_test,x_valid,x_train=x[:n],x[n:2*n],x[2*n:]\ny_test,y_valid,y_train=y[:n],y[n:2*n],y[2*n:]\ndel x,y,shuffle_ids","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h1 style=\"color:#ff355e; font-family:Ewert; font-size:150%;\" class=\"font-effect-3d\">Simple Models for Many Labels</h1>","execution_count":null},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"def model():\n    model=tf.keras.Sequential()    \n    model.add(tkl.Conv2D(32,(5,5),padding='same',\n                         input_shape=x_train.shape[1:]))\n    model.add(tkl.LeakyReLU(alpha=.02))\n    model.add(tkl.MaxPooling2D(pool_size=(2,2)))\n    model.add(tkl.Dropout(.2))\n    model.add(tkl.Conv2D(196,(5,5)))\n    model.add(tkl.LeakyReLU(alpha=.02))    \n    model.add(tkl.MaxPooling2D(pool_size=(2,2)))\n    model.add(tkl.Dropout(.2))\n    model.add(tkl.GlobalMaxPooling2D())   \n    model.add(tkl.Dense(1024))\n    model.add(tkl.LeakyReLU(alpha=.02))\n    model.add(tkl.Dropout(.5))     \n    model.add(tkl.Dense(num_classes))\n    model.add(tkl.Activation('softmax'))\n    model.compile(loss='sparse_categorical_crossentropy',\n                  optimizer='adam', \n                  metrics=['accuracy'])\n    return model\nmodel=model()\ntf.keras.utils.plot_model(model,show_shapes=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"fw='weights.best.cv001-%s'%num_classes+'.hdf5'\ncheckpointer=tf.keras.callbacks.\\\nModelCheckpoint(filepath=fw,verbose=2,\n                save_best_only=True)\nlr_reduction=tf.keras.callbacks.\\\nReduceLROnPlateau(monitor='val_loss',\n                  patience=5,verbose=2,factor=.75)\nhistory=model.fit(x_train,y_train,epochs=100,\n                  batch_size=512,verbose=2,\n                  validation_data=(x_valid,y_valid),\n                  callbacks=[checkpointer,lr_reduction])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.load_weights(fw)\nmodel.evaluate(x_test,y_test)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"py_test=model.predict(x_test)\npy_test=[np.argmax(x) for x in py_test]\nprint(classification_report(y_test,py_test))","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"pd.DataFrame(confusion_matrix(y_test,py_test))\\\n.head(20).T.style.set_properties(**style_dict)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}