{"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-06-02T07:29:27.362537Z","iopub.execute_input":"2023-06-02T07:29:27.363259Z","iopub.status.idle":"2023-06-02T07:29:28.330501Z","shell.execute_reply.started":"2023-06-02T07:29:27.363218Z","shell.execute_reply":"2023-06-02T07:29:28.329022Z"},"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-06-02T07:29:32.276618Z","iopub.execute_input":"2023-06-02T07:29:32.277324Z","iopub.status.idle":"2023-06-02T07:34:40.808751Z","shell.execute_reply.started":"2023-06-02T07:29:32.277281Z","shell.execute_reply":"2023-06-02T07:34:40.807479Z"},"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-06-02T07:34:57.711325Z","iopub.execute_input":"2023-06-02T07:34:57.711961Z","iopub.status.idle":"2023-06-02T07:34:57.724332Z","shell.execute_reply.started":"2023-06-02T07:34:57.711910Z","shell.execute_reply":"2023-06-02T07:34:57.723177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from icevision.all import *","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:35:15.046313Z","iopub.execute_input":"2023-06-02T07:35:15.046755Z","iopub.status.idle":"2023-06-02T07:36:40.625673Z","shell.execute_reply.started":"2023-06-02T07:35:15.046713Z","shell.execute_reply":"2023-06-02T07:36:40.624529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Let's begin!\")","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:36:44.514972Z","iopub.execute_input":"2023-06-02T07:36:44.515498Z","iopub.status.idle":"2023-06-02T07:36:44.522950Z","shell.execute_reply.started":"2023-06-02T07:36:44.515448Z","shell.execute_reply":"2023-06-02T07:36:44.521511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preparation","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\n\nextra_data_path = Path('/kaggle/input/b-m-g-a-extra-data')\n!ls {extra_data_path}","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:36:48.040639Z","iopub.execute_input":"2023-06-02T07:36:48.041142Z","iopub.status.idle":"2023-06-02T07:36:49.173171Z","shell.execute_reply.started":"2023-06-02T07:36:48.041092Z","shell.execute_reply":"2023-06-02T07:36:49.171826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_path = extra_data_path/'line_all_24932'\ni = 0\nfor file_path in sorted(images_path.glob(\"*.jpg\")):\n    i += 1\nprint(f\"{i} image files found.\")","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:37:03.359059Z","iopub.execute_input":"2023-06-02T07:37:03.359497Z","iopub.status.idle":"2023-06-02T07:37:04.069654Z","shell.execute_reply.started":"2023-06-02T07:37:03.359456Z","shell.execute_reply":"2023-06-02T07:37:04.067446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"VOC_XML_path = extra_data_path/'labels_line_all_24932_VOC_XML'\ni = 0\nfor file_path in sorted(VOC_XML_path.glob(\"*.xml\")):\n    i += 1\nprint(f\"{i} VOC XML files found.\")","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:37:10.242941Z","iopub.execute_input":"2023-06-02T07:37:10.243341Z","iopub.status.idle":"2023-06-02T07:37:11.308668Z","shell.execute_reply.started":"2023-06-02T07:37:10.243304Z","shell.execute_reply":"2023-06-02T07:37:11.307292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"competition_data_path = Path('/kaggle/input/benetech-making-graphs-accessible')\n!ls {competition_data_path}","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:37:14.792078Z","iopub.execute_input":"2023-06-02T07:37:14.792511Z","iopub.status.idle":"2023-06-02T07:37:15.760552Z","shell.execute_reply.started":"2023-06-02T07:37:14.792471Z","shell.execute_reply":"2023-06-02T07:37:15.759174Z"},"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-06-02T07:37:17.355150Z","iopub.execute_input":"2023-06-02T07:37:17.355678Z","iopub.status.idle":"2023-06-02T07:37:18.361665Z","shell.execute_reply.started":"2023-06-02T07:37:17.355631Z","shell.execute_reply":"2023-06-02T07:37:18.360274Z"},"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-06-02T07:37:20.366538Z","iopub.execute_input":"2023-06-02T07:37:20.366951Z","iopub.status.idle":"2023-06-02T07:37:20.373967Z","shell.execute_reply.started":"2023-06-02T07:37:20.366914Z","shell.execute_reply":"2023-06-02T07:37:20.372724Z"},"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-06-02T07:37:24.142265Z","iopub.execute_input":"2023-06-02T07:37:24.142719Z","iopub.status.idle":"2023-06-02T07:42:39.343096Z","shell.execute_reply.started":"2023-06-02T07:37:24.142678Z","shell.execute_reply":"2023-06-02T07:42:39.341898Z"},"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-06-02T07:44:20.930897Z","iopub.execute_input":"2023-06-02T07:44:20.931354Z","iopub.status.idle":"2023-06-02T07:44:20.988547Z","shell.execute_reply.started":"2023-06-02T07:44:20.931314Z","shell.execute_reply":"2023-06-02T07:44:20.987443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We'll keep only the rows pertaining to the `'line'` chart type:","metadata":{}},{"cell_type":"code","source":"line_df = df.loc[df['chart_type'] == 'line']\nlen(line_df)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:45:00.837848Z","iopub.execute_input":"2023-06-02T07:45:00.838375Z","iopub.status.idle":"2023-06-02T07:45:00.866031Z","shell.execute_reply.started":"2023-06-02T07:45:00.838319Z","shell.execute_reply":"2023-06-02T07:45:00.864043Z"},"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_line_18px.csv')\ncorrupt_files_df","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:45:05.591899Z","iopub.execute_input":"2023-06-02T07:45:05.592314Z","iopub.status.idle":"2023-06-02T07:45:05.611002Z","shell.execute_reply.started":"2023-06-02T07:45:05.592278Z","shell.execute_reply":"2023-06-02T07:45:05.609588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"line_df = line_df.loc[~ line_df['img_file_name'].isin(corrupt_files_df['corrupt_file'])]\nlen(line_df)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:45:08.106613Z","iopub.execute_input":"2023-06-02T07:45:08.107017Z","iopub.status.idle":"2023-06-02T07:45:08.128809Z","shell.execute_reply.started":"2023-06-02T07:45:08.106980Z","shell.execute_reply":"2023-06-02T07:45:08.127372Z"},"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(extra_data_path/'Validation_Scheme_Line.png')\nval_scheme","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:45:13.349466Z","iopub.execute_input":"2023-06-02T07:45:13.349983Z","iopub.status.idle":"2023-06-02T07:45:13.428254Z","shell.execute_reply.started":"2023-06-02T07:45:13.349936Z","shell.execute_reply":"2023-06-02T07:45:13.427149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"line_extracted = line_df.loc[line_df['source'] == 'extracted']\nlen(line_extracted)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:45:18.335124Z","iopub.execute_input":"2023-06-02T07:45:18.335675Z","iopub.status.idle":"2023-06-02T07:45:18.360559Z","shell.execute_reply.started":"2023-06-02T07:45:18.335621Z","shell.execute_reply":"2023-06-02T07:45:18.359380Z"},"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-06-02T07:45:20.724855Z","iopub.execute_input":"2023-06-02T07:45:20.725370Z","iopub.status.idle":"2023-06-02T07:45:20.752147Z","shell.execute_reply.started":"2023-06-02T07:45:20.725320Z","shell.execute_reply":"2023-06-02T07:45:20.751017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"line_generated = line_df.loc[line_df['source'] == 'generated']\nlen(line_generated)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:45:23.141731Z","iopub.execute_input":"2023-06-02T07:45:23.142243Z","iopub.status.idle":"2023-06-02T07:45:23.168935Z","shell.execute_reply.started":"2023-06-02T07:45:23.142195Z","shell.execute_reply":"2023-06-02T07:45:23.167822Z"},"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-06-02T07:45:27.741778Z","iopub.execute_input":"2023-06-02T07:45:27.742210Z","iopub.status.idle":"2023-06-02T07:45:27.764252Z","shell.execute_reply.started":"2023-06-02T07:45:27.742170Z","shell.execute_reply":"2023-06-02T07:45:27.762841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_file_names = []\ntrain_file_names = []","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:45:40.515787Z","iopub.execute_input":"2023-06-02T07:45:40.516194Z","iopub.status.idle":"2023-06-02T07:45:40.526514Z","shell.execute_reply.started":"2023-06-02T07:45:40.516156Z","shell.execute_reply":"2023-06-02T07:45:40.521922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"line_extracted_in_val = line_extracted.iloc[:150]\nline_extracted_in_train = line_extracted.iloc[150:]\nlen(line_extracted_in_val), len(line_extracted_in_train)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:45:45.897778Z","iopub.execute_input":"2023-06-02T07:45:45.898187Z","iopub.status.idle":"2023-06-02T07:45:45.905801Z","shell.execute_reply.started":"2023-06-02T07:45:45.898149Z","shell.execute_reply":"2023-06-02T07:45:45.904608Z"},"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-06-02T07:45:47.859266Z","iopub.execute_input":"2023-06-02T07:45:47.859801Z","iopub.status.idle":"2023-06-02T07:45:47.868609Z","shell.execute_reply.started":"2023-06-02T07:45:47.859750Z","shell.execute_reply":"2023-06-02T07:45:47.867373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"267 + 333","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:45:50.189484Z","iopub.execute_input":"2023-06-02T07:45:50.189899Z","iopub.status.idle":"2023-06-02T07:45:50.197392Z","shell.execute_reply.started":"2023-06-02T07:45:50.189862Z","shell.execute_reply":"2023-06-02T07:45:50.196147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"line_generated_in_train = line_generated.iloc[:333]\nlen(line_generated_in_train)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:45:52.415403Z","iopub.execute_input":"2023-06-02T07:45:52.415833Z","iopub.status.idle":"2023-06-02T07:45:52.423739Z","shell.execute_reply.started":"2023-06-02T07:45:52.415793Z","shell.execute_reply":"2023-06-02T07:45:52.422445Z"},"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-06-02T07:45:54.203536Z","iopub.execute_input":"2023-06-02T07:45:54.203937Z","iopub.status.idle":"2023-06-02T07:45:54.211532Z","shell.execute_reply.started":"2023-06-02T07:45:54.203901Z","shell.execute_reply":"2023-06-02T07:45:54.210315Z"},"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-06-02T07:46:09.260095Z","iopub.execute_input":"2023-06-02T07:46:09.260586Z","iopub.status.idle":"2023-06-02T07:46:09.269850Z","shell.execute_reply.started":"2023-06-02T07:46:09.260540Z","shell.execute_reply":"2023-06-02T07:46:09.268541Z"},"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-06-02T07:46:09.626901Z","iopub.execute_input":"2023-06-02T07:46:09.627302Z","iopub.status.idle":"2023-06-02T07:46:09.635373Z","shell.execute_reply.started":"2023-06-02T07:46:09.627264Z","shell.execute_reply":"2023-06-02T07:46:09.634199Z"},"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-06-02T07:46:16.935151Z","iopub.execute_input":"2023-06-02T07:46:16.935579Z","iopub.status.idle":"2023-06-02T07:46:16.940865Z","shell.execute_reply.started":"2023-06-02T07:46:16.935537Z","shell.execute_reply":"2023-06-02T07:46:16.939366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the parser:\nparser = parsers.VOCBBoxParser(images_dir=images_path, annotations_dir=VOC_XML_path)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:46:58.976753Z","iopub.execute_input":"2023-06-02T07:46:58.977198Z","iopub.status.idle":"2023-06-02T07:47:17.631560Z","shell.execute_reply.started":"2023-06-02T07:46:58.977158Z","shell.execute_reply":"2023-06-02T07:47:17.630248Z"},"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-06-02T07:48:08.941312Z","iopub.execute_input":"2023-06-02T07:48:08.941859Z","iopub.status.idle":"2023-06-02T07:50:47.089360Z","shell.execute_reply.started":"2023-06-02T07:48:08.941809Z","shell.execute_reply":"2023-06-02T07:50:47.088080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parser.class_map","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:50:51.815235Z","iopub.execute_input":"2023-06-02T07:50:51.815764Z","iopub.status.idle":"2023-06-02T07:50:51.825104Z","shell.execute_reply.started":"2023-06-02T07:50:51.815714Z","shell.execute_reply":"2023-06-02T07:50:51.823893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_records), len(val_records)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:52:33.433317Z","iopub.execute_input":"2023-06-02T07:52:33.433883Z","iopub.status.idle":"2023-06-02T07:52:33.442688Z","shell.execute_reply.started":"2023-06-02T07:52:33.433832Z","shell.execute_reply":"2023-06-02T07:52:33.441525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_records[0]","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:52:33.803778Z","iopub.execute_input":"2023-06-02T07:52:33.804258Z","iopub.status.idle":"2023-06-02T07:52:33.813408Z","shell.execute_reply.started":"2023-06-02T07:52:33.804211Z","shell.execute_reply":"2023-06-02T07:52:33.812190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_records[0]","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:52:35.956067Z","iopub.execute_input":"2023-06-02T07:52:35.956610Z","iopub.status.idle":"2023-06-02T07:52:35.966290Z","shell.execute_reply.started":"2023-06-02T07:52:35.956557Z","shell.execute_reply":"2023-06-02T07:52:35.965072Z"},"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-06-02T07:54:24.616684Z","iopub.execute_input":"2023-06-02T07:54:24.617147Z","iopub.status.idle":"2023-06-02T07:54:24.625515Z","shell.execute_reply.started":"2023-06-02T07:54:24.617105Z","shell.execute_reply":"2023-06-02T07:54:24.623875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"128 * 5","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:54:28.784840Z","iopub.execute_input":"2023-06-02T07:54:28.785315Z","iopub.status.idle":"2023-06-02T07:54:28.792390Z","shell.execute_reply.started":"2023-06-02T07:54:28.785272Z","shell.execute_reply":"2023-06-02T07:54:28.791182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Transforms:\nimage_size = 640\ntrain_tfms = tfms.A.Adapter([tfms.A.OneOf([tfms.A.MultiplicativeNoise(p=0.25), tfms.A.Blur(p=0.25), tfms.A.CLAHE(p=0.25), tfms.A.RGBShift(p=0.25)], p=0.8), *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-06-02T07:55:00.712572Z","iopub.execute_input":"2023-06-02T07:55:00.713003Z","iopub.status.idle":"2023-06-02T07:55:00.727013Z","shell.execute_reply.started":"2023-06-02T07:55:00.712966Z","shell.execute_reply":"2023-06-02T07:55:00.725652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tfms.tfms_list","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:55:03.177552Z","iopub.execute_input":"2023-06-02T07:55:03.178007Z","iopub.status.idle":"2023-06-02T07:55:03.187290Z","shell.execute_reply.started":"2023-06-02T07:55:03.177968Z","shell.execute_reply":"2023-06-02T07:55:03.185984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_tfms.tfms_list","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:55:05.531613Z","iopub.execute_input":"2023-06-02T07:55:05.532067Z","iopub.status.idle":"2023-06-02T07:55:05.539353Z","shell.execute_reply.started":"2023-06-02T07:55:05.532020Z","shell.execute_reply":"2023-06-02T07:55:05.538110Z"},"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-06-02T07:55:22.762359Z","iopub.execute_input":"2023-06-02T07:55:22.762821Z","iopub.status.idle":"2023-06-02T07:55:22.770749Z","shell.execute_reply.started":"2023-06-02T07:55:22.762781Z","shell.execute_reply":"2023-06-02T07:55:22.769526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show a few elements of `train_ds` with augmentations & 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-06-02T07:55:27.458236Z","iopub.execute_input":"2023-06-02T07:55:27.458765Z","iopub.status.idle":"2023-06-02T07:55:31.760815Z","shell.execute_reply.started":"2023-06-02T07:55:27.458714Z","shell.execute_reply":"2023-06-02T07:55:31.759316Z"},"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-06-02T07:56:10.953238Z","iopub.execute_input":"2023-06-02T07:56:10.953784Z","iopub.status.idle":"2023-06-02T07:56:14.677948Z","shell.execute_reply.started":"2023-06-02T07:56:10.953732Z","shell.execute_reply":"2023-06-02T07:56:14.676916Z"},"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-06-02T07:57:10.112860Z","iopub.execute_input":"2023-06-02T07:57:10.113396Z","iopub.status.idle":"2023-06-02T07:57:10.125650Z","shell.execute_reply.started":"2023-06-02T07:57:10.113349Z","shell.execute_reply":"2023-06-02T07:57:10.124336Z"},"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-06-02T07:57:16.661605Z","iopub.execute_input":"2023-06-02T07:57:16.662019Z","iopub.status.idle":"2023-06-02T07:57:18.582881Z","shell.execute_reply.started":"2023-06-02T07:57:16.661980Z","shell.execute_reply":"2023-06-02T07:57:18.581522Z"},"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-06-02T07:57:30.247460Z","iopub.execute_input":"2023-06-02T07:57:30.247976Z","iopub.status.idle":"2023-06-02T07:57:30.257980Z","shell.execute_reply.started":"2023-06-02T07:57:30.247927Z","shell.execute_reply":"2023-06-02T07:57:30.256489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_dl), len(val_dl)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:57:30.661672Z","iopub.execute_input":"2023-06-02T07:57:30.662180Z","iopub.status.idle":"2023-06-02T07:57:30.674202Z","shell.execute_reply.started":"2023-06-02T07:57:30.662124Z","shell.execute_reply":"2023-06-02T07:57:30.672733Z"},"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-06-02T07:57:35.111618Z","iopub.execute_input":"2023-06-02T07:57:35.112138Z","iopub.status.idle":"2023-06-02T07:57:47.137129Z","shell.execute_reply.started":"2023-06-02T07:57:35.112089Z","shell.execute_reply":"2023-06-02T07:57:47.131650Z"},"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-06-02T07:58:36.570074Z","iopub.execute_input":"2023-06-02T07:58:36.570636Z","iopub.status.idle":"2023-06-02T07:58:47.502770Z","shell.execute_reply.started":"2023-06-02T07:58:36.570581Z","shell.execute_reply":"2023-06-02T07:58:47.501556Z"},"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-06-02T07:59:06.832264Z","iopub.execute_input":"2023-06-02T07:59:06.833252Z","iopub.status.idle":"2023-06-02T07:59:06.847747Z","shell.execute_reply.started":"2023-06-02T07:59:06.833198Z","shell.execute_reply":"2023-06-02T07:59:06.846452Z"},"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-06-02T07:59:07.301139Z","iopub.execute_input":"2023-06-02T07:59:07.301692Z","iopub.status.idle":"2023-06-02T07:59:07.311380Z","shell.execute_reply.started":"2023-06-02T07:59:07.301638Z","shell.execute_reply":"2023-06-02T07:59:07.310163Z"},"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-06-02T07:59:18.441490Z","iopub.execute_input":"2023-06-02T07:59:18.441987Z","iopub.status.idle":"2023-06-02T07:59:18.452500Z","shell.execute_reply.started":"2023-06-02T07:59:18.441941Z","shell.execute_reply":"2023-06-02T07:59:18.451096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.callback.tracker import SaveModelCallback\n\ncb = SaveModelCallback(monitor='Accuracy', fname='line_', every_epoch=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:59:34.811371Z","iopub.execute_input":"2023-06-02T07:59:34.811907Z","iopub.status.idle":"2023-06-02T07:59:34.821430Z","shell.execute_reply.started":"2023-06-02T07:59:34.811857Z","shell.execute_reply":"2023-06-02T07:59:34.820200Z"},"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-06-02T07:59:48.976409Z","iopub.execute_input":"2023-06-02T07:59:48.976936Z","iopub.status.idle":"2023-06-02T07:59:49.014390Z","shell.execute_reply.started":"2023-06-02T07:59:48.976890Z","shell.execute_reply":"2023-06-02T07:59:49.013263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-06-02T07:59:55.231676Z","iopub.execute_input":"2023-06-02T07:59:55.232162Z","iopub.status.idle":"2023-06-02T08:00:40.241571Z","shell.execute_reply.started":"2023-06-02T07:59:55.232114Z","shell.execute_reply":"2023-06-02T08:00:40.240090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(10, 2e-3, freeze_epochs=1)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T08:02:48.711790Z","iopub.execute_input":"2023-06-02T08:02:48.712261Z","iopub.status.idle":"2023-06-02T08:18:21.466837Z","shell.execute_reply.started":"2023-06-02T08:02:48.712220Z","shell.execute_reply":"2023-06-02T08:18:21.465451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.load('line__5')","metadata":{"execution":{"iopub.status.busy":"2023-06-02T08:23:24.570555Z","iopub.execute_input":"2023-06-02T08:23:24.571031Z","iopub.status.idle":"2023-06-02T08:23:24.760763Z","shell.execute_reply.started":"2023-06-02T08:23:24.570989Z","shell.execute_reply":"2023-06-02T08:23:24.759483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import icevision\n\nicevision.__version__","metadata":{"execution":{"iopub.status.busy":"2023-06-02T08:23:29.113472Z","iopub.execute_input":"2023-06-02T08:23:29.114001Z","iopub.status.idle":"2023-06-02T08:23:29.122595Z","shell.execute_reply.started":"2023-06-02T08:23:29.113954Z","shell.execute_reply":"2023-06-02T08:23:29.121068Z"},"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-line.pth\", \n    meta={'icevision_version': '0.12.0'}\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T08:23:37.100221Z","iopub.execute_input":"2023-06-02T08:23:37.100687Z","iopub.status.idle":"2023-06-02T08:23:37.674960Z","shell.execute_reply.started":"2023-06-02T08:23:37.100642Z","shell.execute_reply":"2023-06-02T08:23:37.673522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_and_model = model_from_checkpoint('/kaggle/working/faster_rcnn-line.pth')\ntype(checkpoint_and_model)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T08:23:40.868329Z","iopub.execute_input":"2023-06-02T08:23:40.868774Z","iopub.status.idle":"2023-06-02T08:23:41.754032Z","shell.execute_reply.started":"2023-06-02T08:23:40.868737Z","shell.execute_reply":"2023-06-02T08:23:41.752880Z"},"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-06-02T08:23:44.192976Z","iopub.execute_input":"2023-06-02T08:23:44.193496Z","iopub.status.idle":"2023-06-02T08:23:44.200376Z","shell.execute_reply.started":"2023-06-02T08:23:44.193428Z","shell.execute_reply":"2023-06-02T08:23:44.199153Z"},"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-06-02T08:23:48.365606Z","iopub.execute_input":"2023-06-02T08:23:48.366106Z","iopub.status.idle":"2023-06-02T08:24:06.187303Z","shell.execute_reply.started":"2023-06-02T08:23:48.366056Z","shell.execute_reply":"2023-06-02T08:24:06.186058Z"},"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-06-02T08:24:12.030524Z","iopub.execute_input":"2023-06-02T08:24:12.030930Z","iopub.status.idle":"2023-06-02T08:24:12.038626Z","shell.execute_reply.started":"2023-06-02T08:24:12.030893Z","shell.execute_reply":"2023-06-02T08:24:12.037195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics[1].plot()","metadata":{"execution":{"iopub.status.busy":"2023-06-02T08:24:13.912309Z","iopub.execute_input":"2023-06-02T08:24:13.912749Z","iopub.status.idle":"2023-06-02T08:24:14.316613Z","shell.execute_reply.started":"2023-06-02T08:24:13.912709Z","shell.execute_reply":"2023-06-02T08:24:14.315463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"infer_dl = model_type.infer_dl(val_ds, batch_size=8, shuffle=False)\npreds = model_type.predict_from_dl(model, infer_dl, keep_images=True, detection_threshold=0.5)\ntype(preds)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T08:24:26.275159Z","iopub.execute_input":"2023-06-02T08:24:26.275688Z","iopub.status.idle":"2023-06-02T08:24:39.219616Z","shell.execute_reply.started":"2023-06-02T08:24:26.275636Z","shell.execute_reply":"2023-06-02T08:24:39.218386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(preds)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T08:24:42.835911Z","iopub.execute_input":"2023-06-02T08:24:42.836471Z","iopub.status.idle":"2023-06-02T08:24:42.845229Z","shell.execute_reply.started":"2023-06-02T08:24:42.836386Z","shell.execute_reply":"2023-06-02T08:24:42.843951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_preds(preds=preds[:8], display_label=True, figsize=(64, 64))","metadata":{"execution":{"iopub.status.busy":"2023-06-02T08:24:47.600629Z","iopub.execute_input":"2023-06-02T08:24:47.601143Z","iopub.status.idle":"2023-06-02T08:25:00.334875Z","shell.execute_reply.started":"2023-06-02T08:24:47.601095Z","shell.execute_reply":"2023-06-02T08:25:00.323537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I've run the `model_type.interp.plot_top_losses()` method 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    'c030b3e1913d.jpg', \n    '11376f3d0424.jpg', \n    '95812fbd6f6d.jpg', \n    'fcb7801cf77e.jpg', \n    'feaf96b0be5b.jpg', \n    '770ef89c8d56.jpg', \n    '0b533907be09.jpg', \n    '502e1f43e762.jpg', \n    'ef327fad7d34.jpg',\n    'b5fec685e2e8.jpg'\n]","metadata":{"execution":{"iopub.status.busy":"2023-06-02T08:27:44.835761Z","iopub.execute_input":"2023-06-02T08:27:44.836224Z","iopub.status.idle":"2023-06-02T08:27:44.841865Z","shell.execute_reply.started":"2023-06-02T08:27:44.836183Z","shell.execute_reply":"2023-06-02T08:27:44.840548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(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-06-02T08:27:50.677469Z","iopub.execute_input":"2023-06-02T08:27:50.677969Z","iopub.status.idle":"2023-06-02T08:27:53.837772Z","shell.execute_reply.started":"2023-06-02T08:27:50.677922Z","shell.execute_reply":"2023-06-02T08:27:53.836486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(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-06-02T08:28:13.077676Z","iopub.execute_input":"2023-06-02T08:28:13.078194Z","iopub.status.idle":"2023-06-02T08:28:15.201252Z","shell.execute_reply.started":"2023-06-02T08:28:13.078138Z","shell.execute_reply":"2023-06-02T08:28:15.200001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(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-06-02T08:28:32.539441Z","iopub.execute_input":"2023-06-02T08:28:32.539951Z","iopub.status.idle":"2023-06-02T08:28:34.709688Z","shell.execute_reply.started":"2023-06-02T08:28:32.539912Z","shell.execute_reply":"2023-06-02T08:28:34.708449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(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-06-02T08:28:57.215317Z","iopub.execute_input":"2023-06-02T08:28:57.215763Z","iopub.status.idle":"2023-06-02T08:28:59.346365Z","shell.execute_reply.started":"2023-06-02T08:28:57.215724Z","shell.execute_reply":"2023-06-02T08:28:59.345101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(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-06-02T08:29:12.188650Z","iopub.execute_input":"2023-06-02T08:29:12.189191Z","iopub.status.idle":"2023-06-02T08:29:14.526022Z","shell.execute_reply.started":"2023-06-02T08:29:12.189139Z","shell.execute_reply":"2023-06-02T08:29:14.524753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(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-06-02T08:29:31.274366Z","iopub.execute_input":"2023-06-02T08:29:31.274828Z","iopub.status.idle":"2023-06-02T08:29:33.337465Z","shell.execute_reply.started":"2023-06-02T08:29:31.274787Z","shell.execute_reply":"2023-06-02T08:29:33.336302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(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-06-02T08:29:49.623853Z","iopub.execute_input":"2023-06-02T08:29:49.624260Z","iopub.status.idle":"2023-06-02T08:29:51.832692Z","shell.execute_reply.started":"2023-06-02T08:29:49.624222Z","shell.execute_reply":"2023-06-02T08:29:51.831531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(images_path/top_loss_images[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-06-02T08:30:11.366527Z","iopub.execute_input":"2023-06-02T08:30:11.366928Z","iopub.status.idle":"2023-06-02T08:30:13.911329Z","shell.execute_reply.started":"2023-06-02T08:30:11.366889Z","shell.execute_reply":"2023-06-02T08:30:13.910162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(images_path/top_loss_images[8])\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-06-02T08:30:29.704044Z","iopub.execute_input":"2023-06-02T08:30:29.704484Z","iopub.status.idle":"2023-06-02T08:30:31.964676Z","shell.execute_reply.started":"2023-06-02T08:30:29.704440Z","shell.execute_reply":"2023-06-02T08:30:31.963540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(images_path/top_loss_images[9])\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-06-02T08:30:52.853609Z","iopub.execute_input":"2023-06-02T08:30:52.854030Z","iopub.status.idle":"2023-06-02T08:30:54.925949Z","shell.execute_reply.started":"2023-06-02T08:30:52.853993Z","shell.execute_reply":"2023-06-02T08:30:54.924687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Clean-up:\n!rm /kaggle/working/icevision_install.sh","metadata":{"execution":{"iopub.status.busy":"2023-06-02T08:31:05.835514Z","iopub.execute_input":"2023-06-02T08:31:05.835931Z","iopub.status.idle":"2023-06-02T08:31:06.896179Z","shell.execute_reply.started":"2023-06-02T08:31:05.835895Z","shell.execute_reply":"2023-06-02T08:31:06.894603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}