{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"!pip install ../input/efficientnet-pytorch-070/efficientnet_pytorch-0.7.0-py3-none-any.whl","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\n\nimport albumentations\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nfrom scipy.special import softmax\n\nimport torch\nimport torch.nn as nn\nfrom torchvision import models, transforms\n\n\nfrom efficientnet_pytorch import EfficientNet","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"resnet_model_path = \"../input/rn-wc-tta-calr-clahe-cutmix/model(24).pth\"\neffnet_model_path = \"../input/en-b4-tta-calr-clahe-v3/eff_epoch_11.pth\"\n\nsample_sub_path = \"../input/cassava-leaf-disease-classification/sample_submission.csv\"\ntest_images_path = \"../input/cassava-leaf-disease-classification/test_images\"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Creating model and loading weights"},{"metadata":{"trusted":true},"cell_type":"code","source":"# # Creating the resnet model\nresnet_model = models.resnext50_32x4d(pretrained=False)\nresnet_model.fc = nn.Linear(2048, 5)\n\nresnet_model.to(torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\"))\nresnet_model.load_state_dict(torch.load(resnet_model_path))\nresnet_model.eval()\n\n# # Creating the efficientnet model\neffnet_model = EfficientNet.from_name('efficientnet-b4', num_classes=5)\neffnet_model.to(torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\"))\neffnet_model.load_state_dict(torch.load(effnet_model_path))\neffnet_model.eval()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Augmentations"},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_aug = albumentations.Compose([\n                albumentations.RandomResizedCrop(512, 512, scale=(0.5, 1.0)),\n                albumentations.Transpose(p=0.5),\n                albumentations.HorizontalFlip(p=0.5),\n                albumentations.VerticalFlip(p=0.5),\n                albumentations.ShiftScaleRotate(p=0.8),\n                albumentations.Normalize(\n                    mean=[0.485, 0.456, 0.406], \n                    std=[0.229, 0.224, 0.225], \n                    max_pixel_value=255.0, \n                    p=1.0\n                )\n            ], p=1.)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Creating the Submission file"},{"metadata":{"trusted":true},"cell_type":"code","source":"# resnet\nsample_sub = pd.read_csv(sample_sub_path)\ntta_count = 10\n\npredictions = []\nfor _, sample_row in sample_sub.iterrows():\n    \n    image_pred = 0\n    for j in range(tta_count):\n        image = np.array(Image.open(os.path.join(test_images_path, sample_row.image_id)))\n        image = sub_aug(image=image)[\"image\"]\n        image = transforms.ToTensor()(np.array(image))\n        image = image.to(torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\"))\n        \n        # resnet\n        outputs = resnet_model(image.unsqueeze(0))\n        image_pred += outputs.detach().cpu().numpy() * 0.6\n        \n        # effnet\n        outputs = effnet_model(image.unsqueeze(0))\n        image_pred += outputs.detach().cpu().numpy() * 0.4\n    \n    image_pred /= (tta_count)\n    predictions.extend(image_pred)\n    \npredictions = softmax(predictions, axis=1).argmax(axis=1)\n\nsub_df = pd.DataFrame({'image_id':sample_sub.image_id, 'label':predictions})\nsub_df.to_csv('submission.csv', index=False)\nprint(sub_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}