{"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-10T04:24:47.656287Z","iopub.execute_input":"2022-02-10T04:24:47.656632Z","iopub.status.idle":"2022-02-10T04:25:04.231840Z","shell.execute_reply.started":"2022-02-10T04:24:47.656587Z","shell.execute_reply":"2022-02-10T04:25:04.230965Z"},"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.metrics.pairwise import cosine_similarity\nimport pandas as pd\nimport matplotlib.pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2022-02-10T04:25:04.236152Z","iopub.execute_input":"2022-02-10T04:25:04.236345Z","iopub.status.idle":"2022-02-10T04:25:06.081832Z","shell.execute_reply.started":"2022-02-10T04:25:04.236319Z","shell.execute_reply":"2022-02-10T04:25:06.080713Z"},"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 = 10\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)\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-10T04:25:06.083552Z","iopub.execute_input":"2022-02-10T04:25:06.083885Z","iopub.status.idle":"2022-02-10T04:25:23.189590Z","shell.execute_reply.started":"2022-02-10T04:25:06.083846Z","shell.execute_reply":"2022-02-10T04:25:23.188166Z"},"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-10T04:25:23.193735Z","iopub.execute_input":"2022-02-10T04:25:23.194116Z","iopub.status.idle":"2022-02-10T04:25:23.201806Z","shell.execute_reply.started":"2022-02-10T04:25:23.194076Z","shell.execute_reply":"2022-02-10T04:25:23.200790Z"},"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-10T04:25:23.203444Z","iopub.execute_input":"2022-02-10T04:25:23.203724Z","iopub.status.idle":"2022-02-10T04:29:13.964993Z","shell.execute_reply.started":"2022-02-10T04:25:23.203687Z","shell.execute_reply":"2022-02-10T04:29:13.964257Z"},"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":{"execution":{"iopub.status.busy":"2022-02-10T04:29:13.966669Z","iopub.execute_input":"2022-02-10T04:29:13.967112Z","iopub.status.idle":"2022-02-10T04:33:27.355522Z","shell.execute_reply.started":"2022-02-10T04:29:13.967073Z","shell.execute_reply":"2022-02-10T04:33:27.354379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(cluster_centers.shape)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T04:33:27.356955Z","iopub.execute_input":"2022-02-10T04:33:27.357196Z","iopub.status.idle":"2022-02-10T04:33:27.364198Z","shell.execute_reply.started":"2022-02-10T04:33:27.357167Z","shell.execute_reply":"2022-02-10T04:33:27.362940Z"},"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":{"execution":{"iopub.status.busy":"2022-02-10T04:33:27.365961Z","iopub.execute_input":"2022-02-10T04:33:27.366486Z","iopub.status.idle":"2022-02-10T04:36:33.415466Z","shell.execute_reply.started":"2022-02-10T04:33:27.366455Z","shell.execute_reply":"2022-02-10T04:36:33.414825Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T04:36:33.418827Z","iopub.execute_input":"2022-02-10T04:36:33.419233Z","iopub.status.idle":"2022-02-10T04:36:33.439061Z","shell.execute_reply.started":"2022-02-10T04:36:33.419202Z","shell.execute_reply":"2022-02-10T04:36:33.438466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train[0],y_train[0])","metadata":{"execution":{"iopub.status.busy":"2022-02-10T04:36:33.440179Z","iopub.execute_input":"2022-02-10T04:36:33.441502Z","iopub.status.idle":"2022-02-10T04:36:33.449872Z","shell.execute_reply.started":"2022-02-10T04:36:33.441470Z","shell.execute_reply":"2022-02-10T04:36:33.449347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = []\nquery_idx = 7\nfor idx,img in enumerate(x_train):\n    cos_sim = cosine_similarity(img.reshape(1,-1),x_test[query_idx].reshape(1,-1))\n    res.append([cos_sim[0][0],idx])\nresdf = pd.DataFrame(res,columns = ['similarity','index'])\nresdf = resdf.sort_values('similarity', ascending = False)\n\nplt.figure(figsize=(20,10))\n\nplt.subplot(3,5,3)\nplt.imshow(cv2.cvtColor(test_images[query_idx],cv2.COLOR_BGR2RGB))\nplt.title('Query')\nplt.xticks([])\nplt.yticks([])\nfor index,[idx,res] in enumerate(resdf.iterrows()):\n    if(index>=10):\n        break\n    print(res['similarity'],res['index'])\n    plt.subplot(3,5,index+6)\n    plt.imshow(cv2.cvtColor(train_images[res['index'].astype('int')],cv2.COLOR_BGR2RGB))\n    plt.title('Similarity: ' + str(res['similarity']))\n    plt.xticks([])\n    plt.yticks([])","metadata":{"execution":{"iopub.status.busy":"2022-02-10T04:48:59.807829Z","iopub.execute_input":"2022-02-10T04:48:59.808116Z","iopub.status.idle":"2022-02-10T04:49:02.732764Z","shell.execute_reply.started":"2022-02-10T04:48:59.808090Z","shell.execute_reply":"2022-02-10T04:49:02.729088Z"},"trusted":true},"execution_count":null,"outputs":[]}]}