{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Imports, settings and references\nmodels from : https://www.kaggle.com/jtbontinck/cnn-features-extraction-xgb-baseline"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np \n\nfrom PIL import Image\n\nimport torch\nimport torch.nn as nn\nimport torchvision\nfrom torch.utils.data import Dataset\nfrom torchvision import transforms\n\nimport xgboost as xgb\nfrom sklearn.metrics import cohen_kappa_score\n\nimport pickle\n\nDEVICE = torch.device(\"cuda:0\")\nDATA_SOURCE = os.path.join(\"..\",\"input\",\"aptos2019-blindness-detection\")\nMODEL_SOURCE = os.path.join(\"..\",\"input\",\"aptos-cnn-features-extraction-xgb-baseline\")\n# DATA_SOURCE = os.path.join(\"..\",\"input\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# PyTorch's style data loader defintion\nadapted from : https://www.kaggle.com/abhishek/very-simple-pytorch-training-0-59"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"class RetinopathyDatasetTest(Dataset):\n\n    def __init__(self, eval_set=False, random_state=42):\n        # read data list, split in train and eval, select the set\n        csv_file = os.path.join(DATA_SOURCE, \"test.csv\")\n        df = pd.read_csv(csv_file)\n        self.data = df.reset_index(drop=True)\n            \n    def __len__(self):\n        return len(self.data)\n\n    def __getitem__(self, idx):\n        # get image and process it to tensor ready for the model, extract features\n        folder = os.path.join(DATA_SOURCE, \"test_images\")\n        code = str(self.data.loc[idx, 'id_code'])\n        file = code + \".png\"\n        path = os.path.join(folder, file)\n        imgpil = Image.open(path)\n        base_transforms = transforms.Compose([transforms.Resize((224, 224)),\n                                              transforms.ToTensor(),\n                                              transforms.Normalize([0.485, 0.456, 0.406], \n                                                                   [0.229, 0.224, 0.225])])\n        img_tensor = base_transforms(imgpil)\n        return {'image': img_tensor}","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Extract features"},{"metadata":{"trusted":true},"cell_type":"code","source":"# load the pretrained CNN used as feature extractor\n# no classifier defined, we will take the raw output from the CNN layers\nextractor = torchvision.models.resnet101(pretrained=False)\nextractor.fc = nn.Identity() \nmodel_path = os.path.join(MODEL_SOURCE, \"resnet101.pth\")\nextractor.load_state_dict(torch.load(model_path))\nextractor.to(DEVICE)\nextractor.eval()\n\ndata_loader = torch.utils.data.DataLoader(RetinopathyDatasetTest(), \n                            batch_size=2, shuffle=False, num_workers=0, drop_last=False)\n\ndef get_extracted_data(data_loader):\n    for bi, d in enumerate(data_loader):\n        if bi % 32 == 0 : print(\".\", end=\"\")\n        img_tensor = d[\"image\"].to(DEVICE)\n        with torch.no_grad(): feature = extractor(img_tensor)\n        feature = feature.cpu().detach().squeeze(0).numpy()\n        if bi == 0 :\n            features = feature \n        else :\n            features = np.concatenate([features, feature], axis=0)\n    print(\"\")\n    return features\n\nprint(\"...............................\")\nfeatures = get_extracted_data(data_loader)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Fit the XGBoost model"},{"metadata":{"trusted":true},"cell_type":"code","source":"XGBOOST_PARAM = {\n    \"random_state\" : 42,\n    'objective': 'multi:softmax',\n    \"num_class\" : 5,\n    \"n_estimators\" : 200,\n    \"eval_metric\" : \"mlogloss\"\n}\nxgb_model_1 = xgb.XGBClassifier(**XGBOOST_PARAM)\nmodel_path = os.path.join(MODEL_SOURCE, \"xgb_model_1\")\nxgb_model_1 = pickle.load(open(model_path, \"rb\"))\nprediction1 = xgb_model_1.predict_proba(features)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xgb_model_2 = xgb.XGBClassifier(**XGBOOST_PARAM)\nmodel_path = os.path.join(MODEL_SOURCE, \"xgb_model_2\")\nxgb_model_2 = pickle.load(open(model_path, \"rb\"))\nprediction2 = xgb_model_2.predict_proba(features)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction = (prediction1 + prediction2).argmax(axis=1)\ncsv_file = os.path.join(DATA_SOURCE, \"sample_submission.csv\")\ndf = pd.read_csv(csv_file)\ndf[\"diagnosis\"] = prediction\ndf.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}