{"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-13T18:28:52.481126Z","iopub.execute_input":"2023-05-13T18:28:52.481721Z","iopub.status.idle":"2023-05-13T18:29:00.775569Z","shell.execute_reply.started":"2023-05-13T18:28:52.481671Z","shell.execute_reply":"2023-05-13T18:29:00.773768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport cv2\nimport os\nfrom zipfile import ZipFile\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.utils import img_to_array\nfrom keras.utils import np_utils\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom keras.models import Sequential","metadata":{"execution":{"iopub.status.busy":"2023-05-13T18:29:16.055045Z","iopub.execute_input":"2023-05-13T18:29:16.055460Z","iopub.status.idle":"2023-05-13T18:29:24.733782Z","shell.execute_reply.started":"2023-05-13T18:29:16.055425Z","shell.execute_reply":"2023-05-13T18:29:24.732368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = []\nlabels = []","metadata":{"execution":{"iopub.status.busy":"2023-05-13T18:29:28.237498Z","iopub.execute_input":"2023-05-13T18:29:28.238237Z","iopub.status.idle":"2023-05-13T18:29:28.244132Z","shell.execute_reply.started":"2023-05-13T18:29:28.238185Z","shell.execute_reply":"2023-05-13T18:29:28.242887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_Images(label,path):\n    img=cv2.imread(path,cv2.IMREAD_COLOR)\n    try:\n      img_res=cv2.resize(img,(256,256))\n      img_array = img_to_array(img_res)\n      img_array = img_array/255.0\n      dataset.append(img_array)\n      if str(label) == '0':\n        labels.append('0')\n      else:\n        labels.append('1')\n    except:\n      print(\"error\")","metadata":{"execution":{"iopub.status.busy":"2023-05-13T18:29:31.884798Z","iopub.execute_input":"2023-05-13T18:29:31.886098Z","iopub.status.idle":"2023-05-13T18:29:31.895950Z","shell.execute_reply.started":"2023-05-13T18:29:31.886039Z","shell.execute_reply":"2023-05-13T18:29:31.894665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_Data = pd.read_csv(\"/kaggle/input/aptos2019-blindness-detection/train.csv\")\ntrain_Data.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-13T18:29:35.887825Z","iopub.execute_input":"2023-05-13T18:29:35.888446Z","iopub.status.idle":"2023-05-13T18:29:35.937999Z","shell.execute_reply.started":"2023-05-13T18:29:35.888395Z","shell.execute_reply":"2023-05-13T18:29:35.936676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_code_Data = train_Data['id_code']\ndiagnosis_Data = train_Data['diagnosis']","metadata":{"execution":{"iopub.status.busy":"2023-05-13T18:29:38.626623Z","iopub.execute_input":"2023-05-13T18:29:38.627136Z","iopub.status.idle":"2023-05-13T18:29:38.641235Z","shell.execute_reply.started":"2023-05-13T18:29:38.627081Z","shell.execute_reply":"2023-05-13T18:29:38.639563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for id_code,diagnosis in tqdm(zip(id_code_Data,diagnosis_Data)):\n    path = os.path.join('/kaggle/input/aptos2019-blindness-detection/train_images','{}.png'.format(id_code))\n    prepare_Images(diagnosis,path)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T18:29:41.784574Z","iopub.execute_input":"2023-05-13T18:29:41.785023Z","iopub.status.idle":"2023-05-13T18:37:23.042131Z","shell.execute_reply.started":"2023-05-13T18:29:41.784986Z","shell.execute_reply":"2023-05-13T18:37:23.040289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = np.array(dataset)\nlabel_arr = np.array(labels)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-13T18:37:35.777888Z","iopub.execute_input":"2023-05-13T18:37:35.778904Z","iopub.status.idle":"2023-05-13T18:37:37.073154Z","shell.execute_reply.started":"2023-05-13T18:37:35.778845Z","shell.execute_reply":"2023-05-13T18:37:37.071593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(images)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T18:37:38.842549Z","iopub.execute_input":"2023-05-13T18:37:38.843001Z","iopub.status.idle":"2023-05-13T18:37:38.852353Z","shell.execute_reply.started":"2023-05-13T18:37:38.842961Z","shell.execute_reply":"2023-05-13T18:37:38.850828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nfor i in label_arr:\n  if i == '1':\n    count = count+1\nprint(\"no of defected eyes: \",count)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T18:37:41.664841Z","iopub.execute_input":"2023-05-13T18:37:41.665293Z","iopub.status.idle":"2023-05-13T18:37:41.676949Z","shell.execute_reply.started":"2023-05-13T18:37:41.665258Z","shell.execute_reply":"2023-05-13T18:37:41.674675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nfor i in label_arr:\n  if i == '0':\n    count = count+1\nprint(\"no of healthy eyes: \",count)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T18:37:43.925399Z","iopub.execute_input":"2023-05-13T18:37:43.926444Z","iopub.status.idle":"2023-05-13T18:37:43.936767Z","shell.execute_reply.started":"2023-05-13T18:37:43.926394Z","shell.execute_reply":"2023-05-13T18:37:43.935204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test = train_test_split(images,label_arr,stratify=label_arr,test_size=0.20,random_state=44)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-13T18:37:46.400712Z","iopub.execute_input":"2023-05-13T18:37:46.401123Z","iopub.status.idle":"2023-05-13T18:37:48.802185Z","shell.execute_reply.started":"2023-05-13T18:37:46.401090Z","shell.execute_reply":"2023-05-13T18:37:48.801188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.model_selection import train_test_split\n# X_train, X_test, y_train, y_test = train_test_split(images,label_arr,stratify=label_arr, test_size=0.1, random_state=42)\n# X_train, X_val, y_train, y_val = train_test_split(X_train, y_train,stratify=y_train, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T16:20:31.110774Z","iopub.execute_input":"2023-05-13T16:20:31.111417Z","iopub.status.idle":"2023-05-13T16:20:34.026507Z","shell.execute_reply.started":"2023-05-13T16:20:31.111373Z","shell.execute_reply":"2023-05-13T16:20:34.025143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train= np_utils.to_categorical(y_train, num_classes=2)\ny_test = np_utils.to_categorical(y_test, num_classes=2)\n# y_val =np_utils.to_categorical(y_val, num_classes=2)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T18:37:56.117547Z","iopub.execute_input":"2023-05-13T18:37:56.117975Z","iopub.status.idle":"2023-05-13T18:37:56.127398Z","shell.execute_reply.started":"2023-05-13T18:37:56.117939Z","shell.execute_reply":"2023-05-13T18:37:56.126084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(filters=16,kernel_size=2,padding=\"same\",activation=\"relu\",input_shape=(256,256,3)))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=32,kernel_size=2,padding=\"same\",activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=64,kernel_size=2,padding=\"same\",activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=128,kernel_size=2,padding=\"same\",activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Dropout(0.2))\nmodel.add(Flatten())\nmodel.add(Dense(512,activation=\"relu\"))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(2,activation=\"sigmoid\"))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-13T18:38:00.085718Z","iopub.execute_input":"2023-05-13T18:38:00.086202Z","iopub.status.idle":"2023-05-13T18:38:00.583599Z","shell.execute_reply.started":"2023-05-13T18:38:00.086162Z","shell.execute_reply":"2023-05-13T18:38:00.582525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow\n# from keras.callbacks import EarlyStopping\n# early_stop = EarlyStopping(monitor='val_loss', patience=3) callbacks=[early_stop]\nmodel.compile(loss='categorical_crossentropy',\n              optimizer='adam', metrics=['accuracy'])\nhist = model.fit(x_train,y_train,validation_split=0.10,batch_size=32,epochs=50,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T18:49:15.186025Z","iopub.execute_input":"2023-05-13T18:49:15.186588Z","iopub.status.idle":"2023-05-13T19:19:17.597290Z","shell.execute_reply.started":"2023-05-13T18:49:15.186542Z","shell.execute_reply":"2023-05-13T19:19:17.596260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(x_test)\n\nmodel.evaluate(x_test,y_test)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-13T19:19:25.694243Z","iopub.execute_input":"2023-05-13T19:19:25.694713Z","iopub.status.idle":"2023-05-13T19:19:33.913625Z","shell.execute_reply.started":"2023-05-13T19:19:25.694673Z","shell.execute_reply":"2023-05-13T19:19:33.912249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, classification_report,confusion_matrix\nscore = round(accuracy_score(y_test.argmax(axis=1), pred.argmax(axis=1)),2)\nprint(score)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T19:20:03.507662Z","iopub.execute_input":"2023-05-13T19:20:03.508104Z","iopub.status.idle":"2023-05-13T19:20:03.518811Z","shell.execute_reply.started":"2023-05-13T19:20:03.508070Z","shell.execute_reply":"2023-05-13T19:20:03.517162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"report = classification_report(y_test.argmax(axis=1), pred.argmax(axis=1))\nprint(report)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T19:20:40.440972Z","iopub.execute_input":"2023-05-13T19:20:40.441409Z","iopub.status.idle":"2023-05-13T19:20:40.457822Z","shell.execute_reply.started":"2023-05-13T19:20:40.441369Z","shell.execute_reply":"2023-05-13T19:20:40.456536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = hist.history['accuracy']\nloss = hist.history['loss']\n\nplt.figure(figsize = (8,8))\nplt.subplot(1,2,1)\nplt.plot(range(50),acc,label='Training Accuracy')\nplt.legend(loc=\"lower right\")\nplt.title(\"Training over 50 epochs\")\n","metadata":{"execution":{"iopub.status.busy":"2023-05-13T19:20:44.941216Z","iopub.execute_input":"2023-05-13T19:20:44.941650Z","iopub.status.idle":"2023-05-13T19:20:45.235702Z","shell.execute_reply.started":"2023-05-13T19:20:44.941614Z","shell.execute_reply":"2023-05-13T19:20:45.234318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (8,8))\nplt.subplot(1,2,1)\nplt.plot(range(50),loss,label='Training Loss')\nplt.legend(loc=\"upper right\")\nplt.title(\"Training Loss over 50 epochs\")","metadata":{"execution":{"iopub.status.busy":"2023-05-13T19:20:49.765626Z","iopub.execute_input":"2023-05-13T19:20:49.766937Z","iopub.status.idle":"2023-05-13T19:20:50.005797Z","shell.execute_reply.started":"2023-05-13T19:20:49.766889Z","shell.execute_reply":"2023-05-13T19:20:50.004342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}