{"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-29T07:35:52.782624Z","iopub.execute_input":"2023-05-29T07:35:52.783413Z","iopub.status.idle":"2023-05-29T07:35:53.780292Z","shell.execute_reply.started":"2023-05-29T07:35:52.783370Z","shell.execute_reply":"2023-05-29T07:35:53.779000Z"},"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-29T07:36:14.797616Z","iopub.execute_input":"2023-05-29T07:36:14.798380Z","iopub.status.idle":"2023-05-29T07:43:26.226136Z","shell.execute_reply.started":"2023-05-29T07:36:14.798331Z","shell.execute_reply":"2023-05-29T07:43:26.224871Z"},"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-29T07:43:39.642130Z","iopub.execute_input":"2023-05-29T07:43:39.643168Z","iopub.status.idle":"2023-05-29T07:43:39.653592Z","shell.execute_reply.started":"2023-05-29T07:43:39.643086Z","shell.execute_reply":"2023-05-29T07:43:39.652307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from icevision.all import *","metadata":{"execution":{"iopub.status.busy":"2023-05-29T07:46:01.347117Z","iopub.execute_input":"2023-05-29T07:46:01.347605Z","iopub.status.idle":"2023-05-29T07:47:37.432967Z","shell.execute_reply.started":"2023-05-29T07:46:01.347560Z","shell.execute_reply":"2023-05-29T07:47:37.432105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Let's begin!\")","metadata":{"execution":{"iopub.status.busy":"2023-05-29T07:48:16.458419Z","iopub.execute_input":"2023-05-29T07:48:16.458857Z","iopub.status.idle":"2023-05-29T07:48:16.465029Z","shell.execute_reply.started":"2023-05-29T07:48:16.458809Z","shell.execute_reply":"2023-05-29T07:48:16.463601Z"},"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-29T07:48:24.974815Z","iopub.execute_input":"2023-05-29T07:48:24.975259Z","iopub.status.idle":"2023-05-29T07:48:25.974600Z","shell.execute_reply.started":"2023-05-29T07:48:24.975211Z","shell.execute_reply":"2023-05-29T07:48:25.973221Z"},"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-29T07:48:29.807977Z","iopub.execute_input":"2023-05-29T07:48:29.808423Z","iopub.status.idle":"2023-05-29T07:48:30.808397Z","shell.execute_reply.started":"2023-05-29T07:48:29.808380Z","shell.execute_reply":"2023-05-29T07:48:30.806757Z"},"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-29T07:48:32.435380Z","iopub.execute_input":"2023-05-29T07:48:32.435837Z","iopub.status.idle":"2023-05-29T07:48:32.441585Z","shell.execute_reply.started":"2023-05-29T07:48:32.435790Z","shell.execute_reply":"2023-05-29T07:48:32.440207Z"},"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-29T07:48:38.561605Z","iopub.execute_input":"2023-05-29T07:48:38.562034Z","iopub.status.idle":"2023-05-29T07:48:39.580925Z","shell.execute_reply.started":"2023-05-29T07:48:38.561991Z","shell.execute_reply":"2023-05-29T07:48:39.579297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"extra_images_path = extra_data_path/'x_tick_label_extra_346'\ncompetition_VOC_XML_path = extra_data_path/'labels_x_tick_label_33947_VOC_XML'\nextra_VOC_XML_path = extra_data_path/'labels_x_tick_label_extra_346_VOC_XML'","metadata":{"execution":{"iopub.status.busy":"2023-05-29T07:49:06.846069Z","iopub.execute_input":"2023-05-29T07:49:06.846557Z","iopub.status.idle":"2023-05-29T07:49:06.852857Z","shell.execute_reply.started":"2023-05-29T07:49:06.846510Z","shell.execute_reply":"2023-05-29T07:49:06.851591Z"},"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-29T07:49:14.739027Z","iopub.execute_input":"2023-05-29T07:49:14.739477Z","iopub.status.idle":"2023-05-29T07:49:14.745412Z","shell.execute_reply.started":"2023-05-29T07:49:14.739434Z","shell.execute_reply":"2023-05-29T07:49:14.744034Z"},"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-29T07:49:40.995192Z","iopub.execute_input":"2023-05-29T07:49:40.995611Z","iopub.status.idle":"2023-05-29T07:56:05.507495Z","shell.execute_reply.started":"2023-05-29T07:49:40.995573Z","shell.execute_reply":"2023-05-29T07:56:05.506250Z"},"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-29T07:56:29.423102Z","iopub.execute_input":"2023-05-29T07:56:29.423560Z","iopub.status.idle":"2023-05-29T07:56:31.716244Z","shell.execute_reply.started":"2023-05-29T07:56:29.423519Z","shell.execute_reply":"2023-05-29T07:56:31.715061Z"},"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-29T07:56:42.374361Z","iopub.execute_input":"2023-05-29T07:56:42.374780Z","iopub.status.idle":"2023-05-29T07:56:43.487500Z","shell.execute_reply.started":"2023-05-29T07:56:42.374740Z","shell.execute_reply":"2023-05-29T07:56:43.486131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"60578 + 346","metadata":{"execution":{"iopub.status.busy":"2023-05-29T07:56:52.978902Z","iopub.execute_input":"2023-05-29T07:56:52.979348Z","iopub.status.idle":"2023-05-29T07:56:52.987478Z","shell.execute_reply.started":"2023-05-29T07:56:52.979302Z","shell.execute_reply":"2023-05-29T07:56:52.986027Z"},"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-29T07:57:03.278403Z","iopub.execute_input":"2023-05-29T07:57:03.278945Z","iopub.status.idle":"2023-05-29T07:57:03.284806Z","shell.execute_reply.started":"2023-05-29T07:57:03.278902Z","shell.execute_reply":"2023-05-29T07:57:03.283449Z"},"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-29T07:57:17.870836Z","iopub.execute_input":"2023-05-29T07:57:17.871279Z","iopub.status.idle":"2023-05-29T08:02:08.778512Z","shell.execute_reply.started":"2023-05-29T07:57:17.871235Z","shell.execute_reply":"2023-05-29T08:02:08.777261Z"},"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-29T08:02:20.103641Z","iopub.execute_input":"2023-05-29T08:02:20.104176Z","iopub.status.idle":"2023-05-29T08:02:22.204783Z","shell.execute_reply.started":"2023-05-29T08:02:20.104106Z","shell.execute_reply":"2023-05-29T08:02:22.203647Z"},"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-29T08:02:29.629208Z","iopub.execute_input":"2023-05-29T08:02:29.629635Z","iopub.status.idle":"2023-05-29T08:02:30.208729Z","shell.execute_reply.started":"2023-05-29T08:02:29.629598Z","shell.execute_reply":"2023-05-29T08:02:30.207265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"33947 + 346","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:02:38.007076Z","iopub.execute_input":"2023-05-29T08:02:38.007534Z","iopub.status.idle":"2023-05-29T08:02:38.014949Z","shell.execute_reply.started":"2023-05-29T08:02:38.007490Z","shell.execute_reply":"2023-05-29T08:02:38.013597Z"},"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-29T08:02:46.343748Z","iopub.execute_input":"2023-05-29T08:02:46.344195Z","iopub.status.idle":"2023-05-29T08:09:27.448549Z","shell.execute_reply.started":"2023-05-29T08:02:46.344128Z","shell.execute_reply":"2023-05-29T08:09:27.447330Z"},"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-29T08:09:36.043367Z","iopub.execute_input":"2023-05-29T08:09:36.043877Z","iopub.status.idle":"2023-05-29T08:09:36.124854Z","shell.execute_reply.started":"2023-05-29T08:09:36.043826Z","shell.execute_reply":"2023-05-29T08:09:36.123733Z"},"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_x_tick_label_2px_pad.csv')\ncorrupt_files_df","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:09:50.332662Z","iopub.execute_input":"2023-05-29T08:09:50.333105Z","iopub.status.idle":"2023-05-29T08:09:50.356427Z","shell.execute_reply.started":"2023-05-29T08:09:50.333063Z","shell.execute_reply":"2023-05-29T08:09:50.355349Z"},"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-29T08:10:31.243849Z","iopub.execute_input":"2023-05-29T08:10:31.244320Z","iopub.status.idle":"2023-05-29T08:10:31.256591Z","shell.execute_reply.started":"2023-05-29T08:10:31.244270Z","shell.execute_reply":"2023-05-29T08:10:31.255324Z"},"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-29T08:10:36.822496Z","iopub.execute_input":"2023-05-29T08:10:36.823015Z","iopub.status.idle":"2023-05-29T08:10:36.863577Z","shell.execute_reply.started":"2023-05-29T08:10:36.822960Z","shell.execute_reply":"2023-05-29T08:10:36.861436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"60578 - 172","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:10:39.944507Z","iopub.execute_input":"2023-05-29T08:10:39.944964Z","iopub.status.idle":"2023-05-29T08:10:39.952460Z","shell.execute_reply.started":"2023-05-29T08:10:39.944918Z","shell.execute_reply":"2023-05-29T08:10:39.951258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We also need to filter out the images containing rotated x-axis tick labels. (We'll have a separate pipeline for those images.)","metadata":{}},{"cell_type":"code","source":"rotation_data = pd.read_csv(extra_data_path/'rotated_vs_non_rotated_x_tick_labels.csv')\nrotation_data","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:10:49.643927Z","iopub.execute_input":"2023-05-29T08:10:49.644388Z","iopub.status.idle":"2023-05-29T08:10:49.731432Z","shell.execute_reply.started":"2023-05-29T08:10:49.644344Z","shell.execute_reply":"2023-05-29T08:10:49.730257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rotated = rotation_data.loc[rotation_data['rotation'] == 'rotated']\nlen(rotated)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:11:00.086422Z","iopub.execute_input":"2023-05-29T08:11:00.086937Z","iopub.status.idle":"2023-05-29T08:11:00.111571Z","shell.execute_reply.started":"2023-05-29T08:11:00.086885Z","shell.execute_reply":"2023-05-29T08:11:00.110089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.loc[~ df['img_file_name'].isin(rotated['img_file_name'])]\nlen(df)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:11:05.881762Z","iopub.execute_input":"2023-05-29T08:11:05.882214Z","iopub.status.idle":"2023-05-29T08:11:05.911995Z","shell.execute_reply.started":"2023-05-29T08:11:05.882169Z","shell.execute_reply":"2023-05-29T08:11:05.910864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"60406 - 26459","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:11:15.445918Z","iopub.execute_input":"2023-05-29T08:11:15.446343Z","iopub.status.idle":"2023-05-29T08:11:15.453883Z","shell.execute_reply.started":"2023-05-29T08:11:15.446304Z","shell.execute_reply":"2023-05-29T08:11:15.452526Z"},"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_X_Tick_Label.png')\nval_scheme","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:11:20.784197Z","iopub.execute_input":"2023-05-29T08:11:20.784621Z","iopub.status.idle":"2023-05-29T08:11:20.893313Z","shell.execute_reply.started":"2023-05-29T08:11:20.784584Z","shell.execute_reply":"2023-05-29T08:11:20.891899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_file_names = []\ntrain_file_names = []","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:12:42.406956Z","iopub.execute_input":"2023-05-29T08:12:42.407421Z","iopub.status.idle":"2023-05-29T08:12:42.412793Z","shell.execute_reply.started":"2023-05-29T08:12:42.407375Z","shell.execute_reply":"2023-05-29T08:12:42.411484Z"},"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-29T08:13:03.263997Z","iopub.execute_input":"2023-05-29T08:13:03.264451Z","iopub.status.idle":"2023-05-29T08:13:03.287066Z","shell.execute_reply.started":"2023-05-29T08:13:03.264408Z","shell.execute_reply":"2023-05-29T08:13:03.285624Z"},"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-29T08:13:07.595427Z","iopub.execute_input":"2023-05-29T08:13:07.595821Z","iopub.status.idle":"2023-05-29T08:13:07.612785Z","shell.execute_reply.started":"2023-05-29T08:13:07.595784Z","shell.execute_reply":"2023-05-29T08:13:07.611658Z"},"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-29T08:13:18.340027Z","iopub.execute_input":"2023-05-29T08:13:18.340478Z","iopub.status.idle":"2023-05-29T08:13:18.348659Z","shell.execute_reply.started":"2023-05-29T08:13:18.340436Z","shell.execute_reply":"2023-05-29T08:13:18.347476Z"},"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-29T08:13:21.322491Z","iopub.execute_input":"2023-05-29T08:13:21.322909Z","iopub.status.idle":"2023-05-29T08:13:21.331345Z","shell.execute_reply.started":"2023-05-29T08:13:21.322867Z","shell.execute_reply":"2023-05-29T08:13:21.330059Z"},"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-29T08:13:23.475180Z","iopub.execute_input":"2023-05-29T08:13:23.476058Z","iopub.status.idle":"2023-05-29T08:13:23.512035Z","shell.execute_reply.started":"2023-05-29T08:13:23.476007Z","shell.execute_reply":"2023-05-29T08:13:23.510889Z"},"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-29T08:13:28.712205Z","iopub.execute_input":"2023-05-29T08:13:28.712629Z","iopub.status.idle":"2023-05-29T08:13:28.733462Z","shell.execute_reply.started":"2023-05-29T08:13:28.712589Z","shell.execute_reply":"2023-05-29T08:13:28.731915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"line_generated_in_train = line_generated.iloc[:547]\nlen(line_generated_in_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:13:31.956419Z","iopub.execute_input":"2023-05-29T08:13:31.956837Z","iopub.status.idle":"2023-05-29T08:13:31.964707Z","shell.execute_reply.started":"2023-05-29T08:13:31.956792Z","shell.execute_reply":"2023-05-29T08:13:31.963217Z"},"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-29T08:13:34.542662Z","iopub.execute_input":"2023-05-29T08:13:34.543083Z","iopub.status.idle":"2023-05-29T08:13:34.551104Z","shell.execute_reply.started":"2023-05-29T08:13:34.543042Z","shell.execute_reply":"2023-05-29T08:13:34.549739Z"},"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-29T08:13:41.399666Z","iopub.execute_input":"2023-05-29T08:13:41.400074Z","iopub.status.idle":"2023-05-29T08:13:41.422222Z","shell.execute_reply.started":"2023-05-29T08:13:41.400031Z","shell.execute_reply":"2023-05-29T08:13:41.420760Z"},"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-29T08:13:46.189093Z","iopub.execute_input":"2023-05-29T08:13:46.189554Z","iopub.status.idle":"2023-05-29T08:13:46.205123Z","shell.execute_reply.started":"2023-05-29T08:13:46.189512Z","shell.execute_reply":"2023-05-29T08:13:46.203861Z"},"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-29T08:13:49.466233Z","iopub.execute_input":"2023-05-29T08:13:49.466655Z","iopub.status.idle":"2023-05-29T08:13:49.474858Z","shell.execute_reply.started":"2023-05-29T08:13:49.466616Z","shell.execute_reply":"2023-05-29T08:13:49.473554Z"},"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-29T08:13:51.753012Z","iopub.execute_input":"2023-05-29T08:13:51.753478Z","iopub.status.idle":"2023-05-29T08:13:51.762024Z","shell.execute_reply.started":"2023-05-29T08:13:51.753434Z","shell.execute_reply":"2023-05-29T08:13:51.760596Z"},"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-29T08:13:53.631190Z","iopub.execute_input":"2023-05-29T08:13:53.631615Z","iopub.status.idle":"2023-05-29T08:13:53.654196Z","shell.execute_reply.started":"2023-05-29T08:13:53.631575Z","shell.execute_reply":"2023-05-29T08:13:53.652704Z"},"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-29T08:14:00.258252Z","iopub.execute_input":"2023-05-29T08:14:00.258666Z","iopub.status.idle":"2023-05-29T08:14:00.278423Z","shell.execute_reply.started":"2023-05-29T08:14:00.258628Z","shell.execute_reply":"2023-05-29T08:14:00.277380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vertical_bar_generated_in_train = vertical_bar_generated.iloc[:491]\nlen(vertical_bar_generated_in_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:14:02.488458Z","iopub.execute_input":"2023-05-29T08:14:02.488911Z","iopub.status.idle":"2023-05-29T08:14:02.496810Z","shell.execute_reply.started":"2023-05-29T08:14:02.488869Z","shell.execute_reply":"2023-05-29T08:14:02.495500Z"},"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-29T08:14:04.551715Z","iopub.execute_input":"2023-05-29T08:14:04.552309Z","iopub.status.idle":"2023-05-29T08:14:04.560539Z","shell.execute_reply.started":"2023-05-29T08:14:04.552252Z","shell.execute_reply":"2023-05-29T08:14:04.559402Z"},"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-29T08:14:09.553191Z","iopub.execute_input":"2023-05-29T08:14:09.553611Z","iopub.status.idle":"2023-05-29T08:14:09.576495Z","shell.execute_reply.started":"2023-05-29T08:14:09.553571Z","shell.execute_reply":"2023-05-29T08:14:09.575017Z"},"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-29T08:14:23.061400Z","iopub.execute_input":"2023-05-29T08:14:23.061829Z","iopub.status.idle":"2023-05-29T08:14:23.078506Z","shell.execute_reply.started":"2023-05-29T08:14:23.061779Z","shell.execute_reply":"2023-05-29T08:14:23.077353Z"},"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-29T08:14:25.984382Z","iopub.execute_input":"2023-05-29T08:14:25.984789Z","iopub.status.idle":"2023-05-29T08:14:25.993072Z","shell.execute_reply.started":"2023-05-29T08:14:25.984748Z","shell.execute_reply":"2023-05-29T08:14:25.991722Z"},"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-29T08:14:27.829281Z","iopub.execute_input":"2023-05-29T08:14:27.829749Z","iopub.status.idle":"2023-05-29T08:14:27.837928Z","shell.execute_reply.started":"2023-05-29T08:14:27.829707Z","shell.execute_reply":"2023-05-29T08:14:27.836749Z"},"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-29T08:14:29.636633Z","iopub.execute_input":"2023-05-29T08:14:29.637062Z","iopub.status.idle":"2023-05-29T08:14:29.661400Z","shell.execute_reply.started":"2023-05-29T08:14:29.637020Z","shell.execute_reply":"2023-05-29T08:14:29.659914Z"},"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-29T08:14:34.227989Z","iopub.execute_input":"2023-05-29T08:14:34.228440Z","iopub.status.idle":"2023-05-29T08:14:34.247841Z","shell.execute_reply.started":"2023-05-29T08:14:34.228400Z","shell.execute_reply":"2023-05-29T08:14:34.246214Z"},"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-29T08:14:36.358196Z","iopub.execute_input":"2023-05-29T08:14:36.358605Z","iopub.status.idle":"2023-05-29T08:14:36.365379Z","shell.execute_reply.started":"2023-05-29T08:14:36.358566Z","shell.execute_reply":"2023-05-29T08:14:36.364261Z"},"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-29T08:14:38.165607Z","iopub.execute_input":"2023-05-29T08:14:38.166015Z","iopub.status.idle":"2023-05-29T08:14:38.173410Z","shell.execute_reply.started":"2023-05-29T08:14:38.165976Z","shell.execute_reply":"2023-05-29T08:14:38.172261Z"},"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-29T08:14:43.701381Z","iopub.execute_input":"2023-05-29T08:14:43.701831Z","iopub.status.idle":"2023-05-29T08:14:43.717930Z","shell.execute_reply.started":"2023-05-29T08:14:43.701788Z","shell.execute_reply":"2023-05-29T08:14:43.716481Z"},"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-29T08:14:46.144330Z","iopub.execute_input":"2023-05-29T08:14:46.144805Z","iopub.status.idle":"2023-05-29T08:14:46.164600Z","shell.execute_reply.started":"2023-05-29T08:14:46.144761Z","shell.execute_reply":"2023-05-29T08:14:46.163218Z"},"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-29T08:14:48.424896Z","iopub.execute_input":"2023-05-29T08:14:48.425345Z","iopub.status.idle":"2023-05-29T08:14:48.433281Z","shell.execute_reply.started":"2023-05-29T08:14:48.425300Z","shell.execute_reply":"2023-05-29T08:14:48.432046Z"},"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-29T08:14:50.324604Z","iopub.execute_input":"2023-05-29T08:14:50.325028Z","iopub.status.idle":"2023-05-29T08:14:50.333421Z","shell.execute_reply.started":"2023-05-29T08:14:50.324987Z","shell.execute_reply":"2023-05-29T08:14:50.332240Z"},"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-29T08:14:59.963886Z","iopub.execute_input":"2023-05-29T08:14:59.964318Z","iopub.status.idle":"2023-05-29T08:14:59.979376Z","shell.execute_reply.started":"2023-05-29T08:14:59.964276Z","shell.execute_reply":"2023-05-29T08:14:59.977889Z"},"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-29T08:15:01.530620Z","iopub.execute_input":"2023-05-29T08:15:01.531047Z","iopub.status.idle":"2023-05-29T08:15:01.539647Z","shell.execute_reply.started":"2023-05-29T08:15:01.531006Z","shell.execute_reply":"2023-05-29T08:15:01.538158Z"},"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-29T08:15:06.536235Z","iopub.execute_input":"2023-05-29T08:15:06.536656Z","iopub.status.idle":"2023-05-29T08:15:06.549900Z","shell.execute_reply.started":"2023-05-29T08:15:06.536616Z","shell.execute_reply":"2023-05-29T08:15:06.548329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"extras[:5]","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:15:08.468032Z","iopub.execute_input":"2023-05-29T08:15:08.468490Z","iopub.status.idle":"2023-05-29T08:15:08.475600Z","shell.execute_reply.started":"2023-05-29T08:15:08.468447Z","shell.execute_reply":"2023-05-29T08:15:08.474459Z"},"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-29T08:15:10.495921Z","iopub.execute_input":"2023-05-29T08:15:10.496350Z","iopub.status.idle":"2023-05-29T08:15:10.504030Z","shell.execute_reply.started":"2023-05-29T08:15:10.496307Z","shell.execute_reply":"2023-05-29T08:15:10.502899Z"},"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-29T08:15:15.710695Z","iopub.execute_input":"2023-05-29T08:15:15.711116Z","iopub.status.idle":"2023-05-29T08:15:15.719191Z","shell.execute_reply.started":"2023-05-29T08:15:15.711076Z","shell.execute_reply":"2023-05-29T08:15:15.717941Z"},"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-29T08:15:17.054997Z","iopub.execute_input":"2023-05-29T08:15:17.055437Z","iopub.status.idle":"2023-05-29T08:15:17.063566Z","shell.execute_reply.started":"2023-05-29T08:15:17.055393Z","shell.execute_reply":"2023-05-29T08:15:17.062317Z"},"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-29T08:15:19.947873Z","iopub.execute_input":"2023-05-29T08:15:19.948347Z","iopub.status.idle":"2023-05-29T08:15:19.953548Z","shell.execute_reply.started":"2023-05-29T08:15:19.948261Z","shell.execute_reply":"2023-05-29T08:15:19.952292Z"},"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-29T08:15:21.349761Z","iopub.execute_input":"2023-05-29T08:15:21.350191Z","iopub.status.idle":"2023-05-29T08:15:22.324517Z","shell.execute_reply.started":"2023-05-29T08:15:21.350127Z","shell.execute_reply":"2023-05-29T08:15:22.323334Z"},"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-29T08:15:25.708068Z","iopub.execute_input":"2023-05-29T08:15:25.708549Z","iopub.status.idle":"2023-05-29T08:16:01.176695Z","shell.execute_reply.started":"2023-05-29T08:15:25.708508Z","shell.execute_reply":"2023-05-29T08:16:01.175606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parser.class_map","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:16:03.181776Z","iopub.execute_input":"2023-05-29T08:16:03.182250Z","iopub.status.idle":"2023-05-29T08:16:03.189408Z","shell.execute_reply.started":"2023-05-29T08:16:03.182206Z","shell.execute_reply":"2023-05-29T08:16:03.188080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_records), len(val_records)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:16:05.671567Z","iopub.execute_input":"2023-05-29T08:16:05.671984Z","iopub.status.idle":"2023-05-29T08:16:05.679509Z","shell.execute_reply.started":"2023-05-29T08:16:05.671944Z","shell.execute_reply":"2023-05-29T08:16:05.678232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_records[0]","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:16:07.751130Z","iopub.execute_input":"2023-05-29T08:16:07.751590Z","iopub.status.idle":"2023-05-29T08:16:07.758679Z","shell.execute_reply.started":"2023-05-29T08:16:07.751549Z","shell.execute_reply":"2023-05-29T08:16:07.757545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_records[0]","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:16:10.330778Z","iopub.execute_input":"2023-05-29T08:16:10.331218Z","iopub.status.idle":"2023-05-29T08:16:10.339180Z","shell.execute_reply.started":"2023-05-29T08:16:10.331174Z","shell.execute_reply":"2023-05-29T08:16:10.338033Z"},"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-29T08:16:15.084228Z","iopub.execute_input":"2023-05-29T08:16:15.084648Z","iopub.status.idle":"2023-05-29T08:16:15.094689Z","shell.execute_reply.started":"2023-05-29T08:16:15.084610Z","shell.execute_reply":"2023-05-29T08:16:15.093353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"128 * 7","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:16:16.974109Z","iopub.execute_input":"2023-05-29T08:16:16.974693Z","iopub.status.idle":"2023-05-29T08:16:16.982420Z","shell.execute_reply.started":"2023-05-29T08:16:16.974645Z","shell.execute_reply":"2023-05-29T08:16:16.981246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Transforms:\nimage_size = 896\ntrain_tfms = tfms.A.Adapter([tfms.A.RGBShift(p=0.5), *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-29T08:16:24.445420Z","iopub.execute_input":"2023-05-29T08:16:24.445845Z","iopub.status.idle":"2023-05-29T08:16:24.455713Z","shell.execute_reply.started":"2023-05-29T08:16:24.445803Z","shell.execute_reply":"2023-05-29T08:16:24.454462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tfms.tfms_list","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:16:26.346318Z","iopub.execute_input":"2023-05-29T08:16:26.346736Z","iopub.status.idle":"2023-05-29T08:16:26.354402Z","shell.execute_reply.started":"2023-05-29T08:16:26.346699Z","shell.execute_reply":"2023-05-29T08:16:26.353099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_tfms.tfms_list","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:16:28.089793Z","iopub.execute_input":"2023-05-29T08:16:28.090719Z","iopub.status.idle":"2023-05-29T08:16:28.098877Z","shell.execute_reply.started":"2023-05-29T08:16:28.090660Z","shell.execute_reply":"2023-05-29T08:16:28.097575Z"},"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-29T08:16:29.948506Z","iopub.execute_input":"2023-05-29T08:16:29.948936Z","iopub.status.idle":"2023-05-29T08:16:29.957173Z","shell.execute_reply.started":"2023-05-29T08:16:29.948892Z","shell.execute_reply":"2023-05-29T08:16:29.955682Z"},"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-29T08:16:32.087289Z","iopub.execute_input":"2023-05-29T08:16:32.087710Z","iopub.status.idle":"2023-05-29T08:16:36.632593Z","shell.execute_reply.started":"2023-05-29T08:16:32.087671Z","shell.execute_reply":"2023-05-29T08:16:36.629284Z"},"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-29T08:16:45.704533Z","iopub.execute_input":"2023-05-29T08:16:45.704956Z","iopub.status.idle":"2023-05-29T08:16:49.328579Z","shell.execute_reply.started":"2023-05-29T08:16:45.704915Z","shell.execute_reply":"2023-05-29T08:16:49.327418Z"},"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-29T08:16:55.596410Z","iopub.execute_input":"2023-05-29T08:16:55.596850Z","iopub.status.idle":"2023-05-29T08:16:55.606027Z","shell.execute_reply.started":"2023-05-29T08:16:55.596810Z","shell.execute_reply":"2023-05-29T08:16:55.604775Z"},"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-29T08:16:58.348089Z","iopub.execute_input":"2023-05-29T08:16:58.348559Z","iopub.status.idle":"2023-05-29T08:17:01.658510Z","shell.execute_reply.started":"2023-05-29T08:16:58.348517Z","shell.execute_reply":"2023-05-29T08:17:01.657356Z"},"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-29T08:17:07.944015Z","iopub.execute_input":"2023-05-29T08:17:07.944449Z","iopub.status.idle":"2023-05-29T08:17:07.950514Z","shell.execute_reply.started":"2023-05-29T08:17:07.944406Z","shell.execute_reply":"2023-05-29T08:17:07.949081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_dl), len(val_dl)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:17:15.796719Z","iopub.execute_input":"2023-05-29T08:17:15.797133Z","iopub.status.idle":"2023-05-29T08:17:15.804892Z","shell.execute_reply.started":"2023-05-29T08:17:15.797091Z","shell.execute_reply":"2023-05-29T08:17:15.803417Z"},"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-29T08:17:17.208755Z","iopub.execute_input":"2023-05-29T08:17:17.209205Z","iopub.status.idle":"2023-05-29T08:17:25.682080Z","shell.execute_reply.started":"2023-05-29T08:17:17.209139Z","shell.execute_reply":"2023-05-29T08:17:25.680332Z"},"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-29T08:17:36.547118Z","iopub.execute_input":"2023-05-29T08:17:36.547666Z","iopub.status.idle":"2023-05-29T08:17:44.866439Z","shell.execute_reply.started":"2023-05-29T08:17:36.547618Z","shell.execute_reply":"2023-05-29T08:17:44.865176Z"},"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-29T08:17:56.950554Z","iopub.execute_input":"2023-05-29T08:17:56.950985Z","iopub.status.idle":"2023-05-29T08:17:56.962315Z","shell.execute_reply.started":"2023-05-29T08:17:56.950944Z","shell.execute_reply":"2023-05-29T08:17:56.959378Z"},"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-29T08:17:58.721329Z","iopub.execute_input":"2023-05-29T08:17:58.721797Z","iopub.status.idle":"2023-05-29T08:17:58.730299Z","shell.execute_reply.started":"2023-05-29T08:17:58.721742Z","shell.execute_reply":"2023-05-29T08:17:58.728541Z"},"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-29T08:18:02.244368Z","iopub.execute_input":"2023-05-29T08:18:02.244785Z","iopub.status.idle":"2023-05-29T08:18:02.250838Z","shell.execute_reply.started":"2023-05-29T08:18:02.244744Z","shell.execute_reply":"2023-05-29T08:18:02.249104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.callback.tracker import SaveModelCallback\n\ncb = SaveModelCallback(monitor='Accuracy', fname='x_tick_label_', every_epoch=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:18:07.657188Z","iopub.execute_input":"2023-05-29T08:18:07.657713Z","iopub.status.idle":"2023-05-29T08:18:07.667726Z","shell.execute_reply.started":"2023-05-29T08:18:07.657665Z","shell.execute_reply":"2023-05-29T08:18:07.666575Z"},"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-29T08:18:10.090342Z","iopub.execute_input":"2023-05-29T08:18:10.090771Z","iopub.status.idle":"2023-05-29T08:18:10.120217Z","shell.execute_reply.started":"2023-05-29T08:18:10.090730Z","shell.execute_reply":"2023-05-29T08:18:10.119017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T08:18:15.121697Z","iopub.execute_input":"2023-05-29T08:18:15.122133Z","iopub.status.idle":"2023-05-29T08:19:16.061561Z","shell.execute_reply.started":"2023-05-29T08:18:15.122093Z","shell.execute_reply":"2023-05-29T08:19:16.060170Z"},"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-29T08:20:06.117908Z","iopub.execute_input":"2023-05-29T08:20:06.118367Z","iopub.status.idle":"2023-05-29T10:10:48.450749Z","shell.execute_reply.started":"2023-05-29T08:20:06.118322Z","shell.execute_reply":"2023-05-29T10:10:48.449275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.load('x_tick_label__9')","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:12:25.297825Z","iopub.execute_input":"2023-05-29T10:12:25.298361Z","iopub.status.idle":"2023-05-29T10:12:25.544713Z","shell.execute_reply.started":"2023-05-29T10:12:25.298300Z","shell.execute_reply":"2023-05-29T10:12:25.543549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import icevision\n\nicevision.__version__","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:12:30.587972Z","iopub.execute_input":"2023-05-29T10:12:30.588439Z","iopub.status.idle":"2023-05-29T10:12:30.596297Z","shell.execute_reply.started":"2023-05-29T10:12:30.588395Z","shell.execute_reply":"2023-05-29T10:12:30.594798Z"},"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-x_tick_label.pth\", \n    meta={'icevision_version': '0.12.0'}\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:12:38.647892Z","iopub.execute_input":"2023-05-29T10:12:38.648916Z","iopub.status.idle":"2023-05-29T10:12:39.355017Z","shell.execute_reply.started":"2023-05-29T10:12:38.648854Z","shell.execute_reply":"2023-05-29T10:12:39.353853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_and_model = model_from_checkpoint('/kaggle/working/faster_rcnn-x_tick_label.pth')\ntype(checkpoint_and_model)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:12:44.016041Z","iopub.execute_input":"2023-05-29T10:12:44.016475Z","iopub.status.idle":"2023-05-29T10:12:44.657295Z","shell.execute_reply.started":"2023-05-29T10:12:44.016436Z","shell.execute_reply":"2023-05-29T10:12:44.656208Z"},"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-29T10:12:48.334006Z","iopub.execute_input":"2023-05-29T10:12:48.334438Z","iopub.status.idle":"2023-05-29T10:12:48.340708Z","shell.execute_reply.started":"2023-05-29T10:12:48.334399Z","shell.execute_reply":"2023-05-29T10:12:48.339280Z"},"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-29T10:12:53.944252Z","iopub.execute_input":"2023-05-29T10:12:53.944676Z","iopub.status.idle":"2023-05-29T10:15:29.131708Z","shell.execute_reply.started":"2023-05-29T10:12:53.944637Z","shell.execute_reply":"2023-05-29T10:15:29.130488Z"},"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-29T10:16:55.504294Z","iopub.execute_input":"2023-05-29T10:16:55.504763Z","iopub.status.idle":"2023-05-29T10:16:55.512994Z","shell.execute_reply.started":"2023-05-29T10:16:55.504722Z","shell.execute_reply":"2023-05-29T10:16:55.511660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics[1].plot()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:16:58.297931Z","iopub.execute_input":"2023-05-29T10:16:58.298359Z","iopub.status.idle":"2023-05-29T10:16:58.717411Z","shell.execute_reply.started":"2023-05-29T10:16:58.298316Z","shell.execute_reply":"2023-05-29T10:16:58.716212Z"},"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-29T10:17:20.219108Z","iopub.execute_input":"2023-05-29T10:17:20.219565Z","iopub.status.idle":"2023-05-29T10:17:20.226835Z","shell.execute_reply.started":"2023-05-29T10:17:20.219523Z","shell.execute_reply":"2023-05-29T10:17:20.225553Z"},"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-29T10:17:35.283874Z","iopub.execute_input":"2023-05-29T10:17:35.284318Z","iopub.status.idle":"2023-05-29T10:17:35.293511Z","shell.execute_reply.started":"2023-05-29T10:17:35.284275Z","shell.execute_reply":"2023-05-29T10:17:35.292127Z"},"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-29T10:17:42.849553Z","iopub.execute_input":"2023-05-29T10:17:42.849970Z","iopub.status.idle":"2023-05-29T10:17:47.733408Z","shell.execute_reply.started":"2023-05-29T10:17:42.849929Z","shell.execute_reply":"2023-05-29T10:17:47.732229Z"},"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-29T10:17:59.072348Z","iopub.execute_input":"2023-05-29T10:17:59.072771Z","iopub.status.idle":"2023-05-29T10:18:03.268776Z","shell.execute_reply.started":"2023-05-29T10:17:59.072730Z","shell.execute_reply":"2023-05-29T10:18:03.267441Z"},"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-29T10:18:11.867191Z","iopub.execute_input":"2023-05-29T10:18:11.867637Z","iopub.status.idle":"2023-05-29T10:18:16.534167Z","shell.execute_reply.started":"2023-05-29T10:18:11.867600Z","shell.execute_reply":"2023-05-29T10:18:16.532931Z"},"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-29T10:18:25.019043Z","iopub.execute_input":"2023-05-29T10:18:25.019476Z","iopub.status.idle":"2023-05-29T10:18:28.861191Z","shell.execute_reply.started":"2023-05-29T10:18:25.019436Z","shell.execute_reply":"2023-05-29T10:18:28.859847Z"},"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-29T10:18:39.360846Z","iopub.execute_input":"2023-05-29T10:18:39.361299Z","iopub.status.idle":"2023-05-29T10:18:43.615706Z","shell.execute_reply.started":"2023-05-29T10:18:39.361254Z","shell.execute_reply":"2023-05-29T10:18:43.614571Z"},"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-29T10:18:53.118404Z","iopub.execute_input":"2023-05-29T10:18:53.118884Z","iopub.status.idle":"2023-05-29T10:18:57.195386Z","shell.execute_reply.started":"2023-05-29T10:18:53.118831Z","shell.execute_reply":"2023-05-29T10:18:57.194239Z"},"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-29T10:19:06.759790Z","iopub.execute_input":"2023-05-29T10:19:06.760228Z","iopub.status.idle":"2023-05-29T10:19:10.932487Z","shell.execute_reply.started":"2023-05-29T10:19:06.760181Z","shell.execute_reply":"2023-05-29T10:19:10.931273Z"},"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-29T10:19:18.158134Z","iopub.execute_input":"2023-05-29T10:19:18.158580Z","iopub.status.idle":"2023-05-29T10:19:22.047254Z","shell.execute_reply.started":"2023-05-29T10:19:18.158539Z","shell.execute_reply":"2023-05-29T10:19:22.045937Z"},"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":"top_loss_images = [\n    'e832d98c37b2.jpg',\n    'a4dfb7b483fd.jpg',\n    '19cef86ee374.jpg',\n    '9566fffd6e0e.jpg',\n    '46ac53a6b82a.jpg',\n    '7e6e3c247c33.jpg',\n    'd1c4f30988ad.jpg', \n]","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:19:41.621090Z","iopub.execute_input":"2023-05-29T10:19:41.621544Z","iopub.status.idle":"2023-05-29T10:19:41.627214Z","shell.execute_reply.started":"2023-05-29T10:19:41.621503Z","shell.execute_reply":"2023-05-29T10:19:41.625666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/top_loss_images[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-29T10:19:44.412010Z","iopub.execute_input":"2023-05-29T10:19:44.412456Z","iopub.status.idle":"2023-05-29T10:19:49.017518Z","shell.execute_reply.started":"2023-05-29T10:19:44.412417Z","shell.execute_reply":"2023-05-29T10:19:49.016394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/top_loss_images[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-29T10:19:58.582541Z","iopub.execute_input":"2023-05-29T10:19:58.583038Z","iopub.status.idle":"2023-05-29T10:20:02.771884Z","shell.execute_reply.started":"2023-05-29T10:19:58.582989Z","shell.execute_reply":"2023-05-29T10:20:02.770618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/top_loss_images[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-29T10:20:30.176614Z","iopub.execute_input":"2023-05-29T10:20:30.177045Z","iopub.status.idle":"2023-05-29T10:20:34.409039Z","shell.execute_reply.started":"2023-05-29T10:20:30.177004Z","shell.execute_reply":"2023-05-29T10:20:34.407738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/top_loss_images[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-29T10:20:58.259805Z","iopub.execute_input":"2023-05-29T10:20:58.260263Z","iopub.status.idle":"2023-05-29T10:21:02.402462Z","shell.execute_reply.started":"2023-05-29T10:20:58.260217Z","shell.execute_reply":"2023-05-29T10:21:02.401242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/top_loss_images[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-29T10:21:11.740456Z","iopub.execute_input":"2023-05-29T10:21:11.740865Z","iopub.status.idle":"2023-05-29T10:21:16.013119Z","shell.execute_reply.started":"2023-05-29T10:21:11.740827Z","shell.execute_reply":"2023-05-29T10:21:16.011969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/top_loss_images[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-29T10:21:54.659483Z","iopub.execute_input":"2023-05-29T10:21:54.659977Z","iopub.status.idle":"2023-05-29T10:21:59.026852Z","shell.execute_reply.started":"2023-05-29T10:21:54.659931Z","shell.execute_reply":"2023-05-29T10:21:59.025592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(all_images_path/top_loss_images[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-29T10:22:14.114107Z","iopub.execute_input":"2023-05-29T10:22:14.114557Z","iopub.status.idle":"2023-05-29T10:22:18.225403Z","shell.execute_reply.started":"2023-05-29T10:22:14.114515Z","shell.execute_reply":"2023-05-29T10:22:18.224124Z"},"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-29T10:22:29.287260Z","iopub.execute_input":"2023-05-29T10:22:29.287689Z","iopub.status.idle":"2023-05-29T10:22:34.867857Z","shell.execute_reply.started":"2023-05-29T10:22:29.287649Z","shell.execute_reply":"2023-05-29T10:22:34.866241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}