{"cells":[{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"from IPython.display import display,HTML\ndef dhtml(str):\n    display(HTML(\"\"\"<style>\n    @import 'https://fonts.googleapis.com/css?family=Smokum&effect=3d';      \n    </style><h1 class='font-effect-3d' \n    style='font-family:Smokum; color:#aa33ff; font-size:35px;'>\n    %s</h1>\"\"\"%str))","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"dhtml('Code Library, Style, and Links')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"65b564b2cd9c86c6ee962ebc4ad320d4988ce99c"},"cell_type":"markdown","source":"The previous notebook => [Quick, Draw! Doodle Recognition OpenCV1](https://www.kaggle.com/olgabelitskaya/quick-draw-doodle-recognition-1)"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"%%html\n<style>\n@import url('https://fonts.googleapis.com/css?family=Ewert|Roboto&effect=3d|ice|');\nspan {font-family:'Roboto'; color:black; text-shadow: 5px 5px 5px #aaa;}  \ndiv.output_area pre{font-family:'Roboto'; font-size:110%; color: steelblue;}      \n</style>","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9fd863655b240c3540be94f2d0a1505387837bc7"},"cell_type":"code","source":"import numpy as np,pandas as pd,keras as ks\nimport os,ast,cv2,warnings\nimport pylab as pl\nfrom skimage.transform import resize\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix,\\\nclassification_report\nfrom keras.callbacks import ModelCheckpoint,\\\nReduceLROnPlateau\nfrom keras.models import Sequential\nfrom keras.layers.advanced_activations import LeakyReLU\nfrom keras.layers import Activation,Dropout,Dense,\\\nConv2D,MaxPooling2D,GlobalMaxPooling2D\nwarnings.filterwarnings('ignore')\npl.style.use('seaborn-whitegrid')\nstyle_dict={'background-color':'gainsboro','color':'#aa33ff', \n            'border-color':'white','font-family':'Roboto'}\nfpath='../input/quickdraw-doodle-recognition/train_simplified/'\nwpath='../input/quick-draw-model-weights-for-doodle-recognition/'+\\\n      'weights_cv/weights_cv/'\ntpath='../input/quickdraw-doodle-recognition/test_simplified.csv'\nos.listdir(\"../input\")","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"dhtml('Data Exploration')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"files=sorted(os.listdir(fpath))\nlabels=[el.replace(\" \",\"_\")[:-4] for el in files]\nprint(labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"04b7ef643345593df3dac4dcd97cfd0a340ce9f1","_kg_hide-output":true},"cell_type":"code","source":"weights=sorted(os.listdir(wpath))\nprint(weights)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8276d79d5314db939ee49b4d1a77432812757ad5"},"cell_type":"code","source":"I=64 # image size in pixels\nT=20 # number of labels in one set","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"dhtml('The Model')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"da060723b18432ff3154ada18956cd670d1ddb8f"},"cell_type":"code","source":"def model():\n    model=Sequential()\n    model.add(Conv2D(32,(5,5),padding='same',\n                     input_shape=(I,I,1)))\n    model.add(LeakyReLU(alpha=.02))   \n    model.add(MaxPooling2D(pool_size=(2,2)))\n    model.add(Dropout(.2))\n    model.add(Conv2D(196,(5,5)))\n    model.add(LeakyReLU(alpha=.02))  \n    model.add(MaxPooling2D(pool_size=(2,2)))\n    model.add(Dropout(.2))\n    model.add(GlobalMaxPooling2D())   \n    model.add(Dense(1024))\n    model.add(LeakyReLU(alpha=.02))\n    model.add(Dropout(.5))   \n    model.add(Dense(T))\n    model.add(Activation('softmax'))\n    model.compile(loss='sparse_categorical_crossentropy',\n                  optimizer='adam',metrics=['accuracy'])\n    return model\nmodel=model()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"dhtml('Test Predictions')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"def get_image(data,lw=7,time_color=True):\n    data=ast.literal_eval(data)\n    image=np.zeros((300,300),np.uint8)\n    for t,s in enumerate(data):\n        for i in range(len(s[0])-1):\n            color=255-min(t,10)*15 if time_color else 255\n            _=cv2.line(image,(s[0][i]+15,s[1][i]+15),\n                       (s[0][i+1]+15,s[1][i+1]+15),color,lw) \n    return cv2.resize(image,(I,I))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5356ff182e6c05915c13b89d1a160aec5df3afca","_kg_hide-input":true},"cell_type":"code","source":"def test_predict(data):\n    images=[]\n    images.extend([get_image(data.drawing.iloc[i]) \n                   for i in range(len(data))])    \n    images=np.array(images)\n    model.load_weights(wpath+weights[0])\n    predictions=model.predict(images.reshape(-1,I,I,1))\n    for w in weights[1:]:\n        w=wpath+w\n        model.load_weights(w)\n        predictions2=model.predict(images.reshape(-1,I,I,1))\n        predictions=np.concatenate((predictions,predictions2),\n                                   axis=1)        \n    return predictions","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true,"_uuid":"7de78611fd69c66952923c51bc91866eaf7a6560"},"cell_type":"code","source":"test_data=pd.read_csv(tpath,index_col='key_id')\ntest_data.tail(3).T.style\\\n.set_properties(**style_dict)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9b0954789e64cd0c608ceaadb1ab3cdaf99eceb9","_kg_hide-output":true},"cell_type":"code","source":"test_predictions=test_predict(test_data)\ntest_predictions[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"15c5ff1e0e5cef537daf59f41aaab7b087d3c6a3"},"cell_type":"code","source":"test_labels=[[labels[i] for i in \\\n              test_predictions[k].argsort()[-10:][::-1]] \\\n             for k in range(len(test_predictions))]\ntest_labels=[\" \".join(test_labels[i]) \\\n             for i in range(len(test_labels))]\npresubmission=pd.DataFrame({\"key_id\":test_data.index,\n                            \"word\":test_labels})\npresubmission.to_csv('submission_10best.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"def display_drawing(n):\n    pl.figure(figsize=(4,2*n))\n    pl.suptitle('Test Pictures')\n    for i in range(n):\n        picture=ast.literal_eval(\n            test_data.drawing.values[i])\n        for x,y in picture:\n            pl.subplot(n,1,i+1)\n            pl.plot(x, y,'-o',color='gainsboro')\n            pl.xticks([]); pl.yticks([])\n            pl.title(presubmission.iloc[i][1])\n        pl.gca().invert_yaxis()\n        pl.axis('equal')       ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"39612dc351aa3a54c68ef64101274e371eebc056","_kg_hide-output":true},"cell_type":"code","source":"display_drawing(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5e677bedfd6789a9eebbb7145f3eb53c146e7beb"},"cell_type":"code","source":"test_labels=[[labels[i] for i in \\\n              test_predictions[k].argsort()[-3:][::-1]] \\\n             for k in range(len(test_predictions))]\ntest_labels=[ \" \".join(test_labels[i]) \n             for i in range(len(test_labels))]\nsubmission=pd.DataFrame({\"key_id\":test_data.index,\n                         \"word\":test_labels})\nsubmission.to_csv('submission.csv',index=False)\nsubmission.head(10).style\\\n.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}