{"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":"Unzip dataset","metadata":{}},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2022-02-09T12:04:58.883814Z","iopub.execute_input":"2022-02-09T12:04:58.884599Z","iopub.status.idle":"2022-02-09T12:05:20.686158Z","shell.execute_reply.started":"2022-02-09T12:04:58.884425Z","shell.execute_reply":"2022-02-09T12:05:20.684996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Import libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport cv2\nimport os\nfrom tqdm import tqdm\nfrom sklearn.cluster import KMeans\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sns\nfrom sklearn.metrics import accuracy_score\n","metadata":{"execution":{"iopub.status.busy":"2022-02-09T12:05:20.689002Z","iopub.execute_input":"2022-02-09T12:05:20.689538Z","iopub.status.idle":"2022-02-09T12:05:23.052681Z","shell.execute_reply.started":"2022-02-09T12:05:20.689478Z","shell.execute_reply":"2022-02-09T12:05:23.051349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Read dataset (images and labels)","metadata":{}},{"cell_type":"code","source":"cnt_train = 6000\ncnt_test = 700\n\nlabel_id = {'cat' : 0, 'dog' : 1}\ndata_path = '/kaggle/working/train'\ndef read_data(data_type):\n    images = []\n    labels = []\n    cnt = {0 : 0, 1 : 0}\n    for idx,filename in enumerate(os.listdir(data_path)):\n        if(data_type == 0 and idx == cnt_train):\n            break\n        if(data_type == 1 and idx == cnt_train + cnt_test):\n            break\n        if(data_type == 1 and idx < cnt_train):\n            continue  \n        path = data_path + \"/\" + filename\n        img = cv2.imread(path,0)\n        cate = filename.split(\".\")[0]\n        images.append(img)\n        labels.append(label_id[cate])\n        cnt[label_id[cate]]+=1\n    print(cnt[0],cnt[1])\n    return images,labels\n\ntrain_images,y_train = read_data(0)  \ntest_images,y_test = read_data(1) ","metadata":{"execution":{"iopub.status.busy":"2022-02-09T12:05:23.054366Z","iopub.execute_input":"2022-02-09T12:05:23.054656Z","iopub.status.idle":"2022-02-09T12:05:35.800406Z","shell.execute_reply.started":"2022-02-09T12:05:23.054619Z","shell.execute_reply":"2022-02-09T12:05:35.798830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_images),len(y_train))\nprint(len(test_images),len(y_test))","metadata":{"execution":{"iopub.status.busy":"2022-02-09T12:05:35.803227Z","iopub.execute_input":"2022-02-09T12:05:35.803943Z","iopub.status.idle":"2022-02-09T12:05:35.813099Z","shell.execute_reply.started":"2022-02-09T12:05:35.803897Z","shell.execute_reply":"2022-02-09T12:05:35.811747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Extract features and descriptors from the images with SIFT","metadata":{}},{"cell_type":"code","source":"def extract_sift_features(images):\n    all_descriptors = []\n    descriptors = []\n    sift = cv2.xfeatures2d.SIFT_create()\n    for img in tqdm(images):\n        kp, des = sift.detectAndCompute(img,None)\n        all_descriptors.extend(des)\n        descriptors.append(des)\n    return all_descriptors,descriptors\n\nall_descriptors,train_descriptors = extract_sift_features(train_images) \n_,test_descriptors = extract_sift_features(test_images)","metadata":{"execution":{"iopub.status.busy":"2022-02-09T12:05:35.816437Z","iopub.execute_input":"2022-02-09T12:05:35.817334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Train KMeans clustering model on all the descriptors list","metadata":{}},{"cell_type":"code","source":"kmeans = KMeans(n_clusters = 75, max_iter=10,verbose=1,n_init=2)\nkmeans.fit(all_descriptors)\ncluster_centers = kmeans.cluster_centers_ \nprint(cluster_centers)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(cluster_centers.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Convert the images into histograms according to the clusters","metadata":{}},{"cell_type":"code","source":"def convert_features_to_vbow(descriptors):\n    vbow=[]\n    for descriptor in tqdm(descriptors):\n        histogram = np.zeros(len(cluster_centers))\n        indexes = kmeans.predict(descriptor.astype('float'))\n        for idx in indexes:\n            histogram[idx]+=1\n        vbow.append(histogram)\n    return vbow\n\nx_train = convert_features_to_vbow(train_descriptors) \nx_test = convert_features_to_vbow(test_descriptors)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Change datatype into numpy array","metadata":{}},{"cell_type":"code","source":"x_train = np.array(x_train)\ny_train = np.array(y_train)\ny_test = np.array(y_test)\nprint(y_train.shape)\nprint(x_train.shape)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train[0],y_train[0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Train a KNN classifier on the training dataset","metadata":{}},{"cell_type":"code","source":"knn = KNeighborsClassifier(n_neighbors = 3)\nknn.fit(x_train,y_train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predict the training and testing dataset","metadata":{}},{"cell_type":"code","source":"y_pred_train = knn.predict(x_train)\ny_pred_test = knn.predict(x_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Print the accuracy of the prediction (training and testing)","metadata":{}},{"cell_type":"code","source":"print(accuracy_score(y_pred_train,y_train))\nprint(accuracy_score(y_pred_test,y_test))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot the testing dataset confusion matrix","metadata":{}},{"cell_type":"code","source":"sns.heatmap(confusion_matrix(y_test,y_pred_test), annot = True, fmt = 'g')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}