{"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-31T09:06:26.461767Z","iopub.execute_input":"2023-10-31T09:06:26.462422Z","iopub.status.idle":"2023-10-31T09:06:26.850486Z","shell.execute_reply.started":"2023-10-31T09:06:26.462382Z","shell.execute_reply":"2023-10-31T09:06:26.849259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nfrom PIL import Image, ImageFile\nImageFile.LOAD_TRUNCATED_IMAGES = True\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:19:40.928676Z","iopub.execute_input":"2023-10-31T12:19:40.929810Z","iopub.status.idle":"2023-10-31T12:19:40.937558Z","shell.execute_reply.started":"2023-10-31T12:19:40.929765Z","shell.execute_reply":"2023-10-31T12:19:40.936058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm_notebook as tqdm\nfrom functools import partial\nimport scipy as sp\n\nimport random\nimport time\nimport sys\nimport os\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:19:49.638189Z","iopub.execute_input":"2023-10-31T12:19:49.638560Z","iopub.status.idle":"2023-10-31T12:19:49.646862Z","shell.execute_reply.started":"2023-10-31T12:19:49.638531Z","shell.execute_reply":"2023-10-31T12:19:49.645827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\nfrom sklearn.metrics import confusion_matrix\nimport torch\nimport torchvision\n\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\nfrom torchvision import transforms, models, datasets\nfrom torch.utils.data import Dataset\nfrom torch.autograd import Variable\n\n!pip install efficientnet_pytorch\nfrom efficientnet_pytorch import EfficientNet\n\nimport warnings\nwarnings.filterwarnings('ignore')\n!mkdir models","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:20:01.669132Z","iopub.execute_input":"2023-10-31T12:20:01.669541Z","iopub.status.idle":"2023-10-31T12:20:20.850876Z","shell.execute_reply.started":"2023-10-31T12:20:01.669512Z","shell.execute_reply":"2023-10-31T12:20:20.849302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_everything(seed = 23):\n    # tests\n    assert isinstance(seed, int), 'seed has to be an integer'\n\n    # randomness\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:20:25.829597Z","iopub.execute_input":"2023-10-31T12:20:25.831507Z","iopub.status.idle":"2023-10-31T12:20:25.838915Z","shell.execute_reply.started":"2023-10-31T12:20:25.831461Z","shell.execute_reply":"2023-10-31T12:20:25.837751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 256\n#IMAGE PREPROCESSING\n\ndef prepare_image(path,\n                  sigmaX         = 10,\n                  do_random_crop = False):\n\n    '''\n    Preprocess image\n    '''\n\n    # import image\n    image = cv2.imread(path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n    # perform smart crops\n    image = crop_black(image, tol = 7)\n    if do_random_crop == True:\n        image = random_crop(image, size = (0.9, 1))\n\n    # resize and color\n    image = cv2.resize(image, (int(image_size), int(image_size)))\n    image = cv2.addWeighted(image, 4, cv2.GaussianBlur(image, (0, 0), sigmaX), -4, 128)\n\n    # circular crop\n    image = circle_crop(image, sigmaX = sigmaX)\n\n    # convert to tensor\n    image = torch.tensor(image)\n    image = image.permute(2, 1, 0)\n    return image","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:20:46.270075Z","iopub.execute_input":"2023-10-31T12:20:46.270497Z","iopub.status.idle":"2023-10-31T12:20:46.278494Z","shell.execute_reply.started":"2023-10-31T12:20:46.270468Z","shell.execute_reply":"2023-10-31T12:20:46.277490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#CROP FUNCTIONS\n\ndef crop_black(img,\n               tol = 7):\n\n    '''\n    Perform automatic crop of black areas\n    '''\n\n    if img.ndim == 2:\n        mask = img > tol\n        return img[np.ix_(mask.any(1),mask.any(0))]\n\n    elif img.ndim == 3:\n        gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n        mask = gray_img > tol\n        check_shape = img[:,:,0][np.ix_(mask.any(1),mask.any(0))].shape[0]\n\n        if (check_shape == 0):\n            return img\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":"2023-10-31T12:26:27.076092Z","iopub.execute_input":"2023-10-31T12:26:27.076557Z","iopub.status.idle":"2023-10-31T12:26:27.088041Z","shell.execute_reply.started":"2023-10-31T12:26:27.076515Z","shell.execute_reply":"2023-10-31T12:26:27.086527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def circle_crop(img,\n                sigmaX = 10):\n\n    '''\n    Perform circular crop around image center\n    '''\n\n    height, width, depth = img.shape\n\n    largest_side = np.max((height, width))\n    img = cv2.resize(img, (largest_side, largest_side))\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\n    img = cv2.bitwise_and(img, img, mask = circle_img)\n    return img","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:21:11.630835Z","iopub.execute_input":"2023-10-31T12:21:11.631234Z","iopub.status.idle":"2023-10-31T12:21:11.639151Z","shell.execute_reply.started":"2023-10-31T12:21:11.631206Z","shell.execute_reply":"2023-10-31T12:21:11.638119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def random_crop(img,\n                size = (0.9, 1)):\n\n    '''\n    Random crop\n    '''\n\n    height, width, depth = img.shape\n\n    cut = 1 - random.uniform(size[0], size[1])\n\n    i = random.randint(0, int(cut * height))\n    j = random.randint(0, int(cut * width))\n    h = i + int((1 - cut) * height)\n    w = j + int((1 - cut) * width)\n\n    img = img[i:h, j:w, :]\n\n    return img","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:21:28.663282Z","iopub.execute_input":"2023-10-31T12:21:28.663677Z","iopub.status.idle":"2023-10-31T12:21:28.671305Z","shell.execute_reply.started":"2023-10-31T12:21:28.663647Z","shell.execute_reply":"2023-10-31T12:21:28.669781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class EyeData(Dataset):\n\n    # initialize\n    def __init__(self, data, directory, transform = None, do_random_crop = True, itype = '.png'):\n        self.data      = data\n        self.directory = directory\n        self.transform = transform\n        self.do_random_crop = do_random_crop\n        self.itype = itype\n    # length\n    def __len__(self):\n        return len(self.data)\n\n    # get items\n    def __getitem__(self, idx):\n        img_name = os.path.join(self.directory, self.data.loc[idx, 'id_code'] + self.itype)\n        image    = prepare_image(img_name, do_random_crop = self.do_random_crop)\n        image    = self.transform(image)\n        label    = torch.tensor(self.data.loc[idx, 'diagnosis'])\n        return {'image': image, 'label': label}","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:21:44.560129Z","iopub.execute_input":"2023-10-31T12:21:44.561042Z","iopub.status.idle":"2023-10-31T12:21:44.568632Z","shell.execute_reply.started":"2023-10-31T12:21:44.561006Z","shell.execute_reply":"2023-10-31T12:21:44.567280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Data(Dataset):\n\n    # initialize\n    def __init__(self, data, directory, transform = None, do_random_crop = True, itype = '.png'):\n        self.data      = data\n        self.directory = directory\n        self.transform = transform\n        self.do_random_crop = do_random_crop\n        self.itype = itype\n    # length\n    def __len__(self):\n        return len(self.data)\n\n    # get items\n    def __getitem__(self, idx):\n        img_name = os.path.join(self.directory, self.data.loc[idx, 'id_code'] + self.itype)\n        image = cv2.imread(img_name)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        image = crop_black(image, tol = 7)\n        image = cv2.resize(image, (int(image_size), int(image_size)))\n        image = circle_crop(image, sigmaX = 10)\n        image = torch.tensor(image)\n        image = image.permute(2, 1, 0)\n        image    = self.transform(image)\n        label    = torch.tensor(self.data.loc[idx, 'diagnosis'])\n        return {'image': image, 'label': label}","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:21:59.841497Z","iopub.execute_input":"2023-10-31T12:21:59.842144Z","iopub.status.idle":"2023-10-31T12:21:59.852562Z","shell.execute_reply.started":"2023-10-31T12:21:59.842110Z","shell.execute_reply":"2023-10-31T12:21:59.851417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 23\nseed_everything(seed)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:22:31.592677Z","iopub.execute_input":"2023-10-31T12:22:31.593312Z","iopub.status.idle":"2023-10-31T12:22:31.603783Z","shell.execute_reply.started":"2023-10-31T12:22:31.593281Z","shell.execute_reply":"2023-10-31T12:22:31.602927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import data\ntrain = pd.read_csv('../input/diabetic-retinopathy-resized/trainLabels.csv')\ntrain.columns = ['id_code', 'diagnosis']\ntest = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\n\n# check shape\nprint(train.shape, test.shape)\nprint('-' * 15)\nprint(train['diagnosis'].value_counts())\nprint('-' * 15)\nprint(test['diagnosis'].value_counts())","metadata":{"execution":{"iopub.status.busy":"2023-10-31T15:50:28.346612Z","iopub.execute_input":"2023-10-31T15:50:28.347039Z","iopub.status.idle":"2023-10-31T15:50:28.390900Z","shell.execute_reply.started":"2023-10-31T15:50:28.347009Z","shell.execute_reply":"2023-10-31T15:50:28.389879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize = (15, 5))\nplt.hist(train['diagnosis'])\nplt.title('Class Distribution')\nplt.ylabel('Number of examples')\nplt.xlabel('Diagnosis')","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:25:35.208426Z","iopub.execute_input":"2023-10-31T12:25:35.209507Z","iopub.status.idle":"2023-10-31T12:25:35.695818Z","shell.execute_reply.started":"2023-10-31T12:25:35.209464Z","shell.execute_reply":"2023-10-31T12:25:35.694763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transformations\nsample_trans = transforms.Compose([transforms.ToPILImage(),\n                                   transforms.ToTensor(),\n                                  ])\nsample = Data(data       = train.iloc[0:10],\n                      directory  = '../input/diabetic-retinopathy-resized/resized_train/resized_train',\n                      transform  = sample_trans,\n                      itype ='.jpeg')\n\n# data loader\nsample_loader = torch.utils.data.DataLoader(dataset     = sample,\n                                            batch_size  = 10,\n                                            shuffle     = False,\n                                            num_workers = 4)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:33:08.470737Z","iopub.execute_input":"2023-10-31T12:33:08.471174Z","iopub.status.idle":"2023-10-31T12:33:08.480642Z","shell.execute_reply.started":"2023-10-31T12:33:08.471128Z","shell.execute_reply":"2023-10-31T12:33:08.479226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display images\nfor batch_i, data in enumerate(sample_loader):\n\n    # extract data\n    inputs = data['image']\n    labels = data['label'].view(-1, 1)\n\n    # create plot\n    fig = plt.figure(figsize = (15, 7))\n    for i in range(len(labels)):\n        ax = fig.add_subplot(2, int(len(labels)/2), i + 1, xticks = [], yticks = [])\n        plt.imshow(inputs[i].numpy().transpose(1, 2, 0))\n        ax.set_title(labels.numpy()[i])\n\n    break","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:33:11.637508Z","iopub.execute_input":"2023-10-31T12:33:11.637912Z","iopub.status.idle":"2023-10-31T12:33:13.479060Z","shell.execute_reply.started":"2023-10-31T12:33:11.637880Z","shell.execute_reply":"2023-10-31T12:33:13.477796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\n\n# Placeholder for image stats\nimage_stats = []\n\n# Loop through the observations in the 'train' DataFrame\nfor index, observation in tqdm(train.iterrows(), total=len(train)):\n    # Import the image\n    img = cv2.imread('../input/diabetic-retinopathy-resized/resized_train/resized_train/{}.jpeg'.format(observation['id_code']))\n\n    # Compute image statistics\n    height, width, channels = img.shape\n    ratio = width / height\n\n    # Save the statistics\n    image_stats.append([observation['diagnosis'], height, width, channels, ratio])\n\n# Create a DataFrame from the image_stats list\nimage_stats_df = pd.DataFrame(image_stats, columns=['diagnosis', 'height', 'width', 'channels', 'ratio'])\n","metadata":{"execution":{"iopub.status.busy":"2023-10-31T13:50:12.245046Z","iopub.execute_input":"2023-10-31T13:50:12.245656Z","iopub.status.idle":"2023-10-31T13:56:27.831596Z","shell.execute_reply.started":"2023-10-31T13:50:12.245624Z","shell.execute_reply":"2023-10-31T13:56:27.830441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(image_stats_df)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T15:54:28.990675Z","iopub.execute_input":"2023-10-31T15:54:28.991894Z","iopub.status.idle":"2023-10-31T15:54:29.001823Z","shell.execute_reply.started":"2023-10-31T15:54:28.991850Z","shell.execute_reply":"2023-10-31T15:54:29.000594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# IMAGE SIZE DISTRIBUTION\n\nfig = plt.figure(figsize = (15, 5))\n\n# width\nplt.subplot(1, 3, 1)\nplt.hist(image_stats_df['width'])\nplt.title('(a) Image Width')\nplt.ylabel('Number of examples')\nplt.xlabel('Width')\n\n# height\nplt.subplot(1, 3, 2)\nplt.hist(image_stats_df['height'])\nplt.title('(b) Image Height')\nplt.ylabel('Number of examples')\nplt.xlabel('Height')\n\n# ratio\nplt.subplot(1, 3, 3)\nplt.hist(image_stats_df['ratio'])\nplt.title('(c) Aspect Ratio')\nplt.ylabel('Number of examples')\nplt.xlabel('Ratio')","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:53:37.319870Z","iopub.execute_input":"2023-10-31T12:53:37.320284Z","iopub.status.idle":"2023-10-31T12:53:38.289413Z","shell.execute_reply.started":"2023-10-31T12:53:37.320251Z","shell.execute_reply":"2023-10-31T12:53:38.288006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 16\nimage_size = 256\n\n# train transformations\ntrain_trans = transforms.Compose([transforms.ToPILImage(),\n                                  transforms.RandomRotation((-360, 360)),\n                                  transforms.RandomHorizontalFlip(),\n                                  transforms.RandomVerticalFlip(),\n                                  transforms.ToTensor()\n                                 ])\n\n# validation transformations\nvalid_trans = transforms.Compose([transforms.ToPILImage(),\n                                  transforms.ToTensor(),\n                                 ])\n\n# test transformations\ntest_trans = valid_trans","metadata":{"execution":{"iopub.status.busy":"2023-10-31T12:53:46.535444Z","iopub.execute_input":"2023-10-31T12:53:46.536444Z","iopub.status.idle":"2023-10-31T12:53:46.542507Z","shell.execute_reply.started":"2023-10-31T12:53:46.536405Z","shell.execute_reply":"2023-10-31T12:53:46.541746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#EXAMINE FIRST BATCH (TRAIN)\n\n# get dataset\nsample = EyeData(data       = train.iloc[0:10],\n                      directory  = '../input/diabetic-retinopathy-resized/resized_train/resized_train',\n                      transform  = train_trans,\n                      itype ='.jpeg')\n\n# data loader\nsample_loader = torch.utils.data.DataLoader(dataset     = sample,\n                                            batch_size  = batch_size,\n                                            shuffle     = True,\n                                            num_workers = 4)\n\n# display images\nfor batch_i, data in enumerate(sample_loader):\n\n    # extract data\n    inputs = data['image']\n    labels = data['label'].view(-1, 1)\n    print(image.shape)\n    # create plot\n    fig = plt.figure(figsize = (20,10))\n    for i in range(len(labels)):\n        ax = fig.add_subplot(2, int(len(labels)/2), i + 1, xticks = [], yticks = [])\n        plt.imshow(inputs[i].numpy().transpose(1, 2, 0))\n        ax.set_title(labels.numpy()[i])\n\n    break","metadata":{"execution":{"iopub.status.busy":"2023-10-31T15:59:08.496377Z","iopub.execute_input":"2023-10-31T15:59:08.496831Z","iopub.status.idle":"2023-10-31T15:59:10.558288Z","shell.execute_reply.started":"2023-10-31T15:59:08.496783Z","shell.execute_reply":"2023-10-31T15:59:10.557032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#EXAMINE FIRST BATCH (TEST)\n\n# get dataset\nsample = EyeData(data       = test.iloc[0:10],\n                      directory  = '../input/aptos2019-blindness-detection/train_images',\n                      transform  = test_trans,\n                      itype ='.png',\n                      do_random_crop = False)\n\n# data loader\nsample_loader = torch.utils.data.DataLoader(dataset     = sample,\n                                            batch_size  = batch_size,\n                                            shuffle     = False,\n                                            num_workers = 4)\n\n# display images\nfor batch_i, data in enumerate(sample_loader):\n\n    # extract data\n    inputs = data['image']\n    \n    # create plot\n    fig = plt.figure(figsize = (20,10))\n    for i in range(10):\n        ax = fig.add_subplot(2, int(10/2), i + 1, xticks = [], yticks = [])\n        plt.imshow(inputs[i].numpy().transpose(1, 2, 0))\n\n    break","metadata":{"execution":{"iopub.status.busy":"2023-10-31T15:58:16.776035Z","iopub.execute_input":"2023-10-31T15:58:16.776999Z","iopub.status.idle":"2023-10-31T15:58:21.429302Z","shell.execute_reply.started":"2023-10-31T15:58:16.776961Z","shell.execute_reply":"2023-10-31T15:58:21.428077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#DATA PREPARATION\n\n# load splits\ndata_train = train\ndata_valid = test\n\n# create datasets\ntrain_dataset = EyeData(data      = data_train,\n                             directory = '../input/diabetic-retinopathy-resized/resized_train/resized_train',\n                             transform = train_trans,\n                             itype ='.jpeg')\nvalid_dataset = EyeData(data       = data_valid,\n                            directory  = '../input/aptos2019-blindness-detection/train_images',\n                            transform  = valid_trans,\n                            itype ='.png')\n\n# create data loaders\ntrain_loader = torch.utils.data.DataLoader(train_dataset,\n                                           batch_size  = batch_size,\n                                           shuffle     = True,\n                                           num_workers = 4)\nvalid_loader = torch.utils.data.DataLoader(valid_dataset,\n                                           batch_size  = batch_size,\n                                           shuffle     = False,\n                                           num_workers = 4)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T13:17:51.834544Z","iopub.execute_input":"2023-10-31T13:17:51.834948Z","iopub.status.idle":"2023-10-31T13:17:51.841335Z","shell.execute_reply.started":"2023-10-31T13:17:51.834916Z","shell.execute_reply":"2023-10-31T13:17:51.840260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"######################resnet50","metadata":{"execution":{"iopub.status.busy":"2023-10-31T10:01:47.151279Z","iopub.execute_input":"2023-10-31T10:01:47.151646Z","iopub.status.idle":"2023-10-31T10:01:47.156812Z","shell.execute_reply.started":"2023-10-31T10:01:47.151619Z","shell.execute_reply":"2023-10-31T10:01:47.155663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport random\nimport warnings\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix, cohen_kappa_score\nfrom keras.models import Model\nfrom keras import optimizers, applications\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom keras.layers import Dense, Dropout, GlobalAveragePooling2D, Input\n\nfrom tensorflow import set_random_seed\ndef seed_everything(seed=0):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    set_random_seed(0)\nseed_everything()\n\n%matplotlib inline\nsns.set(style=\"whitegrid\")\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2023-10-31T13:14:02.481848Z","iopub.execute_input":"2023-10-31T13:14:02.482280Z","iopub.status.idle":"2023-10-31T13:14:02.533746Z","shell.execute_reply.started":"2023-10-31T13:14:02.482224Z","shell.execute_reply":"2023-10-31T13:14:02.532504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model parameters\nBATCH_SIZE = 10\nEPOCHS = 2\nWARMUP_EPOCHS = 2\nLEARNING_RATE = 1e-4\nWARMUP_LEARNING_RATE = 1e-3\nHEIGHT = 1959\nWIDTH = 1958\nCANAL = 3\nN_CLASSES = train['diagnosis'].nunique()\nES_PATIENCE = 5\nRLROP_PATIENCE = 3\nDECAY_DROP = 0.5","metadata":{"execution":{"iopub.status.busy":"2023-10-31T15:59:39.643143Z","iopub.execute_input":"2023-10-31T15:59:39.643550Z","iopub.status.idle":"2023-10-31T15:59:39.651030Z","shell.execute_reply.started":"2023-10-31T15:59:39.643514Z","shell.execute_reply":"2023-10-31T15:59:39.649887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model(input_shape, n_out):\n    input_tensor = Input(shape=input_shape)\n    base_model = applications.ResNet50(weights=None, \n                                       include_top=False,\n                                       input_tensor=input_tensor)\n    base_model.load_weights('../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')\n\n    x = GlobalAveragePooling2D()(base_model.output)\n    x = Dropout(0.5)(x)\n    x = Dense(2048, activation='relu')(x)\n    x = Dropout(0.5)(x)\n    final_output = Dense(n_out, activation='softmax', name='final_output')(x)\n    model = Model(input_tensor, final_output)\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-10-31T15:56:02.477304Z","iopub.execute_input":"2023-10-31T15:56:02.477681Z","iopub.status.idle":"2023-10-31T15:56:02.484583Z","shell.execute_reply.started":"2023-10-31T15:56:02.477650Z","shell.execute_reply":"2023-10-31T15:56:02.483536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model(input_shape=(HEIGHT, WIDTH, CANAL), n_out=N_CLASSES)\n\nfor layer in model.layers:\n    layer.trainable = False\n\nfor i in range(-5, 0):\n    model.layers[i].trainable = True\n\nmetric_list = [\"accuracy\"]\noptimizer = optimizers.Adam(lr=WARMUP_LEARNING_RATE)\nmodel.compile(optimizer=optimizer, loss=\"categorical_crossentropy\",  metrics=metric_list)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-31T16:03:29.867102Z","iopub.execute_input":"2023-10-31T16:03:29.867524Z","iopub.status.idle":"2023-10-31T16:03:33.566200Z","shell.execute_reply.started":"2023-10-31T16:03:29.867495Z","shell.execute_reply":"2023-10-31T16:03:33.564732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID = valid_generator.n//valid_generator.batch_size\n\nhistory_warmup = model.fit_generator(generator=train_generator,\n                                     steps_per_epoch=STEP_SIZE_TRAIN,\n                                     validation_data=valid_generator,\n                                     validation_steps=STEP_SIZE_VALID,\n                                     epochs=WARMUP_EPOCHS,\n                                     verbose=1).history","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in model.layers:\n    layer.trainable = True\n\nes = EarlyStopping(monitor='val_loss', mode='min', patience=ES_PATIENCE, restore_best_weights=True, verbose=1)\nrlrop = ReduceLROnPlateau(monitor='val_loss', mode='min', patience=RLROP_PATIENCE, factor=DECAY_DROP, min_lr=1e-6, verbose=1)\n\ncallback_list = [es, rlrop]\noptimizer = optimizers.Adam(lr=LEARNING_RATE)\nmodel.compile(optimizer=optimizer, loss=\"binary_crossentropy\",  metrics=metric_list)\nmodel.summary()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_finetunning = model.fit_generator(generator=train_generator,\n                                          steps_per_epoch=STEP_SIZE_TRAIN,\n                                          validation_data=valid_generator,\n                                          validation_steps=STEP_SIZE_VALID,\n                                          epochs=5,\n                                          callbacks=callback_list,\n                                          verbose=1).history","metadata":{},"execution_count":null,"outputs":[]}]}