{"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)\nimport cv2\nimport keras\nimport os\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.preprocessing import image\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm\nfrom PIL import Image, ImageEnhance\nfrom keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,BatchNormalization,Activation,Dropout,GlobalAveragePooling2D\nfrom keras.models import Sequential,Model\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras.utils import plot_model\nfrom tensorflow.keras.optimizers import Adam","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-09T06:22:00.034793Z","iopub.execute_input":"2022-08-09T06:22:00.035211Z","iopub.status.idle":"2022-08-09T06:22:01.439643Z","shell.execute_reply.started":"2022-08-09T06:22:00.035176Z","shell.execute_reply":"2022-08-09T06:22:01.438183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\ntest_df = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:22:07.318021Z","iopub.execute_input":"2022-08-09T06:22:07.318452Z","iopub.status.idle":"2022-08-09T06:22:07.350997Z","shell.execute_reply.started":"2022-08-09T06:22:07.318418Z","shell.execute_reply":"2022-08-09T06:22:07.349381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['diagnosis'].hist()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:22:11.047774Z","iopub.execute_input":"2022-08-09T06:22:11.048224Z","iopub.status.idle":"2022-08-09T06:22:11.370157Z","shell.execute_reply.started":"2022-08-09T06:22:11.048186Z","shell.execute_reply":"2022-08-09T06:22:11.368453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"id_code\"] = train_df[\"id_code\"].apply(lambda x: x + \".png\")\ntest_df[\"id_code\"] = test_df[\"id_code\"].apply(lambda x: x + \".png\")\ntrain_df['diagnosis'] = train_df['diagnosis'].astype('str')","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:22:19.252139Z","iopub.execute_input":"2022-08-09T06:22:19.252645Z","iopub.status.idle":"2022-08-09T06:22:19.267824Z","shell.execute_reply.started":"2022-08-09T06:22:19.252605Z","shell.execute_reply":"2022-08-09T06:22:19.266384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=\"../input/aptos2019-blindness-detection/train_images/\"","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:22:23.482618Z","iopub.execute_input":"2022-08-09T06:22:23.483156Z","iopub.status.idle":"2022-08-09T06:22:23.488726Z","shell.execute_reply.started":"2022-08-09T06:22:23.483114Z","shell.execute_reply":"2022-08-09T06:22:23.487733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"dim = (255,255)dim = (width, height)","metadata":{}},{"cell_type":"code","source":"dim = (224,224)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:22:33.727748Z","iopub.execute_input":"2022-08-09T06:22:33.728229Z","iopub.status.idle":"2022-08-09T06:22:33.733808Z","shell.execute_reply.started":"2022-08-09T06:22:33.728191Z","shell.execute_reply":"2022-08-09T06:22:33.732712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image = []\nfor i in tqdm(range(train_df.shape[0])):\n    src = cv2.imread(str(train+(train_df[\"id_code\"][i])))\n    src = cv2.resize(src, (128,128)) \n    b, g, r = cv2.split(src)\n    g = cv2.GaussianBlur(src,(5,5),cv2.BORDER_DEFAULT)\n    train_image.append(g)\n        ","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:58:40.969845Z","iopub.execute_input":"2022-08-09T06:58:40.971646Z","iopub.status.idle":"2022-08-09T07:06:09.871529Z","shell.execute_reply.started":"2022-08-09T06:58:40.971577Z","shell.execute_reply":"2022-08-09T07:06:09.869939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"=train_df['diagnosis'].values","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:07:09.730479Z","iopub.execute_input":"2022-08-09T07:07:09.732142Z","iopub.status.idle":"2022-08-09T07:07:09.740228Z","shell.execute_reply.started":"2022-08-09T07:07:09.732075Z","shell.execute_reply":"2022-08-09T07:07:09.738176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_image)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:30:16.025878Z","iopub.execute_input":"2022-08-09T06:30:16.026255Z","iopub.status.idle":"2022-08-09T06:30:16.037637Z","shell.execute_reply.started":"2022-08-09T06:30:16.026213Z","shell.execute_reply":"2022-08-09T06:30:16.035887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_valid, Y_train, Y_valid=train_test_split(train_image,Y,test_size=0.2, \nrandom_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:30:16.039437Z","iopub.execute_input":"2022-08-09T06:30:16.039865Z","iopub.status.idle":"2022-08-09T06:30:16.052866Z","shell.execute_reply.started":"2022-08-09T06:30:16.039807Z","shell.execute_reply":"2022-08-09T06:30:16.051503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\ndatagen = ImageDataGenerator( \n rotation_range=30,\n zoom_range = 0.1,\n width_shift_range=0.2, \n height_shift_range=0.2, \n horizontal_flip=True, \n vertical_flip=True,\n validation_split=0.2,\n )","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:46:53.419844Z","iopub.execute_input":"2022-08-09T06:46:53.420501Z","iopub.status.idle":"2022-08-09T06:46:53.430653Z","shell.execute_reply.started":"2022-08-09T06:46:53.420449Z","shell.execute_reply":"2022-08-09T06:46:53.429101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:47:23.132468Z","iopub.execute_input":"2022-08-09T06:47:23.133043Z","iopub.status.idle":"2022-08-09T06:47:23.852916Z","shell.execute_reply.started":"2022-08-09T06:47:23.132995Z","shell.execute_reply":"2022-08-09T06:47:23.851538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen.fit(X_valid)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:30:18.351148Z","iopub.execute_input":"2022-08-09T06:30:18.351468Z","iopub.status.idle":"2022-08-09T06:30:18.484473Z","shell.execute_reply.started":"2022-08-09T06:30:18.351439Z","shell.execute_reply":"2022-08-09T06:30:18.483171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\ndef vgg19_model(num_classes=None):\n    \n    \"\"\" Adding custom model to the VGG-19\n\n    Args:\n      num_classes: Number of layers in the final layer(Number of classes)\n\n    Returns:\n      model: Returns the custom model added to VGG\n    \"\"\"\n\n    model = tf.keras.applications.VGG19(weights='imagenet', include_top=False,input_shape=(128,128,3))\n\n    model.outputs = [model.layers[-1].output]\n\n    #model.layers[-2].outbound_node= []\n    x=Conv2D(256, kernel_size=(2,2),strides=2)(model.output)\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)    \n    x=Conv2D(128, kernel_size=(2,2),strides=1)(x)\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)\n    x=Flatten()(x)\n    x=Dense(num_classes, activation='softmax')(x)\n\n    model=Model(model.input,x)\n\n\n    return model\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:30:18.513455Z","iopub.execute_input":"2022-08-09T06:30:18.513805Z","iopub.status.idle":"2022-08-09T06:30:18.529853Z","shell.execute_reply.started":"2022-08-09T06:30:18.513774Z","shell.execute_reply":"2022-08-09T06:30:18.528995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.vgg19 import VGG19\nfrom keras import backend as K\nnum_classes=5\nmodel = vgg19_model(num_classes)\nmodel.compile(optimizer=\"adam\", loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:38:39.06174Z","iopub.execute_input":"2022-08-09T06:38:39.063905Z","iopub.status.idle":"2022-08-09T06:38:39.764008Z","shell.execute_reply.started":"2022-08-09T06:38:39.063814Z","shell.execute_reply":"2022-08-09T06:38:39.762476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"datagen.fit(np.array(X_valid).reshape(2929, 224, 224, 1))","metadata":{}},{"cell_type":"code","source":"np.array(X_train).reshape( 128, 128, 3,2929)\nnp.array(Y_train).reshape(1,2929)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:49:32.984316Z","iopub.execute_input":"2022-08-09T06:49:32.98471Z","iopub.status.idle":"2022-08-09T06:49:33.078748Z","shell.execute_reply.started":"2022-08-09T06:49:32.984679Z","shell.execute_reply":"2022-08-09T06:49:33.077571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.array(X_train).shape()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:56:16.342005Z","iopub.execute_input":"2022-08-09T06:56:16.342459Z","iopub.status.idle":"2022-08-09T06:56:16.517548Z","shell.execute_reply.started":"2022-08-09T06:56:16.342423Z","shell.execute_reply":"2022-08-09T06:56:16.515304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(datagen.flow(x=X_train,y=Y_train),\nbatch_size=128,\nepochs=150,\nverbose=1,\nshuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:49:37.161535Z","iopub.execute_input":"2022-08-09T06:49:37.162031Z","iopub.status.idle":"2022-08-09T06:49:37.199667Z","shell.execute_reply.started":"2022-08-09T06:49:37.161988Z","shell.execute_reply":"2022-08-09T06:49:37.198011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(datagen.flow(X_train, Y_train, batch_size=32,\n         subset='training'),\n         validation_data=datagen.flow(X_train, Y_train,\n         batch_size=8, subset='validation'),\n         steps_per_epoch=len(X_train) / 32, epochs=epochs)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T03:11:27.254024Z","iopub.execute_input":"2022-08-08T03:11:27.254532Z","iopub.status.idle":"2022-08-08T03:11:27.291853Z","shell.execute_reply.started":"2022-08-08T03:11:27.25449Z","shell.execute_reply":"2022-08-08T03:11:27.289998Z"},"trusted":true},"execution_count":null,"outputs":[]}]}