{"cells":[{"metadata":{"trusted":true,"_uuid":"4a4872c594ca957f7e2f818e286c18ed5b53cb7c"},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom glob import glob\nimport cv2\n#import tensorflow as tf\n#from tensorflow.keras.metrics import top_k_categorical_accuracy\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\nfrom keras.applications import MobileNet\nfrom keras.losses import sparse_categorical_crossentropy\nimport os\nprint(os.listdir(\"../input\"))\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9a05b34741ed91747f5b9d5787dd190aaba6ffa1"},"cell_type":"code","source":"files = glob('../input/train_simplified/*.csv')[:10]\ncol_name = ['countrycode', 'drawing', 'key_id', 'recognized', 'timestamp', 'word']\ndraw_list = []\nfor f in files:\n    df = pd.read_csv(f,nrows=500)\n    df = df[df.recognized==True]\n    draw_list.append(df)\ndrawing_df = pd.DataFrame(np.concatenate(draw_list),columns=col_name)\n\ndrawing_df = drawing_df[[\"word\",\"drawing\"]]\ndel df\ndel draw_list\ndrawing_df\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4fa28f2a4f8224f5451602ac236699a8d70fda54"},"cell_type":"code","source":"def strokes_to_img(strokes):\n    strokes = eval(strokes)\n    fig, ax = plt.subplots()\n    for x, y in strokes:\n        ax.plot(x,y,linewidth=12.)\n    ax.axis('off')\n    fig.canvas.draw()\n    X = np.array(fig.canvas.renderer._renderer)\n    plt.close(fig)\n    return (cv2.resize(X, (96, 96)) / 255.)[::-1]\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f0ef5c131ea3f33b1bfbf78c53e0cb76b4c1775"},"cell_type":"code","source":"class_files = os.listdir(\"../input/train_simplified/\")\nclasses_to_idx = {x.split('.')[0]:i for i, x in enumerate(class_files)}\nidx_to_classes = {i:x.split('.')[0].replace(\" \",\"_\") for i, x in enumerate(class_files)}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76741287a2a2484d999c91c827cb3302cdfa3984"},"cell_type":"code","source":"n_samples = drawing_df.shape[0]\nbatch_size = 10\n\npick_order = np.arange(n_samples)\npick_per_epoch = n_samples // batch_size\n\ndef train_gen():\n    while True:  # Infinity loop\n        np.random.shuffle(pick_order)\n        for i in range(pick_per_epoch):\n            c_pick = pick_order[i*batch_size: (i+1)*batch_size]\n            dfs = drawing_df.iloc[c_pick]\n            out_imgs = list(map(strokes_to_img, dfs[\"drawing\"]))\n            X = np.array(out_imgs)[:, :, :, :3].astype(np.float32)\n            y = np.array([classes_to_idx[x] for x in dfs[\"word\"]])\n            yield X, y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"890793c44bdb5c65f605d295a128afca07a5fd9f"},"cell_type":"code","source":"train_datagen = train_gen()\nx,y = next(train_datagen)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f2699b86225dfb08ab320b01a06f187c3dc0db03"},"cell_type":"code","source":"model = MobileNet(input_shape=(96, 96, 3), weights=None, classes=len(classes_to_idx))\nmodel.summary()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5b83c50b4e515fd82aa37f730e2f3ef633c4d79a"},"cell_type":"code","source":"model.compile(optimizer=\"adam\", loss=sparse_categorical_crossentropy,metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f229cdcbbe4307c8a3fe4eaccc9858c0f33e2192"},"cell_type":"code","source":"model.fit_generator(train_datagen, steps_per_epoch=5000, epochs=1, verbose=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"573d61517abc28edfe1f50bd912a7c86c289e1b2"},"cell_type":"code","source":"test = pd.read_csv(\"../input/test_simplified.csv\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0031a78753597d9ecaa4ee3ca7e39262329d59da"},"cell_type":"code","source":"test_samples = test.shape[0]\npick_order = np.arrange(test_samples)\npick_per_epoch = test_samples // batch_size\nall_preds = []\n\nfor i in trange(pick_per_epoch):\n        c_pick = pick_order[i*batch_size: (i+1)*batch_size]\n        dfs = test.iloc[c_pick]\n        out_imgs = list(map(strokes_to_img, dfs[\"drawing\"]))\n        X = np.array(out_imgs)[:, :, :, :3].astype(np.float32)\n        preds = model.predict(X)\n        for x in preds:\n            all_preds.append(idx_to_class[np.argmax(x)])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"04d73c4f5751c708ef9d1592a99f35ed4add9090"},"cell_type":"code","source":"sdf = pd.DataFrame({\"key_id\": test[\"key_id\"], \"word\": all_preds + ([\"\"] * (test.shape[0] - len(all_preds)))})\nsdf.to_csv(\"first_submit.csv\", index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}