{"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":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import glob\nimport random\nfrom pathlib import Path\nimport pickle\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport shutil\n\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-08T04:28:45.883168Z","iopub.execute_input":"2023-12-08T04:28:45.883513Z","iopub.status.idle":"2023-12-08T04:28:45.890313Z","shell.execute_reply.started":"2023-12-08T04:28:45.883489Z","shell.execute_reply":"2023-12-08T04:28:45.889458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n!unzip /kaggle/input/dogs-vs-cats/train.zip -d /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-12-08T04:28:49.727650Z","iopub.execute_input":"2023-12-08T04:28:49.728122Z","iopub.status.idle":"2023-12-08T04:28:58.219724Z","shell.execute_reply.started":"2023-12-08T04:28:49.728097Z","shell.execute_reply":"2023-12-08T04:28:58.218656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n!unzip /kaggle/input/dogs-vs-cats/test1.zip -d /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-12-08T04:25:44.693263Z","iopub.execute_input":"2023-12-08T04:25:44.693605Z","iopub.status.idle":"2023-12-08T04:25:49.850130Z","shell.execute_reply.started":"2023-12-08T04:25:44.693579Z","shell.execute_reply":"2023-12-08T04:25:49.849282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for path in glob.glob('/kaggle/working/train/*.jpg'):\n    if not(os.path.exists('/kaggle/working/train/cat') or os.path.exists('/kaggle/working/train/dog')):\n        os.makedirs('/kaggle/working/train/cat')\n        os.makedirs('/kaggle/working/train/dog')\n        print('Done!')\n        \n    if 'cat' in path:\n        shutil.move(path, '/kaggle/working/train/cat/' + path.split('/')[-1])\n    else:\n        shutil.move(path, '/kaggle/working/train/dog/' + path.split('/')[-1])\n    ","metadata":{"execution":{"iopub.status.busy":"2023-12-08T04:29:12.478752Z","iopub.execute_input":"2023-12-08T04:29:12.479751Z","iopub.status.idle":"2023-12-08T04:29:13.131792Z","shell.execute_reply.started":"2023-12-08T04:29:12.479725Z","shell.execute_reply":"2023-12-08T04:29:13.130907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_dataset = {}\nfor class_name in glob.glob('/kaggle/working/train/*'):\n        my_dataset[class_name.split('/')[-1]] = glob.glob(class_name + '/*')","metadata":{"execution":{"iopub.status.busy":"2023-12-08T04:51:08.585475Z","iopub.execute_input":"2023-12-08T04:51:08.585774Z","iopub.status.idle":"2023-12-08T04:51:08.646678Z","shell.execute_reply.started":"2023-12-08T04:51:08.585749Z","shell.execute_reply":"2023-12-08T04:51:08.645886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"These are the keys in my dataset {my_dataset.keys()}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-08T04:51:11.900810Z","iopub.execute_input":"2023-12-08T04:51:11.901140Z","iopub.status.idle":"2023-12-08T04:51:11.905795Z","shell.execute_reply.started":"2023-12-08T04:51:11.901113Z","shell.execute_reply":"2023-12-08T04:51:11.904973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X   = my_dataset.keys()\ny = [len(keys)for keys in dataset.values()]","metadata":{"execution":{"iopub.status.busy":"2023-12-08T04:53:23.322649Z","iopub.execute_input":"2023-12-08T04:53:23.323435Z","iopub.status.idle":"2023-12-08T04:53:23.326749Z","shell.execute_reply.started":"2023-12-08T04:53:23.323403Z","shell.execute_reply":"2023-12-08T04:53:23.326107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-12-08T04:54:06.737169Z","iopub.execute_input":"2023-12-08T04:54:06.737488Z","iopub.status.idle":"2023-12-08T04:54:07.490518Z","shell.execute_reply.started":"2023-12-08T04:54:06.737461Z","shell.execute_reply":"2023-12-08T04:54:07.489656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"The shape of my data before splitting is {y}\") #There are a total of 12500 cat images and 12500 Dog images ","metadata":{"execution":{"iopub.status.busy":"2023-12-08T04:54:55.462807Z","iopub.execute_input":"2023-12-08T04:54:55.463152Z","iopub.status.idle":"2023-12-08T04:54:55.468763Z","shell.execute_reply.started":"2023-12-08T04:54:55.463125Z","shell.execute_reply":"2023-12-08T04:54:55.467918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **IMAGE REPRESENTATION IN PYTHON **\n\n\nIn machine learning and python in general images are represented in HWC or CHW format . This is to mean that we describe images as their heights widths and color channels.Images can be represented by numpy multi-dimensional arrays and so their type is NdArrays. A color image is a numpy array with 3 dimensions. By slicing the multi-dimensional array the RGB channels can be separated.\nIn an RGB image, each pixel is represented by three 8 bit numbers associated to the values for Red, Green, Blue respectively","metadata":{}},{"cell_type":"code","source":"# Having the image representation idea we can go further and try plotting some of the images\n# What if we created a dataframe that has Height Width and ColorChannels and the Class or label?\n# Using cv2 we can load our image properties \nimport cv2 as cv\nhelp(cv.imread)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T05:12:49.483970Z","iopub.execute_input":"2023-12-08T05:12:49.484319Z","iopub.status.idle":"2023-12-08T05:12:49.488705Z","shell.execute_reply.started":"2023-12-08T05:12:49.484294Z","shell.execute_reply":"2023-12-08T05:12:49.488171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_properties(file):\n    '''Returns dimenstions for an image interms of HWD or HWC'''\n    image = cv2.imread(file)    \n    h, w, d = image.shape\n    return h, w, d ","metadata":{"execution":{"iopub.status.busy":"2023-12-08T05:13:41.557440Z","iopub.execute_input":"2023-12-08T05:13:41.557739Z","iopub.status.idle":"2023-12-08T05:13:41.563265Z","shell.execute_reply.started":"2023-12-08T05:13:41.557716Z","shell.execute_reply":"2023-12-08T05:13:41.562336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating a Dataframe\n","metadata":{}},{"cell_type":"code","source":"df = pd.DataFrame(columns=['Height',\n                           'Width',\n                           'Channels',\n                           'Class'])\n\nfor pet_class in my_dataset.keys():\n    for path in my_dataset[pet_class]:\n        h, w, d = image_properties(path)\n        df.loc[len(df)] = [h, w, d, pet_class]\n\ndf","metadata":{"execution":{"iopub.status.busy":"2023-12-08T05:54:10.037648Z","iopub.execute_input":"2023-12-08T05:54:10.037998Z","iopub.status.idle":"2023-12-08T05:55:22.822407Z","shell.execute_reply.started":"2023-12-08T05:54:10.037971Z","shell.execute_reply":"2023-12-08T05:55:22.821595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# i CAN VISUALIZE MY CATEGORICAL DATA NOW \nplt.figure(figsize=(15,8))\nsns.countplot(data=df,x='Class')\nplt.title(\"A Countplot showing the total number of Cats and Dogs in our dataset\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-08T05:29:29.074584Z","iopub.execute_input":"2023-12-08T05:29:29.074881Z","iopub.status.idle":"2023-12-08T05:29:29.255839Z","shell.execute_reply.started":"2023-12-08T05:29:29.074857Z","shell.execute_reply":"2023-12-08T05:29:29.254810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# How are our pet heights  distributed?\n# Lets begin with the dogs\nplt.figure(figsize=(20,8))\nplt.subplot(2,2,1)\nsns.histplot(x = df[df['Class']=='dog']['Height'])\nplt.ylabel(\"Total Count\")\nplt.title('The distribution of Dog Height')\nplt.subplot(2,2,2)\nsns.histplot(x = df[df['Class']=='dog']['Width'])\nplt.ylabel(\"Total Count\")\nplt.title('The distribution of Dog Width')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-08T05:37:29.501335Z","iopub.execute_input":"2023-12-08T05:37:29.501691Z","iopub.status.idle":"2023-12-08T05:37:30.033917Z","shell.execute_reply.started":"2023-12-08T05:37:29.501664Z","shell.execute_reply":"2023-12-08T05:37:30.033090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,8))\nplt.subplot(2,2,1)\nsns.histplot(x = df[df['Class']=='cat']['Height'])\nplt.ylabel(\"Total Count\")\nplt.title('The distribution of Cat Height')\nplt.subplot(2,2,2)\nsns.histplot(x = df[df['Class']=='cat']['Width'])\nplt.ylabel(\"Total Count\")\nplt.title('The distribution of Cat Width')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-08T05:37:51.006245Z","iopub.execute_input":"2023-12-08T05:37:51.006597Z","iopub.status.idle":"2023-12-08T05:37:51.556081Z","shell.execute_reply.started":"2023-12-08T05:37:51.006568Z","shell.execute_reply":"2023-12-08T05:37:51.555307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Using CV2 to read our images\n\n* cv2.imread() method loads an image from the specified file","metadata":{}},{"cell_type":"code","source":"# How about we plot some images ? Exciting visualizations\n# Lets go for some  Random Images\ncount = 0\nplt.figure(figsize=(15, 15))\nfor pet_class in my_dataset.keys():    \n    for path in my_dataset[pet_class]:                \n        sp = plt.subplot(2, 4, count + 1, )\n        sp.axis('Off')\n        sp.set_title(pet_class)        \n        image = cv.imread(path)\n        image = cv.cvtColor(image, cv2.COLOR_BGR2RGB)\n        plt.imshow(image)\n        count += 1\n        if count % 4== 0:            \n            break\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-08T05:56:21.723005Z","iopub.execute_input":"2023-12-08T05:56:21.723345Z","iopub.status.idle":"2023-12-08T05:56:22.593079Z","shell.execute_reply.started":"2023-12-08T05:56:21.723312Z","shell.execute_reply":"2023-12-08T05:56:22.592072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DATA PREPROCESSING : SELECTING OUR LABELS AND SCALING DATA ","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nlbl_encoder = LabelEncoder()\ndf['Class'] = lbl_encoder.fit_transform(df['Class'])","metadata":{"execution":{"iopub.status.busy":"2023-12-08T06:03:33.711178Z","iopub.execute_input":"2023-12-08T06:03:33.711508Z","iopub.status.idle":"2023-12-08T06:03:33.716463Z","shell.execute_reply.started":"2023-12-08T06:03:33.711484Z","shell.execute_reply":"2023-12-08T06:03:33.715710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-08T06:03:43.488634Z","iopub.execute_input":"2023-12-08T06:03:43.488979Z","iopub.status.idle":"2023-12-08T06:03:43.498215Z","shell.execute_reply.started":"2023-12-08T06:03:43.488950Z","shell.execute_reply":"2023-12-08T06:03:43.497347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Class'].value_counts().head()","metadata":{"execution":{"iopub.status.busy":"2023-12-08T06:04:11.965755Z","iopub.execute_input":"2023-12-08T06:04:11.966097Z","iopub.status.idle":"2023-12-08T06:04:11.977589Z","shell.execute_reply.started":"2023-12-08T06:04:11.966064Z","shell.execute_reply":"2023-12-08T06:04:11.976370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Selecting  feature and target variable\nfrom sklearn.model_selection import train_test_split\nX =  df.drop(['Class'],axis=1)\ny = df['Class']\nX_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=101)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T06:22:53.823511Z","iopub.execute_input":"2023-12-08T06:22:53.823837Z","iopub.status.idle":"2023-12-08T06:22:53.915642Z","shell.execute_reply.started":"2023-12-08T06:22:53.823810Z","shell.execute_reply":"2023-12-08T06:22:53.914985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-08T06:23:25.009358Z","iopub.execute_input":"2023-12-08T06:23:25.009686Z","iopub.status.idle":"2023-12-08T06:23:25.015794Z","shell.execute_reply.started":"2023-12-08T06:23:25.009661Z","shell.execute_reply":"2023-12-08T06:23:25.014955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train/255  # Reshaping my data so that they range between 0 and 1\nX_test = X_test/255","metadata":{"execution":{"iopub.status.busy":"2023-12-08T06:37:11.925239Z","iopub.execute_input":"2023-12-08T06:37:11.925914Z","iopub.status.idle":"2023-12-08T06:37:11.931511Z","shell.execute_reply.started":"2023-12-08T06:37:11.925873Z","shell.execute_reply":"2023-12-08T06:37:11.930553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2023-12-08T06:38:03.362366Z","iopub.execute_input":"2023-12-08T06:38:03.363142Z","iopub.status.idle":"2023-12-08T06:38:03.373561Z","shell.execute_reply.started":"2023-12-08T06:38:03.363119Z","shell.execute_reply":"2023-12-08T06:38:03.372766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import SVC\nfrom sklearn.model_selection import GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2023-12-08T06:41:09.397178Z","iopub.execute_input":"2023-12-08T06:41:09.397532Z","iopub.status.idle":"2023-12-08T06:41:09.479513Z","shell.execute_reply.started":"2023-12-08T06:41:09.397506Z","shell.execute_reply":"2023-12-08T06:41:09.478831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report,accuracy_score\nsvc = SVC()\nsvc.fit(X_train,y_train)\npredictions = svc.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T06:50:18.781855Z","iopub.execute_input":"2023-12-08T06:50:18.782268Z","iopub.status.idle":"2023-12-08T06:50:33.129033Z","shell.execute_reply.started":"2023-12-08T06:50:18.782243Z","shell.execute_reply":"2023-12-08T06:50:33.128003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"The Classification Report for SVC model is \\n{classification_report(y_test,predictions)}\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"The Accuracy score for  my SVC model is \\n{accuracy_score(y_test,predictions)*100}%\")","metadata":{"execution":{"iopub.status.busy":"2023-12-08T06:52:02.933790Z","iopub.execute_input":"2023-12-08T06:52:02.934819Z","iopub.status.idle":"2023-12-08T06:52:02.941041Z","shell.execute_reply.started":"2023-12-08T06:52:02.934767Z","shell.execute_reply":"2023-12-08T06:52:02.940127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# THE END","metadata":{}}]}