{"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 uninstall fastai --yes\n# !pip install --no-index --find-links \"/kaggle/input/modified-siim-helper/\" fastai efficientnet_pytorch\n!pip install -U fastai efficientnet_pytorch","metadata":{"execution":{"iopub.status.busy":"2021-07-21T07:21:39.879658Z","iopub.execute_input":"2021-07-21T07:21:39.880096Z","iopub.status.idle":"2021-07-21T07:22:41.911733Z","shell.execute_reply.started":"2021-07-21T07:21:39.880005Z","shell.execute_reply":"2021-07-21T07:22:41.9107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import fastai\nfastai.__version__","metadata":{"execution":{"iopub.status.busy":"2021-07-21T07:22:41.913681Z","iopub.execute_input":"2021-07-21T07:22:41.914074Z","iopub.status.idle":"2021-07-21T07:22:41.929439Z","shell.execute_reply.started":"2021-07-21T07:22:41.914036Z","shell.execute_reply":"2021-07-21T07:22:41.92822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from effnetv2 import effnetv2_l\nfrom efficientnet_pytorch import EfficientNet\n\n# cnn_model = effnetv2_l(num_classes=2)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T07:22:41.933565Z","iopub.execute_input":"2021-07-21T07:22:41.933876Z","iopub.status.idle":"2021-07-21T07:22:43.559816Z","shell.execute_reply.started":"2021-07-21T07:22:41.93385Z","shell.execute_reply":"2021-07-21T07:22:43.558786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\nimport pathlib\nimport glob\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-21T07:22:43.561567Z","iopub.execute_input":"2021-07-21T07:22:43.561888Z","iopub.status.idle":"2021-07-21T07:22:43.567368Z","shell.execute_reply.started":"2021-07-21T07:22:43.561858Z","shell.execute_reply":"2021-07-21T07:22:43.566507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\n\ndef predict_batch(self, item, rm_type_tfms=None, with_input=False, num_workers=0):\n    dl = self.dls.test_dl(item, rm_type_tfms=rm_type_tfms, num_workers=num_workers)\n    ret = self.get_preds(dl=dl)\n    return ret\n\nLearner.predict_batch = predict_batch","metadata":{"execution":{"iopub.status.busy":"2021-07-21T07:22:43.568825Z","iopub.execute_input":"2021-07-21T07:22:43.569172Z","iopub.status.idle":"2021-07-21T07:22:44.922988Z","shell.execute_reply.started":"2021-07-21T07:22:43.56911Z","shell.execute_reply":"2021-07-21T07:22:44.921828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import torch\n# import torch.nn as nn\n# \n# class CNNModel(nn.Module):\n#     \"\"\"\n#     CNN Neural Network baseline to compare with more advanced NN.\n#     # , where more advanced NN was also a baseline. \n#     \"\"\"\n#     def __init__(self):\n#         super().__init__()\n#         self.net1 = nn.Sequential(\n#             nn.Conv2d(3, 28, 3),\n#             nn.ReLU(),\n#             nn.MaxPool2d(2, 2),\n#             nn.Conv2d(28, 112, 3),\n#             nn.ReLU(),\n#             nn.MaxPool2d(2, 2)\n#         )\n#         self.net2 = nn.Sequential(\n#             nn.Linear(77280, 512),\n#             nn.ReLU(),\n#             nn.Linear(512, 2)\n#         )\n\n#     def forward(self, x):\n#         x = self.net1(x)\n#         x = torch.flatten(x, 1)\n#         # print(x.size()[1])\n#         x = self.net2(x)\n#         return x\n\n\n# cnn_model = CNNModel()","metadata":{"execution":{"iopub.status.busy":"2021-07-21T07:22:44.924631Z","iopub.execute_input":"2021-07-21T07:22:44.924969Z","iopub.status.idle":"2021-07-21T07:22:44.929662Z","shell.execute_reply.started":"2021-07-21T07:22:44.924935Z","shell.execute_reply":"2021-07-21T07:22:44.92876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # cnn_max.pkl file. \n\n# class CNNModel(nn.Module):\n#     \"\"\"\n#     CNN Neural Network baseline to compare with more advanced NN.\n#     # , where more advanced NN was also a baseline. \n#     \"\"\"\n#     def __init__(self):\n#         super().__init__()\n#         self.net1 = nn.Sequential(\n#             nn.Conv2d(3, 32, 4),\n#             nn.ReLU(),\n#             nn.MaxPool2d(2, 2),\n#             nn.Conv2d(32, 112, 4),\n#             nn.ReLU(),\n#             nn.MaxPool2d(2, 2),\n#             nn.Conv2d(112, 256, 4),\n#             nn.ReLU(),\n#             nn.MaxPool2d(2, 2),\n#             nn.Conv2d(256, 512, 4),\n#             nn.ReLU(),\n#             nn.MaxPool2d(2, 2)\n#         )\n#         self.net2 = nn.Sequential(\n#             nn.Linear(4608, 2560),\n#             nn.LeakyReLU(0.0003),\n#             nn.Dropout(0.05),\n#             nn.Linear(2560, 640),\n#             nn.LeakyReLU(0.0003),\n#             nn.Linear(640, 2)\n#         )\n\n#     def forward(self, x):\n#         x = self.net1(x)\n#         x = torch.flatten(x, 1)\n#         # print(x.shape)\n#         x = self.net2(x)\n#         return x\n\n# cnn_model = CNNModel()","metadata":{"execution":{"iopub.status.busy":"2021-07-21T07:22:44.930826Z","iopub.execute_input":"2021-07-21T07:22:44.931296Z","iopub.status.idle":"2021-07-21T07:22:44.94602Z","shell.execute_reply.started":"2021-07-21T07:22:44.931263Z","shell.execute_reply":"2021-07-21T07:22:44.945182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# baselinetrain v10 version. \n\n# import torch\n# import torch.nn as nn\n\n# class CNNModel(nn.Module):\n#     \"\"\"\n#     CNN Neural Network baseline to compare with more advanced NN.\n#     # , where more advanced NN was also a baseline. \n#     \"\"\"\n#     def __init__(self):\n#         super().__init__()\n#         self.net1 = nn.Sequential(\n#             nn.Conv2d(3, 28, 4),\n#             nn.ReLU(),\n#             nn.MaxPool2d(2, 2),\n#             nn.Conv2d(28, 112, 4),\n#             nn.ReLU(),\n#             nn.MaxPool2d(2, 2),\n#             nn.Conv2d(112, 256, 4),\n#             nn.ReLU(),\n#             nn.MaxPool2d(2, 2), \n#             nn.Conv2d(256, 512, 4),\n#             nn.ReLU(),\n#             nn.MaxPool2d(2, 2)\n#         )\n#         self.net2 = nn.Sequential(\n#             nn.Linear(4608, 2048),\n#             nn.LeakyReLU(0.0003),\n#             nn.Dropout(0.05),\n#             nn.Linear(2048, 512),\n#             nn.LeakyReLU(0.0003),\n#             nn.Linear(512, 2)\n#         )\n\n#     def forward(self, x):\n#         x = self.net1(x)\n#         x = torch.flatten(x, 1)\n# #         print(x.size()[1])\n#         x = self.net2(x)\n#         return x\n    \n# cnn_model = CNNModel()","metadata":{"execution":{"iopub.status.busy":"2021-07-21T07:22:44.947822Z","iopub.execute_input":"2021-07-21T07:22:44.94833Z","iopub.status.idle":"2021-07-21T07:22:44.957303Z","shell.execute_reply.started":"2021-07-21T07:22:44.948297Z","shell.execute_reply":"2021-07-21T07:22:44.956239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Efficientnet B4 (effb7_ft5.pkl)  ==> INCORRECT NAMING USED DUE TO CONFUSION...\n\nclass EffNetModel(nn.Module):\n    def __init__(self):\n        super(EffNetModel, self).__init__()\n        self.net = EfficientNet.from_pretrained(\"efficientnet-b4\")\n        n_features = self.net._fc.in_features\n        self.net._fc = nn.Linear(in_features=n_features, out_features=2, bias=True)\n        \n    def forward(self, x):\n        out = self.net(x)\n        return out\n\n\ncnn_model = EffNetModel()\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_learner(\"../input/models-g2net/effnet_folds/effnet_folds/effb7_ft5.pkl\")\n\ntest_path = [pathlib.Path(file) for file in sorted(glob.glob(\"../input/g2net-as-image/test/*\"))]\ntest_path_pd = [file.split(\"/\")[-1].split(\".\")[0] for file in sorted(glob.glob(\"../input/g2net-as-image/test/*\"))]\ntest_path_pd[:5]","metadata":{"execution":{"iopub.status.busy":"2021-07-21T07:22:44.958746Z","iopub.execute_input":"2021-07-21T07:22:44.959223Z","iopub.status.idle":"2021-07-21T07:23:02.907034Z","shell.execute_reply.started":"2021-07-21T07:22:44.95919Z","shell.execute_reply":"2021-07-21T07:23:02.905952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.dls.vocab","metadata":{"execution":{"iopub.status.busy":"2021-07-21T07:23:30.346228Z","iopub.execute_input":"2021-07-21T07:23:30.346603Z","iopub.status.idle":"2021-07-21T07:23:30.353358Z","shell.execute_reply.started":"2021-07-21T07:23:30.346572Z","shell.execute_reply":"2021-07-21T07:23:30.352274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf = pd.DataFrame([test_path_pd, model.predict_batch(test_path, num_workers=os.cpu_count())[0][:, 0].numpy()]).T\ndf.columns = [\"id\", \"target\"]\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-21T07:23:32.768049Z","iopub.execute_input":"2021-07-21T07:23:32.768458Z","iopub.status.idle":"2021-07-21T07:24:14.928456Z","shell.execute_reply.started":"2021-07-21T07:23:32.768417Z","shell.execute_reply":"2021-07-21T07:24:14.925479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T02:04:28.595735Z","iopub.status.idle":"2021-07-10T02:04:28.596151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}