{"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 all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        \nimport tensorflow as tf\nimport tensorflow.keras as keras\nimport numpy as np\nprint('TensorFlow version: {}'.format(tf.__version__))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"root_model = keras.models.load_model('../input/grapheme-root-model/grapheme_root_model.h5')\nvowel_model = keras.models.load_model('../input/vowel-diacritic-model/vowel_model.h5')\nconsonant_model = keras.models.load_model('../input/consonant-diacritic-model/consonant_model.h5')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"","_uuid":"","trusted":true},"cell_type":"code","source":"import pandas as pd\nclass_map = pd.read_csv(\"../input/bengaliai-cv19/class_map.csv\")\nclass_map_corrected = pd.read_csv(\"../input/bengaliai-cv19/class_map_corrected.csv\")\nsample_submission = pd.read_csv(\"../input/bengaliai-cv19/sample_submission.csv\")\ntest = pd.read_csv(\"../input/bengaliai-cv19/test.csv\")\ntrain = pd.read_csv(\"../input/bengaliai-cv19/train.csv\")\ntrain_multi_diacritics = pd.read_csv(\"../input/bengaliai-cv19/train_multi_diacritics.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom skimage.transform import resize\nfrom tqdm import tqdm\n\nheigth = 137;\nwidth = 236;\n\ndef get_bounding_box(image):\n    a,b = image.shape\n    \n    down = 0\n    while (np.any(image[down,:] < 200) == False) and (down < a):\n        down += 1\n\n    up = a-1\n    while (np.any(image[up,:] < 200) == False) and (up >= 0):\n        up -= 1\n  \n    left = 0\n    while (np.any(image[:,left] < 200) == False) and (left < b):\n        left += 1\n\n    right = b-1\n    while (np.any(image[:,right] < 200) == False) and (right >= 0):\n        right -= 1\n\n    # in order to get the index of the last row/column that was actually zero\n    down = max(down-1,0)\n    up = min(up+1,a-1)\n    left = max(left- 1,0)\n    right = min(right+1,b-1)\n\n    return (down,up,left,right)  \n\ndef resize_bounding_box(graph_image):\n    a,b,c,d = get_bounding_box(graph_image)\n    #plt.figure()\n    #plt.imshow(graph_image)\n    #plt.show()\n    #plt.figure()\n    #plt.imshow(graph_image[a:b+1,c:d+1])\n    #plt.show()\n\n    new_image = resize(graph_image[a:b+1,c:d+1],(50,100))\n    #plt.figure()\n    #plt.imshow(new_image)\n    #plt.show()\n\n    return new_image\n\ndef resize_everything(data_index):\n\n    read = pd.read_parquet(f\"../input/bengaliai-cv19/test_image_data_{data_index}.parquet\")\n    train_read = read.drop(['image_id'], axis=1, inplace=False)\n    img_labels = read['image_id'].values\n    full_array = train_read.values.reshape((-1,heigth,width))\n    \n    num_images,_,_ = full_array.shape\n    X_full_resized = np.zeros((num_images,50,100))\n    \n    for i in tqdm(range(num_images)):\n        X_full_resized[i,:,:] = resize_bounding_box(full_array[i,:,:])\n\n    return (X_full_resized,img_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def test_model(model, X_test, image_ids, task = '_grapheme_root'):\n    y_test = np.argmax(model.predict(X_test),axis = 1)\n    new_image_ids = []\n    for i in range(image_ids.shape[0]):\n        new_image_ids.append(image_ids[i] + task)\n        \n    return (new_image_ids, list(y_test))\n\n\ndef test_all_models(root_model, vowel_model, consonant_model, data_index = 0):\n    \n    X_test, image_ids = resize_everything(data_index)\n    #X_full = train_df0.values.reshape((-1,heigth,width,1))\n\n    num_images = X_test.shape[0]\n    X_test = X_test.reshape((num_images,50,100,1))\n    root_img_ids, y_root_test = test_model(root_model, X_test, image_ids, task = '_grapheme_root')\n    vowel_img_ids, y_vowel_test = test_model(vowel_model, X_test, image_ids, task = '_vowel_diacritic')\n    consonant_img_ids, y_consonant_test = test_model(consonant_model, X_test, image_ids, task = '_consonant_diacritic')\n    return (root_img_ids + vowel_img_ids + consonant_img_ids, y_root_test + y_vowel_test + y_consonant_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_ids = []\nsub_targets = []\nfor i in range(4):\n    a,b = test_all_models(root_model, vowel_model, consonant_model, data_index = i)\n    sub_ids = sub_ids + a\n    sub_targets = sub_targets + b\n\npath_save = 'submission.csv'\nsub_data = pd.DataFrame(data={'row_id':sub_ids, 'target':sub_targets})\nif not os.path.exists(path_save):\n    os.mknod(path_save)\nsub_data.to_csv(path_save, index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"our_submission = pd.read_csv(\"submission.csv\")\nour_submission.head(40)","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}