{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Input data files are available in the read-only \"../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\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-29T03:52:25.052581Z","iopub.execute_input":"2021-06-29T03:52:25.053049Z","iopub.status.idle":"2021-06-29T03:52:25.068817Z","shell.execute_reply.started":"2021-06-29T03:52:25.052928Z","shell.execute_reply":"2021-06-29T03:52:25.067173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Import library","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np \nimport pandas as pd\nimport os\nfrom sklearn.model_selection import train_test_split\nfrom keras.models import Sequential\nfrom keras.models import load_model\nfrom keras.layers import Dense, Flatten, Dropout\nfrom keras.layers import Conv2D, MaxPooling2D, BatchNormalization\nfrom keras.utils import np_utils\nfrom sklearn.preprocessing import LabelEncoder, LabelBinarizer\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\nfrom keras.preprocessing.image import img_to_array, load_img\nfrom keras.preprocessing.image import ImageDataGenerator \nfrom zipfile import ZipFile","metadata":{"execution":{"iopub.status.busy":"2021-06-29T03:52:25.071203Z","iopub.execute_input":"2021-06-29T03:52:25.071479Z","iopub.status.idle":"2021-06-29T03:52:30.730572Z","shell.execute_reply.started":"2021-06-29T03:52:25.071452Z","shell.execute_reply":"2021-06-29T03:52:30.729682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\").extractall()\nZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\", \"r\").extractall()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T03:52:30.732639Z","iopub.execute_input":"2021-06-29T03:52:30.73302Z","iopub.status.idle":"2021-06-29T03:52:47.971866Z","shell.execute_reply.started":"2021-06-29T03:52:30.732982Z","shell.execute_reply":"2021-06-29T03:52:47.971011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"./train\"\ntest_path = \"./test1\"","metadata":{"execution":{"iopub.status.busy":"2021-06-29T03:52:47.973405Z","iopub.execute_input":"2021-06-29T03:52:47.973717Z","iopub.status.idle":"2021-06-29T03:52:47.980983Z","shell.execute_reply.started":"2021-06-29T03:52:47.973679Z","shell.execute_reply":"2021-06-29T03:52:47.980213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get images of dogs and cats\nfile_names = os.listdir(train_path)\ncategories = []\nfor path in file_names:\n    animal_type = path.split('.')[0]\n    categories.append(animal_type)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T03:52:47.98224Z","iopub.execute_input":"2021-06-29T03:52:47.982609Z","iopub.status.idle":"2021-06-29T03:52:48.011544Z","shell.execute_reply.started":"2021-06-29T03:52:47.982548Z","shell.execute_reply":"2021-06-29T03:52:48.010875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'Image': file_names, 'Animal': categories})\ndf.sample(5)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T03:52:48.012652Z","iopub.execute_input":"2021-06-29T03:52:48.013199Z","iopub.status.idle":"2021-06-29T03:52:48.045315Z","shell.execute_reply.started":"2021-06-29T03:52:48.013161Z","shell.execute_reply":"2021-06-29T03:52:48.04463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train, df_valid = train_test_split(df, test_size=0.2, random_state=42)\n\ndf_train.reset_index(drop=True, inplace=True)\ndf_valid.reset_index(drop=True, inplace=True)\n\nprint(df_train.shape)\nprint(df_valid.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T03:52:48.046446Z","iopub.execute_input":"2021-06-29T03:52:48.046762Z","iopub.status.idle":"2021-06-29T03:52:48.05592Z","shell.execute_reply.started":"2021-06-29T03:52:48.046729Z","shell.execute_reply":"2021-06-29T03:52:48.055106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Augment training data\ndatagen_train = ImageDataGenerator(rescale=1./255, rotation_range=30,\n                              width_shift_range=0.1, height_shift_range=0.1,\n                              shear_range=0.2, zoom_range=0.2,\n                              horizontal_flip=True, fill_mode='nearest')\n\n# Augment validating data\ndatagen_valid = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T03:52:48.057097Z","iopub.execute_input":"2021-06-29T03:52:48.057447Z","iopub.status.idle":"2021-06-29T03:52:48.06291Z","shell.execute_reply.started":"2021-06-29T03:52:48.057413Z","shell.execute_reply":"2021-06-29T03:52:48.061931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug_train = datagen_train.flow_from_dataframe(df_train, directory=train_path, \n                                             x_col='Image', y_col='Animal',\n                                             target_size=(224, 224), class_mode='binary',\n                                             batch_size=64)\n\naug_valid = datagen_valid.flow_from_dataframe(df_valid, directory=train_path,\n                                             x_col='Image', y_col='Animal',\n                                             target_size=(224, 224), class_mode='binary',\n                                             batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T03:52:48.06585Z","iopub.execute_input":"2021-06-29T03:52:48.066186Z","iopub.status.idle":"2021-06-29T03:52:48.321056Z","shell.execute_reply.started":"2021-06-29T03:52:48.066153Z","shell.execute_reply":"2021-06-29T03:52:48.320195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint = ModelCheckpoint('best_model.h5', monitor='val_accuracy', verbose=1,\n                           save_best_only=True, mode='max')\nes = EarlyStopping(monitor='val_accuracy', mode='max', patience=9, verbose=1)\nrdlr = ReduceLROnPlateau(monitor='val_accuracy', factor=0.1, patience=4, min_lr=0.00001, verbose=1)\n\ncallbacks = [checkpoint, rdlr, es]","metadata":{"execution":{"iopub.status.busy":"2021-06-29T03:52:48.322607Z","iopub.execute_input":"2021-06-29T03:52:48.322949Z","iopub.status.idle":"2021-06-29T03:52:48.328112Z","shell.execute_reply.started":"2021-06-29T03:52:48.322916Z","shell.execute_reply":"2021-06-29T03:52:48.327216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform',\n                padding='same', input_shape=(224, 224, 3)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Dropout(0.2))  \n\nmodel.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Dropout(0.3))\n\nmodel.add(Flatten())\nmodel.add(Dense(1024, activation='relu', kernel_initializer='he_uniform'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.35))\nmodel.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.4))\nmodel.add(Dense(2, activation='relu', kernel_initializer='he_uniform'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1, activation='sigmoid'))\n\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics='accuracy')","metadata":{"execution":{"iopub.status.busy":"2021-06-29T03:52:48.329566Z","iopub.execute_input":"2021-06-29T03:52:48.330023Z","iopub.status.idle":"2021-06-29T03:52:50.66365Z","shell.execute_reply.started":"2021-06-29T03:52:48.329988Z","shell.execute_reply":"2021-06-29T03:52:50.66262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T03:52:50.667811Z","iopub.execute_input":"2021-06-29T03:52:50.66817Z","iopub.status.idle":"2021-06-29T03:52:50.688916Z","shell.execute_reply.started":"2021-06-29T03:52:50.668135Z","shell.execute_reply":"2021-06-29T03:52:50.688034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit(aug_train, validation_data=aug_valid, epochs=50, verbose=1, callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T03:52:50.692969Z","iopub.execute_input":"2021-06-29T03:52:50.693324Z","iopub.status.idle":"2021-06-29T08:19:22.574972Z","shell.execute_reply.started":"2021-06-29T03:52:50.693292Z","shell.execute_reply":"2021-06-29T08:19:22.572737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(18, 4))\n\nplt.subplot(121)\nplt.title('Loss')\nplt.plot(hist.history['loss'], label='training loss')\nplt.plot(hist.history['val_loss'], label='validation loss')\nplt.xlabel('Epoch')\nplt.legend()\n\nplt.subplot(122)\nplt.title('Accuracy')\nplt.plot(hist.history['accuracy'], label='training accuracy')\nplt.plot(hist.history['val_accuracy'], label='validation accuracy')\nplt.xlabel('Epoch')\nplt.legend()\n\nplt.tight_layout()\nplt.savefig('Learning rate.png')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T08:19:22.576765Z","iopub.execute_input":"2021-06-29T08:19:22.577146Z","iopub.status.idle":"2021-06-29T08:19:23.105721Z","shell.execute_reply.started":"2021-06-29T08:19:22.577103Z","shell.execute_reply":"2021-06-29T08:19:23.10456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model = load_model('best_model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-06-29T08:19:23.107012Z","iopub.execute_input":"2021-06-29T08:19:23.107369Z","iopub.status.idle":"2021-06-29T08:19:26.940554Z","shell.execute_reply.started":"2021-06-29T08:19:23.107335Z","shell.execute_reply":"2021-06-29T08:19:26.939668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_names = os.listdir(test_path)\ndf_test = pd.DataFrame({'Image':file_names})\n\ndf_test.sample(5)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T08:19:26.942094Z","iopub.execute_input":"2021-06-29T08:19:26.94247Z","iopub.status.idle":"2021-06-29T08:19:26.994851Z","shell.execute_reply.started":"2021-06-29T08:19:26.942431Z","shell.execute_reply":"2021-06-29T08:19:26.9941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug_test = datagen_valid.flow_from_dataframe(df_test, directory=test_path, x_col='Image',\n                                             y_col=None, class_mode=None, target_size= (224, 224),\n                                             batch_size=64, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T08:19:26.99599Z","iopub.execute_input":"2021-06-29T08:19:26.99632Z","iopub.status.idle":"2021-06-29T08:19:27.165397Z","shell.execute_reply.started":"2021-06-29T08:19:26.996284Z","shell.execute_reply":"2021-06-29T08:19:27.164556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = best_model.predict(aug_test)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T08:19:27.16672Z","iopub.execute_input":"2021-06-29T08:19:27.167086Z","iopub.status.idle":"2021-06-29T08:20:20.429843Z","shell.execute_reply.started":"2021-06-29T08:19:27.167046Z","shell.execute_reply":"2021-06-29T08:20:20.428932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = np.where(pred > 0.5, 'Dog', 'Cat')","metadata":{"execution":{"iopub.status.busy":"2021-06-29T08:20:20.433209Z","iopub.execute_input":"2021-06-29T08:20:20.433531Z","iopub.status.idle":"2021-06-29T08:20:20.439328Z","shell.execute_reply.started":"2021-06-29T08:20:20.433499Z","shell.execute_reply":"2021-06-29T08:20:20.438462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,15))\nfor i in range(9):\n    plt.subplot(3,3,i+1)\n    plt.imshow(aug_test[0][i])\n    plt.xlabel(pred[i])\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T08:20:20.440756Z","iopub.execute_input":"2021-06-29T08:20:20.441099Z","iopub.status.idle":"2021-06-29T08:20:23.446002Z","shell.execute_reply.started":"2021-06-29T08:20:20.441065Z","shell.execute_reply":"2021-06-29T08:20:23.444579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = np.where(pred == 'Cat', 0, 1)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T08:20:23.447105Z","iopub.execute_input":"2021-06-29T08:20:23.447446Z","iopub.status.idle":"2021-06-29T08:20:23.452016Z","shell.execute_reply.started":"2021-06-29T08:20:23.44741Z","shell.execute_reply":"2021-06-29T08:20:23.451254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission = pd.read_csv('../input/dogs-vs-cats/sampleSubmission.csv')\ndf_submission['label'] = label\ndf_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T08:20:23.453293Z","iopub.execute_input":"2021-06-29T08:20:23.453741Z","iopub.status.idle":"2021-06-29T08:20:24.335141Z","shell.execute_reply.started":"2021-06-29T08:20:23.453704Z","shell.execute_reply":"2021-06-29T08:20:24.334299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}