{"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":"# <span style='color:#D4af37'>CSCI417/ECEN425:Machine Intelligence-2023SPRG </span>\n","metadata":{}},{"cell_type":"code","source":"import os\nimport json\nimport cv2 as cv \nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom glob import glob\nfrom tqdm.notebook import tqdm\nfrom sklearn.utils import shuffle\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import KFold\nfrom sklearn.ensemble import RandomForestClassifier\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-22T23:21:05.685606Z","iopub.execute_input":"2023-05-22T23:21:05.686138Z","iopub.status.idle":"2023-05-22T23:21:05.694274Z","shell.execute_reply.started":"2023-05-22T23:21:05.686104Z","shell.execute_reply":"2023-05-22T23:21:05.692825Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data exploaration","metadata":{}},{"cell_type":"code","source":"INPUT_DIR = '/kaggle/input/benetech-making-graphs-accessible'\ntrain_image_paths = sorted(glob(os.path.join(INPUT_DIR, 'train/images/*.jpg')))\ntrain_annotation_paths = sorted(glob(os.path.join(INPUT_DIR, 'train/annotations/*.json')))\nprint('train images:', len(train_image_paths))\nprint('train annotations:', len(train_annotation_paths))\n","metadata":{"execution":{"iopub.status.busy":"2023-05-22T22:51:47.576305Z","iopub.execute_input":"2023-05-22T22:51:47.576708Z","iopub.status.idle":"2023-05-22T22:51:48.147466Z","shell.execute_reply.started":"2023-05-22T22:51:47.576677Z","shell.execute_reply":"2023-05-22T22:51:48.146652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img=cv.imread('/kaggle/input/benetech-making-graphs-accessible/train/images/0000ae6cbdb1.jpg')\nimg.shape\nplt.imshow(img)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-22T22:09:02.584721Z","iopub.execute_input":"2023-05-22T22:09:02.585168Z","iopub.status.idle":"2023-05-22T22:09:02.857764Z","shell.execute_reply.started":"2023-05-22T22:09:02.585132Z","shell.execute_reply":"2023-05-22T22:09:02.856815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get annotations for the image upove \nfile_name=('/kaggle/input/benetech-making-graphs-accessible/train/images/0000ae6cbdb1.jpg')\na = file_name.split(\"/\")[-1]\nsplit_file_path = file_name.split(\"/\")\nsplit_file_name = a.split(\".\")\njson_file_path = INPUT_DIR+\"/train/annotations/\"+split_file_name[0]+\".json\"\nannot = open(json_file_path)\nannot = json.load(annot)\nannot","metadata":{"execution":{"iopub.status.busy":"2023-05-22T22:11:44.133172Z","iopub.execute_input":"2023-05-22T22:11:44.133565Z","iopub.status.idle":"2023-05-22T22:11:44.16426Z","shell.execute_reply.started":"2023-05-22T22:11:44.133536Z","shell.execute_reply":"2023-05-22T22:11:44.16302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#creating a dataframe with images and annotations for classification\n\ntrain_image_paths = sorted(glob('/kaggle/input/benetech-making-graphs-accessible/train/images/*.jpg'))\ntrain_annotation_paths = sorted(glob('/kaggle/input/benetech-making-graphs-accessible/train/annotations/*.json'))\n\ntrain_df = pd.DataFrame({'image_path':train_image_paths})\n\ntrain_df['train_annotations'] = train_df['image_path'].str.replace('images', 'annotations')\ntrain_df['train_annotations'] = train_df['train_annotations'].str.replace('jpg', 'json')\n\nchart_type, source, height, width = [], [], [], []\nfor annotations in tqdm(train_df['train_annotations']):\n    annot = open(annotations)\n    annot = json.load(annot)\n    chart_type.append(annot['chart-type'])\n    source.append(annot['source'])\n    height.append(annot['plot-bb']['height'])\n    width.append(annot['plot-bb']['width'])\n\ntrain_df['chart_type'] = chart_type\ntrain_df['source'] = source\ntrain_df['height'] = height\ntrain_df['width'] = width","metadata":{"execution":{"iopub.status.busy":"2023-05-22T22:34:04.61429Z","iopub.execute_input":"2023-05-22T22:34:04.614843Z","iopub.status.idle":"2023-05-22T22:34:59.274993Z","shell.execute_reply.started":"2023-05-22T22:34:04.61478Z","shell.execute_reply":"2023-05-22T22:34:59.273825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-05-22T22:35:17.715188Z","iopub.execute_input":"2023-05-22T22:35:17.715591Z","iopub.status.idle":"2023-05-22T22:35:17.734147Z","shell.execute_reply.started":"2023-05-22T22:35:17.715561Z","shell.execute_reply":"2023-05-22T22:35:17.73292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.chart_type.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-22T22:35:21.675034Z","iopub.execute_input":"2023-05-22T22:35:21.676216Z","iopub.status.idle":"2023-05-22T22:35:21.699112Z","shell.execute_reply.started":"2023-05-22T22:35:21.676175Z","shell.execute_reply":"2023-05-22T22:35:21.697582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder()\ntrain_df['lable'] = le.fit_transform(train_df['chart_type'])\n","metadata":{"execution":{"iopub.status.busy":"2023-05-22T22:35:24.518964Z","iopub.execute_input":"2023-05-22T22:35:24.519349Z","iopub.status.idle":"2023-05-22T22:35:24.554376Z","shell.execute_reply.started":"2023-05-22T22:35:24.519321Z","shell.execute_reply":"2023-05-22T22:35:24.552831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-05-22T22:35:27.930981Z","iopub.execute_input":"2023-05-22T22:35:27.931368Z","iopub.status.idle":"2023-05-22T22:35:27.954597Z","shell.execute_reply.started":"2023-05-22T22:35:27.931341Z","shell.execute_reply":"2023-05-22T22:35:27.953252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_features(images):\n    features = []\n    for img in images:\n        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n        # Extract edge features using Canny edge detector\n        edges = cv2.Canny(gray, 50, 150)\n        # Compute histogram of oriented gradients (HOG) features\n        hog = cv2.HOGDescriptor((64, 64), (16, 16), (8, 8), (8, 8), 9)\n        hog_feats = hog.compute(edges)\n        features.append(hog_feats.ravel())\n    return np.array(features)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T22:44:45.914909Z","iopub.execute_input":"2023-05-22T22:44:45.915317Z","iopub.status.idle":"2023-05-22T22:44:45.92353Z","shell.execute_reply.started":"2023-05-22T22:44:45.915284Z","shell.execute_reply":"2023-05-22T22:44:45.922129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = []\nfor train_image_paths in tqdm(train_df['image_path']):\n    img = cv2.imread(train_image_paths)\n    resized_img = cv2.resize(img, (64, 64))\n    images.append(resized_img)\nfeatures = extract_features(images)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T22:55:53.129597Z","iopub.execute_input":"2023-05-22T22:55:53.130961Z","iopub.status.idle":"2023-05-22T23:04:00.368099Z","shell.execute_reply.started":"2023-05-22T22:55:53.130918Z","shell.execute_reply":"2023-05-22T23:04:00.366788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(features))\nfeatures","metadata":{"execution":{"iopub.status.busy":"2023-05-22T23:05:40.941989Z","iopub.execute_input":"2023-05-22T23:05:40.942402Z","iopub.status.idle":"2023-05-22T23:05:40.951908Z","shell.execute_reply.started":"2023-05-22T23:05:40.942364Z","shell.execute_reply":"2023-05-22T23:05:40.95058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.concat([pd.DataFrame(features), train_df], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T23:09:10.036563Z","iopub.execute_input":"2023-05-22T23:09:10.037013Z","iopub.status.idle":"2023-05-22T23:09:10.58427Z","shell.execute_reply.started":"2023-05-22T23:09:10.03698Z","shell.execute_reply":"2023-05-22T23:09:10.582947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-05-22T23:09:14.070715Z","iopub.execute_input":"2023-05-22T23:09:14.071121Z","iopub.status.idle":"2023-05-22T23:09:14.153408Z","shell.execute_reply.started":"2023-05-22T23:09:14.07109Z","shell.execute_reply":"2023-05-22T23:09:14.152426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split data into input features and target variable\nX = train_df.drop(['image_path','train_annotations','chart_type','source'], axis=1)\ny = train_df['chart_type']\n# Convert column names to string data type\nX.columns = X.columns.astype(str)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T23:41:42.727245Z","iopub.execute_input":"2023-05-22T23:41:42.727665Z","iopub.status.idle":"2023-05-22T23:41:42.968984Z","shell.execute_reply.started":"2023-05-22T23:41:42.727625Z","shell.execute_reply":"2023-05-22T23:41:42.967707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize k-fold cross-validation\nkf = KFold(n_splits=10, shuffle=True, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T23:41:49.171784Z","iopub.execute_input":"2023-05-22T23:41:49.172175Z","iopub.status.idle":"2023-05-22T23:41:49.178089Z","shell.execute_reply.started":"2023-05-22T23:41:49.172146Z","shell.execute_reply":"2023-05-22T23:41:49.176701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train and evaluate model on each fold\nscores = []\nfor train_index, test_index in tqdm(kf.split(X)):\n    # Split data into training and validation sets for this fold\n    X_train, X_test = X.iloc[train_index], X.iloc[test_index]\n    y_train, y_test = y.iloc[train_index], y.iloc[test_index]\n\n    # Train a Random Forest classifier on the training set\n    clf = RandomForestClassifier(n_estimators=100)\n    clf.fit(X_train, y_train)\n\n    # Evaluate performance on the validation set for this fold\n    score = clf.score(X_test, y_test)\n    scores.append(score)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T23:42:28.503534Z","iopub.execute_input":"2023-05-22T23:42:28.503921Z","iopub.status.idle":"2023-05-23T00:26:31.535543Z","shell.execute_reply.started":"2023-05-22T23:42:28.503892Z","shell.execute_reply":"2023-05-23T00:26:31.534206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"avg_score = np.mean(scores)\nprint(\"Average accuracy:\", avg_score)","metadata":{"execution":{"iopub.status.busy":"2023-05-23T00:26:54.587284Z","iopub.execute_input":"2023-05-23T00:26:54.58769Z","iopub.status.idle":"2023-05-23T00:26:54.594192Z","shell.execute_reply.started":"2023-05-23T00:26:54.587658Z","shell.execute_reply":"2023-05-23T00:26:54.592974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = clf.predict(img)\n#Print the predicted class labels for each sample in X_new\nprint(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-05-23T01:05:46.721726Z","iopub.execute_input":"2023-05-23T01:05:46.722222Z","iopub.status.idle":"2023-05-23T01:05:47.182105Z","shell.execute_reply.started":"2023-05-23T01:05:46.722178Z","shell.execute_reply":"2023-05-23T01:05:47.180962Z"},"trusted":true},"execution_count":null,"outputs":[]}]}