{"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 Notebook Uses ReSNet50 (Transfer Learning with PreTrained Weights and Retraining) Model to generate Output Predictions","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  <font color='red'>Table of Contents</font>\n","metadata":{}},{"cell_type":"markdown","source":"[9. ResNet50 Models](#section9)<br>\n","metadata":{"id":"I3BHeMMtWiIE"}},{"cell_type":"code","source":"import multiprocessing\nfrom multiprocessing.pool import ThreadPool\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nimport pandas as pd\nimport numpy as np\nfrom tqdm import tqdm,tqdm_notebook\nfrom prettytable import PrettyTable\nimport pickle\nimport os\nprint('CWD is ',os.getcwd())\n\n# Vis Libs..\nfrom sklearn.manifold import TSNE\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\nplt.rcParams[\"axes.grid\"] = False\n\n# Image Libs.\nfrom PIL import Image\nimport cv2\n\n# sklearn libs..\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2021-06-24T05:59:15.120081Z","iopub.execute_input":"2021-06-24T05:59:15.12046Z","iopub.status.idle":"2021-06-24T05:59:16.307416Z","shell.execute_reply.started":"2021-06-24T05:59:15.120372Z","shell.execute_reply":"2021-06-24T05:59:16.30639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Research Kernel Link - https://github.com/dimitreOliveira/APTOS2019BlindnessDetection/blob/master/Model%20backlog/ResNet50/4%20-%20ResNet50%20-%20Batch%20size%2022.ipynb\n\nimport pandas as pd\nimport numpy as np\nimport os\nfrom prettytable import PrettyTable\nimport pickle\nimport multiprocessing\nfrom multiprocessing.pool import ThreadPool\nprint(multiprocessing.cpu_count(),\" CPU cores\")\n\nimport seaborn as sns\n%matplotlib inline\nimport matplotlib.pyplot as plt\nplt.rcParams[\"axes.grid\"] = False\n\nfrom sklearn.metrics import confusion_matrix, cohen_kappa_score,accuracy_score\n\nfrom PIL import Image\nimport cv2\n\nimport keras\nfrom keras import applications\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import optimizers,Model,Sequential\nfrom keras.layers import Input,GlobalAveragePooling2D,Dropout,Dense,Activation\nfrom keras.callbacks import EarlyStopping,ReduceLROnPlateau\n\n# Colab Libs...\n#","metadata":{"id":"22ccXpX_WQ54","outputId":"223b61e2-9b29-4677-8683-c8ad021ba2f0","execution":{"iopub.status.busy":"2021-06-24T05:59:17.750703Z","iopub.execute_input":"2021-06-24T05:59:17.751056Z","iopub.status.idle":"2021-06-24T05:59:22.338574Z","shell.execute_reply.started":"2021-06-24T05:59:17.751025Z","shell.execute_reply":"2021-06-24T05:59:22.337699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport os","metadata":{"execution":{"iopub.status.busy":"2021-06-24T05:59:22.339958Z","iopub.execute_input":"2021-06-24T05:59:22.340301Z","iopub.status.idle":"2021-06-24T05:59:22.347466Z","shell.execute_reply.started":"2021-06-24T05:59:22.340266Z","shell.execute_reply":"2021-06-24T05:59:22.346704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  <a id = 'section9'> <font color='red'>  9. ResNet50 Models  </font> </a>","metadata":{"id":"yRu-rH5tWQ58"}},{"cell_type":"markdown","source":"### <font color='red'> 9.1 Setup Colab Environment </font>","metadata":{"id":"Ej-CCMUPW__D"}},{"cell_type":"code","source":"df = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T05:59:24.977919Z","iopub.execute_input":"2021-06-24T05:59:24.978255Z","iopub.status.idle":"2021-06-24T05:59:24.99478Z","shell.execute_reply.started":"2021-06-24T05:59:24.978224Z","shell.execute_reply":"2021-06-24T05:59:24.993973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-24T05:59:30.784613Z","iopub.execute_input":"2021-06-24T05:59:30.784966Z","iopub.status.idle":"2021-06-24T05:59:30.804913Z","shell.execute_reply.started":"2021-06-24T05:59:30.784935Z","shell.execute_reply":"2021-06-24T05:59:30.803884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['id_code']+='.png'","metadata":{"execution":{"iopub.status.busy":"2021-06-24T05:59:32.269725Z","iopub.execute_input":"2021-06-24T05:59:32.270076Z","iopub.status.idle":"2021-06-24T05:59:32.292401Z","shell.execute_reply.started":"2021-06-24T05:59:32.270044Z","shell.execute_reply":"2021-06-24T05:59:32.291674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['diagnosis']= df['diagnosis'].map(str)","metadata":{"execution":{"iopub.status.busy":"2021-06-24T05:59:33.713245Z","iopub.execute_input":"2021-06-24T05:59:33.71638Z","iopub.status.idle":"2021-06-24T05:59:33.726477Z","shell.execute_reply.started":"2021-06-24T05:59:33.71442Z","shell.execute_reply":"2021-06-24T05:59:33.724753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['diagnosis'].dtype","metadata":{"execution":{"iopub.status.busy":"2021-06-24T05:59:36.021516Z","iopub.execute_input":"2021-06-24T05:59:36.021845Z","iopub.status.idle":"2021-06-24T05:59:36.028649Z","shell.execute_reply.started":"2021-06-24T05:59:36.021813Z","shell.execute_reply":"2021-06-24T05:59:36.027825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_train,df_train_test = df.iloc[:3000], df.iloc[3000:]\nprint(df_train_train.shape,df_train_test.shape,'\\n')\ndf_train_test.head(6)\n","metadata":{"id":"z1F6Uye-WQ6D","outputId":"ed9b014f-f77d-4ceb-a114-7f4e7b8e20f0","execution":{"iopub.status.busy":"2021-06-24T05:59:39.599619Z","iopub.execute_input":"2021-06-24T05:59:39.599966Z","iopub.status.idle":"2021-06-24T05:59:39.614388Z","shell.execute_reply.started":"2021-06-24T05:59:39.599936Z","shell.execute_reply":"2021-06-24T05:59:39.613253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-25T02:18:57.620845Z","iopub.execute_input":"2021-05-25T02:18:57.621158Z","iopub.status.idle":"2021-05-25T02:18:57.627073Z","shell.execute_reply.started":"2021-05-25T02:18:57.621129Z","shell.execute_reply":"2021-05-25T02:18:57.626236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-25T02:18:51.900707Z","iopub.execute_input":"2021-05-25T02:18:51.901146Z","iopub.status.idle":"2021-05-25T02:18:51.905443Z","shell.execute_reply.started":"2021-05-25T02:18:51.901102Z","shell.execute_reply":"2021-05-25T02:18:51.904687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-25T05:51:57.088239Z","iopub.execute_input":"2021-05-25T05:51:57.088606Z","iopub.status.idle":"2021-05-25T05:51:57.095988Z","shell.execute_reply.started":"2021-05-25T05:51:57.088573Z","shell.execute_reply":"2021-05-25T05:51:57.094903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-05-25T05:52:22.90687Z","iopub.execute_input":"2021-05-25T05:52:22.907182Z","iopub.status.idle":"2021-05-25T05:52:22.911343Z","shell.execute_reply.started":"2021-05-25T05:52:22.907152Z","shell.execute_reply":"2021-05-25T05:52:22.910117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-25T05:54:00.540303Z","iopub.execute_input":"2021-05-25T05:54:00.540655Z","iopub.status.idle":"2021-05-25T05:54:00.72851Z","shell.execute_reply.started":"2021-05-25T05:54:00.540624Z","shell.execute_reply":"2021-05-25T05:54:00.727555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-24T04:45:48.450096Z","iopub.execute_input":"2021-05-24T04:45:48.450478Z","iopub.status.idle":"2021-05-24T04:45:48.456892Z","shell.execute_reply.started":"2021-05-24T04:45:48.450443Z","shell.execute_reply":"2021-05-24T04:45:48.456007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-24T04:45:50.300707Z","iopub.execute_input":"2021-05-24T04:45:50.301053Z","iopub.status.idle":"2021-05-24T04:45:50.305814Z","shell.execute_reply.started":"2021-05-24T04:45:50.301022Z","shell.execute_reply":"2021-05-24T04:45:50.30481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-24T11:40:09.508232Z","iopub.execute_input":"2021-05-24T11:40:09.508573Z","iopub.status.idle":"2021-05-24T11:40:09.515192Z","shell.execute_reply.started":"2021-05-24T11:40:09.508537Z","shell.execute_reply":"2021-05-24T11:40:09.514346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### <font color='red'> 9.2 Image Pre Processing </font>","metadata":{"id":"3H9xIXaaMmza"}},{"cell_type":"code","source":"IMG_SIZE = 512","metadata":{"id":"SAct4MuUNjH4","execution":{"iopub.status.busy":"2021-06-24T05:07:00.627872Z","iopub.execute_input":"2021-06-24T05:07:00.62824Z","iopub.status.idle":"2021-06-24T05:07:00.632115Z","shell.execute_reply.started":"2021-06-24T05:07:00.628206Z","shell.execute_reply":"2021-06-24T05:07:00.631177Z"},"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    #         print(img1.shape,img2.shape,img3.shape)\n            img = np.stack([img1,img2,img3],axis=-1)\n    #         print(img.shape)\n        return img\n\ndef circle_crop(img, sigmaX = 30):   \n    \"\"\"\n    Create circular crop around image centre    \n    \"\"\"    \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    cv2.resize(img, (320, 320))\n    return img \n\ndef preprocess_image(file):\n    input_filepath ='../input/aptos2019-blindness-detection/train_images/'+file\n    output_filepath = './pre_resized_data/'+file\n    \n    img = cv2.imread(input_filepath)\n    img = circle_crop(img) \n    \n    #print(img.shape)\n    #plt.imshow(img)\n    cv2.imwrite(output_filepath, cv2.resize(img, (IMG_SIZE,IMG_SIZE)))","metadata":{"id":"L6lBJR9tMo68","execution":{"iopub.status.busy":"2021-05-25T02:19:13.913096Z","iopub.execute_input":"2021-05-25T02:19:13.913551Z","iopub.status.idle":"2021-05-25T02:19:13.926892Z","shell.execute_reply.started":"2021-05-25T02:19:13.91351Z","shell.execute_reply":"2021-05-25T02:19:13.925796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#preprocess_image('000c1434d8d7.png')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T02:19:17.07823Z","iopub.execute_input":"2021-05-25T02:19:17.078598Z","iopub.status.idle":"2021-05-25T02:19:17.082188Z","shell.execute_reply.started":"2021-05-25T02:19:17.078568Z","shell.execute_reply":"2021-05-25T02:19:17.081125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''This Function uses Multi processing for faster saving of images into folder'''\n\ndef multiprocess_image_processor(process:int, imgs:list):\n    \"\"\"\n    Inputs:\n        process: (int) number of process to run\n        imgs:(list) list of images\n    \"\"\"\n    print(f'MESSAGE: Running {process} process')\n    results = ThreadPool(process).map(preprocess_image, imgs)\n    return results","metadata":{"id":"-VIgIRbCNBix","execution":{"iopub.status.busy":"2021-05-25T02:19:18.610388Z","iopub.execute_input":"2021-05-25T02:19:18.610701Z","iopub.status.idle":"2021-05-25T02:19:18.615553Z","shell.execute_reply.started":"2021-05-25T02:19:18.610674Z","shell.execute_reply":"2021-05-25T02:19:18.614411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"multiprocess_image_processor(6, df['id_code'].values) ","metadata":{"execution":{"iopub.status.busy":"2021-05-25T02:19:21.836268Z","iopub.execute_input":"2021-05-25T02:19:21.836631Z","iopub.status.idle":"2021-05-25T03:02:11.709388Z","shell.execute_reply.started":"2021-05-25T02:19:21.836601Z","shell.execute_reply":"2021-05-25T03:02:11.708478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('./pre_resized_data'))","metadata":{"execution":{"iopub.status.busy":"2021-05-25T03:04:13.339111Z","iopub.execute_input":"2021-05-25T03:04:13.339491Z","iopub.status.idle":"2021-05-25T03:04:13.348143Z","shell.execute_reply.started":"2021-05-25T03:04:13.33946Z","shell.execute_reply":"2021-05-25T03:04:13.347063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use 2 cores (colab)\n# ref - https://stackoverflow.com/questions/1006289/how-to-find-out-the-number-of-cpus-using-python\n\n#multiprocess_image_processor(2, list(df_train_train.id_code.values))","metadata":{"id":"Cf_-VV_DNC0N","execution":{"iopub.status.busy":"2021-05-25T03:07:55.236405Z","iopub.execute_input":"2021-05-25T03:07:55.236723Z","iopub.status.idle":"2021-05-25T03:07:55.24049Z","shell.execute_reply.started":"2021-05-25T03:07:55.236694Z","shell.execute_reply":"2021-05-25T03:07:55.239168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### <font color='red'> 9.3 Train Model </font>","metadata":{"id":"Oq5liZxiWQ6H"}},{"cell_type":"code","source":"# Model parameters\nBATCH_SIZE = 8\nEPOCHS = 40\nWARMUP_EPOCHS = 2\nLEARNING_RATE = 1e-4\nWARMUP_LEARNING_RATE = 1e-3\nHEIGHT = 512\nWIDTH = 512\nCANAL = 3\nN_CLASSES = df_train_train['diagnosis'].nunique()\nES_PATIENCE = 5\nRLROP_PATIENCE = 3\nDECAY_DROP = 0.5","metadata":{"id":"KBX1kOjkWQ6H","execution":{"iopub.status.busy":"2021-06-24T05:59:53.54682Z","iopub.execute_input":"2021-06-24T05:59:53.547139Z","iopub.status.idle":"2021-06-24T05:59:53.555046Z","shell.execute_reply.started":"2021-06-24T05:59:53.54711Z","shell.execute_reply":"2021-06-24T05:59:53.553877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def img_generator(train,test):\n    train_datagen=ImageDataGenerator(rescale=1./255, validation_split=0.2,horizontal_flip=True)\n    \n    train_generator=train_datagen.flow_from_dataframe(dataframe=df_train_train,\n                                                      directory=\"../input/project1/1\",\n                                                      x_col=\"id_code\",\n                                                      y_col=\"diagnosis\",\n                                                      batch_size=BATCH_SIZE,\n                                                      class_mode=\"categorical\",\n                                                      target_size=(HEIGHT, WIDTH),\n                                                      subset='training')\n    \n    valid_generator=train_datagen.flow_from_dataframe(dataframe=df_train_train,\n                                                      directory=\"../input/project1/1\",\n                                                      x_col=\"id_code\",\n                                                      y_col=\"diagnosis\",\n                                                      batch_size=BATCH_SIZE,\n                                                      class_mode=\"categorical\",    \n                                                      target_size=(HEIGHT, WIDTH),\n                                                      subset='validation')\n    \n    test_datagen = ImageDataGenerator(rescale=1./255)\n    test_generator = test_datagen.flow_from_dataframe(dataframe=df_train_test,\n                                                      directory = \"../input/project1/1\",\n                                                      x_col=\"id_code\",\n                                                      target_size=(HEIGHT, WIDTH),\n                                                      batch_size=1,\n                                                      shuffle=False,\n                                                      class_mode=None)\n    \n    return train_generator,valid_generator,test_generator","metadata":{"id":"brHRA_U0WQ6K","execution":{"iopub.status.busy":"2021-06-24T05:59:56.564025Z","iopub.execute_input":"2021-06-24T05:59:56.564336Z","iopub.status.idle":"2021-06-24T05:59:56.571838Z","shell.execute_reply.started":"2021-06-24T05:59:56.564307Z","shell.execute_reply":"2021-06-24T05:59:56.570718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gen=ImageDataGenerator(rescale=1./255, validation_split=0.2,horizontal_flip=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-24T05:59:58.779566Z","iopub.execute_input":"2021-06-24T05:59:58.779922Z","iopub.status.idle":"2021-06-24T05:59:58.785484Z","shell.execute_reply.started":"2021-06-24T05:59:58.779889Z","shell.execute_reply":"2021-06-24T05:59:58.784443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator,valid_generator,test_generator = img_generator(df_train_train,df_train_test)","metadata":{"id":"CQjnOOEhWQ6P","outputId":"703a18c6-c2d3-4d5a-c59e-26bc0d684180","execution":{"iopub.status.busy":"2021-06-24T06:00:00.58564Z","iopub.execute_input":"2021-06-24T06:00:00.586036Z","iopub.status.idle":"2021-06-24T06:00:06.853279Z","shell.execute_reply.started":"2021-06-24T06:00:00.586002Z","shell.execute_reply":"2021-06-24T06:00:06.852453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-25T03:08:17.338877Z","iopub.execute_input":"2021-05-25T03:08:17.339213Z","iopub.status.idle":"2021-05-25T03:08:17.831077Z","shell.execute_reply.started":"2021-05-25T03:08:17.339167Z","shell.execute_reply":"2021-05-25T03:08:17.829975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-25T03:08:20.386112Z","iopub.execute_input":"2021-05-25T03:08:20.386437Z","iopub.status.idle":"2021-05-25T03:08:22.294781Z","shell.execute_reply.started":"2021-05-25T03:08:20.386407Z","shell.execute_reply":"2021-05-25T03:08:22.2939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-23T18:13:53.522792Z","iopub.execute_input":"2021-05-23T18:13:53.523176Z","iopub.status.idle":"2021-05-23T18:13:53.537409Z","shell.execute_reply.started":"2021-05-23T18:13:53.523143Z","shell.execute_reply":"2021-05-23T18:13:53.536468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-25T03:09:09.609759Z","iopub.execute_input":"2021-05-25T03:09:09.610081Z","iopub.status.idle":"2021-05-25T03:09:10.923277Z","shell.execute_reply.started":"2021-05-25T03:09:09.610051Z","shell.execute_reply":"2021-05-25T03:09:10.92244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-25T03:08:59.792905Z","iopub.execute_input":"2021-05-25T03:08:59.793205Z","iopub.status.idle":"2021-05-25T03:08:59.800268Z","shell.execute_reply.started":"2021-05-25T03:08:59.793176Z","shell.execute_reply":"2021-05-25T03:08:59.799401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-25T03:09:03.00802Z","iopub.execute_input":"2021-05-25T03:09:03.008355Z","iopub.status.idle":"2021-05-25T03:09:03.022072Z","shell.execute_reply.started":"2021-05-25T03:09:03.008322Z","shell.execute_reply":"2021-05-25T03:09:03.021137Z"},"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='imagenet', include_top=False,input_tensor=input_tensor)\n    #base_model.load_weights('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    return model","metadata":{"id":"p1ktmE4ZWQ6T","execution":{"iopub.status.busy":"2021-06-24T06:00:14.374338Z","iopub.execute_input":"2021-06-24T06:00:14.374657Z","iopub.status.idle":"2021-06-24T06:00:14.383211Z","shell.execute_reply.started":"2021-06-24T06:00:14.374627Z","shell.execute_reply":"2021-06-24T06:00:14.382416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"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\nmodel.summary()","metadata":{"id":"GIHj-I3LWQ6W","outputId":"438f8279-a839-406e-92a0-b28a6c13f7c6","execution":{"iopub.status.busy":"2021-06-24T06:00:17.675833Z","iopub.execute_input":"2021-06-24T06:00:17.676156Z","iopub.status.idle":"2021-06-24T06:00:21.819977Z","shell.execute_reply.started":"2021-06-24T06:00:17.676128Z","shell.execute_reply":"2021-06-24T06:00:21.81912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-24T06:00:36.457507Z","iopub.execute_input":"2021-06-24T06:00:36.457904Z","iopub.status.idle":"2021-06-24T06:00:36.545163Z","shell.execute_reply.started":"2021-06-24T06:00:36.457872Z","shell.execute_reply":"2021-06-24T06:00:36.544327Z"},"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\nprint(STEP_SIZE_TRAIN,STEP_SIZE_VALID)","metadata":{"id":"FdOZMTWQWQ6Y","outputId":"669c9c51-fa6a-463b-d9e8-770aea573a0b","execution":{"iopub.status.busy":"2021-06-24T06:00:47.986474Z","iopub.execute_input":"2021-06-24T06:00:47.986825Z","iopub.status.idle":"2021-06-24T06:00:47.993257Z","shell.execute_reply.started":"2021-06-24T06:00:47.986792Z","shell.execute_reply":"2021-06-24T06:00:47.992176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = optimizers.Adam(lr=WARMUP_LEARNING_RATE),loss = 'categorical_crossentropy',metrics = ['accuracy'])\n\nhistory_warmup = model.fit(train_generator,\n                                     steps_per_epoch=STEP_SIZE_TRAIN,\n                                     validation_data=valid_generator,validation_steps=STEP_SIZE_VALID,\n                                     epochs=WARMUP_EPOCHS,\n                                     verbose=1).history","metadata":{"id":"UR-WhqwSWQ6a","outputId":"3644a7a6-41c8-4449-9674-30613a0d9b1e","execution":{"iopub.status.busy":"2021-06-24T06:00:50.556642Z","iopub.execute_input":"2021-06-24T06:00:50.557022Z","iopub.status.idle":"2021-06-24T06:03:07.166552Z","shell.execute_reply.started":"2021-06-24T06:00:50.556989Z","shell.execute_reply":"2021-06-24T06:03:07.165671Z"},"trusted":true},"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#ckpt = tf.keras.callbacks.ModelCheckpoint('res_train.h5')\ncallback_list = [es, rlrop]\noptimizer = optimizers.Adam(lr=LEARNING_RATE)\nmodel.compile(optimizer=optimizer, loss=\"binary_crossentropy\",  metrics=['accuracy'])\n#model.summary()","metadata":{"id":"I0EwusGsWQ6c","outputId":"265ae8eb-738f-48d1-93a8-9db67513a68d","execution":{"iopub.status.busy":"2021-06-24T06:03:17.037823Z","iopub.execute_input":"2021-06-24T06:03:17.038202Z","iopub.status.idle":"2021-06-24T06:03:17.064824Z","shell.execute_reply.started":"2021-06-24T06:03:17.038162Z","shell.execute_reply":"2021-06-24T06:03:17.063909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_finetunning = model.fit(train_generator,\n                                steps_per_epoch=STEP_SIZE_TRAIN,\n                                validation_data=valid_generator,\n                                validation_steps=STEP_SIZE_VALID,\n                                epochs=EPOCHS,\n                                callbacks=callback_list,\n                                verbose=1).history","metadata":{"id":"caeqUZYJWQ6k","outputId":"8c65ebfb-4637-4fed-d4dc-c76b495910d3","execution":{"iopub.status.busy":"2021-06-24T06:03:18.723697Z","iopub.execute_input":"2021-06-24T06:03:18.724097Z","iopub.status.idle":"2021-06-24T06:19:30.2563Z","shell.execute_reply.started":"2021-06-24T06:03:18.724062Z","shell.execute_reply":"2021-06-24T06:19:30.255449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LEARNING_RATE =  4.19999873689376e-05\noptimizer = optimizers.Adam(lr=LEARNING_RATE)\nmodel.compile(optimizer=optimizer, loss=\"binary_crossentropy\",  metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-05-25T04:46:06.925097Z","iopub.execute_input":"2021-05-25T04:46:06.925483Z","iopub.status.idle":"2021-05-25T04:46:06.959024Z","shell.execute_reply.started":"2021-05-25T04:46:06.925448Z","shell.execute_reply":"2021-05-25T04:46:06.958217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_finetunning = model.fit(train_generator,\n                                steps_per_epoch=STEP_SIZE_TRAIN,\n                                validation_data=valid_generator,\n                                validation_steps=STEP_SIZE_VALID,\n                                epochs=EPOCHS,\n                                callbacks=callback_list,\n                                verbose=1).history","metadata":{"execution":{"iopub.status.busy":"2021-05-25T04:46:09.709824Z","iopub.execute_input":"2021-05-25T04:46:09.710142Z","iopub.status.idle":"2021-05-25T05:19:01.562421Z","shell.execute_reply.started":"2021-05-25T04:46:09.71011Z","shell.execute_reply":"2021-05-25T05:19:01.561582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.models.save_model(model, filepath = './resnet3.h5')","metadata":{"execution":{"iopub.status.busy":"2021-05-24T07:19:27.464102Z","iopub.execute_input":"2021-05-24T07:19:27.464487Z","iopub.status.idle":"2021-05-24T07:19:28.636437Z","shell.execute_reply.started":"2021-05-24T07:19:27.464451Z","shell.execute_reply":"2021-05-24T07:19:28.635271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ref - https://stackoverflow.com/questions/29188757/matplotlib-specify-format-of-floats-for-tick-lables\nplt.figure(figsize=(8,5))\n\nplt.plot(history_finetunning['accuracy'])\nplt.plot(history_finetunning['val_accuracy'])\nplt.title('Model Accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.gca().ticklabel_format(axis='both', style='plain', useOffset=False)\nplt.show()","metadata":{"id":"UcLxU0ZhWQ6m","outputId":"3b67a6d9-2cc8-4753-8536-ef93939ed684","execution":{"iopub.status.busy":"2021-06-24T06:22:18.111108Z","iopub.execute_input":"2021-06-24T06:22:18.111473Z","iopub.status.idle":"2021-06-24T06:22:18.264832Z","shell.execute_reply.started":"2021-06-24T06:22:18.111435Z","shell.execute_reply":"2021-06-24T06:22:18.263867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### <font color='red'> 9.4 Generate Train Predictions on complete Train Data </font>","metadata":{"id":"Cu1geHQHrINJ"}},{"cell_type":"code","source":"complete_datagen = ImageDataGenerator(rescale=1./255)\ncomplete_generator = complete_datagen.flow_from_dataframe(dataframe=df_train_train,\n                                                          directory = \"../input/aptos2019-blindness-detection/train_images\",\n                                                          x_col=\"id_code\",\n                                                          target_size=(HEIGHT, WIDTH),\n                                                          batch_size=1,\n                                                          shuffle=False,\n                                                          class_mode=None)\n\nSTEP_SIZE_COMPLETE = complete_generator.n//complete_generator.batch_size\ntrain_preds = model.predict_generator(complete_generator, steps=STEP_SIZE_COMPLETE,verbose = 1)\ntrain_preds = [np.argmax(pred) for pred in train_preds]","metadata":{"id":"i4isFqHfrIcl","outputId":"d135af2f-266d-41b0-c438-c9e68290f02d","execution":{"iopub.status.busy":"2021-06-24T06:22:24.539723Z","iopub.execute_input":"2021-06-24T06:22:24.540103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train Cohen Kappa score: %.3f\" % cohen_kappa_score(train_preds, df_train_train['diagnosis'].astype('int'), weights='quadratic'))\nprint(\"Train Accuracy score : %.3f\" % accuracy_score(df_train_train['diagnosis'].astype('int'),train_preds))","metadata":{"id":"y5coP6NwsHdS","outputId":"8d7348f1-698d-4352-ba68-bba45fca9663","execution":{"iopub.status.busy":"2021-05-25T05:19:22.880787Z","iopub.execute_input":"2021-05-25T05:19:22.881116Z","iopub.status.idle":"2021-05-25T05:19:22.898055Z","shell.execute_reply.started":"2021-05-25T05:19:22.881084Z","shell.execute_reply":"2021-05-25T05:19:22.897021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### <font color='red'> 9.5 Evaluate Model on Test Data </font>","metadata":{"id":"ESdNiiRPWQ6o"}},{"cell_type":"code","source":"test_generator.reset()\nSTEP_SIZE_TEST = test_generator.n//test_generator.batch_size\ntest_preds = model.predict_generator(test_generator, steps=STEP_SIZE_TEST,verbose = 1)\ntest_labels = [np.argmax(pred) for pred in test_preds]","metadata":{"id":"vBjYmJDxnguQ","outputId":"534699c0-d5a3-40b6-8039-1b5d0ff81b66","execution":{"iopub.status.busy":"2021-05-25T04:43:52.195442Z","iopub.execute_input":"2021-05-25T04:43:52.195782Z","iopub.status.idle":"2021-05-25T04:45:17.263004Z","shell.execute_reply.started":"2021-05-25T04:43:52.195753Z","shell.execute_reply":"2021-05-25T04:45:17.262214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_conf_matrix(true,pred,classes):\n    cf = confusion_matrix(true, pred)\n    \n    df_cm = pd.DataFrame(cf, range(len(classes)), range(len(classes)))\n    plt.figure(figsize=(8,5.5))\n    sns.set(font_scale=1.4)\n    sns.heatmap(df_cm, annot=True, annot_kws={\"size\": 16},xticklabels = classes ,yticklabels = classes,fmt='g')\n    #sns.heatmap(df_cm, annot=True, annot_kws={\"size\": 16})\n    plt.show()","metadata":{"id":"qk-D7ZIvoEQ9","execution":{"iopub.status.busy":"2021-05-25T05:20:02.641455Z","iopub.execute_input":"2021-05-25T05:20:02.642122Z","iopub.status.idle":"2021-05-25T05:20:02.656269Z","shell.execute_reply.started":"2021-05-25T05:20:02.642074Z","shell.execute_reply":"2021-05-25T05:20:02.655142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['0 - No DR', '1 - Mild', '2 - Moderate', '3 - Severe', '4 - Proliferative DR']\nplot_conf_matrix(list(df_train_test['diagnosis'].astype(int)),test_labels,labels)","metadata":{"id":"8sxcjGJ1oR7f","outputId":"029b6c98-b913-4075-f4a1-13122acaf5ae","execution":{"iopub.status.busy":"2021-05-25T05:20:04.84986Z","iopub.execute_input":"2021-05-25T05:20:04.850173Z","iopub.status.idle":"2021-05-25T05:20:05.122028Z","shell.execute_reply.started":"2021-05-25T05:20:04.850143Z","shell.execute_reply":"2021-05-25T05:20:05.121057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnf_matrix = confusion_matrix(df_train_test['diagnosis'].astype('int'), test_labels)\ncnf_matrix_norm = cnf_matrix.astype('float') / cnf_matrix.sum(axis=1)[:, np.newaxis]\ndf_cm = pd.DataFrame(cnf_matrix_norm, index=labels, columns=labels)\nplt.figure(figsize=(16, 7))\nsns.heatmap(df_cm, annot=True, fmt='.2f', cmap=\"Blues\")\nplt.show()","metadata":{"id":"23o5zz0XWQ6r","outputId":"37e5c873-fad8-4a5b-f272-1199944c9c7c","execution":{"iopub.status.busy":"2021-05-25T05:20:06.959928Z","iopub.execute_input":"2021-05-25T05:20:06.960242Z","iopub.status.idle":"2021-05-25T05:20:07.281607Z","shell.execute_reply.started":"2021-05-25T05:20:06.96021Z","shell.execute_reply":"2021-05-25T05:20:07.280823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Test Cohen Kappa score: %.3f\" % cohen_kappa_score(test_labels, df_train_test['diagnosis'].astype('int'), weights='quadratic'))\nprint(\"Test Accuracy score : %.3f\" % accuracy_score(df_train_test['diagnosis'].astype('int'),test_labels))","metadata":{"id":"ZViH2sI0WQ6v","outputId":"0204531e-ec13-4c63-c7eb-1e12d6391233","execution":{"iopub.status.busy":"2021-05-25T05:20:16.100813Z","iopub.execute_input":"2021-05-25T05:20:16.101139Z","iopub.status.idle":"2021-05-25T05:20:16.113072Z","shell.execute_reply.started":"2021-05-25T05:20:16.101107Z","shell.execute_reply":"2021-05-25T05:20:16.111904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### <font color='red'> 9.6 ResNet50 Models Summary </font>","metadata":{"id":"wrNLulwLWQ62"}},{"cell_type":"code","source":"x = PrettyTable()\nx.field_names = [\"S.No.\",\"ResNet50 Model\",\"Image Processing\",\"Data Augmentation\",\"Hyperparameters(BS,Opt,lr,ep)\",\"Train QWK\",\"Test QWK\"]\n\nx.add_row([1,\"R-P-D-p(0.5)-D-p(0.5)-S(5)\",\"--\",\"Hor Flip,Scale 1/255\",\"(4,'Adam','1e-4',7)\",\"0.912\",\"0.905\"])\nx.add_row([2,\"R-P-D-p(0.5)-D-p(0.5)-S(5)\",\"Circle Crop, Gaussian Blur\",\"Hor Flip,Scale 1/255\",\"(4,'Adam','1e-4',7)\",\"0.98\",\"0.904\"])\n\nprint(x)","metadata":{"id":"ncmJ7qMzWQ62","outputId":"2f860351-5842-4a19-d260-d27da55b7954"},"execution_count":null,"outputs":[]}]}