{"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\n\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-06-26T12:19:06.787495Z","iopub.execute_input":"2023-06-26T12:19:06.787960Z","iopub.status.idle":"2023-06-26T12:19:06.794103Z","shell.execute_reply.started":"2023-06-26T12:19:06.787924Z","shell.execute_reply":"2023-06-26T12:19:06.793103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot as plt\nimport cv2\nimport gc\nimport tensorflow as tf\nfrom tqdm import tqdm\nfrom keras.models import Sequential\nfrom keras import layers\nfrom keras.optimizers import Adam\nfrom keras.applications import DenseNet121,DenseNet169,DenseNet201","metadata":{"execution":{"iopub.status.busy":"2023-06-26T12:19:10.844528Z","iopub.execute_input":"2023-06-26T12:19:10.844913Z","iopub.status.idle":"2023-06-26T12:19:18.946665Z","shell.execute_reply.started":"2023-06-26T12:19:10.844885Z","shell.execute_reply":"2023-06-26T12:19:18.945771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom matplotlib import pyplot as plt\nimport cv2 # for image pre-processing\nimport gc\nfrom tqdm import tqdm\n","metadata":{"execution":{"iopub.status.busy":"2023-06-26T12:19:18.948327Z","iopub.execute_input":"2023-06-26T12:19:18.949009Z","iopub.status.idle":"2023-06-26T12:19:18.954618Z","shell.execute_reply.started":"2023-06-26T12:19:18.948980Z","shell.execute_reply":"2023-06-26T12:19:18.953508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loading the dataset\ntrain_df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-06-26T12:19:18.956323Z","iopub.execute_input":"2023-06-26T12:19:18.956917Z","iopub.status.idle":"2023-06-26T12:19:18.980419Z","shell.execute_reply.started":"2023-06-26T12:19:18.956875Z","shell.execute_reply":"2023-06-26T12:19:18.979668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-26T12:19:18.982087Z","iopub.execute_input":"2023-06-26T12:19:18.982657Z","iopub.status.idle":"2023-06-26T12:19:19.012987Z","shell.execute_reply.started":"2023-06-26T12:19:18.982629Z","shell.execute_reply":"2023-06-26T12:19:19.012132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# distribution of classes in dataset ( which is imbalanced )\ntrain_df['diagnosis'].plot(kind='hist')","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:02:10.583075Z","iopub.execute_input":"2023-06-26T06:02:10.583444Z","iopub.status.idle":"2023-06-26T06:02:10.955743Z","shell.execute_reply.started":"2023-06-26T06:02:10.583419Z","shell.execute_reply":"2023-06-26T06:02:10.954704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx = train_df['id_code']\ny = train_df['diagnosis']\ntrain_x, valid_x, train_y, valid_y = train_test_split(x, y, test_size=0.15,\n                                                      stratify=y, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:06:39.839853Z","iopub.execute_input":"2023-06-26T06:06:39.840276Z","iopub.status.idle":"2023-06-26T06:06:40.322106Z","shell.execute_reply.started":"2023-06-26T06:06:39.840245Z","shell.execute_reply":"2023-06-26T06:06:40.320731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ndf_train = train_df\nSEED = 42\nIMG_SIZE = 512\nfig = plt.figure(figsize=(25, 16))\n# display 10 images from each class\nfor class_id in sorted(train_y.unique()):\n    for i, (idx, row) in enumerate(df_train.loc[df_train['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\n        plt.imshow(image)\n        ax.set_title('Label: %d-%d-%s' % (class_id, idx, row['id_code']) )","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:07:25.702954Z","iopub.execute_input":"2023-06-26T06:07:25.703368Z","iopub.status.idle":"2023-06-26T06:07:32.810419Z","shell.execute_reply.started":"2023-06-26T06:07:25.703335Z","shell.execute_reply":"2023-06-26T06:07:32.809181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CROPING IMAGE","metadata":{}},{"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)  # RGB to Grayscale\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    #         print(img1.shape,img2.shape,img3.shape)\n            img = np.stack([img1,img2,img3],axis=-1)\n    #         print(img.shape)\n        return img\n","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:09:22.892293Z","iopub.execute_input":"2023-06-26T06:09:22.893972Z","iopub.status.idle":"2023-06-26T06:09:22.902026Z","shell.execute_reply.started":"2023-06-26T06:09:22.893923Z","shell.execute_reply":"2023-06-26T06:09:22.900896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_image(image_path, desired_size=256):\n    im = cv2.imread(image_path)\n    im = cv2.cvtColor(im,cv2.COLOR_BGR2RGB)\n   # im = crop_image_from_gray(im)\n    im = cv2.resize(im,(desired_size,)*2)\n    res = cv2.addWeighted(im,4.5,cv2.GaussianBlur( im , (0,0) , 10) ,-4 ,100)\n    return res\n\ndef preprocess_image1(image_path, desired_size=256):\n    im = cv2.imread(image_path)\n    im = cv2.cvtColor(im,cv2.COLOR_BGR2RGB)\n    im = crop_image_from_gray(im)\n    im = cv2.resize(im,(desired_size,)*2)\n    res = cv2.addWeighted(im,4.5,cv2.GaussianBlur( im , (0,0) , 10) ,-4 ,100)\n    return res\n","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:09:38.701212Z","iopub.execute_input":"2023-06-26T06:09:38.702525Z","iopub.status.idle":"2023-06-26T06:09:38.710608Z","shell.execute_reply.started":"2023-06-26T06:09:38.702471Z","shell.execute_reply":"2023-06-26T06:09:38.709359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-06-26T12:19:26.469873Z","iopub.execute_input":"2023-06-26T12:19:26.470281Z","iopub.status.idle":"2023-06-26T12:19:26.484158Z","shell.execute_reply.started":"2023-06-26T12:19:26.470251Z","shell.execute_reply":"2023-06-26T12:19:26.482850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N1 = train_df.shape[0]\nx_train1 = np.empty((N1, 256, 256, 3), dtype=np.uint8)\n#tqdm\nfor i, image_id in tqdm(enumerate((train_df['id_code']))):\n    x_train1[i, :, :, :] = preprocess_image1(\n        f'/kaggle/input/aptos2019-blindness-detection/train_images/{image_id}.png'\n    )  ","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:09:53.606937Z","iopub.execute_input":"2023-06-26T06:09:53.607336Z","iopub.status.idle":"2023-06-26T06:22:41.876935Z","shell.execute_reply.started":"2023-06-26T06:09:53.607304Z","shell.execute_reply":"2023-06-26T06:22:41.875659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,8))\nfor i in range(9):\n    plt.subplot(3,3,i+1)\n    plt.imshow(x_train1[i])\n","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:22:41.879614Z","iopub.execute_input":"2023-06-26T06:22:41.880024Z","iopub.status.idle":"2023-06-26T06:22:43.504899Z","shell.execute_reply.started":"2023-06-26T06:22:41.879984Z","shell.execute_reply":"2023-06-26T06:22:43.503656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del N1\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:22:43.506368Z","iopub.execute_input":"2023-06-26T06:22:43.510233Z","iopub.status.idle":"2023-06-26T06:22:43.995208Z","shell.execute_reply.started":"2023-06-26T06:22:43.510192Z","shell.execute_reply":"2023-06-26T06:22:43.994030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = pd.get_dummies(train_df['diagnosis']).values","metadata":{"execution":{"iopub.status.busy":"2023-06-26T12:20:49.949217Z","iopub.execute_input":"2023-06-26T12:20:49.949694Z","iopub.status.idle":"2023-06-26T12:20:49.964994Z","shell.execute_reply.started":"2023-06-26T12:20:49.949660Z","shell.execute_reply":"2023-06-26T12:20:49.963647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2023-06-26T12:20:56.325894Z","iopub.execute_input":"2023-06-26T12:20:56.326282Z","iopub.status.idle":"2023-06-26T12:20:56.334988Z","shell.execute_reply.started":"2023-06-26T12:20:56.326256Z","shell.execute_reply":"2023-06-26T12:20:56.333552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_multi = np.empty(y_train.shape, dtype=y_train.dtype)\ny_train_multi[:, 4] = y_train[:, 4]\n\nfor i in range(3, -1, -1):\n    y_train_multi[:, i] = np.logical_or(y_train[:, i], y_train_multi[:, i+1])\n\nprint(\"Original y_train:\", y_train.sum(axis=0))\nprint(\"Multilabel version:\", y_train_multi.sum(axis=0))","metadata":{"execution":{"iopub.status.busy":"2023-06-26T12:29:45.234764Z","iopub.execute_input":"2023-06-26T12:29:45.235213Z","iopub.status.idle":"2023-06-26T12:29:45.244570Z","shell.execute_reply.started":"2023-06-26T12:29:45.235160Z","shell.execute_reply":"2023-06-26T12:29:45.243433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_multi","metadata":{"execution":{"iopub.status.busy":"2023-06-26T12:29:54.192285Z","iopub.execute_input":"2023-06-26T12:29:54.192717Z","iopub.status.idle":"2023-06-26T12:29:54.199326Z","shell.execute_reply.started":"2023-06-26T12:29:54.192684Z","shell.execute_reply":"2023-06-26T12:29:54.198566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save('y_train1.npy',y_train_multi)\nnp.save('x_train1.npy',x_train1)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:23:47.158287Z","iopub.execute_input":"2023-06-26T06:23:47.158684Z","iopub.status.idle":"2023-06-26T06:23:48.196137Z","shell.execute_reply.started":"2023-06-26T06:23:47.158656Z","shell.execute_reply":"2023-06-26T06:23:48.195094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# TRAINING","metadata":{}},{"cell_type":"code","source":"x_train1 = x_train1\ny_train1 = y_train_multi","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}