{"cells":[{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import pandas as pd\n\ntrainLabels = pd.read_csv(\"../input/trainLabels.csv\")\ntrainLabels.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"803eb8881bbc0b7211523853964714bdced4da78"},"cell_type":"code","source":"from PIL import Image\nfrom keras.preprocessing import image\nimport os\nimport numpy as np\n\n# resize the image to (256, 256)\nimg_rows, img_cols = 256, 256\n\nlisting = os.listdir(\"../input\") \nlisting.remove(\"trainLabels.csv\")\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 = np.array(im.resize((img_rows,img_cols)))\n    \n    # convert to green channel only\n    img[:,:,[0,2]] = 0\n    immatrix.append(img)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":false,"trusted":true,"_uuid":"bc0a0bfdc775a33e93fce40346d82db2a056d7ec"},"cell_type":"code","source":"im = Image.fromarray(immatrix[1],'RGB')\nprint(\"level:\",imlabel[1])\nim","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"42bc0cbf4319d3d94e1f5449f52de04fc6073521"},"cell_type":"code","source":"import random\n\n# define transformation methods\ndef horizontal_flip(image_array):\n    return image_array[:, ::-1]\n\ndef vertical_flip(image_array):\n    return image_array[::-1,:]\n\ndef random_transform(image_array):\n    if random.random() < 0.5:\n        return vertical_flip(image_array)\n    else:\n        return horizontal_flip(image_array)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":false,"trusted":true,"_uuid":"d9937da7d4e8adc07c9eb02274bb0f5cec6a4964"},"cell_type":"code","source":"im = Image.fromarray(vertical_flip(immatrix[1]),'RGB')\nim","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a6fb15532a08d21e42038e698a0e6a7a3fa96e4d"},"cell_type":"code","source":"length = len(immatrix)\nfor i in range(length):\n    if random.random() < 0.1:\n        immatrix.append(random_transform(immatrix[i]))\n        imlabel.append(imlabel[i])\n        \nprint(\"Size of image array before augmentation: \", length)\nprint(\"Size fo image array after augmentation: \", len(immatrix))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"69ad4ce6aaa29274104fed5cfc96e0d1038bf807"},"cell_type":"code","source":"from sklearn.utils import shuffle\n\ndata,label = shuffle(immatrix, imlabel, random_state=42)\ntrain_data = [data,label]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e254e94ebcedb988509ed14ed931df0502891ec2"},"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.hist(label)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"115fd868b0bbc247af96ae26004d107b7c452b41"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train, x_test, y_train, y_test = train_test_split(train_data[0], train_data[1], test_size = 0.1, random_state = 42)\n\nprint(np.array(x_train).shape)\nprint(np.array(y_train).shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6d8bcb794bfbe9d506f0b88a002bf7f9649d1bfc"},"cell_type":"code","source":"from keras.utils import np_utils\n\ny_train = np_utils.to_categorical(np.array(y_train), 5)\ny_test = np_utils.to_categorical(np.array(y_test), 5)\n\nx_train = np.array(x_train).astype(\"float32\")/255.\nx_test = np.array(x_test).astype(\"float32\")/255.\n\nprint(np.array(y_train).shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c98367b6448554a929992cdf58dcd2c0f24bbd9d"},"cell_type":"code","source":"import keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization\n\nmodel = Sequential()\n\nmodel.add(Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=x_train[0].shape))\nmodel.add(Conv2D(32, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D())\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64, (3, 3), padding='same', activation='relu'))\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D())\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64, (3, 3), padding='same', activation='relu'))\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D())\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(activation='softmax', units=5))\n\nmodel.compile(loss='categorical_crossentropy', optimizer = keras.optimizers.SGD(lr=0.0001, momentum=0.9), metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"12a969e9bd6b589d55cce29cf3b3f33c18002b81"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c69af0b5ae2ef1f1412783872cb5483b390e6fac"},"cell_type":"code","source":"model.fit(x_train, y_train, batch_size = 64, epochs=10, shuffle=True, verbose=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"71baa2c3f66a204c4c39870ea01f8f43c56a2a91"},"cell_type":"code","source":"predictions = model.predict(x_test)\npredictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f28170f51950ba901454524daae772003b8aaf7"},"cell_type":"code","source":"score = model.evaluate(x_test, y_test, verbose=0)\nprint(score)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d304380df8352b3d4602b91165a9751c87cc9e97"},"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}