{"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 timm # install pytorch image models\n!pip install torchmetrics","metadata":{"execution":{"iopub.status.busy":"2022-06-13T10:10:50.916020Z","iopub.execute_input":"2022-06-13T10:10:50.916400Z","iopub.status.idle":"2022-06-13T10:11:11.950892Z","shell.execute_reply.started":"2022-06-13T10:10:50.916367Z","shell.execute_reply":"2022-06-13T10:11:11.949879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport pandas as pd\nimport torchvision.models as models\nimport timm\nimport albumentations as A\nimport cv2\nimport numpy as np\nimport tensorflow as tf\n\nfrom torch import nn\nfrom  torch.cuda.amp import autocast, GradScaler\nfrom torch.utils.data import Dataset, DataLoader\nfrom tqdm import tqdm\nfrom torchvision import transforms","metadata":{"execution":{"iopub.status.busy":"2022-06-13T10:11:11.953018Z","iopub.execute_input":"2022-06-13T10:11:11.953505Z","iopub.status.idle":"2022-06-13T10:11:18.946980Z","shell.execute_reply.started":"2022-06-13T10:11:11.953463Z","shell.execute_reply":"2022-06-13T10:11:18.946112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomModel(torch.nn.Module): \n    def __init__(self, model_backbone):\n        super(CustomModel,self).__init__()\n        self.model = model_backbone\n        self.num_in_features = self.model.get_classifier().in_features\n        print(self.num_in_features)\n        self.model.classifier = nn.Sequential(\n            nn.BatchNorm1d(self.num_in_features),\n            nn.Linear(self.num_in_features, 512),\n            nn.Dropout(0.5),\n            nn.ReLU(inplace=True),\n            nn.Linear(512, 100),\n        )\n    def forward(self,x):\n        x = self.model(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-06-13T10:44:39.240678Z","iopub.execute_input":"2022-06-13T10:44:39.241324Z","iopub.status.idle":"2022-06-13T10:44:39.247859Z","shell.execute_reply.started":"2022-06-13T10:44:39.241286Z","shell.execute_reply":"2022-06-13T10:44:39.247082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SorghumDataset(Dataset):\n    def __init__(self, dirs, labels, transformation=None):\n        super(SorghumDataset,self).__init__()\n        self.dirs = dirs\n        self.labels = labels\n        self.transformation = transformation\n    def __len__(self):\n        return len(self.dirs)\n\n    def __getitem__(self, index):\n        image = cv2.imread(self.dirs[index])\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        label = self.labels[index] # need to one hot encoding here\n        \n        image = np.array(image)\n\n        if self.transformation:\n            aug_image = self.transformation(image=image)\n            image = aug_image['image']\n            \n        image = image / 255.\n        image = image.transpose((2, 0, 1))\n        \n        image = torch.from_numpy(image).type(torch.float32)\n        image = transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))(image)\n        \n        labels = torch.from_numpy(np.array(self.labels[index])).type(torch.float32)\n\n\n        return image, labels","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-13T10:44:40.255460Z","iopub.execute_input":"2022-06-13T10:44:40.256114Z","iopub.status.idle":"2022-06-13T10:44:40.264686Z","shell.execute_reply.started":"2022-06-13T10:44:40.256076Z","shell.execute_reply":"2022-06-13T10:44:40.263941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resnet","metadata":{}},{"cell_type":"code","source":"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n\nmodel_name = 'resnext50d_32x4d'\n\nbackbone = timm.create_model(model_name,pretrained=True)\nmodel = CustomModel(backbone)\n\nmodel.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T06:24:01.996426Z","iopub.execute_input":"2022-06-13T06:24:01.996967Z","iopub.status.idle":"2022-06-13T06:24:11.531152Z","shell.execute_reply.started":"2022-06-13T06:24:01.996934Z","shell.execute_reply":"2022-06-13T06:24:11.530380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint = torch.load('../input/sorghum-efficientnetv2-0-846-private-lb/resnext50d_32x4d_best.pt')\nmodel.load_state_dict(checkpoint['model_state_dict'])","metadata":{"execution":{"iopub.status.busy":"2022-06-13T06:24:11.532318Z","iopub.execute_input":"2022-06-13T06:24:11.534056Z","iopub.status.idle":"2022-06-13T06:24:15.088344Z","shell.execute_reply.started":"2022-06-13T06:24:11.534007Z","shell.execute_reply":"2022-06-13T06:24:15.087552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('../input/sorghum-id-fgvc-9/sample_submission.csv')\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T10:44:52.431306Z","iopub.execute_input":"2022-06-13T10:44:52.432029Z","iopub.status.idle":"2022-06-13T10:44:52.466459Z","shell.execute_reply.started":"2022-06-13T10:44:52.431988Z","shell.execute_reply":"2022-06-13T10:44:52.465728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub[\"filename\"] = sub[\"filename\"].apply(lambda image: '../input/sorghum-id-fgvc-9/test/' + image)\nsub[\"cultivar\"] = 0\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T10:45:01.111303Z","iopub.execute_input":"2022-06-13T10:45:01.112143Z","iopub.status.idle":"2022-06-13T10:45:01.132892Z","shell.execute_reply.started":"2022-06-13T10:45:01.112104Z","shell.execute_reply":"2022-06-13T10:45:01.132061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_transformation = A.Compose([\n    A.Resize(width=512, height=512, p=1.0)\n])\n\ntesting_dataset = SorghumDataset(sub['filename'], sub['cultivar'], validation_transformation)\ntesting_dataloader = DataLoader(testing_dataset, \n                                batch_size=32, \n                                shuffle=False, \n                                num_workers=1)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T06:24:15.156118Z","iopub.execute_input":"2022-06-13T06:24:15.156521Z","iopub.status.idle":"2022-06-13T06:24:15.162952Z","shell.execute_reply.started":"2022-06-13T06:24:15.156478Z","shell.execute_reply":"2022-06-13T06:24:15.161107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\ncnt = 0\n\nresnet_preds = []\n\nwith torch.no_grad():\n    for image, label in tqdm(testing_dataloader):\n        image = image.to(device)\n        outputs = model(image)\n        for i in range(len(outputs)):\n            resnet_preds.append(outputs[i][:100])\n#         resnet_preds.append(outputs[0][:100])\n        preds = outputs.detach().cpu()\n        predictions.append(preds.argmax(1))","metadata":{"execution":{"iopub.status.busy":"2022-06-13T06:24:15.164744Z","iopub.execute_input":"2022-06-13T06:24:15.165342Z","iopub.status.idle":"2022-06-13T06:31:15.429809Z","shell.execute_reply.started":"2022-06-13T06:24:15.165269Z","shell.execute_reply":"2022-06-13T06:31:15.428459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_preds_ = []\n\nfor i in range(len(resnet_preds)):\n    resnet_preds_.append(resnet_preds[i].tolist())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_preds_list = pd.DataFrame({'0_class':[]})\n\nfor i in range(99, 0, -1): \n    resnet_preds_list.insert(1, \"{}_class\".format(i), [])\n\nfor i in range(len(resnet_preds_)):\n    resnet_preds_list.loc[i] = resnet_preds_[i]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_preds_list.to_csv('resnet_submission.csv', index=False)\n\nresnet_preds_list.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EfficientNetB5","metadata":{}},{"cell_type":"code","source":"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n\nmodel_name = 'tf_efficientnetv2_m_in21k'\n\nbackbone = timm.create_model(model_name,pretrained=True)\nB5_model = CustomModel(backbone)\n\nB5_model.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T06:40:50.446510Z","iopub.execute_input":"2022-06-13T06:40:50.447489Z","iopub.status.idle":"2022-06-13T06:40:52.726014Z","shell.execute_reply.started":"2022-06-13T06:40:50.447442Z","shell.execute_reply":"2022-06-13T06:40:52.725116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint = torch.load('../input/sorghum-identification-12345/tf_efficientnetv2_m_in21k_best.pt')\nB5_model.load_state_dict(checkpoint['model_state_dict'])","metadata":{"execution":{"iopub.status.busy":"2022-06-13T06:40:57.848900Z","iopub.execute_input":"2022-06-13T06:40:57.849255Z","iopub.status.idle":"2022-06-13T06:40:58.897386Z","shell.execute_reply.started":"2022-06-13T06:40:57.849225Z","shell.execute_reply":"2022-06-13T06:40:58.896606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\ncnt = 0\n\nB5_preds = []\n\nwith torch.no_grad():\n    for image, label in tqdm(testing_dataloader):\n        image = image.to(device)\n        outputs = model(image)\n        for i in range(len(outputs)):\n            B5_preds.append(outputs[i][:100])\n#         resnet_preds.append(outputs[0][:100])\n        preds = outputs.detach().cpu()\n        predictions.append(preds.argmax(1))","metadata":{"execution":{"iopub.status.busy":"2022-06-13T06:41:00.288934Z","iopub.execute_input":"2022-06-13T06:41:00.289652Z","iopub.status.idle":"2022-06-13T07:06:36.982450Z","shell.execute_reply.started":"2022-06-13T06:41:00.289616Z","shell.execute_reply":"2022-06-13T07:06:36.981395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"B5_preds_ = []\n\nfor i in range(len(B5_preds)):\n    B5_preds_.append(B5_preds[i].tolist())","metadata":{"execution":{"iopub.status.busy":"2022-06-13T07:53:57.620245Z","iopub.execute_input":"2022-06-13T07:53:57.620604Z","iopub.status.idle":"2022-06-13T07:53:58.143399Z","shell.execute_reply.started":"2022-06-13T07:53:57.620573Z","shell.execute_reply":"2022-06-13T07:53:58.142586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"B5_preds_list = pd.DataFrame({'0_class':[]})\n\nfor i in range(99, 0, -1): \n    B5_preds_list.insert(1, \"{}_class\".format(i), [])\n\nfor i in range(len(B5_preds_)):\n    B5_preds_list.loc[i] = B5_preds_[i]","metadata":{"execution":{"iopub.status.busy":"2022-06-13T08:03:10.005320Z","iopub.execute_input":"2022-06-13T08:03:10.005741Z","iopub.status.idle":"2022-06-13T08:06:57.952499Z","shell.execute_reply.started":"2022-06-13T08:03:10.005704Z","shell.execute_reply":"2022-06-13T08:06:57.951637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"B5_preds_list","metadata":{"execution":{"iopub.status.busy":"2022-06-13T08:07:02.299669Z","iopub.execute_input":"2022-06-13T08:07:02.300470Z","iopub.status.idle":"2022-06-13T08:07:02.340520Z","shell.execute_reply.started":"2022-06-13T08:07:02.300432Z","shell.execute_reply":"2022-06-13T08:07:02.339612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"B5_preds_list.to_csv('B5_submission.csv', index=False)\n\nB5_preds_list.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T08:09:39.082302Z","iopub.execute_input":"2022-06-13T08:09:39.082678Z","iopub.status.idle":"2022-06-13T08:09:43.235313Z","shell.execute_reply.started":"2022-06-13T08:09:39.082646Z","shell.execute_reply":"2022-06-13T08:09:43.234500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EfficientNetB4","metadata":{}},{"cell_type":"code","source":"!pip install efficientnet_pytorch","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from efficientnet_pytorch import EfficientNet\n\nmodel_B4 = EfficientNet.from_name('efficientnet-b4')\nmodel_B4.load_state_dict(torch.load(\"../input/test-for-kaggle-0426/epoch25.pt\", map_location='cuda'))\n\nmodel_B4.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T09:52:16.497499Z","iopub.execute_input":"2022-06-13T09:52:16.498504Z","iopub.status.idle":"2022-06-13T09:52:16.585892Z","shell.execute_reply.started":"2022-06-13T09:52:16.498394Z","shell.execute_reply":"2022-06-13T09:52:16.584781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\ncnt = 0\n\nB4_preds = []\n\nwith torch.no_grad():\n    for image, label in tqdm(testing_dataloader):\n        image = image.to(device)\n        outputs = model_B4(image)\n        for i in range(len(outputs)):\n            B4_preds.append(outputs[i][:100])\n#         resnet_preds.append(outputs[0][:100])\n        preds = outputs.detach().cpu()\n        predictions.append(preds.argmax(1))","metadata":{"execution":{"iopub.status.busy":"2022-06-12T12:55:06.628565Z","iopub.execute_input":"2022-06-12T12:55:06.628970Z","iopub.status.idle":"2022-06-12T12:55:12.673378Z","shell.execute_reply.started":"2022-06-12T12:55:06.628936Z","shell.execute_reply":"2022-06-12T12:55:12.672116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"B4_preds_ = []\n\nfor i in range(len(B4_preds)):\n    B4_preds_.append(B4_preds[i].tolist())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"B4_preds_list = pd.DataFrame({'0_class':[]})\n\nfor i in range(99, 0, -1): \n    B4_preds_list.insert(1, \"{}_class\".format(i), [])\n\nfor i in range(len(B5_preds_)):\n    B4_preds_list.loc[i] = B4_preds_[i]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"B4_preds_list.to_csv('B4_submission.csv', index=False)\n\nB4_preds_list.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Ensemble Learning (b4+b5+resnet)","metadata":{}},{"cell_type":"code","source":"resnet_result = pd.read_csv('../input/resnet-submission/resnet_submission.csv')\n\nresnet_result.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T12:00:06.656549Z","iopub.execute_input":"2022-06-13T12:00:06.656934Z","iopub.status.idle":"2022-06-13T12:00:07.210572Z","shell.execute_reply.started":"2022-06-13T12:00:06.656902Z","shell.execute_reply":"2022-06-13T12:00:07.209686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b5_result = pd.read_csv('../input/b5-sub/B5_submission.csv')\n\nb5_result = b5_result * 10\n\nb5_result.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T12:00:07.213220Z","iopub.execute_input":"2022-06-13T12:00:07.213604Z","iopub.status.idle":"2022-06-13T12:00:07.799053Z","shell.execute_reply.started":"2022-06-13T12:00:07.213567Z","shell.execute_reply":"2022-06-13T12:00:07.797238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b4_result = pd.read_csv('../input/b4-sub/B4_submission.csv')\n\n# b4_result = b4_result * 10\n\nb4_result.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T12:00:08.439088Z","iopub.execute_input":"2022-06-13T12:00:08.439647Z","iopub.status.idle":"2022-06-13T12:00:08.989578Z","shell.execute_reply.started":"2022-06-13T12:00:08.439612Z","shell.execute_reply":"2022-06-13T12:00:08.988807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b5_result['max_value'] = b5_result.max(axis=1)\nb5_result['class'] = b5_result.idxmax(axis=1)\n\nresnet_result['max_value'] = resnet_result.max(axis=1)\nresnet_result['class'] = resnet_result.idxmax(axis=1)\n\nb4_result['max_value'] = b4_result.max(axis=1)\nb4_result['class'] = b4_result.idxmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T12:00:11.490682Z","iopub.execute_input":"2022-06-13T12:00:11.491538Z","iopub.status.idle":"2022-06-13T12:00:11.692366Z","shell.execute_reply.started":"2022-06-13T12:00:11.491498Z","shell.execute_reply":"2022-06-13T12:00:11.691610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b5_result.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T12:00:23.633703Z","iopub.execute_input":"2022-06-13T12:00:23.634334Z","iopub.status.idle":"2022-06-13T12:00:23.657893Z","shell.execute_reply.started":"2022-06-13T12:00:23.634299Z","shell.execute_reply":"2022-06-13T12:00:23.656958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_result.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T12:00:28.109270Z","iopub.execute_input":"2022-06-13T12:00:28.109693Z","iopub.status.idle":"2022-06-13T12:00:28.142536Z","shell.execute_reply.started":"2022-06-13T12:00:28.109655Z","shell.execute_reply":"2022-06-13T12:00:28.141760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b4_result.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T12:00:32.687139Z","iopub.execute_input":"2022-06-13T12:00:32.687514Z","iopub.status.idle":"2022-06-13T12:00:32.712114Z","shell.execute_reply.started":"2022-06-13T12:00:32.687483Z","shell.execute_reply":"2022-06-13T12:00:32.710869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = []\n\nfor i in range(len(b5_result)):\n    max_val = max((b5_result['max_value'][i] * 0.3),(resnet_result['max_value'][i] * 1.7),(b4_result['max_value'][i] * 0.5))\n    \n    if max_val == (b5_result['max_value'][i] * 0.3):\n        result.append((max_val, b5_result['class'][i]))\n        \n    elif max_val == (resnet_result['max_value'][i] * 1.7):\n        result.append((max_val, resnet_result['class'][i]))\n        \n    else:\n        result.append((max_val, b4_result['class'][i]))","metadata":{"execution":{"iopub.status.busy":"2022-06-13T11:53:47.551325Z","iopub.execute_input":"2022-06-13T11:53:47.552088Z","iopub.status.idle":"2022-06-13T11:53:48.584646Z","shell.execute_reply.started":"2022-06-13T11:53:47.552049Z","shell.execute_reply":"2022-06-13T11:53:48.583755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result[:5]","metadata":{"execution":{"iopub.status.busy":"2022-06-13T11:53:50.006708Z","iopub.execute_input":"2022-06-13T11:53:50.007497Z","iopub.status.idle":"2022-06-13T11:53:50.012974Z","shell.execute_reply.started":"2022-06-13T11:53:50.007460Z","shell.execute_reply":"2022-06-13T11:53:50.012211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\n \ntmp = []\n\nfor i in range(len(result)):\n    tmp.append(int(re.sub(r'[^0-9]', '', result[i][1])))\n    \nprint(tmp)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T11:53:51.722396Z","iopub.execute_input":"2022-06-13T11:53:51.723289Z","iopub.status.idle":"2022-06-13T11:53:51.812730Z","shell.execute_reply.started":"2022-06-13T11:53:51.723248Z","shell.execute_reply":"2022-06-13T11:53:51.809896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all = pd.read_csv('../input/sorghum-id-fgvc-9/train_cultivar_mapping.csv')\ndf_all.dropna(inplace=True)\n\nunique_cultivars = list(df_all[\"cultivar\"].unique())\n\npredictions = [unique_cultivars[pred] for pred in tmp]","metadata":{"execution":{"iopub.status.busy":"2022-06-13T11:53:54.170885Z","iopub.execute_input":"2022-06-13T11:53:54.171593Z","iopub.status.idle":"2022-06-13T11:53:54.205708Z","shell.execute_reply.started":"2022-06-13T11:53:54.171554Z","shell.execute_reply":"2022-06-13T11:53:54.204954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('../input/sorghum-id-fgvc-9/sample_submission.csv')\nsub['cultivar'] = predictions\nsub.to_csv('submission5.csv', index=False)\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T11:53:59.463536Z","iopub.execute_input":"2022-06-13T11:53:59.463915Z","iopub.status.idle":"2022-06-13T11:53:59.542305Z","shell.execute_reply.started":"2022-06-13T11:53:59.463880Z","shell.execute_reply":"2022-06-13T11:53:59.541582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}