{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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)\n\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\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import os\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport ast\nfrom keras.utils.np_utils import to_categorical\nfrom PIL import Image, ImageDraw\nimport pandas as pd\nfrom keras import layers\nfrom keras.layers import Input, Add, Dense, Activation, BatchNormalization, Conv2D, AveragePooling2D, MaxPooling2D, Flatten, LSTM, Dropout, Flatten\nfrom keras.models import Model, load_model\nfrom keras.initializers import glorot_uniform\nfrom keras.metrics import top_k_categorical_accuracy\nfrom keras.callbacks import ModelCheckpoint, LearningRateScheduler, EarlyStopping, ReduceLROnPlateau\nfrom glob import glob\nfrom ast import literal_eval\nfrom sklearn.preprocessing import LabelEncoder\nfrom keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array, array_to_img\nfrom skimage.util.montage import montage2d\nfrom skimage.io import imread, imshow\n\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"770d8b7e6322a3f9ceea1e2ef437cc5322773724"},"cell_type":"code","source":"SAMPLE_SIZE_PER_CSV = 500\nBATCH_SIZE = 16\nMAX_TRAIN_EPOCHS = 10\nALL_TRAIN_PATHS = glob(os.path.join(\"../input/train_simplified/\",\"*.csv\"))\nCOL_NAMES = ['countrycode', 'drawing', 'key_id', 'recognized', 'timestamp', 'word']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2d365461378ae87f3faeab2f7fb30e4c101d559f"},"cell_type":"code","source":"def get_image_array(drawing_vec):\n    \"\"\"\n    converting vector stroke to img\n    \"\"\"\n    strokes = literal_eval(drawing_vec) \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(\"all\")\n    plt.clf()\n    X = (cv2.resize(X, (246, 246)) / 255.)[::-1]\n    X = X[:, :, 3]\n    X = np.stack((X,)*3, -1)\n    return X","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"80c3e8bcf18c7a9b608f7e64780a12fd5e707732"},"cell_type":"code","source":"def train_sampled_data(all_paths, samples, start_row):\n    \"\"\"\n    reads and samples out from each csv file given in all_paths\n    output: dataframe of sampled rows from each csv\n    \"\"\"\n    all_train_img = pd.DataFrame()\n    for path in all_paths:\n        c_path = pd.read_csv(path, nrows = samples*2, skiprows = start_row, header = None, names = COL_NAMES)\n        c_path = c_path[c_path['recognized']==True]\n        c_path_sampled = c_path.sample(samples)[['drawing','word']]\n        all_train_img = pd.concat([all_train_img,c_path_sampled])\n    return all_train_img.reset_index(drop=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d790a98f2154501a421e4e41d5b88f27e134d4fc"},"cell_type":"code","source":"train_df = train_sampled_data(ALL_TRAIN_PATHS, SAMPLE_SIZE_PER_CSV, 1)\nvalid_df = train_sampled_data(ALL_TRAIN_PATHS, 50, SAMPLE_SIZE_PER_CSV*2+1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d564925e1e671f5d3028de21363990335e8fea8e"},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"35caf9b85c5fbf4c0cd42763b0f5d105f0a92ed3"},"cell_type":"code","source":"print ('total training samples: {}'.format(train_df.shape[0]))\nprint ('total validation samples: {}'.format(valid_df.shape[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1240f0c4b882901706191455f66ebff9355b7103"},"cell_type":"code","source":"word_encoder = LabelEncoder()\nword_encoder.fit(train_df['word'])\nprint('words', len(word_encoder.classes_), '=>', ', '.join([x for x in word_encoder.classes_]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9b7ce45c84c5488a2289bded8cd6bf85f4ee16c5"},"cell_type":"code","source":"len(word_encoder.classes_)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7195fe636a2e23a527e26a586526e48a2148d8ef"},"cell_type":"code","source":"def make_gen_data(df, batch_size=BATCH_SIZE):\n    \"\"\"\n    generates a batch of training_data\n    \"\"\"\n    index_generator = np.arange(df.shape[0])\n    steps_per_epoch = df.shape[0]//batch_size\n    while True:\n        np.random.shuffle(index_generator)\n        for i in range(steps_per_epoch):\n            index_set = index_generator[i*batch_size: (i+1)*batch_size]\n            df_part = df.iloc[index_set]\n            out_imgs = list(map(get_image_array, df_part[\"drawing\"]))\n            #X = np.expand_dims(np.array(out_imgs),-1).astype(np.float32)\n            X = np.array(out_imgs).astype(np.float32)\n            y = to_categorical(word_encoder.transform(df_part[\"word\"].values),num_classes=340).astype(np.int32)\n            yield X, y\n            del X, y, out_imgs, df_part","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"00207b632129f5d0ee19333c4fec4ffdfe944266"},"cell_type":"code","source":"train_set_batches = make_gen_data(train_df)\ntrain_x, train_y = next(train_set_batches)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3338fdab1ddc2885e478eb6f0d4d8b3a8f4d928f","scrolled":true},"cell_type":"code","source":"print ('train x shape:{}'.format(train_x.shape))\nprint ('train y shape:{}'.format(train_y.shape))\nprint('train_x', train_x.dtype, train_x.min(), train_x.max())\nprint('train_y', train_y.dtype, train_y.min(), train_y.max())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aeae5c36c81222faabb4e43e21ffd1fd07f88f68"},"cell_type":"code","source":"valid_set_batches = make_gen_data(valid_df, 500)\nvalid_x, valid_y = next(valid_set_batches)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cff2cf89706c1f432bcf5a2d39910b3f47fa08fb"},"cell_type":"code","source":"print ('valid x shape:{}'.format(valid_x.shape))\nprint ('valid y shape:{}'.format(valid_y.shape))\nprint('valid_x', valid_x.dtype, valid_x.min(), valid_x.max())\nprint('valid_y', valid_y.dtype, valid_y.min(), valid_y.max())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"139795740553e2f544ac7541b383f6bec85bb02e"},"cell_type":"markdown","source":"### Preparing augmented data\n"},{"metadata":{"trusted":true,"_uuid":"dcdd0542715b704dec2af82245d3858e402ed1fa"},"cell_type":"code","source":"data_gen_args = dict(width_shift_range=0.1,\n                      height_shift_range=0.1,\n                      shear_range=0.02,\n                      zoom_range=0.2,\n                      horizontal_flip=True)\n\nimage_datagen = ImageDataGenerator(**data_gen_args)\n\ndef create_aug_data(train_set_batches, seed=1):\n    for img, lbl in train_set_batches:\n        t_x = image_datagen.flow(img, batch_size=img.shape[0], shuffle=False)\n        t_y = lbl\n        yield next(t_x), t_y\n\ncur_gen = create_aug_data(train_set_batches)\nt_x, t_y = next(cur_gen)\nprint('x', t_x.shape, t_x.dtype, t_x.min(), t_x.max())\nprint('y', t_y.shape, t_y.dtype, t_y.min(), t_y.max())\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"46484bb86f1e2b3082c894a4a0ebbbc6ca7f2909"},"cell_type":"code","source":"fig, m_axs = plt.subplots(3,3, figsize = (16, 16))\nrand_idxs = np.random.choice(range(t_x.shape[0]), size = 9)\nfor c_id, c_ax in zip(rand_idxs, m_axs.flatten()):\n    test_arr = t_x[c_id, :, :, 0]\n    c_ax.imshow(test_arr)\n    c_ax.axis('off')\n    c_ax.set_title(word_encoder.classes_[np.argmax(t_y[c_id])])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6eaf4e52f2c6038e5ba992d286bb5600ad1c2cdb"},"cell_type":"code","source":"def doodle(input_shape):\n    input_img = Input(input_shape)\n    conv1 = Conv2D(16, (3, 3), activation='relu', padding='valid')(input_img) \n    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) \n    conv2 = Conv2D(32, (3, 3), activation='relu', padding='valid')(pool1) \n    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) \n    conv3 = Conv2D(64, (3, 3), activation='relu', padding='valid')(pool2) \n    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) \n    conv4 = Conv2D(128, (3, 3), activation='relu', padding='valid')(pool3) \n    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) \n    flat = Flatten()(pool4)\n    drop1 = Dropout(0.3)(flat)\n    dense1 = Dense(512)(drop1)\n    drop2 = Dropout(0.3)(dense1)\n    dense2 = Dense(len(word_encoder.classes_), activation = 'softmax')(drop2)\n    \n    model =  Model(inputs = input_img, outputs = dense2, name = 'Doodle_model')    \n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b4827b59246067d821bd7b173f8efbf9f141a554"},"cell_type":"code","source":"model = doodle(input_shape = t_x.shape[1:])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b3929b44f7d824a4f6187b04cd3acf3ba67dec44"},"cell_type":"code","source":"def top_3_accuracy(y_true, y_pred):\n    return top_k_categorical_accuracy(y_true, y_pred, k=3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2d86104fd24fa2439daec82c226abe1020701031"},"cell_type":"code","source":"model.compile(optimizer = 'adam', loss='categorical_crossentropy', metrics = ['categorical_accuracy', top_3_accuracy])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b3c959d628243374ef99124052bce85f16f8ef5f"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9f63cbdfe588281238c5e22eabc24b58c01e1bea"},"cell_type":"code","source":"weight_path=\"model_weights.best.hdf5\"\n\ncheckpoint = ModelCheckpoint(weight_path, monitor='val_loss', verbose=1, save_best_only=True, mode='min', save_weights_only=True, period=1)\n\nearly = EarlyStopping(monitor=\"val_loss\", mode=\"min\", verbose=2,\n                      patience=10) # patience is number of epochs with no improvement after which training will be stopped\n\ncallbacks_list = [checkpoint, early]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"14fc0782a78415459f0813ff1966e96ba1773bc7"},"cell_type":"code","source":"aug_gen = create_aug_data(make_gen_data(train_df))\n\nloss_history = [model.fit_generator(aug_gen,\n                                 epochs=MAX_TRAIN_EPOCHS,\n                                 steps_per_epoch=20,\n                                 validation_data=(valid_x, valid_y),\n                                 callbacks=callbacks_list,\n                                workers=1 # the generator is not very thread safe\n                                           )]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a2996e55a0435f3db88236d4be0fb1e74bbebc31"},"cell_type":"markdown","source":"## Transfer learning using VGG net"},{"metadata":{"trusted":true,"_uuid":"f15985fb543d893bb381dad3bfe87c66240716fa"},"cell_type":"code","source":"from keras import applications","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"083c7a88b943b282a689a2199ae0aa1d0c17b863"},"cell_type":"code","source":"model = applications.VGG19(weights = \"imagenet\", include_top=False, input_shape = (246, 246, 3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"51a629d03fcba41cf537ab5491b06f379182302c"},"cell_type":"code","source":"#Freezing weights of all layers\nfor layer in model.layers:\n    layer.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd54ef25816bed8a1781960e29fc25ad1ed32988"},"cell_type":"code","source":"#Adding custom Layers \nx = model.output\nx = Flatten()(x)\nx = Dense(1024, activation=\"relu\")(x)\nx = Dense(1024, activation=\"relu\")(x)\npredictions = Dense(340, activation=\"softmax\")(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c34e4a23cc03e46e63db32766223092c4b2c2311"},"cell_type":"code","source":"# creating the final model \nmodel_final = Model(inputs = model.input, outputs = predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9845fd724edcbf316cde7a70f95ccd9cbf2e2e2a"},"cell_type":"code","source":"model_final.compile(optimizer = 'adam', loss='categorical_crossentropy', metrics = ['categorical_accuracy', top_3_accuracy])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"41f5c14db030cab65c4da154e34427d7105f21e6"},"cell_type":"code","source":"early = EarlyStopping(monitor=\"val_loss\", mode=\"min\", verbose=2,\n                      patience=5) # patience is number of epochs with no improvement after which training will be stopped\n\ncallbacks_list = [early]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d174ec533348a713527985ec4c6b1c1220189db","scrolled":false},"cell_type":"code","source":"aug_gen = create_aug_data(make_gen_data(train_df))\n\nloss_history = [model_final.fit_generator(aug_gen,\n                                 epochs=50,\n                                 validation_data=(valid_x, valid_y),\n                                 steps_per_epoch=50,\n                                 callbacks=callbacks_list,\n                                workers=1)]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"219e3fcf3de343836cb889350d4345d2d7e38c37"},"cell_type":"markdown","source":"#### VGG net with increased sample size"},{"metadata":{"trusted":true,"_uuid":"17f3007acb4b46656030909aa80b130d8f9bc234"},"cell_type":"code","source":"SAMPLE_SIZE_PER_CSV = 5000\nBATCH_SIZE = 16\nMAX_TRAIN_EPOCHS = 10\nALL_TRAIN_PATHS = glob(os.path.join(\"../input/train_simplified/\",\"*.csv\"))\nCOL_NAMES = ['countrycode', 'drawing', 'key_id', 'recognized', 'timestamp', 'word']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"888e58c56f77f4649c4bea93d02a6ac23ae9282d"},"cell_type":"code","source":"print ('total training samples: {}'.format(train_df.shape[0]))\nprint ('total validation samples: {}'.format(valid_df.shape[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9f6683b1c9526bd51ff2e541927bf251ff851412"},"cell_type":"code","source":"word_encoder = LabelEncoder()\nword_encoder.fit(train_df['word'])\nprint('words', len(word_encoder.classes_), '=>', ', '.join([x for x in word_encoder.classes_]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6b8d1ac68d35806c69096e420be36ae471dd7120"},"cell_type":"code","source":"def make_gen_data(df, batch_size=BATCH_SIZE):\n    \"\"\"\n    generates a batch of training_data\n    \"\"\"\n    index_generator = np.arange(df.shape[0])\n    steps_per_epoch = df.shape[0]//batch_size\n    while True:\n        np.random.shuffle(index_generator)\n        for i in range(steps_per_epoch):\n            index_set = index_generator[i*batch_size: (i+1)*batch_size]\n            df_part = df.iloc[index_set]\n            out_imgs = list(map(get_image_array, df_part[\"drawing\"]))\n            #X = np.expand_dims(np.array(out_imgs),-1).astype(np.float32)\n            X = np.array(out_imgs).astype(np.float32)\n            y = to_categorical(word_encoder.transform(df_part[\"word\"].values),num_classes=340).astype(np.int32)\n            yield X, y\n            del X, y, out_imgs, df_part","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"be422cc2d246828ba75c9097cc6f6993436c1357"},"cell_type":"code","source":"train_set_batches = make_gen_data(train_df)\ntrain_x, train_y = next(train_set_batches)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e1afb96584eab0063da0a192a65575de037a6378"},"cell_type":"code","source":"print ('train x shape:{}'.format(train_x.shape))\nprint ('train y shape:{}'.format(train_y.shape))\nprint('train_x', train_x.dtype, train_x.min(), train_x.max())\nprint('train_y', train_y.dtype, train_y.min(), train_y.max())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ba9de602ee390e3793b39e7783f7133d1b406cc6"},"cell_type":"code","source":"valid_set_batches = make_gen_data(valid_df, 500)\nvalid_x, valid_y = next(valid_set_batches)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e1582cce9b315b1658f38955c4deabd16cc9dbea"},"cell_type":"code","source":"print ('valid x shape:{}'.format(valid_x.shape))\nprint ('valid y shape:{}'.format(valid_y.shape))\nprint('valid_x', valid_x.dtype, valid_x.min(), valid_x.max())\nprint('valid_y', valid_y.dtype, valid_y.min(), valid_y.max())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"36110210454c4ad8bf393574bb8e60f8fd6f86e7"},"cell_type":"code","source":"data_gen_args = dict(width_shift_range=0.1,\n                      height_shift_range=0.1,\n                      shear_range=0.02,\n                      zoom_range=0.2,\n                      horizontal_flip=True)\n\nimage_datagen = ImageDataGenerator(**data_gen_args)\n\ndef create_aug_data(train_set_batches, seed=1):\n    for img, lbl in train_set_batches:\n        t_x = image_datagen.flow(img, batch_size=img.shape[0], shuffle=False)\n        t_y = lbl\n        yield next(t_x), t_y\n\ncur_gen = create_aug_data(train_set_batches)\nt_x, t_y = next(cur_gen)\nprint('x', t_x.shape, t_x.dtype, t_x.min(), t_x.max())\nprint('y', t_y.shape, t_y.dtype, t_y.min(), t_y.max())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d8e12cab1ed3042ba392060061e96886921de926"},"cell_type":"code","source":"#aug_gen = create_aug_data(make_gen_data(train_df))\n'''\nloss_history = [model_final.fit_generator(aug_gen,\n                                 epochs=10,\n                                 steps_per_epoch=100,\n                                 validation_data=(valid_x, valid_y),\n                                 callbacks=callbacks_list,\n                                workers=1 # the generator is not very thread safe\n                                           )]\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"02f72df6cf9ace14752032a9db4ff5d2c320fb74"},"cell_type":"markdown","source":"## Transfer learning using Resnet"},{"metadata":{"trusted":true,"_uuid":"c8f68dd6044cb22437074f253dbd0eafdfb4e819"},"cell_type":"code","source":"from keras import applications\nmodel = applications.ResNet50(weights = \"imagenet\", include_top=False, input_shape = (246, 246, 3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"84b8c1255e1f17867ca1b1edfa878a0fcdf09b06"},"cell_type":"code","source":"#Freezing weights of all layers\nfor layer in model.layers:\n    layer.trainable = False\n    \n#Adding custom Layers \nx = model.output\nx = Flatten()(x)\nx = Dense(1024, activation=\"relu\")(x)\nx = Dense(1024, activation=\"relu\")(x)\npredictions = Dense(340, activation=\"softmax\")(x)\n\nmodel_final = Model(inputs = model.input, outputs = predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe0c9c7da7677ef6ddbe3992a24f47fed0ad436a"},"cell_type":"code","source":"model_final.compile(optimizer = 'adam', loss='categorical_crossentropy', metrics = ['categorical_accuracy', top_3_accuracy])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"19584bd8e49bde0879473ab2f3b26a41b112597e"},"cell_type":"code","source":"early = EarlyStopping(monitor=\"val_loss\", mode=\"min\", verbose=2,\n                      patience=5) # patience is number of epochs with no improvement after which training will be stopped\n\ncallbacks_list = [early]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c1e4121360fd75137f99116a97fb20a34a471b5b"},"cell_type":"code","source":"aug_gen = create_aug_data(make_gen_data(train_df))\n\nloss_history = [model_final.fit_generator(aug_gen,\n                                 epochs=50,\n                                 validation_data=(valid_x, valid_y),\n                                 steps_per_epoch=50,\n                                 callbacks=callbacks_list,\n                                workers=1)]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"095959cd09053728bc10f6b63c0e5121befe7768"},"cell_type":"markdown","source":"## Make a submission"},{"metadata":{"trusted":true,"_uuid":"ce37f438bfac22843e97bd786cc914811f587e33"},"cell_type":"code","source":"import os\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6632ddac4df5a5f60df492b4c42669acc4a03fdf"},"cell_type":"code","source":"test_data = pd.read_csv(\"../input/test_simplified.csv\")\ntest_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df280ed01155b67bb3172e7908e15eae637f3adb"},"cell_type":"code","source":"#Converting test data vector to images\ndef get_image_array(drawing_vec):\n    \"\"\"\n    converting vector stroke to img\n    \"\"\"\n    strokes = literal_eval(drawing_vec) \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(\"all\")\n    plt.clf()\n    X = (cv2.resize(X, (246, 246)) / 255.)[::-1]\n    X = X[:, :, 3]\n    X = np.stack((X,)*3, -1)\n    return X","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"482697d135d2a3bfc39971cf2c9251dbf0b87d0d"},"cell_type":"code","source":"TEST_BATCH_SIZE = 1\ndef make_test_data(df, batch_size=TEST_BATCH_SIZE):\n    \"\"\"\n    generates a batch of test data\n    \"\"\"\n    index_generator = np.arange(df.shape[0])\n    steps_per_epoch = df.shape[0]//batch_size\n    while True:\n        np.random.shuffle(index_generator)\n        for i in range(steps_per_epoch):\n            index_set = index_generator[i*batch_size: (i+1)*batch_size]\n            df_part = df.iloc[index_set]\n            out_imgs = list(map(get_image_array, df_part[\"drawing\"]))\n            #X = np.expand_dims(np.array(out_imgs),-1).astype(np.float32)\n            X = np.array(out_imgs).astype(np.float32)\n            #y = to_categorical(word_encoder.transform(df_part[\"word\"].values),num_classes=340).astype(np.int32)\n            yield X\n            del X, out_imgs, df_part","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6bd1a32442226e01e1646f52cd792ef1c85d2f32"},"cell_type":"code","source":"test_set_batches = make_test_data(test_data)\ntest_x = next(test_set_batches)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6a69956302a5e55d64064d483daca629b3531356"},"cell_type":"code","source":"print ('test x shape:{}'.format(test_x.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8afe9b7b538dd281d0b27e2d4d98b6e8dac508d"},"cell_type":"code","source":"test_steps = len(test_data) / TEST_BATCH_SIZE\n#preds = model_final.predict_generator(make_test_data(test_data), steps = test_steps)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7c095fb0afce8a1fedca48685c14c246c951d56c"},"cell_type":"code","source":"","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}