{"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":"# 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":"2023-02-08T19:39:36.358422Z","iopub.execute_input":"2023-02-08T19:39:36.358856Z","iopub.status.idle":"2023-02-08T19:39:40.577006Z","shell.execute_reply.started":"2023-02-08T19:39:36.358770Z","shell.execute_reply":"2023-02-08T19:39:40.575939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-02-08T19:39:40.578960Z","iopub.execute_input":"2023-02-08T19:39:40.580081Z","iopub.status.idle":"2023-02-08T19:39:41.352414Z","shell.execute_reply.started":"2023-02-08T19:39:40.580042Z","shell.execute_reply":"2023-02-08T19:39:41.351361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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.371880Z","shell.execute_reply.started":"2023-02-08T19:39:41.354308Z","shell.execute_reply":"2023-02-08T19:39:41.370390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-02-08T19:39:41.375261Z","iopub.execute_input":"2023-02-08T19:39:41.376062Z","iopub.status.idle":"2023-02-08T19:39:57.334620Z","shell.execute_reply.started":"2023-02-08T19:39:41.376007Z","shell.execute_reply":"2023-02-08T19:39:57.333279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls ","metadata":{"execution":{"iopub.status.busy":"2023-02-08T19:39:57.337719Z","iopub.execute_input":"2023-02-08T19:39:57.338507Z","iopub.status.idle":"2023-02-08T19:39:58.331599Z","shell.execute_reply.started":"2023-02-08T19:39:57.338460Z","shell.execute_reply":"2023-02-08T19:39:58.330377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/working/train'","metadata":{"execution":{"iopub.status.busy":"2023-02-08T19:39:58.334249Z","iopub.execute_input":"2023-02-08T19:39:58.335301Z","iopub.status.idle":"2023-02-08T19:39:58.340610Z","shell.execute_reply.started":"2023-02-08T19:39:58.335248Z","shell.execute_reply":"2023-02-08T19:39:58.339687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-02-08T19:39:58.342101Z","iopub.execute_input":"2023-02-08T19:39:58.342681Z","iopub.status.idle":"2023-02-08T19:39:59.311592Z","shell.execute_reply.started":"2023-02-08T19:39:58.342647Z","shell.execute_reply":"2023-02-08T19:39:59.310218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-02-08T19:39:59.318387Z","iopub.execute_input":"2023-02-08T19:39:59.321621Z","iopub.status.idle":"2023-02-08T19:39:59.330197Z","shell.execute_reply.started":"2023-02-08T19:39:59.321566Z","shell.execute_reply":"2023-02-08T19:39:59.328876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_generator = ImageDataGenerator(rescale=1./255, validation_split = 0.2)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T19:39:59.334120Z","iopub.execute_input":"2023-02-08T19:39:59.335773Z","iopub.status.idle":"2023-02-08T19:39:59.343481Z","shell.execute_reply.started":"2023-02-08T19:39:59.335718Z","shell.execute_reply":"2023-02-08T19:39:59.342194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-02-08T19:39:59.353832Z","iopub.execute_input":"2023-02-08T19:39:59.355470Z","iopub.status.idle":"2023-02-08T19:40:00.303281Z","shell.execute_reply.started":"2023-02-08T19:39:59.355407Z","shell.execute_reply":"2023-02-08T19:40:00.302194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing Training images","metadata":{}},{"cell_type":"code","source":"sample_training_images, _ = next(train_data_gen) ","metadata":{"execution":{"iopub.status.busy":"2023-02-08T19:40:00.304573Z","iopub.execute_input":"2023-02-08T19:40:00.306291Z","iopub.status.idle":"2023-02-08T19:40:00.566790Z","shell.execute_reply.started":"2023-02-08T19:40:00.306249Z","shell.execute_reply":"2023-02-08T19:40:00.565775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-02-08T19:40:00.569139Z","iopub.execute_input":"2023-02-08T19:40:00.570387Z","iopub.status.idle":"2023-02-08T19:40:00.577533Z","shell.execute_reply.started":"2023-02-08T19:40:00.570340Z","shell.execute_reply":"2023-02-08T19:40:00.576442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotImages(sample_training_images[:5])  # Plot images 0-4","metadata":{"execution":{"iopub.status.busy":"2023-02-08T19:40:00.579483Z","iopub.execute_input":"2023-02-08T19:40:00.580293Z","iopub.status.idle":"2023-02-08T19:40:01.704537Z","shell.execute_reply.started":"2023-02-08T19:40:00.580211Z","shell.execute_reply":"2023-02-08T19:40:01.702966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-02-08T19:40:01.706091Z","iopub.execute_input":"2023-02-08T19:40:01.707927Z","iopub.status.idle":"2023-02-08T19:40:04.510630Z","shell.execute_reply.started":"2023-02-08T19:40:01.707888Z","shell.execute_reply":"2023-02-08T19:40:04.509629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-02-08T19:40:04.512200Z","iopub.execute_input":"2023-02-08T19:40:04.512842Z","iopub.status.idle":"2023-02-08T19:40:04.526846Z","shell.execute_reply.started":"2023-02-08T19:40:04.512794Z","shell.execute_reply":"2023-02-08T19:40:04.525935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-08T19:40:04.529738Z","iopub.execute_input":"2023-02-08T19:40:04.530036Z","iopub.status.idle":"2023-02-08T19:40:06.030579Z","shell.execute_reply.started":"2023-02-08T19:40:04.530004Z","shell.execute_reply":"2023-02-08T19:40:06.029429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-02-08T19:40:06.032236Z","iopub.execute_input":"2023-02-08T19:40:06.032635Z","iopub.status.idle":"2023-02-08T19:51:29.899071Z","shell.execute_reply.started":"2023-02-08T19:40:06.032589Z","shell.execute_reply":"2023-02-08T19:51:29.898141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-02-08T19:51:29.900549Z","iopub.execute_input":"2023-02-08T19:51:29.900928Z","iopub.status.idle":"2023-02-08T19:51:30.306357Z","shell.execute_reply.started":"2023-02-08T19:51:29.900893Z","shell.execute_reply":"2023-02-08T19:51:30.305384Z"},"trusted":true},"execution_count":null,"outputs":[]}]}