{"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":"%matplotlib inline\n# python libraties\nimport os\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\n\n# pytorch libraries\nimport torch\nfrom torch import optim,nn\nfrom torch.utils.data import DataLoader,Dataset\nfrom torchvision import models,transforms\n\n# sklearn libraries\nfrom sklearn.model_selection import train_test_split\n\n# to make the results are reproducible\nnp.random.seed(10)\ntorch.manual_seed(10)\ntorch.cuda.manual_seed(10)\n\npath = \"/kaggle/input/aptos2019-blindness-detection/\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-10T02:40:57.358519Z","iopub.execute_input":"2022-07-10T02:40:57.358948Z","iopub.status.idle":"2022-07-10T02:41:00.541368Z","shell.execute_reply.started":"2022-07-10T02:40:57.358849Z","shell.execute_reply":"2022-07-10T02:41:00.539356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') #Use GPU if it's available or else use CPU.\nprint(device) #Prints the device we're using.","metadata":{"execution":{"iopub.status.busy":"2022-07-10T02:41:00.543139Z","iopub.execute_input":"2022-07-10T02:41:00.543798Z","iopub.status.idle":"2022-07-10T02:41:00.551246Z","shell.execute_reply.started":"2022-07-10T02:41:00.54376Z","shell.execute_reply":"2022-07-10T02:41:00.550083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#For getting a random image from our training set.\ndf_all = pd.read_csv(\"../input/aptos2019-blindness-detection/train.csv\")\nnum = int(np.random.randint(0,len(df_all)-1,(1,))) #Picks a random number.\nsample_image = (f'{path}train_images/{df_all[\"id_code\"][num]}.png')#Image file.\nsample_image = Image.open(sample_image)\ntransform1 = transforms.ToTensor()\ntransform2 = transforms.Compose([transforms.ToTensor(),\n                                 transforms.Resize(32),\n                                 transforms.CenterCrop(32)])\noriginal_image = transform1(sample_image)\n\nplt.imshow(original_image.permute(1,2,0))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T02:41:00.554829Z","iopub.execute_input":"2022-07-10T02:41:00.556033Z","iopub.status.idle":"2022-07-10T02:41:01.730019Z","shell.execute_reply.started":"2022-07-10T02:41:00.555991Z","shell.execute_reply":"2022-07-10T02:41:01.728681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_image = transform2(sample_image)\nplt.imshow(final_image.permute(1,2,0))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T02:41:01.731113Z","iopub.execute_input":"2022-07-10T02:41:01.731724Z","iopub.status.idle":"2022-07-10T02:41:01.906102Z","shell.execute_reply.started":"2022-07-10T02:41:01.731692Z","shell.execute_reply":"2022-07-10T02:41:01.905335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transform = transforms.Compose([transforms.ToTensor(),\n                                      transforms.Resize(32),\n                                      transforms.CenterCrop(32),\n                                      transforms.RandomHorizontalFlip(),\n                                      transforms.RandomVerticalFlip(),\n                                     ])\n\ntest_transform = transforms.Compose([transforms.ToTensor(),\n                                     transforms.Resize(32),\n                                     transforms.CenterCrop(32)\n                                    ])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T02:41:01.907171Z","iopub.execute_input":"2022-07-10T02:41:01.907986Z","iopub.status.idle":"2022-07-10T02:41:01.913509Z","shell.execute_reply.started":"2022-07-10T02:41:01.907954Z","shell.execute_reply":"2022-07-10T02:41:01.912792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MyData(Dataset):\n    def __init__(self, df, transform=None):\n        self.df = df\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, index):\n        # Load data and get label\n        base_dir = \"../input/aptos2019-blindness-detection/train_images/\"\n        X = Image.open(base_dir + self.df['id_code'][index] + \".png\")\n        y = torch.tensor(int(self.df['diagnosis'][index]))\n\n        if self.transform:\n            X = self.transform(X)\n\n        return X, y","metadata":{"execution":{"iopub.status.busy":"2022-07-10T02:41:01.914794Z","iopub.execute_input":"2022-07-10T02:41:01.915375Z","iopub.status.idle":"2022-07-10T02:41:01.926771Z","shell.execute_reply.started":"2022-07-10T02:41:01.915346Z","shell.execute_reply":"2022-07-10T02:41:01.925986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = df_all['diagnosis']\ndf_train, df_test = train_test_split(df_all, test_size=0.2, random_state=47, stratify=y)\ndf_train['diagnosis'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T02:41:01.929027Z","iopub.execute_input":"2022-07-10T02:41:01.929759Z","iopub.status.idle":"2022-07-10T02:41:01.964526Z","shell.execute_reply.started":"2022-07-10T02:41:01.929724Z","shell.execute_reply":"2022-07-10T02:41:01.963487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Copy fewer class to balance the number of 7 classes\ndata_aug_rate = [0, 5, 2, 9, 6]\nfor i in range(5):\n    if data_aug_rate[i]:\n        df_train=df_train.append([df_train.loc[df_train['diagnosis'] == i,:]]*(data_aug_rate[i]-1), ignore_index=True)\ndf_train['diagnosis'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T02:41:01.965597Z","iopub.execute_input":"2022-07-10T02:41:01.965841Z","iopub.status.idle":"2022-07-10T02:41:01.980753Z","shell.execute_reply.started":"2022-07-10T02:41:01.965818Z","shell.execute_reply":"2022-07-10T02:41:01.979956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.reset_index(inplace=True)\ndf_test.reset_index(inplace=True)\n# Define the training set using the table train_df and using our defined transitions (train_transform)\ntrain_set = MyData(df_train, train_transform)\ntrain_dataloader = DataLoader(train_set, batch_size=128, shuffle=True, num_workers=0)\n# Same for the validation set:\ntest_set = MyData(df_test, test_transform)\ntest_dataloader = DataLoader(test_set, batch_size=128, shuffle=False, num_workers=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T02:41:01.984535Z","iopub.execute_input":"2022-07-10T02:41:01.985214Z","iopub.status.idle":"2022-07-10T02:41:01.993866Z","shell.execute_reply.started":"2022-07-10T02:41:01.985188Z","shell.execute_reply":"2022-07-10T02:41:01.992609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from glob import glob\nfrom tqdm import tqdm\ndata_dir = '../input/aptos2019-blindness-detection/train_images'\nall_image_path = glob(os.path.join(data_dir, '*.png'))\nimageid_path_dict = {os.path.splitext(os.path.basename(x))[0]: x for x in all_image_path}\ntrans = transforms.Resize(32)\nos.mkdir(\"./new_data\")\nfor image in tqdm(imageid_path_dict.keys()):\n    X = Image.open(imageid_path_dict[image])\n    XX = trans(X)\n    Image.Image.save(XX, \"./new_data/{}.png\".format(image))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T02:41:22.612862Z","iopub.execute_input":"2022-07-10T02:41:22.613199Z","iopub.status.idle":"2022-07-10T02:41:32.140305Z","shell.execute_reply.started":"2022-07-10T02:41:22.613175Z","shell.execute_reply":"2022-07-10T02:41:32.138824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def train_loop(dataloader, model, loss_fn, optimizer, device):\n#     size = len(dataloader.dataset)\n#     model.train()\n#     start = time.time()\n#     for batch, (X, y) in enumerate(dataloader):\n#         end = time.time()\n#         print(\"加载DataLoader时间: {}\".format(end-start))\n#         X, y = X.to(device), y.to(device)\n#         pred = model(X)\n#         loss = loss_fn(pred, y)\n\n#         optimizer.zero_grad()\n#         loss.backward()\n#         optimizer.step()\n\n#         if batch % 100 == 0:\n#             loss, current = loss.item(), batch * len(X)\n#             print(f\"loss: {loss:>7f}  [{current:>5d}/{size:>5d}]\")\n\n\n# def test_loop(dataloader, model, loss_fn, device):\n#     size = len(dataloader.dataset)\n#     num_batches = len(dataloader)\n#     test_loss, correct = 0, 0\n#     model.eval()\n\n#     with torch.no_grad():\n#         for X, y in dataloader:\n#             X, y = X.to(device), y.to(device)\n#             pred = model(X)\n#             test_loss += loss_fn(pred, y).item()\n#             correct += (pred.argmax(1) == y).type(torch.float).sum().item()\n\n#     test_loss /= num_batches\n#     correct /= size\n\n#     return correct, test_loss","metadata":{"execution":{"iopub.status.busy":"2022-07-10T02:41:02.824981Z","iopub.status.idle":"2022-07-10T02:41:02.825501Z","shell.execute_reply.started":"2022-07-10T02:41:02.82523Z","shell.execute_reply":"2022-07-10T02:41:02.825253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class Square(nn.Module):\n#     def __init__(self):\n#         super(Square, self).__init__()\n\n#     def forward(self, x):\n#         # unfortunately we don't have automatic broadcasting yet\n#         return torch.mul(x, x)\n    \n# class eBaseNet(nn.Module):\n#     def __init__(self):\n#         super(eBaseNet, self).__init__()\n\n#         self.features = nn.Sequential(\n#             nn.Conv2d(3, 16, 3, 1, 1), Square(),\n#             nn.AvgPool2d(2),\n#             nn.Conv2d(16, 32, 3), Square(),\n#             nn.AvgPool2d(2),\n#             nn.Conv2d(32, 64, 3),\n#         self.classifier = nn.Sequential(\n#             nn.Linear(1600, 128), Square(), nn.Dropout(),\n#             nn.Linear(128, 10)\n#         )\n\n#     def forward(self, x):\n#         x = self.features(x) Square()\n#         )\n#         x = x.view(x.size(0), -1)\n#         x = self.classifier(x)\n#         return x","metadata":{"execution":{"iopub.status.busy":"2022-07-08T03:22:50.533877Z","iopub.execute_input":"2022-07-08T03:22:50.535203Z","iopub.status.idle":"2022-07-08T03:22:50.545521Z","shell.execute_reply.started":"2022-07-08T03:22:50.535158Z","shell.execute_reply":"2022-07-08T03:22:50.544671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import time\n# model = eBaseNet().to(device)\n\n# if device == 'cuda':\n#     model = torch.nn.DataParallel(model)\n#     cudnn.benchmark = True\n\n# best_acc = 0\n# lr = 0.001\n# epochs = 200\n# save_dir = \"test.pth\"\n# loss_fn = nn.CrossEntropyLoss()\n# optimizer = optim.Adam(model.parameters(), lr=lr)\n# scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=200)\n\n# for epoch in range(epochs):\n#     print(f\"Epoch {epoch + 1}\\n-------------------------------\")\n#     train_loop(train_dataloader, model, loss_fn, optimizer, device)\n#     test_acc, test_loss = test_loop(test_dataloader, model, loss_fn, device)\n#     if test_acc > best_acc:\n#         state = {\n#             'net': model.state_dict(),\n#             'acc': test_acc,\n#             'epoch': epoch,\n#         }\n#         torch.save(state, save_dir)\n#         best_acc = test_acc\n#     print(\"Test: \\n Best Acc:{:.1f}%, Acc: {:.1f}%, Avg loss: {:.8f}\".format(100 * best_acc, 100 * test_acc, test_loss))\n\n#     scheduler.step()\n# print(\"Done!\")","metadata":{"execution":{"iopub.status.busy":"2022-07-08T03:22:51.903637Z","iopub.execute_input":"2022-07-08T03:22:51.904616Z","iopub.status.idle":"2022-07-08T03:25:44.783551Z","shell.execute_reply.started":"2022-07-08T03:22:51.904578Z","shell.execute_reply":"2022-07-08T03:25:44.781598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}