{"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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2021-05-24T14:44:04.682171Z","iopub.execute_input":"2021-05-24T14:44:04.682502Z","iopub.status.idle":"2021-05-24T14:44:05.257055Z","shell.execute_reply.started":"2021-05-24T14:44:04.682475Z","shell.execute_reply":"2021-05-24T14:44:05.255342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nfrom keras.models import Sequential, load_model\nfrom keras.layers import (Activation, Dropout, Flatten, Dense, GlobalMaxPooling2D,\n                          BatchNormalization, Input, Conv2D, GlobalAveragePooling2D,concatenate,Concatenate)\nfrom keras.callbacks import ModelCheckpoint\nfrom keras import metrics\nfrom keras.optimizers import Adam \nfrom keras import backend as K\nimport keras\nfrom keras.models import Model\nimport matplotlib.pyplot as plt\n\nimport PIL\nfrom PIL import Image, ImageOps\nimport cv2\nfrom keras.losses import binary_crossentropy, categorical_crossentropy\nimport keras.backend as K\nimport tensorflow as tf\nfrom sklearn.metrics import f1_score, fbeta_score, cohen_kappa_score\nfrom tensorflow.keras import Sequential\nfrom keras.utils.np_utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport imgaug as ia\nfrom keras.applications.vgg16 import VGG16","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:05.393996Z","iopub.execute_input":"2021-05-24T14:44:05.394385Z","iopub.status.idle":"2021-05-24T14:44:05.403789Z","shell.execute_reply.started":"2021-05-24T14:44:05.394354Z","shell.execute_reply":"2021-05-24T14:44:05.403049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\ndf_test = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:06.276808Z","iopub.execute_input":"2021-05-24T14:44:06.277165Z","iopub.status.idle":"2021-05-24T14:44:06.30296Z","shell.execute_reply.started":"2021-05-24T14:44:06.27713Z","shell.execute_reply":"2021-05-24T14:44:06.301864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:07.072317Z","iopub.execute_input":"2021-05-24T14:44:07.072651Z","iopub.status.idle":"2021-05-24T14:44:07.082781Z","shell.execute_reply.started":"2021-05-24T14:44:07.072621Z","shell.execute_reply":"2021-05-24T14:44:07.081977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[:20]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:08.871773Z","iopub.execute_input":"2021-05-24T14:44:08.872049Z","iopub.status.idle":"2021-05-24T14:44:08.88224Z","shell.execute_reply.started":"2021-05-24T14:44:08.872025Z","shell.execute_reply":"2021-05-24T14:44:08.881267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def work(x):\n    if x == 0:\n        return 0\n    else:\n        return 1","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:09.704685Z","iopub.execute_input":"2021-05-24T14:44:09.70508Z","iopub.status.idle":"2021-05-24T14:44:09.710883Z","shell.execute_reply.started":"2021-05-24T14:44:09.705045Z","shell.execute_reply":"2021-05-24T14:44:09.709373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['diagnosis']= df_train['diagnosis'].apply(work)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:10.276338Z","iopub.execute_input":"2021-05-24T14:44:10.276646Z","iopub.status.idle":"2021-05-24T14:44:10.286747Z","shell.execute_reply.started":"2021-05-24T14:44:10.276615Z","shell.execute_reply":"2021-05-24T14:44:10.28486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[:20]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:10.919749Z","iopub.execute_input":"2021-05-24T14:44:10.92012Z","iopub.status.idle":"2021-05-24T14:44:10.930513Z","shell.execute_reply.started":"2021-05-24T14:44:10.920067Z","shell.execute_reply":"2021-05-24T14:44:10.929908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_train = df_train.shape[0]\nnum_test = df_test.shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:13.76304Z","iopub.execute_input":"2021-05-24T14:44:13.763478Z","iopub.status.idle":"2021-05-24T14:44:13.766906Z","shell.execute_reply.started":"2021-05-24T14:44:13.763441Z","shell.execute_reply":"2021-05-24T14:44:13.766274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images =  \"../input/aptos2019-blindness-detection/train_images/\"\ntest_images = \"../input/aptos2019-blindness-detection/test_images/\"","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:14.263022Z","iopub.execute_input":"2021-05-24T14:44:14.263449Z","iopub.status.idle":"2021-05-24T14:44:14.266334Z","shell.execute_reply.started":"2021-05-24T14:44:14.263392Z","shell.execute_reply":"2021-05-24T14:44:14.265834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = load_img(train_images +  df_train['id_code'].iloc[50] +\".png\")\nplt.imshow(img)\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:15.070691Z","iopub.execute_input":"2021-05-24T14:44:15.071102Z","iopub.status.idle":"2021-05-24T14:44:15.771298Z","shell.execute_reply.started":"2021-05-24T14:44:15.071077Z","shell.execute_reply":"2021-05-24T14:44:15.770776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train['id_code']\ny = df_train['diagnosis']\n\n\ny.hist()","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:17.501954Z","iopub.execute_input":"2021-05-24T14:44:17.502372Z","iopub.status.idle":"2021-05-24T14:44:17.644257Z","shell.execute_reply.started":"2021-05-24T14:44:17.502335Z","shell.execute_reply":"2021-05-24T14:44:17.642897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title('classify')\nplt.xlabel('class 0/1')\nplt.ylabel('Counts')\ndf_train['diagnosis'].value_counts().plot(kind = 'bar')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:19.535418Z","iopub.execute_input":"2021-05-24T14:44:19.535695Z","iopub.status.idle":"2021-05-24T14:44:19.652486Z","shell.execute_reply.started":"2021-05-24T14:44:19.535671Z","shell.execute_reply":"2021-05-24T14:44:19.651656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 2","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:20.019209Z","iopub.execute_input":"2021-05-24T14:44:20.019515Z","iopub.status.idle":"2021-05-24T14:44:20.023876Z","shell.execute_reply.started":"2021-05-24T14:44:20.019488Z","shell.execute_reply":"2021-05-24T14:44:20.022651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = to_categorical(y, num_classes=num_classes)\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:21.911386Z","iopub.execute_input":"2021-05-24T14:44:21.911674Z","iopub.status.idle":"2021-05-24T14:44:21.917481Z","shell.execute_reply.started":"2021-05-24T14:44:21.911647Z","shell.execute_reply":"2021-05-24T14:44:21.916954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg = VGG16()\nvgg_layers_list = vgg.layers\nmodel = Sequential()\n\nfor i in range(len(vgg_layers_list)-1):\n    model.add(vgg_layers_list[i])\nfor layers in model.layers:\n    layers.trainable = False\n\nmodel.add(Dense(num_classes, activation=\"sigmoid\"))\n\nmodel.compile(loss = \"binary_crossentropy\",\n              optimizer = \"adam\",\n              metrics = [\"accuracy\"])\n","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:22.151331Z","iopub.execute_input":"2021-05-24T14:44:22.151691Z","iopub.status.idle":"2021-05-24T14:44:24.822003Z","shell.execute_reply.started":"2021-05-24T14:44:22.151666Z","shell.execute_reply":"2021-05-24T14:44:24.820594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:26.267282Z","iopub.execute_input":"2021-05-24T14:44:26.26765Z","iopub.status.idle":"2021-05-24T14:44:26.273378Z","shell.execute_reply.started":"2021-05-24T14:44:26.267619Z","shell.execute_reply":"2021-05-24T14:44:26.271804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:28.991694Z","iopub.execute_input":"2021-05-24T14:44:28.992031Z","iopub.status.idle":"2021-05-24T14:44:28.999621Z","shell.execute_reply.started":"2021-05-24T14:44:28.992002Z","shell.execute_reply":"2021-05-24T14:44:28.998083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:38.284933Z","iopub.execute_input":"2021-05-24T14:44:38.285275Z","iopub.status.idle":"2021-05-24T14:44:38.418726Z","shell.execute_reply.started":"2021-05-24T14:44:38.285246Z","shell.execute_reply":"2021-05-24T14:44:38.417833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:42.250662Z","iopub.execute_input":"2021-05-24T14:44:42.250982Z","iopub.status.idle":"2021-05-24T14:44:42.255959Z","shell.execute_reply.started":"2021-05-24T14:44:42.250958Z","shell.execute_reply":"2021-05-24T14:44:42.254861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:45.306291Z","iopub.execute_input":"2021-05-24T14:44:45.306577Z","iopub.status.idle":"2021-05-24T14:44:45.321105Z","shell.execute_reply.started":"2021-05-24T14:44:45.306554Z","shell.execute_reply":"2021-05-24T14:44:45.319963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:49.333376Z","iopub.execute_input":"2021-05-24T14:44:49.333647Z","iopub.status.idle":"2021-05-24T14:44:49.345832Z","shell.execute_reply.started":"2021-05-24T14:44:49.333624Z","shell.execute_reply":"2021-05-24T14:44:49.344217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diagnosis = [0,1]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:51.35288Z","iopub.execute_input":"2021-05-24T14:44:51.35337Z","iopub.status.idle":"2021-05-24T14:44:51.356087Z","shell.execute_reply.started":"2021-05-24T14:44:51.35332Z","shell.execute_reply":"2021-05-24T14:44:51.355564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen=ImageDataGenerator(rescale=1./255.)\ntest_datagen=ImageDataGenerator(rescale=1./255.)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:51.921012Z","iopub.execute_input":"2021-05-24T14:44:51.921416Z","iopub.status.idle":"2021-05-24T14:44:51.925285Z","shell.execute_reply.started":"2021-05-24T14:44:51.921391Z","shell.execute_reply":"2021-05-24T14:44:51.924401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['diagnosis'] = df_train['diagnosis'].astype('str')","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:54.116655Z","iopub.execute_input":"2021-05-24T14:44:54.116979Z","iopub.status.idle":"2021-05-24T14:44:54.124432Z","shell.execute_reply.started":"2021-05-24T14:44:54.116954Z","shell.execute_reply":"2021-05-24T14:44:54.123218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def append_ext(fn):\n    return fn+\".png\"\n\ndf_train[\"id_code\"]=df_train[\"id_code\"].apply(append_ext)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:55.032726Z","iopub.execute_input":"2021-05-24T14:44:55.033049Z","iopub.status.idle":"2021-05-24T14:44:55.039729Z","shell.execute_reply.started":"2021-05-24T14:44:55.033026Z","shell.execute_reply":"2021-05-24T14:44:55.038677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test[\"id_code\"]=df_test[\"id_code\"].apply(append_ext)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:56.277059Z","iopub.execute_input":"2021-05-24T14:44:56.277347Z","iopub.status.idle":"2021-05-24T14:44:56.283864Z","shell.execute_reply.started":"2021-05-24T14:44:56.277323Z","shell.execute_reply":"2021-05-24T14:44:56.282933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator=datagen.flow_from_dataframe(\n    dataframe=df_train[:30],\n    directory= \"../input/aptos2019-blindness-detection/train_images\",\n    x_col=\"id_code\",\n    y_col=\"diagnosis\",\n    batch_size=32,\n    seed=42,\n    shuffle=True,\n    class_mode=\"categorical\",\n    target_size=(224,224))","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:44:59.587893Z","iopub.execute_input":"2021-05-24T14:44:59.58829Z","iopub.status.idle":"2021-05-24T14:44:59.620685Z","shell.execute_reply.started":"2021-05-24T14:44:59.588265Z","shell.execute_reply":"2021-05-24T14:44:59.619897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator=test_datagen.flow_from_dataframe(\ndataframe=df_test,\ndirectory=\"../input/aptos2019-blindness-detection/test_images\",\nx_col=\"id_code\",\nbatch_size=1,\nseed=42,\nshuffle=False,\nclass_mode=None,\ntarget_size=(224,224))","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:45:00.031842Z","iopub.execute_input":"2021-05-24T14:45:00.032162Z","iopub.status.idle":"2021-05-24T14:45:00.990789Z","shell.execute_reply.started":"2021-05-24T14:45:00.032127Z","shell.execute_reply":"2021-05-24T14:45:00.990257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator=datagen.flow_from_dataframe(\ndataframe=df_train[30:60],\ndirectory=\"../input/aptos2019-blindness-detection/train_images\",\nx_col=\"id_code\",\ny_col=\"diagnosis\",\nbatch_size=32,\nseed=42,\nshuffle=True,\nclass_mode=\"categorical\",\nclasses=[\"0\", \"1\"],\ntarget_size=(224,224))","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:45:01.072622Z","iopub.execute_input":"2021-05-24T14:45:01.073158Z","iopub.status.idle":"2021-05-24T14:45:01.094461Z","shell.execute_reply.started":"2021-05-24T14:45:01.073133Z","shell.execute_reply":"2021-05-24T14:45:01.093603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:45:02.800694Z","iopub.execute_input":"2021-05-24T14:45:02.801024Z","iopub.status.idle":"2021-05-24T14:45:02.805022Z","shell.execute_reply.started":"2021-05-24T14:45:02.800996Z","shell.execute_reply":"2021-05-24T14:45:02.803891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(num_test)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:45:03.148749Z","iopub.execute_input":"2021-05-24T14:45:03.149121Z","iopub.status.idle":"2021-05-24T14:45:03.153924Z","shell.execute_reply.started":"2021-05-24T14:45:03.149095Z","shell.execute_reply":"2021-05-24T14:45:03.153067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (num_test // batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:45:03.794223Z","iopub.execute_input":"2021-05-24T14:45:03.794645Z","iopub.status.idle":"2021-05-24T14:45:03.79918Z","shell.execute_reply.started":"2021-05-24T14:45:03.794609Z","shell.execute_reply":"2021-05-24T14:45:03.798637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator,\n           steps_per_epoch=num_train//batch_size,\n           epochs = 10,\n           validation_data=valid_generator,\n           validation_steps= num_test//batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:45:06.595846Z","iopub.execute_input":"2021-05-24T14:45:06.596168Z","iopub.status.idle":"2021-05-24T14:45:37.476509Z","shell.execute_reply.started":"2021-05-24T14:45:06.596135Z","shell.execute_reply":"2021-05-24T14:45:37.475638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport torchvision.transforms as transforms\nfrom torchvision import datasets\nfrom PIL import ImageFile\nImageFile.LOAD_TRUNCATED_IMAGES = True\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_train = history.history['loss']\nloss_val = history.history['val_loss']\nepochs = range(1,2)\nplt.plot(epochs, loss_train, 'g', marker ='o' ,label='Training loss')\nplt.plot(epochs, loss_val, 'b', marker = 'o',label='validation loss')\nplt.title('Training and Validation loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:46:36.338319Z","iopub.execute_input":"2021-05-24T14:46:36.338745Z","iopub.status.idle":"2021-05-24T14:46:36.758071Z","shell.execute_reply.started":"2021-05-24T14:46:36.33872Z","shell.execute_reply":"2021-05-24T14:46:36.756562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"VGG16_predict()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-05-24T14:46:43.096341Z","iopub.execute_input":"2021-05-24T14:46:43.096712Z","iopub.status.idle":"2021-05-24T14:46:43.108063Z","shell.execute_reply.started":"2021-05-24T14:46:43.096683Z","shell.execute_reply":"2021-05-24T14:46:43.107028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}