{"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":"2021-12-12T22:36:46.5675Z","iopub.execute_input":"2021-12-12T22:36:46.567888Z","iopub.status.idle":"2021-12-12T22:36:52.300303Z","shell.execute_reply.started":"2021-12-12T22:36:46.567816Z","shell.execute_reply":"2021-12-12T22:36:52.299362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport os\nimport numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport PIL.Image as Image\n\nfrom sklearn.utils import class_weight, shuffle\nfrom sklearn.model_selection import train_test_split\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset,DataLoader\n\nfrom torchvision import models,transforms\nfrom tqdm import trange\n\n# Set random seed for reproducibility\nseed = 1234\n# cuDNN uses nondeterministic algorithms, set some options for reproducibility\ntorch.backends.cudnn.deterministic = True\ntorch.backends.cudnn.benchmark = False\ntorch.manual_seed(seed)\n\n%matplotlib inline\n# Use GPU if it's available or else use CPU.\nDEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n# DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'\nprint(DEVICE)\n\nIMG_SIZE = 512\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2021-12-12T22:47:03.255101Z","iopub.execute_input":"2021-12-12T22:47:03.255355Z","iopub.status.idle":"2021-12-12T22:47:06.248497Z","shell.execute_reply.started":"2021-12-12T22:47:03.255326Z","shell.execute_reply":"2021-12-12T22:47:06.247756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\nprint(\"No.of.training_samples:\",len(train_data))\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-12T22:47:06.250193Z","iopub.execute_input":"2021-12-12T22:47:06.250802Z","iopub.status.idle":"2021-12-12T22:47:06.282662Z","shell.execute_reply.started":"2021-12-12T22:47:06.250761Z","shell.execute_reply":"2021-12-12T22:47:06.281965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Pre Processing","metadata":{}},{"cell_type":"code","source":"x = train_data['id_code']\ny = train_data['diagnosis']\n\nx, y = shuffle(x, y, random_state=seed)\n\ntrain_x, valid_x, train_y, valid_y = train_test_split(x, y, test_size=0.2,\n                                                      stratify=y, random_state=seed)\nprint(train_x.shape, train_y.shape, valid_x.shape, valid_y.shape)\ntrain_y.hist()\nvalid_y.hist()","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:39:59.823117Z","iopub.execute_input":"2021-12-12T10:39:59.82379Z","iopub.status.idle":"2021-12-12T10:40:00.123455Z","shell.execute_reply.started":"2021-12-12T10:39:59.823749Z","shell.execute_reply":"2021-12-12T10:40:00.122723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(25, 16))\n# Display 5 images from each class\nfor class_id in sorted(train_y.unique()):\n    for i, (idx, row) in enumerate(train_data.loc[train_data['diagnosis'] == class_id].sample(5, random_state=seed).iterrows()):\n        ax = fig.add_subplot(5, 5, class_id * 5 + i + 1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n        plt.imshow(image)\n        ax.set_title('Class:'+str(class_id))","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:40:01.04637Z","iopub.execute_input":"2021-12-12T10:40:01.046641Z","iopub.status.idle":"2021-12-12T10:40:06.458296Z","shell.execute_reply.started":"2021-12-12T10:40:01.04661Z","shell.execute_reply":"2021-12-12T10:40:06.45512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Grayscaling the images\nfig = plt.figure(figsize=(25, 16))\nfor class_id in sorted(train_y.unique()):\n    for i, (idx, row) in enumerate(train_data.loc[train_data['diagnosis'] == class_id].sample(5, random_state=seed).iterrows()):\n        ax = fig.add_subplot(5, 5, class_id * 5 + i + 1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n        plt.imshow(image, cmap='gray')\n        ax.set_title('Class:'+str(class_id))","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:40:06.459862Z","iopub.execute_input":"2021-12-12T10:40:06.460404Z","iopub.status.idle":"2021-12-12T10:40:12.399097Z","shell.execute_reply.started":"2021-12-12T10:40:06.460359Z","shell.execute_reply":"2021-12-12T10:40:12.398392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#inch\ndpi = 80 \n\npath=f\"../input/aptos2019-blindness-detection/train_images/cd54d022e37d.png\"\nimage = cv2.imread(path)\nimage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\nheight, width = image.shape\nprint(height, width)\n\nSCALE=2\nfigsize = (width / float(dpi))/SCALE, (height / float(dpi))/SCALE\n\nfig = plt.figure(figsize=figsize)\nplt.imshow(image, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:40:17.967539Z","iopub.execute_input":"2021-12-12T10:40:17.968105Z","iopub.status.idle":"2021-12-12T10:40:19.620447Z","shell.execute_reply.started":"2021-12-12T10:40:17.968065Z","shell.execute_reply":"2021-12-12T10:40:19.619732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To improve lighting conditon\nfig = plt.figure(figsize=(25, 16))\nfor class_id in sorted(train_y.unique()):\n    for i, (idx, row) in enumerate(train_data.loc[train_data['diagnosis'] == class_id].sample(5, random_state=seed).iterrows()):\n        ax = fig.add_subplot(5, 5, class_id * 5 + i + 1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n        image=cv2.addWeighted( image,4, cv2.GaussianBlur( image , (0,0) , IMG_SIZE/10) ,-4 ,128)\n        plt.imshow(image, cmap='gray')\n        ax.set_title('Class:'+str(class_id))","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:40:26.232332Z","iopub.execute_input":"2021-12-12T10:40:26.232623Z","iopub.status.idle":"2021-12-12T10:40:40.785566Z","shell.execute_reply.started":"2021-12-12T10:40:26.232592Z","shell.execute_reply":"2021-12-12T10:40:40.784852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def crop_image_from_gray(img,tol=7):\n    if img.ndim ==2:\n        mask = img>tol\n        return img[np.ix_(mask.any(1),mask.any(0))]\n    elif img.ndim==3:\n        gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n        mask = gray_img>tol\n        \n        check_shape = img[:,:,0][np.ix_(mask.any(1),mask.any(0))].shape[0]\n        if (check_shape == 0): # image is too dark so that we crop out everything,\n            return img # return original image\n        else:\n            img1=img[:,:,0][np.ix_(mask.any(1),mask.any(0))]\n            img2=img[:,:,1][np.ix_(mask.any(1),mask.any(0))]\n            img3=img[:,:,2][np.ix_(mask.any(1),mask.any(0))]\n            img = np.stack([img1,img2,img3],axis=-1)\n        return img","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:40:45.936812Z","iopub.execute_input":"2021-12-12T10:40:45.937529Z","iopub.status.idle":"2021-12-12T10:40:45.948802Z","shell.execute_reply.started":"2021-12-12T10:40:45.937492Z","shell.execute_reply":"2021-12-12T10:40:45.947823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_trans_color(path, sigmaX=10):\n    image = cv2.imread(path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = crop_image_from_gray(image)\n    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n    image=cv2.addWeighted( image,4, cv2.GaussianBlur( image , (0,0) , sigmaX) ,-4 ,128)\n    return image","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:40:47.113291Z","iopub.execute_input":"2021-12-12T10:40:47.11359Z","iopub.status.idle":"2021-12-12T10:40:47.119869Z","shell.execute_reply.started":"2021-12-12T10:40:47.113555Z","shell.execute_reply":"2021-12-12T10:40:47.118973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(25, 16))\nfor class_id in sorted(train_y.unique()):\n    for i, (idx, row) in enumerate(train_data.loc[train_data['diagnosis'] == class_id].sample(4, random_state=seed).iterrows()):\n        ax = fig.add_subplot(5, 4, class_id * 4 + i + 1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = load_trans_color(path,sigmaX=30)\n        plt.imshow(image)\n        ax.set_title('Class:'+str(class_id))","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:40:50.868442Z","iopub.execute_input":"2021-12-12T10:40:50.869123Z","iopub.status.idle":"2021-12-12T10:40:59.87231Z","shell.execute_reply.started":"2021-12-12T10:40:50.86908Z","shell.execute_reply":"2021-12-12T10:40:59.87057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def circle_crop(img, sigmaX=10):   \n    \"\"\"\n    Create circular crop around image centre    \n    \"\"\"    \n    \n    img = cv2.imread(img)\n    img = crop_image_from_gray(img)    \n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    \n    height, width, depth = img.shape    \n    \n    x = int(width/2)\n    y = int(height/2)\n    r = np.amin((x,y))\n    \n    circle_img = np.zeros((height, width), np.uint8)\n    cv2.circle(circle_img, (x,y), int(r), 1, thickness=-1)\n    img = cv2.bitwise_and(img, img, mask=circle_img)\n    img = crop_image_from_gray(img)\n    img=cv2.addWeighted (img,4, cv2.GaussianBlur(img , (0,0) , sigmaX) ,-4 ,128)\n    return img ","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:41:12.574409Z","iopub.execute_input":"2021-12-12T10:41:12.574671Z","iopub.status.idle":"2021-12-12T10:41:12.583026Z","shell.execute_reply.started":"2021-12-12T10:41:12.574643Z","shell.execute_reply":"2021-12-12T10:41:12.582081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Circle crop\nfig = plt.figure(figsize=(25, 16))\nfor class_id in sorted(train_y.unique()):\n    for i, (idx, row) in enumerate(train_data.loc[train_data['diagnosis'] == class_id].sample(4, random_state=seed).iterrows()):\n        ax = fig.add_subplot(5, 4, class_id * 4 + i + 1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = circle_crop(path,sigmaX=30)\n        plt.imshow(image)\n        ax.set_title('Class:'+str(class_id))","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:41:13.419297Z","iopub.execute_input":"2021-12-12T10:41:13.420011Z","iopub.status.idle":"2021-12-12T10:41:41.40026Z","shell.execute_reply.started":"2021-12-12T10:41:13.419972Z","shell.execute_reply":"2021-12-12T10:41:41.399323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dpi = 80 #inch\n\n# path=f\"../input/aptos2019-blindness-detection/train_images/5c7ab966a3ee.png\" # notice upper part\npath=f\"../input/aptos2019-blindness-detection/train_images/cd54d022e37d.png\" # lower-right, can be class3\nimage = circle_crop(path, sigmaX=10)\n\nheight, width = IMG_SIZE, IMG_SIZE\nprint(height, width)\n\nSCALE=1\nfigsize = (width / float(dpi))/SCALE, (height / float(dpi))/SCALE\n\nfig = plt.figure(figsize=figsize)\nplt.imshow(image, cmap='gray')\n","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:41:41.401765Z","iopub.execute_input":"2021-12-12T10:41:41.402234Z","iopub.status.idle":"2021-12-12T10:41:42.868643Z","shell.execute_reply.started":"2021-12-12T10:41:41.4022Z","shell.execute_reply":"2021-12-12T10:41:42.867807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"CNN","metadata":{}},{"cell_type":"code","source":"# Distribution of classes.\ncounts = train_data.diagnosis.value_counts()\nclass_list = ['No DR', 'Mild', 'Moderate', 'Severe', 'Proliferate']\nfor i,x in enumerate(class_list):\n    counts[x] = counts.pop(i)\n\nplt.figure(figsize=(10,5))\nsns.barplot(x=counts.index, y=counts.values, alpha=0.8, palette='bright')\nplt.title('Distribution of Output Classes')\nplt.ylabel('Number of Occurrences', fontsize=12)\nplt.xlabel('Target Classes', fontsize=12)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-12T22:47:21.608516Z","iopub.execute_input":"2021-12-12T22:47:21.609089Z","iopub.status.idle":"2021-12-12T22:47:21.83848Z","shell.execute_reply.started":"2021-12-12T22:47:21.609051Z","shell.execute_reply":"2021-12-12T22:47:21.837619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#As the data is imbalanced, we've to calculate weights for each class, which can be used in calculating loss.\n\n#For calculating weights for each class.\nfrom sklearn.utils import class_weight \nclass_weights = class_weight.compute_class_weight(class_weight='balanced', classes=np.array([0,1,2,3,4]), y=train_data['diagnosis'].values)\nclass_weights = torch.tensor(class_weights, dtype=torch.float).to(DEVICE)\n\n# Print the calculated weights for the classes.\nprint(class_weights) ","metadata":{"execution":{"iopub.status.busy":"2021-12-12T22:47:22.739224Z","iopub.execute_input":"2021-12-12T22:47:22.739502Z","iopub.status.idle":"2021-12-12T22:47:25.673047Z","shell.execute_reply.started":"2021-12-12T22:47:22.739469Z","shell.execute_reply":"2021-12-12T22:47:25.67229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(12, 12))\n# display random 16 images\ntrain_imgs = os.listdir(\"/kaggle/input/aptos2019-blindness-detection/train_images/\")\nfor idx, img in enumerate(np.random.choice(train_imgs, 16)):\n    ax = fig.add_subplot(4, 4, idx+1, xticks=[], yticks=[])\n    im = Image.open('/kaggle/input/aptos2019-blindness-detection/train_images/' + img)\n    plt.imshow(im)\n    lab = train_data.loc[train_data['id_code'] == img.split('.')[0], 'diagnosis'].values[0]\n    ax.set_title('Class: %s'%lab)","metadata":{"execution":{"iopub.status.busy":"2021-12-12T22:47:27.794455Z","iopub.execute_input":"2021-12-12T22:47:27.794988Z","iopub.status.idle":"2021-12-12T22:47:37.112017Z","shell.execute_reply.started":"2021-12-12T22:47:27.794949Z","shell.execute_reply":"2021-12-12T22:47:37.109696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Inherits from the Dataset class.\nclass dataset(Dataset): \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):\n        super(Dataset,self).__init__()\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        # Returns the number of samples in the dataset.\n        return len(self.df) \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')\n        # If using pre-processing\n#         cv_image = load_trans_color(f'{self.data_path}/{image_id}.png', sigmaX=10)\n        # Converting the cv image\n#         image = Image.fromarray(cv_image)\n  \n        # Apply transformation to the image.\n        if self.image_transform :\n            image = self.image_transform(image) \n        \n        if self.train :\n            label = self.df['diagnosis'][index]\n            return image, label \n        else:\n            return image\n          ","metadata":{"execution":{"iopub.status.busy":"2021-12-12T22:47:59.399761Z","iopub.execute_input":"2021-12-12T22:47:59.400723Z","iopub.status.idle":"2021-12-12T22:47:59.408398Z","shell.execute_reply.started":"2021-12-12T22:47:59.400677Z","shell.execute_reply":"2021-12-12T22:47:59.407622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initial transform (convert to PyTorch Tensor only)\n# transform = transforms.Compose([\n#     transforms.ToTensor(),\n# ])\n\n## Use the following lines to check the basic statistics of this dataset\n# Calculate training data mean and standard deviation to apply normalization to data\n# train_data.data are of type uint8 (range 0,255) so divide by 255.\n# train_mean = train_data.data.double().mean() / 255.\n# train_std = train_data.data.double().std() / 255.\n# print(f'Train Data: Mean={train_mean}, Std={train_std}')\n\n## Optional: Perform normalization of train and test data using calculated training mean and standard deviation\n# This will convert data to be approximately standard normal\n# transform = transforms.Compose([\n#    transforms.ToTensor(),\n#    transforms.Normalize((train_mean, ), (train_std, ))\n# ])\n\ntransform = transforms.Compose([transforms.Resize([512,512]),\n                                      transforms.ToTensor(),\n                                      transforms.RandomHorizontalFlip(),\n                                      transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))]) #Transformations to apply to the image.\n\n# # train transformations\n# train_transform = transforms.Compose([\n#                                   transforms.RandomRotation((-360, 360)),\n#                                   transforms.RandomHorizontalFlip(p=0.5),\n#                                   transforms.RandomVerticalFlip()\n#                                  ])\n# #test transformations\n# 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))]) \n\ndata_set = dataset(train_data, '../input/aptos2019-blindness-detection/train_images', image_transform=transform)\n\ntrain_len = int(0.8*len(data_set))\ntest_len = len(data_set) - train_len\nprint(len(data_set), train_len, test_len)\n\n# Split the data_set so that valid_set contains 0.2 samples of the data_set. \ntrain_data, test_data = torch.utils.data.random_split(data_set, [train_len, test_len])\n\n# train_data = dataset(train_data, f'{path}train_images', image_transform=transform)\n# test_data = dataset(test_data, f'{path}test_images', image_transform=transform)\n\n# train_data.transform = transform\n# test_data.transform = transform\n\nbatch_size = 32\ntorch.manual_seed(seed)\ntrain_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=True) \ntest_loader = DataLoader(test_data, batch_size=batch_size, shuffle=True, num_workers=True) ","metadata":{"execution":{"iopub.status.busy":"2021-12-12T22:49:39.04274Z","iopub.execute_input":"2021-12-12T22:49:39.043168Z","iopub.status.idle":"2021-12-12T22:49:39.065346Z","shell.execute_reply.started":"2021-12-12T22:49:39.043131Z","shell.execute_reply":"2021-12-12T22:49:39.064559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Randomly sample 20 images of the training dataset\nimages, labels = iter(train_loader).next()\n\n# Print information and statistics of the first batch of images\nprint(\"Images shape: \", images.shape)\nprint(\"Labels shape: \", labels.shape)\nprint(f'Mean={images.mean()}, Std={images.std()}')\n\nfig = plt.figure(figsize=(12, 10))\nfor i in np.arange(20):\n    plt.subplot(4, 5, i+1)\n    plt.imshow(images[i][0].squeeze(), cmap='gray', interpolation='none')\n    plt.title(f'Class: {labels[i]}', fontsize=14)\n    plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2021-12-12T22:49:49.339379Z","iopub.execute_input":"2021-12-12T22:49:49.339712Z","iopub.status.idle":"2021-12-12T22:50:01.591093Z","shell.execute_reply.started":"2021-12-12T22:49:49.339676Z","shell.execute_reply":"2021-12-12T22:50:01.587176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install torchsummary","metadata":{"execution":{"iopub.status.busy":"2021-12-12T22:50:01.592816Z","iopub.execute_input":"2021-12-12T22:50:01.593552Z","iopub.status.idle":"2021-12-12T22:50:10.721648Z","shell.execute_reply.started":"2021-12-12T22:50:01.593515Z","shell.execute_reply":"2021-12-12T22:50:10.720821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Net(nn.Module):\n    def __init__ (self):\n        super(Net, self).__init__()\n        self.conv1 = nn.Conv2d(3, 8, 5)\n        self.pool = nn.MaxPool2d(2, 2) \n        self.conv2 = nn.Conv2d(8, 8, 5)\n        self.conv3 = nn.Conv2d(8, 16, 5)\n\n        self.fc1 = nn.Linear(57600, 2000)\n        self.fc2 = nn.Linear(2000, 120)\n        self.fc3 = nn.Linear(120, 80)\n        self.fc4 = nn.Linear(80, 5)\n        # Generalizes outputs\n        self.dropout = nn.Dropout(0.2) \n\n    def forward(self, x):\n        x = self.pool(F.relu(self.conv1(x)))\n        x = self.pool(F.relu(self.conv2(x)))\n        x = self.pool(F.relu(self.conv3(x)))\n        x = self.dropout(x) \n\n        # print(x.shape)\n        x = x.view(-1, 16*60*60)\n        x = F.relu(self.fc1(x))\n        x = self.dropout(x)\n        x = F.relu(self.fc2(x))\n        x = F.relu(self.fc3(x))\n        x = self.dropout(x)\n        x = self.fc4(x)\n        return x\n    \nmodel = Net().to(DEVICE)\n# sanity check\nprint(model)\nfrom torchsummary import summary\nsummary(model, (3, 512, 512))","metadata":{"execution":{"iopub.status.busy":"2021-12-12T22:50:35.17232Z","iopub.execute_input":"2021-12-12T22:50:35.172634Z","iopub.status.idle":"2021-12-12T22:50:42.027595Z","shell.execute_reply.started":"2021-12-12T22:50:35.172582Z","shell.execute_reply":"2021-12-12T22:50:42.026837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_one_epoch(train_loader, model, device, optimizer, log_interval, epoch, loss_fn):\n    model.train()\n    total = 0\n    num_correct = 0\n    running_loss = 0\n    \n    for i, (img, label) in enumerate(train_loader):\n        img, label = img.to(device), label.to(device)\n        optimizer.zero_grad()\n        output = model(img)\n        loss = loss_fn(output,label)\n        running_loss += loss.item()\n        \n        total += label.size(0)\n        pred = output.max(1, keepdim = True)[1]\n        num_correct += pred.eq(label.view_as(pred)).sum().item()\n        \n        loss.backward()\n        optimizer.step()\n    \n    avg_loss = running_loss/len(train_loader)\n    train_accuracy = 100*(num_correct/total)\n    print(f'\\nTraining Loss = {avg_loss:.6f}',end='\\t')\n    print(f'Accuracy on Training set = {100*(num_correct/total):.6f}% [{num_correct}/{total}]') #Prints the Accuracy.\n\n    return avg_loss, train_accuracy","metadata":{"execution":{"iopub.status.busy":"2021-12-12T22:52:30.9464Z","iopub.execute_input":"2021-12-12T22:52:30.946732Z","iopub.status.idle":"2021-12-12T22:52:30.957786Z","shell.execute_reply.started":"2021-12-12T22:52:30.946696Z","shell.execute_reply":"2021-12-12T22:52:30.95701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_one_epoch(test_loader, model, device, loss_fn):\n    model.eval()\n    test_loss = 0\n    num_correct = 0\n    total = 0\n    \n    with torch.no_grad():\n        for i, (img, label) in enumerate(test_loader):\n            img, label = img.to(device), label.to(device)\n            output = model(img)\n            \n            pred = output.max(1, keepdim = True)[1]\n            num_correct += pred.eq(label.view_as(pred)).sum().item()\n            test_loss += loss_fn(output, label).item()\n            total += label.size(0)\n            \n    test_loss /= len(test_loader)\n    test_accuracy = 100*(num_correct/total)\n    print(f'\\nTest Loss = {test_loss:.6f}',end='\\t')\n    print(f'Accuracy on Validation set = {100*(num_correct/total):.6f}% [{num_correct}/{total}]') #Prints the Accuracy.\n    return test_loss, num_correct, test_accuracy","metadata":{"execution":{"iopub.status.busy":"2021-12-12T22:52:31.431336Z","iopub.execute_input":"2021-12-12T22:52:31.431581Z","iopub.status.idle":"2021-12-12T22:52:31.441438Z","shell.execute_reply.started":"2021-12-12T22:52:31.431551Z","shell.execute_reply":"2021-12-12T22:52:31.440716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hyperparameters\nlr = 0.01\nmax_epochs=10\ngamma = 0.95\n\n# Recording data\nlog_interval = 100\n\n# Instantiate optimizer (model was created in previous cell)\noptimizer = torch.optim.SGD(model.parameters(), lr=lr)\nloss_fn   = nn.CrossEntropyLoss(weight=class_weights)\n\ntrain_losses = []\ntest_losses = []\ntest_correct = []\ntrain_accuracy, test_accuracy = [], []\n\nfor epoch in trange(max_epochs, leave=True, desc='Epochs'):\n    print(f'\\nEpoch {epoch+1}/{max_epochs}')\n    print('-------------------------------')\n    train_loss, train_acc = train_one_epoch(train_loader, model, DEVICE, optimizer, log_interval, epoch, loss_fn)\n    test_loss, num_correct, test_acc = test_one_epoch(test_loader, model, DEVICE, loss_fn)\n\n    # Record results\n    train_losses.append(train_loss)\n    test_losses.append(test_loss)\n    test_correct.append(num_correct)\n    train_accuracy.append(train_acc)\n    test_accuracy.append(test_acc)\n    print(train_losses, test_losses, train_accuracy, test_accuracy)\n\nprint('\\nTraining has completed!')\n\nprint(f\"Test accuracy: {test_correct[-1]/len(test_loader.dataset)}\")","metadata":{"execution":{"iopub.status.busy":"2021-12-12T22:52:32.528115Z","iopub.execute_input":"2021-12-12T22:52:32.528376Z","iopub.status.idle":"2021-12-13T00:16:43.686216Z","shell.execute_reply.started":"2021-12-12T22:52:32.528348Z","shell.execute_reply":"2021-12-13T00:16:43.685062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 512*512 with whole preprocessing, augmentation and dropout 17 epochs\n# train_accuracy = [49.197678, 49.265961, 50.051212, 51.143735, 52.645954, 53.806760, 51.758279, 50.529191, 48.207579, 49.778081, 51.075452, 51.485149, 51.280300, 54.353022, 51.758279, 52.236258]\n# test_accuracy = [49.658936, 49.658936, 49.658936, 52.114598, 59.481583, 51.296044, 59.208731, 44.747613, 54.570259, 49.300102, 56.480218, 52.114598, 53.615280, 31.514325, 46.930423, 47.203274, 50.341064, 48.294679]","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:28:24.495425Z","iopub.execute_input":"2021-12-12T10:28:24.496211Z","iopub.status.idle":"2021-12-12T10:28:24.502038Z","shell.execute_reply.started":"2021-12-12T10:28:24.496157Z","shell.execute_reply":"2021-12-12T10:28:24.501101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the graph of train_losses & valid_losses against nb_epochs.\nepochs = range(max_epochs)\nplt.plot(epochs, train_losses, 'g', label='Training loss')\nplt.plot(epochs, test_losses, 'b', label='Test loss')\nplt.title('Training and Test loss (Image size: 512*512)')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-13T00:22:23.370215Z","iopub.execute_input":"2021-12-13T00:22:23.370491Z","iopub.status.idle":"2021-12-13T00:22:23.579102Z","shell.execute_reply.started":"2021-12-13T00:22:23.37046Z","shell.execute_reply":"2021-12-13T00:22:23.578438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the graph of train_accuracy & test_accuracy against nb_epochs.\nepochs = range(max_epochs)\nplt.plot(epochs, train_accuracy, 'g', label='Training Accuracy')\nplt.plot(epochs, test_accuracy, 'b', label='Test Accuracy')\nplt.title('Training and Test Accuracy (Image: 512*512)')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-13T00:23:07.877996Z","iopub.execute_input":"2021-12-13T00:23:07.878261Z","iopub.status.idle":"2021-12-13T00:23:08.09832Z","shell.execute_reply.started":"2021-12-13T00:23:07.87823Z","shell.execute_reply":"2021-12-13T00:23:08.097467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\nresults = []\ni = 0\nj = 0\nk = 0\nl = 0\nm = 0\nfor x, y in DataLoader(data_set):\n  if (y.numpy()[0] == 0 and i<20):\n    result = model(x.to(DEVICE)).argmax(dim=1).cpu().detach()\n    results.append((result.numpy()[0], y.numpy()[0]))\n    i = i+1\n  if (y.numpy()[0] == 1 and j<20):\n    result = model(x.to(DEVICE)).argmax(dim=1).cpu().detach()\n    results.append((result.numpy()[0], y.numpy()[0]))\n    j = j+1\n  if (y.numpy()[0] == 2 and k<20):\n    result = model(x.to(DEVICE)).argmax(dim=1).cpu().detach()\n    results.append((result.numpy()[0], y.numpy()[0]))\n    k = k+1\n  if (y.numpy()[0] == 3 and l<20):\n    result = model(x.to(DEVICE)).argmax(dim=1).cpu().detach()\n    results.append((result.numpy()[0], y.numpy()[0]))\n    l = l+1\n  if (y.numpy()[0] == 4 and m<20):\n    result = model(x.to(DEVICE)).argmax(dim=1).cpu().detach()\n    results.append((result.numpy()[0], y.numpy()[0]))\n    m = m+1\n  if(i+j+k+l+m >=100):\n    break","metadata":{"execution":{"iopub.status.busy":"2021-12-13T00:23:54.223789Z","iopub.execute_input":"2021-12-13T00:23:54.224362Z","iopub.status.idle":"2021-12-13T00:24:42.62745Z","shell.execute_reply.started":"2021-12-13T00:23:54.224321Z","shell.execute_reply":"2021-12-13T00:24:42.626658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport numpy as np\n\nlabels = [val[1] for val in results]\npreds = [val[0] for val in results]\n\ncm = confusion_matrix(labels, preds, labels=[0, 1, 2, 3, 4])\ncm","metadata":{"execution":{"iopub.status.busy":"2021-12-13T00:25:22.172216Z","iopub.execute_input":"2021-12-13T00:25:22.172495Z","iopub.status.idle":"2021-12-13T00:25:22.184796Z","shell.execute_reply.started":"2021-12-13T00:25:22.172465Z","shell.execute_reply":"2021-12-13T00:25:22.184028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm_df = pd.DataFrame(cm,\n                     index = ['0','1','2', '3', '4'], \n                     columns = ['0','1','2', '3', '4'])\ncm_df","metadata":{"execution":{"iopub.status.busy":"2021-12-13T00:25:23.720908Z","iopub.execute_input":"2021-12-13T00:25:23.721156Z","iopub.status.idle":"2021-12-13T00:25:23.738168Z","shell.execute_reply.started":"2021-12-13T00:25:23.721129Z","shell.execute_reply":"2021-12-13T00:25:23.73749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total = sum(cm_df.sum(axis=1))","metadata":{"execution":{"iopub.status.busy":"2021-12-13T00:25:24.810388Z","iopub.execute_input":"2021-12-13T00:25:24.81138Z","iopub.status.idle":"2021-12-13T00:25:24.816148Z","shell.execute_reply.started":"2021-12-13T00:25:24.811337Z","shell.execute_reply":"2021-12-13T00:25:24.815023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr = np.array(cm_df)\ncnfm = []\nfor i in range(0,5):\n  TP = arr[:][i][i]\n  FN = sum(arr[:][i]) - arr[:][i][i]\n  FP = cm_df.sum(axis=0)[i] - arr[:][i][i]\n  TN = total - TP\n  precision = TP /(TP+FP)\n  recall  = TP/(TP+FN)\n  f1 = 2*precision*recall/(precision+recall)\n  cnfm.append([i, TP,FN,FP,TN, precision, recall, f1])\nf1_result = pd.DataFrame(cnfm, columns = ['severity', 'True Positive','False Negative','False Positive', 'True Negative', 'Precision', 'Recall', 'F1-score'])\nf1_result","metadata":{"execution":{"iopub.status.busy":"2021-12-13T00:25:26.070592Z","iopub.execute_input":"2021-12-13T00:25:26.071523Z","iopub.status.idle":"2021-12-13T00:25:26.096194Z","shell.execute_reply.started":"2021-12-13T00:25:26.071468Z","shell.execute_reply":"2021-12-13T00:25:26.094981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting the confusion matrix\nplt.figure(figsize=(5,4))\nsns.heatmap(cm_df, annot=True)\nplt.title('Confusion Matrix')\nplt.ylabel('Actual Values')\nplt.xlabel('Predicted Values')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-13T00:26:33.919291Z","iopub.execute_input":"2021-12-13T00:26:33.919932Z","iopub.status.idle":"2021-12-13T00:26:34.199281Z","shell.execute_reply.started":"2021-12-13T00:26:33.919892Z","shell.execute_reply":"2021-12-13T00:26:34.198508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_curve\nfrom sklearn.metrics import roc_auc_score, auc\n\ndef test_class_probabilities(model, test_loader):\n    model.eval()\n    actuals = []\n    probabilities = []\n    i = 0\n    j = 0\n    k = 0\n    l = 0\n    f = 0\n    with torch.no_grad():\n        for x, y in test_loader:\n          y = y.numpy()[0]\n          outputs = model(x.to(DEVICE))\n          prediction = outputs.argmax(dim=1, keepdim=True).cpu().detach()\n          # if (prediction.numpy()[0] == y):\n          if (y == 0 and i<10):\n            actuals.append(y)\n            values = outputs.cpu().detach().numpy()[0]\n            m = -1*min(values)\n            values = [val+m for val in values]\n            prob = values/sum(values)\n            probabilities.append(np.exp(prob))\n            i = i+1\n          if (y == 1 and j<10):\n            actuals.append(y)\n            values = outputs.cpu().detach().numpy()[0]\n            m = -1*min(values)\n            values = [val+m for val in values]\n            prob = values/sum(values)\n            probabilities.append(np.exp(prob))\n            j = j+1\n          if (y == 2 and k<10):\n            actuals.append(y)\n            values = outputs.cpu().detach().numpy()[0]\n            m = -1*min(values)\n            values = [val+m for val in values]\n            prob = values/sum(values)\n            probabilities.append(np.exp(prob))\n            k = k+1\n          if (y == 3 and l<10):\n            actuals.append(y)\n            values = outputs.cpu().detach().numpy()[0]\n            m = -1*min(values)\n            values = [val+m for val in values]\n            prob = values/sum(values)\n            probabilities.append(np.exp(prob))\n            l = l+1\n          if (y == 4 and f<10):\n            actuals.append(y)\n            values = outputs.cpu().detach().numpy()[0]\n            m = -1*min(values)\n            values = [val+m for val in values]\n            prob = values/sum(values)\n            probabilities.append(np.exp(prob))\n            f = f+1\n          if(i+j+k+l+f >=50):\n            break\n    return actuals,probabilities\n\n\ndef plot_roc( actuals,  probabilities):\n    \"\"\"\n    compute ROC curve and ROC area for each class in each fold\n\n    \"\"\"\n    fpr = {}\n    tpr = {}\n    thresh ={}\n    roc_auc = {}\n    n_class = 5\n\n    for i in range(n_class):    \n        fpr[i], tpr[i], thresh[i] = roc_curve(actuals, [val[i] for val in probabilities], pos_label=i)\n        roc_auc[i] = auc(fpr[i], tpr[i])\n\n        \n    # plotting    \n    plt.plot(fpr[0], tpr[0], linestyle='--',color='orange', label=\"Class 0 vs Rest (area = %0.2f)\" % roc_auc[0])\n    plt.plot(fpr[1], tpr[1], linestyle='--',color='green', label=\"Class 1 vs Rest (area = %0.2f)\" % roc_auc[1])\n    plt.plot(fpr[2], tpr[2], linestyle='--',color='blue', label=\"Class 2 vs Rest (area = %0.2f)\" % roc_auc[2])\n    plt.plot(fpr[3], tpr[3], linestyle='--',color='red', label=\"Class 3 vs Rest (area = %0.2f)\" % roc_auc[3])\n    plt.plot(fpr[3], tpr[3], linestyle='--',color='purple', label=\"Class 4 vs Rest (area = %0.2f)\" % roc_auc[4])\n    plt.plot([0, 1], [0, 1], color=\"navy\", linestyle=\"--\")\n    plt.title('Multiclass ROC curve')\n    plt.xlabel('False Positive Rate')\n    plt.ylabel('True Positive rate')\n    plt.legend(loc='best')","metadata":{"execution":{"iopub.status.busy":"2021-12-13T00:29:00.482108Z","iopub.execute_input":"2021-12-13T00:29:00.482364Z","iopub.status.idle":"2021-12-13T00:29:00.505967Z","shell.execute_reply.started":"2021-12-13T00:29:00.482334Z","shell.execute_reply":"2021-12-13T00:29:00.505129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"act, prob = test_class_probabilities(model, DataLoader(data_set))","metadata":{"execution":{"iopub.status.busy":"2021-12-13T00:30:53.984011Z","iopub.execute_input":"2021-12-13T00:30:53.985053Z","iopub.status.idle":"2021-12-13T00:31:18.820293Z","shell.execute_reply.started":"2021-12-13T00:30:53.98498Z","shell.execute_reply":"2021-12-13T00:31:18.819551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_roc(act, prob)\nprint(act)","metadata":{"execution":{"iopub.status.busy":"2021-12-13T00:31:18.821978Z","iopub.execute_input":"2021-12-13T00:31:18.822257Z","iopub.status.idle":"2021-12-13T00:31:19.099245Z","shell.execute_reply.started":"2021-12-13T00:31:18.822222Z","shell.execute_reply":"2021-12-13T00:31:19.098462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}