{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\nimport torch.nn.functional as F\nimport numpy as np\nimport torchvision\nfrom torchvision import datasets, models, transforms\nimport matplotlib.pyplot as plt\nimport time\nimport os\nimport copy\nfrom sklearn.metrics import confusion_matrix\nfrom torch.utils.data import random_split, Dataset\nfrom torch.utils.data.sampler import SubsetRandomSampler\nimport glob\nfrom PIL import Image\nimport glob\nimport cv2\nimport gc #garbage collector for gpu memory ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"transform = transforms.Compose(\n        [transforms.Resize((320,320)),\n        transforms.ToTensor(),\n        transforms.Normalize([0.460, 0.247, 0.080], [0.249, 0.138, 0.081])\n        ])\n\nclass APTOSDataset(Dataset):\n    \"\"\"Eye images dataset.\"\"\"\n    def __init__(self, csv_file, filetype, transform=None):\n        self.eye_frame = pd.read_csv(csv_file)\n        self.filetype = filetype\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.eye_frame)\n    \n    def __getitem__(self, idx):\n        if self.filetype == 'train':\n            img_name = os.path.join('../input/aptos2019-blindness-detection/train_images',\n                                    self.eye_frame.loc[idx,'id_code'] + '.png')\n\n            image = Image.open(img_name)\n            if self.transform:\n                image = self.transform(image)\n            else:\n                image = transforms.ToTensor()(image)\n\n            return image,self.eye_frame.diagnosis[idx]\n        \n        else:\n            img_name = os.path.join('../input/aptos2019-blindness-detection/test_images',\n                                    self.eye_frame.loc[idx,'id_code'] + '.png')\n            image = Image.open(img_name)\n            if self.transform:\n                image = self.transform(image)\n            else:\n                image = transforms.ToTensor()(image)\n            return image, self.eye_frame.loc[idx,'id_code']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_dataset = APTOSDataset(csv_file='../input/aptos2019-blindness-detection/test.csv', filetype='test',transform=transform)\ntest_loader = torch.utils.data.DataLoader(test_dataset, batch_size=24, shuffle=False, num_workers=4)\n\n\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lazy Coding"},{"metadata":{"trusted":true},"cell_type":"code","source":"model0 = torchvision.models.resnet152(pretrained=False)\nstate = torch.load('../input/resnet/FinalResnet152_0.pt')\nnum_ftrs = model0.fc.in_features\nmodel0.fc = nn.Linear(num_ftrs,5)\nmodel0.load_state_dict(state)\nmodel0 = model0.to(device)\n\nmodel1 = torchvision.models.resnet101(pretrained=False)\nstate = torch.load('../input/resnet/FinalResnet02.pt')\nnum_ftrs = model1.fc.in_features\nmodel1.fc = nn.Linear(num_ftrs,5)\nmodel1.load_state_dict(state)\nmodel1 = model1.to(device)\n\nmodel2 = torchvision.models.resnet101(pretrained=False)\nstate = torch.load('../input/resnet/FinalResnet01.pt')\nnum_ftrs = model2.fc.in_features\nmodel2.fc = nn.Linear(num_ftrs,5)\nmodel2.load_state_dict(state)\nmodel2 = model2.to(device)\n\nmodel3 = torchvision.models.resnet101(pretrained=False)\nstate = torch.load('../input/resnet/FinalResnet00.pt')\nnum_ftrs = model3.fc.in_features\nmodel3.fc = nn.Linear(num_ftrs,5)\nmodel3.load_state_dict(state)\nmodel3 = model3.to(device)\n\nmodel4 = torchvision.models.resnet101(pretrained=False)\nstate = torch.load('../input/resnet/FinalResnet0.pt')\nnum_ftrs = model4.fc.in_features\nmodel4.fc = nn.Linear(num_ftrs,5)\nmodel4.load_state_dict(state)\nmodel4 = model4.to(device)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tqdm._tqdm_notebook import tqdm_notebook\ndef compute_predictions(model, model_type, data_loader, device):\n    if model_type == 'train':\n        predictions = []\n        correct_pred, num_examples = 0, 0\n        tqdm_notebook()\n        for i, (inputs, labels) in enumerate(tqdm_notebook(data_loader)):\n            inputs = inputs.to(device)\n            labels = labels.to(device)\n            outputs = model(inputs)\n            _, preds = torch.max(outputs, 1)\n            predictions.append(preds)\n            num_examples += labels.size(0)\n            correct_pred += (preds==labels).sum()\n        return predictions, correct_pred.item()/num_examples*100\n    \n    else:\n        predictions = []\n        img_ids = []\n        out = []\n        tqdm_notebook()\n\n        for i, (inputs, img_id) in enumerate(tqdm_notebook(data_loader)):\n            inputs = inputs.to(device)\n            outputs = model(inputs)\n            _, preds = torch.max(outputs, 1)\n            predictions.extend(preds)\n            img_ids.extend(img_id)\n            out.extend(outputs)\n            \n        predictions = [pred.item() for pred in predictions]\n        final_predictions = pd.DataFrame(np.array([img_ids, predictions])).transpose()\n        final_predictions.columns = ['id_code', 'diagnosis']\n        final_predictions_df = final_predictions.copy()\n        #final_predictions.to_csv(\"submission.csv\",index=False)\n        return final_predictions_df, out, img_ids","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with torch.set_grad_enabled(False):\n    model0.eval()\n    model1.eval()\n    model2.eval()\n    model3.eval()\n    model4.eval()\n\n    #train_predictions, train_accuracy = compute_predictions(model, 'train', train_loader, device)\n    #print('Train Accuracy: ', train_accuracy)\n    print('Computing Test Predictions')\n    test_predictions0,out0,ids0 = compute_predictions(model0, 'test', test_loader, device)\n    test_predictions1,out1,ids1 = compute_predictions(model1, 'test', test_loader, device)\n    test_predictions2,out2,ids2 = compute_predictions(model2, 'test', test_loader, device)\n    test_predictions3,out3,ids3 = compute_predictions(model3, 'test', test_loader, device)\n    test_predictions4,out4,ids4 = compute_predictions(model4, 'test', test_loader, device)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"out = (torch.stack(out0)+torch.stack(out1)+torch.stack(out2)+torch.stack(out3)+torch.stack(out4))/5\nimg_ids = np.array(ids0)\n_, predictions = torch.max(out, 1)\npredictions = predictions.cpu().numpy()\nfinal_predictions = pd.DataFrame(np.array([img_ids, predictions])).transpose()\nfinal_predictions.columns = ['id_code', 'diagnosis']\nfinal_predictions_df = final_predictions.copy()\nfinal_predictions.to_csv(\"submission.csv\",index=False)","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":1}