{"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"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":10200,"databundleVersionId":868375,"sourceType":"competition"},{"sourceId":7371743,"sourceType":"kernelVersion"}],"dockerImageVersionId":12836,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Keras MobileNet Benchmark\n\nIn a previous benchmark we used a simple three layer ConvNet. This time we use a deeper MobileNet architecture on greyscale strokes. \n\nThis kernel has three main components:\n\n* MobileNet\n* Fast and memory efficient Image Generator with temporal colored strokes\n* Full training & submission with Kaggle Kernel\n\nI did some paramer search but it should not be hard to improve the current score.","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a"}},{"cell_type":"markdown","source":"## Setup\nImport the necessary libraries and a few helper functions.","metadata":{"_uuid":"f7f2a9516140a84124bf7bbf538ee4c30860b778"}},{"cell_type":"code","source":"%matplotlib inline\nfrom IPython.core.interactiveshell import InteractiveShell\nInteractiveShell.ast_node_interactivity = \"all\"\nimport os\nimport json\nimport datetime as dt\nimport matplotlib.pyplot as plt\nplt.rcParams['figure.figsize'] = [16, 10]\nplt.rcParams['font.size'] = 14\nimport seaborn as sns\nimport cv2\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Activation\nfrom tensorflow.keras.metrics import categorical_accuracy, top_k_categorical_accuracy, categorical_crossentropy\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.applications import MobileNet\nfrom tensorflow.keras.applications.mobilenet import preprocess_input\nstart = dt.datetime.now()","metadata":{"_uuid":"ce6d2aa7de1fa341144def7d3a5b1ffdea26bc91","_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-11-04T02:01:57.551696Z","iopub.execute_input":"2024-11-04T02:01:57.551934Z","iopub.status.idle":"2024-11-04T02:01:58.555931Z","shell.execute_reply.started":"2024-11-04T02:01:57.55188Z","shell.execute_reply":"2024-11-04T02:01:58.555143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DP_DIR = '../input/shuffle-csvs/'\nINPUT_DIR = '../input/quickdraw-doodle-recognition/'\n\nBASE_SIZE = 256\nNCSVS = 100\nNCATS = 340\nnp.random.seed(seed=1987)\ntf.set_random_seed(seed=1987)\n\ndef f2cat(filename: str) -> str:\n    return filename.split('.')[0]\n\ndef list_all_categories():\n    files = os.listdir(os.path.join(INPUT_DIR, 'train_simplified'))\n    return sorted([f2cat(f) for f in files], key=str.lower)","metadata":{"_kg_hide-input":true,"_uuid":"978b1e827e598c53df3ef09838a6d85591d83052","execution":{"iopub.status.busy":"2024-11-04T02:01:58.55806Z","iopub.execute_input":"2024-11-04T02:01:58.558382Z","iopub.status.idle":"2024-11-04T02:01:58.589629Z","shell.execute_reply.started":"2024-11-04T02:01:58.558324Z","shell.execute_reply":"2024-11-04T02:01:58.588986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def apk(actual, predicted, k=3):\n    \"\"\"\n    Source: https://github.com/benhamner/Metrics/blob/master/Python/ml_metrics/average_precision.py\n    \"\"\"\n    if len(predicted) > k:\n        predicted = predicted[:k]\n    score = 0.0\n    num_hits = 0.0\n    for i, p in enumerate(predicted):\n        if p in actual and p not in predicted[:i]:\n            num_hits += 1.0\n            score += num_hits / (i + 1.0)\n    if not actual:\n        return 0.0\n    return score / min(len(actual), k)\n\ndef mapk(actual, predicted, k=3):\n    \"\"\"\n    Source: https://github.com/benhamner/Metrics/blob/master/Python/ml_metrics/average_precision.py\n    \"\"\"\n    return np.mean([apk(a, p, k) for a, p in zip(actual, predicted)])\n\ndef preds2catids(predictions):\n    return pd.DataFrame(np.argsort(-predictions, axis=1)[:, :3], columns=['a', 'b', 'c'])\n\ndef top_3_accuracy(y_true, y_pred):\n    return top_k_categorical_accuracy(y_true, y_pred, k=3)","metadata":{"_kg_hide-input":true,"_uuid":"b2fcd1a08ae1ae0619be38a113a244eb6515b63b","execution":{"iopub.status.busy":"2024-11-04T02:01:58.592087Z","iopub.execute_input":"2024-11-04T02:01:58.592406Z","iopub.status.idle":"2024-11-04T02:01:58.603925Z","shell.execute_reply.started":"2024-11-04T02:01:58.592348Z","shell.execute_reply":"2024-11-04T02:01:58.603166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## MobileNet\n\nMobileNets are based on a streamlined architecture that uses depthwise separable convolutions to build light weight deep neural networks.\n\n[MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications](https://arxiv.org/pdf/1704.04861.pdf)","metadata":{"_uuid":"264156422a95e4b350886d558d516ae8bd2e25c0"}},{"cell_type":"code","source":"STEPS = 800\nEPOCHS = 16\nsize = 64\nbatchsize = 680","metadata":{"_uuid":"54e5f0c637195b6624e2f3e6db5e7f8990e14eb7","execution":{"iopub.status.busy":"2024-11-04T02:01:58.606282Z","iopub.execute_input":"2024-11-04T02:01:58.606585Z","iopub.status.idle":"2024-11-04T02:01:58.624856Z","shell.execute_reply.started":"2024-11-04T02:01:58.606531Z","shell.execute_reply":"2024-11-04T02:01:58.623917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = MobileNet(input_shape=(size, size, 1), alpha=1., weights=None, classes=NCATS)\nmodel.compile(optimizer=Adam(lr=0.002), loss='categorical_crossentropy',\n              metrics=[categorical_crossentropy, categorical_accuracy, top_3_accuracy])\nprint(model.summary())","metadata":{"_uuid":"0860ec35bee03f0c5cd21202dc7471c2d201cf5f","_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-11-04T02:01:58.625956Z","iopub.execute_input":"2024-11-04T02:01:58.626279Z","iopub.status.idle":"2024-11-04T02:02:01.002141Z","shell.execute_reply.started":"2024-11-04T02:01:58.626219Z","shell.execute_reply":"2024-11-04T02:02:01.00104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training with Image Generator","metadata":{"_uuid":"ab1834ea2757a53d602a3508efffcc34bc190dc7"}},{"cell_type":"code","source":"def draw_cv2(raw_strokes, size=256, lw=6, time_color=True):\n    img = np.zeros((BASE_SIZE, BASE_SIZE), np.uint8)\n    for t, stroke in enumerate(raw_strokes):\n        for i in range(len(stroke[0]) - 1):\n            color = 255 - min(t, 10) * 13 if time_color else 255\n            _ = cv2.line(img, (stroke[0][i], stroke[1][i]),\n                         (stroke[0][i + 1], stroke[1][i + 1]), color, lw)\n    if size != BASE_SIZE:\n        return cv2.resize(img, (size, size))\n    else:\n        return img\n\ndef image_generator_xd(size, batchsize, ks, lw=6, time_color=True):\n    while True:\n        for k in np.random.permutation(ks):\n            filename = os.path.join(DP_DIR, 'train_k{}.csv.gz'.format(k))\n            for df in pd.read_csv(filename, chunksize=batchsize):\n                df['drawing'] = df['drawing'].apply(json.loads)\n                x = np.zeros((len(df), size, size, 1))\n                for i, raw_strokes in enumerate(df.drawing.values):\n                    x[i, :, :, 0] = draw_cv2(raw_strokes, size=size, lw=lw,\n                                             time_color=time_color)\n                x = preprocess_input(x).astype(np.float32)\n                y = keras.utils.to_categorical(df.y, num_classes=NCATS)\n                yield x, y\n\ndef df_to_image_array_xd(df, size, lw=6, time_color=True):\n    df['drawing'] = df['drawing'].apply(json.loads)\n    x = np.zeros((len(df), size, size, 1))\n    for i, raw_strokes in enumerate(df.drawing.values):\n        x[i, :, :, 0] = draw_cv2(raw_strokes, size=size, lw=lw, time_color=time_color)\n    x = preprocess_input(x).astype(np.float32)\n    return x","metadata":{"_uuid":"f6455bf9555b8381b6a4292098a64a0eb7ff54dc","execution":{"iopub.status.busy":"2024-11-04T02:02:01.003395Z","iopub.execute_input":"2024-11-04T02:02:01.003725Z","iopub.status.idle":"2024-11-04T02:02:01.022162Z","shell.execute_reply.started":"2024-11-04T02:02:01.003664Z","shell.execute_reply":"2024-11-04T02:02:01.021319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_df = pd.read_csv(os.path.join(DP_DIR, 'train_k{}.csv.gz'.format(NCSVS - 1)), nrows=34000)\nx_valid = df_to_image_array_xd(valid_df, size)\ny_valid = keras.utils.to_categorical(valid_df.y, num_classes=NCATS)\nprint(x_valid.shape, y_valid.shape)\nprint('Validation array memory {:.2f} GB'.format(x_valid.nbytes / 1024.**3 ))","metadata":{"_uuid":"98ff512e1a1b5e86e86d9eef4127525bedf3b9e1","execution":{"iopub.status.busy":"2024-11-04T02:02:01.023283Z","iopub.execute_input":"2024-11-04T02:02:01.023632Z","iopub.status.idle":"2024-11-04T02:02:09.07791Z","shell.execute_reply.started":"2024-11-04T02:02:01.023564Z","shell.execute_reply":"2024-11-04T02:02:09.077027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = image_generator_xd(size=size, batchsize=batchsize, ks=range(NCSVS - 1))","metadata":{"_uuid":"d80ad7f4d378ea7f30479221d604eeeed559cae4","execution":{"iopub.status.busy":"2024-11-04T02:02:09.07896Z","iopub.execute_input":"2024-11-04T02:02:09.07925Z","iopub.status.idle":"2024-11-04T02:02:09.083227Z","shell.execute_reply.started":"2024-11-04T02:02:09.079192Z","shell.execute_reply":"2024-11-04T02:02:09.082483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x, y = next(train_datagen)\nn = 8\nfig, axs = plt.subplots(nrows=n, ncols=n, sharex=True, sharey=True, figsize=(12, 12))\nfor i in range(n**2):\n    ax = axs[i // n, i % n]\n    (-x[i]+1)/2\n    ax.imshow((-x[i, :, :, 0] + 1)/2, cmap=plt.cm.gray)\n    ax.axis('off')\nplt.tight_layout()\nfig.savefig('gs.png', dpi=300)\nplt.show();","metadata":{"_uuid":"9ce5fb89fbb77777316d6fca7689b6636c0e6021","execution":{"iopub.status.busy":"2024-11-04T02:02:09.084226Z","iopub.execute_input":"2024-11-04T02:02:09.084529Z","iopub.status.idle":"2024-11-04T02:02:15.664464Z","shell.execute_reply.started":"2024-11-04T02:02:09.084464Z","shell.execute_reply":"2024-11-04T02:02:15.663736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%timeit\nx, y = next(train_datagen)","metadata":{"_uuid":"8e7853cd5bcbea1e68b4b93298e7c1a548b7b538","execution":{"iopub.status.busy":"2024-11-04T02:02:15.665352Z","iopub.execute_input":"2024-11-04T02:02:15.665578Z","iopub.status.idle":"2024-11-04T02:02:26.168979Z","shell.execute_reply.started":"2024-11-04T02:02:15.665539Z","shell.execute_reply":"2024-11-04T02:02:26.167969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [\n    ReduceLROnPlateau(monitor='val_top_3_accuracy', factor=0.75, patience=3, min_delta=0.001,\n                          mode='max', min_lr=1e-5, verbose=1),\n    ModelCheckpoint('model.h5', monitor='val_top_3_accuracy', mode='max', save_best_only=True,\n                    save_weights_only=True),\n]\nhists = []\nhist = model.fit_generator(\n    train_datagen, steps_per_epoch=STEPS, epochs=70, verbose=1,\n    validation_data=(x_valid, y_valid),\n    callbacks = callbacks\n)\nhists.append(hist)","metadata":{"_uuid":"da72d70fc1781e80427d45a80c07b3571dda0b36","execution":{"iopub.status.busy":"2024-11-04T02:02:26.172032Z","iopub.execute_input":"2024-11-04T02:02:26.172396Z","iopub.status.idle":"2024-11-04T04:53:24.496453Z","shell.execute_reply.started":"2024-11-04T02:02:26.172233Z","shell.execute_reply":"2024-11-04T04:53:24.495702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist_df = pd.concat([pd.DataFrame(hist.history) for hist in hists], sort=True)\nhist_df.index = np.arange(1, len(hist_df)+1)\nfig, axs = plt.subplots(nrows=2, sharex=True, figsize=(16, 10))\naxs[0].plot(hist_df.val_categorical_accuracy, lw=5, label='Validation Accuracy')\naxs[0].plot(hist_df.categorical_accuracy, lw=5, label='Training Accuracy')\naxs[0].set_ylabel('Accuracy')\naxs[0].set_xlabel('Epoch')\naxs[0].grid()\naxs[0].legend(loc=0)\naxs[1].plot(hist_df.val_categorical_crossentropy, lw=5, label='Validation MLogLoss')\naxs[1].plot(hist_df.categorical_crossentropy, lw=5, label='Training MLogLoss')\naxs[1].set_ylabel('MLogLoss')\naxs[1].set_xlabel('Epoch')\naxs[1].grid()\naxs[1].legend(loc=0)\nfig.savefig('hist.png', dpi=300)\nplt.show();","metadata":{"_uuid":"05767778d356bc63b7cded355159fd4082eee1a5","execution":{"iopub.status.busy":"2024-11-04T04:53:24.497915Z","iopub.execute_input":"2024-11-04T04:53:24.498182Z","iopub.status.idle":"2024-11-04T04:53:27.415841Z","shell.execute_reply.started":"2024-11-04T04:53:24.498132Z","shell.execute_reply":"2024-11-04T04:53:27.415038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_predictions = model.predict(x_valid, batch_size=128, verbose=1)\nmap3 = mapk(valid_df[['y']].values, preds2catids(valid_predictions).values)\nprint('Map3: {:.3f}'.format(map3))","metadata":{"_uuid":"8c1927f22d3c45cba0bdee7d6f4b6c858d82d614","execution":{"iopub.status.busy":"2024-11-04T04:53:27.417034Z","iopub.execute_input":"2024-11-04T04:53:27.417326Z","iopub.status.idle":"2024-11-04T04:53:32.53385Z","shell.execute_reply.started":"2024-11-04T04:53:27.417278Z","shell.execute_reply":"2024-11-04T04:53:32.532879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create Submission","metadata":{"_uuid":"be4577a9ba00611697eea8f241a42c504981e86f"}},{"cell_type":"code","source":"test = pd.read_csv(os.path.join(INPUT_DIR, 'test_simplified.csv'))\ntest.head()\nx_test = df_to_image_array_xd(test, size)\nprint(test.shape, x_test.shape)\nprint('Test array memory {:.2f} GB'.format(x_test.nbytes / 1024.**3 ))","metadata":{"_uuid":"a7d14348150baf753e90cf2719b9f31dd564f6a2","execution":{"iopub.status.busy":"2024-11-04T04:53:32.535205Z","iopub.execute_input":"2024-11-04T04:53:32.535535Z","iopub.status.idle":"2024-11-04T04:54:01.68914Z","shell.execute_reply.started":"2024-11-04T04:53:32.535475Z","shell.execute_reply":"2024-11-04T04:54:01.688278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions = model.predict(x_test, batch_size=128, verbose=1)\n\ntop3 = preds2catids(test_predictions)\ntop3.head()\ntop3.shape\n\ncats = list_all_categories()\nid2cat = {k: cat.replace(' ', '_') for k, cat in enumerate(cats)}\ntop3cats = top3.replace(id2cat)\ntop3cats.head()\ntop3cats.shape","metadata":{"_uuid":"608b02f5c7909ae62becbe5c931b7264171296e8","execution":{"iopub.status.busy":"2024-11-04T04:54:01.690071Z","iopub.execute_input":"2024-11-04T04:54:01.690285Z","iopub.status.idle":"2024-11-04T04:54:20.194611Z","shell.execute_reply.started":"2024-11-04T04:54:01.690248Z","shell.execute_reply":"2024-11-04T04:54:20.193927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['word'] = top3cats['a'] + ' ' + top3cats['b'] + ' ' + top3cats['c']\nsubmission = test[['key_id', 'word']]\nsubmission.to_csv('gs_mn_submission_{}.csv'.format(int(map3 * 10**4)), index=False)\nsubmission.head()\nsubmission.shape","metadata":{"_uuid":"52e0f9c44f2a9a38fd1550ffb9c07fb7ea22b17d","execution":{"iopub.status.busy":"2024-11-04T04:54:20.195645Z","iopub.execute_input":"2024-11-04T04:54:20.195919Z","iopub.status.idle":"2024-11-04T04:54:21.176953Z","shell.execute_reply.started":"2024-11-04T04:54:20.195859Z","shell.execute_reply":"2024-11-04T04:54:21.175969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"end = dt.datetime.now()\nprint('Latest run {}.\\nTotal time {}s'.format(end, (end - start).seconds))","metadata":{"_uuid":"b418f4c06c4e4453aa1b5ab16dde344eb8b735c5","execution":{"iopub.status.busy":"2024-11-04T04:54:21.178179Z","iopub.execute_input":"2024-11-04T04:54:21.178429Z","iopub.status.idle":"2024-11-04T04:54:21.183074Z","shell.execute_reply.started":"2024-11-04T04:54:21.178385Z","shell.execute_reply":"2024-11-04T04:54:21.182294Z"},"trusted":true},"execution_count":null,"outputs":[]}]}