{"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-02-08T09:26:15.787105Z","iopub.execute_input":"2023-02-08T09:26:15.787583Z","iopub.status.idle":"2023-02-08T09:26:23.236115Z","shell.execute_reply.started":"2023-02-08T09:26:15.787502Z","shell.execute_reply":"2023-02-08T09:26:23.235149Z"},"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-02-08T09:26:23.238090Z","iopub.execute_input":"2023-02-08T09:26:23.238970Z","iopub.status.idle":"2023-02-08T09:26:28.786903Z","shell.execute_reply.started":"2023-02-08T09:26:23.238934Z","shell.execute_reply":"2023-02-08T09:26:28.785857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = []\nlabels = []","metadata":{"execution":{"iopub.status.busy":"2023-02-08T09:26:28.788644Z","iopub.execute_input":"2023-02-08T09:26:28.789684Z","iopub.status.idle":"2023-02-08T09:26:28.804068Z","shell.execute_reply.started":"2023-02-08T09:26:28.789633Z","shell.execute_reply":"2023-02-08T09:26:28.802848Z"},"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-02-08T09:26:28.806993Z","iopub.execute_input":"2023-02-08T09:26:28.807360Z","iopub.status.idle":"2023-02-08T09:26:28.820392Z","shell.execute_reply.started":"2023-02-08T09:26:28.807326Z","shell.execute_reply":"2023-02-08T09:26:28.819174Z"},"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-02-08T09:26:28.822640Z","iopub.execute_input":"2023-02-08T09:26:28.823856Z","iopub.status.idle":"2023-02-08T09:26:28.862730Z","shell.execute_reply.started":"2023-02-08T09:26:28.823821Z","shell.execute_reply":"2023-02-08T09:26:28.861845Z"},"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-02-08T09:26:28.866751Z","iopub.execute_input":"2023-02-08T09:26:28.869058Z","iopub.status.idle":"2023-02-08T09:26:28.877565Z","shell.execute_reply.started":"2023-02-08T09:26:28.869019Z","shell.execute_reply":"2023-02-08T09:26:28.876498Z"},"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-02-08T09:26:28.882432Z","iopub.execute_input":"2023-02-08T09:26:28.884785Z","iopub.status.idle":"2023-02-08T09:30:14.160464Z","shell.execute_reply.started":"2023-02-08T09:26:28.884748Z","shell.execute_reply":"2023-02-08T09:30:14.157592Z"},"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-02-08T09:30:19.471745Z","iopub.execute_input":"2023-02-08T09:30:19.472115Z","iopub.status.idle":"2023-02-08T09:30:19.934161Z","shell.execute_reply.started":"2023-02-08T09:30:19.472083Z","shell.execute_reply":"2023-02-08T09:30:19.933006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(images)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T09:30:20.017684Z","iopub.execute_input":"2023-02-08T09:30:20.018032Z","iopub.status.idle":"2023-02-08T09:30:20.024193Z","shell.execute_reply.started":"2023-02-08T09:30:20.017983Z","shell.execute_reply":"2023-02-08T09:30:20.023270Z"},"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-02-08T09:30:20.576821Z","iopub.execute_input":"2023-02-08T09:30:20.577191Z","iopub.status.idle":"2023-02-08T09:30:20.583701Z","shell.execute_reply.started":"2023-02-08T09:30:20.577159Z","shell.execute_reply":"2023-02-08T09:30:20.582701Z"},"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-02-08T09:30:21.878891Z","iopub.execute_input":"2023-02-08T09:30:21.879585Z","iopub.status.idle":"2023-02-08T09:30:21.885601Z","shell.execute_reply.started":"2023-02-08T09:30:21.879547Z","shell.execute_reply":"2023-02-08T09:30:21.884559Z"},"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-02-08T09:30:23.998605Z","iopub.execute_input":"2023-02-08T09:30:23.999068Z","iopub.status.idle":"2023-02-08T09:30:24.898617Z","shell.execute_reply.started":"2023-02-08T09:30:23.999025Z","shell.execute_reply":"2023-02-08T09:30:24.897435Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T09:30:24.900516Z","iopub.execute_input":"2023-02-08T09:30:24.901022Z","iopub.status.idle":"2023-02-08T09:30:24.908909Z","shell.execute_reply.started":"2023-02-08T09:30:24.900963Z","shell.execute_reply":"2023-02-08T09:30:24.908027Z"},"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-02-08T09:30:27.107585Z","iopub.execute_input":"2023-02-08T09:30:27.107951Z","iopub.status.idle":"2023-02-08T09:30:30.098299Z","shell.execute_reply.started":"2023-02-08T09:30:27.107919Z","shell.execute_reply":"2023-02-08T09:30:30.097284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow\nmodel.compile(loss='categorical_crossentropy',\n              optimizer='adam', metrics=['accuracy'])\nhist = model.fit(x_train,y_train,batch_size=32,epochs=50,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T09:30:37.214979Z","iopub.execute_input":"2023-02-08T09:30:37.215566Z","iopub.status.idle":"2023-02-08T09:33:02.221969Z","shell.execute_reply.started":"2023-02-08T09:30:37.215531Z","shell.execute_reply":"2023-02-08T09:33:02.220916Z"},"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-02-08T09:33:02.223989Z","iopub.execute_input":"2023-02-08T09:33:02.224659Z","iopub.status.idle":"2023-02-08T09:33:04.275933Z","shell.execute_reply.started":"2023-02-08T09:33:02.224604Z","shell.execute_reply":"2023-02-08T09:33:04.274973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conMat = confusion_matrix(y_test.argmax(axis=1),pred.argmax(axis=1))\nprint(conMat)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T09:33:18.632247Z","iopub.execute_input":"2023-02-08T09:33:18.632609Z","iopub.status.idle":"2023-02-08T09:33:18.642047Z","shell.execute_reply.started":"2023-02-08T09:33:18.632577Z","shell.execute_reply":"2023-02-08T09:33:18.640922Z"},"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-02-08T09:33:16.482295Z","iopub.execute_input":"2023-02-08T09:33:16.482651Z","iopub.status.idle":"2023-02-08T09:33:16.490024Z","shell.execute_reply.started":"2023-02-08T09:33:16.482621Z","shell.execute_reply":"2023-02-08T09:33:16.488901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import normalize\nimport seaborn as sns\nnormed_confusion_matrix = normalize(conMat , axis = 1, norm = 'l1')\ncm_df = pd.DataFrame(normed_confusion_matrix)\nsns.heatmap (cm_df , annot =True)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T09:33:21.730080Z","iopub.execute_input":"2023-02-08T09:33:21.730766Z","iopub.status.idle":"2023-02-08T09:33:22.144354Z","shell.execute_reply.started":"2023-02-08T09:33:21.730731Z","shell.execute_reply":"2023-02-08T09:33:22.143250Z"},"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-02-08T09:33:25.140303Z","iopub.execute_input":"2023-02-08T09:33:25.140994Z","iopub.status.idle":"2023-02-08T09:33:25.154266Z","shell.execute_reply.started":"2023-02-08T09:33:25.140956Z","shell.execute_reply":"2023-02-08T09:33:25.153181Z"},"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-02-08T09:33:27.950809Z","iopub.execute_input":"2023-02-08T09:33:27.951211Z","iopub.status.idle":"2023-02-08T09:33:28.158330Z","shell.execute_reply.started":"2023-02-08T09:33:27.951177Z","shell.execute_reply":"2023-02-08T09:33:28.156949Z"},"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-02-08T09:33:34.677906Z","iopub.execute_input":"2023-02-08T09:33:34.678865Z","iopub.status.idle":"2023-02-08T09:33:34.875983Z","shell.execute_reply.started":"2023-02-08T09:33:34.678815Z","shell.execute_reply":"2023-02-08T09:33:34.874948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}