{"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":"# Importing libraries","metadata":{}},{"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nimport matplotlib.pyplot as plt\nfrom matplotlib import image\nimport seaborn as sns\nimport os\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.model_selection import train_test_split\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-10T17:32:19.487492Z","iopub.execute_input":"2023-03-10T17:32:19.488303Z","iopub.status.idle":"2023-03-10T17:32:19.494084Z","shell.execute_reply.started":"2023-03-10T17:32:19.488262Z","shell.execute_reply":"2023-03-10T17:32:19.492994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading images","metadata":{}},{"cell_type":"code","source":"print(os.listdir(\"../input\"))\n\nimport zipfile\n\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:32:28.837634Z","iopub.execute_input":"2023-03-10T17:32:28.838337Z","iopub.status.idle":"2023-03-10T17:32:45.716686Z","shell.execute_reply.started":"2023-03-10T17:32:28.838300Z","shell.execute_reply":"2023-03-10T17:32:45.715688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir = '/kaggle/working/train'\ntrain_img = ['/kaggle/working/train/{}'.format(i) for i in os.listdir(dir)]","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:32:45.718781Z","iopub.execute_input":"2023-03-10T17:32:45.719182Z","iopub.status.idle":"2023-03-10T17:32:45.744898Z","shell.execute_reply.started":"2023-03-10T17:32:45.719141Z","shell.execute_reply":"2023-03-10T17:32:45.743908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploring images","metadata":{}},{"cell_type":"code","source":"len(train_img)","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:32:45.746616Z","iopub.execute_input":"2023-03-10T17:32:45.747033Z","iopub.status.idle":"2023-03-10T17:32:45.756477Z","shell.execute_reply.started":"2023-03-10T17:32:45.746993Z","shell.execute_reply":"2023-03-10T17:32:45.755234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pic =  cv2.imread(train_img[8], cv2.IMREAD_COLOR)\nprint((pic.shape))\nplt.imshow(pic)","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:32:45.759092Z","iopub.execute_input":"2023-03-10T17:32:45.759859Z","iopub.status.idle":"2023-03-10T17:32:46.153786Z","shell.execute_reply.started":"2023-03-10T17:32:45.759822Z","shell.execute_reply":"2023-03-10T17:32:46.152799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = []\n\nfor p in os.listdir(dir):\n    category = p.split(\".\")[0]\n    if category =='dog':\n        classes.append(1)\n    else:\n        classes.append(0)","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:32:46.162159Z","iopub.execute_input":"2023-03-10T17:32:46.162523Z","iopub.status.idle":"2023-03-10T17:32:46.191439Z","shell.execute_reply.started":"2023-03-10T17:32:46.162488Z","shell.execute_reply":"2023-03-10T17:32:46.190548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({\n    'filename': train_img,\n    'class': classes\n})\ndf","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:32:55.678886Z","iopub.execute_input":"2023-03-10T17:32:55.679511Z","iopub.status.idle":"2023-03-10T17:32:55.710007Z","shell.execute_reply.started":"2023-03-10T17:32:55.679473Z","shell.execute_reply":"2023-03-10T17:32:55.708970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:32:56.871728Z","iopub.execute_input":"2023-03-10T17:32:56.872584Z","iopub.status.idle":"2023-03-10T17:32:56.880870Z","shell.execute_reply.started":"2023-03-10T17:32:56.872544Z","shell.execute_reply":"2023-03-10T17:32:56.880070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['class'].value_counts().plot.bar()\n","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:32:58.109318Z","iopub.execute_input":"2023-03-10T17:32:58.109923Z","iopub.status.idle":"2023-03-10T17:32:58.327081Z","shell.execute_reply.started":"2023-03-10T17:32:58.109885Z","shell.execute_reply":"2023-03-10T17:32:58.325999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# let's display first 15 images\n\ncolumns = 3\nshow =15\nplt.figure(figsize=(20,15))\n\nfor idx, img in enumerate(train_img):\n    \n    if idx >= show:\n        break\n    temp = cv2.imread(img, cv2.IMREAD_COLOR)\n    \n    plt.subplot(int(np.ceil(show / columns) ), columns, idx + 1)\n    plt.imshow(temp)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:32:59.253910Z","iopub.execute_input":"2023-03-10T17:32:59.254881Z","iopub.status.idle":"2023-03-10T17:33:01.548257Z","shell.execute_reply.started":"2023-03-10T17:32:59.254828Z","shell.execute_reply":"2023-03-10T17:33:01.547316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image preprocessing","metadata":{}},{"cell_type":"code","source":"p =cv2.imread(train_img[0])\npp = cv2.resize(p,(32,32))\nplt.imshow(pp)","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:33:01.549907Z","iopub.execute_input":"2023-03-10T17:33:01.550667Z","iopub.status.idle":"2023-03-10T17:33:01.797829Z","shell.execute_reply.started":"2023-03-10T17:33:01.550629Z","shell.execute_reply":"2023-03-10T17:33:01.796784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_to_feature_vector(image, size=(32, 32)):\n\n    return cv2.resize(image, size).flatten() \n# “feature vector” will be a list of 32 x 32 x 3 = 3,072 numbers","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:33:02.271960Z","iopub.execute_input":"2023-03-10T17:33:02.272631Z","iopub.status.idle":"2023-03-10T17:33:02.278380Z","shell.execute_reply.started":"2023-03-10T17:33:02.272594Z","shell.execute_reply":"2023-03-10T17:33:02.276982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# histogram for first 15 images\ncolumns = 3\nshow =15\nplt.figure(figsize=(20,15))\n\nfor idx, img in enumerate(train_img):\n    \n    if idx >= show:\n        break\n    temp = cv2.imread(img, cv2.IMREAD_COLOR)\n    temp1 = cv2.cvtColor(temp, cv2.COLOR_BGR2RGB)\n\n    plt.subplot(int(np.ceil(show / columns) ), columns, idx + 1)\n    color = ('b','g','r')\n    for i,col in enumerate(color):\n        histr = cv2.calcHist([temp1],[i],None,[256],[0,256])\n        plt.plot(histr,color = col)\n        plt.xlim([0,256])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:33:03.602811Z","iopub.execute_input":"2023-03-10T17:33:03.605232Z","iopub.status.idle":"2023-03-10T17:33:05.336602Z","shell.execute_reply.started":"2023-03-10T17:33:03.605183Z","shell.execute_reply":"2023-03-10T17:33:05.335702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# histogram Equalization\ncolumns = 3\nshow =15\nplt.figure(figsize=(20,15))\n\nfor idx, img in enumerate(train_img):\n    \n    if idx >= show:\n        break\n    temp = cv2.imread(img)\n    \n    #  ## Color Images\n    hsv = cv2.cvtColor(temp, cv2.COLOR_BGR2HSV)\n    hsv[:,:,2] = cv2.equalizeHist(hsv[:,:,2])\n    eq_color = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)\n\n\n    plt.subplot(int(np.ceil(show / columns) ), columns, idx + 1)\n    for i,col in enumerate(color):\n        histr = cv2.calcHist([eq_color],[i],None,[256],[0,256])\n        plt.plot(histr,color = col)\n        plt.xlim([0,256])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:33:05.338663Z","iopub.execute_input":"2023-03-10T17:33:05.339253Z","iopub.status.idle":"2023-03-10T17:33:07.028536Z","shell.execute_reply.started":"2023-03-10T17:33:05.339217Z","shell.execute_reply":"2023-03-10T17:33:07.027187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display image after equalizer\n\ncolumns = 3\nshow =15\nplt.figure(figsize=(20,15))\n\nfor idx, img in enumerate(train_img):\n    \n    if idx >= show:\n        break\n    temp = cv2.imread(img)\n    \n    #  ## Color Images\n    hsv = cv2.cvtColor(temp, cv2.COLOR_BGR2HSV)\n    hsv[:,:,2] = cv2.equalizeHist(hsv[:,:,2])\n    eq_color = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)\n\n\n    plt.subplot(int(np.ceil(show / columns) ), columns, idx + 1)\n    plt.imshow(eq_color)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:33:30.325286Z","iopub.execute_input":"2023-03-10T17:33:30.325888Z","iopub.status.idle":"2023-03-10T17:33:32.608173Z","shell.execute_reply.started":"2023-03-10T17:33:30.325852Z","shell.execute_reply":"2023-03-10T17:33:32.605018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def hist_eq(img):\n    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n    hsv[:,:,2] = cv2.equalizeHist(hsv[:,:,2])\n    eq_color = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)\n    return  cv2.resize(eq_color, (32,32)).flatten() \n\n    ","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:34:20.806535Z","iopub.execute_input":"2023-03-10T17:34:20.807546Z","iopub.status.idle":"2023-03-10T17:34:20.814162Z","shell.execute_reply.started":"2023-03-10T17:34:20.807507Z","shell.execute_reply":"2023-03-10T17:34:20.812768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rawImages = []\nrawImages2 = []\nlabels = []","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:34:21.237310Z","iopub.execute_input":"2023-03-10T17:34:21.238362Z","iopub.status.idle":"2023-03-10T17:34:21.244213Z","shell.execute_reply.started":"2023-03-10T17:34:21.238308Z","shell.execute_reply":"2023-03-10T17:34:21.242957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(df)):\n    img=cv2.imread(train_img[i])\n    pixels = image_to_feature_vector(img)\n    hist = hist_eq(img)\n    rawImages.append(pixels)\n    rawImages2.append(hist)\n    labels.append(df['class'].loc[i])\n","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:34:21.734260Z","iopub.execute_input":"2023-03-10T17:34:21.735198Z","iopub.status.idle":"2023-03-10T17:36:05.602100Z","shell.execute_reply.started":"2023-03-10T17:34:21.735143Z","shell.execute_reply":"2023-03-10T17:36:05.600695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.shape(rawImages))\nprint(np.shape(rawImages2))\nprint(np.shape(labels))","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:36:10.148442Z","iopub.execute_input":"2023-03-10T17:36:10.148814Z","iopub.status.idle":"2023-03-10T17:36:10.233879Z","shell.execute_reply.started":"2023-03-10T17:36:10.148779Z","shell.execute_reply":"2023-03-10T17:36:10.232650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# apply knn ","metadata":{}},{"cell_type":"code","source":"k = round(np.sqrt(25000))\nk","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:36:16.348189Z","iopub.execute_input":"2023-03-10T17:36:16.348554Z","iopub.status.idle":"2023-03-10T17:36:16.357114Z","shell.execute_reply.started":"2023-03-10T17:36:16.348521Z","shell.execute_reply":"2023-03-10T17:36:16.355636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(trainRI, testRI, trainRL, testRL) = train_test_split(rawImages, labels, test_size=0.3, random_state=42)\n(trainFeat, testFeat, trainLabels, testLabels) = train_test_split(rawImages2, labels, test_size=0.3, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:44:14.930693Z","iopub.execute_input":"2023-03-10T17:44:14.931443Z","iopub.status.idle":"2023-03-10T17:44:14.958805Z","shell.execute_reply.started":"2023-03-10T17:44:14.931403Z","shell.execute_reply":"2023-03-10T17:44:14.957850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = KNeighborsClassifier(n_neighbors=5,n_jobs=-1)\nmodel.fit(trainRI, trainRL)\nprint(model.score(trainRI, trainRL))\nacc = model.score(testRI, testRL)\nprint(\"Raw pixel accuracy: {:.2f}%\".format(acc * 100))","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:44:15.911247Z","iopub.execute_input":"2023-03-10T17:44:15.911649Z","iopub.status.idle":"2023-03-10T17:45:17.659078Z","shell.execute_reply.started":"2023-03-10T17:44:15.911616Z","shell.execute_reply":"2023-03-10T17:45:17.657817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = KNeighborsClassifier(n_neighbors=5,n_jobs=-1)\nmodel.fit(trainFeat, trainLabels)\nprint(model.score(trainFeat, trainLabels))\nacc = model.score(testFeat, testLabels)\nprint(\"histogram accuracy: {:.2f}%\".format(acc * 100))","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:45:17.660929Z","iopub.execute_input":"2023-03-10T17:45:17.661323Z","iopub.status.idle":"2023-03-10T17:46:18.143774Z","shell.execute_reply.started":"2023-03-10T17:45:17.661285Z","shell.execute_reply":"2023-03-10T17:46:18.142610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- OVERFITTING!","metadata":{}},{"cell_type":"markdown","source":"- let's try k = 158","metadata":{}},{"cell_type":"code","source":"model = KNeighborsClassifier(n_neighbors=k,n_jobs=-1)\nmodel.fit(trainRI, trainRL)\nprint(model.score(trainRI, trainRL))\nacc = model.score(testRI, testRL)\nprint(\"Raw pixel accuracy: {:.2f}%\".format(acc * 100))","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:46:18.145288Z","iopub.execute_input":"2023-03-10T17:46:18.145890Z","iopub.status.idle":"2023-03-10T17:47:19.057461Z","shell.execute_reply.started":"2023-03-10T17:46:18.145844Z","shell.execute_reply":"2023-03-10T17:47:19.056195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = KNeighborsClassifier(n_neighbors=k,n_jobs=-1)\nmodel.fit(trainFeat, trainLabels)\nprint(model.score(trainFeat, trainLabels))\nacc = model.score(testFeat, testLabels)\nprint(\"histogram accuracy: {:.2f}%\".format(acc * 100))","metadata":{"execution":{"iopub.status.busy":"2023-03-10T17:47:19.060175Z","iopub.execute_input":"2023-03-10T17:47:19.060594Z","iopub.status.idle":"2023-03-10T17:48:20.259162Z","shell.execute_reply.started":"2023-03-10T17:47:19.060552Z","shell.execute_reply":"2023-03-10T17:48:20.257995Z"},"trusted":true},"execution_count":null,"outputs":[]}]}