{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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"},{"sourceId":10642576,"sourceType":"datasetVersion","datasetId":6589565}],"dockerImageVersionId":30839,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import necessary libraries","metadata":{}},{"cell_type":"code","source":"\n\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\nimport matplotlib.pyplot as plt\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:18:24.845475Z","iopub.execute_input":"2025-02-10T09:18:24.845819Z","iopub.status.idle":"2025-02-10T09:18:36.578522Z","shell.execute_reply.started":"2025-02-10T09:18:24.845790Z","shell.execute_reply":"2025-02-10T09:18:36.577620Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data preprocessing","metadata":{}},{"cell_type":"markdown","source":"## Extraction of input zip files and file format check","metadata":{}},{"cell_type":"code","source":"import zipfile\nzip_files = ['test1','train']\nfor zip_file in zip_files:\n    with zipfile.ZipFile(f'/kaggle/input/dogs-vs-cats/{zip_file}.zip','r') as file:\n        file.extractall(\".\")\n        print(\"{} unzipped successfully\".format(zip_file))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:22:55.696388Z","iopub.execute_input":"2025-02-10T09:22:55.696733Z","iopub.status.idle":"2025-02-10T09:23:10.219198Z","shell.execute_reply.started":"2025-02-10T09:22:55.696684Z","shell.execute_reply":"2025-02-10T09:23:10.218403Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(os.listdir(\".\"))\ntrain_path=\"./train\"\nfile_names=os.listdir(train_path)\nprint(\"train samples \",len(file_names))\nprint(file_names[:3])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:23:10.220169Z","iopub.execute_input":"2025-02-10T09:23:10.220391Z","iopub.status.idle":"2025-02-10T09:23:10.238225Z","shell.execute_reply.started":"2025-02-10T09:23:10.220372Z","shell.execute_reply":"2025-02-10T09:23:10.237414Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.mkdir(\"/kaggle/working/final_train\")\nos.mkdir(\"/kaggle/working/final_train/dog\")\nos.mkdir(\"/kaggle/working/final_train/cat\")\ndog_dest=\"/kaggle/working/final_train/dog\"\ncat_dest=\"/kaggle/working/final_train/cat\"\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:20:42.969924Z","iopub.execute_input":"2025-02-10T09:20:42.970231Z","iopub.status.idle":"2025-02-10T09:20:42.986376Z","shell.execute_reply.started":"2025-02-10T09:20:42.970207Z","shell.execute_reply":"2025-02-10T09:20:42.985269Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Mapping necessary files to folders","metadata":{}},{"cell_type":"code","source":"def make_folder(train_path):\n    for file in os.listdir(train_path):\n        if file.split(\".\")[0]=='dog':\n            src_path = os.path.join(train_path, file)\n            dst_path = os.path.join(dog_dest, file)\n            os.rename(src_path, dst_path)\n        elif file.split(\".\")[0]=='cat':\n            src_path = os.path.join(train_path, file)\n            dst_path = os.path.join(cat_dest, file)\n            os.rename(src_path, dst_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:23:29.617806Z","iopub.execute_input":"2025-02-10T09:23:29.618112Z","iopub.status.idle":"2025-02-10T09:23:29.622655Z","shell.execute_reply.started":"2025-02-10T09:23:29.618090Z","shell.execute_reply":"2025-02-10T09:23:29.621929Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"make_folder(train_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:23:32.861309Z","iopub.execute_input":"2025-02-10T09:23:32.861598Z","iopub.status.idle":"2025-02-10T09:23:33.428916Z","shell.execute_reply.started":"2025-02-10T09:23:32.861575Z","shell.execute_reply":"2025-02-10T09:23:33.428199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# tf.keras.preprocessing.image_dataset_from_directory is used to take data from a directory,\n# where data is divided into categories by folder\ndataset=tf.keras.preprocessing.image_dataset_from_directory('/kaggle/working/final_train',batch_size=256, image_size=(128, 128),)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:47:04.839349Z","iopub.execute_input":"2025-02-10T09:47:04.839693Z","iopub.status.idle":"2025-02-10T09:47:05.848409Z","shell.execute_reply.started":"2025-02-10T09:47:04.839664Z","shell.execute_reply":"2025-02-10T09:47:05.847729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Get basic information\nfor images, labels in dataset.take(1):\n    print(\"Batch shape:\", images.shape)\n    print(\"Labels shape:\", labels.shape)\n    print(\"Number of classes:\", len(dataset.class_names))\n    print(\"Class names:\", dataset.class_names)\n\n# Count total number of samples\ntotal_samples = 0\nfor images, labels in dataset:\n    total_samples += len(images)\nprint(\"Total number of images:\", total_samples)\n\n# Get batch size from the first batch\nbatch_size = next(iter(dataset))[0].shape[0]\nprint(\"Batch size:\", batch_size)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:47:06.438575Z","iopub.execute_input":"2025-02-10T09:47:06.438913Z","iopub.status.idle":"2025-02-10T09:47:15.862134Z","shell.execute_reply.started":"2025-02-10T09:47:06.438889Z","shell.execute_reply":"2025-02-10T09:47:15.861300Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# This os done for normailzation of the pixels 0-white 255 represents black\ndataset = dataset.map(lambda x, y: (x/255.0, y))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:47:15.863238Z","iopub.execute_input":"2025-02-10T09:47:15.863497Z","iopub.status.idle":"2025-02-10T09:47:15.876657Z","shell.execute_reply.started":"2025-02-10T09:47:15.863476Z","shell.execute_reply":"2025-02-10T09:47:15.875911Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndata_iterator = dataset.as_numpy_iterator() # Creates numpy array from the data as batch\nbatch = data_iterator.next() # Takes a batch from the data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:47:15.878160Z","iopub.execute_input":"2025-02-10T09:47:15.878394Z","iopub.status.idle":"2025-02-10T09:47:16.011184Z","shell.execute_reply.started":"2025-02-10T09:47:15.878375Z","shell.execute_reply":"2025-02-10T09:47:16.010234Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\n# 1 is dog 0 is cat","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:47:16.012321Z","iopub.execute_input":"2025-02-10T09:47:16.012623Z","iopub.status.idle":"2025-02-10T09:47:16.017942Z","shell.execute_reply.started":"2025-02-10T09:47:16.012585Z","shell.execute_reply":"2025-02-10T09:47:16.016818Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, ax = plt.subplots(ncols=4, figsize=(20,20))\nfor idx, img in enumerate(batch[0][:4]):\n    ax[idx].imshow(img)\n    ax[idx].title.set_text(batch[1][idx])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:47:16.018631Z","iopub.execute_input":"2025-02-10T09:47:16.019644Z","iopub.status.idle":"2025-02-10T09:47:17.401222Z","shell.execute_reply.started":"2025-02-10T09:47:16.019613Z","shell.execute_reply":"2025-02-10T09:47:17.400236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Dividing the train, test, and val data\ntrain_size = int(len(dataset)*.7)\nval_size = int(len(dataset)*.2)\ntest_size = int(len(dataset)*.1)\ntrain = dataset.take(train_size)\nval = dataset.skip(train_size).take(val_size)\ntest = dataset.skip(train_size+val_size).take(test_size)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:47:17.402401Z","iopub.execute_input":"2025-02-10T09:47:17.402733Z","iopub.status.idle":"2025-02-10T09:47:17.415357Z","shell.execute_reply.started":"2025-02-10T09:47:17.402688Z","shell.execute_reply":"2025-02-10T09:47:17.414707Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model Architecture","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout, BatchNormalization, GlobalAveragePooling2D","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:47:17.416312Z","iopub.execute_input":"2025-02-10T09:47:17.416569Z","iopub.status.idle":"2025-02-10T09:47:17.425136Z","shell.execute_reply.started":"2025-02-10T09:47:17.416549Z","shell.execute_reply":"2025-02-10T09:47:17.424329Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model=Sequential()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:48:17.071248Z","iopub.execute_input":"2025-02-10T09:48:17.071591Z","iopub.status.idle":"2025-02-10T09:48:17.075989Z","shell.execute_reply.started":"2025-02-10T09:48:17.071562Z","shell.execute_reply":"2025-02-10T09:48:17.075142Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.add(Conv2D(32,(3,3), 1, activation='relu', input_shape=(128,128,3)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(32,(3,3), 1, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64,(3,3), 1, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(64,(3,3), 1, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128,(3,3), 1, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(128,(3,3), 1, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128,(3,3), 1, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(128,(3,3), 1, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:48:17.634474Z","iopub.execute_input":"2025-02-10T09:48:17.634814Z","iopub.status.idle":"2025-02-10T09:48:17.915167Z","shell.execute_reply.started":"2025-02-10T09:48:17.634789Z","shell.execute_reply":"2025-02-10T09:48:17.914493Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# model.add(Flatten())\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1,activation='sigmoid'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:48:19.769996Z","iopub.execute_input":"2025-02-10T09:48:19.770288Z","iopub.status.idle":"2025-02-10T09:48:19.864331Z","shell.execute_reply.started":"2025-02-10T09:48:19.770267Z","shell.execute_reply":"2025-02-10T09:48:19.863659Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:48:21.402616Z","iopub.execute_input":"2025-02-10T09:48:21.402977Z","iopub.status.idle":"2025-02-10T09:48:21.437765Z","shell.execute_reply.started":"2025-02-10T09:48:21.402952Z","shell.execute_reply":"2025-02-10T09:48:21.437104Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile('adam', loss=tf.losses.BinaryCrossentropy(), metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:48:25.504188Z","iopub.execute_input":"2025-02-10T09:48:25.504482Z","iopub.status.idle":"2025-02-10T09:48:25.513068Z","shell.execute_reply.started":"2025-02-10T09:48:25.504461Z","shell.execute_reply":"2025-02-10T09:48:25.511986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"logdir='log'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:48:26.271673Z","iopub.execute_input":"2025-02-10T09:48:26.272003Z","iopub.status.idle":"2025-02-10T09:48:26.275362Z","shell.execute_reply.started":"2025-02-10T09:48:26.271980Z","shell.execute_reply":"2025-02-10T09:48:26.274616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=logdir)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:48:26.907622Z","iopub.execute_input":"2025-02-10T09:48:26.908070Z","iopub.status.idle":"2025-02-10T09:48:26.912394Z","shell.execute_reply.started":"2025-02-10T09:48:26.908041Z","shell.execute_reply":"2025-02-10T09:48:26.911434Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"hist = model.fit(train, epochs=20, validation_data=val, callbacks=[tensorboard_callback])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:48:27.450200Z","iopub.execute_input":"2025-02-10T09:48:27.450564Z","iopub.status.idle":"2025-02-10T09:55:13.211911Z","shell.execute_reply.started":"2025-02-10T09:48:27.450540Z","shell.execute_reply":"2025-02-10T09:55:13.211025Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Evaluation","metadata":{}},{"cell_type":"code","source":"fig = plt.figure()\nplt.plot(hist.history['loss'], color='teal', label='loss')\nplt.plot(hist.history['val_loss'], color='orange', label='val_loss')\nfig.suptitle('Loss', fontsize=20)\nplt.legend(loc=\"upper left\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:55:13.213162Z","iopub.execute_input":"2025-02-10T09:55:13.213409Z","iopub.status.idle":"2025-02-10T09:55:13.367295Z","shell.execute_reply.started":"2025-02-10T09:55:13.213388Z","shell.execute_reply":"2025-02-10T09:55:13.366533Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig = plt.figure()\nplt.plot(hist.history['accuracy'], color='teal', label='accuracy')\nplt.plot(hist.history['val_accuracy'], color='orange', label='val_accuracy')\nfig.suptitle('Accuracy', fontsize=20)\nplt.legend(loc=\"upper left\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T09:55:13.368381Z","iopub.execute_input":"2025-02-10T09:55:13.368606Z","iopub.status.idle":"2025-02-10T09:55:13.515573Z","shell.execute_reply.started":"2025-02-10T09:55:13.368587Z","shell.execute_reply":"2025-02-10T09:55:13.514812Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir('/kaggle/working/test1')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-02T16:11:19.335246Z","iopub.execute_input":"2025-02-02T16:11:19.335463Z","iopub.status.idle":"2025-02-02T16:11:19.356751Z","shell.execute_reply.started":"2025-02-02T16:11:19.335444Z","shell.execute_reply":"2025-02-02T16:11:19.355925Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Custom presictions","metadata":{}},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\n\n# Read the image using OpenCV\n# img = cv2.imread('/kaggle/working/test1/11878.jpg')\nimg = cv2.imread('/kaggle/input/image-single/images.jpg')\n\n\n# Display the image using Matplotlib\nplt.imshow(img)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T10:00:23.434351Z","iopub.execute_input":"2025-02-10T10:00:23.434669Z","iopub.status.idle":"2025-02-10T10:00:23.691098Z","shell.execute_reply.started":"2025-02-10T10:00:23.434645Z","shell.execute_reply":"2025-02-10T10:00:23.690124Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"resize = tf.image.resize(img, (128,128))\nyhat = model.predict(np.expand_dims(resize/255, 0))\n# model.predict()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T10:00:46.728960Z","iopub.execute_input":"2025-02-10T10:00:46.729279Z","iopub.status.idle":"2025-02-10T10:00:47.702403Z","shell.execute_reply.started":"2025-02-10T10:00:46.729253Z","shell.execute_reply":"2025-02-10T10:00:47.701516Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if yhat > 0.5: \n    print(f'Predicted class is dog')\nelse:\n    print(f'Predicted class is cat')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T10:00:49.057254Z","iopub.execute_input":"2025-02-10T10:00:49.057555Z","iopub.status.idle":"2025-02-10T10:00:49.062166Z","shell.execute_reply.started":"2025-02-10T10:00:49.057534Z","shell.execute_reply":"2025-02-10T10:00:49.061295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(yhat)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for file in os.listdir(\"/kaggle/working/test1\"):\n    img = cv2.imread('/kaggle/input/image-single/images.jpg')\n\n\n# Display the image using Matplotlib\nplt.imshow(img)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T10:01:37.699789Z","iopub.execute_input":"2025-02-10T10:01:37.700076Z","iopub.status.idle":"2025-02-10T10:01:51.334512Z","shell.execute_reply.started":"2025-02-10T10:01:37.700055Z","shell.execute_reply":"2025-02-10T10:01:51.333585Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#  Save model\nmodel.save(os.path.join('models','imageclassifier.h5'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T10:01:57.865585Z","iopub.execute_input":"2025-02-10T10:01:57.865906Z","iopub.status.idle":"2025-02-10T10:01:57.964182Z","shell.execute_reply.started":"2025-02-10T10:01:57.865879Z","shell.execute_reply":"2025-02-10T10:01:57.963251Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Submit csv file","metadata":{}},{"cell_type":"code","source":"# import os\n# import tensorflow as tf\n# import pandas as pd\n# import numpy as np\n\n# # Define the function to create CSV of predictions\n# def create_prediction_csv(model, folder_path, csv_filename):\n#     # Initialize a list to store results\n#     predictions = []\n\n#     # Loop through the images in the folder\n#     for img_name in os.listdir(folder_path):\n#         img_path = os.path.join(folder_path, img_name)\n        \n#         # Check if the file is an image (you can add other checks for supported formats)\n#         if img_name.lower().endswith(('.png', '.jpg', '.jpeg')):\n#             # Load and preprocess the image\n#             img = tf.keras.preprocessing.image.load_img(img_path)\n#             img = tf.keras.preprocessing.image.img_to_array(img)\n\n#             # Resize image using tf.image.resize as you mentioned\n#             img_resized = tf.image.resize(img, (128, 128))\n\n#             # Make the image ready for prediction (normalize and expand dimensions)\n#             img_resized = np.expand_dims(img_resized, axis=0)  # Add batch dimension\n#             img_resized = img_resized / 255.0  # Normalize the image\n\n#             # Get model prediction\n#             prediction = model.predict(img_resized)\n#             predicted_class = np.argmax(prediction)  # Assuming it's a classification task\n\n#             # Append the result to the list\n#             predictions.append([img_name, predicted_class])\n\n#     # Create a DataFrame from the predictions\n#     df = pd.DataFrame(predictions, columns=['Image', 'Predicted_Class'])\n\n#     # Save the DataFrame as a CSV file\n#     df.to_csv(csv_filename, index=False)\n\n#     print(f'CSV file created: {csv_filename}')\n\n# # Usage example:\n# # Assuming you have a trained model and an image folder path\n# # model = tf.keras.models.load_model('your_model.h5')\n# create_prediction_csv(model, '/kaggle/working/test1', 'predictions.csv')\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}