{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"'/kaggle/input/dogs-vs-cats/test1.zip'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T18:28:23.694883Z","iopub.execute_input":"2024-11-12T18:28:23.695754Z","iopub.status.idle":"2024-11-12T18:28:23.701475Z","shell.execute_reply.started":"2024-11-12T18:28:23.695686Z","shell.execute_reply":"2024-11-12T18:28:23.700535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport zipfile\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T17:56:58.165186Z","iopub.execute_input":"2024-11-13T17:56:58.165566Z","iopub.status.idle":"2024-11-13T17:56:58.169898Z","shell.execute_reply.started":"2024-11-13T17:56:58.165529Z","shell.execute_reply":"2024-11-13T17:56:58.168959Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path='/kaggle/input/dogs-vs-cats'\nfor folder in os.listdir(path)[:2]:  #train.zip and test.zip\n    zipfile.ZipFile(f\"{path}/{folder}\",).extractall(\".\")\n    print(f\"{path}/{folder}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T17:57:01.591854Z","iopub.execute_input":"2024-11-13T17:57:01.592475Z","iopub.status.idle":"2024-11-13T17:57:13.899798Z","shell.execute_reply.started":"2024-11-13T17:57:01.592436Z","shell.execute_reply":"2024-11-13T17:57:13.898893Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_images = os.listdir(\"/kaggle/working/train\")\ntrain_images[:20]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:00:01.356648Z","iopub.execute_input":"2024-11-13T18:00:01.357032Z","iopub.status.idle":"2024-11-13T18:00:01.379083Z","shell.execute_reply.started":"2024-11-13T18:00:01.356994Z","shell.execute_reply":"2024-11-13T18:00:01.378241Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lables=[]\n\nfor i in os.listdir(\"/kaggle/working/train\"):\n    if i.split('.')[0].strip()=='dog':\n       lables.append(0)\n    else:\n        lables.append(1)\n\n       \n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:00:48.225125Z","iopub.execute_input":"2024-11-13T18:00:48.225764Z","iopub.status.idle":"2024-11-13T18:00:48.259254Z","shell.execute_reply.started":"2024-11-13T18:00:48.225725Z","shell.execute_reply":"2024-11-13T18:00:48.258567Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\nlabels = to_categorical(lables, NUM_CLASSES)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:26:36.082962Z","iopub.execute_input":"2024-11-13T18:26:36.08334Z","iopub.status.idle":"2024-11-13T18:26:36.090499Z","shell.execute_reply.started":"2024-11-13T18:26:36.083307Z","shell.execute_reply":"2024-11-13T18:26:36.089509Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels.shape ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:34:02.965223Z","iopub.execute_input":"2024-11-13T18:34:02.965845Z","iopub.status.idle":"2024-11-13T18:34:02.972069Z","shell.execute_reply.started":"2024-11-13T18:34:02.965804Z","shell.execute_reply":"2024-11-13T18:34:02.971158Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:34:14.436537Z","iopub.execute_input":"2024-11-13T18:34:14.436913Z","iopub.status.idle":"2024-11-13T18:34:14.443534Z","shell.execute_reply.started":"2024-11-13T18:34:14.436876Z","shell.execute_reply":"2024-11-13T18:34:14.442537Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in os.listdir('/kaggle/working/train'):\n    print(i)\n    break","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T18:31:45.459233Z","iopub.execute_input":"2024-11-12T18:31:45.460116Z","iopub.status.idle":"2024-11-12T18:31:45.481558Z","shell.execute_reply.started":"2024-11-12T18:31:45.460073Z","shell.execute_reply":"2024-11-12T18:31:45.480548Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_img=train_images\nY_lbl=lables","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:38:57.116181Z","iopub.execute_input":"2024-11-13T18:38:57.116578Z","iopub.status.idle":"2024-11-13T18:38:57.120922Z","shell.execute_reply.started":"2024-11-13T18:38:57.11654Z","shell.execute_reply":"2024-11-13T18:38:57.119977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:01:00.691491Z","iopub.execute_input":"2024-11-13T18:01:00.692308Z","iopub.status.idle":"2024-11-13T18:01:00.699538Z","shell.execute_reply.started":"2024-11-13T18:01:00.692255Z","shell.execute_reply":"2024-11-13T18:01:00.698615Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y_lbl.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:35:31.525793Z","iopub.execute_input":"2024-11-13T18:35:31.526534Z","iopub.status.idle":"2024-11-13T18:35:31.532227Z","shell.execute_reply.started":"2024-11-13T18:35:31.52649Z","shell.execute_reply":"2024-11-13T18:35:31.531214Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.unique(Y_lbl,return_counts=True) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:39:01.516916Z","iopub.execute_input":"2024-11-13T18:39:01.517768Z","iopub.status.idle":"2024-11-13T18:39:01.527081Z","shell.execute_reply.started":"2024-11-13T18:39:01.517723Z","shell.execute_reply":"2024-11-13T18:39:01.526184Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_img[100] ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T19:25:13.319699Z","iopub.execute_input":"2024-11-12T19:25:13.320646Z","iopub.status.idle":"2024-11-12T19:25:13.327336Z","shell.execute_reply.started":"2024-11-12T19:25:13.320606Z","shell.execute_reply":"2024-11-12T19:25:13.326226Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y_lbl[100] ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:39:10.550556Z","iopub.execute_input":"2024-11-13T18:39:10.550936Z","iopub.status.idle":"2024-11-13T18:39:10.5576Z","shell.execute_reply.started":"2024-11-13T18:39:10.550898Z","shell.execute_reply":"2024-11-13T18:39:10.556625Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in X_img:\n    print(i)\n    break","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T18:30:06.709392Z","iopub.execute_input":"2024-11-12T18:30:06.710275Z","iopub.status.idle":"2024-11-12T18:30:06.715592Z","shell.execute_reply.started":"2024-11-12T18:30:06.710223Z","shell.execute_reply":"2024-11-12T18:30:06.714465Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMG_SIZE=256","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn.model_selection import train_test_split\nimport numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:01:10.609764Z","iopub.execute_input":"2024-11-13T18:01:10.610135Z","iopub.status.idle":"2024-11-13T18:01:25.427613Z","shell.execute_reply.started":"2024-11-13T18:01:10.610099Z","shell.execute_reply":"2024-11-13T18:01:25.426777Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:01:25.429403Z","iopub.execute_input":"2024-11-13T18:01:25.43006Z","iopub.status.idle":"2024-11-13T18:01:25.434487Z","shell.execute_reply.started":"2024-11-13T18:01:25.429999Z","shell.execute_reply":"2024-11-13T18:01:25.433461Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i,img in enumerate(X_img[:3]):\n    path = os.path.join(\"/kaggle/working/train\",img)\n    img = plt.imread(path)             #read the the value of each pixel in the image\n    plt.subplot(5, 2, i + 1) \n    plt.imshow(img)\n    plt.axis('off')  \n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:01:25.435602Z","iopub.execute_input":"2024-11-13T18:01:25.435958Z","iopub.status.idle":"2024-11-13T18:01:25.75118Z","shell.execute_reply.started":"2024-11-13T18:01:25.435917Z","shell.execute_reply":"2024-11-13T18:01:25.750059Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y_lbl[:3]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:28:03.919072Z","iopub.execute_input":"2024-11-13T18:28:03.919751Z","iopub.status.idle":"2024-11-13T18:28:03.925701Z","shell.execute_reply.started":"2024-11-13T18:28:03.919712Z","shell.execute_reply":"2024-11-13T18:28:03.924653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(X_img)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T19:07:31.812256Z","iopub.execute_input":"2024-11-12T19:07:31.812654Z","iopub.status.idle":"2024-11-12T19:07:31.818969Z","shell.execute_reply.started":"2024-11-12T19:07:31.812616Z","shell.execute_reply":"2024-11-12T19:07:31.817896Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMG_SIZE=(128,128)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:01:29.447652Z","iopub.execute_input":"2024-11-13T18:01:29.448048Z","iopub.status.idle":"2024-11-13T18:01:29.452149Z","shell.execute_reply.started":"2024-11-13T18:01:29.447999Z","shell.execute_reply":"2024-11-13T18:01:29.451243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nx_array = np.zeros((len(X_img), IMG_SIZE[0], IMG_SIZE[1], 3), dtype='uint8')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:35:45.295935Z","iopub.execute_input":"2024-11-13T18:35:45.296343Z","iopub.status.idle":"2024-11-13T18:35:45.30608Z","shell.execute_reply.started":"2024-11-13T18:35:45.296305Z","shell.execute_reply":"2024-11-13T18:35:45.305156Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:01:37.082823Z","iopub.execute_input":"2024-11-13T18:01:37.083216Z","iopub.status.idle":"2024-11-13T18:01:37.33439Z","shell.execute_reply.started":"2024-11-13T18:01:37.083179Z","shell.execute_reply":"2024-11-13T18:01:37.333523Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_size=64","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:01:37.505231Z","iopub.execute_input":"2024-11-13T18:01:37.50556Z","iopub.status.idle":"2024-11-13T18:01:37.512017Z","shell.execute_reply.started":"2024-11-13T18:01:37.505526Z","shell.execute_reply":"2024-11-13T18:01:37.511111Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i,img in enumerate(X_img):\n    path = os.path.join(\"/kaggle/working/train\",img)\n    img = cv2.imread(path)\n    if img is None:\n        print(f\"Error: Could not load image at path {path}\")\n    continue\n    img = cv2.resize(img, image_size) \n    img = cv2.cvtColor(img)  \n    x_array[i] = img\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:39:47.937478Z","iopub.execute_input":"2024-11-13T18:39:47.937884Z","iopub.status.idle":"2024-11-13T18:40:07.251572Z","shell.execute_reply.started":"2024-11-13T18:39:47.937844Z","shell.execute_reply":"2024-11-13T18:40:07.250552Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_array[0].shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:41:21.509023Z","iopub.execute_input":"2024-11-13T18:41:21.509435Z","iopub.status.idle":"2024-11-13T18:41:21.515491Z","shell.execute_reply.started":"2024-11-13T18:41:21.509396Z","shell.execute_reply":"2024-11-13T18:41:21.514581Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y_lbl[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:41:59.492865Z","iopub.execute_input":"2024-11-13T18:41:59.493704Z","iopub.status.idle":"2024-11-13T18:41:59.499224Z","shell.execute_reply.started":"2024-11-13T18:41:59.493664Z","shell.execute_reply":"2024-11-13T18:41:59.498223Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(x_array,Y_lbl,test_size=0.2) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:42:03.512373Z","iopub.execute_input":"2024-11-13T18:42:03.513081Z","iopub.status.idle":"2024-11-13T18:42:03.591401Z","shell.execute_reply.started":"2024-11-13T18:42:03.513031Z","shell.execute_reply":"2024-11-13T18:42:03.590602Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:42:06.238105Z","iopub.execute_input":"2024-11-13T18:42:06.238874Z","iopub.status.idle":"2024-11-13T18:42:06.244596Z","shell.execute_reply.started":"2024-11-13T18:42:06.238834Z","shell.execute_reply":"2024-11-13T18:42:06.243617Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.unique(y_train,return_counts=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:02:39.251974Z","iopub.execute_input":"2024-11-13T18:02:39.252823Z","iopub.status.idle":"2024-11-13T18:02:39.261666Z","shell.execute_reply.started":"2024-11-13T18:02:39.252783Z","shell.execute_reply":"2024-11-13T18:02:39.260711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:36:29.434962Z","iopub.execute_input":"2024-11-13T18:36:29.435653Z","iopub.status.idle":"2024-11-13T18:36:29.441458Z","shell.execute_reply.started":"2024-11-13T18:36:29.435613Z","shell.execute_reply":"2024-11-13T18:36:29.440548Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_test[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:42:13.624546Z","iopub.execute_input":"2024-11-13T18:42:13.625324Z","iopub.status.idle":"2024-11-13T18:42:13.631424Z","shell.execute_reply.started":"2024-11-13T18:42:13.625282Z","shell.execute_reply":"2024-11-13T18:42:13.630473Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMG_SIZE = (64, 64)\nx_train_resized = [cv2.resize(img, IMG_SIZE) for img in x_train]\nx_test_resized = [cv2.resize(img, IMG_SIZE) for img in x_test]\n\n# Convert lists to numpy arrays and normalize\nx_train = np.array(x_train_resized).astype('float32') \nx_test = np.array(x_test_resized).astype('float32') \n\n# Ensure y_train and y_test are numpy arrays as well\ny_train = np.array(y_train)\ny_test = np.array(y_test)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:42:50.179076Z","iopub.execute_input":"2024-11-13T18:42:50.179777Z","iopub.status.idle":"2024-11-13T18:42:51.670602Z","shell.execute_reply.started":"2024-11-13T18:42:50.179739Z","shell.execute_reply":"2024-11-13T18:42:51.669571Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.optimizers import Adam\nimport numpy as np\nimport keras\nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D,Dense,Dropout,Flatten\n\nIMG_SIZE = (64, 64)\nBATCH_SIZE = 32\nEPOCHS = 10\n\n\nmodel = models.Sequential([\n    layers.Conv2D(128, (4, 4), activation='relu', input_shape=(64, 64, 3)),\n    layers.MaxPooling2D((2, 2)),\n    \n    layers.Conv2D(256, (4, 4), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    \n    layers.Conv2D(512, (4, 4), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n\n    layers.Flatten(),\n    layers.Dense(180, activation='relu'),\n    layers.Dense(64, activation='relu'),\n    layers.Dense(1, activation='sigmoid')\n])\n\nmodel.summary()\n\n\nmodel.compile(optimizer='SGD',\n              loss='binary_crossentropy',\n              metrics=['accuracy'])\n\n\nhistory = model.fit(\n    x_train, y_train,\n    batch_size=BATCH_SIZE,\n    epochs=EPOCHS,\n    validation_data=(x_test, y_test),\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T18:58:57.668952Z","iopub.execute_input":"2024-11-13T18:58:57.669355Z","iopub.status.idle":"2024-11-13T19:01:48.38503Z","shell.execute_reply.started":"2024-11-13T18:58:57.669316Z","shell.execute_reply":"2024-11-13T19:01:48.384169Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nval_loss, val_accuracy = model.evaluate(x_test, y_test, verbose=2)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T19:19:52.101235Z","iopub.execute_input":"2024-11-12T19:19:52.101955Z","iopub.status.idle":"2024-11-12T19:19:53.267292Z","shell.execute_reply.started":"2024-11-12T19:19:52.101911Z","shell.execute_reply":"2024-11-12T19:19:53.266294Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_loss,val_accuracy","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T19:20:01.34556Z","iopub.execute_input":"2024-11-12T19:20:01.346335Z","iopub.status.idle":"2024-11-12T19:20:01.352384Z","shell.execute_reply.started":"2024-11-12T19:20:01.346295Z","shell.execute_reply":"2024-11-12T19:20:01.351253Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Predict on the test set\ny_pred = model.predict(x_test)\n# Convert probabilities to binary class predictions (0 or 1)\ny_pred_classes = (y_pred > 0.5).astype(\"int32\").flatten()\n\n# Print classification report\nprint(\"Classification Report:\")\nprint(classification_report(y_test, y_pred_classes, target_names=[\"Cat\", \"Dog\"]))\n\n# Compute confusion matrix\nconf_matrix = confusion_matrix(y_test, y_pred_classes)\n\n# Plot confusion matrix\nplt.figure(figsize=(8, 6))\nsns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=[\"Cat\", \"Dog\"], yticklabels=[\"Cat\", \"Dog\"])\nplt.xlabel('Predicted Label')\nplt.ylabel('True Label')\nplt.title('Confusion Matrix')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T19:02:03.348111Z","iopub.execute_input":"2024-11-13T19:02:03.348498Z","iopub.status.idle":"2024-11-13T19:02:05.953823Z","shell.execute_reply.started":"2024-11-13T19:02:03.348459Z","shell.execute_reply":"2024-11-13T19:02:05.952872Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}