{"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":"# Install","metadata":{}},{"cell_type":"markdown","source":"## Install transformers","metadata":{}},{"cell_type":"code","source":"!pip uninstall transformers -y","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:49:41.192242Z","iopub.execute_input":"2023-06-27T14:49:41.192517Z","iopub.status.idle":"2023-06-27T14:49:46.125373Z","shell.execute_reply.started":"2023-06-27T14:49:41.192492Z","shell.execute_reply":"2023-06-27T14:49:46.123929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m pip install --no-index --find-links=/kaggle/input/benetech-pip transformers","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:49:46.128828Z","iopub.execute_input":"2023-06-27T14:49:46.134534Z","iopub.status.idle":"2023-06-27T14:50:20.568873Z","shell.execute_reply.started":"2023-06-27T14:49:46.134494Z","shell.execute_reply":"2023-06-27T14:50:20.567722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import transformers\nprint(transformers.__name__, transformers.__version__)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:50:20.571220Z","iopub.execute_input":"2023-06-27T14:50:20.571615Z","iopub.status.idle":"2023-06-27T14:50:21.767327Z","shell.execute_reply.started":"2023-06-27T14:50:20.571578Z","shell.execute_reply":"2023-06-27T14:50:21.766379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp /kaggle/input/deplot-font/image_processing_pix2struct.py /opt/conda/lib/python3.10/site-packages/transformers/models/pix2struct","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:50:21.769829Z","iopub.execute_input":"2023-06-27T14:50:21.771001Z","iopub.status.idle":"2023-06-27T14:50:22.750228Z","shell.execute_reply.started":"2023-06-27T14:50:21.770961Z","shell.execute_reply":"2023-06-27T14:50:22.748861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Install CACHED","metadata":{}},{"cell_type":"code","source":"! pip install --no-index --find-links=file:///kaggle/input/cached \\\n  addict terminaltables pycocotools mmcv_full mmdet","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:50:22.755606Z","iopub.execute_input":"2023-06-27T14:50:22.755960Z","iopub.status.idle":"2023-06-27T14:50:36.270107Z","shell.execute_reply.started":"2023-06-27T14:50:22.755924Z","shell.execute_reply":"2023-06-27T14:50:36.268984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Install YOLOX","metadata":{}},{"cell_type":"code","source":"!pip install /kaggle/input/yolox-main/dependency/loguru-0.7.0-py3-none-any.whl\n!pip install /kaggle/input/yolox-main/dependency/thop-0.1.1.post2209072238-py3-none-any.whl\n!pip install /kaggle/input/yolox-main/dependency/onnx_simplifier-0.4.10-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:50:36.272807Z","iopub.execute_input":"2023-06-27T14:50:36.273206Z","iopub.status.idle":"2023-06-27T14:52:09.787655Z","shell.execute_reply.started":"2023-06-27T14:50:36.273168Z","shell.execute_reply":"2023-06-27T14:52:09.786471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/yolox-main/YOLOX-main /tmp\n%cd /tmp/YOLOX-main\n!pip install -v -e .\n%cd /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:52:09.789532Z","iopub.execute_input":"2023-06-27T14:52:09.789891Z","iopub.status.idle":"2023-06-27T14:53:07.627054Z","shell.execute_reply.started":"2023-06-27T14:52:09.789862Z","shell.execute_reply":"2023-06-27T14:53:07.625646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## import libs","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport shutil\nfrom tqdm import tqdm\nfrom glob import glob\nimport cv2\nimport json\nfrom mmdet.apis import init_detector, inference_detector","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:53:07.629854Z","iopub.execute_input":"2023-06-27T14:53:07.630248Z","iopub.status.idle":"2023-06-27T14:53:13.292778Z","shell.execute_reply.started":"2023-06-27T14:53:07.630216Z","shell.execute_reply":"2023-06-27T14:53:13.291823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Classification","metadata":{}},{"cell_type":"code","source":"!pip uninstall timm -y\n!pip install /kaggle/input/timm-0613/timm-0.6.13-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:53:13.294367Z","iopub.execute_input":"2023-06-27T14:53:13.294753Z","iopub.status.idle":"2023-06-27T14:53:47.301349Z","shell.execute_reply.started":"2023-06-27T14:53:13.294718Z","shell.execute_reply":"2023-06-27T14:53:47.300133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import timm\ntimm.__version__","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:53:47.307303Z","iopub.execute_input":"2023-06-27T14:53:47.307650Z","iopub.status.idle":"2023-06-27T14:53:48.163217Z","shell.execute_reply.started":"2023-06-27T14:53:47.307621Z","shell.execute_reply":"2023-06-27T14:53:48.162309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_path = \"/kaggle/input/benetech-making-graphs-accessible/test/images\"\ncls_model_base_path = \"/kaggle/input/benetech-classificationmodel\"\ncls_dst_dir = \"/tmp/clssification_results\"\ncls_models = [\n    \"convnext_large_384_in22ft1k\",\n    \"swin_l_384\",\n]","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:53:48.164810Z","iopub.execute_input":"2023-06-27T14:53:48.165189Z","iopub.status.idle":"2023-06-27T14:53:48.170350Z","shell.execute_reply.started":"2023-06-27T14:53:48.165155Z","shell.execute_reply":"2023-06-27T14:53:48.168951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(cls_dst_dir, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:53:48.171704Z","iopub.execute_input":"2023-06-27T14:53:48.172295Z","iopub.status.idle":"2023-06-27T14:53:48.180645Z","shell.execute_reply.started":"2023-06-27T14:53:48.172260Z","shell.execute_reply":"2023-06-27T14:53:48.179697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inference_script = os.path.join(cls_model_base_path, \"inference.py\")\ndef exec_classification(model):\n    model_dir = os.path.join(cls_model_base_path, model)\n    config_path = os.path.join(model_dir, \"config.txt\")\n    \n    config = {}\n    with open(config_path) as f:\n        for line in f:\n            line = line.replace(\" \", \"\")\n            line = line.replace(\"\\n\", \"\")\n            k, v = line.split(\":\")\n            try:\n                config[k] = int(v)\n            except:\n                config[k] = v\n    \n    cmd = f\"{inference_script} \"\n    cmd += f\"-m {config['model']} \"\n    cmd += f\"-w {os.path.join(model_dir, 'best.bin')} \"\n    cmd += f\"--size {config['size']} \"\n    cmd += f\"-i {input_path} \"\n    cmd += f\"-o {os.path.join(cls_dst_dir, model)}.csv \"\n    print(f\"========== START {config['model']} ==========\")\n    print(cmd)\n    !python {cmd}","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:53:48.181991Z","iopub.execute_input":"2023-06-27T14:53:48.182388Z","iopub.status.idle":"2023-06-27T14:53:48.193961Z","shell.execute_reply.started":"2023-06-27T14:53:48.182355Z","shell.execute_reply":"2023-06-27T14:53:48.192941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(os.path.join(cls_model_base_path, \"weight.json\"), \"r\") as f:\n    model_weights = json.load(f)\nmodel_weights","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:53:48.195593Z","iopub.execute_input":"2023-06-27T14:53:48.195925Z","iopub.status.idle":"2023-06-27T14:53:48.213872Z","shell.execute_reply.started":"2023-06-27T14:53:48.195894Z","shell.execute_reply":"2023-06-27T14:53:48.212874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Inference\ndf_list = []\nweights = []\nfor cls_model in cls_models:\n    exec_classification(cls_model)\n    df = pd.read_csv(os.path.join(cls_dst_dir, f\"{cls_model}.csv\"))\n    df[\"logits\"] = df[\"logits\"].map(lambda x: eval(x))\n    df_list.append(df)\n    weights.append(model_weights[cls_model])","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:53:48.215270Z","iopub.execute_input":"2023-06-27T14:53:48.215680Z","iopub.status.idle":"2023-06-27T14:54:27.824123Z","shell.execute_reply.started":"2023-06-27T14:53:48.215649Z","shell.execute_reply":"2023-06-27T14:54:27.822853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Ensemble\nall_logits = []\nfor df in df_list:\n    logits = df[\"logits\"].values\n    logits = np.array([np.array(l) for l in logits])\n    all_logits.append(logits)\nall_logits = np.array(all_logits)\nensemble_preds = np.average(all_logits, axis=0, weights=np.array(weights))\npred_labels = np.argmax(ensemble_preds, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:54:27.827558Z","iopub.execute_input":"2023-06-27T14:54:27.828589Z","iopub.status.idle":"2023-06-27T14:54:27.836851Z","shell.execute_reply.started":"2023-06-27T14:54:27.828542Z","shell.execute_reply":"2023-06-27T14:54:27.835737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_map = [\n    \"vertical_bar\",\n    \"horizontal_bar\",\n    \"line\",\n    \"dot\",\n    \"scatter\"\n]\n\npred_chart_type_df = df_list[0].copy()\npred_chart_type_df[\"pred_label\"] = pred_labels.tolist()\npred_chart_type_df[\"pred_label\"] = pred_chart_type_df[\"pred_label\"].map(lambda x: label_map[x])\npred_chart_type_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:54:27.838907Z","iopub.execute_input":"2023-06-27T14:54:27.839379Z","iopub.status.idle":"2023-06-27T14:54:27.862920Z","shell.execute_reply.started":"2023-06-27T14:54:27.839344Z","shell.execute_reply":"2023-06-27T14:54:27.862075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Copy images\nDST_BASE_DIR = \"/tmp/output\"\nfor chart_type in [\"dot\", \"horizontal_bar\", \"vertical_bar\", \"line\", \"scatter\"]:\n    dst_dir = os.path.join(DST_BASE_DIR, chart_type, \"images\")\n    os.makedirs(dst_dir, exist_ok=True)\n    \n    _df = pred_chart_type_df.query(\"pred_label==@chart_type\")\n    data_id_list = _df[\"id\"].values.tolist()\n    for data_id in tqdm(data_id_list):\n        src_img_path = os.path.join(input_path, f\"{data_id}.jpg\")\n        dst_img_path = os.path.join(dst_dir, f\"{data_id}.jpg\")\n        shutil.copyfile(src_img_path, dst_img_path)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:54:27.864379Z","iopub.execute_input":"2023-06-27T14:54:27.864774Z","iopub.status.idle":"2023-06-27T14:54:27.913289Z","shell.execute_reply.started":"2023-06-27T14:54:27.864742Z","shell.execute_reply":"2023-06-27T14:54:27.912369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference by chart type","metadata":{}},{"cell_type":"markdown","source":"## Helper Functions","metadata":{}},{"cell_type":"code","source":"def inference(cfg):\n    inference_script = f\"/kaggle/input/benetech-{cfg['exp_name']}/inference.py\"\n    model_dir = f\"/kaggle/input/benetech-{cfg['exp_name']}/{cfg['model_name']}\"\n    batch_size = cfg['batch_size']\n    dst_dir = cfg['dst_dir']\n    data_dir = cfg['data_dir']\n    os.makedirs(dst_dir, exist_ok=True)\n    \n    cmd = f'{inference_script} --model_dir {model_dir} --batch_size {batch_size} --dst_dir {dst_dir} --data_dir {data_dir}'\n    print(f\"!python {cmd}\")\n    !python {cmd}","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:54:27.914764Z","iopub.execute_input":"2023-06-27T14:54:27.916028Z","iopub.status.idle":"2023-06-27T14:54:27.923697Z","shell.execute_reply.started":"2023-06-27T14:54:27.915955Z","shell.execute_reply":"2023-06-27T14:54:27.922458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## vertical bar","metadata":{}},{"cell_type":"code","source":"config = {\n    \"exp_name\": \"specificcharttrain-verticalbar-deplot\",\n    \"model_name\": \"20230617120654\",\n    \"batch_size\": 4,\n    \"dst_dir\": \"/tmp/output/vertical_bar\",\n    \"data_dir\": \"/tmp/output/vertical_bar\",\n}","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:54:27.927187Z","iopub.execute_input":"2023-06-27T14:54:27.927553Z","iopub.status.idle":"2023-06-27T14:54:27.934296Z","shell.execute_reply.started":"2023-06-27T14:54:27.927527Z","shell.execute_reply":"2023-06-27T14:54:27.933302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'===== Start {config[\"exp_name\"]}/{config[\"model_name\"]} =====')\ninference(config)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:54:27.935788Z","iopub.execute_input":"2023-06-27T14:54:27.936840Z","iopub.status.idle":"2023-06-27T14:55:01.105909Z","shell.execute_reply.started":"2023-06-27T14:54:27.936806Z","shell.execute_reply":"2023-06-27T14:55:01.104739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(os.path.join(config[\"dst_dir\"], \"submission.csv\"))\ndf[\"chart_type\"] = \"vertical_bar\"\ndf.to_csv(os.path.join(config[\"dst_dir\"], \"submission.csv\"), index=False)\ndf","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:55:01.108885Z","iopub.execute_input":"2023-06-27T14:55:01.109287Z","iopub.status.idle":"2023-06-27T14:55:01.130306Z","shell.execute_reply.started":"2023-06-27T14:55:01.109249Z","shell.execute_reply":"2023-06-27T14:55:01.129392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## line","metadata":{}},{"cell_type":"code","source":"config = {\n    \"exp_name\": \"specificcharttrain-line-deplot\",\n    \"model_name\": \"20230606124846\",\n    \"batch_size\": 4,\n    \"dst_dir\": \"/tmp/output/line\",\n    \"data_dir\": \"/tmp/output/line\",\n}","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:55:01.131592Z","iopub.execute_input":"2023-06-27T14:55:01.131925Z","iopub.status.idle":"2023-06-27T14:55:01.138117Z","shell.execute_reply.started":"2023-06-27T14:55:01.131892Z","shell.execute_reply":"2023-06-27T14:55:01.137110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'===== Start {config[\"exp_name\"]}/{config[\"model_name\"]} =====')\ninference(config)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:55:01.139759Z","iopub.execute_input":"2023-06-27T14:55:01.140726Z","iopub.status.idle":"2023-06-27T14:55:25.398929Z","shell.execute_reply.started":"2023-06-27T14:55:01.140690Z","shell.execute_reply":"2023-06-27T14:55:25.397780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(os.path.join(config[\"dst_dir\"], \"submission.csv\"))\ndf[\"chart_type\"] = \"line\"\ndf.to_csv(os.path.join(config[\"dst_dir\"], \"submission.csv\"), index=False)\ndf","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:55:25.400734Z","iopub.execute_input":"2023-06-27T14:55:25.401121Z","iopub.status.idle":"2023-06-27T14:55:25.420763Z","shell.execute_reply.started":"2023-06-27T14:55:25.401082Z","shell.execute_reply":"2023-06-27T14:55:25.419291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## dot","metadata":{}},{"cell_type":"code","source":"config = {\n    \"exp_name\": \"allcharttrain-deplot\",\n    \"model_name\": \"20230602234838\",\n    \"batch_size\": 4,\n    \"dst_dir\": \"/tmp/output/dot\",\n    \"data_dir\": \"/tmp/output/dot\",\n}","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:55:25.422296Z","iopub.execute_input":"2023-06-27T14:55:25.422881Z","iopub.status.idle":"2023-06-27T14:55:25.427409Z","shell.execute_reply.started":"2023-06-27T14:55:25.422848Z","shell.execute_reply":"2023-06-27T14:55:25.426472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'===== Start {config[\"exp_name\"]}/{config[\"model_name\"]} =====')\ninference(config)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:55:25.428790Z","iopub.execute_input":"2023-06-27T14:55:25.429367Z","iopub.status.idle":"2023-06-27T14:55:48.207037Z","shell.execute_reply.started":"2023-06-27T14:55:25.429335Z","shell.execute_reply":"2023-06-27T14:55:48.205843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(os.path.join(config[\"dst_dir\"], \"submission.csv\"))\ndf[\"chart_type\"] = \"dot\"\ndf.to_csv(os.path.join(config[\"dst_dir\"], \"submission.csv\"), index=False)\ndf","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:55:48.214813Z","iopub.execute_input":"2023-06-27T14:55:48.215436Z","iopub.status.idle":"2023-06-27T14:55:48.233600Z","shell.execute_reply.started":"2023-06-27T14:55:48.215373Z","shell.execute_reply":"2023-06-27T14:55:48.232540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## horizontal_bar","metadata":{}},{"cell_type":"code","source":"config = {\n    \"exp_name\": \"allcharttrain-deplot\",\n    \"model_name\": \"20230602234838\",\n    \"batch_size\": 4,\n    \"dst_dir\": \"/tmp/output/horizontal_bar\",\n    \"data_dir\": \"/tmp/output/horizontal_bar\",\n}","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:55:48.234917Z","iopub.execute_input":"2023-06-27T14:55:48.235243Z","iopub.status.idle":"2023-06-27T14:55:48.240268Z","shell.execute_reply.started":"2023-06-27T14:55:48.235212Z","shell.execute_reply":"2023-06-27T14:55:48.239121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'===== Start {config[\"exp_name\"]}/{config[\"model_name\"]} =====')\ninference(config)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:55:48.241878Z","iopub.execute_input":"2023-06-27T14:55:48.242549Z","iopub.status.idle":"2023-06-27T14:56:04.078005Z","shell.execute_reply.started":"2023-06-27T14:55:48.242512Z","shell.execute_reply":"2023-06-27T14:56:04.076843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## swap x y\ndf = pd.read_csv(os.path.join(config[\"dst_dir\"], \"submission.csv\"))\ndata_id_list = glob(os.path.join(config[\"data_dir\"], \"images/*.jpg\"))\ndata_id_list = [\".\".join(os.path.basename(x).split(\".\")[:-1]) for x in data_id_list]\n\nfor data_id in data_id_list:\n    x_data_series = df.loc[df['id'] == f'{data_id}_x', 'data_series'].values[0]\n    y_data_series = df.loc[df['id'] == f'{data_id}_y', 'data_series'].values[0]\n    df.loc[df['id'] == f'{data_id}_x', 'data_series'] = y_data_series\n    df.loc[df['id'] == f'{data_id}_y', 'data_series'] = x_data_series\n\ndf[\"chart_type\"] = \"horizontal_bar\"\ndf.to_csv(os.path.join(config[\"dst_dir\"], \"submission.csv\"), index=False)\ndf","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:56:04.079873Z","iopub.execute_input":"2023-06-27T14:56:04.080359Z","iopub.status.idle":"2023-06-27T14:56:04.103897Z","shell.execute_reply.started":"2023-06-27T14:56:04.080310Z","shell.execute_reply":"2023-06-27T14:56:04.102736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## scatter","metadata":{}},{"cell_type":"code","source":"axis_reading_config = {\n    \"exp_name\": \"scatter-axis-reading\",\n    \"model_name\": \"20230530215211\",\n    \"batch_size\": 4,\n    \"dst_dir\": \"/tmp/output/scatter/axis_reading_output\",\n    \"data_dir\": \"/tmp/output/scatter/\",\n}\nscatter_point_detection_config = {\n    \"model_name\": \"yolox_l_benetech_scatter_point_001_1280\",\n    \"size\": 1280,\n    \"conf\": 0.25,\n    \"nms\": 0.45\n}\ninference_config = {\n    \"version\": \"v5\",\n    \"point_conf_thresh\": 0.4,\n    \"label_pos_conf_thresh\": 0.8,\n    \"exception_area_thresh\": 2.0,\n    \"overlap_thresh\": 2.0,\n    \"point_area_thresh_min\": 0.2,\n    \"point_area_thresh_max\": 5.0,\n}","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:56:04.105383Z","iopub.execute_input":"2023-06-27T14:56:04.106123Z","iopub.status.idle":"2023-06-27T14:56:04.112634Z","shell.execute_reply.started":"2023-06-27T14:56:04.106088Z","shell.execute_reply":"2023-06-27T14:56:04.111696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CACHED inference","metadata":{}},{"cell_type":"code","source":"wdir = '/kaggle/input/cached/work_dirs'\nconfig_file = wdir + '/cascade_rcnn_swin-t_fpn_LGF_VCE_PCE_coco_focalsmoothloss/cascade_rcnn_swin-t_fpn_LGF_VCE_PCE_coco_focalsmoothloss.py'\ncheckpoint_file = wdir + '/cascade_rcnn_swin-t_fpn_LGF_VCE_PCE_coco_focalsmoothloss/checkpoint.pth'\n\nmodel = init_detector(config_file, checkpoint_file, device='cuda')","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:56:04.114207Z","iopub.execute_input":"2023-06-27T14:56:04.114856Z","iopub.status.idle":"2023-06-27T14:56:24.130611Z","shell.execute_reply.started":"2023-06-27T14:56:04.114824Z","shell.execute_reply":"2023-06-27T14:56:24.129532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dst_dir = \"/tmp/output/scatter/cached_output\"\nos.makedirs(dst_dir, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:56:24.132210Z","iopub.execute_input":"2023-06-27T14:56:24.132589Z","iopub.status.idle":"2023-06-27T14:56:24.137683Z","shell.execute_reply.started":"2023-06-27T14:56:24.132555Z","shell.execute_reply":"2023-06-27T14:56:24.136529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_id_list = glob(\"/tmp/output/scatter/images/*.jpg\")\ndata_id_list = [\".\".join(os.path.basename(x).split(\".\")[:-1]) for x in data_id_list]\nfor data_id in tqdm(data_id_list):\n    filename = f\"/tmp/output/scatter/images/{data_id}.jpg\"\n    result = inference_detector(model, filename)\n    result = np.array(result)\n    np.save(os.path.join(dst_dir, f\"{data_id}.npy\"), result)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:56:24.139196Z","iopub.execute_input":"2023-06-27T14:56:24.139786Z","iopub.status.idle":"2023-06-27T14:56:25.372291Z","shell.execute_reply.started":"2023-06-27T14:56:24.139754Z","shell.execute_reply":"2023-06-27T14:56:25.371313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\nimport torch\ndel model\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:56:25.373942Z","iopub.execute_input":"2023-06-27T14:56:25.374307Z","iopub.status.idle":"2023-06-27T14:56:25.731997Z","shell.execute_reply.started":"2023-06-27T14:56:25.374273Z","shell.execute_reply":"2023-06-27T14:56:25.731015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### create crop dataset","metadata":{}},{"cell_type":"code","source":"dst_dir = \"/tmp/output/scatter/crop_images\"\nos.makedirs(dst_dir, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:56:25.733612Z","iopub.execute_input":"2023-06-27T14:56:25.733952Z","iopub.status.idle":"2023-06-27T14:56:25.738963Z","shell.execute_reply.started":"2023-06-27T14:56:25.733919Z","shell.execute_reply":"2023-06-27T14:56:25.737714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_id_list = glob(\"/tmp/output/scatter/images/*.jpg\")\ndata_id_list = [\".\".join(os.path.basename(x).split(\".\")[:-1]) for x in data_id_list]\nfor data_id in tqdm(data_id_list):\n    img = cv2.imread(f\"/tmp/output/scatter/images/{data_id}.jpg\")\n    annot = np.load(f\"/tmp/output/scatter/cached_output/{data_id}.npy\", allow_pickle=True)\n    plot_area_dets = annot[2]\n    if len(plot_area_dets) == 0:\n        cv2.imwrite(os.path.join(dst_dir, f\"{data_id}.jpg\"), img)\n    plot_area_det = plot_area_dets[0]\n    xmin, ymin, xmax, ymax, conf = plot_area_det\n    crop_img = img[int(ymin):int(ymax), int(xmin):int(xmax)]\n    cv2.imwrite(os.path.join(dst_dir, f\"{data_id}.jpg\"), crop_img)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T14:56:25.740381Z","iopub.execute_input":"2023-06-27T14:56:25.740974Z","iopub.status.idle":"2023-06-27T14:56:25.765531Z","shell.execute_reply.started":"2023-06-27T14:56:25.740939Z","shell.execute_reply":"2023-06-27T14:56:25.764659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### detect scatter point","metadata":{}},{"cell_type":"code","source":"inference_script = \"demo_scatter_point.py\"\n!cp /kaggle/input/benetech-scatter-point-detect-yolox/{inference_script} /tmp/YOLOX-main/tools","metadata":{"execution":{"iopub.status.busy":"2023-06-27T15:04:45.045983Z","iopub.execute_input":"2023-06-27T15:04:45.046813Z","iopub.status.idle":"2023-06-27T15:04:46.142023Z","shell.execute_reply.started":"2023-06-27T15:04:45.046778Z","shell.execute_reply":"2023-06-27T15:04:46.140643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cmd = f\"{inference_script} image \"\ncmd += f\"-f /kaggle/input/benetech-scatter-point-detect-yolox/{scatter_point_detection_config['model_name']}/{scatter_point_detection_config['model_name']}.py \"\ncmd += f\"-c /kaggle/input/benetech-scatter-point-detect-yolox/{scatter_point_detection_config['model_name']}/best_ckpt.pth \"\ncmd += f\"--path /tmp/output/scatter/crop_images \"\ncmd += f\"--conf {scatter_point_detection_config['conf']} --nms {scatter_point_detection_config['nms']} --tsize {scatter_point_detection_config['size']} \"\ncmd += \"--save_result --device gpu \"\ncmd += \"--output_dir /tmp/output/scatter/scatter_point_detection_output\"\nprint(f\"!python tools/{cmd}\")","metadata":{"execution":{"iopub.status.busy":"2023-06-27T15:04:47.524347Z","iopub.execute_input":"2023-06-27T15:04:47.525542Z","iopub.status.idle":"2023-06-27T15:04:47.533392Z","shell.execute_reply.started":"2023-06-27T15:04:47.525489Z","shell.execute_reply":"2023-06-27T15:04:47.532113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /tmp/YOLOX-main\n!python tools/{cmd}\n%cd /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-06-27T15:04:50.954089Z","iopub.execute_input":"2023-06-27T15:04:50.955269Z","iopub.status.idle":"2023-06-27T15:05:02.470144Z","shell.execute_reply.started":"2023-06-27T15:04:50.955224Z","shell.execute_reply":"2023-06-27T15:05:02.469024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### reading axis","metadata":{}},{"cell_type":"code","source":"print(f'===== Start {axis_reading_config[\"exp_name\"]}/{axis_reading_config[\"model_name\"]} =====')\ninference(axis_reading_config)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T15:05:11.319046Z","iopub.execute_input":"2023-06-27T15:05:11.319474Z","iopub.status.idle":"2023-06-27T15:05:29.227144Z","shell.execute_reply.started":"2023-06-27T15:05:11.319435Z","shell.execute_reply":"2023-06-27T15:05:29.225935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### inference sacatter","metadata":{}},{"cell_type":"code","source":"inference_script = f\"/kaggle/input/benetech-scatter-inference-scripts/inference_{inference_config['version']}.py\"\ncmd = f\"{inference_script} \"\ncmd += \"--label_csv /tmp/output/scatter/axis_reading_output/submission.csv \"\ncmd += \"--label_pos_dets_dir /tmp/output/scatter/cached_output \"\ncmd += \"--point_dets_dir /tmp/output/scatter/scatter_point_detection_output \"\ncmd += f\"--point_conf_thresh {inference_config['point_conf_thresh']} \"\ncmd += f\"--label_pos_conf_thresh {inference_config['label_pos_conf_thresh']} \"\ncmd += f\"--exception_area_thresh {inference_config['exception_area_thresh']} \"\ncmd += f\"--overlap_thresh {inference_config['overlap_thresh']} \"\ncmd += f\"--point_area_thresh_min {inference_config['point_area_thresh_min']} \"\ncmd += f\"--point_area_thresh_max {inference_config['point_area_thresh_max']} \"\ncmd += \"--dst_dir /tmp/output/scatter \"\n#cmd += \"--img_dir /tmp/output/scatter/images --is_debug\"\nprint(f\"!python {cmd}\")\n!python {cmd}","metadata":{"execution":{"iopub.status.busy":"2023-06-27T15:05:29.230365Z","iopub.execute_input":"2023-06-27T15:05:29.231359Z","iopub.status.idle":"2023-06-27T15:05:30.952161Z","shell.execute_reply.started":"2023-06-27T15:05:29.231323Z","shell.execute_reply":"2023-06-27T15:05:30.950924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(os.path.join(\"/tmp/output/scatter\", \"submission.csv\"))\ndf[\"chart_type\"] = \"scatter\"\ndf.to_csv(os.path.join(\"/tmp/output/scatter\", \"submission.csv\"), index=False)\ndf","metadata":{"execution":{"iopub.status.busy":"2023-06-27T15:05:30.954592Z","iopub.execute_input":"2023-06-27T15:05:30.955007Z","iopub.status.idle":"2023-06-27T15:05:30.976652Z","shell.execute_reply.started":"2023-06-27T15:05:30.954954Z","shell.execute_reply":"2023-06-27T15:05:30.975808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Merge csv","metadata":{}},{"cell_type":"code","source":"df_list = []\nfor chart_type in [\"dot\", \"horizontal_bar\", \"vertical_bar\", \"line\", \"scatter\"]:\n    csv_path = os.path.join(DST_BASE_DIR, chart_type, \"submission.csv\")\n    df_list.append(pd.read_csv(csv_path))\nmerge_df = pd.concat(df_list).reset_index(drop=True)\nmerge_df","metadata":{"execution":{"iopub.status.busy":"2023-06-27T15:05:40.892044Z","iopub.execute_input":"2023-06-27T15:05:40.892488Z","iopub.status.idle":"2023-06-27T15:05:40.940494Z","shell.execute_reply.started":"2023-06-27T15:05:40.892442Z","shell.execute_reply":"2023-06-27T15:05:40.939503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Replace nan and inf","metadata":{}},{"cell_type":"code","source":"def is_inf_or_nan(s):\n    try:\n        n = float(s)\n    except:\n        return False\n    \n    if np.isnan(n) or np.isinf(n):\n        return True\n    return False\n\ndef replace_nan_inf(data_series_list, replace_str=\"0\"):\n    replaced_data_series_list = []\n    for data_series in data_series_list:\n        splited_data_series = data_series.split(\";\")\n        replaced_data_series = \";\".join([replace_str if is_inf_or_nan(s) else s for s in splited_data_series])\n        replaced_data_series_list.append(replaced_data_series)\n    return replaced_data_series_list","metadata":{"execution":{"iopub.status.busy":"2023-06-27T15:05:43.505626Z","iopub.execute_input":"2023-06-27T15:05:43.505991Z","iopub.status.idle":"2023-06-27T15:05:43.513858Z","shell.execute_reply.started":"2023-06-27T15:05:43.505962Z","shell.execute_reply":"2023-06-27T15:05:43.512854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_series_list = merge_df[\"data_series\"].values\nreplaced_data_series_list = replace_nan_inf(data_series_list)\nmerge_df[\"data_series\"] = replaced_data_series_list","metadata":{"execution":{"iopub.status.busy":"2023-06-27T15:05:43.772660Z","iopub.execute_input":"2023-06-27T15:05:43.773029Z","iopub.status.idle":"2023-06-27T15:05:43.778931Z","shell.execute_reply.started":"2023-06-27T15:05:43.772999Z","shell.execute_reply":"2023-06-27T15:05:43.777856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge_df.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T15:05:44.172121Z","iopub.execute_input":"2023-06-27T15:05:44.173712Z","iopub.status.idle":"2023-06-27T15:05:44.180289Z","shell.execute_reply.started":"2023-06-27T15:05:44.173664Z","shell.execute_reply":"2023-06-27T15:05:44.179199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/working/submission.csv\")\ndf","metadata":{"execution":{"iopub.status.busy":"2023-06-27T15:05:48.068636Z","iopub.execute_input":"2023-06-27T15:05:48.069009Z","iopub.status.idle":"2023-06-27T15:05:48.084428Z","shell.execute_reply.started":"2023-06-27T15:05:48.068979Z","shell.execute_reply":"2023-06-27T15:05:48.083329Z"},"trusted":true},"execution_count":null,"outputs":[]}]}