{"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":"# Import","metadata":{"_uuid":"8d5d54fc-692e-4f7f-8c23-565ffaec92b0","_cell_guid":"7ae795a0-e429-449d-a2e7-6f268f48c2a5","papermill":{"duration":0.020903,"end_time":"2021-04-06T10:10:02.343521","exception":false,"start_time":"2021-04-06T10:10:02.322618","status":"completed"},"tags":[],"trusted":true}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport torch\nimport torch.nn as nn\nimport albumentations as A\nimport pytorch_lightning as pl\nimport matplotlib.pyplot as plt\nimport PIL.Image as Image\n\nfrom torch.utils.data import Dataset, DataLoader\nimport torchvision.transforms as transforms\nimport torch.nn.functional as F\n\nimport albumentations as A\nfrom albumentations.core.composition import Compose, OneOf\nfrom albumentations.augmentations.transforms import CLAHE, GaussNoise, ISONoise\nfrom albumentations.pytorch import ToTensorV2\n\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn import preprocessing","metadata":{"_uuid":"7fb33a1d-1931-4056-ac00-bb78448e4232","_cell_guid":"28e85636-4b93-4898-b865-4e2aa1ed0e7b","collapsed":false,"papermill":{"duration":5.364408,"end_time":"2021-04-06T10:10:07.728013","exception":false,"start_time":"2021-04-06T10:10:02.363605","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Config","metadata":{"_uuid":"7d9191c3-6472-4492-8de7-d549649f8c5f","_cell_guid":"ba64a3c2-922f-433b-a743-d71e4c15c022","papermill":{"duration":0.021686,"end_time":"2021-04-06T10:10:07.769736","exception":false,"start_time":"2021-04-06T10:10:07.74805","status":"completed"},"tags":[],"trusted":true}},{"cell_type":"code","source":"DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')","metadata":{"_uuid":"317f418d-2127-476e-852d-ebf84629dcb1","_cell_guid":"cea3f9fb-40e6-4ee9-9218-fe1b227551a1","collapsed":false,"papermill":{"duration":0.586785,"end_time":"2021-04-06T10:10:08.383518","exception":false,"start_time":"2021-04-06T10:10:07.796733","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    seed = 42\n    pretrained = False\n    img_size = 299\n    num_classes = 6\n    lr = .00001\n    min_lr = 1e-6\n    t_max = 20\n    num_epochs = 10\n    batch_size = 16\n    augmentation_probability = 0.25\n    accum = 1\n    precision = 16\n    n_fold = 5\n    weight_decay = .05\n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"_uuid":"b9241d08-172e-494c-9309-c6afdaec5faa","_cell_guid":"29337e98-2d48-40e0-93ea-04d413997ffb","collapsed":false,"papermill":{"duration":0.027565,"end_time":"2021-04-06T10:10:08.431289","exception":false,"start_time":"2021-04-06T10:10:08.403724","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = \"../input/plant-pathology-2021-fgvc8/\"\n\nTEST_DIR = PATH + 'test_images/'","metadata":{"_uuid":"d05eae1f-2abe-4c82-8a42-d0ad0bd5c52d","_cell_guid":"7672d1fd-7e29-4130-968c-8f604b8034e6","collapsed":false,"papermill":{"duration":0.025594,"end_time":"2021-04-06T10:10:08.475996","exception":false,"start_time":"2021-04-06T10:10:08.450402","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all = pd.read_csv(PATH + \"train.csv\")\ndf_all.shape","metadata":{"_uuid":"b611546c-7228-4a63-bfd2-833691af1532","_cell_guid":"fe1aa8fa-10a0-4d8a-88ea-f72e06901a17","collapsed":false,"papermill":{"duration":0.065069,"end_time":"2021-04-06T10:10:08.560197","exception":false,"start_time":"2021-04-06T10:10:08.495128","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import defaultdict\n\n\ndct = defaultdict(list)\n\nfor i, label in enumerate(df_all.labels):\n    for category in label.split():\n        dct[category].append(i)\n \ndct = {key: np.array(val) for key, val in dct.items()}\ndct","metadata":{"_uuid":"7123dd84-a0e3-44fc-b5f3-57ce071bd203","_cell_guid":"1547be89-b809-4ee6-b26f-87ab71861ab7","collapsed":false,"papermill":{"duration":0.048516,"end_time":"2021-04-06T10:10:08.628417","exception":false,"start_time":"2021-04-06T10:10:08.579901","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df = pd.DataFrame(np.zeros((df_all.shape[0], len(dct.keys())), dtype=np.int8), columns=dct.keys())\n\nfor key, val in dct.items():\n    new_df.loc[val, key] = 1\n    \ndf_all = pd.concat([df_all, new_df], axis=1)\ndf_all.head()","metadata":{"_uuid":"86c83037-9239-42b4-aa96-bad2a90b941d","_cell_guid":"80a2f0c3-1bd2-406c-bbd8-8a4351f78096","collapsed":false,"papermill":{"duration":0.047821,"end_time":"2021-04-06T10:10:08.695984","exception":false,"start_time":"2021-04-06T10:10:08.648163","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"multi_labels = new_df.columns\nmulti_labels","metadata":{"_uuid":"ced1ab8c-df49-453a-8d08-796e3c5bf937","_cell_guid":"0ca2aed4-51b3-4877-b72f-c96e04627009","collapsed":false,"papermill":{"duration":0.035107,"end_time":"2021-04-06T10:10:08.753972","exception":false,"start_time":"2021-04-06T10:10:08.718865","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(PATH + \"sample_submission.csv\")\nsub.head()","metadata":{"_uuid":"abc57c30-98e4-4ab0-9e21-812c50a796af","_cell_guid":"367d42fd-9e94-4bc6-a804-cdee6df8e093","collapsed":false,"papermill":{"duration":0.041581,"end_time":"2021-04-06T10:10:08.817901","exception":false,"start_time":"2021-04-06T10:10:08.77632","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = pd.DataFrame(np.zeros([len(sub), len(new_df.columns)]), columns=multi_labels)\nsub = pd.concat([sub, tmp], axis=1)\nsub.head()","metadata":{"_uuid":"b029e908-04e1-4149-93aa-256021066def","_cell_guid":"29b440ca-38c7-4ec8-b1fd-2a491988581f","collapsed":false,"papermill":{"duration":0.040846,"end_time":"2021-04-06T10:10:08.881115","exception":false,"start_time":"2021-04-06T10:10:08.840269","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define Dataset","metadata":{"_uuid":"5445e25a-7f2b-4007-beeb-b7926960ec76","_cell_guid":"fce26281-605f-4d5a-ac71-4ebc26373ffe","papermill":{"duration":0.022206,"end_time":"2021-04-06T10:10:08.92669","exception":false,"start_time":"2021-04-06T10:10:08.904484","status":"completed"},"tags":[],"trusted":true}},{"cell_type":"code","source":"class PlantDataset(Dataset):\n    def __init__(self, df, directory, transform=None):\n        self.image_id = df['image'].values\n        self.labels = df.iloc[:, 2:].values\n        self.directory = directory\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.labels)\n\n    def __getitem__(self, idx):\n        image_id = self.image_id[idx]\n        label = torch.tensor(self.labels[idx], dtype=torch.float32)\n        \n        image_path = self.directory + image_id\n        image = cv2.imread(image_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        \n        \n        augmented = self.transform(image=image)\n        image = augmented['image']\n        return {'image':image, 'target': label}","metadata":{"_uuid":"5a988f60-1571-406f-a19e-fe99fa571284","_cell_guid":"a4f5ddfa-1640-429b-a229-4d837f9c3628","collapsed":false,"papermill":{"duration":0.031154,"end_time":"2021-04-06T10:10:08.981212","exception":false,"start_time":"2021-04-06T10:10:08.950058","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_transform(phase: str):\n    if phase == 'train':\n        '''\n        return Compose([\n            A.RandomResizedCrop(height=CFG.img_size, width=CFG.img_size),\n            A.HorizontalFlip(p=0.5),\n            A.ShiftScaleRotate(p=0.5),\n            A.RandomBrightnessContrast(p=0.5),\n            A.Normalize(),\n            ToTensorV2(),\n        ])\n        '''\n        return Compose([\n                      A.Resize(height=CFG.img_size, width=CFG.img_size),\n                      A.HorizontalFlip(p=CFG.augmentation_probability),\n                      A.VerticalFlip(p=CFG.augmentation_probability),\n                      A.ShiftScaleRotate(p=CFG.augmentation_probability),\n                      A.Rotate(p=CFG.augmentation_probability, limit=90),\n            \n                      A.RGBShift(p=CFG.augmentation_probability),\n            \n                      A.IAAAffine(rotate=90., p=CFG.augmentation_probability),\n                      A.IAAAffine(rotate=180., p=CFG.augmentation_probability),\n            \n                      A.RandomBrightnessContrast(p=CFG.augmentation_probability),\n                      A.RandomContrast(limit = 0.5,p = CFG.augmentation_probability),\n                      A.RandomSunFlare(p=CFG.augmentation_probability), \n                      A.RandomBrightness(p=CFG.augmentation_probability),\n            \n                      A.Normalize(),\n                      ToTensorV2(),\n        ])\n    else:\n        return Compose([\n            A.Resize(height=CFG.img_size, width=CFG.img_size),\n            A.Normalize(),\n            ToTensorV2(),\n        ])","metadata":{"_uuid":"c0f19c59-af91-42d8-a804-c525938fba17","_cell_guid":"b1b1fd71-9b3f-49c3-b7e5-bc448a46613c","collapsed":false,"papermill":{"duration":0.030723,"end_time":"2021-04-06T10:10:09.033187","exception":false,"start_time":"2021-04-06T10:10:09.002464","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = PlantDataset(df_all, PATH + \"train_images/\", get_transform('train'))\ndataset_loader = DataLoader(train_dataset, batch_size=CFG.batch_size, shuffle=True, num_workers=5)\ntest_dataset = PlantDataset(sub, PATH + \"test_images/\", get_transform('valid'))\ntest_loader = DataLoader(test_dataset, batch_size=CFG.batch_size, shuffle=False, num_workers=5)","metadata":{"_uuid":"6f232220-531e-4702-b999-0d97871b1a90","_cell_guid":"5df4e357-ec52-4c08-b266-25deadcac310","collapsed":false,"papermill":{"duration":0.030983,"end_time":"2021-04-06T10:10:09.085226","exception":false,"start_time":"2021-04-06T10:10:09.054243","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define Model","metadata":{"_uuid":"75f9b9fa-9bd1-4563-9db2-eb5f0c50616a","_cell_guid":"d40ba9d1-bd90-4948-85e3-7db0d1b0c3b0","papermill":{"duration":0.021332,"end_time":"2021-04-06T10:10:09.127948","exception":false,"start_time":"2021-04-06T10:10:09.106616","status":"completed"},"tags":[],"trusted":true}},{"cell_type":"code","source":"class SeparableConv2d(nn.Module):\n    def __init__(self,in_channels,out_channels,kernel_size=1,stride=1,padding=0,dilation=1,bias=False):\n        super(SeparableConv2d,self).__init__()\n\n        self.conv1 = nn.Conv2d(in_channels,in_channels,kernel_size,stride,padding,dilation,groups=in_channels,bias=bias)\n        self.pointwise = nn.Conv2d(in_channels,out_channels,1,1,0,1,1,bias=bias)\n\n    def forward(self,x):\n        x = self.conv1(x)\n        x = self.pointwise(x)\n        return x","metadata":{"_uuid":"cdcd450a-9c50-493a-a4ea-540d3949c1f6","_cell_guid":"649b90e9-3e62-4241-921b-8c9b421a52ac","collapsed":false,"papermill":{"duration":0.030069,"end_time":"2021-04-06T10:10:09.179338","exception":false,"start_time":"2021-04-06T10:10:09.149269","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Block(nn.Module):\n    def __init__(self,in_filters,out_filters,reps,strides=1,start_with_relu=True,grow_first=True):\n        super(Block, self).__init__()\n\n        if out_filters != in_filters or strides!=1:\n            self.skip = nn.Conv2d(in_filters,out_filters,1,stride=strides, bias=False)\n            self.skipbn = nn.BatchNorm2d(out_filters)\n        else:\n            self.skip=None\n\n        self.relu = nn.ReLU(inplace=True)\n        rep=[]\n\n        filters=in_filters\n        if grow_first:\n            rep.append(self.relu)\n            rep.append(SeparableConv2d(in_filters,out_filters,3,stride=1,padding=1,bias=False))\n            rep.append(nn.BatchNorm2d(out_filters))\n            filters = out_filters\n\n        for i in range(reps-1):\n            rep.append(self.relu)\n            rep.append(SeparableConv2d(filters,filters,3,stride=1,padding=1,bias=False))\n            rep.append(nn.BatchNorm2d(filters))\n\n        if not grow_first:\n            rep.append(self.relu)\n            rep.append(SeparableConv2d(in_filters,out_filters,3,stride=1,padding=1,bias=False))\n            rep.append(nn.BatchNorm2d(out_filters))\n\n        if not start_with_relu:\n            rep = rep[1:]\n        else:\n            rep[0] = nn.ReLU(inplace=False)\n\n        if strides != 1:\n            rep.append(nn.MaxPool2d(3,strides,1))\n        self.rep = nn.Sequential(*rep)\n\n    def forward(self,inp):\n        x = self.rep(inp)\n\n        if self.skip is not None:\n            skip = self.skip(inp)\n            skip = self.skipbn(skip)\n        else:\n            skip = inp\n\n        x+=skip\n        return x","metadata":{"_uuid":"f9f7d81a-bb38-46e3-aa71-34ba50395a7a","_cell_guid":"aef46ad2-152f-4728-b004-d5b9ea349260","collapsed":false,"papermill":{"duration":0.034815,"end_time":"2021-04-06T10:10:09.235509","exception":false,"start_time":"2021-04-06T10:10:09.200694","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Xception(nn.Module):\n    def __init__(self, num_classes=1000):\n        super(Xception, self).__init__()\n        self.num_classes = num_classes\n\n        self.conv1 = nn.Conv2d(3, 32, 3,2, 0, bias=False)\n        self.bn1 = nn.BatchNorm2d(32)\n        self.relu = nn.ReLU(inplace=True)\n\n        self.conv2 = nn.Conv2d(32,64,3,bias=False)\n        self.bn2 = nn.BatchNorm2d(64)\n\n        self.block1=Block(64,128,2,2,start_with_relu=False,grow_first=True)\n        self.block2=Block(128,256,2,2,start_with_relu=True,grow_first=True)\n        self.block3=Block(256,728,2,2,start_with_relu=True,grow_first=True)\n\n        self.block4=Block(728,728,3,1,start_with_relu=True,grow_first=True)\n        self.block5=Block(728,728,3,1,start_with_relu=True,grow_first=True)\n        self.block6=Block(728,728,3,1,start_with_relu=True,grow_first=True)\n        self.block7=Block(728,728,3,1,start_with_relu=True,grow_first=True)\n\n        self.block8=Block(728,728,3,1,start_with_relu=True,grow_first=True)\n        self.block9=Block(728,728,3,1,start_with_relu=True,grow_first=True)\n        self.block10=Block(728,728,3,1,start_with_relu=True,grow_first=True)\n        self.block11=Block(728,728,3,1,start_with_relu=True,grow_first=True)\n\n        self.block12=Block(728,1024,2,2,start_with_relu=True,grow_first=False)\n\n        self.conv3 = SeparableConv2d(1024,1536,3,1,1)\n        self.bn3 = nn.BatchNorm2d(1536)\n\n        self.conv4 = SeparableConv2d(1536,2048,3,1,1)\n        self.bn4 = nn.BatchNorm2d(2048)\n\n        self.fc = nn.Linear(2048, num_classes)\n\n\n\n    def features(self, input):\n        x = self.conv1(input)\n        x = self.bn1(x)\n        x = self.relu(x)\n\n        x = self.conv2(x)\n        x = self.bn2(x)\n        x = self.relu(x)\n\n        x = self.block1(x)\n        x = self.block2(x)\n        x = self.block3(x)\n        x = self.block4(x)\n        x = self.block5(x)\n        x = self.block6(x)\n        x = self.block7(x)\n        x = self.block8(x)\n        x = self.block9(x)\n        x = self.block10(x)\n        x = self.block11(x)\n        x = self.block12(x)\n\n        x = self.conv3(x)\n        x = self.bn3(x)\n        x = self.relu(x)\n\n        x = self.conv4(x)\n        x = self.bn4(x)\n        return x\n\n    def logits(self, features):\n        x = self.relu(features)\n\n        x = F.adaptive_avg_pool2d(x, (1, 1))\n        x = x.view(x.size(0), -1)\n        x = self.fc(x)\n        return x\n\n    def forward(self, input):\n        x = self.features(input)\n        x = self.logits(x)\n        return x","metadata":{"_uuid":"7be15333-5774-4d3c-9e86-4c52d064b427","_cell_guid":"21c414a9-12f5-48ec-814e-8319a2e1c6cc","collapsed":false,"papermill":{"duration":0.041128,"end_time":"2021-04-06T10:10:09.298002","exception":false,"start_time":"2021-04-06T10:10:09.256874","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import OrderedDict\n\ndef fix_model_state_dict(state_dict):\n    new_state_dict = OrderedDict()\n    for k, v in state_dict.items():\n        name = k\n        if name.startswith('model.'):\n            name = name[6:]  # remove 'model.' of dataparallel\n        new_state_dict[name] = v\n    return new_state_dict","metadata":{"_uuid":"3a8a535e-5445-4e1a-9ccf-791522c30923","_cell_guid":"2931ffab-d115-4920-8816-6299fd02b462","collapsed":false,"papermill":{"duration":0.028386,"end_time":"2021-04-06T10:10:09.34812","exception":false,"start_time":"2021-04-06T10:10:09.319734","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Xception()\n\nxceptionModelPath = None\n\nfor dirpath, subdirs, files in os.walk('/kaggle/input/xceptionimagenetcheckpoint'):\n    if xceptionModelPath:\n        break\n    for file in files:\n        if file.endswith('.pth'):\n            modelPath = os.path.join(dirpath,file)\n            break\n\nif xceptionModelPath:\n    model.load_state_dict(torch.load(xceptionModelPath), torch.device(DEVICE))\n\nmodel.fc = nn.Linear(2048, CFG.num_classes)\nmodel = model.to(DEVICE)\n\noptimizer = torch.optim.Adam(model.parameters(), lr=CFG.lr, weight_decay=CFG.weight_decay)\n\ncriterion = nn.BCELoss()\ncriterion = criterion.to(DEVICE)","metadata":{"_uuid":"118ba21c-2963-4fd8-8c5d-4a48c31a796f","_cell_guid":"f5ecc813-0b09-44db-8558-c51447c732c5","collapsed":false,"papermill":{"duration":4.190197,"end_time":"2021-04-06T10:10:13.559257","exception":false,"start_time":"2021-04-06T10:10:09.36906","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def saveModel(model):\n    torch.save(model.state_dict(), model.__class__.__name__+'.pth')","metadata":{"_uuid":"ed52e119-94d0-4e8e-9f88-f566c681d059","_cell_guid":"93684a63-5af1-4a9f-a40a-212063dc452c","collapsed":false,"papermill":{"duration":0.028842,"end_time":"2021-04-06T10:10:13.610026","exception":false,"start_time":"2021-04-06T10:10:13.581184","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{"_uuid":"80f58855-d457-47fa-95a3-94f00828c345","_cell_guid":"be6baacc-8654-4fed-ad5b-a1d03c4a2d25","papermill":{"duration":0.020944,"end_time":"2021-04-06T10:10:13.652868","exception":false,"start_time":"2021-04-06T10:10:13.631924","status":"completed"},"tags":[],"trusted":true}},{"cell_type":"code","source":"def fitModel(model):\n    sigmoid = nn.Sigmoid()\n    sigmoid = sigmoid.to(DEVICE)\n    \n    for epoch in range(CFG.num_epochs):\n        model = model.train()\n\n        for i, batch in enumerate(dataset_loader, start=1):\n            image = batch['image'].cuda()\n            labels = batch['target'].cuda()\n\n            logits = model(image)\n            output = sigmoid(logits)\n            loss = criterion(output, labels)\n            optimizer.zero_grad()\n            loss.backward()\n            optimizer.step()\n\n            train_loss = loss.detach().item()\n            print(\"Epoch: {0}/{1}, Current Epoch Progress: {2:.2f}%, Loss: {3:.4f}\".format(epoch+1, CFG.num_epochs, 100*i/(len(dataset_loader)), train_loss))\n\n        saveModel(model)\n\n        model.eval()","metadata":{"_uuid":"a3dd0401-3068-4577-9fba-ccaa18d0c6cf","_cell_guid":"7f0c2ff4-0529-45c4-b4c9-c9ccc38a8e95","collapsed":false,"papermill":{"duration":0.030484,"end_time":"2021-04-06T10:10:13.704408","exception":false,"start_time":"2021-04-06T10:10:13.673924","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelPath = None\n\nfor dirpath, subdirs, files in os.walk('/kaggle'):\n    if modelPath:\n        break\n    for file in files:\n        if file.endswith('.pth') and \"xception-43020ad28.pth\" not in file:\n            modelPath = os.path.join(dirpath,file)\n            break\n\nif modelPath:\n    print(\"Using pretrained model: \" + modelPath)\n    model.load_state_dict(torch.load(modelPath), torch.device(DEVICE))\nelse:\n    fitModel(model)","metadata":{"_uuid":"9fbc8ed2-8f82-4c39-bdc4-2ba331531714","_cell_guid":"3296ab32-c2e1-40b7-b295-707caaffc07c","collapsed":false,"papermill":{"duration":25.609727,"end_time":"2021-04-06T10:10:39.335553","exception":false,"start_time":"2021-04-06T10:10:13.725826","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.cuda()\nmodel.eval()\n\nsigmoid = nn.Sigmoid()\n\npredictions = []\nfor batch in test_loader:\n    image = batch['image'].cuda()\n    with torch.no_grad():\n        outputs = model(image)\n        preds = outputs.detach().cpu()\n        predictions.append(sigmoid(preds).numpy() > 0.5)","metadata":{"_uuid":"a31fbb76-1d95-4ec8-87c5-88361a520051","_cell_guid":"ee14dbbc-e34e-4ee4-aa76-62fd5816c8f6","collapsed":false,"papermill":{"duration":1.794895,"end_time":"2021-04-06T10:10:41.166258","exception":false,"start_time":"2021-04-06T10:10:39.371363","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = pd.DataFrame(np.concatenate(predictions).astype(np.int), columns=new_df.columns)","metadata":{"_uuid":"78c93fd6-f76f-4a5c-a6bc-650b09f8ac1a","_cell_guid":"0903718c-2d32-4de6-aa77-8d7463a9a507","collapsed":false,"papermill":{"duration":0.030103,"end_time":"2021-04-06T10:10:41.218368","exception":false,"start_time":"2021-04-06T10:10:41.188265","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.iloc[:, 2:] = predictions\nsub","metadata":{"_uuid":"aeba9fc7-6c71-4296-8a4f-64f297a7d085","_cell_guid":"7ce757b1-16d2-4f81-a061-c19ec83af395","collapsed":false,"papermill":{"duration":0.040982,"end_time":"2021-04-06T10:10:41.282242","exception":false,"start_time":"2021-04-06T10:10:41.24126","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = []\nfor i, row in sub.iloc[:, 2:].iterrows():\n    if (row['healthy'] == 1):\n        tmp = 'healthy'\n    elif (row['healthy'] == 0 and\n             row['scab'] == 0 and\n             row['frog_eye_leaf_spot'] == 0 and\n             row['complex'] == 0 and\n             row['rust'] == 0 and\n             row['powdery_mildew'] == 0):\n        tmp = 'healthy'\n    else:\n        tmp = ' '.join(multi_labels[row==row.max()])\n    labels.append(tmp)","metadata":{"_uuid":"bd6ac08d-b3f8-4140-a034-c4114cc68f14","_cell_guid":"838680ed-37f3-448a-891c-e3203dcaa5e9","collapsed":false,"papermill":{"duration":0.050058,"end_time":"2021-04-06T10:10:41.354392","exception":false,"start_time":"2021-04-06T10:10:41.304334","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['labels'] = labels\nsub[['image', 'labels']].to_csv('submission.csv', index=False)\nsub.head()","metadata":{"_uuid":"82b59c85-8741-431f-bc9d-62a819d8cbfd","_cell_guid":"92f12c21-75ca-4d6a-8b70-06383e0160c7","collapsed":false,"papermill":{"duration":0.142333,"end_time":"2021-04-06T10:10:41.519605","exception":false,"start_time":"2021-04-06T10:10:41.377272","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}