{"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":"code","source":"#!/usr/bin/env python\n# coding: utf-8\n\n# In[12]:\n\n\n\n\nimport numpy as np\nimport pandas as pd\nfrom sklearn import datasets\nfrom skimage.feature import hog\nfrom sklearn import svm\nimport cv2\nimport os\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nfrom sklearn.svm import LinearSVC\nfrom sklearn.svm import SVC\nTraining_data = 'c://assigement/train'\ncategories = []\nimages_names = []\ndata_size=0\nvariables = []\n\nfor file_name in os.listdir(Training_data):\n    # append the image name to the list of names\n    images_names.append(file_name)\n\n    # define the cateory (class (dog or cat)) of each image \n    if 'dog' in file_name:\n        categories.append(1)\n    else:\n        categories.append(0)\n#to read data\n    my_image = cv2.imread(os.path.join(Training_data, file_name))\n#resiz the image\n    img_resize = cv2.resize(my_image, (128, 64))\n# extract the features using HOG\n    fd, hog_img = hog(img_resize, orientations = 9, pixels_per_cell = (8, 8),cells_per_block = (2, 2), visualize = True, multichannel = True)\n#add features to list\n\n    variables.append(fd)\n    data_size+=1\n    if data_size ==2200:\n        break\n\n\n\nX= variables\nY= categories\n\n\n\n#train the SVM on the extracted features\nX_train, X_test, Y_Train, Y_test = train_test_split(X, Y,test_size = 0.1, random_state=10,shuffle=True)\nC=0.1\n\n######lin_svc\nlin_svc = svm.LinearSVC(C=C).fit(X_train, Y_Train)\naccuracy_test = lin_svc.score(X_test, Y_test)\naccuracy_train = lin_svc.score(X_train, Y_Train)\nprint('accuracy_train of lin_svc model : ' , accuracy_train*100)\naccuracy_test = lin_svc.score(X_test, Y_test)\nprint('accuracy_test of lin_svc model : ' , accuracy_test*100)\n\nprint(\"---------------------------------------------\")\n######SVC\nsvc = svm.SVC(kernel='linear', C=C).fit(X_train, Y_Train)\naccuracy_train = svc.score(X_train, Y_Train)\nprint('accuracy_train of svc model : ' , accuracy_train*100)\naccuracy_test = svc.score(X_test, Y_test)\nprint('accuracy_test of svc model : ' , accuracy_test*100)\n\nprint(\"---------------------------------------------\")\n#######rbf_svc\nrbf_svc = svm.SVC(kernel='rbf', gamma=0.8, C=C).fit(X_train, Y_Train)\naccuracy_train = rbf_svc.score(X_train, Y_Train)\nprint('accuracy_train of rbf_svc model : ' , accuracy_train*100)\naccuracy_test = rbf_svc.score(X_test, Y_test)\nprint('accuracy_test of rbf_svc model : ' , accuracy_test*100)\n#######poly_svc\n\nprint(\"---------------------------------------------\")\npoly_svc = svm.SVC(kernel='poly', degree=3, C=C).fit(X_train, Y_Train)\naccuracy_train = poly_svc.score(X_train, Y_Train)\nprint('accuracy_train of poly_svc model : ' , accuracy_train*100)\naccuracy_test = poly_svc.score(X_test, Y_test)\nprint('accuracy_test of poly_svc model : ' , accuracy_test*100)\n\n   \n\n\n\n\n\n\n\n\n\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-20T11:50:57.079897Z","iopub.execute_input":"2022-05-20T11:50:57.08102Z","iopub.status.idle":"2022-05-20T11:50:59.891653Z","shell.execute_reply.started":"2022-05-20T11:50:57.080875Z","shell.execute_reply":"2022-05-20T11:50:59.889934Z"},"trusted":true},"execution_count":null,"outputs":[]}]}