{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"1990382b-69a2-75ec-1396-dfb4ca9b4075"},"outputs":[],"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\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"68771599-c5f6-d0d8-0b43-2d137d0ffbf2"},"outputs":[],"source":"import os\nfrom glob import glob\nTRAIN_DATA = \"../input/train\"\ntype_1_files = glob(os.path.join(TRAIN_DATA, \"Type_1\", \"*.jpg\"))\ntype_2_files = glob(os.path.join(TRAIN_DATA, \"Type_2\", \"*.jpg\"))\ntype_3_files = glob(os.path.join(TRAIN_DATA, \"Type_3\", \"*.jpg\"))\n\ntype_1_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_1\"))+1:-4] for s in type_1_files])\ntype_2_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_2\"))+1:-4] for s in type_2_files])\ntype_3_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_3\"))+1:-4] for s in type_3_files])\n\nprint(len(type_1_files), len(type_2_files), len(type_3_files))\nprint(\"Type 1\", type_1_ids[:10])\nprint(\"Type 2\", type_2_ids[:10])\nprint(\"Type 3\", type_3_ids[:10])"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"81fa09e1-b31e-a8bc-1184-cecdb14ea923"},"outputs":[],"source":"def get_filename(image_id, image_type):\n    \"\"\"\n    Method to get image file path from its id and type   \n    \"\"\"\n    try:\n        [\"Type_1\", \"Type_2\", \"Type_3\"].index(image_type)\n    except:\n        raise Exception(\"Image type {} is not recognized\".format(image_type))\n\n    ext = 'jpg'\n    data_path = os.path.join(TRAIN_DATA, image_type)\n    return os.path.join(data_path, \"{}.{}\".format(image_id, ext))\n\n\nimport cv2\n\ndef get_image_data(image_id, image_type):\n    \"\"\"\n    Method to get image data as np.array specifying image id and type\n    \"\"\"\n    fname = get_filename(image_id, image_type)\n    img = cv2.imread(fname)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    return img"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"161fa63a-188c-5f6b-5d72-3274c165d978"},"outputs":[],"source":"import matplotlib.pyplot as plt\n%matplotlib inline\ndef plt_st(l1=15,l2=15):\n    plt.figure(figsize=(l1,l2))"},{"cell_type":"markdown","metadata":{"_cell_guid":"49dd6558-c8a0-cac9-5ddc-6e0a52ac4cfb"},"source":"## Display all images of Type_1"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"bf168145-b13b-31eb-6bb0-8cac8eba3599"},"outputs":[],"source":"l = len(type_1_ids) # 250\nnp.floor(25.6) # 25.0\n\ntile_size"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"1dc33e6f-c3e7-73c1-ed21-69159586e7ba"},"outputs":[],"source":"tile_size = (256, 256)\nn = 10 # number of columns\nm = int(np.floor(l / n)) # number of rows\ntest_zeros = np.zeros((2,4,3), dtype=np.uint8)\ntest_zeros"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"bf8c31d8-b052-1393-9cc7-18785256ad37"},"outputs":[],"source":"tile_size = (256, 256)\nn = 15 # number of columns\n\ncomplete_images = []\nfor k, type_ids in enumerate([type_1_ids, type_2_ids, type_3_ids]):\n    m = int(np.floor(len(type_ids) / n)) # number of rows\n    complete_image = np.zeros((m*(tile_size[0]+2), n*(tile_size[1]+2), 3), dtype=np.uint8)\n    train_ids = sorted(type_ids)\n    counter = 0\n    for i in range(m):\n        ys = i*(tile_size[1] + 2)\n        ye = ys + tile_size[1]\n        for j in range(n):\n            xs = j*(tile_size[0] + 2)\n            xe = xs + tile_size[0]\n            image_id = train_ids[counter]; counter+=1\n            img = get_image_data(image_id, 'Type_%i' % (k+1))\n            img = cv2.resize(img, dsize=tile_size)\n            complete_image[ys:ye, xs:xe] = img[:,:,:]\n    complete_images.append(complete_image)\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"154ee0a3-cd67-bc20-5f50-667087bd608c"},"outputs":[],"source":"plt_st()\nplt.title(\"Training dataset of type 1\")\nplt.imshow(complete_image)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ef2341d5-6baf-1570-fd62-ea17d4c629dd"},"outputs":[],"source":""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"570d5ac0-dd16-3fb7-11b7-95655664438e"},"outputs":[],"source":""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"4f781ff7-e3d5-a0ec-4acb-cd7692b32015"},"outputs":[],"source":""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"08e2fc12-a2ec-f33e-703a-fb9a07b1d6d1"},"outputs":[],"source":""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"a562a1b6-a49a-d72c-78b2-52294f3f55f2"},"outputs":[],"source":""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"66a4387a-188f-4f2f-4445-92e1005409ae"},"outputs":[],"source":""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"20d8a7b9-423d-b4f4-702a-512eaa1d4239"},"outputs":[],"source":""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"40ec98eb-1d08-556a-a881-c1d370ef1be8"},"outputs":[],"source":"plt_st(20, 20)\nplt.imshow(complete_images[0])\nplt.title(\"Training dataset of type %i\" % (1))\n"}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.0"}},"nbformat":4,"nbformat_minor":0}