{"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":"**Note**\n\n* Stage 1 - Training: https://www.kaggle.com/code/bibanh/simple-submission-classification-task-training\n","metadata":{}},{"cell_type":"markdown","source":"# 1. Define Some Libraries","metadata":{}},{"cell_type":"code","source":"import torch\nimport torchvision\nimport torch.utils.data\nfrom torchvision import transforms\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-22T04:38:03.900736Z","iopub.execute_input":"2023-03-22T04:38:03.901125Z","iopub.status.idle":"2023-03-22T04:38:03.906743Z","shell.execute_reply.started":"2023-03-22T04:38:03.901093Z","shell.execute_reply":"2023-03-22T04:38:03.905690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder, LabelEncoder\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.metrics import roc_auc_score\nimport numpy as np\nimport math, random\nimport pandas as pd\nimport pathlib\nimport glob\nimport cv2 as cv\nimport numpy as np\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport random\nfrom sklearn.model_selection import train_test_split\nimport copy\nimport time \nfrom collections import defaultdict\nfrom tqdm import tqdm\nimport os\nimport json\n","metadata":{"execution":{"iopub.status.busy":"2023-03-22T04:38:03.911361Z","iopub.execute_input":"2023-03-22T04:38:03.912119Z","iopub.status.idle":"2023-03-22T04:38:03.921310Z","shell.execute_reply.started":"2023-03-22T04:38:03.912089Z","shell.execute_reply":"2023-03-22T04:38:03.920300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    isOneHot = False\n    label_map = {'dot': 0, 'horizontal_bar' : 1, 'vertical_bar': 2, 'line': 3, 'scatter': 4}\n    num_classes = 5\n    batchSize = 32\n    fold_train = 0","metadata":{"execution":{"iopub.status.busy":"2023-03-22T04:38:03.924214Z","iopub.execute_input":"2023-03-22T04:38:03.925325Z","iopub.status.idle":"2023-03-22T04:38:03.931473Z","shell.execute_reply.started":"2023-03-22T04:38:03.925287Z","shell.execute_reply":"2023-03-22T04:38:03.930508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. MetaData for Testing","metadata":{}},{"cell_type":"code","source":"files_and_labels = {}\nfor dirname, _, filenames in os.walk('/kaggle/input/benetech-making-graphs-accessible/test/images'):\n    for filename in filenames:\n        files_and_labels[os.path.join(dirname, filename)] = 'line'\ndf_test = pd.DataFrame(files_and_labels, index = [0]).T.reset_index().rename(columns = {'index':'filename', 0 :'chart_type'})\ndf_test","metadata":{"execution":{"iopub.status.busy":"2023-03-22T04:38:03.933782Z","iopub.execute_input":"2023-03-22T04:38:03.934418Z","iopub.status.idle":"2023-03-22T04:38:03.952738Z","shell.execute_reply.started":"2023-03-22T04:38:03.934375Z","shell.execute_reply":"2023-03-22T04:38:03.951539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Define Dataset","metadata":{}},{"cell_type":"code","source":"transforms_train = transforms.Compose(\n    [transforms.ToTensor(),\n     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])\ntransforms_test = transforms.Compose(\n    [transforms.ToTensor(),\n     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])","metadata":{"execution":{"iopub.status.busy":"2023-03-22T04:38:03.955108Z","iopub.execute_input":"2023-03-22T04:38:03.955769Z","iopub.status.idle":"2023-03-22T04:38:03.963114Z","shell.execute_reply.started":"2023-03-22T04:38:03.955732Z","shell.execute_reply":"2023-03-22T04:38:03.961824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ImageCharts(torch.utils.data.Dataset):\n    def __init__(self, df, transforms  = None): ### phase = train/test\n        self.df = df\n        self.transforms = transforms\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self,idx):\n        name_img = self.df.iloc[idx]['filename']\n        img = cv.imread(name_img)\n        img = cv.cvtColor(img,cv.COLOR_BGR2RGB)\n        img = cv.resize(img,(500,300))\n        img = img.astype(np.float32)/255.0\n\n        if(self.transforms is not None):\n            img = self.transforms(img)\n        return img, np.array(name_img.split('/')[-1][:-4]).tobytes()","metadata":{"execution":{"iopub.status.busy":"2023-03-22T04:38:03.964760Z","iopub.execute_input":"2023-03-22T04:38:03.965977Z","iopub.status.idle":"2023-03-22T04:38:03.975219Z","shell.execute_reply.started":"2023-03-22T04:38:03.965891Z","shell.execute_reply":"2023-03-22T04:38:03.974211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = ImageCharts(df_test, transforms=transforms_train)\n\ntest_dl = torch.utils.data.DataLoader(test_ds, batch_size= CFG.batchSize, shuffle=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-22T04:38:03.977892Z","iopub.execute_input":"2023-03-22T04:38:03.978306Z","iopub.status.idle":"2023-03-22T04:38:03.986420Z","shell.execute_reply.started":"2023-03-22T04:38:03.978226Z","shell.execute_reply":"2023-03-22T04:38:03.985397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a,b = next(iter(test_dl))\nprint(a.shape)\nprint(''.join(list(np.frombuffer(b[0], dtype = np.dtype('<U4')))))","metadata":{"execution":{"iopub.status.busy":"2023-03-22T04:38:03.988606Z","iopub.execute_input":"2023-03-22T04:38:03.988889Z","iopub.status.idle":"2023-03-22T04:38:04.046534Z","shell.execute_reply.started":"2023-03-22T04:38:03.988863Z","shell.execute_reply":"2023-03-22T04:38:04.045266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Load Pretrained Model","metadata":{}},{"cell_type":"code","source":"model = torchvision.models.resnet50(pretrained=False)\n\nnum_features = model.fc.in_features\nmodel.fc = nn.Linear(num_features, CFG.num_classes)\nmodel.load_state_dict(torch.load('/kaggle/input/benetech-classification-pretrained/Benetech _ResNet50_fold0.pth'))\n\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\nmyModel = model.to(device)\nnext(myModel.parameters()).device","metadata":{"execution":{"iopub.status.busy":"2023-03-22T04:38:04.048287Z","iopub.execute_input":"2023-03-22T04:38:04.049175Z","iopub.status.idle":"2023-03-22T04:38:04.861530Z","shell.execute_reply.started":"2023-03-22T04:38:04.049132Z","shell.execute_reply":"2023-03-22T04:38:04.860374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Inference","metadata":{}},{"cell_type":"code","source":"sample_submission = pd.read_csv('/kaggle/input/benetech-making-graphs-accessible/sample_submission.csv')\nmyModel.eval()\nwith torch.no_grad():\n    correct = 0\n    total = 0\n    val_loss = 0 \n    for idx, data_ in enumerate(test_dl):\n        inputs, filename = data_[0].to(device), data_[1]\n        inputs_m, inputs_s = inputs.mean(), inputs.std()\n        inputs = (inputs - inputs_m) / inputs_s\n\n        outputs = model(inputs)\n        _, prediction = torch.max(outputs,1)\n        prediction = prediction.detach().cpu().numpy()\n        files = [''.join(list(np.frombuffer(f, dtype = np.dtype('<U4')))) for f in filename]\n        for idx, f in enumerate(files):\n            sample_submission.loc[sample_submission.id == f + '_x', 'chart_type'] = list(CFG.label_map)[prediction[idx]]\n            sample_submission.loc[sample_submission.id == f + '_y', 'chart_type'] = list(CFG.label_map)[prediction[idx]]\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2023-03-22T04:58:31.975025Z","iopub.execute_input":"2023-03-22T04:58:31.975741Z","iopub.status.idle":"2023-03-22T04:58:32.067168Z","shell.execute_reply.started":"2023-03-22T04:58:31.975692Z","shell.execute_reply":"2023-03-22T04:58:32.066076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-03-22T04:38:04.956281Z","iopub.execute_input":"2023-03-22T04:38:04.957123Z","iopub.status.idle":"2023-03-22T04:38:04.964961Z","shell.execute_reply.started":"2023-03-22T04:38:04.957084Z","shell.execute_reply":"2023-03-22T04:38:04.963976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-03-22T04:38:33.208468Z","iopub.execute_input":"2023-03-22T04:38:33.208924Z","iopub.status.idle":"2023-03-22T04:38:33.228347Z","shell.execute_reply.started":"2023-03-22T04:38:33.208884Z","shell.execute_reply":"2023-03-22T04:38:33.226813Z"},"trusted":true},"execution_count":null,"outputs":[]}]}