{"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":"markdown","source":"Lets work with sample data.","metadata":{}},{"cell_type":"code","source":"from torch.utils.data import Dataset, DataLoader","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.384081Z","iopub.execute_input":"2021-06-13T03:20:05.384608Z","iopub.status.idle":"2021-06-13T03:20:05.388557Z","shell.execute_reply.started":"2021-06-13T03:20:05.384574Z","shell.execute_reply":"2021-06-13T03:20:05.387805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !unzip ../input/diabetic-retinopathy-detection/sample.zip\n# !unzip ../input/diabetic-retinopathy-detection/sampleSubmission.csv.zip","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.389672Z","iopub.execute_input":"2021-06-13T03:20:05.390069Z","iopub.status.idle":"2021-06-13T03:20:05.404128Z","shell.execute_reply.started":"2021-06-13T03:20:05.390041Z","shell.execute_reply":"2021-06-13T03:20:05.403413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ! dir sample","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.411090Z","iopub.execute_input":"2021-06-13T03:20:05.411549Z","iopub.status.idle":"2021-06-13T03:20:05.418462Z","shell.execute_reply.started":"2021-06-13T03:20:05.411519Z","shell.execute_reply":"2021-06-13T03:20:05.417438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !unzip ../input/diabetic-retinopathy-detection/trainLabels.csv.zip","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.419763Z","iopub.execute_input":"2021-06-13T03:20:05.420297Z","iopub.status.idle":"2021-06-13T03:20:05.432647Z","shell.execute_reply.started":"2021-06-13T03:20:05.420264Z","shell.execute_reply":"2021-06-13T03:20:05.431860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from PIL import Image\n\n# img = Image.open(\"./sample/16_right.jpeg\")\n\n# import matplotlib.pyplot as plt\n\n# plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.433936Z","iopub.execute_input":"2021-06-13T03:20:05.434318Z","iopub.status.idle":"2021-06-13T03:20:05.445316Z","shell.execute_reply.started":"2021-06-13T03:20:05.434290Z","shell.execute_reply":"2021-06-13T03:20:05.444388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"But there are no labels!","metadata":{}},{"cell_type":"code","source":"# import pandas as pd\n\n# pd.read_csv(\"./trainLabels.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.446581Z","iopub.execute_input":"2021-06-13T03:20:05.446981Z","iopub.status.idle":"2021-06-13T03:20:05.460014Z","shell.execute_reply.started":"2021-06-13T03:20:05.446946Z","shell.execute_reply":"2021-06-13T03:20:05.458533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Lets get the first train file","metadata":{}},{"cell_type":"code","source":"#!mv ../input/diabetic-retinopathy-detection/train.zip.001 ./train1.zip","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.461829Z","iopub.execute_input":"2021-06-13T03:20:05.462281Z","iopub.status.idle":"2021-06-13T03:20:05.475553Z","shell.execute_reply.started":"2021-06-13T03:20:05.462233Z","shell.execute_reply":"2021-06-13T03:20:05.474439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !unzip train1.zip\n\n# I guess we need to find something else altogether","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.477118Z","iopub.execute_input":"2021-06-13T03:20:05.477574Z","iopub.status.idle":"2021-06-13T03:20:05.487707Z","shell.execute_reply.started":"2021-06-13T03:20:05.477524Z","shell.execute_reply":"2021-06-13T03:20:05.486537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# New dataset , diabetic-retinopathy-resized","metadata":{}},{"cell_type":"code","source":"pd.read_csv(\"../input/diabetic-retinopathy-resized/trainLabels.csv\")['level'].unique()\n\n# classes","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.490203Z","iopub.execute_input":"2021-06-13T03:20:05.490682Z","iopub.status.idle":"2021-06-13T03:20:05.533587Z","shell.execute_reply.started":"2021-06-13T03:20:05.490635Z","shell.execute_reply":"2021-06-13T03:20:05.532257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\ncropped = pd.read_csv(\"../input/diabetic-retinopathy-resized/trainLabels_cropped.csv\")[:5000]","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.535779Z","iopub.execute_input":"2021-06-13T03:20:05.536206Z","iopub.status.idle":"2021-06-13T03:20:05.573576Z","shell.execute_reply.started":"2021-06-13T03:20:05.536160Z","shell.execute_reply":"2021-06-13T03:20:05.572516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cropped.iloc[0]","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.575351Z","iopub.execute_input":"2021-06-13T03:20:05.575810Z","iopub.status.idle":"2021-06-13T03:20:05.584431Z","shell.execute_reply.started":"2021-06-13T03:20:05.575766Z","shell.execute_reply":"2021-06-13T03:20:05.583474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_names = os.listdir(\"../input/diabetic-retinopathy-resized/resized_train_cropped/resized_train_cropped/\")\nimage_names.sort()\nimage_names[:5]","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.586724Z","iopub.execute_input":"2021-06-13T03:20:05.587034Z","iopub.status.idle":"2021-06-13T03:20:05.634312Z","shell.execute_reply.started":"2021-06-13T03:20:05.586996Z","shell.execute_reply":"2021-06-13T03:20:05.633141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_path = \"../input/diabetic-retinopathy-resized/resized_train_cropped/resized_train_cropped\"\nimg_path = os.path.join(images_path, cropped.iloc[3500].image+\".jpeg\")\nimg_path","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.636096Z","iopub.execute_input":"2021-06-13T03:20:05.636378Z","iopub.status.idle":"2021-06-13T03:20:05.645381Z","shell.execute_reply.started":"2021-06-13T03:20:05.636350Z","shell.execute_reply":"2021-06-13T03:20:05.644099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(img_path)\n\nplt.imshow(img)\n\nimg.size","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.646886Z","iopub.execute_input":"2021-06-13T03:20:05.647203Z","iopub.status.idle":"2021-06-13T03:20:05.936286Z","shell.execute_reply.started":"2021-06-13T03:20:05.647164Z","shell.execute_reply":"2021-06-13T03:20:05.935223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.transforms as transforms\nmy_transform = transforms.Compose([\n    transforms.Resize((299,299)),\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-06-13T03:20:05.937645Z","iopub.execute_input":"2021-06-13T03:20:05.937961Z","iopub.status.idle":"2021-06-13T03:20:05.943945Z","shell.execute_reply.started":"2021-06-13T03:20:05.937922Z","shell.execute_reply":"2021-06-13T03:20:05.942230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\nclass retinaDataset(Dataset):\n    def __init__(self, imagepath=\"../input/diabetic-retinopathy-resized/resized_train_cropped/resized_train_cropped\", total=None,transform=my_transform):\n        self.df = pd.read_csv(\"../input/diabetic-retinopathy-resized/trainLabels_cropped.csv\")\n        \n        if (total is not None):\n            self.df = self.df[:total]\n        \n        self.transform = transform\n        \n        self.imagepath = imagepath\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, index):\n        img_path = os.path.join(self.imagepath, self.df.iloc[index].image +\".jpeg\")\n        img = Image.open(img_path)\n        \n        if(self.transform):\n            img = self.transform(img)\n        \n        return img, torch.tensor(self.df.iloc[index].level)\n","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.945633Z","iopub.execute_input":"2021-06-13T03:20:05.945947Z","iopub.status.idle":"2021-06-13T03:20:05.957048Z","shell.execute_reply.started":"2021-06-13T03:20:05.945915Z","shell.execute_reply":"2021-06-13T03:20:05.955676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = retinaDataset(total=5000)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:05.958484Z","iopub.execute_input":"2021-06-13T03:20:05.958880Z","iopub.status.idle":"2021-06-13T03:20:06.002864Z","shell.execute_reply.started":"2021-06-13T03:20:05.958846Z","shell.execute_reply":"2021-06-13T03:20:06.002039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = train_dataset[0]\n\nplt.imshow(img.permute(1,2,0))\nimg.shape, label, train_dataset.__len__()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:06.004101Z","iopub.execute_input":"2021-06-13T03:20:06.004576Z","iopub.status.idle":"2021-06-13T03:20:06.215607Z","shell.execute_reply.started":"2021-06-13T03:20:06.004541Z","shell.execute_reply":"2021-06-13T03:20:06.214554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 5\nlearning_rate = 1e-4\nnum_epochs = 1\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:06.217000Z","iopub.execute_input":"2021-06-13T03:20:06.217294Z","iopub.status.idle":"2021-06-13T03:20:06.221364Z","shell.execute_reply.started":"2021-06-13T03:20:06.217265Z","shell.execute_reply":"2021-06-13T03:20:06.220737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.models import inception_v3\n\nmodel = inception_v3(pretrained=True)\n\nfor param in model.parameters():\n    param.requires_grad = False\n    \nmodel.fc","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:06.223635Z","iopub.execute_input":"2021-06-13T03:20:06.224127Z","iopub.status.idle":"2021-06-13T03:20:06.579048Z","shell.execute_reply.started":"2021-06-13T03:20:06.224095Z","shell.execute_reply":"2021-06-13T03:20:06.578192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fc = torch.nn.Linear(in_features=2048, out_features=5, bias=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:06.580806Z","iopub.execute_input":"2021-06-13T03:20:06.581457Z","iopub.status.idle":"2021-06-13T03:20:06.586687Z","shell.execute_reply.started":"2021-06-13T03:20:06.581410Z","shell.execute_reply":"2021-06-13T03:20:06.585648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.aux_logits = False","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:06.588194Z","iopub.execute_input":"2021-06-13T03:20:06.588614Z","iopub.status.idle":"2021-06-13T03:20:06.598094Z","shell.execute_reply.started":"2021-06-13T03:20:06.588571Z","shell.execute_reply":"2021-06-13T03:20:06.597101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = model.to(device=device)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:06.599671Z","iopub.execute_input":"2021-06-13T03:20:06.600161Z","iopub.status.idle":"2021-06-13T03:20:06.616463Z","shell.execute_reply.started":"2021-06-13T03:20:06.600117Z","shell.execute_reply":"2021-06-13T03:20:06.615511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:06.617764Z","iopub.execute_input":"2021-06-13T03:20:06.618052Z","iopub.status.idle":"2021-06-13T03:20:06.628942Z","shell.execute_reply.started":"2021-06-13T03:20:06.618022Z","shell.execute_reply":"2021-06-13T03:20:06.627975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = torch.optim.Adam(model.parameters(), lr = learning_rate)\nloss_criterion = torch.nn.CrossEntropyLoss()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:06.630221Z","iopub.execute_input":"2021-06-13T03:20:06.630505Z","iopub.status.idle":"2021-06-13T03:20:06.643187Z","shell.execute_reply.started":"2021-06-13T03:20:06.630478Z","shell.execute_reply":"2021-06-13T03:20:06.642342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:06.644508Z","iopub.execute_input":"2021-06-13T03:20:06.644873Z","iopub.status.idle":"2021-06-13T03:20:06.654436Z","shell.execute_reply.started":"2021-06-13T03:20:06.644757Z","shell.execute_reply":"2021-06-13T03:20:06.653420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:20:06.655739Z","iopub.execute_input":"2021-06-13T03:20:06.656093Z","iopub.status.idle":"2021-06-13T03:20:06.666948Z","shell.execute_reply.started":"2021-06-13T03:20:06.656064Z","shell.execute_reply":"2021-06-13T03:20:06.665902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\nfor epoch in range(num_epochs):\n    for data, target in tqdm(train_dataloader):\n        data = data.to(device=device)\n        target = target.to(device=device)\n        \n        score = model(data)\n        optimizer.zero_grad()\n        \n        loss = loss_criterion(score, target)\n        loss.backward()\n        \n        optimizer.step()\n    \n    print(f\"for epoch {epoch}, loss : {loss}\")\n    ","metadata":{"execution":{"iopub.status.busy":"2021-06-13T03:31:54.891046Z","iopub.execute_input":"2021-06-13T03:31:54.891380Z","iopub.status.idle":"2021-06-13T03:50:19.883930Z","shell.execute_reply.started":"2021-06-13T03:31:54.891351Z","shell.execute_reply":"2021-06-13T03:50:19.882884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def check_accuracy(model, loader):\n    model.eval()\n    \n    correct_output = 0\n    total_output = 0\n    \n    with torch.no_grad():\n        for x, y in tqdm(loader):\n            x = x.to(device=device)\n            y = y.to(device=device)\n            \n            score = model(x)\n            _,predictions = score.max(1)\n            \n            correct_output += (y==predictions).sum()\n            total_output += predictions.shape[0]\n    model.train()\n    print(f\"out of {total_output} , total correct: {correct_output} with an accuracy of {float(correct_output/total_output)*100}\")","metadata":{"execution":{"iopub.status.busy":"2021-06-13T04:02:51.069191Z","iopub.execute_input":"2021-06-13T04:02:51.069598Z","iopub.status.idle":"2021-06-13T04:02:51.077157Z","shell.execute_reply.started":"2021-06-13T04:02:51.069567Z","shell.execute_reply":"2021-06-13T04:02:51.075634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check_accuracy(model, train_dataloader)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T04:02:56.721135Z","iopub.execute_input":"2021-06-13T04:02:56.721484Z","iopub.status.idle":"2021-06-13T04:20:06.438780Z","shell.execute_reply.started":"2021-06-13T04:02:56.721455Z","shell.execute_reply":"2021-06-13T04:20:06.437776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = Image.open(\"../input/diabetic-retinopathy-resized/resized_train_cropped/resized_train_cropped/10007_right.jpeg\")\n\nimg = my_transform(image).unsqueeze(0)\n\nscore = model(img)\n\n_, predictions = score.max(1)\n\nplt.imshow(image)\n\nprint(\"predicted: \", predictions)\n\ndf = pd.read_csv(\"../input/diabetic-retinopathy-resized/trainLabels_cropped.csv\")\n\n# print(df[image][:5])\n# print(df.loc[df[image]==\"10_left\"].level)\nprint(\"actual: \", df.loc[df['image'].isin(['10007_right'])].level)\n","metadata":{"execution":{"iopub.status.busy":"2021-06-13T04:49:48.904171Z","iopub.execute_input":"2021-06-13T04:49:48.904638Z","iopub.status.idle":"2021-06-13T04:49:49.565293Z","shell.execute_reply.started":"2021-06-13T04:49:48.904601Z","shell.execute_reply":"2021-06-13T04:49:49.564470Z"},"trusted":true},"execution_count":null,"outputs":[]}]}