{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30527,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# Input data files are available in the read-only \"../input/\" directory\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\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-06T10:58:13.161768Z","iopub.execute_input":"2023-12-06T10:58:13.162158Z","iopub.status.idle":"2023-12-06T10:58:13.188092Z","shell.execute_reply.started":"2023-12-06T10:58:13.162129Z","shell.execute_reply":"2023-12-06T10:58:13.187147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import models, layers\nimport matplotlib.pyplot as plt\nimport os, glob\nimport pandas as pd\nimport pandas as pd\nimport numpy as np\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-12-06T10:58:13.189626Z","iopub.execute_input":"2023-12-06T10:58:13.189912Z","iopub.status.idle":"2023-12-06T10:58:13.194904Z","shell.execute_reply.started":"2023-12-06T10:58:13.189889Z","shell.execute_reply":"2023-12-06T10:58:13.193767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_to_zip_file = \"../input/dogs-vs-cats/train.zip\"\ndirectory_to_extract_to = \"./\"\nimport zipfile\nwith zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:\n    zip_ref.extractall(directory_to_extract_to)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T10:58:13.195984Z","iopub.execute_input":"2023-12-06T10:58:13.196237Z","iopub.status.idle":"2023-12-06T10:58:21.200980Z","shell.execute_reply.started":"2023-12-06T10:58:13.196215Z","shell.execute_reply":"2023-12-06T10:58:21.200058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = tf.keras.preprocessing.image_dataset_from_directory(\"/kaggle/working/\",shuffle=True,image_size=(256,256),\n                                                          batch_size=32)\npaths = list(glob.glob('/kaggle/working'+'/**/*.*'))\nlabels = []\nfor i in range(len(paths)):\n    if \"dog\" in paths[i]:\n        labels.append(\"dog\")\n    elif \"cat\" in paths[i]:\n        labels.append(\"cat\")\n\npath = pd.Series(paths, name='Filepath').astype(str)\nlabels=pd.Series(labels, name='Label')\ntrain_data=pd.concat([path,labels],axis=1)\ntrain_data=train_data.sample(frac=1).reset_index(drop=True)\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T10:58:21.203072Z","iopub.execute_input":"2023-12-06T10:58:21.203369Z","iopub.status.idle":"2023-12-06T10:58:23.337072Z","shell.execute_reply.started":"2023-12-06T10:58:21.203344Z","shell.execute_reply":"2023-12-06T10:58:23.336031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense","metadata":{"execution":{"iopub.status.busy":"2023-12-06T10:58:23.338178Z","iopub.execute_input":"2023-12-06T10:58:23.338495Z","iopub.status.idle":"2023-12-06T10:58:23.344069Z","shell.execute_reply.started":"2023-12-06T10:58:23.338457Z","shell.execute_reply":"2023-12-06T10:58:23.343078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot as plt\nfrom matplotlib import image as mpimg\n\nfig, axes = plt.subplots(1, 5, figsize=(15,3))\n\nfor i in range(5):\n    image = mpimg.imread(train_data['Filepath'].iloc[i])\n    axes[i].imshow(image)\n    axes[i].set_title(train_data['Label'].iloc[i])\n    axes[i].axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T10:58:23.345277Z","iopub.execute_input":"2023-12-06T10:58:23.345559Z","iopub.status.idle":"2023-12-06T10:58:23.935763Z","shell.execute_reply.started":"2023-12-06T10:58:23.345535Z","shell.execute_reply":"2023-12-06T10:58:23.934767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_to_zip_file = \"../input/dogs-vs-cats/test1.zip\"\ndirectory_to_extract_to = \"./\"\nimport zipfile\nwith zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:\n    zip_ref.extractall(directory_to_extract_to)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T10:58:23.937083Z","iopub.execute_input":"2023-12-06T10:58:23.937406Z","iopub.status.idle":"2023-12-06T10:58:27.153362Z","shell.execute_reply.started":"2023-12-06T10:58:23.937378Z","shell.execute_reply":"2023-12-06T10:58:27.152405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = tf.keras.preprocessing.image_dataset_from_directory(\"/kaggle/working/\",shuffle=True,image_size=(256,256),\n                                                          batch_size=32)\npaths = list(glob.glob('/kaggle/working'+'/**/*.*'))\nlabels = []\nfor i in range(len(paths)):\n    if \"dog\" in paths[i]:\n        labels.append(\"dog\")\n    elif \"cat\" in paths[i]:\n        labels.append(\"cat\")\n    else:\n        labels.append(\"cat\")\npath = pd.Series(paths, name='Filepath').astype(str)\nlabels=pd.Series(labels, name='Label')\ntest_data=pd.concat([path,labels],axis=1)\ntest_data=test_data.sample(frac=1).reset_index(drop=True)\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T10:58:27.154542Z","iopub.execute_input":"2023-12-06T10:58:27.154887Z","iopub.status.idle":"2023-12-06T10:58:29.203590Z","shell.execute_reply.started":"2023-12-06T10:58:27.154857Z","shell.execute_reply":"2023-12-06T10:58:29.202644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the CNN model architecture\nmodel = Sequential([Conv2D(32, (2, 2), activation='relu', input_shape=(256,256,3)),\n                     MaxPooling2D((2, 2)),\n                     Conv2D(64, (2, 2), activation='relu'),\n                     MaxPooling2D((2, 2)),\n                    Conv2D(64, (2, 2), activation='relu'),\n                     MaxPooling2D((2, 2)),\n                    Conv2D(64, (2, 2), activation='relu'),\n                     MaxPooling2D((2, 2)),\n                    Conv2D(64, (2, 2), activation='relu'),\n                     MaxPooling2D((2, 2)),\n                    Conv2D(64, (2, 2), activation='relu'),\n                     MaxPooling2D((2, 2)),\n                    Conv2D(64, (2, 2), activation='relu'),\n                     MaxPooling2D((2, 2)),\n                    \n                    Flatten(),\n                    \n                   Dense(64, activation='relu'),\n                Dense(2, activation='softmax')])\nmodel.build(input_shape=(256,256,3))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T10:58:29.204751Z","iopub.execute_input":"2023-12-06T10:58:29.205029Z","iopub.status.idle":"2023-12-06T10:58:29.385586Z","shell.execute_reply.started":"2023-12-06T10:58:29.205006Z","shell.execute_reply":"2023-12-06T10:58:29.384600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-06T10:58:29.389002Z","iopub.execute_input":"2023-12-06T10:58:29.389289Z","iopub.status.idle":"2023-12-06T10:58:29.400037Z","shell.execute_reply.started":"2023-12-06T10:58:29.389264Z","shell.execute_reply":"2023-12-06T10:58:29.398980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Storing the accuracies of all the folds\ntrain_acc = []\nval_acc = []\ntest_acc = []\nprec = []\nrec = []\ntrain_loss = []\nval_loss = []","metadata":{"execution":{"iopub.status.busy":"2023-12-06T10:58:29.401097Z","iopub.execute_input":"2023-12-06T10:58:29.401350Z","iopub.status.idle":"2023-12-06T10:58:29.413649Z","shell.execute_reply.started":"2023-12-06T10:58:29.401328Z","shell.execute_reply":"2023-12-06T10:58:29.412772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen=ImageDataGenerator(rescale=1./255,zoom_range=0.5,shear_range=0.3,horizontal_flip=True)\ntest_datagen=ImageDataGenerator(rescale=1./255)\nvalidation_datagen=ImageDataGenerator(rescale=1./255)\ntest_gen=test_datagen.flow_from_dataframe(dataframe=test_data,x_col='Filepath',\n                                              y_col='Label',target_size=(256,256),class_mode='categorical',\n                                              batch_size=32, shuffle=False,seed=42)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T10:58:29.414909Z","iopub.execute_input":"2023-12-06T10:58:29.415198Z","iopub.status.idle":"2023-12-06T10:58:29.852203Z","shell.execute_reply.started":"2023-12-06T10:58:29.415175Z","shell.execute_reply":"2023-12-06T10:58:29.851422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#kfold cross validation\nfrom sklearn.model_selection import StratifiedKFold\n# Drop the rows with 'NaN' in the 'Label' column\ntrain_data = train_data.dropna(subset=['Label'])\nvalidation_datagen = ImageDataGenerator(rescale = 1./255)\nkfold = StratifiedKFold(n_splits=3,shuffle=True,random_state=42)\nj = 0 #iterating\nfor train_idx, val_idx in list(kfold.split(train_data['Filepath'],train_data['Label'])):\n    x_train_df = train_data.iloc[train_idx]\n    x_valid_df = train_data.iloc[val_idx]\n    j+=1\n\n    train_set = train_datagen.flow_from_dataframe(dataframe=x_train_df,\n                                                 x_col='Filepath',\n                                              y_col='Label',target_size=(256,256),class_mode='categorical',\n                                              batch_size=32)\n\n    valid_set = validation_datagen.flow_from_dataframe(dataframe=x_valid_df, x_col='Filepath',\n                                              y_col='Label',target_size=(256,256),class_mode='categorical',\n                                              batch_size=32)\n    history = model.fit(train_set, epochs=15,steps_per_epoch=200, batch_size=32, validation_data = valid_set, verbose=1)\n    predictions = model.predict(test_gen)\n    y_pred = [np.argmax(predictions[i]) for i in range(37500)]\n    y_true = test_gen.labels\n    train_acc.append(history.history['accuracy'])\n    val_acc.append(history.history['val_accuracy'])\n    test_acc.append(model.evaluate(test_gen))\n    #precision values for every fold\n    m = tf.keras.metrics.Precision()\n    m.update_state(y_true,y_pred)\n    prec.append(m.result().numpy())\n    #Recall values for every fold\n    m1 = tf.keras.metrics.Recall()\n    m1.update_state(y_true,y_pred)\n    rec.append(m1.result().numpy())\n    train_loss.append(history.history['loss'])\n    val_loss.append(history.history['val_loss'])","metadata":{"execution":{"iopub.status.busy":"2023-12-06T11:12:03.488693Z","iopub.execute_input":"2023-12-06T11:12:03.489109Z","iopub.status.idle":"2023-12-06T12:43:58.700308Z","shell.execute_reply.started":"2023-12-06T11:12:03.489079Z","shell.execute_reply":"2023-12-06T12:43:58.699390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_acc,val_acc, test_acc,prec, rec ,val_loss,train_loss","metadata":{"execution":{"iopub.status.busy":"2023-12-06T12:43:58.702307Z","iopub.execute_input":"2023-12-06T12:43:58.702934Z","iopub.status.idle":"2023-12-06T12:43:58.712176Z","shell.execute_reply.started":"2023-12-06T12:43:58.702898Z","shell.execute_reply":"2023-12-06T12:43:58.711316Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/dogs-vs-cats_model.h5',save_format='h5')","metadata":{"execution":{"iopub.status.busy":"2023-12-06T12:43:58.713280Z","iopub.execute_input":"2023-12-06T12:43:58.713636Z","iopub.status.idle":"2023-12-06T12:43:58.785504Z","shell.execute_reply.started":"2023-12-06T12:43:58.713613Z","shell.execute_reply":"2023-12-06T12:43:58.784707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Average of all metrics for the 3 folds\navg_train_acc = np.mean(train_acc)\navg_test_acc = np.mean(test_acc)\navg_val_acc = np.mean(val_acc)\navg_prec = np.mean(prec)\navg_rec = np.mean(rec)\navg_val_loss = np.mean(val_loss)\navg_train_loss = np.mean(train_loss)\n\n#print all the metrics\nprint(\"Average Train accuracy of all three models\", avg_train_acc)\nprint(\"Average Test accuracy of all three models\",avg_test_acc)\nprint(\"Average Validation accuracy of all three models\",avg_val_acc)\nprint(\"Average Precision of all three models\",avg_prec)\nprint(\"Average Recall of all three models\",avg_rec)\nprint(\"Average validation loss of all three models\",avg_val_loss)\nprint(\"Average Training loss of all three models\",avg_train_loss)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T12:43:58.787492Z","iopub.execute_input":"2023-12-06T12:43:58.787803Z","iopub.status.idle":"2023-12-06T12:43:58.795247Z","shell.execute_reply.started":"2023-12-06T12:43:58.787777Z","shell.execute_reply":"2023-12-06T12:43:58.794350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plot train vs validation accuracies graph and train vs validation loss graph\nacc = np.mean(np.array(train_acc),axis=0)\nval_acc = np.mean(np.array(val_acc),axis=0)\nloss=np.mean(np.array(train_loss),axis=0)\nval_loss=np.mean(np.array(val_loss),axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T12:43:58.796757Z","iopub.execute_input":"2023-12-06T12:43:58.797402Z","iopub.status.idle":"2023-12-06T12:43:58.815511Z","shell.execute_reply.started":"2023-12-06T12:43:58.797368Z","shell.execute_reply":"2023-12-06T12:43:58.814678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,6))\nplt.subplot(1,2,1)\nplt.plot(range(15),acc,label='Training accuracy')\nplt.plot(range(15),val_acc, label='Validation accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1,2,2)\nplt.plot(range(15),loss,label='Training loss')\nplt.plot(range(15),val_loss, label='Validation loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T12:43:58.816579Z","iopub.execute_input":"2023-12-06T12:43:58.816895Z","iopub.status.idle":"2023-12-06T12:43:59.271149Z","shell.execute_reply.started":"2023-12-06T12:43:58.816871Z","shell.execute_reply":"2023-12-06T12:43:59.270178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names=['cat','dog']\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator\ndef prediction(path):\n    img = load_img(path,target_size=(256,256))\n    i = img_to_array(img)\n    img = np.expand_dims(i,axis=0)\n    pred = np.argmax(model.predict(img))\n    return class_names[pred]\nprediction(test_data.iloc[2,0])","metadata":{"execution":{"iopub.status.busy":"2023-12-06T13:03:48.841505Z","iopub.execute_input":"2023-12-06T13:03:48.842240Z","iopub.status.idle":"2023-12-06T13:03:48.908254Z","shell.execute_reply.started":"2023-12-06T13:03:48.842207Z","shell.execute_reply":"2023-12-06T13:03:48.907268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the image path\nimage_path = test_data.iloc[10,0]\n# Get the true label\ntrue_label = test_data.iloc[10,1]\n# Predict the label\npredicted_label = prediction(image_path)\n# Load and display the image\nimg = mpimg.imread(image_path)\nplt.imshow(img)\nplt.axis('off')\n# Add the true and predicted labels as the title\nplt.title(f'True Label: {true_label}, Predicted Label: {predicted_label}')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T13:05:57.024595Z","iopub.execute_input":"2023-12-06T13:05:57.025313Z","iopub.status.idle":"2023-12-06T13:05:57.384660Z","shell.execute_reply.started":"2023-12-06T13:05:57.025278Z","shell.execute_reply":"2023-12-06T13:05:57.383710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}