{"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":"# 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\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 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-17T01:44:40.164546Z","iopub.execute_input":"2021-06-17T01:44:40.164909Z","iopub.status.idle":"2021-06-17T01:44:40.176796Z","shell.execute_reply.started":"2021-06-17T01:44:40.164835Z","shell.execute_reply":"2021-06-17T01:44:40.175954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import all the required libraries.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread\nimport seaborn as sns\nimport os\nimport glob as gb\nimport random\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nimport tensorflow.keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, BatchNormalization, MaxPool2D, Flatten, Dense, Dropout\nfrom zipfile import ZipFile\nimport matplotlib.image as mpimg\nimport warnings\n\n\nsns.set()\n%matplotlib inline\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:44:40.178182Z","iopub.execute_input":"2021-06-17T01:44:40.178965Z","iopub.status.idle":"2021-06-17T01:44:45.947486Z","shell.execute_reply.started":"2021-06-17T01:44:40.178929Z","shell.execute_reply":"2021-06-17T01:44:45.946439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prepare data","metadata":{}},{"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-17T01:44:45.952123Z","iopub.execute_input":"2021-06-17T01:44:45.952466Z","iopub.status.idle":"2021-06-17T01:45:03.359311Z","shell.execute_reply.started":"2021-06-17T01:44:45.952432Z","shell.execute_reply":"2021-06-17T01:45:03.35846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"./train\"\ntest_path = \"./test1\"","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:03.360844Z","iopub.execute_input":"2021-06-17T01:45:03.361184Z","iopub.status.idle":"2021-06-17T01:45:03.365083Z","shell.execute_reply.started":"2021-06-17T01:45:03.361146Z","shell.execute_reply":"2021-06-17T01:45:03.364128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Total number of images in train and test datasets\nprint('Total images in Train and Test Data Set:')\nprint(len(os.listdir(train_path)))\nprint(len(os.listdir(test_path)))\n","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:03.36647Z","iopub.execute_input":"2021-06-17T01:45:03.366856Z","iopub.status.idle":"2021-06-17T01:45:03.395393Z","shell.execute_reply.started":"2021-06-17T01:45:03.366819Z","shell.execute_reply":"2021-06-17T01:45:03.394482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(train_path)\ncategories = []\nfor filename in filenames:\n    category = filename.split('.')[0]\n    categories.append(category)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:03.396513Z","iopub.execute_input":"2021-06-17T01:45:03.396869Z","iopub.status.idle":"2021-06-17T01:45:03.423977Z","shell.execute_reply.started":"2021-06-17T01:45:03.396836Z","shell.execute_reply":"2021-06-17T01:45:03.42326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.DataFrame({\n    'Image': filenames,\n    'Category': categories})\ndata.head(10)\n","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:03.426171Z","iopub.execute_input":"2021-06-17T01:45:03.426501Z","iopub.status.idle":"2021-06-17T01:45:03.453664Z","shell.execute_reply.started":"2021-06-17T01:45:03.426468Z","shell.execute_reply":"2021-06-17T01:45:03.452629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['Category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:03.455776Z","iopub.execute_input":"2021-06-17T01:45:03.456344Z","iopub.status.idle":"2021-06-17T01:45:03.610406Z","shell.execute_reply.started":"2021-06-17T01:45:03.456306Z","shell.execute_reply":"2021-06-17T01:45:03.609448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Split Train and Test Set\ndata_train, data_validation = train_test_split(data, test_size=0.1, random_state=18)\n#(training_images, training_labels), (test_images, test_labels) = mnist.load_data()\ndata_train = data_train.reset_index(drop=True)\ndata_validation = data_validation.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:03.611857Z","iopub.execute_input":"2021-06-17T01:45:03.612206Z","iopub.status.idle":"2021-06-17T01:45:03.623367Z","shell.execute_reply.started":"2021-06-17T01:45:03.612169Z","shell.execute_reply":"2021-06-17T01:45:03.622609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train['Category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:03.624487Z","iopub.execute_input":"2021-06-17T01:45:03.625524Z","iopub.status.idle":"2021-06-17T01:45:03.750735Z","shell.execute_reply.started":"2021-06-17T01:45:03.625482Z","shell.execute_reply":"2021-06-17T01:45:03.749788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_validation['Category'].value_counts().plot.bar()\n#print(data_validation.loc[data_validation['Category'] == 'cat'].value_counts())\n#print(data_validation.loc[data_validation['Category'] == 'dog'].value_counts())\n\n","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:03.752014Z","iopub.execute_input":"2021-06-17T01:45:03.752356Z","iopub.status.idle":"2021-06-17T01:45:03.872746Z","shell.execute_reply.started":"2021-06-17T01:45:03.75232Z","shell.execute_reply":"2021-06-17T01:45:03.87198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = random.choice(data['Image'])\nplt.imshow(plt.imread((\"./train/\"+sample)))","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:03.874006Z","iopub.execute_input":"2021-06-17T01:45:03.87435Z","iopub.status.idle":"2021-06-17T01:45:04.081484Z","shell.execute_reply.started":"2021-06-17T01:45:03.874315Z","shell.execute_reply":"2021-06-17T01:45:04.080687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Setup Variables","metadata":{}},{"cell_type":"code","source":"size  = 150\nchannels = 3\nbatch = 128\nepochs = 50","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:04.082584Z","iopub.execute_input":"2021-06-17T01:45:04.083027Z","iopub.status.idle":"2021-06-17T01:45:04.087535Z","shell.execute_reply.started":"2021-06-17T01:45:04.082991Z","shell.execute_reply":"2021-06-17T01:45:04.086782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Augmentation\nLet's make augmentation on training data and only scaling validation and test data.","metadata":{}},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1./255,\n                             zoom_range=0.2,\n                             width_shift_range=.2, height_shift_range=.2,\n                             rotation_range=30,\n                             brightness_range=[0.8, 1.2],\n                             horizontal_flip=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:04.089027Z","iopub.execute_input":"2021-06-17T01:45:04.089716Z","iopub.status.idle":"2021-06-17T01:45:04.097375Z","shell.execute_reply.started":"2021-06-17T01:45:04.089679Z","shell.execute_reply":"2021-06-17T01:45:04.096526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagenValidation = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:04.09875Z","iopub.execute_input":"2021-06-17T01:45:04.099161Z","iopub.status.idle":"2021-06-17T01:45:04.107173Z","shell.execute_reply.started":"2021-06-17T01:45:04.099124Z","shell.execute_reply":"2021-06-17T01:45:04.106158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = datagen.flow_from_dataframe(\n    data_train, \n    directory = train_path, \n    x_col = 'Image',\n    y_col = 'Category',\n    target_size= (size,size),\n    class_mode = 'binary',\n    shuffle = True,\n    batch_size = batch)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:04.109379Z","iopub.execute_input":"2021-06-17T01:45:04.110015Z","iopub.status.idle":"2021-06-17T01:45:04.332404Z","shell.execute_reply.started":"2021-06-17T01:45:04.109977Z","shell.execute_reply":"2021-06-17T01:45:04.331527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_validation = datagenValidation.flow_from_dataframe(\n    data_validation, \n    directory = train_path, \n    x_col = 'Image',\n    y_col = 'Category',\n    target_size= (size,size),\n    class_mode = 'binary',\n    shuffle = True,\n    batch_size = batch)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:04.33368Z","iopub.execute_input":"2021-06-17T01:45:04.334023Z","iopub.status.idle":"2021-06-17T01:45:04.367531Z","shell.execute_reply.started":"2021-06-17T01:45:04.333991Z","shell.execute_reply":"2021-06-17T01:45:04.366711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (12,12))\nfor i in range(0, 15):\n    plt.subplot(5, 3, i+1)\n    for X_batch, Y_batch in X_train:\n        image = X_batch[0] #Y_batch is Category\n        plt.imshow(image)\n        break\n        \nplt.show()        \n","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:04.368734Z","iopub.execute_input":"2021-06-17T01:45:04.36908Z","iopub.status.idle":"2021-06-17T01:45:20.187539Z","shell.execute_reply.started":"2021-06-17T01:45:04.369044Z","shell.execute_reply":"2021-06-17T01:45:20.186558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Setup Callbacks","metadata":{}},{"cell_type":"code","source":"checkpoint_filepath = 'best_model.hdf5'\ncallback_checkpoint = ModelCheckpoint(filepath=checkpoint_filepath, save_weights_only=False, monitor='val_accuracy', mode='max', save_best_only=True)\ncallback_learningrate = ReduceLROnPlateau(monitor='val_accuracy', mode='max', min_delta=0.03, patience=3, factor=.5, min_lr=0.00001, verbose=1)\ncallbacks = [callback_checkpoint, callback_learningrate]","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:20.191149Z","iopub.execute_input":"2021-06-17T01:45:20.19142Z","iopub.status.idle":"2021-06-17T01:45:20.196448Z","shell.execute_reply.started":"2021-06-17T01:45:20.191391Z","shell.execute_reply":"2021-06-17T01:45:20.195293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Design the Neural Network","metadata":{}},{"cell_type":"code","source":"Model = Sequential([Conv2D(filters=32,  kernel_size=(3,3), activation=\"relu\", input_shape=(size,size,channels)),\n                    BatchNormalization(),\n                    MaxPool2D(2,2),\n                    Dropout(0.2),\n                 \n                    Conv2D(filters=64,  kernel_size=(5,5), activation=\"relu\"),\n                    BatchNormalization(),\n                    MaxPool2D(3,3),\n                    Dropout(0.2),\n                \n                    Conv2D(filters=128, kernel_size=(7,7), activation=\"relu\"),\n                    BatchNormalization(),\n                    MaxPool2D(4,4),\n                    Dropout(0.2),\n                 \n                    Flatten(),\n                    \n                    Dense(units=4096, activation=\"relu\"),                 \n                    BatchNormalization(),\n                    Dropout(0.4),\n                    \n                    Dense(units=1024, activation=\"relu\"),\n                    BatchNormalization(),\n                    Dropout(0.3),\n                    \n                                        \n                    Dense(units=2, activation=\"relu\"),\n                    BatchNormalization(),\n                    Dropout(0.5),\n                    \n                    Dense(units=1, activation=\"sigmoid\"),\n])\n\n\nModel.compile(optimizer='adam', loss=\"binary_crossentropy\", metrics=[\"accuracy\"])\n\nModel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:20.198173Z","iopub.execute_input":"2021-06-17T01:45:20.198671Z","iopub.status.idle":"2021-06-17T01:45:22.456271Z","shell.execute_reply.started":"2021-06-17T01:45:20.19863Z","shell.execute_reply":"2021-06-17T01:45:22.455334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Let's train","metadata":{}},{"cell_type":"code","source":"history = Model.fit(X_train, validation_data=X_validation, epochs=epochs, callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T01:45:22.458174Z","iopub.execute_input":"2021-06-17T01:45:22.458528Z","iopub.status.idle":"2021-06-17T04:22:40.007522Z","shell.execute_reply.started":"2021-06-17T01:45:22.458492Z","shell.execute_reply":"2021-06-17T04:22:40.006665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs_range = range(len(acc))\n\nplt.figure(figsize=(15, 15))\nplt.subplot(2, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-17T04:22:40.009736Z","iopub.execute_input":"2021-06-17T04:22:40.010007Z","iopub.status.idle":"2021-06-17T04:22:40.354017Z","shell.execute_reply.started":"2021-06-17T04:22:40.009979Z","shell.execute_reply":"2021-06-17T04:22:40.353262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Let's predict","metadata":{}},{"cell_type":"code","source":"Model = tf.keras.models.load_model('best_model.hdf5')\n","metadata":{"execution":{"iopub.status.busy":"2021-06-17T04:22:40.355276Z","iopub.execute_input":"2021-06-17T04:22:40.355669Z","iopub.status.idle":"2021-06-17T04:22:40.85107Z","shell.execute_reply.started":"2021-06-17T04:22:40.35564Z","shell.execute_reply":"2021-06-17T04:22:40.850213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(test_path)\ndf_test = pd.DataFrame({'Image':filenames})\ndf_test.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T04:22:40.852511Z","iopub.execute_input":"2021-06-17T04:22:40.852872Z","iopub.status.idle":"2021-06-17T04:22:40.872854Z","shell.execute_reply.started":"2021-06-17T04:22:40.852839Z","shell.execute_reply":"2021-06-17T04:22:40.872152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = datagenValidation.flow_from_dataframe(\n    df_test, \n    directory=test_path, \n    x_col='Image',\n    y_col=None,\n    class_mode=None,\n    target_size= (size,size),\n    batch_size=batch,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T04:22:40.874025Z","iopub.execute_input":"2021-06-17T04:22:40.874322Z","iopub.status.idle":"2021-06-17T04:22:40.977812Z","shell.execute_reply.started":"2021-06-17T04:22:40.874295Z","shell.execute_reply":"2021-06-17T04:22:40.977076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.class_indices","metadata":{"execution":{"iopub.status.busy":"2021-06-17T04:22:40.979025Z","iopub.execute_input":"2021-06-17T04:22:40.979329Z","iopub.status.idle":"2021-06-17T04:22:40.983902Z","shell.execute_reply.started":"2021-06-17T04:22:40.979297Z","shell.execute_reply":"2021-06-17T04:22:40.983095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = Model.predict(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2021-06-17T04:22:40.985091Z","iopub.execute_input":"2021-06-17T04:22:40.985414Z","iopub.status.idle":"2021-06-17T04:23:15.3672Z","shell.execute_reply.started":"2021-06-17T04:22:40.985379Z","shell.execute_reply":"2021-06-17T04:23:15.366205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ny_pred = np.where(y_pred > .5, \"Dog\", \"Cat\")\ny_pred\n","metadata":{"execution":{"iopub.status.busy":"2021-06-17T04:23:15.368461Z","iopub.execute_input":"2021-06-17T04:23:15.36875Z","iopub.status.idle":"2021-06-17T04:23:15.375906Z","shell.execute_reply.started":"2021-06-17T04:23:15.368724Z","shell.execute_reply":"2021-06-17T04:23:15.375096Z"},"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(X_test[0][i])\n    plt.xlabel(y_pred[i])\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-17T04:23:15.376993Z","iopub.execute_input":"2021-06-17T04:23:15.377252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = np.where(y_pred==\"Cat\", 0, 1)\nlabel","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Let's submit","metadata":{}},{"cell_type":"code","source":"df_submission = pd.read_csv('../input/dogs-vs-cats/sampleSubmission.csv')\ndf_submission['label'] = label\ndf_submission.head(10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('./submission.csv')\nsubmission.head(10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}