{"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":"# ****Import Libraries****","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nimport pylab as pl\nimport random\nfrom sklearn.metrics import confusion_matrix,accuracy_score\nimport os\nfrom sklearn.metrics import classification_report\nfrom matplotlib import pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:29:17.686178Z","iopub.execute_input":"2022-02-10T18:29:17.686648Z","iopub.status.idle":"2022-02-10T18:29:18.281654Z","shell.execute_reply.started":"2022-02-10T18:29:17.686556Z","shell.execute_reply":"2022-02-10T18:29:18.280884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"unzip the data zip (skip if already done)","metadata":{}},{"cell_type":"code","source":"!unzip '/kaggle/input/dogs-vs-cats/train.zip'","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:29:18.282973Z","iopub.execute_input":"2022-02-10T18:29:18.283271Z","iopub.status.idle":"2022-02-10T18:29:18.964821Z","shell.execute_reply.started":"2022-02-10T18:29:18.283233Z","shell.execute_reply":"2022-02-10T18:29:18.964024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/working/train'","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:29:18.968363Z","iopub.execute_input":"2022-02-10T18:29:18.968602Z","iopub.status.idle":"2022-02-10T18:29:18.974262Z","shell.execute_reply.started":"2022-02-10T18:29:18.968571Z","shell.execute_reply":"2022-02-10T18:29:18.972848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Prepare Dataset****","metadata":{}},{"cell_type":"code","source":"classes = [\"cat\", \"dog\"]\ndata = []\nlabel = []\nsize = (32, 32)\nfor path in os.listdir(train_path):\n    img_path = os.path.join(train_path, path)\n    data.append(cv2.imread(img_path))\n    if \"dog\" in path:\n        label.append(\"dog\")\n    else:\n        label.append(\"cat\")","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:29:18.978563Z","iopub.execute_input":"2022-02-10T18:29:18.978858Z","iopub.status.idle":"2022-02-10T18:30:11.997693Z","shell.execute_reply.started":"2022-02-10T18:29:18.978820Z","shell.execute_reply":"2022-02-10T18:30:11.996625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Data length {len(data)}, label length {len(label)}')","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:30:11.999148Z","iopub.execute_input":"2022-02-10T18:30:11.999447Z","iopub.status.idle":"2022-02-10T18:30:12.005005Z","shell.execute_reply.started":"2022-02-10T18:30:11.999408Z","shell.execute_reply":"2022-02-10T18:30:12.004262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****My GPU cannot handle too much image, so i decided only use 1000 images, 950 training, 50 testing****","metadata":{}},{"cell_type":"code","source":"dataset = list(zip(data, label))\nrandom.shuffle(dataset)\n\nlength = 1000\nmid = 950\nX_train = []\ny_train = []\n\nx_test = []\ny_test = []","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:30:12.006179Z","iopub.execute_input":"2022-02-10T18:30:12.006841Z","iopub.status.idle":"2022-02-10T18:30:12.046309Z","shell.execute_reply.started":"2022-02-10T18:30:12.006802Z","shell.execute_reply":"2022-02-10T18:30:12.045557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx, (img_data, label) in enumerate(dataset):\n    if idx < length:\n        if idx < mid:  \n            X_train.append(img_data)\n            y_train.append(label)\n        else:\n            x_test.append(img_data)\n            y_test.append(label)\n        ","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:30:12.047513Z","iopub.execute_input":"2022-02-10T18:30:12.047804Z","iopub.status.idle":"2022-02-10T18:30:12.060433Z","shell.execute_reply.started":"2022-02-10T18:30:12.047765Z","shell.execute_reply":"2022-02-10T18:30:12.059273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'X_train {len(X_train)}')\nprint(f'y_train {len(y_train)}')\nprint(f'x_test {len(x_test)}')\nprint(f'y_test {len(y_test)}')","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:30:12.061982Z","iopub.execute_input":"2022-02-10T18:30:12.062392Z","iopub.status.idle":"2022-02-10T18:30:12.071024Z","shell.execute_reply.started":"2022-02-10T18:30:12.062351Z","shell.execute_reply":"2022-02-10T18:30:12.069966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****import orb detection model for feature detection****","metadata":{}},{"cell_type":"code","source":"orb = cv2.ORB_create()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:30:12.072540Z","iopub.execute_input":"2022-02-10T18:30:12.072961Z","iopub.status.idle":"2022-02-10T18:30:12.079388Z","shell.execute_reply.started":"2022-02-10T18:30:12.072923Z","shell.execute_reply":"2022-02-10T18:30:12.078484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****sample image****","metadata":{}},{"cell_type":"code","source":"plt.imshow(X_train[3])\nplt.title(y_train[3])","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:30:12.081163Z","iopub.execute_input":"2022-02-10T18:30:12.081525Z","iopub.status.idle":"2022-02-10T18:30:12.371298Z","shell.execute_reply.started":"2022-02-10T18:30:12.081485Z","shell.execute_reply":"2022-02-10T18:30:12.370510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****the function for drawing keypoints****","metadata":{}},{"cell_type":"code","source":"def draw_keypoints(vis, keypoints, color = (0, 255, 255)):\n    for kp in keypoints:\n            x, y = kp.pt\n            plt.imshow(cv2.circle(vis, (int(x), int(y)), 2, color))","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:30:12.372348Z","iopub.execute_input":"2022-02-10T18:30:12.372608Z","iopub.status.idle":"2022-02-10T18:30:12.378627Z","shell.execute_reply.started":"2022-02-10T18:30:12.372574Z","shell.execute_reply":"2022-02-10T18:30:12.377968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = X_train[3]\nkp = orb.detect(image, None)\nkp, des = orb.compute(image, kp)\nimg = draw_keypoints(image, kp)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:30:12.380284Z","iopub.execute_input":"2022-02-10T18:30:12.380814Z","iopub.status.idle":"2022-02-10T18:30:30.922227Z","shell.execute_reply.started":"2022-02-10T18:30:12.380777Z","shell.execute_reply":"2022-02-10T18:30:30.921361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Appending descriptors of the training images in list****","metadata":{}},{"cell_type":"code","source":"descriptors_train = []\nfor train_img in X_train:\n    kp=orb.detect(train_img,None)\n    keypoints,descriptor= orb.compute(train_img, kp)\n    descriptors_train.append((train_img,descriptor))","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:30:30.926013Z","iopub.execute_input":"2022-02-10T18:30:30.926275Z","iopub.status.idle":"2022-02-10T18:30:37.944093Z","shell.execute_reply.started":"2022-02-10T18:30:30.926240Z","shell.execute_reply":"2022-02-10T18:30:37.943289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"descriptors = descriptors_train[0][1]\nfor image_path,descriptor in descriptors_train[1:]:\n    if descriptor is not None:\n         descriptors=np.vstack((descriptors,descriptor)) ","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:30:37.945318Z","iopub.execute_input":"2022-02-10T18:30:37.945589Z","iopub.status.idle":"2022-02-10T18:30:39.492804Z","shell.execute_reply.started":"2022-02-10T18:30:37.945552Z","shell.execute_reply":"2022-02-10T18:30:39.492019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"descriptors.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:30:39.494298Z","iopub.execute_input":"2022-02-10T18:30:39.494576Z","iopub.status.idle":"2022-02-10T18:30:39.500094Z","shell.execute_reply.started":"2022-02-10T18:30:39.494536Z","shell.execute_reply":"2022-02-10T18:30:39.499379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"descriptors_float=descriptors.astype(float)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:30:39.501454Z","iopub.execute_input":"2022-02-10T18:30:39.501917Z","iopub.status.idle":"2022-02-10T18:30:39.535732Z","shell.execute_reply.started":"2022-02-10T18:30:39.501878Z","shell.execute_reply":"2022-02-10T18:30:39.534946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.cluster.vq import kmeans,vq","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:30:39.537176Z","iopub.execute_input":"2022-02-10T18:30:39.537614Z","iopub.status.idle":"2022-02-10T18:30:39.547113Z","shell.execute_reply.started":"2022-02-10T18:30:39.537574Z","shell.execute_reply":"2022-02-10T18:30:39.546293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Performing KMeans clustering on descriptors****","metadata":{}},{"cell_type":"code","source":"k=200\nvoc,variance=kmeans(descriptors_float,k,1)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:30:39.548683Z","iopub.execute_input":"2022-02-10T18:30:39.549067Z","iopub.status.idle":"2022-02-10T18:33:20.206442Z","shell.execute_reply.started":"2022-02-10T18:30:39.549029Z","shell.execute_reply":"2022-02-10T18:33:20.205548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"des_len = len(descriptors_train)\nprint(des_len)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:33:20.211437Z","iopub.execute_input":"2022-02-10T18:33:20.212249Z","iopub.status.idle":"2022-02-10T18:33:20.221653Z","shell.execute_reply.started":"2022-02-10T18:33:20.212200Z","shell.execute_reply":"2022-02-10T18:33:20.220837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Creating histogram of training image****","metadata":{}},{"cell_type":"code","source":"im_features=np.zeros((des_len,k),\"float32\")\nfor i in range(des_len):\n    words,distance=vq(descriptors_train[i][1],voc)\n    for w in words:\n        im_features[i][w]+=1","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:33:20.226429Z","iopub.execute_input":"2022-02-10T18:33:20.226959Z","iopub.status.idle":"2022-02-10T18:33:24.553775Z","shell.execute_reply.started":"2022-02-10T18:33:20.226917Z","shell.execute_reply":"2022-02-10T18:33:24.552899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Scale Feature****","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler().fit(im_features)\nim_features=scaler.transform(im_features)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:33:24.558964Z","iopub.execute_input":"2022-02-10T18:33:24.561261Z","iopub.status.idle":"2022-02-10T18:33:24.575230Z","shell.execute_reply.started":"2022-02-10T18:33:24.561210Z","shell.execute_reply":"2022-02-10T18:33:24.574318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(im_features))\nprint(len(y_train))","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:33:24.579925Z","iopub.execute_input":"2022-02-10T18:33:24.582152Z","iopub.status.idle":"2022-02-10T18:33:24.587467Z","shell.execute_reply.started":"2022-02-10T18:33:24.582105Z","shell.execute_reply":"2022-02-10T18:33:24.586630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Use SVM as classification model****","metadata":{}},{"cell_type":"code","source":"from sklearn.svm import LinearSVC\nclf=LinearSVC(max_iter=80000)\nclf.fit(im_features,np.array(y_train))","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:33:24.589001Z","iopub.execute_input":"2022-02-10T18:33:24.589389Z","iopub.status.idle":"2022-02-10T18:33:29.399752Z","shell.execute_reply.started":"2022-02-10T18:33:24.589355Z","shell.execute_reply":"2022-02-10T18:33:29.399021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"des_list_test=[]","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:33:29.401258Z","iopub.execute_input":"2022-02-10T18:33:29.401541Z","iopub.status.idle":"2022-02-10T18:33:29.406242Z","shell.execute_reply.started":"2022-02-10T18:33:29.401505Z","shell.execute_reply":"2022-02-10T18:33:29.405477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img in x_test:\n    kp=orb.detect(img,None)\n    keypoints_test,descriptor_test= orb.compute(img, kp)\n    des_list_test.append((img,descriptor_test))","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:33:29.407536Z","iopub.execute_input":"2022-02-10T18:33:29.408296Z","iopub.status.idle":"2022-02-10T18:33:29.772489Z","shell.execute_reply.started":"2022-02-10T18:33:29.408259Z","shell.execute_reply":"2022-02-10T18:33:29.771703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_len = len(des_list_test)\ntest_features=np.zeros((new_len,k),\"float32\")\nfor i in range(new_len):\n    words,distance=vq(des_list_test[i][1],voc)\n    for w in words:\n        test_features[i][w]+=1","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:33:29.773651Z","iopub.execute_input":"2022-02-10T18:33:29.773922Z","iopub.status.idle":"2022-02-10T18:33:29.999537Z","shell.execute_reply.started":"2022-02-10T18:33:29.773886Z","shell.execute_reply":"2022-02-10T18:33:29.998645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features=scaler.transform(test_features)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:33:30.000722Z","iopub.execute_input":"2022-02-10T18:33:30.001282Z","iopub.status.idle":"2022-02-10T18:33:30.007021Z","shell.execute_reply.started":"2022-02-10T18:33:30.001240Z","shell.execute_reply":"2022-02-10T18:33:30.006053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_classes=[]\nfor i in clf.predict(test_features):\n    predict_classes.append(i)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:33:30.008649Z","iopub.execute_input":"2022-02-10T18:33:30.009465Z","iopub.status.idle":"2022-02-10T18:33:30.021002Z","shell.execute_reply.started":"2022-02-10T18:33:30.009423Z","shell.execute_reply":"2022-02-10T18:33:30.020241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy=accuracy_score(y_test,predict_classes)\nprint(f'Accuracy : {accuracy}')","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:33:30.022912Z","iopub.execute_input":"2022-02-10T18:33:30.024251Z","iopub.status.idle":"2022-02-10T18:33:30.033104Z","shell.execute_reply.started":"2022-02-10T18:33:30.024085Z","shell.execute_reply":"2022-02-10T18:33:30.032145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(y_test, predict_classes)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:33:30.035005Z","iopub.execute_input":"2022-02-10T18:33:30.036491Z","iopub.status.idle":"2022-02-10T18:33:30.047172Z","shell.execute_reply.started":"2022-02-10T18:33:30.036447Z","shell.execute_reply":"2022-02-10T18:33:30.046471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, predict_classes))","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:33:30.048357Z","iopub.execute_input":"2022-02-10T18:33:30.050123Z","iopub.status.idle":"2022-02-10T18:33:30.070731Z","shell.execute_reply.started":"2022-02-10T18:33:30.050080Z","shell.execute_reply":"2022-02-10T18:33:30.070042Z"},"trusted":true},"execution_count":null,"outputs":[]}]}