{"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    for filename in filenames:\n        print(os.path.join(dirname, filename))\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":"2023-03-04T05:51:48.381882Z","iopub.execute_input":"2023-03-04T05:51:48.382650Z","iopub.status.idle":"2023-03-04T05:51:54.501077Z","shell.execute_reply.started":"2023-03-04T05:51:48.382613Z","shell.execute_reply":"2023-03-04T05:51:54.500035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd #For reading csv files.\nimport numpy as np \nimport matplotlib.pyplot as plt #For plotting.\n\nimport PIL.Image as Image #For working with image files.\n\n#Importing torch\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nfrom torch.utils.data import Dataset,DataLoader #For working with data.\n\nfrom torchvision import models,transforms #For pretrained models,image transformations.","metadata":{"execution":{"iopub.status.busy":"2023-03-04T05:51:12.851536Z","iopub.execute_input":"2023-03-04T05:51:12.851948Z","iopub.status.idle":"2023-03-04T05:51:15.767496Z","shell.execute_reply.started":"2023-03-04T05:51:12.851914Z","shell.execute_reply":"2023-03-04T05:51:15.766441Z"},"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":"2023-03-04T05:51:29.370471Z","iopub.execute_input":"2023-03-04T05:51:29.370909Z","iopub.status.idle":"2023-03-04T05:51:29.395393Z","shell.execute_reply.started":"2023-03-04T05:51:29.370872Z","shell.execute_reply":"2023-03-04T05:51:29.392262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"/kaggle/input/aptos2019-blindness-detection/\"\n\ntrain_df = pd.read_csv(f\"{path}train.csv\")\nprint(f'No.of.training_samples: {len(train_df)}')\n\ntest_df = pd.read_csv(f'{path}test.csv')\nprint(f'No.of.testing_samples: {len(test_df)}')","metadata":{"execution":{"iopub.status.busy":"2023-03-04T05:52:46.709937Z","iopub.execute_input":"2023-03-04T05:52:46.710899Z","iopub.status.idle":"2023-03-04T05:52:46.725010Z","shell.execute_reply.started":"2023-03-04T05:52:46.710858Z","shell.execute_reply":"2023-03-04T05:52:46.723853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.diagnosis.hist()\nplt.xticks([0,1,2,3,4])\nplt.grid(False)\nplt.show() ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils import class_weight #For calculating weights for each class.\nclass_weights = class_weight.compute_class_weight(class_weight='balanced',classes=np.array([0,1,2,3,4]),y=train_df['diagnosis'].values)\nclass_weights = torch.tensor(class_weights,dtype=torch.float).to(device)\n \nprint(class_weights) #Prints the calculated weights for the classes.","metadata":{"execution":{"iopub.status.busy":"2023-03-04T05:54:00.387295Z","iopub.execute_input":"2023-03-04T05:54:00.387683Z","iopub.status.idle":"2023-03-04T05:54:04.024273Z","shell.execute_reply.started":"2023-03-04T05:54:00.387650Z","shell.execute_reply":"2023-03-04T05:54:04.023059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#For getting a random image from our training set.\nnum = int(np.random.randint(0,len(train_df)-1,(1,))) #Picks a random number.\nsample_image = (f'{path}train_images/{train_df[\"id_code\"][num]}.png')#Image file.\nsample_image = Image.open(sample_image) \nplt.imshow(sample_image)\nplt.axis('off')\nplt.title(f'Class: {train_df[\"diagnosis\"][num]}') #Class of the random image.\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-04T05:54:05.516229Z","iopub.execute_input":"2023-03-04T05:54:05.516950Z","iopub.status.idle":"2023-03-04T05:54:05.960150Z","shell.execute_reply.started":"2023-03-04T05:54:05.516910Z","shell.execute_reply":"2023-03-04T05:54:05.959120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class dataset(Dataset): # Inherits from the Dataset class.\n    '''\n    dataset class overloads the __init__, __len__, __getitem__ methods of the Dataset class. \n    \n    Attributes :\n        df:  DataFrame object for the csv file.\n        data_path: Location of the dataset.\n        image_transform: Transformations to apply to the image.\n        train: A boolean indicating whether it is a training_set or not.\n    '''\n    \n    def __init__(self,df,data_path,image_transform=None,train=True): # Constructor.\n        super(Dataset,self).__init__() #Calls the constructor of the Dataset class.\n        self.df = df\n        self.data_path = data_path\n        self.image_transform = image_transform\n        self.train = train\n        \n    def __len__(self):\n        return len(self.df) #Returns the number of samples in the dataset.\n    \n    def __getitem__(self,index):\n        image_id = self.df['id_code'][index]\n        image = Image.open(f'{self.data_path}/{image_id}.png') #Image.\n        if self.image_transform :\n            image = self.image_transform(image) #Applies transformation to the image.\n        \n        if self.train :\n            label = self.df['diagnosis'][index] #Label.\n            return image,label #If train == True, return image & label.\n        \n        else:\n            return image #If train != True, return image.","metadata":{"execution":{"iopub.status.busy":"2023-03-04T05:54:14.173321Z","iopub.execute_input":"2023-03-04T05:54:14.173858Z","iopub.status.idle":"2023-03-04T05:54:14.186353Z","shell.execute_reply.started":"2023-03-04T05:54:14.173811Z","shell.execute_reply":"2023-03-04T05:54:14.185284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.manual_seed(101)\nimage_transform = transforms.Compose([transforms.Resize([512,512]),\n                                      transforms.ToTensor(),\n                                      transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))]) #Transformations to apply to the image.\ndata_set = dataset(train_df,f'{path}train_images',image_transform=image_transform)\ntrain_set,valid_set = torch.utils.data.random_split(data_set,[3302,360])\n\n#Split the data_set so that valid_set contains 0.1 samples of the data_set. \n","metadata":{"execution":{"iopub.status.busy":"2023-03-04T05:54:16.442942Z","iopub.execute_input":"2023-03-04T05:54:16.443660Z","iopub.status.idle":"2023-03-04T05:54:16.454479Z","shell.execute_reply.started":"2023-03-04T05:54:16.443621Z","shell.execute_reply":"2023-03-04T05:54:16.453208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.manual_seed(101)\ntrain_dataloader = DataLoader(train_set,batch_size=16,shuffle=True) #DataLoader for train_set.\nvalid_dataloader = DataLoader(valid_set,batch_size=16,shuffle=False) #DataLoader for validation_set.","metadata":{"execution":{"iopub.status.busy":"2023-03-04T05:54:18.814742Z","iopub.execute_input":"2023-03-04T05:54:18.815359Z","iopub.status.idle":"2023-03-04T05:54:18.821340Z","shell.execute_reply.started":"2023-03-04T05:54:18.815311Z","shell.execute_reply":"2023-03-04T05:54:18.820112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_dataloader), len(valid_dataloader)","metadata":{"execution":{"iopub.status.busy":"2023-03-04T05:54:21.287086Z","iopub.execute_input":"2023-03-04T05:54:21.287778Z","iopub.status.idle":"2023-03-04T05:54:21.294627Z","shell.execute_reply.started":"2023-03-04T05:54:21.287742Z","shell.execute_reply":"2023-03-04T05:54:21.293414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import DataLoader\nfrom torchvision import datasets, transforms\nfrom torchvision.utils import make_grid\nimport torch.optim as optim\n\nimport numpy as np\nimport pandas as pd\nfrom sklearn.metrics import confusion_matrix\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-03-04T05:54:23.028577Z","iopub.execute_input":"2023-03-04T05:54:23.029159Z","iopub.status.idle":"2023-03-04T05:54:23.072212Z","shell.execute_reply.started":"2023-03-04T05:54:23.029119Z","shell.execute_reply":"2023-03-04T05:54:23.071233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install efficientnet_pytorch \n","metadata":{"execution":{"iopub.status.busy":"2023-03-04T05:55:25.075384Z","iopub.execute_input":"2023-03-04T05:55:25.076463Z","iopub.status.idle":"2023-03-04T05:55:37.574117Z","shell.execute_reply.started":"2023-03-04T05:55:25.076417Z","shell.execute_reply":"2023-03-04T05:55:37.572762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from efficientnet_pytorch import EfficientNet\nnum_gpu = torch.cuda.device_count()\nprint(num_gpu)\nmodel = EfficientNet.from_pretrained('efficientnet-b0', num_classes=5)\nmodel = model.cuda(0)","metadata":{"execution":{"iopub.status.busy":"2023-03-04T05:55:40.717364Z","iopub.execute_input":"2023-03-04T05:55:40.718489Z","iopub.status.idle":"2023-03-04T05:55:41.477819Z","shell.execute_reply.started":"2023-03-04T05:55:40.718443Z","shell.execute_reply":"2023-03-04T05:55:41.476143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# If you have more thsn one gpus krubbb uncomment\nmodel = nn.DataParallel(model)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-04T05:55:51.665707Z","iopub.execute_input":"2023-03-04T05:55:51.666080Z","iopub.status.idle":"2023-03-04T05:55:51.675607Z","shell.execute_reply.started":"2023-03-04T05:55:51.666042Z","shell.execute_reply":"2023-03-04T05:55:51.674574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_fn = nn.CrossEntropyLoss()\noptimizer  = optim.Adam(model.parameters(), lr=0.01)","metadata":{"execution":{"iopub.status.busy":"2023-03-04T05:55:54.327688Z","iopub.execute_input":"2023-03-04T05:55:54.328159Z","iopub.status.idle":"2023-03-04T05:55:54.336321Z","shell.execute_reply.started":"2023-03-04T05:55:54.328112Z","shell.execute_reply":"2023-03-04T05:55:54.335290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 10\nfor epoch in tqdm(range(num_epochs)):\n    model.train()\n    train_loss = 0.0\n    for i,data in enumerate(train_dataloader):\n        inputs, labels = data\n        inputs = inputs.cuda()\n        labels = labels.cuda()\n\n        optimizer.zero_grad()\n\n        outputs = model(inputs)\n        loss = loss_fn(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        train_loss += loss.item()\n    print('Epoch: {}, Train Loss: {:.4f}'.format(epoch, train_loss/len(train_dataloader)))\n    model.eval()\n    correct = 0\n    total = 0\n    with torch.no_grad():\n        for test in valid_dataloader:\n            img, target = test\n            img = img.cuda()\n            target = target.cuda()\n            output2 = model(img)\n            _,pred = torch.max(output2.data,1)\n            total += target.size(0)\n            correct += (pred == target).sum().item()\n    print(\"Accuracy : %d %%\" % (100* correct/total))","metadata":{"execution":{"iopub.status.busy":"2023-03-04T05:56:13.458588Z","iopub.execute_input":"2023-03-04T05:56:13.459160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}