{"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":"# Cats vs Dogs Classification using SVM\n- [x] Read training images\n- [x] Resize the images to a standered size (128, 64)\n- [x] Extract the features using HOG\n- [x] Train the SVM on the extracted features\n- [x] Calculate the training accuracy\n- [x] Test the trained model using the test data\n- [x] Calculate the testing accuracy","metadata":{}},{"cell_type":"markdown","source":"## Imports","metadata":{}},{"cell_type":"code","source":"import cv2\nimport os\nimport pandas as pd \nimport numpy as np\nimport random\nfrom skimage.feature import hog\nfrom sklearn import svm","metadata":{"execution":{"iopub.status.busy":"2021-12-17T16:50:17.943771Z","iopub.execute_input":"2021-12-17T16:50:17.944121Z","iopub.status.idle":"2021-12-17T16:50:17.949165Z","shell.execute_reply.started":"2021-12-17T16:50:17.94409Z","shell.execute_reply":"2021-12-17T16:50:17.948107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature Extraction \n- Read training images \n- define the category of each image\n- read each image\n- resize each image to (128, 64)\n- extract the features of the image using hog\n- construct lists for categories, images names, features","metadata":{}},{"cell_type":"code","source":"training_path = './training/train'\n\ni = 0\n\ncategories = []\nimgs_names = []\nfeatures = []\n\nfor filename in os.listdir(training_path):\n    # append the image name to the list of names\n    imgs_names.append(filename)\n\n    # define the cateory (class (dog or cat)) of each image \n    if 'dog' in filename:\n        categories.append(1)\n    else:\n        categories.append(0)\n        \n    \n    # read the image \n    img = cv2.imread(os.path.join(training_path, filename))\n    # resize the image\n    resized_img = cv2.resize(img, (128, 64))\n    # extract the features using HOG\n    fd, hog_img = hog(resized_img, orientations = 9, pixels_per_cell = (8, 8), \n                      cells_per_block = (2, 2), visualize = True, multichannel = True)\n    # append the feature array to the list of features \n    features.append(fd)\n    \n    # take only the first 3000 data images\n    i += 1\n    if i == 3000:\n        break","metadata":{"execution":{"iopub.status.busy":"2021-12-17T16:50:17.950751Z","iopub.execute_input":"2021-12-17T16:50:17.951016Z","iopub.status.idle":"2021-12-17T16:52:09.021853Z","shell.execute_reply.started":"2021-12-17T16:50:17.950987Z","shell.execute_reply":"2021-12-17T16:52:09.020824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(categories)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T16:52:09.023489Z","iopub.execute_input":"2021-12-17T16:52:09.023752Z","iopub.status.idle":"2021-12-17T16:52:09.030075Z","shell.execute_reply.started":"2021-12-17T16:52:09.023713Z","shell.execute_reply":"2021-12-17T16:52:09.029196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(features)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T16:52:09.031288Z","iopub.execute_input":"2021-12-17T16:52:09.031901Z","iopub.status.idle":"2021-12-17T16:52:09.043377Z","shell.execute_reply.started":"2021-12-17T16:52:09.031869Z","shell.execute_reply":"2021-12-17T16:52:09.042809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train the SVM on the extracted features","metadata":{}},{"cell_type":"code","source":"X = features\nY = categories\n\n# we create an instance of SVM and fit out data.\nC = 0.1  # SVM regularization parameter\nsvc = svm.SVC(kernel='poly', C=C).fit(X, Y)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:05:25.751476Z","iopub.execute_input":"2021-12-17T17:05:25.752126Z","iopub.status.idle":"2021-12-17T17:06:09.285864Z","shell.execute_reply.started":"2021-12-17T17:05:25.752083Z","shell.execute_reply":"2021-12-17T17:06:09.284551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predict using the trainig data","metadata":{}},{"cell_type":"code","source":"predictions = svc.predict(X)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:06:09.287385Z","iopub.execute_input":"2021-12-17T17:06:09.287638Z","iopub.status.idle":"2021-12-17T17:06:51.167348Z","shell.execute_reply.started":"2021-12-17T17:06:09.287606Z","shell.execute_reply":"2021-12-17T17:06:51.16642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Calculate the training set accuracy","metadata":{}},{"cell_type":"code","source":"accuracy = np.mean(predictions == Y)\naccuracy","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:06:51.168903Z","iopub.execute_input":"2021-12-17T17:06:51.169287Z","iopub.status.idle":"2021-12-17T17:06:51.177899Z","shell.execute_reply.started":"2021-12-17T17:06:51.169244Z","shell.execute_reply":"2021-12-17T17:06:51.176873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Read the testing set","metadata":{}},{"cell_type":"code","source":"i = 0\n\ntest_names = []\ntest_features = []\ntest_categories = []\nfor filename in os.listdir(training_path):\n    # take only the last 2000 data images from the first 5000 images\n    if(i > 3000):\n        # append the image name to the list of names\n        test_names.append(filename)\n\n        # define the cateory (class (dog or cat)) of each image \n        if 'dog' in filename:\n            test_categories.append(1)\n        else:\n            test_categories.append(0)\n\n\n        # read the image \n        img = cv2.imread(os.path.join(training_path, filename))\n        # resize the image\n        resized_img = cv2.resize(img, (128, 64))\n        # extract the features using HOG\n        fd, hog_img = hog(resized_img, orientations = 9, pixels_per_cell = (8, 8), \n                          cells_per_block = (2, 2), visualize = True, multichannel = True)\n        # append the feature array to the list of features \n        test_features.append(fd)\n\n    i += 1\n    \n    # take only the last 2000 data images from the first 5000 images\n    if i == 5000:\n        break","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:09:47.791885Z","iopub.execute_input":"2021-12-17T17:09:47.792117Z","iopub.status.idle":"2021-12-17T17:11:01.814551Z","shell.execute_reply.started":"2021-12-17T17:09:47.792089Z","shell.execute_reply":"2021-12-17T17:11:01.813639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predict for the testing set","metadata":{}},{"cell_type":"code","source":"test_X = test_features\ntest_Y = test_categories\n\ntest_predictions = svc.predict(test_X)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:11:01.815632Z","iopub.execute_input":"2021-12-17T17:11:01.815862Z","iopub.status.idle":"2021-12-17T17:11:29.775241Z","shell.execute_reply.started":"2021-12-17T17:11:01.815824Z","shell.execute_reply":"2021-12-17T17:11:29.774042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Calculate the accuracy of the testing set","metadata":{}},{"cell_type":"code","source":"accuracy = np.mean(test_predictions == test_Y)\naccuracy","metadata":{"execution":{"iopub.status.busy":"2021-12-17T17:11:29.781183Z","iopub.execute_input":"2021-12-17T17:11:29.781542Z","iopub.status.idle":"2021-12-17T17:11:29.790838Z","shell.execute_reply.started":"2021-12-17T17:11:29.781499Z","shell.execute_reply":"2021-12-17T17:11:29.789426Z"},"trusted":true},"execution_count":null,"outputs":[]}]}