{"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-03-23T12:31:26.748841Z","iopub.execute_input":"2023-03-23T12:31:26.749929Z","iopub.status.idle":"2023-03-23T12:31:31.53049Z","shell.execute_reply.started":"2023-03-23T12:31:26.749812Z","shell.execute_reply":"2023-03-23T12:31:31.529025Z"},"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.57896Z","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.37188Z","shell.execute_reply.started":"2023-02-08T19:39:41.354308Z","shell.execute_reply":"2023-02-08T19:39:41.37039Z"},"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-03-23T12:31:42.698781Z","iopub.execute_input":"2023-03-23T12:31:42.699388Z","iopub.status.idle":"2023-03-23T12:32:00.745164Z","shell.execute_reply.started":"2023-03-23T12:31:42.699352Z","shell.execute_reply":"2023-03-23T12:32:00.743855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls ","metadata":{"execution":{"iopub.status.busy":"2023-03-23T12:32:15.147479Z","iopub.execute_input":"2023-03-23T12:32:15.147879Z","iopub.status.idle":"2023-03-23T12:32:16.088482Z","shell.execute_reply.started":"2023-03-23T12:32:15.147838Z","shell.execute_reply":"2023-03-23T12:32:16.08729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/working/train'","metadata":{"execution":{"iopub.status.busy":"2023-03-23T12:32:19.528619Z","iopub.execute_input":"2023-03-23T12:32:19.52902Z","iopub.status.idle":"2023-03-23T12:32:19.535368Z","shell.execute_reply.started":"2023-03-23T12:32:19.52898Z","shell.execute_reply":"2023-03-23T12:32:19.533417Z"},"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-03-23T12:32:29.041109Z","iopub.execute_input":"2023-03-23T12:32:29.041573Z","iopub.status.idle":"2023-03-23T12:32:29.774714Z","shell.execute_reply.started":"2023-03-23T12:32:29.041533Z","shell.execute_reply":"2023-03-23T12:32:29.773726Z"},"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-03-23T12:32:39.173033Z","iopub.execute_input":"2023-03-23T12:32:39.173425Z","iopub.status.idle":"2023-03-23T12:32:39.17836Z","shell.execute_reply.started":"2023-03-23T12:32:39.173391Z","shell.execute_reply":"2023-03-23T12:32:39.176603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nimage_generator = ImageDataGenerator(rescale=1./255, validation_split = 0.2)","metadata":{"execution":{"iopub.status.busy":"2023-03-23T12:35:07.318393Z","iopub.execute_input":"2023-03-23T12:35:07.31911Z","iopub.status.idle":"2023-03-23T12:35:08.080724Z","shell.execute_reply.started":"2023-03-23T12:35:07.319069Z","shell.execute_reply":"2023-03-23T12:35:08.079709Z"},"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-03-23T12:35:13.236284Z","iopub.execute_input":"2023-03-23T12:35:13.237319Z","iopub.status.idle":"2023-03-23T12:35:14.018947Z","shell.execute_reply.started":"2023-03-23T12:35:13.237242Z","shell.execute_reply":"2023-03-23T12:35:14.017979Z"},"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)\nsample_test_images, _ = next(val_data_gen) ","metadata":{"execution":{"iopub.status.busy":"2023-03-23T12:59:51.887844Z","iopub.execute_input":"2023-03-23T12:59:51.888212Z","iopub.status.idle":"2023-03-23T12:59:52.368521Z","shell.execute_reply.started":"2023-03-23T12:59:51.888174Z","shell.execute_reply":"2023-03-23T12:59:52.367476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_test_images.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-23T13:20:26.666235Z","iopub.execute_input":"2023-03-23T13:20:26.667123Z","iopub.status.idle":"2023-03-23T13:20:26.675747Z","shell.execute_reply.started":"2023-03-23T13:20:26.667083Z","shell.execute_reply":"2023-03-23T13:20:26.674593Z"},"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-03-23T12:35:26.0625Z","iopub.execute_input":"2023-03-23T12:35:26.063517Z","iopub.status.idle":"2023-03-23T12:35:26.06978Z","shell.execute_reply.started":"2023-03-23T12:35:26.063469Z","shell.execute_reply":"2023-03-23T12:35:26.06832Z"},"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-03-23T13:19:37.790212Z","iopub.execute_input":"2023-03-23T13:19:37.790602Z","iopub.status.idle":"2023-03-23T13:19:38.617085Z","shell.execute_reply.started":"2023-03-23T13:19:37.79057Z","shell.execute_reply":"2023-03-23T13:19:38.616307Z"},"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-03-23T12:35:50.229209Z","iopub.execute_input":"2023-03-23T12:35:50.229609Z","iopub.status.idle":"2023-03-23T12:35:52.970535Z","shell.execute_reply.started":"2023-03-23T12:35:50.229578Z","shell.execute_reply":"2023-03-23T12:35:52.969555Z"},"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-03-23T12:36:02.428375Z","iopub.execute_input":"2023-03-23T12:36:02.429059Z","iopub.status.idle":"2023-03-23T12:36:02.440633Z","shell.execute_reply.started":"2023-03-23T12:36:02.429021Z","shell.execute_reply":"2023-03-23T12:36:02.439695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T12:36:05.256119Z","iopub.execute_input":"2023-03-23T12:36:05.256806Z","iopub.status.idle":"2023-03-23T12:36:05.263927Z","shell.execute_reply.started":"2023-03-23T12:36:05.256766Z","shell.execute_reply":"2023-03-23T12:36:05.262731Z"},"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-03-23T12:36:14.963953Z","iopub.execute_input":"2023-03-23T12:36:14.964361Z","iopub.status.idle":"2023-03-23T12:47:35.655913Z","shell.execute_reply.started":"2023-03-23T12:36:14.964324Z","shell.execute_reply":"2023-03-23T12:47:35.654931Z"},"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-03-23T12:50:37.501793Z","iopub.execute_input":"2023-03-23T12:50:37.502159Z","iopub.status.idle":"2023-03-23T12:50:37.981546Z","shell.execute_reply.started":"2023-03-23T12:50:37.502126Z","shell.execute_reply":"2023-03-23T12:50:37.980468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=plotImages(sample_test_images[20:30])\n","metadata":{"execution":{"iopub.status.busy":"2023-03-23T13:07:13.048866Z","iopub.execute_input":"2023-03-23T13:07:13.049243Z","iopub.status.idle":"2023-03-23T13:07:14.052947Z","shell.execute_reply.started":"2023-03-23T13:07:13.049203Z","shell.execute_reply":"2023-03-23T13:07:14.04694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n#img = cv2.imread('/kaggle/input/cat-and-dog/test_set/test_set/cats/cat.4004.jpg')\nimg = cv2.imread('/kaggle/input/cat-and-dog/test_set/test_set/dogs/dog.4008.jpg')\nimg = cv2.resize(img, (150, 150)) # Resize the image to match the input size of the model\nplt.imshow(img)\n# Normalize the pixel values\nimg = np.array(img) / 255.0\n\n# Reshape the image to match the input shape of the model\nimg = img.reshape(-1, 150, 150, 3)\n\n# Use the model to predict the class of the image\nprediction = model.predict(img)\n\n# Print the predicted class (1 = dog, 0 = cat)\nif prediction[0][0] > prediction[0][1]:\n    print('The image is a cat.')\nelse:\n    print('The image is a dog.')\n","metadata":{"execution":{"iopub.status.busy":"2023-03-23T13:25:33.917137Z","iopub.execute_input":"2023-03-23T13:25:33.917842Z","iopub.status.idle":"2023-03-23T13:25:34.174848Z","shell.execute_reply.started":"2023-03-23T13:25:33.917803Z","shell.execute_reply":"2023-03-23T13:25:34.173742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimg = cv2.imread('/kaggle/input/cat-and-dog/test_set/test_set/cats/cat.4004.jpg')\n#img = cv2.imread('/kaggle/input/cat-and-dog/test_set/test_set/dogs/dog.4008.jpg')\nimg = cv2.resize(img, (150, 150)) # Resize the image to match the input size of the model\nplt.imshow(img)\n# Normalize the pixel values\nimg = np.array(img) / 255.0\n\n# Reshape the image to match the input shape of the model\nimg = img.reshape(-1, 150, 150, 3)\n\n# Use the model to predict the class of the image\nprediction = model.predict(img)\n\n# Print the predicted class (1 = dog, 0 = cat)\nif prediction[0][0] > prediction[0][1]:\n    print('The image is a cat.')\nelse:\n    print('The image is a dog.')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T13:26:31.952266Z","iopub.execute_input":"2023-03-23T13:26:31.952649Z","iopub.status.idle":"2023-03-23T13:26:32.209182Z","shell.execute_reply.started":"2023-03-23T13:26:31.952617Z","shell.execute_reply":"2023-03-23T13:26:32.208219Z"},"trusted":true},"execution_count":null,"outputs":[]}]}