{"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"d285dd77-0fbf-82ed-c6d2-688ca8e41e98"},"source":""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"60f21533-cc69-995e-4ff9-340b358a1901"},"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":"36cd7420-7f5c-e88d-9b5a-a2372e8dfecd"},"outputs":[],"source":"trainLabels = pd.read_csv(\"../input/trainLabels.csv\")\ntrainLabels.head()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"11fef0cd-62f4-2932-ac12-d436a8652f90"},"outputs":[],"source":"import os\n\nlisting = os.listdir(\"../input\") \nlisting.remove(\"trainLabels.csv\")\nnp.size(listing)\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"561e635f-4fbc-ce2d-8188-3b7d84ca7b8e"},"outputs":[],"source":"from PIL import Image\n\n# input image dimensions\nimg_rows, img_cols = 200, 200\n\nimmatrix = []\nimlabel = []\n\nfor file in listing:\n    base = os.path.basename(\"../input/\" + file)\n    fileName = os.path.splitext(base)[0]\n    imlabel.append(trainLabels.loc[trainLabels.image==fileName, 'level'].values[0])\n    im = Image.open(\"../input/\" + file)   \n    img = im.resize((img_rows,img_cols))\n    gray = img.convert('L')\n    immatrix.append(np.array(gray).flatten())"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ef35420b-54a1-9e00-ff35-96ce2eaa31db"},"outputs":[],"source":"immatrix = np.asarray(immatrix)\nimlabel = np.asarray(imlabel)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"6a03bbb9-1a55-fca7-fa87-f4ad81bb1c44"},"outputs":[],"source":"from sklearn.utils import shuffle\n\ndata,Label = shuffle(immatrix,imlabel, random_state=2)\ntrain_data = [data,Label]\ntype(train_data)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"773958d5-db76-7c5b-e95b-d1195feb43da"},"outputs":[],"source":"import matplotlib.pyplot as plt\nimport matplotlib\n\nimg=immatrix[204].reshape(img_rows,img_cols)\nplt.imshow(img)\nplt.imshow(img,cmap='gray')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"43b1fb95-250d-c686-e7f4-0cd9c31aca26"},"outputs":[],"source":"#batch_size to train\nbatch_size = 32\n# number of output classes\nnb_classes = 5\n# number of epochs to train\nnb_epoch = 3"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"89aff692-cfbb-1f57-5b2f-a03427aa48ed"},"outputs":[],"source":"# number of convolutional filters to use\nnb_filters = 32\n# size of pooling area for max pooling\nnb_pool = 2\n# convolution kernel size\nnb_conv = 3"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"252b3993-de53-ef8e-9bfa-4e2abc3d00ec"},"outputs":[],"source":"(X, y) = (train_data[0],train_data[1])"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"e4d8e68d-f977-b505-c9c4-5d3a259d0036"},"outputs":[],"source":"print(train_data[0])\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"08f27159-92e3-a198-f411-fcbd767773ed"},"outputs":[],"source":"print(train_data[1])"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"b19598af-8242-75b8-a673-76288e1d2e34"},"outputs":[],"source":"print('train_data', train_data[0].shape)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"9a0843c5-6962-5c34-f9ee-b12319d90eed"},"outputs":[],"source":"print(train_data[100, 0])"}],"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}