{"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":"markdown","source":"# 1- Hello Friends\n\nHello Future Engineers, Nice to meet you!\n\nFirst we import our libraries that will we need.","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\nsns.set()\n%matplotlib inline\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-30T21:41:19.577435Z","iopub.execute_input":"2021-05-30T21:41:19.577807Z","iopub.status.idle":"2021-05-30T21:41:25.143319Z","shell.execute_reply.started":"2021-05-30T21:41:19.577717Z","shell.execute_reply":"2021-05-30T21:41:25.142356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2- Prepare Data\nImport **train** and **test** data as dataframes by extracting images from zip files and add images names in dataframe.","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-05-30T21:41:25.145046Z","iopub.execute_input":"2021-05-30T21:41:25.145371Z","iopub.status.idle":"2021-05-30T21:41:42.634944Z","shell.execute_reply.started":"2021-05-30T21:41:25.145336Z","shell.execute_reply":"2021-05-30T21:41:42.634137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"./train\"\ntest_path = \"./test1\"","metadata":{"execution":{"iopub.status.busy":"2021-05-30T21:41:42.636237Z","iopub.execute_input":"2021-05-30T21:41:42.636584Z","iopub.status.idle":"2021-05-30T21:41:42.641334Z","shell.execute_reply.started":"2021-05-30T21:41:42.636549Z","shell.execute_reply":"2021-05-30T21:41:42.640423Z"},"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    if category == 'dog':\n        categories.append(category)\n    else:\n        categories.append(category)\n\ndf = pd.DataFrame({\n    'Image': filenames,\n    'Category': categories})\ndf.head(-5)","metadata":{"execution":{"iopub.status.busy":"2021-05-30T21:41:42.6431Z","iopub.execute_input":"2021-05-30T21:41:42.643471Z","iopub.status.idle":"2021-05-30T21:41:42.702089Z","shell.execute_reply.started":"2021-05-30T21:41:42.643433Z","shell.execute_reply":"2021-05-30T21:41:42.701054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-05-30T21:41:42.705288Z","iopub.execute_input":"2021-05-30T21:41:42.705636Z","iopub.status.idle":"2021-05-30T21:41:42.861576Z","shell.execute_reply.started":"2021-05-30T21:41:42.7056Z","shell.execute_reply":"2021-05-30T21:41:42.86068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train, df_validation = train_test_split(df, test_size=0.1, random_state=18)\ndf_train = df_train.reset_index(drop=True)\ndf_validation = df_validation.reset_index(drop=True)\ndf_train['Category'].value_counts().plot.bar()\nplt.show()\ndf_validation['Category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-05-30T21:41:42.864391Z","iopub.execute_input":"2021-05-30T21:41:42.864752Z","iopub.status.idle":"2021-05-30T21:41:43.098981Z","shell.execute_reply.started":"2021-05-30T21:41:42.864714Z","shell.execute_reply":"2021-05-30T21:41:43.097979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's check our images.","metadata":{}},{"cell_type":"code","source":"sample = random.choice(df['Image'])\nplt.imshow(plt.imread((\"./train/\"+sample)))","metadata":{"execution":{"iopub.status.busy":"2021-05-30T21:41:43.100504Z","iopub.execute_input":"2021-05-30T21:41:43.100869Z","iopub.status.idle":"2021-05-30T21:41:43.377381Z","shell.execute_reply.started":"2021-05-30T21:41:43.100831Z","shell.execute_reply":"2021-05-30T21:41:43.374739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3- Setup Variables\nDefine the size of image and its channel, batch size  and the number of epochs.","metadata":{}},{"cell_type":"code","source":"size  = 150\nchannels = 3\nbatch = 128\nepochs = 50","metadata":{"execution":{"iopub.status.busy":"2021-05-30T21:41:43.380569Z","iopub.execute_input":"2021-05-30T21:41:43.381596Z","iopub.status.idle":"2021-05-30T21:41:43.393592Z","shell.execute_reply.started":"2021-05-30T21:41:43.381214Z","shell.execute_reply":"2021-05-30T21:41:43.391309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4- 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)\n\ndatagenValidation = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2021-05-30T21:41:43.403186Z","iopub.execute_input":"2021-05-30T21:41:43.403499Z","iopub.status.idle":"2021-05-30T21:41:43.419414Z","shell.execute_reply.started":"2021-05-30T21:41:43.403471Z","shell.execute_reply":"2021-05-30T21:41:43.417256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = datagen.flow_from_dataframe(\n    df_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)\n\nX_validation = datagenValidation.flow_from_dataframe(\n    df_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-05-30T21:41:43.422087Z","iopub.execute_input":"2021-05-30T21:41:43.422823Z","iopub.status.idle":"2021-05-30T21:41:43.882475Z","shell.execute_reply.started":"2021-05-30T21:41:43.422772Z","shell.execute_reply":"2021-05-30T21:41:43.881571Z"},"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]\n        plt.imshow(image)\n        break\n# plt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-30T21:41:43.886129Z","iopub.execute_input":"2021-05-30T21:41:43.886622Z","iopub.status.idle":"2021-05-30T21:41:58.397231Z","shell.execute_reply.started":"2021-05-30T21:41:43.886585Z","shell.execute_reply":"2021-05-30T21:41:58.396452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5- Setup Callbacks","metadata":{}},{"cell_type":"code","source":"checkpoint_filepath = 'best_model.hdf5'\n\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)\n\ncallbacks = [callback_checkpoint, callback_learningrate]","metadata":{"execution":{"iopub.status.busy":"2021-05-30T21:41:58.398372Z","iopub.execute_input":"2021-05-30T21:41:58.398837Z","iopub.status.idle":"2021-05-30T21:41:58.404039Z","shell.execute_reply.started":"2021-05-30T21:41:58.398802Z","shell.execute_reply":"2021-05-30T21:41:58.403229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6- 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-05-30T21:41:58.405209Z","iopub.execute_input":"2021-05-30T21:41:58.4057Z","iopub.status.idle":"2021-05-30T21:42:01.064736Z","shell.execute_reply.started":"2021-05-30T21:41:58.405666Z","shell.execute_reply":"2021-05-30T21:42:01.063846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 7- 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-05-30T21:42:01.068621Z","iopub.execute_input":"2021-05-30T21:42:01.068891Z","iopub.status.idle":"2021-05-31T00:04:19.895968Z","shell.execute_reply.started":"2021-05-30T21:42:01.068864Z","shell.execute_reply":"2021-05-31T00:04:19.895034Z"},"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-05-31T00:04:19.899665Z","iopub.execute_input":"2021-05-31T00:04:19.899966Z","iopub.status.idle":"2021-05-31T00:04:20.256092Z","shell.execute_reply.started":"2021-05-31T00:04:19.899934Z","shell.execute_reply":"2021-05-31T00:04:20.255083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 8- Let's predict","metadata":{}},{"cell_type":"code","source":"Model = tf.keras.models.load_model('best_model.hdf5')","metadata":{"execution":{"iopub.status.busy":"2021-05-31T00:04:20.257696Z","iopub.execute_input":"2021-05-31T00:04:20.258073Z","iopub.status.idle":"2021-05-31T00:04:20.802303Z","shell.execute_reply.started":"2021-05-31T00:04:20.258036Z","shell.execute_reply":"2021-05-31T00:04:20.801341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(test_path)\ndf_test = pd.DataFrame({'Image':filenames})\n\ndf_test.head(-5)","metadata":{"execution":{"iopub.status.busy":"2021-05-31T00:04:20.803725Z","iopub.execute_input":"2021-05-31T00:04:20.804128Z","iopub.status.idle":"2021-05-31T00:04:20.828557Z","shell.execute_reply.started":"2021-05-31T00:04:20.804091Z","shell.execute_reply":"2021-05-31T00:04:20.827765Z"},"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-05-31T00:04:20.8302Z","iopub.execute_input":"2021-05-31T00:04:20.83054Z","iopub.status.idle":"2021-05-31T00:04:20.943512Z","shell.execute_reply.started":"2021-05-31T00:04:20.830504Z","shell.execute_reply":"2021-05-31T00:04:20.942466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.class_indices","metadata":{"execution":{"iopub.status.busy":"2021-05-31T00:04:20.944827Z","iopub.execute_input":"2021-05-31T00:04:20.945348Z","iopub.status.idle":"2021-05-31T00:04:20.951381Z","shell.execute_reply.started":"2021-05-31T00:04:20.945305Z","shell.execute_reply":"2021-05-31T00:04:20.95037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = Model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-05-31T00:04:20.952871Z","iopub.execute_input":"2021-05-31T00:04:20.9534Z","iopub.status.idle":"2021-05-31T00:04:54.455987Z","shell.execute_reply.started":"2021-05-31T00:04:20.953362Z","shell.execute_reply":"2021-05-31T00:04:54.455179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = np.where(y_pred > .5, \"Dog\", \"Cat\")\ny_pred","metadata":{"execution":{"iopub.status.busy":"2021-05-31T00:04:54.459446Z","iopub.execute_input":"2021-05-31T00:04:54.459706Z","iopub.status.idle":"2021-05-31T00:04:54.466729Z","shell.execute_reply.started":"2021-05-31T00:04:54.45968Z","shell.execute_reply":"2021-05-31T00:04:54.465709Z"},"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-05-31T00:04:54.468251Z","iopub.execute_input":"2021-05-31T00:04:54.469039Z","iopub.status.idle":"2021-05-31T00:04:58.959561Z","shell.execute_reply.started":"2021-05-31T00:04:54.468883Z","shell.execute_reply":"2021-05-31T00:04:58.958693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = np.where(y_pred==\"Cat\", 0, 1)\nlabel","metadata":{"execution":{"iopub.status.busy":"2021-05-31T00:04:58.961106Z","iopub.execute_input":"2021-05-31T00:04:58.961434Z","iopub.status.idle":"2021-05-31T00:04:58.967989Z","shell.execute_reply.started":"2021-05-31T00:04:58.961401Z","shell.execute_reply":"2021-05-31T00:04:58.967254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 9- 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(-5)","metadata":{"execution":{"iopub.status.busy":"2021-05-31T00:04:58.969204Z","iopub.execute_input":"2021-05-31T00:04:58.969762Z","iopub.status.idle":"2021-05-31T00:04:58.998653Z","shell.execute_reply.started":"2021-05-31T00:04:58.969719Z","shell.execute_reply":"2021-05-31T00:04:58.99784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-31T00:04:59.000176Z","iopub.execute_input":"2021-05-31T00:04:59.000508Z","iopub.status.idle":"2021-05-31T00:04:59.17372Z","shell.execute_reply.started":"2021-05-31T00:04:59.000474Z","shell.execute_reply":"2021-05-31T00:04:59.173015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('./submission.csv')\nsubmission.head(-5)","metadata":{"execution":{"iopub.status.busy":"2021-05-31T00:04:59.175152Z","iopub.execute_input":"2021-05-31T00:04:59.17549Z","iopub.status.idle":"2021-05-31T00:04:59.192945Z","shell.execute_reply.started":"2021-05-31T00:04:59.175455Z","shell.execute_reply":"2021-05-31T00:04:59.191958Z"},"trusted":true},"execution_count":null,"outputs":[]}]}