{"cells":[{"metadata":{"_uuid":"325ae8542b1308d28be28abcd54593d7e9b50088"},"cell_type":"markdown","source":"## Loading libraries"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom tensorflow.keras.preprocessing.image import img_to_array\nfrom tensorflow.keras.preprocessing.image import load_img\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, Flatten, MaxPooling2D, Dropout, BatchNormalization\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom sklearn.model_selection import train_test_split\n\nimage_size = 100","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4f98c8891ba16c372d67ccb0176e8d5470f5a0e8"},"cell_type":"markdown","source":"## Loading data"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/labels.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"24dee6d629deccb097a0ccd48bc057d141ee462b"},"cell_type":"code","source":"df.describe()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"63d8fcd74319c12e23e91482a07c0cdec8e1b2ae"},"cell_type":"markdown","source":"## Checking for NaN values"},{"metadata":{"trusted":true,"_uuid":"9725b9329df13727f6859db6bd89a7be14b5051b"},"cell_type":"code","source":"df[df.isnull()]['breed'].count()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7c2429b4e75279e115fc624bf43ecc775a34c58e"},"cell_type":"markdown","source":"## Plotting the data"},{"metadata":{"trusted":true,"_uuid":"4b36987f0f23e6eda390a40e09eac7dc6228eda8"},"cell_type":"code","source":"df['breed'].value_counts().sort_values(axis=0).plot(kind='barh', \n                                                    figsize=(10, 30), \n                                                    title='Dog breeds')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"793db888e57a66d87ead44d04253e38f8675714d"},"cell_type":"code","source":"print('Minimum count eskimo_dog %s' % df[df['breed']=='eskimo_dog']['breed'].count())\nprint('Maximum count scottish_deerhound %s' % df[df['breed']=='scottish_deerhound']['breed'].count())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f2cb612d734f805fc2d200fad3d1478aee64f37f"},"cell_type":"markdown","source":"## Preparing label data"},{"metadata":{"trusted":true,"_uuid":"f71aafb0c0c9e2de06bf184bebc0478623c97da4","scrolled":true},"cell_type":"code","source":"labels_df = pd.get_dummies(df['breed'])\nlabels = labels_df.values\n\nprint(labels.shape)\nlabels[0]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3a586ea2726ee8c85874fb50ee73a9d947da11f8"},"cell_type":"markdown","source":"## Preparing images\n\nBecause there are too few images I am duplicating original images and flipping them."},{"metadata":{"trusted":true,"_uuid":"75bd15dc69f0481016bb2de6f86f1a9ee74f3708"},"cell_type":"code","source":"data_original = np.array([img_to_array(load_img('../input/train/%s.jpg'%img, target_size=(image_size, image_size), color_mode='grayscale')) for img in df['id'].values.tolist()]).astype(np.int16)\ndata_flipped = np.array(list(map(np.fliplr, data_original)))\n\nprint(data_original.shape, data_flipped.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2ade240c3a9d982e2b3698d058c26a3642cdc1e0"},"cell_type":"code","source":"plt.imshow(data_original[0].reshape(image_size, image_size), cmap='gray')\nplt.show()\nplt.imshow(data_flipped[0].reshape(image_size, image_size), cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38d38e43474bfe94641a5ce30a90fcc720b5b715"},"cell_type":"code","source":"data = np.concatenate((data_original, data_flipped),axis=0)\ndata = np.true_divide(data, 255, dtype=np.float64)\n\nlabels = np.concatenate((labels, labels), axis=0)\n\nprint(data.shape, labels.shape)\nplt.imshow(data[0].reshape(image_size, image_size), cmap='gray')\nplt.show()\nplt.imshow(data[10222].reshape(image_size, image_size), cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0a7c58992b7e9cdf35238f3b717550b271ed9481"},"cell_type":"markdown","source":"## Performing train/test split"},{"metadata":{"trusted":true,"_uuid":"858c10e6a939e4d628950bbabe0545fb9c288a69"},"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.3)\nprint(x_train.shape, y_test.shape)\n\ndel data\ndel labels\ndel data_original\ndel data_flipped","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2a7c5c064e7633ecfbf54b1da3ee4c764bd1a5e9"},"cell_type":"markdown","source":"## Building the model"},{"metadata":{"trusted":true,"_uuid":"a53050ef28b2a604c4eab29f6e7a298eb06e2d15"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, (3, 3), \n                 padding='same', \n                 input_shape=(image_size, image_size, 1), \n                 activation='relu'))\nmodel.add(Conv2D(32, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.1))\n\nmodel.add(Conv2D(64, (3, 3), padding='same', activation='relu'))\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.1))\n\nmodel.add(Conv2D(128, (3, 3), padding='same', activation='relu'))\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.1))\n\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(120, activation='softmax'))\n\nmodel.compile(loss='categorical_crossentropy', \n              optimizer=optimizers.Adam(), \n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e7a0e271474cbec5ff1e341fa14574f05d16afd5"},"cell_type":"markdown","source":"## Training model"},{"metadata":{"trusted":true,"_uuid":"7ded625270615c6f6d3f5b4b95ee406208c8abbb","scrolled":true},"cell_type":"code","source":"model_hist = model.fit(x_train, y_train,\n                       epochs=5,\n                       batch_size=16,\n                       validation_data=(x_test, y_test))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7efa0a26e1b080ecda43fb2169b683e5756bc9b3"},"cell_type":"markdown","source":"## Plotting training statistics"},{"metadata":{"trusted":true,"_uuid":"cb3e05bd6d78a175b3ce0bd1174cdb648d7ca536"},"cell_type":"code","source":"plt.plot(model_hist.history['acc'])\nplt.plot(model_hist.history['val_acc'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"260ef65aedf74821a6e8e2759f9b911f37edde24"},"cell_type":"code","source":"plt.plot(model_hist.history['loss'])\nplt.plot(model_hist.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f08047aa4fc6450ab2fe65f3080b9c617e9592d1"},"cell_type":"markdown","source":"## Preparing data for submission"},{"metadata":{"trusted":true,"_uuid":"d1803a18cf1b1b26b0949baf3c80fb0d6d1fbe17"},"cell_type":"code","source":"sample_sub_df = pd.read_csv('../input/sample_submission.csv')\nsample_sub_data = np.array([img_to_array(load_img('../input/test/%s.jpg'%img, target_size=(image_size, image_size), color_mode='grayscale')) for img in sample_sub_df['id'].values.tolist()]).astype(np.int16)\nsample_sub_data = np.true_divide(sample_sub_data, 255, dtype=np.float64)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bfb243fa915398ea0b55d64b98fbd5ac0b10cfcc"},"cell_type":"code","source":"print(sample_sub_data.shape)\nplt.imshow(sample_sub_data[0].reshape(image_size, image_size), cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ebe10fb7bd0b7c322b41373661306b638fcb8eff"},"cell_type":"code","source":"predictions = model.predict(sample_sub_data, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58bab6dc1ecdf00d4fe8ad4ce4d5baa2cd1fbdaf"},"cell_type":"code","source":"submission_df = pd.DataFrame(predictions)\ncolumns_names = labels_df.columns.values\nsubmission_df.columns = columns_names\nsubmission_df.insert(0, 'id', sample_sub_df['id'])\n\nsubmission_df.to_csv('output.csv', index = False)\n\nsubmission_df.head()","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}