{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.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":31234,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from zipfile import ZipFile\n\ndataset = '/kaggle/input/dogs-vs-cats/train.zip'\n\nwith ZipFile(dataset, 'r') as zip:\n    zip.extractall('/kaggle/working')\n\nprint(\"Extracted successfully\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T15:42:00.285522Z","iopub.execute_input":"2026-01-12T15:42:00.285921Z","iopub.status.idle":"2026-01-12T15:42:07.574562Z","shell.execute_reply.started":"2026-01-12T15:42:00.28589Z","shell.execute_reply":"2026-01-12T15:42:07.573692Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_names = os.listdir('/kaggle/working/train')\nprint(file_names[10:15])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T15:42:07.576469Z","iopub.execute_input":"2026-01-12T15:42:07.576772Z","iopub.status.idle":"2026-01-12T15:42:07.595555Z","shell.execute_reply.started":"2026-01-12T15:42:07.576746Z","shell.execute_reply":"2026-01-12T15:42:07.594613Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\nfrom google.colab.patches import cv2_imshow","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T15:42:07.596647Z","iopub.execute_input":"2026-01-12T15:42:07.596947Z","iopub.status.idle":"2026-01-12T15:42:08.133293Z","shell.execute_reply.started":"2026-01-12T15:42:07.59692Z","shell.execute_reply":"2026-01-12T15:42:08.132377Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img = mpimg.imread('/kaggle/working/train/dog.9815.jpg')\nimgplt = plt.imshow(img)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T15:42:08.134601Z","iopub.execute_input":"2026-01-12T15:42:08.13492Z","iopub.status.idle":"2026-01-12T15:42:08.37834Z","shell.execute_reply.started":"2026-01-12T15:42:08.134884Z","shell.execute_reply":"2026-01-12T15:42:08.377459Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img = mpimg.imread('/kaggle/working/train/cat.10544.jpg')\nimgplt = plt.imshow(img)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T15:42:08.380495Z","iopub.execute_input":"2026-01-12T15:42:08.380838Z","iopub.status.idle":"2026-01-12T15:42:08.570164Z","shell.execute_reply.started":"2026-01-12T15:42:08.380789Z","shell.execute_reply":"2026-01-12T15:42:08.569171Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_names = os.listdir('/kaggle/working/train')\n\nfor i in range(5):\n    name = file_names[i]\n    print(name[0:3])\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T15:42:08.571131Z","iopub.execute_input":"2026-01-12T15:42:08.571414Z","iopub.status.idle":"2026-01-12T15:42:08.590521Z","shell.execute_reply.started":"2026-01-12T15:42:08.571389Z","shell.execute_reply":"2026-01-12T15:42:08.589632Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_names = os.listdir('/kaggle/working/train')\n\ndog_count = 0\ncat_count = 0\n\nfor img_file in file_names:\n    name = img_file[0:3]\n\n    if name == 'dog':\n        dog_count += 1\n    else:\n        cat_count += 1\n\nprint(\"Total dog count is \", dog_count)\nprint(\"Total cat count is \", cat_count)\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T15:42:08.591537Z","iopub.execute_input":"2026-01-12T15:42:08.591838Z","iopub.status.idle":"2026-01-12T15:42:08.617907Z","shell.execute_reply.started":"2026-01-12T15:42:08.591784Z","shell.execute_reply":"2026-01-12T15:42:08.616964Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T15:42:08.619055Z","iopub.execute_input":"2026-01-12T15:42:08.61941Z","iopub.status.idle":"2026-01-12T15:42:08.623539Z","shell.execute_reply.started":"2026-01-12T15:42:08.619376Z","shell.execute_reply":"2026-01-12T15:42:08.622634Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"original_folder = '/kaggle/working/train/'\nresized_folder = '/kaggle/working/image resized'\n\nfor i in range(2000):\n\n    filename = os.listdir(original_folder)[i]\n    img_path = original_folder + filename \n\n    img  = Image.open(img_path)\n    img = img.resize((224,224))\n    img = img.convert('RGB')\n\n    newImgPath = resized_folder + filename\n    img.save(newImgPath)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T15:42:08.624649Z","iopub.execute_input":"2026-01-12T15:42:08.625443Z","iopub.status.idle":"2026-01-12T15:42:40.743592Z","shell.execute_reply.started":"2026-01-12T15:42:08.625414Z","shell.execute_reply":"2026-01-12T15:42:40.742708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img = mpimg.imread('/kaggle/working/image resizedcat.10544.jpg')\nimgplt = plt.imshow(img)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T15:44:47.773373Z","iopub.execute_input":"2026-01-12T15:44:47.773744Z","iopub.status.idle":"2026-01-12T15:44:47.965233Z","shell.execute_reply.started":"2026-01-12T15:44:47.773714Z","shell.execute_reply":"2026-01-12T15:44:47.964296Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img = mpimg.imread('/kaggle/working/image resizeddog.10787.jpg')\nimgplt = plt.imshow(img)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T15:46:29.579262Z","iopub.execute_input":"2026-01-12T15:46:29.579935Z","iopub.status.idle":"2026-01-12T15:46:29.767259Z","shell.execute_reply.started":"2026-01-12T15:46:29.579903Z","shell.execute_reply":"2026-01-12T15:46:29.766113Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filenames = os.listdir('/kaggle/working/')\n\nlabels = []\n\nfor i in range(2000):\n    file_name = filenames[i]\n    if 'dog' in file_name:\n        labels.append(1)\n    elif 'cat' in file_name:\n        labels.append(0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T16:35:51.514729Z","iopub.execute_input":"2026-01-12T16:35:51.515129Z","iopub.status.idle":"2026-01-12T16:35:51.522755Z","shell.execute_reply.started":"2026-01-12T16:35:51.515099Z","shell.execute_reply":"2026-01-12T16:35:51.522044Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(filenames[0:5])\nprint(len(filenames))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T16:35:56.015351Z","iopub.execute_input":"2026-01-12T16:35:56.015698Z","iopub.status.idle":"2026-01-12T16:35:56.021144Z","shell.execute_reply.started":"2026-01-12T16:35:56.01567Z","shell.execute_reply":"2026-01-12T16:35:56.020148Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(labels[0:5])\nprint(len(labels))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T16:35:58.340644Z","iopub.execute_input":"2026-01-12T16:35:58.341002Z","iopub.status.idle":"2026-01-12T16:35:58.346364Z","shell.execute_reply.started":"2026-01-12T16:35:58.340974Z","shell.execute_reply":"2026-01-12T16:35:58.345415Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"values , counts = np.unique(labels,return_counts = True)\nprint(values)\nprint(counts)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T16:42:19.673569Z","iopub.execute_input":"2026-01-12T16:42:19.675569Z","iopub.status.idle":"2026-01-12T16:42:19.687827Z","shell.execute_reply.started":"2026-01-12T16:42:19.675432Z","shell.execute_reply":"2026-01-12T16:42:19.686697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport glob","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T16:43:19.181161Z","iopub.execute_input":"2026-01-12T16:43:19.18205Z","iopub.status.idle":"2026-01-12T16:43:19.186347Z","shell.execute_reply.started":"2026-01-12T16:43:19.182013Z","shell.execute_reply":"2026-01-12T16:43:19.185491Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_directory = '/kaggle/working/'\nimage_extension = ['png','jpg']\n\nfiles = []\n\n[files.extend(glob.glob(image_directory + '*.' + e)) for e in image_extension]\n\ndog_cat_images = np.asarray([cv2.imread(file) for file in files])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T16:45:19.236984Z","iopub.execute_input":"2026-01-12T16:45:19.237376Z","iopub.status.idle":"2026-01-12T16:45:20.529969Z","shell.execute_reply.started":"2026-01-12T16:45:19.237344Z","shell.execute_reply":"2026-01-12T16:45:20.529176Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(dog_cat_images)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T16:45:36.54494Z","iopub.execute_input":"2026-01-12T16:45:36.545835Z","iopub.status.idle":"2026-01-12T16:45:36.553663Z","shell.execute_reply.started":"2026-01-12T16:45:36.545775Z","shell.execute_reply":"2026-01-12T16:45:36.552748Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"type(dog_cat_images)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T16:45:55.560244Z","iopub.execute_input":"2026-01-12T16:45:55.562144Z","iopub.status.idle":"2026-01-12T16:45:55.5752Z","shell.execute_reply.started":"2026-01-12T16:45:55.562084Z","shell.execute_reply":"2026-01-12T16:45:55.574238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(dog_cat_images.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T16:46:11.104612Z","iopub.execute_input":"2026-01-12T16:46:11.105336Z","iopub.status.idle":"2026-01-12T16:46:11.110835Z","shell.execute_reply.started":"2026-01-12T16:46:11.105301Z","shell.execute_reply":"2026-01-12T16:46:11.109765Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(len(dog_cat_images))\nprint(len(labels))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T16:54:47.382081Z","iopub.execute_input":"2026-01-12T16:54:47.382519Z","iopub.status.idle":"2026-01-12T16:54:47.388307Z","shell.execute_reply.started":"2026-01-12T16:54:47.382488Z","shell.execute_reply":"2026-01-12T16:54:47.387365Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"min_len = min(len(dog_cat_images), len(labels))\n\nX = np.array(dog_cat_images[:min_len])\nY = np.array(labels[:min_len])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T16:56:17.937001Z","iopub.execute_input":"2026-01-12T16:56:17.937435Z","iopub.status.idle":"2026-01-12T16:56:18.562505Z","shell.execute_reply.started":"2026-01-12T16:56:17.937401Z","shell.execute_reply":"2026-01-12T16:56:18.561556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size=0.2,random_state=2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T16:56:25.67286Z","iopub.execute_input":"2026-01-12T16:56:25.673724Z","iopub.status.idle":"2026-01-12T16:56:25.835594Z","shell.execute_reply.started":"2026-01-12T16:56:25.673682Z","shell.execute_reply":"2026-01-12T16:56:25.83466Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(X.shape,X_train.shape,X_test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T16:56:57.287109Z","iopub.execute_input":"2026-01-12T16:56:57.287447Z","iopub.status.idle":"2026-01-12T16:56:57.293323Z","shell.execute_reply.started":"2026-01-12T16:56:57.287419Z","shell.execute_reply":"2026-01-12T16:56:57.292346Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_trained_scaled = X_train/255\n\nX_test_scaled = X_test/255","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T17:03:57.537852Z","iopub.execute_input":"2026-01-12T17:03:57.538856Z","iopub.status.idle":"2026-01-12T17:03:58.789516Z","shell.execute_reply.started":"2026-01-12T17:03:57.538816Z","shell.execute_reply":"2026-01-12T17:03:58.788499Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(X_trained_scaled)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T17:04:20.536868Z","iopub.execute_input":"2026-01-12T17:04:20.537923Z","iopub.status.idle":"2026-01-12T17:04:20.557581Z","shell.execute_reply.started":"2026-01-12T17:04:20.537859Z","shell.execute_reply":"2026-01-12T17:04:20.556393Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_hub as hub","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T17:05:19.282392Z","iopub.execute_input":"2026-01-12T17:05:19.282785Z","iopub.status.idle":"2026-01-12T17:05:41.173693Z","shell.execute_reply.started":"2026-01-12T17:05:19.282754Z","shell.execute_reply":"2026-01-12T17:05:41.172736Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mobilenet_model = 'https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T17:05:59.465918Z","iopub.execute_input":"2026-01-12T17:05:59.466534Z","iopub.status.idle":"2026-01-12T17:05:59.471132Z","shell.execute_reply.started":"2026-01-12T17:05:59.466503Z","shell.execute_reply":"2026-01-12T17:05:59.47007Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pretrained_model = hub.KerasLayer(mobilenet_model,input_shape=(224,224,3),trainable=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T17:07:28.213047Z","iopub.execute_input":"2026-01-12T17:07:28.213434Z","iopub.status.idle":"2026-01-12T17:07:29.886703Z","shell.execute_reply.started":"2026-01-12T17:07:28.213405Z","shell.execute_reply":"2026-01-12T17:07:29.885872Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install tf_keras\nimport tf_keras","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T17:10:05.16974Z","iopub.execute_input":"2026-01-12T17:10:05.170112Z","iopub.status.idle":"2026-01-12T17:10:10.751212Z","shell.execute_reply.started":"2026-01-12T17:10:05.170077Z","shell.execute_reply":"2026-01-12T17:10:10.749905Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_of_classes = 2\n\nmodel = tf_keras.Sequential([\n\n    pretrained_model,\n    tf_keras.layers.Dense(num_of_classes)\n\n])\n\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T17:10:46.422986Z","iopub.execute_input":"2026-01-12T17:10:46.423376Z","iopub.status.idle":"2026-01-12T17:10:47.044161Z","shell.execute_reply.started":"2026-01-12T17:10:46.423341Z","shell.execute_reply":"2026-01-12T17:10:47.042789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(\n    optimizer = 'adam',\n    loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n    metrics = ['acc']\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T17:12:28.894124Z","iopub.execute_input":"2026-01-12T17:12:28.894498Z","iopub.status.idle":"2026-01-12T17:12:28.920569Z","shell.execute_reply.started":"2026-01-12T17:12:28.894465Z","shell.execute_reply":"2026-01-12T17:12:28.919561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(X_trained_scaled,Y_train,epochs=5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T17:15:53.173472Z","iopub.execute_input":"2026-01-12T17:15:53.173863Z","iopub.status.idle":"2026-01-12T17:18:04.964136Z","shell.execute_reply.started":"2026-01-12T17:15:53.173834Z","shell.execute_reply":"2026-01-12T17:18:04.963201Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score,acc = model.evaluate(X_test_scaled,Y_test)\nprint(\"Test loss\",score)\nprint(\"Test Accuracy\",acc)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T17:19:56.906896Z","iopub.execute_input":"2026-01-12T17:19:56.90726Z","iopub.status.idle":"2026-01-12T17:20:06.487575Z","shell.execute_reply.started":"2026-01-12T17:19:56.907235Z","shell.execute_reply":"2026-01-12T17:20:06.486761Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"input_image_path = input('Path of the image to be predicted: ')\n\ninput_image = cv2.imread(input_image_path)\n\ncv2_imshow(input_image)\n\ninput_image_resize = cv2.resize(input_image, (224,224))\n\ninput_image_scaled = input_image_resize/255\n\nimage_reshaped = np.reshape(input_image_scaled, [1,224,224,3])\n\ninput_prediction = model.predict(image_reshaped)\n\nprint(input_prediction)\n\ninput_pred_label = np.argmax(input_prediction)\n\nprint(input_pred_label)\n\nif input_pred_label == 0:\n  print('The image represents a Cat')\n\nelse:\n  print('The image represents a Dog')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T17:22:57.689452Z","iopub.execute_input":"2026-01-12T17:22:57.689857Z","iopub.status.idle":"2026-01-12T17:23:25.345542Z","shell.execute_reply.started":"2026-01-12T17:22:57.689826Z","shell.execute_reply":"2026-01-12T17:23:25.344589Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"input_image_path = input('Path of the image to be predicted: ')\n\ninput_image = cv2.imread(input_image_path)\n\ncv2_imshow(input_image)\n\ninput_image_resize = cv2.resize(input_image, (224,224))\n\ninput_image_scaled = input_image_resize/255\n\nimage_reshaped = np.reshape(input_image_scaled, [1,224,224,3])\n\ninput_prediction = model.predict(image_reshaped)\n\nprint(input_prediction)\n\ninput_pred_label = np.argmax(input_prediction)\n\nprint(input_pred_label)\n\nif input_pred_label == 0:\n  print('The image represents a Cat')\n\nelse:\n  print('The image represents a Dog')\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T17:24:06.130296Z","iopub.execute_input":"2026-01-12T17:24:06.130654Z","iopub.status.idle":"2026-01-12T17:24:12.676573Z","shell.execute_reply.started":"2026-01-12T17:24:06.130625Z","shell.execute_reply":"2026-01-12T17:24:12.675868Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}