{"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":"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)\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-08-08T15:32:33.594242Z","iopub.execute_input":"2023-08-08T15:32:33.594716Z","iopub.status.idle":"2023-08-08T15:32:33.603659Z","shell.execute_reply.started":"2023-08-08T15:32:33.594673Z","shell.execute_reply":"2023-08-08T15:32:33.602228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from zipfile import ZipFile\ndata=\"/kaggle/input/dogs-vs-cats/train.zip\"\nwith ZipFile(data,'r') as zip:\n    zip.extractall()","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:32:33.606075Z","iopub.execute_input":"2023-08-08T15:32:33.606547Z","iopub.status.idle":"2023-08-08T15:32:43.056151Z","shell.execute_reply.started":"2023-08-08T15:32:33.606501Z","shell.execute_reply":"2023-08-08T15:32:43.055155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\npath,dirs,files=next(os.walk('/kaggle/working/train'))\nfile_count=len(files)\nprint(file_count)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:32:43.057436Z","iopub.execute_input":"2023-08-08T15:32:43.057982Z","iopub.status.idle":"2023-08-08T15:32:43.099679Z","shell.execute_reply.started":"2023-08-08T15:32:43.057949Z","shell.execute_reply":"2023-08-08T15:32:43.097918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nfrom sklearn.model_selection import train_test_split\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:32:43.102235Z","iopub.execute_input":"2023-08-08T15:32:43.102617Z","iopub.status.idle":"2023-08-08T15:32:43.10884Z","shell.execute_reply.started":"2023-08-08T15:32:43.102581Z","shell.execute_reply":"2023-08-08T15:32:43.107553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = '/kaggle/working/train/dog.8298.jpg'\nimg = cv2.imread(img_path)\nplt.imshow(img)\nplt.axis('off') \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:32:43.110102Z","iopub.execute_input":"2023-08-08T15:32:43.110468Z","iopub.status.idle":"2023-08-08T15:32:43.366103Z","shell.execute_reply.started":"2023-08-08T15:32:43.110423Z","shell.execute_reply":"2023-08-08T15:32:43.364634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = '/kaggle/working/train/cat.8288.jpg'\nimg = cv2.imread(img_path)\nplt.imshow(img)\nplt.axis('off') \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:32:43.367776Z","iopub.execute_input":"2023-08-08T15:32:43.36826Z","iopub.status.idle":"2023-08-08T15:32:43.618993Z","shell.execute_reply.started":"2023-08-08T15:32:43.368212Z","shell.execute_reply":"2023-08-08T15:32:43.618023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Selecting Images","metadata":{}},{"cell_type":"code","source":"file_names=os.listdir('/kaggle/working/train')\nprint(file_names[0:10])","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:32:43.620491Z","iopub.execute_input":"2023-08-08T15:32:43.621089Z","iopub.status.idle":"2023-08-08T15:32:43.64435Z","shell.execute_reply.started":"2023-08-08T15:32:43.621051Z","shell.execute_reply":"2023-08-08T15:32:43.642879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_count=0;\ncat_count=0;\nfor img_file in file_names:\n    name=img_file[0:3]\n    if(name==\"dog\"):\n        dog_count+=1;\n    else:\n        cat_count+=1;\nprint(\"Dog count : \",dog_count);\nprint(\"Cat count : \",cat_count);","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:32:43.64604Z","iopub.execute_input":"2023-08-08T15:32:43.646704Z","iopub.status.idle":"2023-08-08T15:32:43.66807Z","shell.execute_reply.started":"2023-08-08T15:32:43.646668Z","shell.execute_reply":"2023-08-08T15:32:43.666634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resizing","metadata":{}},{"cell_type":"code","source":"try:\n    os.mkdir('/kaggle/working/resized')\nexcept:\n    None","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:32:43.669341Z","iopub.execute_input":"2023-08-08T15:32:43.669711Z","iopub.status.idle":"2023-08-08T15:32:43.677141Z","shell.execute_reply.started":"2023-08-08T15:32:43.669661Z","shell.execute_reply":"2023-08-08T15:32:43.67613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original='/kaggle/working/train/'\nresized='/kaggle/working/resized/'\nfor i in range(2000):\n    file_name = os.listdir(original)[i]\n    img_path = os.path.join(original, file_name)\n    \n    img = cv2.imread(img_path)\n    resized_img = cv2.resize(img, (224, 224))\n    \n    resized_img_rgb = cv2.cvtColor(resized_img, cv2.COLOR_BGR2RGB)\n    \n    new_img_path = os.path.join(resized, file_name)\n    cv2.imwrite(new_img_path, resized_img_rgb)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:32:43.682103Z","iopub.execute_input":"2023-08-08T15:32:43.682467Z","iopub.status.idle":"2023-08-08T15:33:24.876683Z","shell.execute_reply.started":"2023-08-08T15:32:43.682435Z","shell.execute_reply":"2023-08-08T15:33:24.87522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = '/kaggle/working/resized/dog.12162.jpg'\nimg = cv2.imread(img_path)\nplt.imshow(img)\n \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:33:24.878278Z","iopub.execute_input":"2023-08-08T15:33:24.879571Z","iopub.status.idle":"2023-08-08T15:33:25.283258Z","shell.execute_reply.started":"2023-08-08T15:33:24.879529Z","shell.execute_reply":"2023-08-08T15:33:25.281996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = '/kaggle/working/resized/cat.4400.jpg'\nimg = cv2.imread(img_path)\nplt.imshow(img)\n \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:33:25.284834Z","iopub.execute_input":"2023-08-08T15:33:25.285265Z","iopub.status.idle":"2023-08-08T15:33:25.662814Z","shell.execute_reply.started":"2023-08-08T15:33:25.285227Z","shell.execute_reply":"2023-08-08T15:33:25.661563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Labelling","metadata":{}},{"cell_type":"code","source":"# cat => 0 dog => 1\nY=[]\nfilenames=os.listdir('/kaggle/working/resized/')\nfor i in range(2000):\n    file_name=filenames[i]\n    label=file_name[0:3]\n    if(label=='dog'):\n        Y.append(1)\n    else:\n        Y.append(0)\n        \nprint(Y[:50])","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:33:25.664133Z","iopub.execute_input":"2023-08-08T15:33:25.664477Z","iopub.status.idle":"2023-08-08T15:33:25.675825Z","shell.execute_reply.started":"2023-08-08T15:33:25.664445Z","shell.execute_reply":"2023-08-08T15:33:25.674504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#counting images of dogs in sample data (2000)\nvalues,counts=np.unique(Y,return_counts=True)\nprint(values)\nprint(counts)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:33:25.679001Z","iopub.execute_input":"2023-08-08T15:33:25.679517Z","iopub.status.idle":"2023-08-08T15:33:25.691817Z","shell.execute_reply.started":"2023-08-08T15:33:25.679468Z","shell.execute_reply":"2023-08-08T15:33:25.690318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Converting images to np arrays","metadata":{}},{"cell_type":"code","source":"image_dir='/kaggle/working/resized/'\nimg_ext=['png','jpg']\n\nfiles = [os.path.join(image_dir, file) for file in os.listdir(image_dir) if file.split('.')[-1].lower() in img_ext]\n\nX=[]\nfor file in files:\n    img=cv2.imread(file)\n    if(img is not None):\n        X.append(img)\n        \nX=np.array(X)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:33:25.693542Z","iopub.execute_input":"2023-08-08T15:33:25.694114Z","iopub.status.idle":"2023-08-08T15:33:28.504155Z","shell.execute_reply.started":"2023-08-08T15:33:25.69406Z","shell.execute_reply":"2023-08-08T15:33:28.502909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Training","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_tr,X_ts,Y_tr,Y_ts=train_test_split(X,Y,random_state=2)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:33:28.505729Z","iopub.execute_input":"2023-08-08T15:33:28.506118Z","iopub.status.idle":"2023-08-08T15:33:28.651888Z","shell.execute_reply.started":"2023-08-08T15:33:28.506083Z","shell.execute_reply":"2023-08-08T15:33:28.650646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Scaling\nX_tr_scaled=X_tr/255\nX_ts_scaled=X_ts/255","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:33:28.653321Z","iopub.execute_input":"2023-08-08T15:33:28.653673Z","iopub.status.idle":"2023-08-08T15:33:31.579802Z","shell.execute_reply.started":"2023-08-08T15:33:28.653641Z","shell.execute_reply":"2023-08-08T15:33:31.578521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_tr=np.array(Y_tr)\nY_ts=np.array(Y_ts)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:33:31.581467Z","iopub.execute_input":"2023-08-08T15:33:31.581965Z","iopub.status.idle":"2023-08-08T15:33:31.588961Z","shell.execute_reply.started":"2023-08-08T15:33:31.581895Z","shell.execute_reply":"2023-08-08T15:33:31.587671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Neural Network\nimport tensorflow as tf\nimport tensorflow_hub as hub","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:33:31.590894Z","iopub.execute_input":"2023-08-08T15:33:31.591422Z","iopub.status.idle":"2023-08-08T15:33:35.724479Z","shell.execute_reply.started":"2023-08-08T15:33:31.591375Z","shell.execute_reply":"2023-08-08T15:33:35.723136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model='https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4'\npretrained_model=hub.KerasLayer(model,input_shape=(224,224,3),trainable=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:33:35.725838Z","iopub.execute_input":"2023-08-08T15:33:35.72669Z","iopub.status.idle":"2023-08-08T15:33:37.855984Z","shell.execute_reply.started":"2023-08-08T15:33:35.726648Z","shell.execute_reply":"2023-08-08T15:33:37.854618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_of_classes=2\nmodel=tf.keras.Sequential([\n    pretrained_model,\n    tf.keras.layers.Dense(num_of_classes)\n])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:33:37.857435Z","iopub.execute_input":"2023-08-08T15:33:37.857778Z","iopub.status.idle":"2023-08-08T15:33:38.832058Z","shell.execute_reply.started":"2023-08-08T15:33:37.857748Z","shell.execute_reply":"2023-08-08T15:33:38.830617Z"},"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=['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:33:38.833797Z","iopub.execute_input":"2023-08-08T15:33:38.834269Z","iopub.status.idle":"2023-08-08T15:33:38.859008Z","shell.execute_reply.started":"2023-08-08T15:33:38.834224Z","shell.execute_reply":"2023-08-08T15:33:38.857716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_tr_scaled,Y_tr,epochs=5)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:33:38.860611Z","iopub.execute_input":"2023-08-08T15:33:38.861072Z","iopub.status.idle":"2023-08-08T15:37:07.940514Z","shell.execute_reply.started":"2023-08-08T15:33:38.861028Z","shell.execute_reply":"2023-08-08T15:37:07.93871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Testing","metadata":{}},{"cell_type":"code","source":"score,acc=model.evaluate(X_ts_scaled,Y_ts)\nprint('Test Loss = ',score)\nprint('Test Accuracy =',acc)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:37:07.942755Z","iopub.execute_input":"2023-08-08T15:37:07.943274Z","iopub.status.idle":"2023-08-08T15:37:29.938104Z","shell.execute_reply.started":"2023-08-08T15:37:07.94323Z","shell.execute_reply":"2023-08-08T15:37:29.936719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictions","metadata":{}},{"cell_type":"code","source":"def predict(img_path):\n    input_img=cv2.imread(img_path)\n    input_img_resized=cv2.resize(input_img,(224,224))\n    input_img_scaled=input_img_resized/255\n    input_img_reshaped=np.reshape(input_img_scaled,[1,224,224,3])\n    pred=model.predict(input_img_reshaped)\n    label=np.argmax(pred)\n    if(label==0):\n        return \"Cat\"\n    else:\n        return \"Dog\"","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:37:29.940246Z","iopub.execute_input":"2023-08-08T15:37:29.94074Z","iopub.status.idle":"2023-08-08T15:37:29.950936Z","shell.execute_reply.started":"2023-08-08T15:37:29.940694Z","shell.execute_reply":"2023-08-08T15:37:29.949405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from zipfile import ZipFile\ndata=\"/kaggle/input/dogs-vs-cats/test1.zip\"\nwith ZipFile(data,'r') as zip:\n    zip.extractall()","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:37:29.953163Z","iopub.execute_input":"2023-08-08T15:37:29.953688Z","iopub.status.idle":"2023-08-08T15:37:34.447865Z","shell.execute_reply.started":"2023-08-08T15:37:29.953586Z","shell.execute_reply":"2023-08-08T15:37:34.446961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_names=os.listdir('/kaggle/working/test1')\nprint(file_names[0:10])","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:37:34.449222Z","iopub.execute_input":"2023-08-08T15:37:34.450164Z","iopub.status.idle":"2023-08-08T15:37:34.465927Z","shell.execute_reply.started":"2023-08-08T15:37:34.450128Z","shell.execute_reply":"2023-08-08T15:37:34.464728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testing_images=[]\npath='/kaggle/working/test1/'\nfor file in file_names:\n    testing_images.append(path+file)\nprint(testing_images[:10])","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:37:34.471698Z","iopub.execute_input":"2023-08-08T15:37:34.472116Z","iopub.status.idle":"2023-08-08T15:37:34.48261Z","shell.execute_reply.started":"2023-08-08T15:37:34.472078Z","shell.execute_reply":"2023-08-08T15:37:34.481243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Model Prediction : \",predict(testing_images[0]))\nimg = cv2.imread(testing_images[0])\nplt.axis('off')\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T15:37:34.484359Z","iopub.execute_input":"2023-08-08T15:37:34.484817Z","iopub.status.idle":"2023-08-08T15:37:35.615505Z","shell.execute_reply.started":"2023-08-08T15:37:34.484775Z","shell.execute_reply":"2023-08-08T15:37:35.614131Z"},"trusted":true},"execution_count":null,"outputs":[]}]}