{"cells":[{"metadata":{"_uuid":"356c3059c74e26e77f7f81e6121537aa16f2e3fd"},"cell_type":"markdown","source":"<h1 style=\"color:steelblue; font-family:Ewert; font-size:150%;\" class=\"font-effect-fire-animation\">Code Libraries, Style, & Links</h1>","execution_count":null},{"metadata":{"trusted":true,"_uuid":"7e43dc56517dedb8c560238055543287cf0171e4","_kg_hide-input":true},"cell_type":"code","source":"%%html\n<style>\n@import url('https://fonts.googleapis.com/css?family=Ewert|Roboto&effect=3d|fire-animation');\nspan {font-family:'Roboto'; color:black; text-shadow:4px 4px 4px #aaa;}  \ndiv.output_area pre{font-family:'Roboto'; font-size:120%; color: steelblue;}      \n</style>","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":false},"cell_type":"code","source":"import numpy as np,pandas as pd\nimport keras as ks,pylab as pl\nimport os,ast,h5py,warnings\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,ReduceLROnPlateau\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':'steelblue', \n            'border-color':'white','font-family':'Roboto'}\nfpath='../input/quickdraw-doodle-recognition/train_simplified/'","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"9a448249a51a787fc4b3151413c5da351cf05e6b"},"cell_type":"code","source":"def get_line(x1,y1,x2,y2):\n    steep=abs(y2-y1)>abs(x2-x1)\n    if steep: x1,y1,x2,y2=y1,x1,y2,x2\n    rev=False\n    if x1>x2:\n        x1,x2,y1,y2=x2,x1,y2,y1\n        rev=True\n    dx=x2-x1; dy=abs(y2-y1)\n    error=int(dx/2)\n    xy=[]; y=y1; ystep=None\n    if y1<y2: ystep=1\n    else: ystep=-1\n    for x in range(x1,x2+1):\n        if steep: xy.append([y,x])\n        else: xy.append([x,y])\n        error-=dy\n        if error<0:\n            y+=ystep\n            error+=dx\n    if rev: xy.reverse()\n    return xy\ndef display_drawing():\n    for k in range (5) :  \n        pl.figure(figsize=(10,2))\n        pl.suptitle(files[(S-1)*T+k])\n        for i in range(5):\n            picture=ast.literal_eval(data[labels[(S-1)*T+k]].values[i])\n            for x,y in picture:\n                pl.subplot(1,5,i+1)\n                pl.plot(x,y,'-o',markersize=1,color='slategray')\n                pl.xticks([]); pl.yticks([])\n            pl.gca().invert_yaxis()\n            pl.axis('equal');            \ndef get_image(data,k,I):\n    img=np.zeros((280,280))\n    picture=ast.literal_eval(data.iloc[k])\n    for x,y in picture:\n        for i in range(len(x)):\n            img[y[i]+10][x[i]+10]=1\n            if (i<len(x)-1):\n                x1,y1,x2,y2=x[i],y[i],x[i+1],y[i+1]\n            else:\n                x1,y1,x2,y2=x[i],y[i],x[0],y[0]\n            for [xl,yl] in get_line(x1,y1,x2,y2):\n                img[yl+10][xl+10]=1                \n    return resize(img,(I,I))    ","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"<h1 style=\"color:steelblue; font-family:Ewert; font-size:150%;\" class=\"font-effect-fire-animation\">Data Exploration</h1>","execution_count":null},{"metadata":{"_kg_hide-output":true,"trusted":true,"_uuid":"f98f183962d2ec825603200744ff1a06a9b880d0"},"cell_type":"code","source":"data_alarm_clock=pd.read_csv(fpath+'alarm clock.csv',\n                             index_col='key_id')\ndata_alarm_clock.tail(3).T\\\n.style.set_properties(**style_dict)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true,"_uuid":"5c71faeb3a088ec12c83ba2a3891ef8186dfc39a"},"cell_type":"code","source":"I=64 # image size in pixels\nS=2 # number of the label set {1,...,10}->{1-34,...,307-340}\nT=20 # number of labels in one set \nN=7000 # number of images with the same label in the training set\nfiles=sorted(os.listdir(fpath))\nprint(files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4bea86884691a1425583355ed207671140f0f9c9","_kg_hide-output":false},"cell_type":"code","source":"labels=[el.replace(\" \",\"_\")[:-4] for el in files]\ndata=pd.DataFrame(index=range(N),\n                  columns=labels[(S-1)*T:S*T])\nfor i in range((S-1)*T,S*T):\n    data[labels[i]]=\\\n    pd.read_csv(fpath+files[i],\n                index_col='key_id').drawing.values[:N]\ndata.shape","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":false,"trusted":true,"_uuid":"bd1ac7647ea26e0118298674e23c6adfaef95aa8"},"cell_type":"code","source":"display_drawing()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0c6609326c555674ace18288aa73a12506363f83"},"cell_type":"code","source":"images=[]\nfor label in labels[(S-1)*T:S*T]:\n    images.extend([get_image(data[label],i,I) \n                   for i in range(N)])    \nimages=np.array(images)\ntargets=np.array([[]+N*[k] for k in range((S-1)*T,S*T)],\n                 dtype=np.uint8).reshape(N*T)\ndel data,data_alarm_clock \nimages.shape,targets.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8a61e9e5fd38dafdd637e7f9f65b53823d319837"},"cell_type":"code","source":"#with h5py.File('QuickDrawImages001-020.h5','w') as f:\n#    f.create_dataset('images',data=images)\n#    f.create_dataset('targets',data=targets)\n#    f.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ecc0b009d7a27f849a27437f4de9072c7b7349f3"},"cell_type":"code","source":"images=images.reshape(-1,I,I,1)\nx_train,x_test,y_train,y_test=\\\ntrain_test_split(images,targets,\n                 test_size=.2,random_state=1)\nn=int(len(x_test)/2)\nx_valid,y_valid=x_test[:n],y_test[:n]\nx_test,y_test=x_test[n:],y_test[n:]\ndel images,targets\n[x_train.shape,x_valid.shape,x_test.shape,\n y_train.shape,y_valid.shape,y_test.shape]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"nn=np.random.randint(0,int(.8*T*N),3)\nll=labels[y_train[nn[0]]]+', '+labels[y_train[nn[1]]]+\\\n   ', '+labels[y_train[nn[2]]]\npl.figure(figsize=(10,2))\npl.subplot(1,3,1); pl.imshow(x_train[nn[0]].reshape(I,I))\npl.subplot(1,3,2); pl.imshow(x_train[nn[1]].reshape(I,I))\npl.subplot(1,3,3); pl.imshow(x_train[nn[2]].reshape(I,I))\npl.suptitle('Key Points to Lines: %s'%ll);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"830efe6bb0e5e762663fb36ae9009a41d1b61e7c"},"cell_type":"markdown","source":"<h1 style=\"color:steelblue; font-family:Ewert; font-size:150%;\" class=\"font-effect-fire-animation\">The Model</h1>","execution_count":null},{"metadata":{"trusted":true,"_uuid":"d34305e5c7d152cb910f8f7a23ad90bdc43060f0"},"cell_type":"code","source":"def model():\n    model=Sequential()    \n    model.add(Conv2D(32,(5,5),padding='same',\n                     input_shape=x_train.shape[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', \n                  metrics=['accuracy'])\n    return model\nmodel=model()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true,"_uuid":"033865c140c30e37496a0de9ccd8f204b4a35bf7"},"cell_type":"code","source":"fw='weights.best.model.021-040.hdf5'\ncheckpointer=ModelCheckpoint(filepath=fw,verbose=2,\n                             save_best_only=True)\nlr_reduction=ReduceLROnPlateau(monitor='val_loss',\n                               patience=5,verbose=2,factor=.75)\nhistory=model.fit(x_train,y_train-(S-1)*T,epochs=100,\n                  batch_size=1024,verbose=2,\n                  validation_data=(x_valid,y_valid-(S-1)*T),\n                  callbacks=[checkpointer,lr_reduction])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"243d810c767d3eabb0a0ba33dfbe17152dcd54ff"},"cell_type":"markdown","source":"<h1 style=\"color:steelblue; font-family:Ewert; font-size:150%;\" class=\"font-effect-fire-animation\">Evaluation</h1>","execution_count":null},{"metadata":{"trusted":true,"_uuid":"46a60ec475aacff48431a50c6658c3ae8baffd53"},"cell_type":"code","source":"model.load_weights(fw)\nmodel.evaluate(x_test,y_test-(S-1)*T)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"_kg_hide-input":false,"trusted":true,"_uuid":"24720c820a366d968633b9904d9a5671629fdb8c"},"cell_type":"code","source":"p_test=model.predict(x_test)\nwell_predicted=[]\nfor p in range(len(x_test)):\n    if (np.argmax(p_test[p])==y_test[p]-(S-1)*T):\n        well_predicted.append(labels[(S-1)*T+np.argmax(p_test[p])])\nu=np.unique(well_predicted,return_counts=True)\npd.DataFrame({'labels':u[0],'correct predictions':u[1]})\\\n.sort_values('correct predictions',ascending=False)\\\n.style.set_properties(**style_dict)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e9b511cc27372e6de334d5837c6125b28fca9b55"},"cell_type":"markdown","source":"<h1 style=\"color:steelblue; font-family:Ewert; font-size:150%;\" class=\"font-effect-fire-animation\">The Next Step</h1>\nThe weights for each label set have saved in the special database and will be used for image recognition in the test data.<br/>\nThe next notebook [Quick, Draw! Doodle Recognition 2](https://www.kaggle.com/olgabelitskaya/quick-draw-doodle-recognition-2)","execution_count":null}],"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}