{"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":"## load train data","metadata":{}},{"cell_type":"code","source":"import os\nos.listdir('/kaggle/input')","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:50:54.732631Z","iopub.execute_input":"2021-11-09T20:50:54.732991Z","iopub.status.idle":"2021-11-09T20:50:54.754186Z","shell.execute_reply.started":"2021-11-09T20:50:54.732899Z","shell.execute_reply":"2021-11-09T20:50:54.753422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/input/cmpe188image-classification/cmpe188_train/cmpe188_train')","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:50:55.041761Z","iopub.execute_input":"2021-11-09T20:50:55.042256Z","iopub.status.idle":"2021-11-09T20:50:55.051199Z","shell.execute_reply.started":"2021-11-09T20:50:55.042222Z","shell.execute_reply":"2021-11-09T20:50:55.050505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.listdir('/kaggle/input/cmpe188image-classification/cmpe188_test (2)/cmpe188_test')","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:50:55.065120Z","iopub.execute_input":"2021-11-09T20:50:55.065827Z","iopub.status.idle":"2021-11-09T20:50:55.069293Z","shell.execute_reply.started":"2021-11-09T20:50:55.065789Z","shell.execute_reply":"2021-11-09T20:50:55.068621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport random\n\ntrain_dir = '/kaggle/input/cmpe188image-classification/cmpe188_train/cmpe188_train'\n\ntargets = ['buildings','forest','glacier','mountain','sea','street']\ntrain_data = []\n#load train set\nfor target in targets:\n    train_path = os.path.join(train_dir,target)\n    label = targets.index(target)\n    for img in os.listdir(train_path):\n        train_imgpath = os.path.join(train_path, img) #train_imgpath = './xxx/xxx/xxx.jpg'\n        train_img = plt.imread(train_imgpath)\n        try:\n            train_img = cv2.resize(train_img,(144,144))\n            train_image = np.array(train_img)#.flatten()\n            train_data.append([train_image,label])\n        except Exception as e:\n            pass\n\nlen(train_data)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:50:55.071254Z","iopub.execute_input":"2021-11-09T20:50:55.071845Z","iopub.status.idle":"2021-11-09T20:51:52.750183Z","shell.execute_reply.started":"2021-11-09T20:50:55.071785Z","shell.execute_reply":"2021-11-09T20:51:52.749308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.shuffle(train_data)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:51:52.751683Z","iopub.execute_input":"2021-11-09T20:51:52.751937Z","iopub.status.idle":"2021-11-09T20:51:52.771510Z","shell.execute_reply.started":"2021-11-09T20:51:52.751891Z","shell.execute_reply":"2021-11-09T20:51:52.770920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### train data has been shuffled before here ","metadata":{}},{"cell_type":"code","source":"features = []\nlabels = []\n\nfor feature, label in train_data:\n    features.append(feature)\n    labels.append(label)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:51:52.774270Z","iopub.execute_input":"2021-11-09T20:51:52.774465Z","iopub.status.idle":"2021-11-09T20:51:52.784460Z","shell.execute_reply.started":"2021-11-09T20:51:52.774441Z","shell.execute_reply":"2021-11-09T20:51:52.782971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np_features = np.array(features)\nnp_lables = np.array(labels)\nnp_features = np_features.astype(np.int32)\nnp_lables","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:51:52.786855Z","iopub.execute_input":"2021-11-09T20:51:52.787064Z","iopub.status.idle":"2021-11-09T20:51:53.927230Z","shell.execute_reply.started":"2021-11-09T20:51:52.787030Z","shell.execute_reply":"2021-11-09T20:51:53.926484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np_features.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:51:53.928529Z","iopub.execute_input":"2021-11-09T20:51:53.928791Z","iopub.status.idle":"2021-11-09T20:51:53.933774Z","shell.execute_reply.started":"2021-11-09T20:51:53.928755Z","shell.execute_reply":"2021-11-09T20:51:53.933106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-11-09T20:51:53.935191Z","iopub.execute_input":"2021-11-09T20:51:53.935608Z","iopub.status.idle":"2021-11-09T20:51:53.945624Z","shell.execute_reply.started":"2021-11-09T20:51:53.935557Z","shell.execute_reply":"2021-11-09T20:51:53.944928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(np_features[242])","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:51:53.947236Z","iopub.execute_input":"2021-11-09T20:51:53.947509Z","iopub.status.idle":"2021-11-09T20:51:54.209559Z","shell.execute_reply.started":"2021-11-09T20:51:53.947473Z","shell.execute_reply":"2021-11-09T20:51:54.208875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(18,18))\nfig.suptitle(\"Some examples of images of the dataset\", fontsize=16)\nfor i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(np_features[i], cmap=plt.cm.binary)\n    plt.xlabel(targets[np_lables[i]])\nplt.show()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-11-09T20:51:54.210520Z","iopub.execute_input":"2021-11-09T20:51:54.210767Z","iopub.status.idle":"2021-11-09T20:51:55.806277Z","shell.execute_reply.started":"2021-11-09T20:51:54.210735Z","shell.execute_reply":"2021-11-09T20:51:55.805618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Convert the images to grayscale colorspace before calculating the HOG features for each image","metadata":{}},{"cell_type":"code","source":"# from skimage import color\n# data_gray = [ color.rgb2gray(i) for i in np_features]\n","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:51:55.807539Z","iopub.execute_input":"2021-11-09T20:51:55.807936Z","iopub.status.idle":"2021-11-09T20:51:55.811612Z","shell.execute_reply.started":"2021-11-09T20:51:55.807901Z","shell.execute_reply":"2021-11-09T20:51:55.810627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(data_gray[0])","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:51:55.813143Z","iopub.execute_input":"2021-11-09T20:51:55.813546Z","iopub.status.idle":"2021-11-09T20:51:55.822091Z","shell.execute_reply.started":"2021-11-09T20:51:55.813496Z","shell.execute_reply":"2021-11-09T20:51:55.821241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## generate Hog features for train data","metadata":{}},{"cell_type":"code","source":"from skimage.feature import hog\n\nhog_images = []\nhog_features = []\nfor image in np_features:\n    fd,hog_image = hog(image, orientations=8, pixels_per_cell=(16,16),cells_per_block=(1,1),\n                       block_norm= 'L2',visualize=True, multichannel=True)\n    hog_images.append(hog_image)\n    hog_features.append(fd)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:51:55.823987Z","iopub.execute_input":"2021-11-09T20:51:55.824490Z","iopub.status.idle":"2021-11-09T20:57:00.364474Z","shell.execute_reply.started":"2021-11-09T20:51:55.824450Z","shell.execute_reply":"2021-11-09T20:57:00.363703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np_hog_features = np.array(hog_features)\nnp_hog_features.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:57:00.365917Z","iopub.execute_input":"2021-11-09T20:57:00.366161Z","iopub.status.idle":"2021-11-09T20:57:00.406943Z","shell.execute_reply.started":"2021-11-09T20:57:00.366128Z","shell.execute_reply":"2021-11-09T20:57:00.406099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np_hog_images = np.array(hog_images)\nnp_hog_images.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:57:00.410972Z","iopub.execute_input":"2021-11-09T20:57:00.411172Z","iopub.status.idle":"2021-11-09T20:57:01.121129Z","shell.execute_reply.started":"2021-11-09T20:57:00.411148Z","shell.execute_reply":"2021-11-09T20:57:01.120417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(np_hog_images[242])\nnp_hog_images.shape","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-11-09T20:57:01.122654Z","iopub.execute_input":"2021-11-09T20:57:01.123166Z","iopub.status.idle":"2021-11-09T20:57:01.352999Z","shell.execute_reply.started":"2021-11-09T20:57:01.123124Z","shell.execute_reply":"2021-11-09T20:57:01.352376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig_hog = plt.figure(figsize=(18,18))\nfig_hog.suptitle(\"Some examples of hog images of the dataset\", fontsize=16)\nfor i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(np_hog_images[i])\n    plt.xlabel(targets[np_lables[i]])\nplt.show()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-11-09T20:57:01.354338Z","iopub.execute_input":"2021-11-09T20:57:01.354790Z","iopub.status.idle":"2021-11-09T20:57:02.654637Z","shell.execute_reply.started":"2021-11-09T20:57:01.354754Z","shell.execute_reply":"2021-11-09T20:57:02.653204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### PCA","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca_90 = PCA(n_components=0.90,svd_solver='full')\nnp_hog_features_pca = pca_90.fit_transform(np_hog_features)\nnp_hog_features_pca.shape","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-11-09T20:57:02.655807Z","iopub.execute_input":"2021-11-09T20:57:02.656149Z","iopub.status.idle":"2021-11-09T20:57:04.549816Z","shell.execute_reply.started":"2021-11-09T20:57:02.656118Z","shell.execute_reply":"2021-11-09T20:57:04.546487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca_90.explained_variance_ratio_))\nplt.xlabel('number of components')\nplt.ylabel('cumulative explained variance');\nplt.hlines([0.8], 0, 300, linestyles='dashed', colors='red')\nplt.show()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-11-09T20:57:04.551816Z","iopub.execute_input":"2021-11-09T20:57:04.552336Z","iopub.status.idle":"2021-11-09T20:57:04.859831Z","shell.execute_reply.started":"2021-11-09T20:57:04.552268Z","shell.execute_reply":"2021-11-09T20:57:04.858455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## grid search","metadata":{}},{"cell_type":"code","source":"np_hog_features_pca.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:57:04.861113Z","iopub.execute_input":"2021-11-09T20:57:04.861469Z","iopub.status.idle":"2021-11-09T20:57:04.867036Z","shell.execute_reply.started":"2021-11-09T20:57:04.861434Z","shell.execute_reply":"2021-11-09T20:57:04.866239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import svm\nfrom sklearn.model_selection import GridSearchCV\nclf = GridSearchCV(svm.SVC(), {\n    'C': [2,3,4,5],\n    'kernel': ['rbf']\n}, cv=4, return_train_score=False)\nclf.fit(np_hog_features_pca, np_lables)\nclf.cv_results_","metadata":{"execution":{"iopub.status.busy":"2021-11-09T20:57:04.868456Z","iopub.execute_input":"2021-11-09T20:57:04.868864Z","iopub.status.idle":"2021-11-09T21:09:53.032339Z","shell.execute_reply.started":"2021-11-09T20:57:04.868828Z","shell.execute_reply":"2021-11-09T21:09:53.031448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf = pd.DataFrame(clf.cv_results_)\ndf[['param_C','param_kernel','mean_test_score']]","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:09:53.033625Z","iopub.execute_input":"2021-11-09T21:09:53.034064Z","iopub.status.idle":"2021-11-09T21:09:53.058883Z","shell.execute_reply.started":"2021-11-09T21:09:53.034019Z","shell.execute_reply":"2021-11-09T21:09:53.058076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model training","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(np_hog_features_pca, np_lables, test_size= 0.25106)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:09:53.060024Z","iopub.execute_input":"2021-11-09T21:09:53.061734Z","iopub.status.idle":"2021-11-09T21:09:53.097613Z","shell.execute_reply.started":"2021-11-09T21:09:53.061687Z","shell.execute_reply":"2021-11-09T21:09:53.096811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-11-09T21:09:53.099198Z","iopub.execute_input":"2021-11-09T21:09:53.099464Z","iopub.status.idle":"2021-11-09T21:09:53.105379Z","shell.execute_reply.started":"2021-11-09T21:09:53.099428Z","shell.execute_reply":"2021-11-09T21:09:53.104429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import SVC\nsvc_model = SVC(C=3)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:09:53.107232Z","iopub.execute_input":"2021-11-09T21:09:53.107904Z","iopub.status.idle":"2021-11-09T21:09:53.114603Z","shell.execute_reply.started":"2021-11-09T21:09:53.107862Z","shell.execute_reply":"2021-11-09T21:09:53.113700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:09:53.116450Z","iopub.execute_input":"2021-11-09T21:09:53.117037Z","iopub.status.idle":"2021-11-09T21:09:53.126308Z","shell.execute_reply.started":"2021-11-09T21:09:53.116996Z","shell.execute_reply":"2021-11-09T21:09:53.125623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svc_model.fit(X_train, y_train)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-11-09T21:09:53.127406Z","iopub.execute_input":"2021-11-09T21:09:53.127806Z","iopub.status.idle":"2021-11-09T21:10:24.438216Z","shell.execute_reply.started":"2021-11-09T21:09:53.127767Z","shell.execute_reply":"2021-11-09T21:10:24.437496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ny_pred = svc_model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:10:24.439506Z","iopub.execute_input":"2021-11-09T21:10:24.439831Z","iopub.status.idle":"2021-11-09T21:10:36.640374Z","shell.execute_reply.started":"2021-11-09T21:10:24.439791Z","shell.execute_reply":"2021-11-09T21:10:36.639640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sn\n# %matplotlib inline\nCM = confusion_matrix(y_test, y_pred)\nax = plt.axes()\nsn.set(font_scale=1.4)\nsn.heatmap(CM, annot=True,annot_kws={\"size\": 16},  xticklabels=targets, yticklabels=targets, ax = ax, fmt=\"d\",cmap='YlGnBu')\nax.set_title('Confusion matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:10:36.641584Z","iopub.execute_input":"2021-11-09T21:10:36.644623Z","iopub.status.idle":"2021-11-09T21:10:37.117070Z","shell.execute_reply.started":"2021-11-09T21:10:36.644592Z","shell.execute_reply":"2021-11-09T21:10:37.116418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svc_model.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:10:37.118274Z","iopub.execute_input":"2021-11-09T21:10:37.118678Z","iopub.status.idle":"2021-11-09T21:10:48.874258Z","shell.execute_reply.started":"2021-11-09T21:10:37.118638Z","shell.execute_reply":"2021-11-09T21:10:48.873472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## load train data","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport random\n#load test set\ntest_data = []\ntest_dir = '/kaggle/input/cmpe188image-classification/cmpe188_test (2)/cmpe188_test'\n\nfor img in os.listdir(test_dir):\n    path = os.path.join(test_dir, img)\n    test_img = plt.imread(path)\n    \n    try:\n        test_img = cv2.resize(test_img,(144,144))\n        image = np.array(test_img)\n        test_data.append([image,img])\n    except Exception as e:\n        pass\n\nlen(test_data)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:10:48.875542Z","iopub.execute_input":"2021-11-09T21:10:48.875904Z","iopub.status.idle":"2021-11-09T21:11:03.967227Z","shell.execute_reply.started":"2021-11-09T21:10:48.875862Z","shell.execute_reply":"2021-11-09T21:11:03.966502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features = []\nimage_ids = []\n\nfor test_feature, image_id in test_data:\n    test_features.append(test_feature)\n    image_ids.append(image_id)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:11:03.968544Z","iopub.execute_input":"2021-11-09T21:11:03.968802Z","iopub.status.idle":"2021-11-09T21:11:03.977274Z","shell.execute_reply.started":"2021-11-09T21:11:03.968767Z","shell.execute_reply":"2021-11-09T21:11:03.976448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np_test_features = np.array(test_features)\nnp_test_features = np_test_features.astype(np.int32) #very important!!!\nnp_image_ids = np.array(image_ids)\nnp_test_features.shape","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-11-09T21:11:03.978667Z","iopub.execute_input":"2021-11-09T21:11:03.978957Z","iopub.status.idle":"2021-11-09T21:11:04.845100Z","shell.execute_reply.started":"2021-11-09T21:11:03.978910Z","shell.execute_reply":"2021-11-09T21:11:04.844398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:11:04.846501Z","iopub.execute_input":"2021-11-09T21:11:04.846981Z","iopub.status.idle":"2021-11-09T21:11:04.852148Z","shell.execute_reply.started":"2021-11-09T21:11:04.846943Z","shell.execute_reply":"2021-11-09T21:11:04.851432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(np_test_features[123])","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:11:04.853557Z","iopub.execute_input":"2021-11-09T21:11:04.853876Z","iopub.status.idle":"2021-11-09T21:11:05.265369Z","shell.execute_reply.started":"2021-11-09T21:11:04.853839Z","shell.execute_reply":"2021-11-09T21:11:05.264721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np_image_ids[123]","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:11:05.266577Z","iopub.execute_input":"2021-11-09T21:11:05.266967Z","iopub.status.idle":"2021-11-09T21:11:05.275874Z","shell.execute_reply.started":"2021-11-09T21:11:05.266921Z","shell.execute_reply":"2021-11-09T21:11:05.274845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from skimage import color\n# test_data_gray = [ color.rgb2gray(i) for i in np_test_features]","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:11:05.276951Z","iopub.execute_input":"2021-11-09T21:11:05.278708Z","iopub.status.idle":"2021-11-09T21:11:05.283422Z","shell.execute_reply.started":"2021-11-09T21:11:05.278669Z","shell.execute_reply":"2021-11-09T21:11:05.281200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_data_gray[123]","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:11:05.285347Z","iopub.execute_input":"2021-11-09T21:11:05.286045Z","iopub.status.idle":"2021-11-09T21:11:05.291211Z","shell.execute_reply.started":"2021-11-09T21:11:05.286006Z","shell.execute_reply":"2021-11-09T21:11:05.290554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## generate Hog features for test data","metadata":{}},{"cell_type":"code","source":"from skimage.feature import hog\n\nhog_test_images = []\nhog_test_features = []\nfor test_image in np_test_features:\n    fd,hog_image_test = hog(test_image, orientations=8, pixels_per_cell=(16,16),\n                            cells_per_block=(1, 1),block_norm= 'L2',visualize=True,multichannel=True)\n    hog_test_images.append(hog_image_test)\n    hog_test_features.append(fd)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:11:05.294269Z","iopub.execute_input":"2021-11-09T21:11:05.294471Z","iopub.status.idle":"2021-11-09T21:12:22.037564Z","shell.execute_reply.started":"2021-11-09T21:11:05.294435Z","shell.execute_reply":"2021-11-09T21:12:22.036778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(hog_test_features)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:12:22.038927Z","iopub.execute_input":"2021-11-09T21:12:22.039223Z","iopub.status.idle":"2021-11-09T21:12:22.048917Z","shell.execute_reply.started":"2021-11-09T21:12:22.039186Z","shell.execute_reply":"2021-11-09T21:12:22.048148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(hog_test_images[123])","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:12:22.050628Z","iopub.execute_input":"2021-11-09T21:12:22.051209Z","iopub.status.idle":"2021-11-09T21:12:22.299258Z","shell.execute_reply.started":"2021-11-09T21:12:22.051172Z","shell.execute_reply":"2021-11-09T21:12:22.298425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np_hog_test_images = np.array(hog_test_images)\nnp_hog_test_images.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:12:22.303615Z","iopub.execute_input":"2021-11-09T21:12:22.303895Z","iopub.status.idle":"2021-11-09T21:12:22.824067Z","shell.execute_reply.started":"2021-11-09T21:12:22.303866Z","shell.execute_reply":"2021-11-09T21:12:22.823393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np_hog_test_features = np.array(hog_test_features)\nnp_hog_test_features.shape","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-11-09T21:12:22.825289Z","iopub.execute_input":"2021-11-09T21:12:22.825805Z","iopub.status.idle":"2021-11-09T21:12:22.849164Z","shell.execute_reply.started":"2021-11-09T21:12:22.825766Z","shell.execute_reply":"2021-11-09T21:12:22.848430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Apply PCA to test features","metadata":{}},{"cell_type":"code","source":"np_hog_test_features_pca = pca_90.transform(np_hog_test_features)\nnp_hog_test_features_pca.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:12:22.850446Z","iopub.execute_input":"2021-11-09T21:12:22.850745Z","iopub.status.idle":"2021-11-09T21:12:22.919535Z","shell.execute_reply.started":"2021-11-09T21:12:22.850710Z","shell.execute_reply":"2021-11-09T21:12:22.918702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svc_model.get_params","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:12:22.924098Z","iopub.execute_input":"2021-11-09T21:12:22.924750Z","iopub.status.idle":"2021-11-09T21:12:22.931863Z","shell.execute_reply.started":"2021-11-09T21:12:22.924708Z","shell.execute_reply":"2021-11-09T21:12:22.930934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nkaggle_predict = svc_model.predict(np_hog_test_features_pca)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:12:22.933801Z","iopub.execute_input":"2021-11-09T21:12:22.934101Z","iopub.status.idle":"2021-11-09T21:12:34.624986Z","shell.execute_reply.started":"2021-11-09T21:12:22.934064Z","shell.execute_reply":"2021-11-09T21:12:34.624235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kaggle_predict","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-11-09T21:12:34.626504Z","iopub.execute_input":"2021-11-09T21:12:34.626784Z","iopub.status.idle":"2021-11-09T21:12:34.633526Z","shell.execute_reply.started":"2021-11-09T21:12:34.626748Z","shell.execute_reply":"2021-11-09T21:12:34.632811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf = pd.DataFrame()\ndf['Id'] = np_image_ids\ndf['Category'] = kaggle_predict","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:12:34.634789Z","iopub.execute_input":"2021-11-09T21:12:34.635607Z","iopub.status.idle":"2021-11-09T21:12:34.650488Z","shell.execute_reply.started":"2021-11-09T21:12:34.635552Z","shell.execute_reply":"2021-11-09T21:12:34.649698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('/kaggle/working/submit.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-09T21:12:34.651806Z","iopub.execute_input":"2021-11-09T21:12:34.653221Z","iopub.status.idle":"2021-11-09T21:12:34.673382Z","shell.execute_reply.started":"2021-11-09T21:12:34.653191Z","shell.execute_reply":"2021-11-09T21:12:34.672676Z"},"trusted":true},"execution_count":null,"outputs":[]}]}