{"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":"#Define libraries\nimport numpy as np\nimport pandas as pd\nimport torchvision\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision.transforms as transforms\nfrom torchvision.models import resnet18\nfrom torch.utils.data import DataLoader\nfrom torchvision.datasets import ImageFolder\nfrom glob import glob\nimport json\nimport os\nimport sys\nfrom tqdm.notebook import tqdm\nimport argparse\nimport torch.utils.data\nimport torch.nn.functional as F\nfrom torch.optim import lr_scheduler\nfrom sklearn.preprocessing import OneHotEncoder, LabelEncoder\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.metrics import roc_auc_score\nimport math, random\nimport pathlib\nimport cv2 as cv\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport random\nfrom sklearn.model_selection import train_test_split\nimport copy\nimport time \nfrom collections import defaultdict\n\n\n\nfrom pathlib import Path\nfrom PIL import Image\nimport matplotlib.patches as patches\n\nfrom skimage.transform import resize\nfrom skimage.io import imread\nfrom sklearn import svm\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.multiclass import OneVsRestClassifier\nfrom sklearn.svm import SVC\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-25T07:35:46.098778Z","iopub.execute_input":"2023-05-25T07:35:46.099211Z","iopub.status.idle":"2023-05-25T07:35:46.112224Z","shell.execute_reply.started":"2023-05-25T07:35:46.099177Z","shell.execute_reply":"2023-05-25T07:35:46.110462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n\nseed = 1999\nseed_everything(seed)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T07:35:46.284704Z","iopub.execute_input":"2023-05-25T07:35:46.285168Z","iopub.status.idle":"2023-05-25T07:35:46.295602Z","shell.execute_reply.started":"2023-05-25T07:35:46.285134Z","shell.execute_reply":"2023-05-25T07:35:46.29393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    isOneHot = False\n    label_map = {'dot': 0, 'horizontal_bar' : 1, 'vertical_bar': 2, 'line': 3, 'scatter': 4}\n    num_classes = 5\n    batchSize = 32\n    fold_train = 0","metadata":{"execution":{"iopub.status.busy":"2023-05-25T07:35:46.50098Z","iopub.execute_input":"2023-05-25T07:35:46.501439Z","iopub.status.idle":"2023-05-25T07:35:46.507692Z","shell.execute_reply.started":"2023-05-25T07:35:46.501376Z","shell.execute_reply":"2023-05-25T07:35:46.506576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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))\nargs = argparse.Namespace(\n    sampling_size = 10000,\n    thresholds = [9, 128]\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T07:35:46.713979Z","iopub.execute_input":"2023-05-25T07:35:46.71476Z","iopub.status.idle":"2023-05-25T07:35:48.668017Z","shell.execute_reply.started":"2023-05-25T07:35:46.714717Z","shell.execute_reply":"2023-05-25T07:35:48.666214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#load a random image from the training directory\nplt.figure(figsize=(14, 6))\nfile_name = random.choice(train_image_paths)\nimage = plt.imread(file_name)\nplt.imshow(image)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-25T07:35:48.670999Z","iopub.execute_input":"2023-05-25T07:35:48.671533Z","iopub.status.idle":"2023-05-25T07:35:49.055718Z","shell.execute_reply.started":"2023-05-25T07:35:48.671481Z","shell.execute_reply":"2023-05-25T07:35:49.054579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#load the annotations for the above graph\na = file_name.split(\"/\")[-1]\n\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\n","metadata":{"execution":{"iopub.status.busy":"2023-05-25T07:35:49.057123Z","iopub.execute_input":"2023-05-25T07:35:49.058147Z","iopub.status.idle":"2023-05-25T07:35:49.084577Z","shell.execute_reply.started":"2023-05-25T07:35:49.058115Z","shell.execute_reply":"2023-05-25T07:35:49.083434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loading a random image from the test directory\nplt.figure(figsize=(14, 6))\nfile_name = random.choice(os.listdir(INPUT_DIR+\"/test/images/\"))\nimage = plt.imread(INPUT_DIR+\"/test/images/\"+file_name)\nplt.imshow(image)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-25T07:35:49.086898Z","iopub.execute_input":"2023-05-25T07:35:49.08821Z","iopub.status.idle":"2023-05-25T07:35:49.408757Z","shell.execute_reply.started":"2023-05-25T07:35:49.08815Z","shell.execute_reply":"2023-05-25T07:35:49.407606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#creating a dataframe with images and annotations for classification\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-25T07:35:49.410932Z","iopub.execute_input":"2023-05-25T07:35:49.414836Z","iopub.status.idle":"2023-05-25T07:43:06.074659Z","shell.execute_reply.started":"2023-05-25T07:35:49.414781Z","shell.execute_reply":"2023-05-25T07:43:06.073244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T06:01:35.412479Z","iopub.execute_input":"2023-05-25T06:01:35.413254Z","iopub.status.idle":"2023-05-25T06:01:35.435612Z","shell.execute_reply.started":"2023-05-25T06:01:35.413226Z","shell.execute_reply":"2023-05-25T06:01:35.434761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.chart_type.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-13T12:03:40.65399Z","iopub.execute_input":"2023-05-13T12:03:40.654348Z","iopub.status.idle":"2023-05-13T12:03:40.673791Z","shell.execute_reply.started":"2023-05-13T12:03:40.654321Z","shell.execute_reply":"2023-05-13T12:03:40.672739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data transformation\ntransforms_train = transforms.Compose(\n    [transforms.ToTensor(),\n     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])\ntransforms_test = transforms.Compose(\n    [transforms.ToTensor(),\n     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])\n","metadata":{"execution":{"iopub.status.busy":"2023-05-13T12:07:36.323336Z","iopub.execute_input":"2023-05-13T12:07:36.323718Z","iopub.status.idle":"2023-05-13T12:07:36.330875Z","shell.execute_reply.started":"2023-05-13T12:07:36.323686Z","shell.execute_reply":"2023-05-13T12:07:36.329001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ImageCharts(torch.utils.data.Dataset):\n    def __init__(self, df, transforms  = None): ### phase = train/test\n        self.df = df\n        self.transforms = transforms\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self,idx):\n        name_img = self.df.iloc[idx]['image_path']\n        img = cv.imread(name_img)\n        img = cv.cvtColor(img,cv.COLOR_BGR2RGB)\n        img = cv.resize(img,(500,300))\n        img = img.astype(np.float32)/255.0\n\n        label = np.array(self.df.iloc[idx]['label'])\n\n        if(self.transforms is not None):\n            img = self.transforms(img)\n        return img, label","metadata":{"execution":{"iopub.status.busy":"2023-05-13T12:41:11.633077Z","iopub.execute_input":"2023-05-13T12:41:11.633505Z","iopub.status.idle":"2023-05-13T12:41:11.64085Z","shell.execute_reply.started":"2023-05-13T12:41:11.633474Z","shell.execute_reply":"2023-05-13T12:41:11.63995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cv_split(Xtrain, ytrain, n_folds, seed):\n    kfold = StratifiedKFold(n_splits = n_folds, shuffle = True, random_state = seed)\n    for num, (train_index, val_index) in enumerate(kfold.split(Xtrain, ytrain)):\n        Xtrain.loc[val_index, 'fold'] = int(num)\n    Xtrain['fold'] = Xtrain['fold'].astype(int)\n    return Xtrain\n\nmeta_df = cv_split(train_df,train_df['chart_type'], 5, 42)\nmeta_df['label'] = meta_df['chart_type'].apply(lambda x: CFG.label_map[x])\nmeta_df.head(42)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T07:43:17.17915Z","iopub.execute_input":"2023-05-25T07:43:17.179653Z","iopub.status.idle":"2023-05-25T07:43:17.519667Z","shell.execute_reply.started":"2023-05-25T07:43:17.179618Z","shell.execute_reply":"2023-05-25T07:43:17.518274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_df['label']","metadata":{"execution":{"iopub.status.busy":"2023-05-24T09:15:27.106388Z","iopub.execute_input":"2023-05-24T09:15:27.106802Z","iopub.status.idle":"2023-05-24T09:15:27.116613Z","shell.execute_reply.started":"2023-05-24T09:15:27.106759Z","shell.execute_reply":"2023-05-24T09:15:27.115302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dataloaders\ntrain_ds = ImageCharts(meta_df[meta_df.fold != CFG.fold_train].reset_index(), transforms=transforms_train)\nval_ds = ImageCharts(meta_df[meta_df.fold == CFG.fold_train].reset_index(), transforms=transforms_train)\ntrain_dl = torch.utils.data.DataLoader(train_ds, batch_size= CFG.batchSize, shuffle=True)\nval_dl = torch.utils.data.DataLoader(val_ds, batch_size= CFG.batchSize, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T12:41:49.245864Z","iopub.execute_input":"2023-05-13T12:41:49.246707Z","iopub.status.idle":"2023-05-13T12:41:49.272392Z","shell.execute_reply.started":"2023-05-13T12:41:49.246674Z","shell.execute_reply":"2023-05-13T12:41:49.271475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a,b = next(iter(train_dl))\nprint(a.shape)\nprint(b.shape)\nprint('chart_type:',list(CFG.label_map)[b[0].detach().numpy()])\nplt.imshow(a[0].permute(1,2,0))","metadata":{"execution":{"iopub.status.busy":"2023-05-13T12:41:51.375741Z","iopub.execute_input":"2023-05-13T12:41:51.376121Z","iopub.status.idle":"2023-05-13T12:41:52.145587Z","shell.execute_reply.started":"2023-05-13T12:41:51.37609Z","shell.execute_reply":"2023-05-13T12:41:52.144663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torchvision.models.resnet50(pretrained=False)\n\n\nnum_features = model.fc.in_features\nmodel.fc = nn.Linear(num_features, CFG.num_classes)\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\nmyModel = model.to(device)\nnext(myModel.parameters()).device","metadata":{"execution":{"iopub.status.busy":"2023-05-13T12:42:10.943008Z","iopub.execute_input":"2023-05-13T12:42:10.943644Z","iopub.status.idle":"2023-05-13T12:42:11.379268Z","shell.execute_reply.started":"2023-05-13T12:42:10.943607Z","shell.execute_reply":"2023-05-13T12:42:11.37815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def training(model, train_dl, val_dl, num_epochs):\n    \n  # Loss Function, Optimizer and Scheduler\n    criterion = nn.CrossEntropyLoss()\n    optimizer = torch.optim.Adam(model.parameters(),lr=0.0001)\n    scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer, max_lr=0.001,\n                                                steps_per_epoch=int(len(train_dl)),\n                                                epochs=num_epochs,\n                                                anneal_strategy='linear')\n\n  # Repeat for each epoch\n    best_acc = -1\n    for epoch in range(num_epochs):\n        \n        running_loss = 0.0\n        correct_prediction = 0\n        total_prediction = 0\n        \n\n    # Repeat for each batch in the training set\n        for i, data in enumerate(train_dl):\n            \n            \n        # Get the input features and target labels, and put them on the GPU\n            inputs, labels = data[0].to(device), data[1].to(device)\n\n        # Normalize the inputs\n            inputs_m, inputs_s = inputs.mean(), inputs.std()\n            inputs = (inputs - inputs_m) / inputs_s\n\n        # Zero the parameter gradients\n            optimizer.zero_grad()\n\n        # forward + backward + optimize\n            outputs = model(inputs)\n            loss = criterion(outputs, labels)\n            loss.backward()\n            optimizer.step()\n            scheduler.step()\n\n        # Keep stats for Loss and Accuracy\n            running_loss += loss.item()\n\n        # Get the predicted class with the highest score\n            _, prediction = torch.max(outputs,1)\n        # Count of predictions that matched the target label\n            correct_prediction += (prediction == labels).sum().item()\n            total_prediction += prediction.shape[0]\n\n            if (i + 1) % 50 == 0:\n                # print every 10 mini-batches\n                print('Epoch [{}/{}], Step [{}/{}], Loss : {:.4f}'\n               .format(epoch + 1, num_epochs, i + 1, len(train_dl), running_loss/(i + 1)))\n    \n    # Print stats at the end of the epoch\n    num_batches = len(train_dl)\n    avg_loss = running_loss / num_batches\n    acc = correct_prediction/total_prediction\n    print(f'Epoch: {epoch + 1}, Loss: {avg_loss:.4f}, Accuracy: {acc:.4f}')\n\n    gt = []\n    pred = []\n\n    with torch.no_grad():\n        correct = 0\n        total = 0\n        val_loss = 0 \n        for idx, data_ in enumerate(val_dl):\n            inputs, labels = data_[0].to(device), data_[1].to(device)\n\n            # Normalize the inputs\n            inputs_m, inputs_s = inputs.mean(), inputs.std()\n            inputs = (inputs - inputs_m) / inputs_s\n\n            # forward + backward + optimize\n            outputs = model(inputs)\n            loss = criterion(outputs, labels)\n\n            # Keep stats for Loss and Accuracy\n            val_loss += loss.item()\n\n            # Get the predicted class with the highest score\n            _, prediction = torch.max(outputs,1)\n            predi = torch.softmax(outputs, dim = -1)\n            gt.append(labels)\n            pred.append(predi[:,1])\n            # Count of predictions that matched the target label\n            correct += (prediction == labels).sum().item()\n            total += prediction.shape[0]\n        print('Accuracy of the network val: {:.4f} %'.format(100 * correct / total))\n\n        final_score = 100 * correct / total\n\n\n        if (best_acc < final_score):\n            best_acc = final_score\n            print(\"Saving best model!\")\n            torch.save(model.state_dict(), f'Benetech _ResNet50_fold0.pth')\n    print('Finished Training')\nnum_epochs = 1   # Just for demo, adjust this higher.\ntraining(myModel, train_dl, val_dl, num_epochs)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T13:17:42.159442Z","iopub.execute_input":"2023-05-13T13:17:42.159816Z","iopub.status.idle":"2023-05-13T13:39:28.355372Z","shell.execute_reply.started":"2023-05-13T13:17:42.159786Z","shell.execute_reply":"2023-05-13T13:39:28.354398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## SVM Model","metadata":{}},{"cell_type":"code","source":"img = cv.imread('/kaggle/input/benetech-making-graphs-accessible/train/images/0000ae6cbdb1.jpg')\nprint(img.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T23:00:46.105549Z","iopub.execute_input":"2023-05-24T23:00:46.105919Z","iopub.status.idle":"2023-05-24T23:00:46.130748Z","shell.execute_reply.started":"2023-05-24T23:00:46.105891Z","shell.execute_reply":"2023-05-24T23:00:46.129705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counter = 0\nflat_data_arr=[] \ntarget_arr=[]\nfor img in meta_df['image_path']:\n        img_array=imread(img)\n        img_resized=resize(img_array,(150,150,3))\n        flat_data_arr.append(img_resized.flatten())\n        target_arr.append(meta_df[meta_df['image_path']==img].label)\n        if counter == 500:\n            break\n        counter+=1\nflat_data=np.array(flat_data_arr)\ntarget=np.array(target_arr)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-25T07:43:35.772065Z","iopub.execute_input":"2023-05-25T07:43:35.772487Z","iopub.status.idle":"2023-05-25T07:44:01.846915Z","shell.execute_reply.started":"2023-05-25T07:43:35.772455Z","shell.execute_reply":"2023-05-25T07:44:01.845712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# flat_data","metadata":{"execution":{"iopub.status.busy":"2023-05-25T06:07:52.752223Z","iopub.execute_input":"2023-05-25T06:07:52.753188Z","iopub.status.idle":"2023-05-25T06:07:52.757516Z","shell.execute_reply.started":"2023-05-25T06:07:52.75314Z","shell.execute_reply":"2023-05-25T06:07:52.756505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = flat_data\ny = target","metadata":{"execution":{"iopub.status.busy":"2023-05-25T08:00:47.561507Z","iopub.execute_input":"2023-05-25T08:00:47.562179Z","iopub.status.idle":"2023-05-25T08:00:47.569098Z","shell.execute_reply.started":"2023-05-25T08:00:47.562135Z","shell.execute_reply":"2023-05-25T08:00:47.567671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.size)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T08:00:50.702284Z","iopub.execute_input":"2023-05-25T08:00:50.702726Z","iopub.status.idle":"2023-05-25T08:00:50.708803Z","shell.execute_reply.started":"2023-05-25T08:00:50.702693Z","shell.execute_reply":"2023-05-25T08:00:50.707505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(X,y,test_size=0.20,\n                                               random_state=0)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T08:00:51.676106Z","iopub.execute_input":"2023-05-25T08:00:51.676559Z","iopub.status.idle":"2023-05-25T08:00:52.135042Z","shell.execute_reply.started":"2023-05-25T08:00:51.676527Z","shell.execute_reply":"2023-05-25T08:00:52.132678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_test.size)\nprint(len(x_train[0]))","metadata":{"execution":{"iopub.status.busy":"2023-05-25T08:00:52.137354Z","iopub.execute_input":"2023-05-25T08:00:52.13787Z","iopub.status.idle":"2023-05-25T08:00:52.145765Z","shell.execute_reply.started":"2023-05-25T08:00:52.137767Z","shell.execute_reply":"2023-05-25T08:00:52.144015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nclf = OneVsRestClassifier(SVC()).fit(X, y)\nclf.predict(x_test)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-25T08:00:52.294716Z","iopub.execute_input":"2023-05-25T08:00:52.295204Z","iopub.status.idle":"2023-05-25T08:01:31.535958Z","shell.execute_reply.started":"2023-05-25T08:00:52.295171Z","shell.execute_reply":"2023-05-25T08:01:31.534157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# clf = OneVsRestClassifier(SVC( C=1.0, kernel='rbf', degree=3, gamma='scale', coef0=0.0, shrinking=True, probability=False, \n#                               tol=0.001,\n#                               decision_function_shape='ovr', break_ties=False, random_state=None)).fit(X, y)\n# clf.predict(x_test)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T23:44:37.227392Z","iopub.execute_input":"2023-05-24T23:44:37.228043Z","iopub.status.idle":"2023-05-24T23:44:37.232592Z","shell.execute_reply.started":"2023-05-24T23:44:37.228012Z","shell.execute_reply":"2023-05-24T23:44:37.231662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = clf.predict(x_train)\n  \naccuracy = accuracy_score(y_pred, y_train)\n  \n\nprint(f\"The model is {accuracy*100}% accurate\")","metadata":{"execution":{"iopub.status.busy":"2023-05-25T06:23:01.157262Z","iopub.execute_input":"2023-05-25T06:23:01.159149Z","iopub.status.idle":"2023-05-25T06:23:35.776727Z","shell.execute_reply.started":"2023-05-25T06:23:01.159118Z","shell.execute_reply":"2023-05-25T06:23:35.775982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_pred = clf.predict(x_test)\n  \n\naccuracy = accuracy_score(y_test_pred, y_test)\n  \n\nprint(f\"The model is {accuracy*100}% accurate\")","metadata":{"execution":{"iopub.status.busy":"2023-05-25T06:23:47.077337Z","iopub.execute_input":"2023-05-25T06:23:47.077675Z","iopub.status.idle":"2023-05-25T06:23:55.274247Z","shell.execute_reply.started":"2023-05-25T06:23:47.07765Z","shell.execute_reply":"2023-05-25T06:23:55.273478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rbf = OneVsRestClassifier(SVC(gamma='auto',coef0=1.0)).fit(X, y)\nrbf.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T06:53:50.07129Z","iopub.execute_input":"2023-05-25T06:53:50.071643Z","iopub.status.idle":"2023-05-25T06:54:07.153522Z","shell.execute_reply.started":"2023-05-25T06:53:50.071615Z","shell.execute_reply":"2023-05-25T06:54:07.152877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = rbf.predict(x_train)\n  \naccuracy = accuracy_score(y_pred, y_train)\n  \n\nprint(f\"The model is {accuracy*100}% accurate\")","metadata":{"execution":{"iopub.status.busy":"2023-05-25T06:54:07.154927Z","iopub.execute_input":"2023-05-25T06:54:07.155557Z","iopub.status.idle":"2023-05-25T06:54:46.389835Z","shell.execute_reply.started":"2023-05-25T06:54:07.155532Z","shell.execute_reply":"2023-05-25T06:54:46.389043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel_poly = OneVsRestClassifier(SVC(kernel='poly',coef0=0.0,degree= 2,tol=0.1)).fit(X, y)\nmodel_poly.predict(x_test)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-25T06:39:52.808717Z","iopub.execute_input":"2023-05-25T06:39:52.809044Z","iopub.status.idle":"2023-05-25T06:40:03.280467Z","shell.execute_reply.started":"2023-05-25T06:39:52.809017Z","shell.execute_reply":"2023-05-25T06:40:03.279333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_poly.predict(x_train)\n  \n\naccuracy = accuracy_score(y_pred, y_train)\n  \n\nprint(f\"The model is {accuracy*100}% accurate\")","metadata":{"execution":{"iopub.status.busy":"2023-05-25T06:40:05.018048Z","iopub.execute_input":"2023-05-25T06:40:05.0184Z","iopub.status.idle":"2023-05-25T06:40:10.97478Z","shell.execute_reply.started":"2023-05-25T06:40:05.018373Z","shell.execute_reply":"2023-05-25T06:40:10.974055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = OneVsRestClassifier(SVC(C=1, kernel='sigmoid')).fit(X, y)\nmodel.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T06:18:57.98395Z","iopub.execute_input":"2023-05-25T06:18:57.984834Z","iopub.status.idle":"2023-05-25T06:19:01.028596Z","shell.execute_reply.started":"2023-05-25T06:18:57.984792Z","shell.execute_reply":"2023-05-25T06:19:01.027724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(x_train)\n  \naccuracy = accuracy_score(y_pred, y_train)\n  \n\nprint(f\"The model is {accuracy*100}% accurate\")","metadata":{"execution":{"iopub.status.busy":"2023-05-25T06:19:01.030027Z","iopub.execute_input":"2023-05-25T06:19:01.030463Z","iopub.status.idle":"2023-05-25T06:19:02.743629Z","shell.execute_reply.started":"2023-05-25T06:19:01.030437Z","shell.execute_reply":"2023-05-25T06:19:02.742977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# flat_data_arr = []\n# target_arr = []\n# image_path = '/kaggle/input/benetech-making-graphs-accessible/train/images/0000ae6cbdb1.jpg'\n# for img in image_path:\n#         img_array=imread(img)\n#         img_resized=resize(img_array,(150,150,3))\n#         flat_data_arr.append(img_resized.flatten())\n#         target_arr.append(meta_df[meta_df['image_path']==img].label)\n    \n\n# clf = OneVsRestClassifier(SVC()).fit(flat_data_arr,target_arr )\n# clf.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T21:09:48.539496Z","iopub.execute_input":"2023-05-24T21:09:48.540646Z","iopub.status.idle":"2023-05-24T21:09:48.546508Z","shell.execute_reply.started":"2023-05-24T21:09:48.540601Z","shell.execute_reply":"2023-05-24T21:09:48.545322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## bounding boxes of images (ocr)","metadata":{}},{"cell_type":"code","source":"annotations = Path(\"/kaggle/input/benetech-making-graphs-accessible/train/annotations\")\nimages = Path(\"/kaggle/input/benetech-making-graphs-accessible/train/images\")\nall_annotations = list(annotations.iterdir())","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:15:11.096607Z","iopub.execute_input":"2023-05-24T13:15:11.097091Z","iopub.status.idle":"2023-05-24T13:15:11.62364Z","shell.execute_reply.started":"2023-05-24T13:15:11.097044Z","shell.execute_reply":"2023-05-24T13:15:11.622405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_with_annotations(filename, ax):\n    with open(filename, \"r\") as f:\n        row = pd.json_normalize(json.load(f)).iloc[0]\n        \n    img = Image.open(images / filename.with_suffix(\".jpg\").name)\n    \n    ax.imshow(img)\n    bbox = patches.Rectangle(\n        (row[\"plot-bb.x0\"], row[\"plot-bb.y0\"]), \n        row[\"plot-bb.width\"], \n        row[\"plot-bb.height\"], \n        linewidth=1, \n        edgecolor=\"r\", \n        facecolor=\"none\"\n    )\n    ax.add_patch(bbox)\n    \n    role2color = {\n        'axis_title': \"blue\",\n        'chart_title':\"green\",\n        'legend_label':\"yellow\",\n        'other':\"grey\",\n        'tick_grouping':\"brown\",\n        'tick_label':\"purple\",\n    }\n    \n    for text in row[\"text\"]:\n        polygon = text[\"polygon\"]\n        xs = np.array([v for k, v in polygon.items() if \"x\" in k])\n        ys = np.array([v for k, v in polygon.items() if \"y\" in k])\n        xy = np.vstack([xs, ys]).T\n        text_polygon = patches.Polygon(xy, linewidth=1, edgecolor=role2color[text[\"role\"]], facecolor=\"none\")\n        ax.add_patch(text_polygon)\n\n    \n    ax.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:15:11.874104Z","iopub.execute_input":"2023-05-24T13:15:11.874616Z","iopub.status.idle":"2023-05-24T13:15:11.887363Z","shell.execute_reply.started":"2023-05-24T13:15:11.874578Z","shell.execute_reply":"2023-05-24T13:15:11.885891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nrows, ncols = 2,2\nfig, ax = plt.subplots(nrows, ncols, figsize=(15, 10))\nfig.tight_layout()\nax = ax.flatten()\n\nidxs = np.random.randint(low=0, high=len(all_annotations), size=nrows*ncols)\n\nfor i, idx in enumerate(idxs):\n    plot_with_annotations(all_annotations[idx], ax[i])\n    \nplt.savefig(\"annotated_examples.pdf\", bbox_inches=\"tight\")","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:15:16.396963Z","iopub.execute_input":"2023-05-24T13:15:16.39785Z","iopub.status.idle":"2023-05-24T13:15:18.376213Z","shell.execute_reply.started":"2023-05-24T13:15:16.397798Z","shell.execute_reply":"2023-05-24T13:15:18.375167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}