{"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":"import json \nimport os\nimport pandas as pd \nfrom pathlib import Path\nimport random\n\nfrom fastai import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-14T18:02:02.835079Z","iopub.execute_input":"2023-05-14T18:02:02.835732Z","iopub.status.idle":"2023-05-14T18:02:02.849376Z","shell.execute_reply.started":"2023-05-14T18:02:02.835697Z","shell.execute_reply":"2023-05-14T18:02:02.848490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_path = Path(\"/kaggle/input/benetech-making-graphs-accessible/train/images\")\nannotations_path  = Path(\"/kaggle/input/benetech-making-graphs-accessible/train/annotations\")\ntest_images_path  = Path(\"/kaggle/input/benetech-making-graphs-accessible/test/images\")","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:02:02.850858Z","iopub.execute_input":"2023-05-14T18:02:02.851486Z","iopub.status.idle":"2023-05-14T18:02:02.860203Z","shell.execute_reply.started":"2023-05-14T18:02:02.851452Z","shell.execute_reply":"2023-05-14T18:02:02.859399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nested_dict = dict()\nfor file_name in os.listdir(annotations_path):\n    image_idx = file_name.split('.')[0]\n    nested_dict[image_idx] = json.load(open(annotations_path/file_name))\n    \ndf= pd.DataFrame.from_dict(nested_dict, orient=\"index\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:02:02.861564Z","iopub.execute_input":"2023-05-14T18:02:02.862149Z","iopub.status.idle":"2023-05-14T18:06:05.654663Z","shell.execute_reply.started":"2023-05-14T18:02:02.862117Z","shell.execute_reply":"2023-05-14T18:06:05.653595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.rename(columns = {'chart-type':'chart_type',\n                     'plot-bb'   :'plot_bb' ,\n                      'data-series':'data_series',\n                     'axes':'xy_axes'} , inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:06:05.658085Z","iopub.execute_input":"2023-05-14T18:06:05.658829Z","iopub.status.idle":"2023-05-14T18:06:05.665401Z","shell.execute_reply.started":"2023-05-14T18:06:05.658773Z","shell.execute_reply":"2023-05-14T18:06:05.664185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=df.reset_index()\ndf['fname'] = df['index'].map(lambda x: x + '.jpg')\ndf = df.set_index('index')\ndf.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:06:05.667010Z","iopub.execute_input":"2023-05-14T18:06:05.667768Z","iopub.status.idle":"2023-05-14T18:06:05.950266Z","shell.execute_reply.started":"2023-05-14T18:06:05.667726Z","shell.execute_reply":"2023-05-14T18:06:05.949314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.chart_type.unique()","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:06:05.954539Z","iopub.execute_input":"2023-05-14T18:06:05.956972Z","iopub.status.idle":"2023-05-14T18:06:05.988319Z","shell.execute_reply.started":"2023-05-14T18:06:05.956932Z","shell.execute_reply":"2023-05-14T18:06:05.987466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vbar_idx    = df[df.chart_type == 'vertical_bar'].index\nhbar_idx    = df[df.chart_type == 'horizontal_bar'].index\nline_idx    = df[df.chart_type == 'line'].index\nscatter_idx = df[df.chart_type == 'scatter'].index\ndot_idx     = df[df.chart_type == 'dot'].index","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:06:05.992583Z","iopub.execute_input":"2023-05-14T18:06:05.994848Z","iopub.status.idle":"2023-05-14T18:06:06.150026Z","shell.execute_reply.started":"2023-05-14T18:06:05.994808Z","shell.execute_reply":"2023-05-14T18:06:06.149068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vbar_idx_val    = random.sample(set(vbar_idx) , int(len(vbar_idx)*0.2))\nhbar_idx_val    = random.sample(set(hbar_idx) , int(len(hbar_idx)*0.2))\nline_idx_val    = random.sample(set(line_idx) , int(len(line_idx)*0.2))\nscatter_idx_val = random.sample(set(scatter_idx) , int(len(scatter_idx)*0.2))\ndot_idx_val     = random.sample(set(dot_idx), int(len(dot_idx)*0.2))","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:06:06.154803Z","iopub.execute_input":"2023-05-14T18:06:06.157027Z","iopub.status.idle":"2023-05-14T18:06:06.216866Z","shell.execute_reply.started":"2023-05-14T18:06:06.156989Z","shell.execute_reply":"2023-05-14T18:06:06.216009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_idx = vbar_idx_val + hbar_idx_val + line_idx_val + scatter_idx_val + dot_idx_val\n\ndf['is_valid'] = False\nfor idx in val_idx:\n    df.loc[idx,'is_valid'] = True\n    \ndf.is_valid.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:06:06.220887Z","iopub.execute_input":"2023-05-14T18:06:06.223227Z","iopub.status.idle":"2023-05-14T18:06:07.586764Z","shell.execute_reply.started":"2023-05-14T18:06:06.223191Z","shell.execute_reply":"2023-05-14T18:06:07.585890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = df[['fname','chart_type','is_valid']]\ndata = data.rename(columns={'chart_type':'label'})\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:06:07.590904Z","iopub.execute_input":"2023-05-14T18:06:07.593145Z","iopub.status.idle":"2023-05-14T18:06:07.620531Z","shell.execute_reply.started":"2023-05-14T18:06:07.593110Z","shell.execute_reply":"2023-05-14T18:06:07.619592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:06:07.624911Z","iopub.execute_input":"2023-05-14T18:06:07.627370Z","iopub.status.idle":"2023-05-14T18:06:13.359553Z","shell.execute_reply.started":"2023-05-14T18:06:07.627317Z","shell.execute_reply":"2023-05-14T18:06:13.358539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#path = \"C:\\\\Users\\\\ASUS TUF GAMING\\\\Documents\\\\benetech_competition\\\\Data\\\\kaggle_data\\\\train\"\ndls = ImageDataLoaders.from_df(data, train_images_path , valid_col='is_valid' , item_tfms=Resize(224))","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:06:13.364542Z","iopub.execute_input":"2023-05-14T18:06:13.366896Z","iopub.status.idle":"2023-05-14T18:06:24.892234Z","shell.execute_reply.started":"2023-05-14T18:06:13.366856Z","shell.execute_reply":"2023-05-14T18:06:24.891240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch(max_n=9)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:06:24.899380Z","iopub.execute_input":"2023-05-14T18:06:24.901675Z","iopub.status.idle":"2023-05-14T18:06:27.298677Z","shell.execute_reply.started":"2023-05-14T18:06:24.901638Z","shell.execute_reply":"2023-05-14T18:06:27.297785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.valid.show_batch(max_n=6)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:06:27.299893Z","iopub.execute_input":"2023-05-14T18:06:27.300308Z","iopub.status.idle":"2023-05-14T18:06:28.927982Z","shell.execute_reply.started":"2023-05-14T18:06:27.300271Z","shell.execute_reply":"2023-05-14T18:06:28.926933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet34, metrics=error_rate)\nlearn.fine_tune(1)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:06:28.932414Z","iopub.execute_input":"2023-05-14T18:06:28.933136Z","iopub.status.idle":"2023-05-14T18:22:36.732300Z","shell.execute_reply.started":"2023-05-14T18:06:28.933097Z","shell.execute_reply":"2023-05-14T18:22:36.731089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_image = Image.open(\"/kaggle/input/benetech-making-graphs-accessible/test/images/000b92c3b098.jpg\")\nexample_image","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:27:06.407592Z","iopub.execute_input":"2023-05-14T18:27:06.408055Z","iopub.status.idle":"2023-05-14T18:27:06.447301Z","shell.execute_reply.started":"2023-05-14T18:27:06.408013Z","shell.execute_reply":"2023-05-14T18:27:06.446394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.predict(\"/kaggle/input/benetech-making-graphs-accessible/test/images/000b92c3b098.jpg\")[0]","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:27:28.827912Z","iopub.execute_input":"2023-05-14T18:27:28.828484Z","iopub.status.idle":"2023-05-14T18:27:28.908706Z","shell.execute_reply.started":"2023-05-14T18:27:28.828441Z","shell.execute_reply":"2023-05-14T18:27:28.907822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"second_example_image = Image.open(\"/kaggle/input/benetech-making-graphs-accessible/test/images/00dcf883a459.jpg\")\nsecond_example_image","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:29:02.667072Z","iopub.execute_input":"2023-05-14T18:29:02.667525Z","iopub.status.idle":"2023-05-14T18:29:02.709023Z","shell.execute_reply.started":"2023-05-14T18:29:02.667489Z","shell.execute_reply":"2023-05-14T18:29:02.708175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.predict(\"/kaggle/input/benetech-making-graphs-accessible/test/images/00dcf883a459.jpg\")[0]","metadata":{"execution":{"iopub.status.busy":"2023-05-14T18:29:09.296302Z","iopub.execute_input":"2023-05-14T18:29:09.296752Z","iopub.status.idle":"2023-05-14T18:29:09.374328Z","shell.execute_reply.started":"2023-05-14T18:29:09.296716Z","shell.execute_reply":"2023-05-14T18:29:09.373443Z"},"trusted":true},"execution_count":null,"outputs":[]}]}