{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"1. [Preparing Data](#1)\n    * [Resizing all the images](#2)\n    * [Converting all the resized images to numpy arrays](#3)\n1. [Training](#4)\n    * [Building the Neural Network](#5)\n    * [ Evaluating model](#6)\n1. [Predictive System](#7)","metadata":{}},{"cell_type":"markdown","source":"<a id = \"1\"></a><br>\n# Preparing Data","metadata":{}},{"cell_type":"code","source":"# extracting the compressed dataset\nfrom zipfile import ZipFile\n\ndataset = '/kaggle/input/dogs-vs-cats/train.zip'\n\nwith ZipFile(dataset, 'r') as zip:\n  zip.extractall()\n  print('The dataset is extracted')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-04T14:23:40.248179Z","iopub.execute_input":"2023-05-04T14:23:40.248697Z","iopub.status.idle":"2023-05-04T14:23:48.563335Z","shell.execute_reply.started":"2023-05-04T14:23:40.248643Z","shell.execute_reply":"2023-05-04T14:23:48.562361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n# counting the number of files in train folder\npath, dirs, files = next(os.walk('/kaggle/working/train'))\nfile_count = len(files)\nprint('Number of images: ', file_count)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:23:48.565249Z","iopub.execute_input":"2023-05-04T14:23:48.565551Z","iopub.status.idle":"2023-05-04T14:23:48.594078Z","shell.execute_reply.started":"2023-05-04T14:23:48.565526Z","shell.execute_reply":"2023-05-04T14:23:48.592880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_names = os.listdir('/kaggle/working/train')","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:23:48.595428Z","iopub.execute_input":"2023-05-04T14:23:48.596444Z","iopub.status.idle":"2023-05-04T14:23:48.615219Z","shell.execute_reply.started":"2023-05-04T14:23:48.596403Z","shell.execute_reply":"2023-05-04T14:23:48.614301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:23:48.619752Z","iopub.execute_input":"2023-05-04T14:23:48.620727Z","iopub.status.idle":"2023-05-04T14:23:48.625914Z","shell.execute_reply.started":"2023-05-04T14:23:48.620689Z","shell.execute_reply":"2023-05-04T14:23:48.624714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display dog image\nimg = mpimg.imread('/kaggle/working/train/dog.8298.jpg')\nimgplt = plt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:23:48.627211Z","iopub.execute_input":"2023-05-04T14:23:48.627547Z","iopub.status.idle":"2023-05-04T14:23:48.882808Z","shell.execute_reply.started":"2023-05-04T14:23:48.627518Z","shell.execute_reply":"2023-05-04T14:23:48.881799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display cat image\nimg = mpimg.imread('/kaggle/working/train/cat.4352.jpg')\nimgplt = plt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:23:48.884411Z","iopub.execute_input":"2023-05-04T14:23:48.884800Z","iopub.status.idle":"2023-05-04T14:23:49.187210Z","shell.execute_reply.started":"2023-05-04T14:23:48.884766Z","shell.execute_reply":"2023-05-04T14:23:49.186106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# looking to file names\nfile_names = os.listdir('/kaggle/working/train')\n\nfor i in range(5):\n\n  name = file_names[i] \n  print(name[0:3])","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:23:49.188936Z","iopub.execute_input":"2023-05-04T14:23:49.189637Z","iopub.status.idle":"2023-05-04T14:23:49.211021Z","shell.execute_reply.started":"2023-05-04T14:23:49.189600Z","shell.execute_reply":"2023-05-04T14:23:49.209961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#counting dogs and cats\nfile_names = os.listdir('/kaggle/working/train/')\n\ndog_count = 0\ncat_count = 0\n\nfor img_file in file_names:\n\n  name = img_file[0:3]\n\n  if name == 'dog':\n    dog_count += 1\n\n  else:\n    cat_count += 1\n\nprint('Number of dog images =', dog_count)\nprint('Number of cat images =', cat_count)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:23:49.212236Z","iopub.execute_input":"2023-05-04T14:23:49.212556Z","iopub.status.idle":"2023-05-04T14:23:49.239842Z","shell.execute_reply.started":"2023-05-04T14:23:49.212529Z","shell.execute_reply":"2023-05-04T14:23:49.238850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"2\"></a><br>\n# Resizing all the images","metadata":{}},{"cell_type":"code","source":"#creating a directory for resized images \nos.mkdir('/kaggle/working/resized_img')","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:23:49.241179Z","iopub.execute_input":"2023-05-04T14:23:49.241502Z","iopub.status.idle":"2023-05-04T14:23:49.264137Z","shell.execute_reply.started":"2023-05-04T14:23:49.241474Z","shell.execute_reply":"2023-05-04T14:23:49.262294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original_folder = '/kaggle/working/train'\nresized_folder = '/kaggle/working/resized_img'\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":{"execution":{"iopub.status.busy":"2023-05-04T15:25:44.535609Z","iopub.execute_input":"2023-05-04T15:25:44.536054Z","iopub.status.idle":"2023-05-04T15:26:20.011311Z","shell.execute_reply.started":"2023-05-04T15:25:44.536022Z","shell.execute_reply":"2023-05-04T15:26:20.010165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creaing a for loop to assign labels\nfilenames = os.listdir('/kaggle/working/resized_img/')\n\n\nlabels = []\n\nfor i in range(2000):\n\n  file_name = filenames[i]\n  label = file_name[0:3]\n\n  if label == 'dog':\n    labels.append(1)\n\n  else:\n    labels.append(0)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:26:48.255186Z","iopub.execute_input":"2023-05-04T14:26:48.255581Z","iopub.status.idle":"2023-05-04T14:26:48.263539Z","shell.execute_reply.started":"2023-05-04T14:26:48.255549Z","shell.execute_reply":"2023-05-04T14:26:48.262565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(filenames[0:5])\nprint(len(filenames))","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:26:55.494481Z","iopub.execute_input":"2023-05-04T14:26:55.494845Z","iopub.status.idle":"2023-05-04T14:26:55.500317Z","shell.execute_reply.started":"2023-05-04T14:26:55.494818Z","shell.execute_reply":"2023-05-04T14:26:55.499141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels[0:5])\nprint(len(labels))","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:27:10.616910Z","iopub.execute_input":"2023-05-04T14:27:10.617280Z","iopub.status.idle":"2023-05-04T14:27:10.622581Z","shell.execute_reply.started":"2023-05-04T14:27:10.617251Z","shell.execute_reply":"2023-05-04T14:27:10.621611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# counting the images of dogs and cats out of 2000 images\nvalues, counts = np.unique(labels, return_counts=True)\nprint(values)\nprint(counts)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:27:16.077412Z","iopub.execute_input":"2023-05-04T14:27:16.078448Z","iopub.status.idle":"2023-05-04T14:27:16.084259Z","shell.execute_reply.started":"2023-05-04T14:27:16.078411Z","shell.execute_reply":"2023-05-04T14:27:16.083110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"3\"></a><br>\n# Converting all the resized images to numpy arrays","metadata":{}},{"cell_type":"code","source":"import cv2\nimport glob","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:27:31.030237Z","iopub.execute_input":"2023-05-04T14:27:31.030593Z","iopub.status.idle":"2023-05-04T14:27:31.211254Z","shell.execute_reply.started":"2023-05-04T14:27:31.030567Z","shell.execute_reply":"2023-05-04T14:27:31.209831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_directory = '/kaggle/working/resized_img/'\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":{"execution":{"iopub.status.busy":"2023-05-04T14:27:56.364836Z","iopub.execute_input":"2023-05-04T14:27:56.365582Z","iopub.status.idle":"2023-05-04T14:27:58.371558Z","shell.execute_reply.started":"2023-05-04T14:27:56.365543Z","shell.execute_reply":"2023-05-04T14:27:58.370410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(dog_cat_images)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:28:00.657540Z","iopub.execute_input":"2023-05-04T14:28:00.657918Z","iopub.status.idle":"2023-05-04T14:28:00.665983Z","shell.execute_reply.started":"2023-05-04T14:28:00.657889Z","shell.execute_reply":"2023-05-04T14:28:00.665044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(dog_cat_images)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:28:37.602912Z","iopub.execute_input":"2023-05-04T14:28:37.603795Z","iopub.status.idle":"2023-05-04T14:28:37.609994Z","shell.execute_reply.started":"2023-05-04T14:28:37.603758Z","shell.execute_reply":"2023-05-04T14:28:37.609005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(dog_cat_images.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:28:43.247882Z","iopub.execute_input":"2023-05-04T14:28:43.248259Z","iopub.status.idle":"2023-05-04T14:28:43.253466Z","shell.execute_reply.started":"2023-05-04T14:28:43.248229Z","shell.execute_reply":"2023-05-04T14:28:43.252478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = dog_cat_images\ny = np.asarray(labels)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:28:53.105403Z","iopub.execute_input":"2023-05-04T14:28:53.106178Z","iopub.status.idle":"2023-05-04T14:28:53.110833Z","shell.execute_reply.started":"2023-05-04T14:28:53.106138Z","shell.execute_reply":"2023-05-04T14:28:53.109757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"4\"></a><br>\n# Training","metadata":{}},{"cell_type":"code","source":"x_train,x_test,y_train,y_test = train_test_split(x,y,train_size=0.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:29:39.616294Z","iopub.execute_input":"2023-05-04T14:29:39.616780Z","iopub.status.idle":"2023-05-04T14:29:39.717457Z","shell.execute_reply.started":"2023-05-04T14:29:39.616745Z","shell.execute_reply":"2023-05-04T14:29:39.716401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape, x_train.shape, x_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:29:52.918255Z","iopub.execute_input":"2023-05-04T14:29:52.918769Z","iopub.status.idle":"2023-05-04T14:29:52.925270Z","shell.execute_reply.started":"2023-05-04T14:29:52.918723Z","shell.execute_reply":"2023-05-04T14:29:52.924058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scaling the data\nx_train_scaled = x_train/255\n\nx_test_scaled = x_test/255","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:30:09.367622Z","iopub.execute_input":"2023-05-04T14:30:09.368008Z","iopub.status.idle":"2023-05-04T14:30:10.411005Z","shell.execute_reply.started":"2023-05-04T14:30:09.367975Z","shell.execute_reply":"2023-05-04T14:30:10.409783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train_scaled)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:30:28.656050Z","iopub.execute_input":"2023-05-04T14:30:28.656495Z","iopub.status.idle":"2023-05-04T14:30:28.666973Z","shell.execute_reply.started":"2023-05-04T14:30:28.656464Z","shell.execute_reply":"2023-05-04T14:30:28.665926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"5\"></a><br>\n# Building the Neural Network","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_hub as hub","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:30:50.077013Z","iopub.execute_input":"2023-05-04T14:30:50.077416Z","iopub.status.idle":"2023-05-04T14:30:57.825448Z","shell.execute_reply.started":"2023-05-04T14:30:50.077386Z","shell.execute_reply":"2023-05-04T14:30:57.824273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mobilenet_model = 'https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4'\n\npretrained_model = hub.KerasLayer(mobilenet_model, input_shape=(224,224,3), trainable=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:30:59.785297Z","iopub.execute_input":"2023-05-04T14:30:59.786040Z","iopub.status.idle":"2023-05-04T14:31:01.652558Z","shell.execute_reply.started":"2023-05-04T14:30:59.786003Z","shell.execute_reply":"2023-05-04T14:31:01.651609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2023-05-04T14:31:06.744897Z","iopub.execute_input":"2023-05-04T14:31:06.745278Z","iopub.status.idle":"2023-05-04T14:31:07.504051Z","shell.execute_reply.started":"2023-05-04T14:31:06.745247Z","shell.execute_reply":"2023-05-04T14:31:07.503014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer = 'adam',\n    loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n    metrics = ['acc']\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:31:20.671106Z","iopub.execute_input":"2023-05-04T14:31:20.672238Z","iopub.status.idle":"2023-05-04T14:31:20.683081Z","shell.execute_reply.started":"2023-05-04T14:31:20.672197Z","shell.execute_reply":"2023-05-04T14:31:20.682044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x_train_scaled, y_train, epochs=5)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:31:29.025845Z","iopub.execute_input":"2023-05-04T14:31:29.026241Z","iopub.status.idle":"2023-05-04T14:32:10.834607Z","shell.execute_reply.started":"2023-05-04T14:31:29.026209Z","shell.execute_reply":"2023-05-04T14:32:10.833712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"6\"></a><br>\n# Evaluating model\n","metadata":{}},{"cell_type":"code","source":"score, acc = model.evaluate(x_test_scaled, y_test)\nprint(f'Test Loss ={score}')\nprint(f'Test Accuracy ={acc}')","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:32:40.762698Z","iopub.execute_input":"2023-05-04T14:32:40.763244Z","iopub.status.idle":"2023-05-04T14:33:23.116698Z","shell.execute_reply.started":"2023-05-04T14:32:40.763207Z","shell.execute_reply":"2023-05-04T14:33:23.115586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"7\"></a><br>\n# Predictive System","metadata":{}},{"cell_type":"code","source":"#import cv2\n#import numpy as np\n\n#input_image_path = \"/kaggle/working/resized_img/cat.10255.jpg\"\n#input_image = cv2.imread(input_image_path)\n\n#input_image_resize = cv2.resize(input_image, (224, 224))\n#input_image_scaled = input_image_resize / 255.0\n#image_reshaped = np.reshape(input_image_scaled, [1, 224, 224, 3])\n#input_prediction = model.predict(image_reshaped)\n#input_pred_label = np.argmax(input_prediction)\n\n#if input_pred_label == 0:\n#    print('The image represents a Cat')\n#else:\n#    print('The image represents a Dog')","metadata":{"execution":{"iopub.status.busy":"2023-05-04T16:24:42.245497Z","iopub.execute_input":"2023-05-04T16:24:42.245877Z","iopub.status.idle":"2023-05-04T16:24:42.875429Z","shell.execute_reply.started":"2023-05-04T16:24:42.245844Z","shell.execute_reply":"2023-05-04T16:24:42.873758Z"},"trusted":true},"execution_count":null,"outputs":[]}]}