{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"code","source":"import os\n\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nimport cv2\nimport skimage.io\n\nfrom sklearn.model_selection import train_test_split, StratifiedKFold\n\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader, Dataset\n\nimport torchvision\nfrom torchvision import models, transforms\n\nfrom albumentations import Compose, Normalize, HorizontalFlip, VerticalFlip\nfrom albumentations.pytorch import ToTensorV2\n\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\n\nimport time\nimport copy\n\nfrom PIL import Image\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\ndevice","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"../input/prostate-cancer-grade-assessment/train.csv\")\ntest = pd.read_csv(\"../input/prostate-cancer-grade-assessment/test.csv\")\nsample = pd.read_csv(\"../input/prostate-cancer-grade-assessment/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def tile(img, sz=128, N=16):\n    shape = img.shape\n    pad0,pad1 = (sz - shape[0]%sz)%sz, (sz - shape[1]%sz)%sz\n    img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],\n                 constant_values=255)\n    img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)\n    img = img.transpose(0,2,1,3,4).reshape(-1,sz,sz,3)\n    if len(img) < N:\n        img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)\n    idxs = np.argsort(img.reshape(img.shape[0],-1).sum(-1))[:N]\n    img = img[idxs]\n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# def get_transforms(*, data):\n    \n#     assert data in ('train', 'valid')\n    \n#     if data == 'valid':\n#         return Compose([\n#             Normalize(\n#                 mean=[0.485, 0.456, 0.406],\n#                 std=[0.229, 0.224, 0.225],\n#             ),\n#             ToTensorV2(),\n#         ])\n\ndef get_transforms(*, data):\n    \n    assert data in ('train', 'valid')\n    \n    if data == 'valid':\n        return transforms.Compose([transforms.ToTensor(),\n                                   transforms.Normalize(\n                                       mean=[0.485, 0.456, 0.406],\n                                       std=[0.229, 0.224, 0.225])\n                                  ]\n                                 )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class TestDataset(Dataset):\n    def __init__ (self, image_id, transform=None):\n        self.image_id = image_id\n#         self.dir_name = dir_name\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.image_id)\n    \n    def __getitem__(self, idx):\n#         Creating the image\n        name = self.image_id[idx]\n#         file_path = f\"../input/prostate-cancer-grade-assessment/test_images/{name}.tiff\"\n        file_path = f\"../input/prostate-cancer-grade-assessment/test_images/{name}.tiff\"\n        image = skimage.io.MultiImage(file_path)[-1]\n        image = tile(image, sz=128, N=16)\n        image = cv2.hconcat([cv2.vconcat([image[0], image[1], image[2], image[3]]),\n                                   cv2.vconcat([image[4], image[5], image[6], image[7]]),\n                                   cv2.vconcat([image[8], image[9], image[10], image[11]]),\n                                   cv2.vconcat([image[12], image[13], image[14], image[15]])])\n#         image_tiles = cv2.cvtColor(image_tiles, cv2.COLOR_BGR2RGB)\n        if self.transform:\n            augmented = self.transform(image=image)\n            image = augmented['image']\n#         Return image\n        return image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Creates probabilites of predictions\ndef inference(model, test_loader, device):\n#     send model to gpu\n    model.to(device)\n    predictions = []\n    \n    for i, images in enumerate(test_loader):\n#         send images to gpu\n        images = images.to(device)\n#         Temporary sets the require_gradient parameter to False.\n#         Pytorch doesn't have to calcuate the gradients of the weights\n        with torch.no_grad():\n            outputs = model(images)\n            \n        predictions.append(outputs.to('cpu').numpy().argmax(1))\n#         Send predictions back to cpu (save memory) convert to numpy\n    \n    predictions = np.concatenate(predictions)\n#     Joins the arrays into one axis\n    return predictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if os.path.exists(\"../input/prostate-cancer-grade-assessment/test_images\"):\n    print('test_images_exist')\nelse:\n    print('test_images_not_exist')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = torch.load(\"../input/complete-model/complete_model.pth\")\nmodel.fc\nmodel.to(device)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def submit(sample):\n    if os.path.exists('../input/prostate-cancer-grade-assessment/test_images'):\n        test_dataset = TestDataset(sample['image_id'], transform=get_transforms(data='valid'))\n        test_loader = DataLoader(test_dataset, batch_size=1, shuffle=False)\n        preds = inference(model, test_loader, device)\n        sample['isup_grade'] = preds\n    return sample","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = submit(sample)\nsubmission['isup_grade'] = submission['isup_grade'].astype(int)\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.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":4}