{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"<h1 style=\"color:steelblue; font-family:Ewert; font-size:150%;\" class=\"font-effect-3d\">Code Library, Style, and Links</h1>","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","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},"cell_type":"code","source":"import numpy as np,pandas as pd\nimport os,ast,cv2,h5py,warnings\nimport tensorflow as tf,pylab as pl\nfrom IPython.display import display,HTML\nfrom IPython.core.magic import register_line_magic\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/'\nos.listdir(\"../input\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h1 style=\"color:steelblue; font-family:Ewert; font-size:150%;\" class=\"font-effect-3d\">Data Exploration</h1>","execution_count":null},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"I=96 # image size in pixels\nS=1 # current number of the label set {1,...,34} -> {1-10,...,331-340}\nT=10 # number of labels in one set \nN=10000 # number of images with the same label in the training set\nfiles=sorted(os.listdir(fpath))\nlabels=[el.replace(\" \",\"_\")[:-4] for el in files]\nprint(labels)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"def display_drawing(data,n,S):\n    for k in range(n):  \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(); pl.axis('equal');\ndef get_image(data,lw=7,time_color=True):\n    data=ast.literal_eval(data)\n    image=np.zeros((280,280),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]+10,s[1][i]+10),\n                       (s[0][i+1]+10,s[1][i+1]+10),color,lw) \n    return cv2.resize(image,(I,I))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"nn=np.random.randint(0,T*N,3)\nnn","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h1 style=\"color:steelblue; font-family:Ewert; font-size:150%;\" class=\"font-effect-3d\">Data Compression</h1>","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"@register_line_magic\ndef data_compression(s):\n    S=int(s)\n    data=pd.DataFrame(index=range(N),\n                      columns=labels[(S-1)*T:S*T])\n    for 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]\n    display(data.head(3).T.style.set_properties(**style_dict))\n    display_drawing(data,5,S)\n    images=[]\n    for label in labels[(S-1)*T:S*T]:\n        images.extend([get_image(data[label].iloc[i]) \n                       for i in range(N)])\n    images=np.array(images,dtype=np.uint8)\n    targets=np.array([[]+N*[k] for k in range((S-1)*T,S*T)],\n                     dtype=np.int32).reshape(N*T)\n    nn=np.random.randint(0,T*N,3)\n    ll=labels[targets[nn[0]]]+', '+labels[targets[nn[1]]]+\\\n   ', '+labels[targets[nn[2]]]\n    pl.figure(figsize=(10,2))\n    pl.subplot(1,3,1); pl.imshow(images[nn[0]])\n    pl.subplot(1,3,2); pl.imshow(images[nn[1]])\n    pl.subplot(1,3,3); pl.imshow(images[nn[2]])\n    pl.suptitle('Key Points to Lines: %s'%ll)\n    pl.show()\n    h5f='QuickDrawImages%d.h5'%S\n    with h5py.File(h5f,'w') as f:\n        f.create_dataset('images',data=images)\n        f.create_dataset('targets',data=targets)\n        f.close()\n    del data,images,targets","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"%data_compression 1","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"%data_compression 2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"%data_compression 3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"%data_compression 4","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"%data_compression 5","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}