{"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":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Deep Learning Assignment: Convolutional Neural Network**","metadata":{}},{"cell_type":"markdown","source":"### ***Submitted By: Arya B***","metadata":{}},{"cell_type":"markdown","source":"# Implementation of CNN for classification of Dogs and Cats","metadata":{}},{"cell_type":"markdown","source":"### **Introduction to Convolutional Neural Networks (CNNs)**\nConvolutional Neural Networks (CNNs) represent a powerful class of deep learning models specifically designed for image processing tasks, such as image classification and pattern recognition. In this notebook, we will embark on a foundational exploration of CNNs, focusing on their core principles and practical implementation.\n\n**CNN Architecture:**\n\nWe will delve into the architecture of CNNs, understanding the role of convolutional layers, pooling layers, and fully connected layers. This exploration will provide insights into how CNNs hierarchically extract features from input images.\n**Practical Implementation:**\n\nA step-by-step guide will be provided for implementing a basic CNN using a popular deep learning framework, emphasizing hands-on experience and code implementation. The example will focus on image classification tasks.\n**Training and Evaluation:**\n\nLearn how to train a CNN model using a dataset, evaluate its performance, and interpret key metrics. This section aims to provide a practical understanding of the training process and model evaluation.","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, Conv2D, MaxPooling2D","metadata":{"execution":{"iopub.status.busy":"2023-12-04T03:45:04.634410Z","iopub.execute_input":"2023-12-04T03:45:04.634842Z","iopub.status.idle":"2023-12-04T03:45:21.333464Z","shell.execute_reply.started":"2023-12-04T03:45:04.634807Z","shell.execute_reply":"2023-12-04T03:45:21.331933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nprint(os.listdir(\"../input\"))\n\nimport zipfile\n\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2023-12-04T03:48:43.878051Z","iopub.execute_input":"2023-12-04T03:48:43.878566Z","iopub.status.idle":"2023-12-04T03:49:04.819225Z","shell.execute_reply.started":"2023-12-04T03:48:43.878527Z","shell.execute_reply":"2023-12-04T03:49:04.817664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_dir = \"/kaggle/working/\"\ntrain_dir = \"train\"\npath = os.path.join(main_dir,train_dir)\n\nfor p in os.listdir(path):\n    category = p.split(\".\")[0]\n    img_array = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE)\n    new_img_array = cv2.resize(img_array, dsize=(80, 80))\n    plt.imshow(new_img_array,cmap=\"gray\")\n    break","metadata":{"execution":{"iopub.status.busy":"2023-12-04T03:49:06.453739Z","iopub.execute_input":"2023-12-04T03:49:06.454257Z","iopub.status.idle":"2023-12-04T03:49:06.869553Z","shell.execute_reply.started":"2023-12-04T03:49:06.454210Z","shell.execute_reply":"2023-12-04T03:49:06.868112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = []\ny = []\nconvert = lambda category : int(category == 'dog')\ndef create_test_data(path):\n    for p in os.listdir(path):\n        category = p.split(\".\")[0]\n        category = convert(category)\n        img_array = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE)\n        new_img_array = cv2.resize(img_array, dsize=(80, 80))\n        X.append(new_img_array)\n        y.append(category)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-12-04T03:49:25.322802Z","iopub.execute_input":"2023-12-04T03:49:25.324108Z","iopub.status.idle":"2023-12-04T03:49:25.333736Z","shell.execute_reply.started":"2023-12-04T03:49:25.324049Z","shell.execute_reply":"2023-12-04T03:49:25.332067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_test_data(path)\nX = np.array(X).reshape(-1, 80,80,1)\ny = np.array(y)","metadata":{"execution":{"iopub.status.busy":"2023-12-04T03:49:36.375093Z","iopub.execute_input":"2023-12-04T03:49:36.376380Z","iopub.status.idle":"2023-12-04T03:50:03.075999Z","shell.execute_reply.started":"2023-12-04T03:49:36.376334Z","shell.execute_reply":"2023-12-04T03:50:03.074864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalize data\nX = X/255.0","metadata":{"execution":{"iopub.status.busy":"2023-12-04T03:50:03.077887Z","iopub.execute_input":"2023-12-04T03:50:03.078260Z","iopub.status.idle":"2023-12-04T03:50:03.718551Z","shell.execute_reply.started":"2023-12-04T03:50:03.078230Z","shell.execute_reply":"2023-12-04T03:50:03.717453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **CNN model**\n\n**Define a Sequential model**\n\nStart adding layers to it.\nFirst we will add a Conv2D layer with 64 nodes and kernel size of (3,3). You can also experiment with different values here like 32, 128 etc. Also we have to specify input shape which is your X shape. Activation we will take 'relu' for now however there are many others to experiment with.\nNow after every Conv layer we always do max pooling so we will add max pooling layer with a size of (2,2)\nWe will repeat this combination again because come on 2 is better than one. Haha. We you can also add 3 or more convolution layers but keep in mind the more layers you add more time it will take to train.\nBut we don't have much time so we will add a flatten layer now. As we have to feed our data to Dense layer later.\nWe will now add a Dense layer of 64 nodes. Note for all these layers we are using activation as 'relu' because I found results better with this. You can skip specifying activation but this might make a model a conveniently linear which might not work for us.\nIn the end for getting our result we will add final Dense layer . Activation can be sigmoid or softmax (if you need probability use sigmoid else use softmax). Here I have used sigmoid.\nFinally we will compile the model . \nThere are 3 things to mention here . Loss, Optimizer, Metrics\n\n**Loss** :- To make our model better we either minimize loss or maximize accuracy. NN always minimize loss. To measure it we can use different formulas like 'categorical_crossentropy' or 'binary_crossentropy'. Here I have used binary_crossentropy\n\n**Optimizer** :- If you know a lil bit about mathematics of machine learning you might be familier with local minima or global minima or cost function. To minimize cost function we use different methods For ex :- like gradient descent, stochastic gradient descent. So these are call optimizers. We are using a default one here which is adam\n\n**Metrics** :- This is to denote the measure of your model. Can be accuracy or some other metric.","metadata":{}},{"cell_type":"code","source":"model = Sequential()\n# Adds a densely-connected layer with 64 units to the model:\nmodel.add(Conv2D(64,(3,3), activation = 'relu', input_shape = X.shape[1:]))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n# Add another:\nmodel.add(Conv2D(64,(3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(64, activation='relu'))\n# Add a softmax layer with 10 output units:\nmodel.add(Dense(1, activation='sigmoid'))\n\nmodel.compile(optimizer=\"adam\",\n              loss='binary_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-04T03:51:32.130793Z","iopub.execute_input":"2023-12-04T03:51:32.131309Z","iopub.status.idle":"2023-12-04T03:51:32.412160Z","shell.execute_reply.started":"2023-12-04T03:51:32.131274Z","shell.execute_reply":"2023-12-04T03:51:32.410736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X, y, epochs=20, batch_size=32, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-12-04T03:51:43.563208Z","iopub.execute_input":"2023-12-04T03:51:43.563620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_val_accuracy = model.history.history['val_accuracy'][-1]\nprint(f\"Final Validation Accuracy: {final_val_accuracy * 100:.2f}%\")\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = \"test1\"\npath = os.path.join(main_dir,train_dir)\n#os.listdir(path)\n\nX_test = []\nid_line = []\ndef create_test1_data(path):\n    for p in os.listdir(path):\n        id_line.append(p.split(\".\")[0])\n        img_array = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE)\n        new_img_array = cv2.resize(img_array, dsize=(80, 80))\n        X_test.append(new_img_array)\ncreate_test1_data(path)\nX_test = np.array(X_test).reshape(-1,80,80,1)\nX_test = X_test/255","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_val = [int(round(p[0])) for p in predictions]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_and_predictions = list(zip(id_line, predicted_val))\nprint(id_and_predictions)","metadata":{},"execution_count":null,"outputs":[]}]}