{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30381,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Continuation here: [Dogs / Cats image classification with CNN](https://www.kaggle.com/code/kattat/dogs-cats-image-classification-with-cnn)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\nimport logging\nlogger = tf.get_logger()\nlogger.setLevel(logging.ERROR)","metadata":{"execution":{"iopub.status.busy":"2024-12-14T14:38:09.531925Z","iopub.execute_input":"2024-12-14T14:38:09.532274Z","iopub.status.idle":"2024-12-14T14:38:14.050271Z","shell.execute_reply.started":"2024-12-14T14:38:09.532200Z","shell.execute_reply":"2024-12-14T14:38:14.049313Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2024-12-14T14:38:21.723275Z","iopub.execute_input":"2024-12-14T14:38:21.724140Z","iopub.status.idle":"2024-12-14T14:38:22.548424Z","shell.execute_reply.started":"2024-12-14T14:38:21.724106Z","shell.execute_reply":"2024-12-14T14:38:22.547676Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # Detect TPU, return appropriate distribution strategy\n# try:\n#     tpu = tf.distribute.cluster_resolver.TPUClusterResolver() \n#     print('Running on TPU ', tpu.master())\n# except ValueError:\n#     tpu = None\n\n# if tpu:\n#     tf.config.experimental_connect_to_cluster(tpu)\n#     tf.tpu.experimental.initialize_tpu_system(tpu)\n#     strategy = tf.distribute.experimental.TPUStrategy(tpu)\n# else:\n#     strategy = tf.distribute.get_strategy() \n\n# print(\"REPLICAS: \", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T19:39:41.353976Z","iopub.execute_input":"2023-02-08T19:39:41.354343Z","iopub.status.idle":"2023-02-08T19:39:41.37188Z","shell.execute_reply.started":"2023-02-08T19:39:41.354308Z","shell.execute_reply":"2023-02-08T19:39:41.37039Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!unzip -q \"/kaggle/input/dogs-vs-cats/test1.zip\"\n!unzip -q \"/kaggle/input/dogs-vs-cats/train.zip\"","metadata":{"execution":{"iopub.status.busy":"2024-12-14T16:18:21.328089Z","iopub.execute_input":"2024-12-14T16:18:21.329011Z","iopub.status.idle":"2024-12-14T16:18:40.326346Z","shell.execute_reply.started":"2024-12-14T16:18:21.328976Z","shell.execute_reply":"2024-12-14T16:18:40.324792Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls ","metadata":{"execution":{"iopub.status.busy":"2024-12-14T16:18:47.858425Z","iopub.execute_input":"2024-12-14T16:18:47.859096Z","iopub.status.idle":"2024-12-14T16:18:48.906889Z","shell.execute_reply.started":"2024-12-14T16:18:47.859058Z","shell.execute_reply":"2024-12-14T16:18:48.905622Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dir = '/kaggle/working/train'","metadata":{"execution":{"iopub.status.busy":"2024-12-14T14:39:11.720872Z","iopub.execute_input":"2024-12-14T14:39:11.721236Z","iopub.status.idle":"2024-12-14T14:39:11.725637Z","shell.execute_reply.started":"2024-12-14T14:39:11.721205Z","shell.execute_reply":"2024-12-14T14:39:11.724694Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# create folders and move files\nif not os.path.exists((os.path.join(train_dir, 'cat'))):\n    os.mkdir(os.path.join(train_dir, 'cat'))\nif not os.path.exists((os.path.join(train_dir, 'dog'))):\n    os.mkdir(os.path.join(train_dir, 'dog'))\n\nfor file in os.listdir(train_dir):\n    if file[-3] == 'j':\n        if file[0] == 'c':\n            os.replace(os.path.join(train_dir,file), os.path.join(train_dir,'cat', file))\n        else:\n            os.replace(os.path.join(train_dir,file), os.path.join(train_dir,'dog', file))","metadata":{"execution":{"iopub.status.busy":"2024-12-14T14:39:15.741378Z","iopub.execute_input":"2024-12-14T14:39:15.742277Z","iopub.status.idle":"2024-12-14T14:39:16.345155Z","shell.execute_reply.started":"2024-12-14T14:39:15.742243Z","shell.execute_reply":"2024-12-14T14:39:16.344123Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BATCH_SIZE = 100  # Number of training examples to process before updating our models variables\nIMG_SHAPE  = 150  # Our training data consists of images with width of 150 pixels and height of 150 pixels","metadata":{"execution":{"iopub.status.busy":"2024-12-14T14:39:22.227528Z","iopub.execute_input":"2024-12-14T14:39:22.228439Z","iopub.status.idle":"2024-12-14T14:39:22.232271Z","shell.execute_reply.started":"2024-12-14T14:39:22.228407Z","shell.execute_reply":"2024-12-14T14:39:22.231429Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_generator = ImageDataGenerator(rescale=1./255, validation_split = 0.2)","metadata":{"execution":{"iopub.status.busy":"2024-12-14T14:39:26.405987Z","iopub.execute_input":"2024-12-14T14:39:26.406602Z","iopub.status.idle":"2024-12-14T14:39:26.410822Z","shell.execute_reply.started":"2024-12-14T14:39:26.406568Z","shell.execute_reply":"2024-12-14T14:39:26.409929Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data_gen = image_generator.flow_from_directory(batch_size=BATCH_SIZE,\n                                                     directory=train_dir,\n                                                     shuffle=True,\n                                                     target_size=(IMG_SHAPE, IMG_SHAPE), # (150,150)\n                                                     class_mode='binary',\n                                                     subset='training')\n\nval_data_gen = image_generator.flow_from_directory(batch_size=BATCH_SIZE,\n                                                   directory=train_dir,\n                                                   shuffle=False,\n                                                   target_size=(IMG_SHAPE, IMG_SHAPE), # (150,150)\n                                                   class_mode='binary',\n                                                   subset='validation')","metadata":{"execution":{"iopub.status.busy":"2024-12-14T14:39:28.593318Z","iopub.execute_input":"2024-12-14T14:39:28.594005Z","iopub.status.idle":"2024-12-14T14:39:29.156719Z","shell.execute_reply.started":"2024-12-14T14:39:28.593970Z","shell.execute_reply":"2024-12-14T14:39:29.155939Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Visualizing Training images","metadata":{}},{"cell_type":"code","source":"sample_training_images, _ = next(train_data_gen) ","metadata":{"execution":{"iopub.status.busy":"2024-12-14T16:38:48.636183Z","iopub.execute_input":"2024-12-14T16:38:48.636528Z","iopub.status.idle":"2024-12-14T16:38:48.900341Z","shell.execute_reply.started":"2024-12-14T16:38:48.636486Z","shell.execute_reply":"2024-12-14T16:38:48.899582Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# This function will plot images in the form of a grid with 1 row and 5 columns where images are placed in each column.\ndef plotImages(images_arr):\n    fig, axes = plt.subplots(1, 5, figsize=(20,20))\n    axes = axes.flatten()\n    for img, ax in zip(images_arr, axes):\n        ax.imshow(img)\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-12-14T14:39:35.925435Z","iopub.execute_input":"2024-12-14T14:39:35.925822Z","iopub.status.idle":"2024-12-14T14:39:35.931493Z","shell.execute_reply.started":"2024-12-14T14:39:35.925791Z","shell.execute_reply":"2024-12-14T14:39:35.930441Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plotImages(sample_training_images[:5])  # Plot images 0-4","metadata":{"execution":{"iopub.status.busy":"2024-12-14T14:39:39.797599Z","iopub.execute_input":"2024-12-14T14:39:39.797997Z","iopub.status.idle":"2024-12-14T14:39:40.846941Z","shell.execute_reply.started":"2024-12-14T14:39:39.797967Z","shell.execute_reply":"2024-12-14T14:39:40.845923Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model creation","metadata":{}},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)),\n    tf.keras.layers.MaxPooling2D(2, 2),\n\n    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    \n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    \n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    \n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(512, activation='relu'),\n    tf.keras.layers.Dense(2)\n])","metadata":{"execution":{"iopub.status.busy":"2024-12-14T14:39:51.600629Z","iopub.execute_input":"2024-12-14T14:39:51.601015Z","iopub.status.idle":"2024-12-14T14:39:54.399136Z","shell.execute_reply.started":"2024-12-14T14:39:51.600985Z","shell.execute_reply":"2024-12-14T14:39:54.398392Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-12-14T14:39:57.241605Z","iopub.execute_input":"2024-12-14T14:39:57.242432Z","iopub.status.idle":"2024-12-14T14:39:57.255557Z","shell.execute_reply.started":"2024-12-14T14:39:57.242401Z","shell.execute_reply":"2024-12-14T14:39:57.254700Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-12-14T14:39:59.965847Z","iopub.execute_input":"2024-12-14T14:39:59.966604Z","iopub.status.idle":"2024-12-14T14:39:59.972396Z","shell.execute_reply.started":"2024-12-14T14:39:59.966574Z","shell.execute_reply":"2024-12-14T14:39:59.971402Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"EPOCHS = 10\nhistory = model.fit_generator(\n    train_data_gen,\n    steps_per_epoch=int(np.ceil(20000 / float(BATCH_SIZE))),\n    epochs=EPOCHS,\n    validation_data=val_data_gen,\n    validation_steps=int(np.ceil(5000 / float(BATCH_SIZE)))\n)","metadata":{"execution":{"iopub.status.busy":"2024-12-14T14:40:03.709532Z","iopub.execute_input":"2024-12-14T14:40:03.710436Z","iopub.status.idle":"2024-12-14T14:50:38.064662Z","shell.execute_reply.started":"2024-12-14T14:40:03.710405Z","shell.execute_reply":"2024-12-14T14:50:38.063487Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Visualizing results of the training","metadata":{}},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(EPOCHS)\n\nplt.figure(figsize=(8, 8))\nplt.subplot(1, 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(1, 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.savefig('./foo.png')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-12-14T15:30:14.843408Z","iopub.execute_input":"2024-12-14T15:30:14.843867Z","iopub.status.idle":"2024-12-14T15:30:15.248819Z","shell.execute_reply.started":"2024-12-14T15:30:14.843835Z","shell.execute_reply":"2024-12-14T15:30:15.247696Z"},"trusted":true},"outputs":[],"execution_count":null}]}