{"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":"2022-04-25T04:54:33.091817Z","iopub.execute_input":"2022-04-25T04:54:33.092245Z","iopub.status.idle":"2022-04-25T04:54:34.090821Z","shell.execute_reply.started":"2022-04-25T04:54:33.092159Z","shell.execute_reply":"2022-04-25T04:54:34.089795Z"},"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":"2022-04-25T04:54:34.09358Z","iopub.execute_input":"2022-04-25T04:54:34.094021Z","iopub.status.idle":"2022-04-25T04:54:34.098745Z","shell.execute_reply.started":"2022-04-25T04:54:34.093984Z","shell.execute_reply":"2022-04-25T04:54:34.098003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ! dir sample","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:54:34.100147Z","iopub.execute_input":"2022-04-25T04:54:34.100805Z","iopub.status.idle":"2022-04-25T04:54:34.111256Z","shell.execute_reply.started":"2022-04-25T04:54:34.100773Z","shell.execute_reply":"2022-04-25T04:54:34.110273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !unzip ../input/diabetic-retinopathy-detection/trainLabels.csv.zip","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:54:34.112599Z","iopub.execute_input":"2022-04-25T04:54:34.113029Z","iopub.status.idle":"2022-04-25T04:54:34.12337Z","shell.execute_reply.started":"2022-04-25T04:54:34.112997Z","shell.execute_reply":"2022-04-25T04:54:34.122523Z"},"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":"2022-04-25T04:54:34.1244Z","iopub.execute_input":"2022-04-25T04:54:34.124795Z","iopub.status.idle":"2022-04-25T04:54:34.134763Z","shell.execute_reply.started":"2022-04-25T04:54:34.124766Z","shell.execute_reply":"2022-04-25T04:54:34.133738Z"},"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":"2022-04-25T04:54:34.135841Z","iopub.execute_input":"2022-04-25T04:54:34.136309Z","iopub.status.idle":"2022-04-25T04:54:34.147095Z","shell.execute_reply.started":"2022-04-25T04:54:34.136269Z","shell.execute_reply":"2022-04-25T04:54:34.145958Z"},"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":"2022-04-25T04:54:34.148382Z","iopub.execute_input":"2022-04-25T04:54:34.148808Z","iopub.status.idle":"2022-04-25T04:54:34.158973Z","shell.execute_reply.started":"2022-04-25T04:54:34.148777Z","shell.execute_reply":"2022-04-25T04:54:34.157974Z"},"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":"2022-04-25T04:54:34.161812Z","iopub.execute_input":"2022-04-25T04:54:34.162292Z","iopub.status.idle":"2022-04-25T04:54:34.170486Z","shell.execute_reply.started":"2022-04-25T04:54:34.162259Z","shell.execute_reply":"2022-04-25T04:54:34.169647Z"},"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":"2022-04-25T04:54:34.171975Z","iopub.execute_input":"2022-04-25T04:54:34.172388Z","iopub.status.idle":"2022-04-25T04:54:34.242312Z","shell.execute_reply.started":"2022-04-25T04:54:34.172358Z","shell.execute_reply":"2022-04-25T04:54:34.241512Z"},"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":"2022-04-25T04:54:34.243341Z","iopub.execute_input":"2022-04-25T04:54:34.243736Z","iopub.status.idle":"2022-04-25T04:54:34.292729Z","shell.execute_reply.started":"2022-04-25T04:54:34.243706Z","shell.execute_reply":"2022-04-25T04:54:34.291921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cropped.iloc[0]","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:54:34.294116Z","iopub.execute_input":"2022-04-25T04:54:34.294486Z","iopub.status.idle":"2022-04-25T04:54:34.303239Z","shell.execute_reply.started":"2022-04-25T04:54:34.294454Z","shell.execute_reply":"2022-04-25T04:54:34.302418Z"},"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":"2022-04-25T04:54:34.304468Z","iopub.execute_input":"2022-04-25T04:54:34.304785Z","iopub.status.idle":"2022-04-25T04:54:35.233371Z","shell.execute_reply.started":"2022-04-25T04:54:34.304747Z","shell.execute_reply":"2022-04-25T04:54:35.23205Z"},"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":"2022-04-25T04:54:35.234799Z","iopub.execute_input":"2022-04-25T04:54:35.235202Z","iopub.status.idle":"2022-04-25T04:54:35.245401Z","shell.execute_reply.started":"2022-04-25T04:54:35.235162Z","shell.execute_reply":"2022-04-25T04:54:35.244459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\nimg = Image.open(img_path)\n\nimport matplotlib.pyplot as plt\n\nplt.imshow(img)\n\nimg.size","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:54:35.246957Z","iopub.execute_input":"2022-04-25T04:54:35.247581Z","iopub.status.idle":"2022-04-25T04:54:35.614208Z","shell.execute_reply.started":"2022-04-25T04:54:35.247538Z","shell.execute_reply":"2022-04-25T04:54:35.61307Z"},"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":"2022-04-25T04:54:35.615637Z","iopub.execute_input":"2022-04-25T04:54:35.616244Z","iopub.status.idle":"2022-04-25T04:54:35.773228Z","shell.execute_reply.started":"2022-04-25T04:54:35.616208Z","shell.execute_reply":"2022-04-25T04:54:35.772192Z"},"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":"2022-04-25T04:54:35.774647Z","iopub.execute_input":"2022-04-25T04:54:35.775089Z","iopub.status.idle":"2022-04-25T04:54:35.783901Z","shell.execute_reply.started":"2022-04-25T04:54:35.775043Z","shell.execute_reply":"2022-04-25T04:54:35.782671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = retinaDataset(total=5000)","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:54:35.785503Z","iopub.execute_input":"2022-04-25T04:54:35.785952Z","iopub.status.idle":"2022-04-25T04:54:35.834773Z","shell.execute_reply.started":"2022-04-25T04:54:35.785882Z","shell.execute_reply":"2022-04-25T04:54:35.833855Z"},"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":"2022-04-25T04:54:35.835899Z","iopub.execute_input":"2022-04-25T04:54:35.836371Z","iopub.status.idle":"2022-04-25T04:54:36.125662Z","shell.execute_reply.started":"2022-04-25T04:54:35.83634Z","shell.execute_reply":"2022-04-25T04:54:36.12464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 5\nlearning_rate = 1e-4\nnum_epochs = 4\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:54:36.126993Z","iopub.execute_input":"2022-04-25T04:54:36.127296Z","iopub.status.idle":"2022-04-25T04:54:36.132121Z","shell.execute_reply.started":"2022-04-25T04:54:36.127265Z","shell.execute_reply":"2022-04-25T04:54:36.131151Z"},"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\n\n","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:54:36.133909Z","iopub.execute_input":"2022-04-25T04:54:36.134322Z","iopub.status.idle":"2022-04-25T04:54:37.314029Z","shell.execute_reply.started":"2022-04-25T04:54:36.13428Z","shell.execute_reply":"2022-04-25T04:54:37.313145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fc = torch.nn.Linear(in_features=2048, out_features=5, bias=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:54:37.316176Z","iopub.execute_input":"2022-04-25T04:54:37.316474Z","iopub.status.idle":"2022-04-25T04:54:37.324102Z","shell.execute_reply.started":"2022-04-25T04:54:37.316445Z","shell.execute_reply":"2022-04-25T04:54:37.323265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.aux_logits = False","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:54:37.325321Z","iopub.execute_input":"2022-04-25T04:54:37.325603Z","iopub.status.idle":"2022-04-25T04:54:37.334142Z","shell.execute_reply.started":"2022-04-25T04:54:37.325576Z","shell.execute_reply":"2022-04-25T04:54:37.333151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = model.to(device=device)","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:54:37.338341Z","iopub.execute_input":"2022-04-25T04:54:37.338986Z","iopub.status.idle":"2022-04-25T04:54:37.357504Z","shell.execute_reply.started":"2022-04-25T04:54:37.338924Z","shell.execute_reply":"2022-04-25T04:54:37.356604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:54:37.359008Z","iopub.execute_input":"2022-04-25T04:54:37.359518Z","iopub.status.idle":"2022-04-25T04:54:37.370131Z","shell.execute_reply.started":"2022-04-25T04:54:37.359486Z","shell.execute_reply":"2022-04-25T04:54:37.369131Z"},"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":"2022-04-25T04:54:37.371822Z","iopub.execute_input":"2022-04-25T04:54:37.372663Z","iopub.status.idle":"2022-04-25T04:54:37.387227Z","shell.execute_reply.started":"2022-04-25T04:54:37.372613Z","shell.execute_reply":"2022-04-25T04:54:37.386413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:54:37.388783Z","iopub.execute_input":"2022-04-25T04:54:37.389518Z","iopub.status.idle":"2022-04-25T04:54:37.398491Z","shell.execute_reply.started":"2022-04-25T04:54:37.38947Z","shell.execute_reply":"2022-04-25T04:54:37.397487Z"},"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":"2022-04-25T04:54:37.400858Z","iopub.execute_input":"2022-04-25T04:54:37.401299Z","iopub.status.idle":"2022-04-25T04:54:37.411757Z","shell.execute_reply.started":"2022-04-25T04:54:37.401256Z","shell.execute_reply":"2022-04-25T04:54:37.410768Z"},"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    print(f\"for epoch {epoch}, loss : {loss}\")\n    ","metadata":{"execution":{"iopub.status.busy":"2022-04-25T05:08:19.3113Z","iopub.execute_input":"2022-04-25T05:08:19.312042Z","iopub.status.idle":"2022-04-25T06:19:40.443306Z","shell.execute_reply.started":"2022-04-25T05:08:19.31199Z","shell.execute_reply":"2022-04-25T06:19:40.442248Z"},"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} , loss : {loss}\")","metadata":{"execution":{"iopub.status.busy":"2022-04-25T06:20:26.537635Z","iopub.execute_input":"2022-04-25T06:20:26.538021Z","iopub.status.idle":"2022-04-25T06:20:26.545239Z","shell.execute_reply.started":"2022-04-25T06:20:26.537988Z","shell.execute_reply":"2022-04-25T06:20:26.544279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check_accuracy(model, train_dataloader)","metadata":{"execution":{"iopub.status.busy":"2022-04-25T06:20:31.902809Z","iopub.execute_input":"2022-04-25T06:20:31.903379Z","iopub.status.idle":"2022-04-25T06:37:10.472252Z","shell.execute_reply.started":"2022-04-25T06:20:31.90333Z","shell.execute_reply":"2022-04-25T06:37:10.471305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}