{"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":"# Recreate the Backbone","metadata":{}},{"cell_type":"code","source":"import torch\nimport torch.nn as nn","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:43:07.263203Z","iopub.execute_input":"2023-05-22T11:43:07.264001Z","iopub.status.idle":"2023-05-22T11:43:09.868514Z","shell.execute_reply.started":"2023-05-22T11:43:07.263953Z","shell.execute_reply":"2023-05-22T11:43:09.867425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/utils/torch_utils.py\ndef check_all_model_params_in_groups2(model, param_groups):\n    num_params = len([param for group in param_groups for param in group])\n    num_params_expected = len(list(model.parameters()))\n\n    if num_params != num_params_expected:\n        raise RuntimeError(f\"{num_params_expected} params in model, but only {num_params} in parameter group.\")","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:43:20.645290Z","iopub.execute_input":"2023-05-22T11:43:20.646788Z","iopub.status.idle":"2023-05-22T11:43:20.654218Z","shell.execute_reply.started":"2023-05-22T11:43:20.646737Z","shell.execute_reply":"2023-05-22T11:43:20.653134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/models/torchvision/faster_rcnn/backbones/resnet_fpn_utils.py\ndef param_groups(model):\n    body = model.body\n\n    layers = []\n    layers += [nn.Sequential(body.conv1, body.bn1)]\n    layers += [getattr(body, l) for l in list(body) if l.startswith(\"layer\")]\n    layers += [model.fpn]\n\n    _param_groups = [list(layer.parameters()) for layer in layers]\n    check_all_model_params_in_groups2(model, _param_groups)\n\n    return _param_groups","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:43:33.648314Z","iopub.execute_input":"2023-05-22T11:43:33.649412Z","iopub.status.idle":"2023-05-22T11:43:33.656987Z","shell.execute_reply.started":"2023-05-22T11:43:33.649340Z","shell.execute_reply":"2023-05-22T11:43:33.655841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/models/torchvision/faster_rcnn/backbones/resnet_fpn_utils.py\nfrom types import MethodType\n\ndef patch_param_groups(model):\n    model.param_groups = MethodType(param_groups, model)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:43:43.265155Z","iopub.execute_input":"2023-05-22T11:43:43.265670Z","iopub.status.idle":"2023-05-22T11:43:43.271114Z","shell.execute_reply.started":"2023-05-22T11:43:43.265629Z","shell.execute_reply":"2023-05-22T11:43:43.269842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/models/torchvision/faster_rcnn/backbones/resnet_fpn_configs.py\nfrom torchvision.models.detection.backbone_utils import resnet_fpn_backbone\n\ndef _resnet_fpn(name, pretrained=True, **kwargs):\n    model = resnet_fpn_backbone(backbone_name=name, pretrained=pretrained, **kwargs)\n    patch_param_groups(model)\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:43:51.462344Z","iopub.execute_input":"2023-05-22T11:43:51.463139Z","iopub.status.idle":"2023-05-22T11:43:51.759590Z","shell.execute_reply.started":"2023-05-22T11:43:51.463100Z","shell.execute_reply":"2023-05-22T11:43:51.758479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/models/torchvision/faster_rcnn/backbones/resnet_fpn_configs.py\ndef resnet50_fpn_fn(pretrained=True, **kwargs):\n    return _resnet_fpn(\"resnet50\", pretrained=pretrained, **kwargs)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:43:59.681059Z","iopub.execute_input":"2023-05-22T11:43:59.681492Z","iopub.status.idle":"2023-05-22T11:43:59.687589Z","shell.execute_reply.started":"2023-05-22T11:43:59.681452Z","shell.execute_reply":"2023-05-22T11:43:59.686262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"horizontal_bar_backbone = resnet50_fpn_fn(pretrained=False)\nhorizontal_bar_backbone","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:44:07.561762Z","iopub.execute_input":"2023-05-22T11:44:07.562374Z","iopub.status.idle":"2023-05-22T11:44:08.172432Z","shell.execute_reply.started":"2023-05-22T11:44:07.562332Z","shell.execute_reply":"2023-05-22T11:44:08.171321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Recreate the Model","metadata":{}},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/models/torchvision/utils.py\ndef patch_param_groups2(model, head_layers, backbone_param_groups):\n    def param_groups2(model):\n        head_param_groups = [list(layer.parameters()) for layer in head_layers]\n\n        _param_groups = backbone_param_groups + head_param_groups\n        check_all_model_params_in_groups2(model, _param_groups)\n\n        return _param_groups\n\n    model.param_groups = MethodType(param_groups2, model)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:44:35.631360Z","iopub.execute_input":"2023-05-22T11:44:35.632075Z","iopub.status.idle":"2023-05-22T11:44:35.639486Z","shell.execute_reply.started":"2023-05-22T11:44:35.632036Z","shell.execute_reply":"2023-05-22T11:44:35.637491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/models/torchvision/utils.py\ndef patch_rcnn_param_groups(model):\n    return patch_param_groups2(model=model, head_layers=[model.rpn, model.roi_heads], backbone_param_groups=model.backbone.param_groups())","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:46:12.173840Z","iopub.execute_input":"2023-05-22T11:46:12.174675Z","iopub.status.idle":"2023-05-22T11:46:12.180481Z","shell.execute_reply.started":"2023-05-22T11:46:12.174633Z","shell.execute_reply":"2023-05-22T11:46:12.179267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/models/torchvision/utils.py\ndef _noop_normalize(image):\n    return image\n\ndef _noop_resize(image, target):\n    return image, target\n\ndef remove_internal_model_transforms(model):\n    model.transform.normalize = _noop_normalize\n    model.transform.resize = _noop_resize","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:46:19.363851Z","iopub.execute_input":"2023-05-22T11:46:19.364465Z","iopub.status.idle":"2023-05-22T11:46:19.370992Z","shell.execute_reply.started":"2023-05-22T11:46:19.364419Z","shell.execute_reply":"2023-05-22T11:46:19.369859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/models/torchvision/faster_rcnn/model.py\nfrom torchvision.models.detection.faster_rcnn import FasterRCNN\n\ndef get_model(num_classes, backbone, remove_internal_transforms=True, **faster_rcnn_kwargs):\n    model = FasterRCNN(backbone, num_classes=num_classes, **faster_rcnn_kwargs)\n\n    patch_rcnn_param_groups(model=model)\n\n    if remove_internal_transforms:\n        remove_internal_model_transforms(model)\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:46:36.216979Z","iopub.execute_input":"2023-05-22T11:46:36.217945Z","iopub.status.idle":"2023-05-22T11:46:36.225006Z","shell.execute_reply.started":"2023-05-22T11:46:36.217889Z","shell.execute_reply":"2023-05-22T11:46:36.223744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"horizontal_bar_model = get_model(num_classes=2, backbone=horizontal_bar_backbone)\nhorizontal_bar_model","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:46:43.912251Z","iopub.execute_input":"2023-05-22T11:46:43.912960Z","iopub.status.idle":"2023-05-22T11:46:44.128063Z","shell.execute_reply.started":"2023-05-22T11:46:43.912920Z","shell.execute_reply":"2023-05-22T11:46:44.126861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"horizontal_bar_model.load_state_dict(torch.load('/kaggle/input/b-m-g-a-extra-data/horizontal_bar_23.pth'))","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:47:14.790382Z","iopub.execute_input":"2023-05-22T11:47:14.791101Z","iopub.status.idle":"2023-05-22T11:47:20.202820Z","shell.execute_reply.started":"2023-05-22T11:47:14.791061Z","shell.execute_reply":"2023-05-22T11:47:20.200264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\ndevice","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:47:28.270763Z","iopub.execute_input":"2023-05-22T11:47:28.271248Z","iopub.status.idle":"2023-05-22T11:47:28.284245Z","shell.execute_reply.started":"2023-05-22T11:47:28.271201Z","shell.execute_reply":"2023-05-22T11:47:28.282815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"horizontal_bar_model = horizontal_bar_model.to(device)\nhorizontal_bar_model.eval();","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:47:34.956212Z","iopub.execute_input":"2023-05-22T11:47:34.957155Z","iopub.status.idle":"2023-05-22T11:47:35.024120Z","shell.execute_reply.started":"2023-05-22T11:47:34.957098Z","shell.execute_reply":"2023-05-22T11:47:35.023091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Postprocessing Functions","metadata":{}},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/utils/imageio.py\nfrom collections import namedtuple\n\nImgSize = namedtuple(\"ImgSize\", \"width, height\")","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:47:47.914691Z","iopub.execute_input":"2023-05-22T11:47:47.915676Z","iopub.status.idle":"2023-05-22T11:47:47.921350Z","shell.execute_reply.started":"2023-05-22T11:47:47.915621Z","shell.execute_reply":"2023-05-22T11:47:47.920000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/utils/imageio.py\nimport PIL\nimport numpy as np\n\ndef get_img_size_from_data(data):\n    \"\"\"\n    Returns image size.\n    If the data is a NumPy array, it is expected to be in the format (h, w) or (h, w, c).\n    \"\"\"\n    assert isinstance(data, (PIL.Image.Image, np.ndarray))\n\n    if isinstance(data, PIL.Image.Image):\n        width, height = data.size\n    else:\n        height, width, *_ = data.shape\n\n    return ImgSize(width=width, height=height)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:47:59.218620Z","iopub.execute_input":"2023-05-22T11:47:59.219308Z","iopub.status.idle":"2023-05-22T11:47:59.226708Z","shell.execute_reply.started":"2023-05-22T11:47:59.219267Z","shell.execute_reply":"2023-05-22T11:47:59.225467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import albumentations as A\n\nhorizontal_bar_tfms_list = [\n    A.LongestMaxSize(always_apply=False, p=1, max_size=896, interpolation=1),\n    A.PadIfNeeded(always_apply=False, p=1.0, min_height=896, min_width=896, pad_height_divisor=None, pad_width_divisor=None, border_mode=0, value=[124, 116, 104], mask_value=None),\n    A.Normalize(always_apply=False, p=1.0, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0)\n]","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:48:15.674906Z","iopub.execute_input":"2023-05-22T11:48:15.675552Z","iopub.status.idle":"2023-05-22T11:48:16.988829Z","shell.execute_reply.started":"2023-05-22T11:48:15.675509Z","shell.execute_reply":"2023-05-22T11:48:16.987788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/tfms/albumentations/albumentations_helpers.py\ndef get_transform(tfms_list, t):\n    \"\"\"\n    Extract transform `t` from `tfms_list`.\n    \n    Parameters\n    ----------\n    tfms_list: list of albumentations transforms.\n    t: name (str) of the transform to look for and return from within `tfms_list`.\n    \n    Returns\n    -------\n    The `t` transform if found inside `tfms_list`, otherwise `None`.\n    \"\"\"\n    for el in tfms_list:\n        if t in str(type(el)):\n            return el\n    return None","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:48:28.087794Z","iopub.execute_input":"2023-05-22T11:48:28.088187Z","iopub.status.idle":"2023-05-22T11:48:28.094504Z","shell.execute_reply.started":"2023-05-22T11:48:28.088150Z","shell.execute_reply":"2023-05-22T11:48:28.093357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/tfms/albumentations/albumentations_helpers.py\ndef py3round(number):\n    \"\"\"\n    Unified rounding in all Python versions. Used by Albumentations.\n    \n    Parameters\n    ----------\n    number: float to round.\n    \n    Returns\n    -------\n    Rounded number.\n    \"\"\"\n    if abs(round(number) - number) == 0.5:\n        return int(2.0 * round(number / 2.0))\n\n    return int(round(number))","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:48:38.678334Z","iopub.execute_input":"2023-05-22T11:48:38.678823Z","iopub.status.idle":"2023-05-22T11:48:38.686443Z","shell.execute_reply.started":"2023-05-22T11:48:38.678777Z","shell.execute_reply":"2023-05-22T11:48:38.685098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/tfms/albumentations/albumentations_helpers.py\ndef func_max_size(height, width, max_size, func):\n    \"\"\"\n    Calculate rescaled height and width of the image in question w.r.t. to a specific size.\n    \n    Parameters\n    ----------\n    height: height of the image in question.\n    width: width of the image in question.\n    max_size: size w.r.t. the image needs to be rescaled (resized).\n    func: min/max. Whether to compare max_size to the smallest/longest of the image dims.\n    \n    Returns\n    -------\n    Rescaled height and width.\n    \"\"\"\n    scale = max_size / float(func(width, height))\n\n    if scale != 1.0:\n        height, width = tuple(py3round(dim * scale) for dim in (height, width))\n    return height, width","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:48:50.305807Z","iopub.execute_input":"2023-05-22T11:48:50.306756Z","iopub.status.idle":"2023-05-22T11:48:50.314258Z","shell.execute_reply.started":"2023-05-22T11:48:50.306700Z","shell.execute_reply":"2023-05-22T11:48:50.313107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/tfms/albumentations/albumentations_helpers.py\ndef get_size_without_padding(tfms_list, before_tfm_img_size, height, width):\n    \"\"\"\n    Infer the height and width of the pre-processed image after removing padding.\n    \n    Parameters\n    ----------\n    tfms_list: list of albumentations transforms applied to the `before_tfm_img` image before passing it to the model for inference.\n    before_tfm_img_size: original image size before being pre-processed for inference.\n    height: height of output image from icevision `predict` function.\n    width: width of output image from icevision `predict` function.\n    \n    Returns\n    -------\n    height and width of the image coming out of the inference pipeline, after removing padding.\n    \"\"\"\n    if get_transform(tfms_list, \"Pad\") is not None:\n\n        t = get_transform(tfms_list, \"SmallestMaxSize\")\n        if t is not None:\n            presize = t.max_size\n            height, width = func_max_size(before_tfm_img_size.height, before_tfm_img_size.width, presize, min)\n\n        t = get_transform(tfms_list, \"LongestMaxSize\")\n        if t is not None:\n            size = t.max_size\n            height, width = func_max_size(before_tfm_img_size.height, before_tfm_img_size.width, size, max)\n\n    return height, width","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:48:59.418260Z","iopub.execute_input":"2023-05-22T11:48:59.418866Z","iopub.status.idle":"2023-05-22T11:48:59.427070Z","shell.execute_reply.started":"2023-05-22T11:48:59.418826Z","shell.execute_reply":"2023-05-22T11:48:59.425961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BBox = namedtuple(\"BBox\", \"xmin, ymin, xmax, ymax\")","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:49:03.228381Z","iopub.execute_input":"2023-05-22T11:49:03.229093Z","iopub.status.idle":"2023-05-22T11:49:03.233972Z","shell.execute_reply.started":"2023-05-22T11:49:03.229054Z","shell.execute_reply":"2023-05-22T11:49:03.232768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Original source: https://github.com/airctic/icevision/blob/master/icevision/models/inference.py\ndef postprocess_bbox(img_size, bbox, transforms, h_after, w_after):\n    \"\"\"\n    Post-process predicted bbox to adjust coordinates to input image size.\n    \n    Parameters\n    ----------\n    img_size: original image size, before any model-pre-processing done.\n    bbox: predicted bbox.\n    transforms: list of model-pre-processing transforms.\n    h_after: height of image after model-pre-processing transforms.\n    w_after: width of image after model-pre-processing transforms.\n    \n    Returns\n    -------\n    Tuple with (xmin, ymin, xmax, ymax) rescaled and re-adjusted to match the original image size.\n    \"\"\"\n    h_after, w_after = get_size_without_padding(transforms, img_size, h_after, w_after)\n    pad = np.abs(h_after - w_after) // 2\n\n    h_scale, w_scale = h_after / img_size.height, w_after / img_size.width\n    if h_after < w_after:\n        xmin, xmax, ymin, ymax = (\n            int(bbox.xmin),\n            int(bbox.xmax),\n            int(bbox.ymin) - pad,\n            int(bbox.ymax) - pad,\n        )\n    else:\n        xmin, xmax, ymin, ymax = (\n            int(bbox.xmin) - pad,\n            int(bbox.xmax) - pad,\n            int(bbox.ymin),\n            int(bbox.ymax),\n        )\n\n    xmin, xmax, ymin, ymax = (\n        max(xmin, 0),\n        min(xmax, w_after),\n        max(ymin, 0),\n        min(ymax, h_after),\n    )\n    xmin, xmax, ymin, ymax = (\n        int(xmin / w_scale),\n        int(xmax / w_scale),\n        int(ymin / h_scale),\n        int(ymax / h_scale),\n    )\n\n    return xmin, ymin, xmax, ymax","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:49:15.813466Z","iopub.execute_input":"2023-05-22T11:49:15.813851Z","iopub.status.idle":"2023-05-22T11:49:15.826332Z","shell.execute_reply.started":"2023-05-22T11:49:15.813815Z","shell.execute_reply":"2023-05-22T11:49:15.824448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating a DataFrame","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\n\npath = Path('/kaggle/input/benetech-making-graphs-accessible')\n!ls {path}","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:49:25.819145Z","iopub.execute_input":"2023-05-22T11:49:25.819672Z","iopub.status.idle":"2023-05-22T11:49:27.021549Z","shell.execute_reply.started":"2023-05-22T11:49:25.819623Z","shell.execute_reply":"2023-05-22T11:49:27.020256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = path/'train'\n!ls {train_path}","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:49:27.442346Z","iopub.execute_input":"2023-05-22T11:49:27.443482Z","iopub.status.idle":"2023-05-22T11:49:28.502973Z","shell.execute_reply.started":"2023-05-22T11:49:27.443413Z","shell.execute_reply":"2023-05-22T11:49:28.501639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotations_path = train_path/'annotations'\nimages_path = train_path/'images'","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:49:29.549805Z","iopub.execute_input":"2023-05-22T11:49:29.550824Z","iopub.status.idle":"2023-05-22T11:49:29.556739Z","shell.execute_reply.started":"2023-05-22T11:49:29.550765Z","shell.execute_reply":"2023-05-22T11:49:29.555348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm\nimport json\n\nimg_file_names = []\nsources = []\nchart_types = []\n\nfor file_path in tqdm(sorted(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-22T11:49:39.833043Z","iopub.execute_input":"2023-05-22T11:49:39.834033Z","iopub.status.idle":"2023-05-22T11:55:37.589442Z","shell.execute_reply.started":"2023-05-22T11:49:39.833992Z","shell.execute_reply":"2023-05-22T11:55:37.588353Z"},"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-22T11:55:49.651271Z","iopub.execute_input":"2023-05-22T11:55:49.652190Z","iopub.status.idle":"2023-05-22T11:55:49.698567Z","shell.execute_reply.started":"2023-05-22T11:55:49.652139Z","shell.execute_reply":"2023-05-22T11:55:49.697440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"horizontal_bar_df = df.loc[df['chart_type'] == 'horizontal_bar'].reset_index(drop=True)\nhorizontal_bar_df","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:55:54.041046Z","iopub.execute_input":"2023-05-22T11:55:54.042067Z","iopub.status.idle":"2023-05-22T11:55:54.071360Z","shell.execute_reply.started":"2023-05-22T11:55:54.042022Z","shell.execute_reply":"2023-05-22T11:55:54.070428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference & Postprocessing","metadata":{}},{"cell_type":"markdown","source":"Let's run inference on 8 randomly chosen images.","metadata":{}},{"cell_type":"code","source":"horizontal_bar_df = horizontal_bar_df.sample(frac=1, random_state=42, ignore_index=True)\nsample_df = horizontal_bar_df.iloc[:8]\nsample_df","metadata":{"execution":{"iopub.status.busy":"2023-05-22T11:59:25.707596Z","iopub.execute_input":"2023-05-22T11:59:25.708602Z","iopub.status.idle":"2023-05-22T11:59:25.724185Z","shell.execute_reply.started":"2023-05-22T11:59:25.708535Z","shell.execute_reply":"2023-05-22T11:59:25.723121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from albumentations.pytorch.transforms import ToTensorV2\n\nhorizontal_bar_transforms = A.Compose([\n    A.LongestMaxSize(always_apply=False, p=1, max_size=896, interpolation=1),\n    A.PadIfNeeded(always_apply=False, p=1.0, min_height=896, min_width=896, pad_height_divisor=None, pad_width_divisor=None, border_mode=0, value=[124, 116, 104], mask_value=None),\n    A.Normalize(always_apply=False, p=1.0, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0), \n    ToTensorV2()\n])\nhorizontal_bar_transforms","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:00:03.592264Z","iopub.execute_input":"2023-05-22T12:00:03.593101Z","iopub.status.idle":"2023-05-22T12:00:03.609289Z","shell.execute_reply.started":"2023-05-22T12:00:03.593057Z","shell.execute_reply":"2023-05-22T12:00:03.608203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:00:43.562169Z","iopub.execute_input":"2023-05-22T12:00:43.562572Z","iopub.status.idle":"2023-05-22T12:00:43.567920Z","shell.execute_reply.started":"2023-05-22T12:00:43.562529Z","shell.execute_reply":"2023-05-22T12:00:43.566815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_threshold = 0.5","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:00:53.295211Z","iopub.execute_input":"2023-05-22T12:00:53.295945Z","iopub.status.idle":"2023-05-22T12:00:53.301201Z","shell.execute_reply.started":"2023-05-22T12:00:53.295903Z","shell.execute_reply":"2023-05-22T12:00:53.299821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_horizontal_bar_bboxes(img_file_name):\n    img = Image.open(images_path/img_file_name)\n    img_arr = np.array(img)\n    transformed = horizontal_bar_transforms(image=img_arr)\n    batched = transformed['image'].unsqueeze(0)\n    batched = batched.to(device)\n    with torch.no_grad():\n        out = horizontal_bar_model(batched)\n    first_out = out[0]\n    first_out['boxes'] = first_out['boxes'][first_out['scores'] > score_threshold]\n    postprocessed_bboxes = []\n    for box in first_out['boxes']:\n        bbox = BBox(box[0].item(), box[1].item(), box[2].item(), box[3].item())\n        new_bbox = postprocess_bbox(img_size=ImgSize(width=img.size[0], height=img.size[1]), bbox=bbox, transforms=horizontal_bar_tfms_list, h_after=896, w_after=896)\n        postprocessed_bboxes.append(list(new_bbox))\n    return postprocessed_bboxes","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:16:20.407557Z","iopub.execute_input":"2023-05-22T12:16:20.408538Z","iopub.status.idle":"2023-05-22T12:16:20.418411Z","shell.execute_reply.started":"2023-05-22T12:16:20.408496Z","shell.execute_reply":"2023-05-22T12:16:20.417222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_img_with_bbs(img_file_name, postprocessed_bboxes):\n    img = Image.open(images_path/img_file_name)\n    img_arr = np.array(img)\n    for i in range(len(postprocessed_bboxes)):\n        start_point = postprocessed_bboxes[i][0], postprocessed_bboxes[i][1]\n        end_point = postprocessed_bboxes[i][2], postprocessed_bboxes[i][3]\n        cv2.rectangle(img_arr, start_point, end_point, color=(0, 0, 255), thickness=2)\n    img_with_bbs = Image.fromarray(img_arr)\n    return img_with_bbs","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:16:22.600798Z","iopub.execute_input":"2023-05-22T12:16:22.601611Z","iopub.status.idle":"2023-05-22T12:16:22.609868Z","shell.execute_reply.started":"2023-05-22T12:16:22.601570Z","shell.execute_reply":"2023-05-22T12:16:22.608630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"img_file_name = sample_df['img_file_name'][0]\nimg_file_name","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:16:25.692713Z","iopub.execute_input":"2023-05-22T12:16:25.693690Z","iopub.status.idle":"2023-05-22T12:16:25.702323Z","shell.execute_reply.started":"2023-05-22T12:16:25.693635Z","shell.execute_reply":"2023-05-22T12:16:25.700991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\npostprocessed_bboxes = get_horizontal_bar_bboxes(img_file_name)\npostprocessed_bboxes","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:16:36.292993Z","iopub.execute_input":"2023-05-22T12:16:36.293444Z","iopub.status.idle":"2023-05-22T12:16:36.385179Z","shell.execute_reply.started":"2023-05-22T12:16:36.293381Z","shell.execute_reply":"2023-05-22T12:16:36.384087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_with_bbs = get_img_with_bbs(img_file_name, postprocessed_bboxes)\nimg_with_bbs","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:16:38.438704Z","iopub.execute_input":"2023-05-22T12:16:38.439460Z","iopub.status.idle":"2023-05-22T12:16:38.466716Z","shell.execute_reply.started":"2023-05-22T12:16:38.439390Z","shell.execute_reply":"2023-05-22T12:16:38.465589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"img_file_name = sample_df['img_file_name'][1]\nimg_file_name","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:16:41.573888Z","iopub.execute_input":"2023-05-22T12:16:41.574588Z","iopub.status.idle":"2023-05-22T12:16:41.581585Z","shell.execute_reply.started":"2023-05-22T12:16:41.574544Z","shell.execute_reply":"2023-05-22T12:16:41.580524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\npostprocessed_bboxes = get_horizontal_bar_bboxes(img_file_name)\npostprocessed_bboxes","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:16:53.175980Z","iopub.execute_input":"2023-05-22T12:16:53.176931Z","iopub.status.idle":"2023-05-22T12:16:53.265053Z","shell.execute_reply.started":"2023-05-22T12:16:53.176875Z","shell.execute_reply":"2023-05-22T12:16:53.263922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_with_bbs = get_img_with_bbs(img_file_name, postprocessed_bboxes)\nimg_with_bbs","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:16:55.402718Z","iopub.execute_input":"2023-05-22T12:16:55.403687Z","iopub.status.idle":"2023-05-22T12:16:55.430086Z","shell.execute_reply.started":"2023-05-22T12:16:55.403630Z","shell.execute_reply":"2023-05-22T12:16:55.428855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"img_file_name = sample_df['img_file_name'][2]\nimg_file_name","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:16:58.838063Z","iopub.execute_input":"2023-05-22T12:16:58.838854Z","iopub.status.idle":"2023-05-22T12:16:58.846108Z","shell.execute_reply.started":"2023-05-22T12:16:58.838814Z","shell.execute_reply":"2023-05-22T12:16:58.844996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\npostprocessed_bboxes = get_horizontal_bar_bboxes(img_file_name)\npostprocessed_bboxes","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:17:08.888560Z","iopub.execute_input":"2023-05-22T12:17:08.889060Z","iopub.status.idle":"2023-05-22T12:17:08.987853Z","shell.execute_reply.started":"2023-05-22T12:17:08.889025Z","shell.execute_reply":"2023-05-22T12:17:08.986699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_with_bbs = get_img_with_bbs(img_file_name, postprocessed_bboxes)\nimg_with_bbs","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:17:11.593368Z","iopub.execute_input":"2023-05-22T12:17:11.594109Z","iopub.status.idle":"2023-05-22T12:17:11.618984Z","shell.execute_reply.started":"2023-05-22T12:17:11.594069Z","shell.execute_reply":"2023-05-22T12:17:11.617809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"img_file_name = sample_df['img_file_name'][3]\nimg_file_name","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:17:15.477236Z","iopub.execute_input":"2023-05-22T12:17:15.478339Z","iopub.status.idle":"2023-05-22T12:17:15.486481Z","shell.execute_reply.started":"2023-05-22T12:17:15.478285Z","shell.execute_reply":"2023-05-22T12:17:15.484896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\npostprocessed_bboxes = get_horizontal_bar_bboxes(img_file_name)\npostprocessed_bboxes","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:17:24.704245Z","iopub.execute_input":"2023-05-22T12:17:24.705219Z","iopub.status.idle":"2023-05-22T12:17:24.789524Z","shell.execute_reply.started":"2023-05-22T12:17:24.705163Z","shell.execute_reply":"2023-05-22T12:17:24.788449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_with_bbs = get_img_with_bbs(img_file_name, postprocessed_bboxes)\nimg_with_bbs","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:17:26.956160Z","iopub.execute_input":"2023-05-22T12:17:26.956675Z","iopub.status.idle":"2023-05-22T12:17:26.978155Z","shell.execute_reply.started":"2023-05-22T12:17:26.956636Z","shell.execute_reply":"2023-05-22T12:17:26.977006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"img_file_name = sample_df['img_file_name'][4]\nimg_file_name","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:17:32.469200Z","iopub.execute_input":"2023-05-22T12:17:32.469957Z","iopub.status.idle":"2023-05-22T12:17:32.477945Z","shell.execute_reply.started":"2023-05-22T12:17:32.469914Z","shell.execute_reply":"2023-05-22T12:17:32.476625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\npostprocessed_bboxes = get_horizontal_bar_bboxes(img_file_name)\npostprocessed_bboxes","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:17:38.455112Z","iopub.execute_input":"2023-05-22T12:17:38.456192Z","iopub.status.idle":"2023-05-22T12:17:38.544439Z","shell.execute_reply.started":"2023-05-22T12:17:38.456127Z","shell.execute_reply":"2023-05-22T12:17:38.543425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_with_bbs = get_img_with_bbs(img_file_name, postprocessed_bboxes)\nimg_with_bbs","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:17:41.692517Z","iopub.execute_input":"2023-05-22T12:17:41.693238Z","iopub.status.idle":"2023-05-22T12:17:41.715939Z","shell.execute_reply.started":"2023-05-22T12:17:41.693196Z","shell.execute_reply":"2023-05-22T12:17:41.714727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"img_file_name = sample_df['img_file_name'][5]\nimg_file_name","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:17:45.640033Z","iopub.execute_input":"2023-05-22T12:17:45.640456Z","iopub.status.idle":"2023-05-22T12:17:45.647978Z","shell.execute_reply.started":"2023-05-22T12:17:45.640410Z","shell.execute_reply":"2023-05-22T12:17:45.646752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\npostprocessed_bboxes = get_horizontal_bar_bboxes(img_file_name)\npostprocessed_bboxes","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:17:51.195368Z","iopub.execute_input":"2023-05-22T12:17:51.196110Z","iopub.status.idle":"2023-05-22T12:17:51.284073Z","shell.execute_reply.started":"2023-05-22T12:17:51.196070Z","shell.execute_reply":"2023-05-22T12:17:51.282914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_with_bbs = get_img_with_bbs(img_file_name, postprocessed_bboxes)\nimg_with_bbs","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:17:53.506382Z","iopub.execute_input":"2023-05-22T12:17:53.507368Z","iopub.status.idle":"2023-05-22T12:17:53.536905Z","shell.execute_reply.started":"2023-05-22T12:17:53.507300Z","shell.execute_reply":"2023-05-22T12:17:53.535744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"img_file_name = sample_df['img_file_name'][6]\nimg_file_name","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:17:56.303016Z","iopub.execute_input":"2023-05-22T12:17:56.304026Z","iopub.status.idle":"2023-05-22T12:17:56.311012Z","shell.execute_reply.started":"2023-05-22T12:17:56.303984Z","shell.execute_reply":"2023-05-22T12:17:56.309881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\npostprocessed_bboxes = get_horizontal_bar_bboxes(img_file_name)\npostprocessed_bboxes","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:18:02.128728Z","iopub.execute_input":"2023-05-22T12:18:02.129611Z","iopub.status.idle":"2023-05-22T12:18:02.215340Z","shell.execute_reply.started":"2023-05-22T12:18:02.129564Z","shell.execute_reply":"2023-05-22T12:18:02.214250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_with_bbs = get_img_with_bbs(img_file_name, postprocessed_bboxes)\nimg_with_bbs","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:18:05.292013Z","iopub.execute_input":"2023-05-22T12:18:05.292429Z","iopub.status.idle":"2023-05-22T12:18:05.319575Z","shell.execute_reply.started":"2023-05-22T12:18:05.292372Z","shell.execute_reply":"2023-05-22T12:18:05.318563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"img_file_name = sample_df['img_file_name'][7]\nimg_file_name","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:18:09.740252Z","iopub.execute_input":"2023-05-22T12:18:09.740976Z","iopub.status.idle":"2023-05-22T12:18:09.748481Z","shell.execute_reply.started":"2023-05-22T12:18:09.740935Z","shell.execute_reply":"2023-05-22T12:18:09.747437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\npostprocessed_bboxes = get_horizontal_bar_bboxes(img_file_name)\npostprocessed_bboxes","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:18:15.960890Z","iopub.execute_input":"2023-05-22T12:18:15.961279Z","iopub.status.idle":"2023-05-22T12:18:16.049106Z","shell.execute_reply.started":"2023-05-22T12:18:15.961243Z","shell.execute_reply":"2023-05-22T12:18:16.048066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_with_bbs = get_img_with_bbs(img_file_name, postprocessed_bboxes)\nimg_with_bbs","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:18:18.173639Z","iopub.execute_input":"2023-05-22T12:18:18.174640Z","iopub.status.idle":"2023-05-22T12:18:18.203172Z","shell.execute_reply.started":"2023-05-22T12:18:18.174584Z","shell.execute_reply":"2023-05-22T12:18:18.201986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"print(\"That's all!\")","metadata":{"execution":{"iopub.status.busy":"2023-05-22T12:18:35.719881Z","iopub.execute_input":"2023-05-22T12:18:35.720257Z","iopub.status.idle":"2023-05-22T12:18:35.726327Z","shell.execute_reply.started":"2023-05-22T12:18:35.720223Z","shell.execute_reply":"2023-05-22T12:18:35.725207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}