{"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":"# Installing IceVision","metadata":{}},{"cell_type":"code","source":"!python --version","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:33:02.421874Z","iopub.execute_input":"2023-05-20T15:33:02.422598Z","iopub.status.idle":"2023-05-20T15:33:03.407286Z","shell.execute_reply.started":"2023-05-20T15:33:02.422558Z","shell.execute_reply":"2023-05-20T15:33:03.406109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Note:** You can ignore the 'errors' and warnings that are output by pip in the next code cell.","metadata":{}},{"cell_type":"code","source":"# Download IceVision installation script:\n!wget https://raw.githubusercontent.com/airctic/icevision/master/icevision_install.sh\n\n# Choose installation target: cuda11 or cuda10 or cpu\n!bash icevision_install.sh cuda11 master","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:33:11.863846Z","iopub.execute_input":"2023-05-20T15:33:11.864994Z","iopub.status.idle":"2023-05-20T15:40:04.264534Z","shell.execute_reply.started":"2023-05-20T15:33:11.864948Z","shell.execute_reply":"2023-05-20T15:40:04.263233Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Restart kernel after installation:\nimport IPython\n\nIPython.Application.instance().kernel.do_shutdown(True)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:40:36.884126Z","iopub.execute_input":"2023-05-20T15:40:36.884950Z","iopub.status.idle":"2023-05-20T15:40:36.895240Z","shell.execute_reply.started":"2023-05-20T15:40:36.884903Z","shell.execute_reply":"2023-05-20T15:40:36.894034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from icevision.all import *","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:40:54.585968Z","iopub.execute_input":"2023-05-20T15:40:54.586399Z","iopub.status.idle":"2023-05-20T15:42:08.695142Z","shell.execute_reply.started":"2023-05-20T15:40:54.586359Z","shell.execute_reply":"2023-05-20T15:42:08.692915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Let's begin!\")","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:42:17.583220Z","iopub.execute_input":"2023-05-20T15:42:17.583663Z","iopub.status.idle":"2023-05-20T15:42:17.589307Z","shell.execute_reply.started":"2023-05-20T15:42:17.583620Z","shell.execute_reply":"2023-05-20T15:42:17.587948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preparation","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\n\ncompetition_data_path = Path('/kaggle/input/benetech-making-graphs-accessible')\n!ls {competition_data_path}","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:42:26.020090Z","iopub.execute_input":"2023-05-20T15:42:26.020577Z","iopub.status.idle":"2023-05-20T15:42:27.037253Z","shell.execute_reply.started":"2023-05-20T15:42:26.020530Z","shell.execute_reply":"2023-05-20T15:42:27.035857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = competition_data_path/'train'\n!ls {train_path}","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:42:30.033251Z","iopub.execute_input":"2023-05-20T15:42:30.033673Z","iopub.status.idle":"2023-05-20T15:42:31.057595Z","shell.execute_reply.started":"2023-05-20T15:42:30.033631Z","shell.execute_reply":"2023-05-20T15:42:31.056179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"competition_images_path = train_path/'images'\ncompetition_annotations_path = train_path/'annotations'","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:42:33.448468Z","iopub.execute_input":"2023-05-20T15:42:33.448902Z","iopub.status.idle":"2023-05-20T15:42:33.458228Z","shell.execute_reply.started":"2023-05-20T15:42:33.448860Z","shell.execute_reply":"2023-05-20T15:42:33.456949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"extra_data_path = Path('/kaggle/input/b-m-g-a-extra-data')\n!ls {extra_data_path}","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:42:38.471202Z","iopub.execute_input":"2023-05-20T15:42:38.471603Z","iopub.status.idle":"2023-05-20T15:42:39.441077Z","shell.execute_reply.started":"2023-05-20T15:42:38.471564Z","shell.execute_reply":"2023-05-20T15:42:39.439658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"extra_images_path = extra_data_path/'y_tick_label_extra_314'/'y_tick_label_extra_314'\ncompetition_VOC_XML_path = extra_data_path/'labels_y_tick_label_60415_VOC_XML'\nextra_VOC_XML_path = extra_data_path/'labels_y_tick_label_extra_314_VOC_XML'/'labels_y_tick_label_extra_314_VOC_XML'","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:42:55.104270Z","iopub.execute_input":"2023-05-20T15:42:55.104717Z","iopub.status.idle":"2023-05-20T15:42:55.110262Z","shell.execute_reply.started":"2023-05-20T15:42:55.104655Z","shell.execute_reply":"2023-05-20T15:42:55.108991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's create a single folder in our working directory, and copy all the images into it.","metadata":{}},{"cell_type":"code","source":"working_dir = Path('/kaggle/working/')\nall_images_path = working_dir/'all_images'\nall_images_path.mkdir()","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:43:03.187194Z","iopub.execute_input":"2023-05-20T15:43:03.187606Z","iopub.status.idle":"2023-05-20T15:43:03.193048Z","shell.execute_reply.started":"2023-05-20T15:43:03.187564Z","shell.execute_reply":"2023-05-20T15:43:03.191632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"First, we'll copy the competition images:","metadata":{}},{"cell_type":"code","source":"from tqdm.notebook import tqdm\nimport shutil\n\norigin = str(competition_images_path) + '/'\ntarget = str(all_images_path) + '/'\ni = 0\nfor file_path in tqdm(sorted(competition_images_path.glob(\"*.jpg\"))):\n    file_name = str(file_path).split(\"/\")[-1]\n    shutil.copy(origin + file_name, target + file_name)\n    i += 1\nprint(f\"{i} files copied.\")","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:43:20.065457Z","iopub.execute_input":"2023-05-20T15:43:20.065891Z","iopub.status.idle":"2023-05-20T15:50:07.421772Z","shell.execute_reply.started":"2023-05-20T15:43:20.065847Z","shell.execute_reply":"2023-05-20T15:50:07.420526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Next, we'll copy the extra images:","metadata":{}},{"cell_type":"code","source":"origin = str(extra_images_path) + '/'\ni = 0\nfor file_path in sorted(extra_images_path.glob(\"*.jpg\")):\n    file_name = str(file_path).split(\"/\")[-1]\n    shutil.copy(origin + file_name, target + file_name)\n    i += 1\nprint(f\"{i} files copied.\")","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:50:32.098305Z","iopub.execute_input":"2023-05-20T15:50:32.098882Z","iopub.status.idle":"2023-05-20T15:50:33.084553Z","shell.execute_reply.started":"2023-05-20T15:50:32.098826Z","shell.execute_reply":"2023-05-20T15:50:33.083328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's see how many images we have in total.","metadata":{}},{"cell_type":"code","source":"i = 0\nfor file_path in sorted(all_images_path.glob(\"*.jpg\")):\n    i += 1\nprint(f\"{i} image files found.\")","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:50:39.375930Z","iopub.execute_input":"2023-05-20T15:50:39.376434Z","iopub.status.idle":"2023-05-20T15:50:40.579878Z","shell.execute_reply.started":"2023-05-20T15:50:39.376385Z","shell.execute_reply":"2023-05-20T15:50:40.578589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"60578 + 314","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:50:54.120833Z","iopub.execute_input":"2023-05-20T15:50:54.121274Z","iopub.status.idle":"2023-05-20T15:50:54.129310Z","shell.execute_reply.started":"2023-05-20T15:50:54.121235Z","shell.execute_reply":"2023-05-20T15:50:54.128003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Similarly, let's create a single folder in our working directory, and copy all the VOC XML files into it.","metadata":{}},{"cell_type":"code","source":"all_VOC_XML_path = working_dir/'all_VOC_XML'\nall_VOC_XML_path.mkdir()","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:51:02.559026Z","iopub.execute_input":"2023-05-20T15:51:02.559453Z","iopub.status.idle":"2023-05-20T15:51:02.564907Z","shell.execute_reply.started":"2023-05-20T15:51:02.559412Z","shell.execute_reply":"2023-05-20T15:51:02.563396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"First, we'll copy the VOC XML files corresponding to the competition images:","metadata":{}},{"cell_type":"code","source":"origin = str(competition_VOC_XML_path) + '/'\ntarget = str(all_VOC_XML_path) + '/'\ni = 0\nfor file_path in tqdm(sorted(competition_VOC_XML_path.glob(\"*.xml\"))):\n    file_name = str(file_path).split(\"/\")[-1]\n    shutil.copy(origin + file_name, target + file_name)\n    i += 1\nprint(f\"{i} files copied.\")","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:51:16.876398Z","iopub.execute_input":"2023-05-20T15:51:16.876947Z","iopub.status.idle":"2023-05-20T15:55:30.657083Z","shell.execute_reply.started":"2023-05-20T15:51:16.876891Z","shell.execute_reply":"2023-05-20T15:55:30.655816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Next, we'll copy the VOC XML files corresponding to the extra images:","metadata":{}},{"cell_type":"code","source":"origin = str(extra_VOC_XML_path) + '/'\ni = 0\nfor file_path in sorted(extra_VOC_XML_path.glob(\"*.xml\")):\n    file_name = str(file_path).split(\"/\")[-1]\n    shutil.copy(origin + file_name, target + file_name)\n    i += 1\nprint(f\"{i} files copied.\")","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:56:25.366628Z","iopub.execute_input":"2023-05-20T15:56:25.367163Z","iopub.status.idle":"2023-05-20T15:56:26.520647Z","shell.execute_reply.started":"2023-05-20T15:56:25.367117Z","shell.execute_reply":"2023-05-20T15:56:26.519465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's see how many VOC XML files we have in total.","metadata":{}},{"cell_type":"code","source":"i = 0\nfor file_path in sorted(all_VOC_XML_path.glob(\"*.xml\")):\n    i += 1\nprint(f\"{i} VOC XML files found.\")","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:56:34.110296Z","iopub.execute_input":"2023-05-20T15:56:34.110736Z","iopub.status.idle":"2023-05-20T15:56:35.428793Z","shell.execute_reply.started":"2023-05-20T15:56:34.110676Z","shell.execute_reply":"2023-05-20T15:56:35.427285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"60415 + 314","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:56:40.744339Z","iopub.execute_input":"2023-05-20T15:56:40.744764Z","iopub.status.idle":"2023-05-20T15:56:40.751130Z","shell.execute_reply.started":"2023-05-20T15:56:40.744717Z","shell.execute_reply":"2023-05-20T15:56:40.749918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now, let's create a data frame containing the image file names, sources and chart types.","metadata":{}},{"cell_type":"code","source":"import json\n\nimg_file_names = []\nsources = []\nchart_types = []\n\nfor file_path in tqdm(sorted(competition_annotations_path.glob(\"*.json\"))):\n    with open(file_path, \"r\") as json_file:\n        json_file_name = str(file_path).split(\"/\")[-1]\n        without_ext = json_file_name.split(\".\")[0]\n        img_file_name = without_ext + \".jpg\"\n        img_file_names.append(img_file_name)\n        data = json.load(json_file)\n        sources.append(data['source'])\n        chart_types.append(data['chart-type'])\n\nlen(img_file_names), len(sources), len(chart_types)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T15:56:48.852941Z","iopub.execute_input":"2023-05-20T15:56:48.853451Z","iopub.status.idle":"2023-05-20T16:04:15.859416Z","shell.execute_reply.started":"2023-05-20T15:56:48.853402Z","shell.execute_reply":"2023-05-20T16:04:15.858197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ndf = pd.DataFrame({'img_file_name': img_file_names, 'source': sources, 'chart_type': chart_types})\ndf","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:04:31.147652Z","iopub.execute_input":"2023-05-20T16:04:31.148240Z","iopub.status.idle":"2023-05-20T16:04:31.227673Z","shell.execute_reply.started":"2023-05-20T16:04:31.148187Z","shell.execute_reply":"2023-05-20T16:04:31.226580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We need to filter out the 'corrupt' images, i.e., the images where one or more bounding box lies outside the image.","metadata":{}},{"cell_type":"code","source":"corrupt_files_df = pd.read_csv(extra_data_path/'corrupt_files_y_tick_label_2px_pad.csv')\ncorrupt_files_df","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:04:42.157598Z","iopub.execute_input":"2023-05-20T16:04:42.158041Z","iopub.status.idle":"2023-05-20T16:04:42.179042Z","shell.execute_reply.started":"2023-05-20T16:04:42.157999Z","shell.execute_reply":"2023-05-20T16:04:42.177822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrupt = corrupt_files_df['corrupt_file'].tolist()\ncorrupt = [c[:-5] + \".jpg\" for c in corrupt]\ncorrupt[:5]","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:04:50.936690Z","iopub.execute_input":"2023-05-20T16:04:50.937129Z","iopub.status.idle":"2023-05-20T16:04:50.948359Z","shell.execute_reply.started":"2023-05-20T16:04:50.937089Z","shell.execute_reply":"2023-05-20T16:04:50.947112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.loc[~ df['img_file_name'].isin(corrupt)]\nlen(df)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:04:56.540905Z","iopub.execute_input":"2023-05-20T16:04:56.541327Z","iopub.status.idle":"2023-05-20T16:04:56.569738Z","shell.execute_reply.started":"2023-05-20T16:04:56.541286Z","shell.execute_reply":"2023-05-20T16:04:56.568589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Validation Scheme","metadata":{}},{"cell_type":"code","source":"from PIL import Image\n\nval_scheme = Image.open('/kaggle/input/b-m-g-a-extra-data/Validation_Scheme_Y_Tick_Label.png')\nval_scheme","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:05:19.079957Z","iopub.execute_input":"2023-05-20T16:05:19.080374Z","iopub.status.idle":"2023-05-20T16:05:19.220106Z","shell.execute_reply.started":"2023-05-20T16:05:19.080335Z","shell.execute_reply":"2023-05-20T16:05:19.216848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_file_names = []\ntrain_file_names = []","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:05:30.448536Z","iopub.execute_input":"2023-05-20T16:05:30.448973Z","iopub.status.idle":"2023-05-20T16:05:30.454236Z","shell.execute_reply.started":"2023-05-20T16:05:30.448930Z","shell.execute_reply":"2023-05-20T16:05:30.452598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We'll go chart type by chart type, and add the respective file names to the above lists.","metadata":{}},{"cell_type":"markdown","source":"## 'line'","metadata":{}},{"cell_type":"code","source":"line_extracted = df.loc[(df['source'] == 'extracted') & (df['chart_type'] == 'line')]\nlen(line_extracted)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:05:48.334276Z","iopub.execute_input":"2023-05-20T16:05:48.334680Z","iopub.status.idle":"2023-05-20T16:05:48.366128Z","shell.execute_reply.started":"2023-05-20T16:05:48.334638Z","shell.execute_reply":"2023-05-20T16:05:48.364536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Shuffle the rows:","metadata":{}},{"cell_type":"code","source":"line_extracted = line_extracted.sample(frac=1, random_state=42, ignore_index=True)\nline_extracted","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:05:54.512907Z","iopub.execute_input":"2023-05-20T16:05:54.513311Z","iopub.status.idle":"2023-05-20T16:05:54.530573Z","shell.execute_reply.started":"2023-05-20T16:05:54.513273Z","shell.execute_reply":"2023-05-20T16:05:54.529136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"line_extracted_in_val = line_extracted.iloc[:223]\nline_extracted_in_train = line_extracted.iloc[223:]\nlen(line_extracted_in_val), len(line_extracted_in_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:06:01.000248Z","iopub.execute_input":"2023-05-20T16:06:01.000649Z","iopub.status.idle":"2023-05-20T16:06:01.008968Z","shell.execute_reply.started":"2023-05-20T16:06:01.000609Z","shell.execute_reply":"2023-05-20T16:06:01.007567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_file_names.extend(line_extracted_in_val['img_file_name'].tolist())\ntrain_file_names.extend(line_extracted_in_train['img_file_name'].tolist())\nlen(val_file_names), len(train_file_names)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:06:04.195225Z","iopub.execute_input":"2023-05-20T16:06:04.195621Z","iopub.status.idle":"2023-05-20T16:06:04.204154Z","shell.execute_reply.started":"2023-05-20T16:06:04.195582Z","shell.execute_reply":"2023-05-20T16:06:04.202665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"line_generated = df.loc[(df['source'] == 'generated') & (df['chart_type'] == 'line')]\nlen(line_generated)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:06:07.727343Z","iopub.execute_input":"2023-05-20T16:06:07.727801Z","iopub.status.idle":"2023-05-20T16:06:07.761430Z","shell.execute_reply.started":"2023-05-20T16:06:07.727756Z","shell.execute_reply":"2023-05-20T16:06:07.760396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Shuffle the rows:","metadata":{}},{"cell_type":"code","source":"line_generated = line_generated.sample(frac=1, random_state=42, ignore_index=True)\nline_generated","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:06:11.094407Z","iopub.execute_input":"2023-05-20T16:06:11.094857Z","iopub.status.idle":"2023-05-20T16:06:11.117003Z","shell.execute_reply.started":"2023-05-20T16:06:11.094813Z","shell.execute_reply":"2023-05-20T16:06:11.115794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"line_generated_in_train = line_generated.iloc[:520]\nlen(line_generated_in_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:06:14.480939Z","iopub.execute_input":"2023-05-20T16:06:14.481355Z","iopub.status.idle":"2023-05-20T16:06:14.489230Z","shell.execute_reply.started":"2023-05-20T16:06:14.481314Z","shell.execute_reply":"2023-05-20T16:06:14.487820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file_names.extend(line_generated_in_train['img_file_name'].tolist())\nlen(val_file_names), len(train_file_names)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:06:16.954669Z","iopub.execute_input":"2023-05-20T16:06:16.955109Z","iopub.status.idle":"2023-05-20T16:06:16.963147Z","shell.execute_reply.started":"2023-05-20T16:06:16.955067Z","shell.execute_reply":"2023-05-20T16:06:16.961906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 'vertical_bar'","metadata":{}},{"cell_type":"code","source":"vertical_bar_extracted = df.loc[(df['source'] == 'extracted') & (df['chart_type'] == 'vertical_bar')]\nlen(vertical_bar_extracted)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:06:22.057582Z","iopub.execute_input":"2023-05-20T16:06:22.058137Z","iopub.status.idle":"2023-05-20T16:06:22.104503Z","shell.execute_reply.started":"2023-05-20T16:06:22.058084Z","shell.execute_reply":"2023-05-20T16:06:22.103337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Shuffle the rows:","metadata":{}},{"cell_type":"code","source":"vertical_bar_extracted = vertical_bar_extracted.sample(frac=1, random_state=42, ignore_index=True)\nvertical_bar_extracted","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:06:26.086736Z","iopub.execute_input":"2023-05-20T16:06:26.087156Z","iopub.status.idle":"2023-05-20T16:06:26.101479Z","shell.execute_reply.started":"2023-05-20T16:06:26.087118Z","shell.execute_reply":"2023-05-20T16:06:26.100355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vertical_bar_extracted_in_val = vertical_bar_extracted.iloc[:257]\nvertical_bar_extracted_in_train = vertical_bar_extracted.iloc[257:]\nlen(vertical_bar_extracted_in_val), len(vertical_bar_extracted_in_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:06:29.303697Z","iopub.execute_input":"2023-05-20T16:06:29.304129Z","iopub.status.idle":"2023-05-20T16:06:29.312399Z","shell.execute_reply.started":"2023-05-20T16:06:29.304091Z","shell.execute_reply":"2023-05-20T16:06:29.311052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_file_names.extend(vertical_bar_extracted_in_val['img_file_name'].tolist())\ntrain_file_names.extend(vertical_bar_extracted_in_train['img_file_name'].tolist())\nlen(val_file_names), len(train_file_names)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:06:31.905912Z","iopub.execute_input":"2023-05-20T16:06:31.906329Z","iopub.status.idle":"2023-05-20T16:06:31.914435Z","shell.execute_reply.started":"2023-05-20T16:06:31.906289Z","shell.execute_reply":"2023-05-20T16:06:31.913215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vertical_bar_generated = df.loc[(df['source'] == 'generated') & (df['chart_type'] == 'vertical_bar')]\nlen(vertical_bar_generated)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:06:34.274158Z","iopub.execute_input":"2023-05-20T16:06:34.274566Z","iopub.status.idle":"2023-05-20T16:06:34.306277Z","shell.execute_reply.started":"2023-05-20T16:06:34.274526Z","shell.execute_reply":"2023-05-20T16:06:34.305042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Shuffle the rows:","metadata":{}},{"cell_type":"code","source":"vertical_bar_generated = vertical_bar_generated.sample(frac=1, random_state=42, ignore_index=True)\nvertical_bar_generated","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:06:39.525507Z","iopub.execute_input":"2023-05-20T16:06:39.526032Z","iopub.status.idle":"2023-05-20T16:06:39.546422Z","shell.execute_reply.started":"2023-05-20T16:06:39.525982Z","shell.execute_reply":"2023-05-20T16:06:39.545114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vertical_bar_generated_in_train = vertical_bar_generated.iloc[:430]\nlen(vertical_bar_generated_in_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:02.596593Z","iopub.execute_input":"2023-05-20T16:07:02.597048Z","iopub.status.idle":"2023-05-20T16:07:02.604107Z","shell.execute_reply.started":"2023-05-20T16:07:02.597008Z","shell.execute_reply":"2023-05-20T16:07:02.602958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file_names.extend(vertical_bar_generated_in_train['img_file_name'].tolist())\nlen(val_file_names), len(train_file_names)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:05.036673Z","iopub.execute_input":"2023-05-20T16:07:05.037131Z","iopub.status.idle":"2023-05-20T16:07:05.045044Z","shell.execute_reply.started":"2023-05-20T16:07:05.037090Z","shell.execute_reply":"2023-05-20T16:07:05.043902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 'scatter'","metadata":{}},{"cell_type":"code","source":"scatter_extracted = df.loc[(df['source'] == 'extracted') & (df['chart_type'] == 'scatter')]\nlen(scatter_extracted)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:09.041831Z","iopub.execute_input":"2023-05-20T16:07:09.042260Z","iopub.status.idle":"2023-05-20T16:07:09.070541Z","shell.execute_reply.started":"2023-05-20T16:07:09.042220Z","shell.execute_reply":"2023-05-20T16:07:09.069029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Shuffle the rows:","metadata":{}},{"cell_type":"code","source":"scatter_extracted = scatter_extracted.sample(frac=1, random_state=42, ignore_index=True)\nscatter_extracted","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:13.703534Z","iopub.execute_input":"2023-05-20T16:07:13.703953Z","iopub.status.idle":"2023-05-20T16:07:13.718266Z","shell.execute_reply.started":"2023-05-20T16:07:13.703912Z","shell.execute_reply":"2023-05-20T16:07:13.717137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scatter_extracted_in_val = scatter_extracted.iloc[:115]\nscatter_extracted_in_train = scatter_extracted.iloc[115:]\nlen(scatter_extracted_in_val), len(scatter_extracted_in_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:18.064682Z","iopub.execute_input":"2023-05-20T16:07:18.065134Z","iopub.status.idle":"2023-05-20T16:07:18.072668Z","shell.execute_reply.started":"2023-05-20T16:07:18.065098Z","shell.execute_reply":"2023-05-20T16:07:18.071558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_file_names.extend(scatter_extracted_in_val['img_file_name'].tolist())\ntrain_file_names.extend(scatter_extracted_in_train['img_file_name'].tolist())\nlen(val_file_names), len(train_file_names)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:20.486386Z","iopub.execute_input":"2023-05-20T16:07:20.486816Z","iopub.status.idle":"2023-05-20T16:07:20.495445Z","shell.execute_reply.started":"2023-05-20T16:07:20.486770Z","shell.execute_reply":"2023-05-20T16:07:20.494291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scatter_generated = df.loc[(df['source'] == 'generated') & (df['chart_type'] == 'scatter')]\nlen(scatter_generated)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:22.670771Z","iopub.execute_input":"2023-05-20T16:07:22.671242Z","iopub.status.idle":"2023-05-20T16:07:22.703506Z","shell.execute_reply.started":"2023-05-20T16:07:22.671192Z","shell.execute_reply":"2023-05-20T16:07:22.701983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Shuffle the rows:","metadata":{}},{"cell_type":"code","source":"scatter_generated = scatter_generated.sample(frac=1, random_state=42, ignore_index=True)\nscatter_generated","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:27.494644Z","iopub.execute_input":"2023-05-20T16:07:27.495086Z","iopub.status.idle":"2023-05-20T16:07:27.515577Z","shell.execute_reply.started":"2023-05-20T16:07:27.495048Z","shell.execute_reply":"2023-05-20T16:07:27.514585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scatter_generated_in_train = scatter_generated.iloc[:280]\nlen(scatter_generated_in_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:30.509026Z","iopub.execute_input":"2023-05-20T16:07:30.509428Z","iopub.status.idle":"2023-05-20T16:07:30.517356Z","shell.execute_reply.started":"2023-05-20T16:07:30.509389Z","shell.execute_reply":"2023-05-20T16:07:30.515908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file_names.extend(scatter_generated_in_train['img_file_name'].tolist())\nlen(val_file_names), len(train_file_names)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:36.108583Z","iopub.execute_input":"2023-05-20T16:07:36.109147Z","iopub.status.idle":"2023-05-20T16:07:36.119297Z","shell.execute_reply.started":"2023-05-20T16:07:36.109092Z","shell.execute_reply":"2023-05-20T16:07:36.118025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 'dot'","metadata":{}},{"cell_type":"code","source":"dot = df.loc[df['chart_type'] == 'dot']\nlen(dot)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:42.365337Z","iopub.execute_input":"2023-05-20T16:07:42.365874Z","iopub.status.idle":"2023-05-20T16:07:42.390138Z","shell.execute_reply.started":"2023-05-20T16:07:42.365816Z","shell.execute_reply":"2023-05-20T16:07:42.388771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Shuffle the rows:","metadata":{}},{"cell_type":"code","source":"dot = dot.sample(frac=1, random_state=42, ignore_index=True)\ndot","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:46.110173Z","iopub.execute_input":"2023-05-20T16:07:46.110683Z","iopub.status.idle":"2023-05-20T16:07:46.132048Z","shell.execute_reply.started":"2023-05-20T16:07:46.110633Z","shell.execute_reply":"2023-05-20T16:07:46.130886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dot_in_val = dot.iloc[:332]\ndot_in_train = dot.iloc[332:652]\nlen(dot_in_val), len(dot_in_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:49.624858Z","iopub.execute_input":"2023-05-20T16:07:49.625372Z","iopub.status.idle":"2023-05-20T16:07:49.635576Z","shell.execute_reply.started":"2023-05-20T16:07:49.625319Z","shell.execute_reply":"2023-05-20T16:07:49.633849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_file_names.extend(dot_in_val['img_file_name'].tolist())\ntrain_file_names.extend(dot_in_train['img_file_name'].tolist())\nlen(val_file_names), len(train_file_names)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:52.800937Z","iopub.execute_input":"2023-05-20T16:07:52.801483Z","iopub.status.idle":"2023-05-20T16:07:52.820206Z","shell.execute_reply.started":"2023-05-20T16:07:52.801430Z","shell.execute_reply":"2023-05-20T16:07:52.819003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 'horizontal_bar'","metadata":{}},{"cell_type":"code","source":"horizontal_bar = df.loc[df['chart_type'] == 'horizontal_bar']\nlen(horizontal_bar)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:07:59.649344Z","iopub.execute_input":"2023-05-20T16:07:59.654983Z","iopub.status.idle":"2023-05-20T16:07:59.712009Z","shell.execute_reply.started":"2023-05-20T16:07:59.654916Z","shell.execute_reply":"2023-05-20T16:07:59.709944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_file_names.extend(horizontal_bar['img_file_name'].tolist())\nlen(val_file_names), len(train_file_names)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:08:00.182315Z","iopub.execute_input":"2023-05-20T16:08:00.182825Z","iopub.status.idle":"2023-05-20T16:08:00.193230Z","shell.execute_reply.started":"2023-05-20T16:08:00.182774Z","shell.execute_reply":"2023-05-20T16:08:00.191569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"extras = []\nfor file_path in extra_images_path.glob(\"*.jpg\"):\n    file_name = str(file_path).split(\"/\")[-1]\n    extras.append(file_name)\nlen(extras)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:08:14.367446Z","iopub.execute_input":"2023-05-20T16:08:14.367998Z","iopub.status.idle":"2023-05-20T16:08:14.389355Z","shell.execute_reply.started":"2023-05-20T16:08:14.367941Z","shell.execute_reply":"2023-05-20T16:08:14.388255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"extras[:5]","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:08:15.169693Z","iopub.execute_input":"2023-05-20T16:08:15.170233Z","iopub.status.idle":"2023-05-20T16:08:15.178854Z","shell.execute_reply.started":"2023-05-20T16:08:15.170181Z","shell.execute_reply":"2023-05-20T16:08:15.177372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file_names.extend(extras)\nlen(val_file_names), len(train_file_names)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:08:18.169076Z","iopub.execute_input":"2023-05-20T16:08:18.169584Z","iopub.status.idle":"2023-05-20T16:08:18.183958Z","shell.execute_reply.started":"2023-05-20T16:08:18.169536Z","shell.execute_reply":"2023-05-20T16:08:18.182616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"val_file_names = [f_n[:-4] for f_n in val_file_names]\nval_file_names[:5]","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:08:22.780951Z","iopub.execute_input":"2023-05-20T16:08:22.781474Z","iopub.status.idle":"2023-05-20T16:08:22.791637Z","shell.execute_reply.started":"2023-05-20T16:08:22.781424Z","shell.execute_reply":"2023-05-20T16:08:22.790354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file_names = [f_n[:-4] for f_n in train_file_names]\ntrain_file_names[:5]","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:08:23.455491Z","iopub.execute_input":"2023-05-20T16:08:23.456048Z","iopub.status.idle":"2023-05-20T16:08:23.468180Z","shell.execute_reply.started":"2023-05-20T16:08:23.455992Z","shell.execute_reply":"2023-05-20T16:08:23.466051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_splitter = FixedSplitter([train_file_names, val_file_names])","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:08:25.678769Z","iopub.execute_input":"2023-05-20T16:08:25.679255Z","iopub.status.idle":"2023-05-20T16:08:25.687376Z","shell.execute_reply.started":"2023-05-20T16:08:25.679209Z","shell.execute_reply":"2023-05-20T16:08:25.685932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the parser:\nparser = parsers.VOCBBoxParser(images_dir=all_images_path, annotations_dir=all_VOC_XML_path)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:08:30.224689Z","iopub.execute_input":"2023-05-20T16:08:30.225232Z","iopub.status.idle":"2023-05-20T16:08:32.415983Z","shell.execute_reply.started":"2023-05-20T16:08:30.225172Z","shell.execute_reply":"2023-05-20T16:08:32.414667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Parse annotations to create records:\ntrain_records, val_records = parser.parse(data_splitter, autofix=True)\ntype(train_records), type(val_records)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:08:32.421730Z","iopub.execute_input":"2023-05-20T16:08:32.424447Z","iopub.status.idle":"2023-05-20T16:09:58.855367Z","shell.execute_reply.started":"2023-05-20T16:08:32.424398Z","shell.execute_reply":"2023-05-20T16:09:58.854197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parser.class_map","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:10:00.788434Z","iopub.execute_input":"2023-05-20T16:10:00.788975Z","iopub.status.idle":"2023-05-20T16:10:00.799807Z","shell.execute_reply.started":"2023-05-20T16:10:00.788922Z","shell.execute_reply":"2023-05-20T16:10:00.798479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_records), len(val_records)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:10:04.622960Z","iopub.execute_input":"2023-05-20T16:10:04.623473Z","iopub.status.idle":"2023-05-20T16:10:04.660425Z","shell.execute_reply.started":"2023-05-20T16:10:04.623424Z","shell.execute_reply":"2023-05-20T16:10:04.651751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_records[0]","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:10:08.362051Z","iopub.execute_input":"2023-05-20T16:10:08.362558Z","iopub.status.idle":"2023-05-20T16:10:08.371530Z","shell.execute_reply.started":"2023-05-20T16:10:08.362509Z","shell.execute_reply":"2023-05-20T16:10:08.370219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_records[0]","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:10:08.815184Z","iopub.execute_input":"2023-05-20T16:10:08.815677Z","iopub.status.idle":"2023-05-20T16:10:08.824575Z","shell.execute_reply.started":"2023-05-20T16:10:08.815630Z","shell.execute_reply":"2023-05-20T16:10:08.823384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Training","metadata":{}},{"cell_type":"code","source":"import os\nimport random\nimport numpy as np\nimport torch\n\ndef seed_everything(seed):\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n\nseed_everything(42)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:10:16.796412Z","iopub.execute_input":"2023-05-20T16:10:16.796969Z","iopub.status.idle":"2023-05-20T16:10:16.808183Z","shell.execute_reply.started":"2023-05-20T16:10:16.796913Z","shell.execute_reply":"2023-05-20T16:10:16.806880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"128 * 5","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:10:38.760051Z","iopub.execute_input":"2023-05-20T16:10:38.760564Z","iopub.status.idle":"2023-05-20T16:10:38.773190Z","shell.execute_reply.started":"2023-05-20T16:10:38.760516Z","shell.execute_reply":"2023-05-20T16:10:38.771373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Transforms:\nimage_size = 640\ntrain_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(image_size), tfms.A.Normalize()])\nval_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(image_size), tfms.A.Normalize()])\ntype(train_tfms), type(val_tfms)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:10:39.319258Z","iopub.execute_input":"2023-05-20T16:10:39.319811Z","iopub.status.idle":"2023-05-20T16:10:39.334414Z","shell.execute_reply.started":"2023-05-20T16:10:39.319755Z","shell.execute_reply":"2023-05-20T16:10:39.332015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tfms.tfms_list","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:10:43.502469Z","iopub.execute_input":"2023-05-20T16:10:43.503454Z","iopub.status.idle":"2023-05-20T16:10:43.511749Z","shell.execute_reply.started":"2023-05-20T16:10:43.503401Z","shell.execute_reply":"2023-05-20T16:10:43.510522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_tfms.tfms_list","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:10:46.422132Z","iopub.execute_input":"2023-05-20T16:10:46.422634Z","iopub.status.idle":"2023-05-20T16:10:46.431575Z","shell.execute_reply.started":"2023-05-20T16:10:46.422585Z","shell.execute_reply":"2023-05-20T16:10:46.430304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# `Dataset` objects:\ntrain_ds = Dataset(train_records, train_tfms)\nval_ds = Dataset(val_records, val_tfms)\ntype(train_ds), type(val_ds)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:10:49.908670Z","iopub.execute_input":"2023-05-20T16:10:49.909213Z","iopub.status.idle":"2023-05-20T16:10:49.925149Z","shell.execute_reply.started":"2023-05-20T16:10:49.909159Z","shell.execute_reply":"2023-05-20T16:10:49.924058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show a few elements of `train_ds` with transformations applied:\nsamples = [train_ds[i] for i in range(4)]\nshow_samples(samples, display_label=False, ncols=2)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:10:54.383820Z","iopub.execute_input":"2023-05-20T16:10:54.384317Z","iopub.status.idle":"2023-05-20T16:10:58.829922Z","shell.execute_reply.started":"2023-05-20T16:10:54.384267Z","shell.execute_reply":"2023-05-20T16:10:58.828811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show a few elements of `val_ds` with transformations applied:\nsamples = [val_ds[i] for i in range(4)]\nshow_samples(samples, display_label=False, ncols=2)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:11:10.401312Z","iopub.execute_input":"2023-05-20T16:11:10.401857Z","iopub.status.idle":"2023-05-20T16:11:14.979246Z","shell.execute_reply.started":"2023-05-20T16:11:10.401800Z","shell.execute_reply":"2023-05-20T16:11:14.976803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"extra_args = {}\n\nmodel_type = models.torchvision.faster_rcnn\nbackbone = model_type.backbones.resnet50_fpn\n\nmodel_type, backbone, extra_args","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:11:24.270851Z","iopub.execute_input":"2023-05-20T16:11:24.271370Z","iopub.status.idle":"2023-05-20T16:11:24.281814Z","shell.execute_reply.started":"2023-05-20T16:11:24.271322Z","shell.execute_reply":"2023-05-20T16:11:24.280340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Instantiate the model:\nmodel = model_type.model(backbone=backbone(pretrained=True), num_classes=len(parser.class_map), **extra_args)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:11:27.644365Z","iopub.execute_input":"2023-05-20T16:11:27.644921Z","iopub.status.idle":"2023-05-20T16:11:30.379872Z","shell.execute_reply.started":"2023-05-20T16:11:27.644864Z","shell.execute_reply":"2023-05-20T16:11:30.378607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dl = model_type.train_dl(train_ds, batch_size=8, num_workers=2, shuffle=True)\nval_dl = model_type.valid_dl(val_ds, batch_size=8, num_workers=2, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:11:31.776119Z","iopub.execute_input":"2023-05-20T16:11:31.777092Z","iopub.status.idle":"2023-05-20T16:11:31.784727Z","shell.execute_reply.started":"2023-05-20T16:11:31.777039Z","shell.execute_reply":"2023-05-20T16:11:31.783586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_dl), len(val_dl)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:11:35.094567Z","iopub.execute_input":"2023-05-20T16:11:35.095123Z","iopub.status.idle":"2023-05-20T16:11:35.106854Z","shell.execute_reply.started":"2023-05-20T16:11:35.095071Z","shell.execute_reply":"2023-05-20T16:11:35.105670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_type.show_batch(first(train_dl), display_label=False, ncols=2)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:11:37.180184Z","iopub.execute_input":"2023-05-20T16:11:37.180695Z","iopub.status.idle":"2023-05-20T16:11:47.265044Z","shell.execute_reply.started":"2023-05-20T16:11:37.180646Z","shell.execute_reply":"2023-05-20T16:11:47.263841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_type.show_batch(first(val_dl), display_label=False, ncols=2)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:11:59.562770Z","iopub.execute_input":"2023-05-20T16:11:59.563279Z","iopub.status.idle":"2023-05-20T16:12:09.624415Z","shell.execute_reply.started":"2023-05-20T16:11:59.563228Z","shell.execute_reply":"2023-05-20T16:12:09.623006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Accuracy(SimpleConfusionMatrix):\n    def __init__(self):\n        super().__init__()\n    \n    def finalize(self):\n        assert len(self.target_labels) == len(self.predicted_labels)\n        label_ids = list(self.class_map._class2id.values())\n        self.confusion_matrix = sklearn.metrics.confusion_matrix(\n            y_true=self.target_labels,\n            y_pred=self.predicted_labels,\n            labels=label_ids,\n        )\n        accuracy = (self.confusion_matrix[0, 0] + self.confusion_matrix[1, 1]) / self.confusion_matrix.sum()\n        if self.print_summary:\n            print(self.confusion_matrix)\n        self._reset()\n        return {\"accuracy\": accuracy}","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:12:23.008343Z","iopub.execute_input":"2023-05-20T16:12:23.008910Z","iopub.status.idle":"2023-05-20T16:12:23.023892Z","shell.execute_reply.started":"2023-05-20T16:12:23.008854Z","shell.execute_reply":"2023-05-20T16:12:23.022377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class NumErrors(SimpleConfusionMatrix):\n    def __init__(self):\n        super().__init__()\n    \n    def finalize(self):\n        assert len(self.target_labels) == len(self.predicted_labels)\n        label_ids = list(self.class_map._class2id.values())\n        self.confusion_matrix = sklearn.metrics.confusion_matrix(\n            y_true=self.target_labels,\n            y_pred=self.predicted_labels,\n            labels=label_ids,\n        )\n        num_errors = self.confusion_matrix[0, 1] + self.confusion_matrix[1, 0]\n        if self.print_summary:\n            print(self.confusion_matrix)\n        self._reset()\n        return {\"num_errors\": num_errors}","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:12:25.153073Z","iopub.execute_input":"2023-05-20T16:12:25.153575Z","iopub.status.idle":"2023-05-20T16:12:25.164085Z","shell.execute_reply.started":"2023-05-20T16:12:25.153526Z","shell.execute_reply":"2023-05-20T16:12:25.162243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics = [COCOMetric(metric_type=COCOMetricType.bbox), SimpleConfusionMatrix(), Accuracy(), NumErrors()]","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:12:31.111003Z","iopub.execute_input":"2023-05-20T16:12:31.111536Z","iopub.status.idle":"2023-05-20T16:12:31.118300Z","shell.execute_reply.started":"2023-05-20T16:12:31.111487Z","shell.execute_reply":"2023-05-20T16:12:31.116927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.callback.tracker import SaveModelCallback\n\ncb = SaveModelCallback(monitor='Accuracy', fname='y_tick_label_', every_epoch=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:12:38.587659Z","iopub.execute_input":"2023-05-20T16:12:38.588195Z","iopub.status.idle":"2023-05-20T16:12:38.593980Z","shell.execute_reply.started":"2023-05-20T16:12:38.588152Z","shell.execute_reply":"2023-05-20T16:12:38.592614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = model_type.fastai.learner(dls=[train_dl, val_dl], model=model, metrics=metrics, cbs=[cb])","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:12:41.193028Z","iopub.execute_input":"2023-05-20T16:12:41.193439Z","iopub.status.idle":"2023-05-20T16:12:41.221176Z","shell.execute_reply.started":"2023-05-20T16:12:41.193401Z","shell.execute_reply":"2023-05-20T16:12:41.219907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:12:44.292603Z","iopub.execute_input":"2023-05-20T16:12:44.293063Z","iopub.status.idle":"2023-05-20T16:13:33.800092Z","shell.execute_reply.started":"2023-05-20T16:12:44.293022Z","shell.execute_reply":"2023-05-20T16:13:33.798747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(10, 1.5e-3, freeze_epochs=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T16:13:41.185695Z","iopub.execute_input":"2023-05-20T16:13:41.186273Z","iopub.status.idle":"2023-05-20T17:19:08.151317Z","shell.execute_reply.started":"2023-05-20T16:13:41.186219Z","shell.execute_reply":"2023-05-20T17:19:08.149821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.load('y_tick_label__6')","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:20:00.857650Z","iopub.execute_input":"2023-05-20T17:20:00.858103Z","iopub.status.idle":"2023-05-20T17:20:01.023607Z","shell.execute_reply.started":"2023-05-20T17:20:00.858062Z","shell.execute_reply":"2023-05-20T17:20:01.022253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import icevision\n\nicevision.__version__","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:20:12.488569Z","iopub.execute_input":"2023-05-20T17:20:12.489058Z","iopub.status.idle":"2023-05-20T17:20:12.497084Z","shell.execute_reply.started":"2023-05-20T17:20:12.489012Z","shell.execute_reply":"2023-05-20T17:20:12.495729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from icevision.models import *\n\nsave_icevision_checkpoint(\n    model, \n    model_name='torchvision.faster_rcnn', \n    backbone_name='resnet50_fpn',\n    classes=parser.class_map.get_classes(), \n    img_size=image_size, \n    filename=\"faster_rcnn-y_tick_label.pth\", \n    meta={'icevision_version': '0.12.0'}\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:20:20.845339Z","iopub.execute_input":"2023-05-20T17:20:20.845889Z","iopub.status.idle":"2023-05-20T17:20:21.655718Z","shell.execute_reply.started":"2023-05-20T17:20:20.845837Z","shell.execute_reply":"2023-05-20T17:20:21.654295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_and_model = model_from_checkpoint('/kaggle/working/faster_rcnn-y_tick_label.pth')\ntype(checkpoint_and_model)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:20:28.690286Z","iopub.execute_input":"2023-05-20T17:20:28.690895Z","iopub.status.idle":"2023-05-20T17:20:30.156594Z","shell.execute_reply.started":"2023-05-20T17:20:28.690824Z","shell.execute_reply":"2023-05-20T17:20:30.151981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_map = checkpoint_and_model[\"class_map\"]\nimg_size = checkpoint_and_model[\"img_size\"]\nmodel_type = checkpoint_and_model[\"model_type\"]\nbackbone = checkpoint_and_model[\"backbone\"]\nloaded_model = checkpoint_and_model[\"model\"]","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:20:33.473976Z","iopub.execute_input":"2023-05-20T17:20:33.474420Z","iopub.status.idle":"2023-05-20T17:20:33.480192Z","shell.execute_reply.started":"2023-05-20T17:20:33.474377Z","shell.execute_reply":"2023-05-20T17:20:33.478937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Evaluation","metadata":{}},{"cell_type":"code","source":"learn.validate(dl=val_dl)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:20:38.036605Z","iopub.execute_input":"2023-05-20T17:20:38.037035Z","iopub.status.idle":"2023-05-20T17:22:18.718172Z","shell.execute_reply.started":"2023-05-20T17:20:38.036995Z","shell.execute_reply":"2023-05-20T17:22:18.716796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The above list contains `valid_loss`, `COCOMetric`, `SimpleConfusionMatrix`, `Accuracy` and `NumErrors` corresponding to the best epoch.","metadata":{}},{"cell_type":"code","source":"metrics[1].confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:22:33.091227Z","iopub.execute_input":"2023-05-20T17:22:33.091645Z","iopub.status.idle":"2023-05-20T17:22:33.099842Z","shell.execute_reply.started":"2023-05-20T17:22:33.091605Z","shell.execute_reply":"2023-05-20T17:22:33.098387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics[1].plot()","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:22:36.387281Z","iopub.execute_input":"2023-05-20T17:22:36.387680Z","iopub.status.idle":"2023-05-20T17:22:36.861114Z","shell.execute_reply.started":"2023-05-20T17:22:36.387642Z","shell.execute_reply":"2023-05-20T17:22:36.859789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's examine the model's predictions on 8 randomly chosen images from the validation set.","metadata":{}},{"cell_type":"code","source":"val_file_names = [f_n + '.jpg' for f_n in val_file_names]","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:22:48.621596Z","iopub.execute_input":"2023-05-20T17:22:48.622053Z","iopub.status.idle":"2023-05-20T17:22:48.627918Z","shell.execute_reply.started":"2023-05-20T17:22:48.622010Z","shell.execute_reply":"2023-05-20T17:22:48.626510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.seed(21)\nrandom.shuffle(val_file_names)\nval_file_names[:8]","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:23:33.063420Z","iopub.execute_input":"2023-05-20T17:23:33.063982Z","iopub.status.idle":"2023-05-20T17:23:33.075119Z","shell.execute_reply.started":"2023-05-20T17:23:33.063926Z","shell.execute_reply":"2023-05-20T17:23:33.073851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/val_file_names[0])\npred_dict = model_type.end2end_detect(img, val_tfms, loaded_model, class_map=class_map, detection_threshold=0.5, display_label=False)\npred_dict['img']","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:23:43.734061Z","iopub.execute_input":"2023-05-20T17:23:43.734463Z","iopub.status.idle":"2023-05-20T17:23:46.237468Z","shell.execute_reply.started":"2023-05-20T17:23:43.734422Z","shell.execute_reply":"2023-05-20T17:23:46.236146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/val_file_names[1])\npred_dict = model_type.end2end_detect(img, val_tfms, loaded_model, class_map=class_map, detection_threshold=0.5, display_label=False)\npred_dict['img']","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:23:50.906157Z","iopub.execute_input":"2023-05-20T17:23:50.906609Z","iopub.status.idle":"2023-05-20T17:23:53.055418Z","shell.execute_reply.started":"2023-05-20T17:23:50.906566Z","shell.execute_reply":"2023-05-20T17:23:53.054115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/val_file_names[2])\npred_dict = model_type.end2end_detect(img, val_tfms, loaded_model, class_map=class_map, detection_threshold=0.5, display_label=False)\npred_dict['img']","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:24:01.046690Z","iopub.execute_input":"2023-05-20T17:24:01.047131Z","iopub.status.idle":"2023-05-20T17:24:03.192923Z","shell.execute_reply.started":"2023-05-20T17:24:01.047090Z","shell.execute_reply":"2023-05-20T17:24:03.191762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/val_file_names[3])\npred_dict = model_type.end2end_detect(img, val_tfms, loaded_model, class_map=class_map, detection_threshold=0.5, display_label=False)\npred_dict['img']","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:24:08.955739Z","iopub.execute_input":"2023-05-20T17:24:08.956161Z","iopub.status.idle":"2023-05-20T17:24:11.002449Z","shell.execute_reply.started":"2023-05-20T17:24:08.956121Z","shell.execute_reply":"2023-05-20T17:24:11.001175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/val_file_names[4])\npred_dict = model_type.end2end_detect(img, val_tfms, loaded_model, class_map=class_map, detection_threshold=0.5, display_label=False)\npred_dict['img']","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:24:14.626554Z","iopub.execute_input":"2023-05-20T17:24:14.626992Z","iopub.status.idle":"2023-05-20T17:24:16.799152Z","shell.execute_reply.started":"2023-05-20T17:24:14.626950Z","shell.execute_reply":"2023-05-20T17:24:16.797828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/val_file_names[5])\npred_dict = model_type.end2end_detect(img, val_tfms, loaded_model, class_map=class_map, detection_threshold=0.5, display_label=False)\npred_dict['img']","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:24:23.043418Z","iopub.execute_input":"2023-05-20T17:24:23.043884Z","iopub.status.idle":"2023-05-20T17:24:25.153244Z","shell.execute_reply.started":"2023-05-20T17:24:23.043841Z","shell.execute_reply":"2023-05-20T17:24:25.151941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/val_file_names[6])\npred_dict = model_type.end2end_detect(img, val_tfms, loaded_model, class_map=class_map, detection_threshold=0.5, display_label=False)\npred_dict['img']","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:24:30.342901Z","iopub.execute_input":"2023-05-20T17:24:30.343304Z","iopub.status.idle":"2023-05-20T17:24:32.438580Z","shell.execute_reply.started":"2023-05-20T17:24:30.343265Z","shell.execute_reply":"2023-05-20T17:24:32.437264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/val_file_names[7])\npred_dict = model_type.end2end_detect(img, val_tfms, loaded_model, class_map=class_map, detection_threshold=0.5, display_label=False)\npred_dict['img']","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:24:36.252732Z","iopub.execute_input":"2023-05-20T17:24:36.253139Z","iopub.status.idle":"2023-05-20T17:24:38.482149Z","shell.execute_reply.started":"2023-05-20T17:24:36.253100Z","shell.execute_reply":"2023-05-20T17:24:38.480914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Finally, let's take a look at some of the top loss images.\n\n**Note:** The `model_type.interp.plot_top_losses()` method is exhausting the 13GB of RAM provided by Kaggle. Hence, I've run it on a separate instance, and gathered the file names of the top loss images. We'll examine the predictions on these images below.","metadata":{}},{"cell_type":"code","source":"img = Image.open(all_images_path/'0e46c988beac.jpg')\npred_dict = model_type.end2end_detect(img, val_tfms, loaded_model, class_map=class_map, detection_threshold=0.5, display_label=False)\npred_dict['img']","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:24:58.767392Z","iopub.execute_input":"2023-05-20T17:24:58.767984Z","iopub.status.idle":"2023-05-20T17:25:02.356128Z","shell.execute_reply.started":"2023-05-20T17:24:58.767927Z","shell.execute_reply":"2023-05-20T17:25:02.354967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/'d0f1d33c137f.jpg')\npred_dict = model_type.end2end_detect(img, val_tfms, loaded_model, class_map=class_map, detection_threshold=0.5, display_label=False)\npred_dict['img']","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:25:15.938918Z","iopub.execute_input":"2023-05-20T17:25:15.939346Z","iopub.status.idle":"2023-05-20T17:25:18.033406Z","shell.execute_reply.started":"2023-05-20T17:25:15.939304Z","shell.execute_reply":"2023-05-20T17:25:18.032151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/'c681d45e7d37.jpg')\npred_dict = model_type.end2end_detect(img, val_tfms, loaded_model, class_map=class_map, detection_threshold=0.5, display_label=False)\npred_dict['img']","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:25:43.202509Z","iopub.execute_input":"2023-05-20T17:25:43.203048Z","iopub.status.idle":"2023-05-20T17:25:47.093979Z","shell.execute_reply.started":"2023-05-20T17:25:43.202995Z","shell.execute_reply":"2023-05-20T17:25:47.092879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/'7dfde4a8afb2.jpg')\npred_dict = model_type.end2end_detect(img, val_tfms, loaded_model, class_map=class_map, detection_threshold=0.5, display_label=False)\npred_dict['img']","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:25:58.507518Z","iopub.execute_input":"2023-05-20T17:25:58.508051Z","iopub.status.idle":"2023-05-20T17:26:01.864482Z","shell.execute_reply.started":"2023-05-20T17:25:58.507998Z","shell.execute_reply":"2023-05-20T17:26:01.863326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/'6627e6967a98.jpg')\npred_dict = model_type.end2end_detect(img, val_tfms, loaded_model, class_map=class_map, detection_threshold=0.5, display_label=False)\npred_dict['img']","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:26:18.896080Z","iopub.execute_input":"2023-05-20T17:26:18.896568Z","iopub.status.idle":"2023-05-20T17:26:20.971039Z","shell.execute_reply.started":"2023-05-20T17:26:18.896522Z","shell.execute_reply":"2023-05-20T17:26:20.969745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Cleanup:\n!rm -r /kaggle/working/all_images/\n!rm -r /kaggle/working/all_VOC_XML/\n!rm /kaggle/working/icevision_install.sh","metadata":{"execution":{"iopub.status.busy":"2023-05-20T17:27:02.896125Z","iopub.execute_input":"2023-05-20T17:27:02.896552Z","iopub.status.idle":"2023-05-20T17:27:08.976685Z","shell.execute_reply.started":"2023-05-20T17:27:02.896510Z","shell.execute_reply":"2023-05-20T17:27:08.975090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}