{"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":"code","source":"!pip install -U -qqq fastai","metadata":{"execution":{"iopub.status.busy":"2023-06-02T19:12:50.261528Z","iopub.execute_input":"2023-06-02T19:12:50.262004Z","iopub.status.idle":"2023-06-02T19:13:06.536546Z","shell.execute_reply.started":"2023-06-02T19:12:50.261966Z","shell.execute_reply":"2023-06-02T19:13:06.535023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport cv2\nfrom matplotlib import pyplot as plt\nfrom pathlib import Path\nimport json\nfrom fastai.vision.all import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-02T19:13:06.543307Z","iopub.execute_input":"2023-06-02T19:13:06.545908Z","iopub.status.idle":"2023-06-02T19:13:06.556196Z","shell.execute_reply.started":"2023-06-02T19:13:06.545853Z","shell.execute_reply":"2023-06-02T19:13:06.554964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root = Path(\"/kaggle/input/benetech-making-graphs-accessible\")\nim_path = root/\"train/images\"\nann_path = root/\"train/annotations\"\nim_path, ann_path","metadata":{"execution":{"iopub.status.busy":"2023-06-02T19:13:06.561163Z","iopub.execute_input":"2023-06-02T19:13:06.564276Z","iopub.status.idle":"2023-06-02T19:13:06.576426Z","shell.execute_reply.started":"2023-06-02T19:13:06.564240Z","shell.execute_reply":"2023-06-02T19:13:06.575313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastcore.parallel import *\ndef get_y(o): return json.load(open(ann_path/f\"{o.stem}.json\"))['chart-type']\nch_tp = pd.Series(parallel(get_y, im_path.glob(\"*.jpg\"), n_workers=8))\nch_tp.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-06-02T19:13:06.582386Z","iopub.execute_input":"2023-06-02T19:13:06.585004Z","iopub.status.idle":"2023-06-02T19:15:38.391601Z","shell.execute_reply.started":"2023-06-02T19:13:06.584970Z","shell.execute_reply":"2023-06-02T19:15:38.390501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ndef custom_splitter(labels, ts=0.3):\n    lbs = labels\n    def splitter(items):\n        tr_idx, vl_idx = train_test_split(range(len(items)), \n                                            test_size=.3,\n                                           stratify=lbs.values)\n        return vl_idx, tr_idx\n    return splitter","metadata":{"execution":{"iopub.status.busy":"2023-06-02T19:15:38.396551Z","iopub.execute_input":"2023-06-02T19:15:38.398872Z","iopub.status.idle":"2023-06-02T19:15:38.407303Z","shell.execute_reply.started":"2023-06-02T19:15:38.398836Z","shell.execute_reply":"2023-06-02T19:15:38.406128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gray2rgb(img): return img.convert('RGB')\nimg = next(im_path.glob(\"*.jpg\"))\ngray2rgb(Image.open(img))","metadata":{"execution":{"iopub.status.busy":"2023-06-02T19:15:38.412006Z","iopub.execute_input":"2023-06-02T19:15:38.414934Z","iopub.status.idle":"2023-06-02T19:15:38.508956Z","shell.execute_reply.started":"2023-06-02T19:15:38.414900Z","shell.execute_reply":"2023-06-02T19:15:38.508104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = DataBlock( \n    blocks=(ImageBlock, CategoryBlock),\n    get_items=get_image_files,\n    get_y=get_y,\n    splitter=custom_splitter(ch_tp),\n    item_tfms=[Resize(224, 'squish')],\n    batch_tfms=aug_transforms()\n).dataloaders(im_path, ds=64, num_workers=8, pin_memory=True)\ndls.show_batch(max_n=8)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T19:15:38.513485Z","iopub.execute_input":"2023-06-02T19:15:38.516044Z","iopub.status.idle":"2023-06-02T19:16:10.658277Z","shell.execute_reply.started":"2023-06-02T19:15:38.516009Z","shell.execute_reply":"2023-06-02T19:16:10.657126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cls_nm = \"convnext_small.fb_in22k_ft_in1k\"\nlearn = vision_learner(dls, cls_nm,\n                       metrics=[BalancedAccuracy(), \n                                F1Score(average='weighted')],).to_fp16() ","metadata":{"execution":{"iopub.status.busy":"2023-06-02T19:16:10.660381Z","iopub.execute_input":"2023-06-02T19:16:10.661358Z","iopub.status.idle":"2023-06-02T19:16:18.442395Z","shell.execute_reply.started":"2023-06-02T19:16:10.661321Z","shell.execute_reply":"2023-06-02T19:16:18.441444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(4, 0.1)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T19:16:18.446908Z","iopub.execute_input":"2023-06-02T19:16:18.449572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export(\"learn.pkl\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = ch_tp[ch_tp == 'horizontal_bar'].index\nfiles = list(im_path.glob(\"*.jpg\"))\nfiles = np.array(files)[idx]\ntst_dl = dls.test_dl(files)\nprobs,_,_ = learn.get_preds(dl=tst_dl, with_decoded=True)\npreds = probs.argmax(axis=-1)\npd.Series(dls.vocab[preds]).value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}