{"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":"import numpy as np \nimport pandas as pd \nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import img_to_array, load_img\nimport os\nimport glob \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":"2023-11-08T02:33:45.099142Z","iopub.execute_input":"2023-11-08T02:33:45.099535Z","iopub.status.idle":"2023-11-08T02:33:45.108435Z","shell.execute_reply.started":"2023-11-08T02:33:45.099496Z","shell.execute_reply":"2023-11-08T02:33:45.107528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"../input/dogs-vs-cats/test1.zip\"\ntest_path = \"../input/dogs-vs-cats/train.zip\"\n\nfiles = \"/kaggle/working/\"\n\nimport zipfile\n\nwith zipfile.ZipFile(train_path, 'r') as zipp:\n    zipp.extractall(files)\n    \nwith zipfile.ZipFile(test_path, 'r') as zipp:\n    zipp.extractall(files)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T02:33:49.183433Z","iopub.execute_input":"2023-11-08T02:33:49.184124Z","iopub.status.idle":"2023-11-08T02:33:59.810398Z","shell.execute_reply.started":"2023-11-08T02:33:49.184087Z","shell.execute_reply":"2023-11-08T02:33:59.809567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = []\nlabel = []\nbase_dir = '/kaggle/working'\ntrain_path_content = os.listdir(f'{base_dir}/train')\nprint(f'train_size: {len(train_path_content)}')\ntrain_dir = os.path.join(base_dir, 'train')\n\ntrain_imgs = glob.glob(os.path.join(train_dir, \"*\"))\nfor img in train_imgs:\n    image_name = os.path.splitext(os.path.basename(img))[0]\n    if 'cat' in image_name:\n    # move it to directory /content/train/cats\n        image.append(img)\n        label.append('cat')\n    elif 'dog' in image_name:\n    # move it to directory /content/train/dogs\n        image.append(img)\n        label.append('dog')\ndf = pd.DataFrame({'path':image,'label':label})     ","metadata":{"execution":{"iopub.status.busy":"2023-11-08T02:34:03.90127Z","iopub.execute_input":"2023-11-08T02:34:03.901972Z","iopub.status.idle":"2023-11-08T02:34:04.095437Z","shell.execute_reply.started":"2023-11-08T02:34:03.901938Z","shell.execute_reply":"2023-11-08T02:34:04.0945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-11-08T02:34:08.349875Z","iopub.execute_input":"2023-11-08T02:34:08.350237Z","iopub.status.idle":"2023-11-08T02:34:08.363796Z","shell.execute_reply.started":"2023-11-08T02:34:08.350205Z","shell.execute_reply":"2023-11-08T02:34:08.362807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train, df_test = train_test_split(df, train_size=0.8, shuffle=True, random_state=1)\n\ninput_shape = (150, 150)\n\n# Define the data generator\ndatagen = ImageDataGenerator(rescale=1./255,\n                            rotation_range=40,\n                            width_shift_range=0.2,\n                            height_shift_range=0.2,\n                            shear_range=0.2,\n                            zoom_range=0.2,\n                            horizontal_flip=True,\n                            fill_mode='nearest',\n                            validation_split=0.2)\n\ntest_generator = ImageDataGenerator(\n    rescale=1./255\n)\n\n\ntrain_generator = datagen.flow_from_dataframe(dataframe=df_train,\n                                              x_col='path',\n                                              y_col='label',\n                                              subset='training',\n                                              target_size=input_shape,\n                                              batch_size=16,\n                                              class_mode='binary')\n\nval_generator = datagen.flow_from_dataframe(dataframe=df_train,\n                                            x_col='path',\n                                            y_col='label',\n                                            subset='validation',\n                                            target_size=input_shape,\n                                            batch_size=16,\n                                            class_mode='binary')\n\ntest_images = test_generator.flow_from_dataframe(\n    dataframe=df_test,\n    x_col='path',\n    y_col='label',\n    target_size=input_shape,\n    batch_size=16,\n    class_mode='binary'\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-08T02:34:12.658322Z","iopub.execute_input":"2023-11-08T02:34:12.65866Z","iopub.status.idle":"2023-11-08T02:34:13.132195Z","shell.execute_reply.started":"2023-11-08T02:34:12.658634Z","shell.execute_reply":"2023-11-08T02:34:13.131459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2023-11-08T02:34:19.504214Z","iopub.execute_input":"2023-11-08T02:34:19.504547Z","iopub.status.idle":"2023-11-08T02:34:19.515422Z","shell.execute_reply.started":"2023-11-08T02:34:19.504522Z","shell.execute_reply":"2023-11-08T02:34:19.514531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model():\n\n    model = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(32, (3, 3), input_shape=(150, 150, 3)),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Activation('ReLU'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(64, (3, 3)),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Activation('ReLU'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n#     tf.keras.layers.Conv2D(128, (3, 3)),\n#     tf.keras.layers.BatchNormalization(),\n#     tf.keras.layers.Activation('LeakyReLU'),\n#     tf.keras.layers.MaxPooling2D(2, 2),\n#     tf.keras.layers.Conv2D(128, (3, 3)),\n#     tf.keras.layers.BatchNormalization(),\n#     tf.keras.layers.Activation('LeakyReLU'),\n#     tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(128, (3, 3)),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Activation('ReLU'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(512, activation='ReLU'),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(1, activation='sigmoid')\n  ])\n\n\n    model.compile(optimizer = RMSprop(learning_rate=0.0001),\n                loss = 'binary_crossentropy',\n                metrics = ['accuracy'])\n\n  \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-11-08T02:38:01.489485Z","iopub.execute_input":"2023-11-08T02:38:01.489786Z","iopub.status.idle":"2023-11-08T02:38:01.501755Z","shell.execute_reply.started":"2023-11-08T02:38:01.489759Z","shell.execute_reply":"2023-11-08T02:38:01.500913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T02:38:06.869179Z","iopub.execute_input":"2023-11-08T02:38:06.869537Z","iopub.status.idle":"2023-11-08T02:38:07.033672Z","shell.execute_reply.started":"2023-11-08T02:38:06.869506Z","shell.execute_reply":"2023-11-08T02:38:07.032767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator,\n                    epochs=20,\n                    validation_data=val_generator)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T02:38:37.579839Z","iopub.execute_input":"2023-11-08T02:38:37.580763Z","iopub.status.idle":"2023-11-08T03:16:51.370975Z","shell.execute_reply.started":"2023-11-08T02:38:37.58072Z","shell.execute_reply":"2023-11-08T03:16:51.370036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import visualkeras\n# visualkeras.layered_view(model)\nvisualkeras.layered_view(model, legend=True) # without custom font","metadata":{"execution":{"iopub.status.busy":"2023-11-08T03:21:50.69475Z","iopub.execute_input":"2023-11-08T03:21:50.695129Z","iopub.status.idle":"2023-11-08T03:21:50.777352Z","shell.execute_reply.started":"2023-11-08T03:21:50.6951Z","shell.execute_reply":"2023-11-08T03:21:50.776435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install visualkeras","metadata":{"execution":{"iopub.status.busy":"2023-11-08T03:20:20.109262Z","iopub.execute_input":"2023-11-08T03:20:20.109648Z","iopub.status.idle":"2023-11-08T03:20:32.789364Z","shell.execute_reply.started":"2023-11-08T03:20:20.109614Z","shell.execute_reply":"2023-11-08T03:20:32.788121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir(\"/kaggle/working/test1\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]\n\ntest_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"./test1/\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=(150,150),\n    batch_size=16,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T02:30:20.910126Z","iopub.execute_input":"2023-11-08T02:30:20.91049Z","iopub.status.idle":"2023-11-08T02:30:21.043663Z","shell.execute_reply.started":"2023-11-08T02:30:20.910462Z","shell.execute_reply":"2023-11-08T02:30:21.042978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T02:30:26.789426Z","iopub.execute_input":"2023-11-08T02:30:26.789785Z","iopub.status.idle":"2023-11-08T02:30:45.111462Z","shell.execute_reply.started":"2023-11-08T02:30:26.789759Z","shell.execute_reply":"2023-11-08T02:30:45.110451Z"},"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']\n\nepochs=range(len(acc)) # Get number of epochs\n\n\nplt.plot(epochs, acc, 'r', \"Training Accuracy\")\nplt.plot(epochs, val_acc, 'b', \"Validation Accuracy\")\nplt.title('Training and validation accuracy')\nplt.show()\nprint(\"\")\n\nplt.plot(epochs, loss, 'r', \"Training Loss\")\nplt.plot(epochs, val_loss, 'b', \"Validation Loss\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T03:24:19.064322Z","iopub.execute_input":"2023-11-08T03:24:19.065273Z","iopub.status.idle":"2023-11-08T03:24:19.409845Z","shell.execute_reply.started":"2023-11-08T03:24:19.065238Z","shell.execute_reply":"2023-11-08T03:24:19.40895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.evaluate(test_images, verbose=0)\n\nprint(\"    Test Loss: {:.5f}\".format(results[0]))\nprint(\"Test Accuracy: {:.2f}%\".format(results[1] * 100))","metadata":{"execution":{"iopub.status.busy":"2023-11-08T03:24:37.229544Z","iopub.execute_input":"2023-11-08T03:24:37.230457Z","iopub.status.idle":"2023-11-08T03:24:44.228375Z","shell.execute_reply.started":"2023-11-08T03:24:37.23042Z","shell.execute_reply":"2023-11-08T03:24:44.22744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, classification_report\nimport seaborn as sns\n\npredictions = model.predict(test_images)\n\ncm = confusion_matrix(test_images.labels, predictions, labels=[0, 1])\nclr = classification_report(test_images.labels, predictions, labels=[0, 1], target_names=[\"CAT\", \"DOG\"])\n\nplt.figure(figsize=(6, 6))\nsns.heatmap(cm, annot=True, fmt='g', vmin=0, cmap='Blues', cbar=False)\nplt.xticks(ticks=[0.5, 1.5], labels=[\"CAT\", \"DOG\"])\nplt.yticks(ticks=[0.5, 1.5], labels=[\"CAT\", \"DOG\"])\nplt.xlabel(\"Predicted\")\nplt.ylabel(\"Actual\")\nplt.title(\"Confusion Matrix\")\nplt.show()\n\nprint(\"Classification Report:\\n----------------------\\n\", clr)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T03:25:10.152163Z","iopub.execute_input":"2023-11-08T03:25:10.152533Z","iopub.status.idle":"2023-11-08T03:25:17.567707Z","shell.execute_reply.started":"2023-11-08T03:25:10.152504Z","shell.execute_reply":"2023-11-08T03:25:17.566435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'] = np.argmax(predict, axis=-1)\nlabel_map = dict((category,filename) for filename,category in train_generator.class_indices.items())\ntest_df['category'] = test_df['category'].replace(label_map)\ntest_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"execution":{"iopub.status.busy":"2023-11-07T18:42:59.530243Z","iopub.execute_input":"2023-11-07T18:42:59.530636Z","iopub.status.idle":"2023-11-07T18:42:59.549058Z","shell.execute_reply.started":"2023-11-07T18:42:59.530602Z","shell.execute_reply":"2023-11-07T18:42:59.548159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-07T18:42:59.550315Z","iopub.execute_input":"2023-11-07T18:42:59.550671Z","iopub.status.idle":"2023-11-07T18:42:59.590392Z","shell.execute_reply.started":"2023-11-07T18:42:59.550638Z","shell.execute_reply":"2023-11-07T18:42:59.589511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}