{"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":"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\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 read-only \"../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    print(dirname, len(filenames))\n\n# You can write up to 20GB to the current directory /kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-26T07:48:54.693913Z","iopub.execute_input":"2021-07-26T07:48:54.694386Z","iopub.status.idle":"2021-07-26T07:48:58.724997Z","shell.execute_reply.started":"2021-07-26T07:48:54.694291Z","shell.execute_reply":"2021-07-26T07:48:58.723933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim \nimport torch.nn.functional as F\nimport torchvision\n\nfrom torch.utils.data import  TensorDataset, DataLoader \nfrom torch.optim import lr_scheduler\nfrom torchvision import datasets, models, transforms\n\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'\nprint(\"Using device\", device)\n\nfrom cv2 import cv2\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\nimport time\nimport copy\nimport random\n\nfrom tqdm import tqdm\n\nrandom_seed = 42\n\ntorch.manual_seed(random_seed)\ntorch.cuda.manual_seed(random_seed)\ntorch.cuda.manual_seed_all(random_seed) # if use multi-GPU\ntorch.backends.cudnn.deterministic = True\ntorch.backends.cudnn.benchmark = False\nnp.random.seed(random_seed)\nrandom.seed(random_seed)\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:16.2251Z","iopub.execute_input":"2021-07-26T07:49:16.225493Z","iopub.status.idle":"2021-07-26T07:49:18.263276Z","shell.execute_reply.started":"2021-07-26T07:49:16.225461Z","shell.execute_reply":"2021-07-26T07:49:18.262063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"/kaggle/input/aptos2019-blindness-detection/train_images/\"\ntest_path = \"/kaggle/input/aptos2019-blindness-detection/test_images/\"\ntrain_data = pd.read_csv(\"../input/aptos2019-blindness-detection/train.csv\")\ntest_data = pd.read_csv(\"../input/aptos2019-blindness-detection/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:21.113383Z","iopub.execute_input":"2021-07-26T07:49:21.113796Z","iopub.status.idle":"2021-07-26T07:49:21.146943Z","shell.execute_reply.started":"2021-07-26T07:49:21.113766Z","shell.execute_reply":"2021-07-26T07:49:21.145797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_data.shape)\nprint(train_data.head(10))\nprint(test_data.shape)\nprint(test_data.head(10))","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:49:41.935331Z","iopub.execute_input":"2021-07-26T07:49:41.935739Z","iopub.status.idle":"2021-07-26T07:49:41.946672Z","shell.execute_reply.started":"2021-07-26T07:49:41.935707Z","shell.execute_reply":"2021-07-26T07:49:41.94503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(12, 12))\n\nfor i in range(1, 10):\n    img = cv2.imread(train_path +'/' + train_data['id_code'][i] +'.png')\n    ax = fig.add_subplot(3, 3, i)\n    ax.imshow(img)\n    ax.set_title(img.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:51:51.181397Z","iopub.execute_input":"2021-07-26T07:51:51.18182Z","iopub.status.idle":"2021-07-26T07:51:59.771438Z","shell.execute_reply.started":"2021-07-26T07:51:51.181787Z","shell.execute_reply":"2021-07-26T07:51:59.77036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = torchvision.transforms.Compose([\n    transforms.Resize(224),\n    transforms.CenterCrop(224),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n])","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:52:14.736002Z","iopub.execute_input":"2021-07-26T07:52:14.736477Z","iopub.status.idle":"2021-07-26T07:52:14.743929Z","shell.execute_reply.started":"2021-07-26T07:52:14.736445Z","shell.execute_reply":"2021-07-26T07:52:14.742256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SimpleDataset():\n    def __init__(self, data, root, transform):\n        self.files = list(root + data['id_code'] + '.png')\n        self.targets = data['diagnosis']\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.files)\n\n    def __getitem__(self, idx):\n        img = Image.open(self.files[idx])\n        x = self.transform(img)\n        y = torch.tensor(self.targets[idx]).float()\n        return x, y","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:52:29.795098Z","iopub.execute_input":"2021-07-26T07:52:29.795524Z","iopub.status.idle":"2021-07-26T07:52:29.803385Z","shell.execute_reply.started":"2021-07-26T07:52:29.795491Z","shell.execute_reply":"2021-07-26T07:52:29.801768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_ft=models.resnet18(pretrained=True)\nmodel_ft","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:52:38.095299Z","iopub.execute_input":"2021-07-26T07:52:38.095768Z","iopub.status.idle":"2021-07-26T07:52:39.218573Z","shell.execute_reply.started":"2021-07-26T07:52:38.095735Z","shell.execute_reply":"2021-07-26T07:52:39.217214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_features=model_ft.fc.in_features\nmodel_ft.fc=nn.Linear(num_features,1)\n\nmodel_ft=model_ft.to(device)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:52:44.722801Z","iopub.execute_input":"2021-07-26T07:52:44.723214Z","iopub.status.idle":"2021-07-26T07:52:50.164582Z","shell.execute_reply.started":"2021-07-26T07:52:44.723176Z","shell.execute_reply":"2021-07-26T07:52:50.163413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = SimpleDataset(train_data, train_path, transform)\ndataloader = DataLoader(train_dataset, batch_size=32, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:53:06.486801Z","iopub.execute_input":"2021-07-26T07:53:06.48724Z","iopub.status.idle":"2021-07-26T07:53:06.512659Z","shell.execute_reply.started":"2021-07-26T07:53:06.487208Z","shell.execute_reply":"2021-07-26T07:53:06.511493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion =nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(lr=1e-4, params=model_ft.parameters())\nscheduler = lr_scheduler.StepLR(optimizer, step_size=10)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:53:44.684903Z","iopub.execute_input":"2021-07-26T07:53:44.685432Z","iopub.status.idle":"2021-07-26T07:53:44.695116Z","shell.execute_reply.started":"2021-07-26T07:53:44.685401Z","shell.execute_reply":"2021-07-26T07:53:44.691592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"since = time.time()\ncriterion = torch.nn.MSELoss()\nnum_epochs = 15\nfor epoch in range(num_epochs):\n    print('Epoch {}/{}'.format(epoch, num_epochs - 1))\n    print('-' * 10)\n    scheduler.step()\n    model_ft.train()\n    running_loss = 0.0\n    tk0 = tqdm(dataloader, total=int(len(dataloader)))\n    counter = 0\n    for bi, (d, t) in enumerate(tk0):\n        inputs = d\n        labels = t\n        inputs = inputs.to(device, dtype=torch.float)\n        labels = labels.to(device, dtype=torch.float)\n        optimizer.zero_grad()\n        with torch.set_grad_enabled(True):\n            outputs = model_ft(inputs)\n            loss = criterion(outputs, labels)\n            loss.backward()\n            optimizer.step()\n        running_loss += loss.item() * inputs.size(0)\n        counter += 1\n        tk0.set_postfix(loss=(running_loss / (counter * dataloader.batch_size)))\n    epoch_loss = running_loss / len(dataloader)\n    print('Training Loss: {:.4f}'.format(epoch_loss))\n\ntime_elapsed = time.time() - since\nprint('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))\ntorch.save(model.state_dict(), \"model.bin\")","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:53:49.932097Z","iopub.execute_input":"2021-07-26T07:53:49.932489Z","iopub.status.idle":"2021-07-26T07:55:59.034745Z","shell.execute_reply.started":"2021-07-26T07:53:49.932459Z","shell.execute_reply":"2021-07-26T07:55:59.031193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}