{"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":"markdown","source":"source : [https://www.kaggle.com/code/mehmetbicici/diabetic-retinopathy-research-and-detection/notebook](http://)","metadata":{}},{"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-10-07T06:24:13.738888Z","iopub.execute_input":"2023-10-07T06:24:13.739585Z","iopub.status.idle":"2023-10-07T06:24:14.118168Z","shell.execute_reply.started":"2023-10-07T06:24:13.739552Z","shell.execute_reply":"2023-10-07T06:24:14.117123Z"},"jupyter":{"source_hidden":true}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os, sys\nimport matplotlib.pyplot as plt\nimport cv2\nfrom sklearn.model_selection import train_test_split\nfrom PIL import Image\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nIMG_SIZE = 512","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:02:58.902372Z","iopub.execute_input":"2023-10-07T07:02:58.902725Z","iopub.status.idle":"2023-10-07T07:02:58.909303Z","shell.execute_reply.started":"2023-10-07T07:02:58.902699Z","shell.execute_reply":"2023-10-07T07:02:58.907589Z"},"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":"2023-10-07T07:03:00.013245Z","iopub.execute_input":"2023-10-07T07:03:00.013648Z","iopub.status.idle":"2023-10-07T07:03:00.031735Z","shell.execute_reply.started":"2023-10-07T07:03:00.013619Z","shell.execute_reply":"2023-10-07T07:03:00.030875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:03:00.200645Z","iopub.execute_input":"2023-10-07T07:03:00.201260Z","iopub.status.idle":"2023-10-07T07:03:00.211714Z","shell.execute_reply.started":"2023-10-07T07:03:00.201227Z","shell.execute_reply":"2023-10-07T07:03:00.210145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = df_train[\"id_code\"]\ny = df_train[\"diagnosis\"]","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:03:00.364300Z","iopub.execute_input":"2023-10-07T07:03:00.366471Z","iopub.status.idle":"2023-10-07T07:03:00.371534Z","shell.execute_reply.started":"2023-10-07T07:03:00.366422Z","shell.execute_reply":"2023-10-07T07:03:00.370640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = [\"Train\",\"Test\"]\nsizes = [len(df_train),len(df_test)]\n\nplt.pie(sizes,labels=labels,autopct='%1.1f%%')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:03:00.521570Z","iopub.execute_input":"2023-10-07T07:03:00.521940Z","iopub.status.idle":"2023-10-07T07:03:00.633159Z","shell.execute_reply.started":"2023-10-07T07:03:00.521904Z","shell.execute_reply":"2023-10-07T07:03:00.632088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.15, random_state=42)\nprint(\"x_train shape: \", x_train.shape)\nprint(\"x_test shape: \", x_test.shape)\nprint(\"y_train shape: \",y_train.shape)\nprint(\"y_test shape: \", y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:03:00.679482Z","iopub.execute_input":"2023-10-07T07:03:00.680121Z","iopub.status.idle":"2023-10-07T07:03:00.693900Z","shell.execute_reply.started":"2023-10-07T07:03:00.680070Z","shell.execute_reply":"2023-10-07T07:03:00.692707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(y_train,label=\"y_train\")\nplt.hist(y_test,label=\"y_test\")\nplt.title(\"Retinopathy Type and number\")\nplt.xlabel(\"0:No DR,1:Mild , 2:Moderate, 3:Severe, 4:Proliferative DR\")\nplt.ylabel(\"Number\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:03:00.837222Z","iopub.execute_input":"2023-10-07T07:03:00.837939Z","iopub.status.idle":"2023-10-07T07:03:01.172629Z","shell.execute_reply.started":"2023-10-07T07:03:00.837901Z","shell.execute_reply":"2023-10-07T07:03:01.171568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# IMAGE PROCESSING","metadata":{}},{"cell_type":"code","source":" %%time\nfig = plt.figure(figsize=(25, 16))\n# display 10 images from each class\nfor i in sorted(y_train.unique()):\n    for j, (idx, row) in enumerate(df_train.loc[df_train['diagnosis'] == i].sample(5, random_state=42).iterrows()):\n        ax = fig.add_subplot(5, 5, i * 5 + j + 1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n        plt.imshow(image)\n        ax.set_title('Label: %d-%d-%s' % (i, idx, row['id_code']) )","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:20:09.542453Z","iopub.execute_input":"2023-10-07T07:20:09.542929Z","iopub.status.idle":"2023-10-07T07:20:16.028801Z","shell.execute_reply.started":"2023-10-07T07:20:09.542887Z","shell.execute_reply":"2023-10-07T07:20:16.027349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfig=plt.figure(figsize=(25,16))\nfor i in sorted(y_train.unique()):\n    for j ,(idx,row) in enumerate(df_train.loc[df_train[\"diagnosis\"]==i].sample(5,random_state=42).iterrows()):\n        ax = fig.add_subplot(5, 5, i*5+j+1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n\n        plt.imshow(image)\n        ax.set_title('Label: %d-%d-%s' % (i, idx, row['id_code']) )","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:20:16.031414Z","iopub.execute_input":"2023-10-07T07:20:16.032128Z","iopub.status.idle":"2023-10-07T07:20:21.777952Z","shell.execute_reply.started":"2023-10-07T07:20:16.032090Z","shell.execute_reply":"2023-10-07T07:20:21.776843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfig=plt.figure(figsize=(25,16))\nfor i in sorted(y_train.unique()):\n    for j ,(idx,row) in enumerate(df_train.loc[df_train[\"diagnosis\"]==i].sample(5,random_state=42).iterrows()):\n        ax = fig.add_subplot(5, 5, i*5+j+1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n\n        plt.imshow(image,cmap=\"gray\")\n        ax.set_title('Label: %d-%d-%s' % (i, idx, row['id_code']) )","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:24:38.125985Z","iopub.execute_input":"2023-10-07T07:24:38.126402Z","iopub.status.idle":"2023-10-07T07:24:43.609584Z","shell.execute_reply.started":"2023-10-07T07:24:38.126371Z","shell.execute_reply":"2023-10-07T07:24:43.608598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dpi=80\n\npath=f\"../input/aptos2019-blindness-detection/train_images/838c87c63422.png\"\nimage = cv2.imread(path)\nimage=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)\nheight,width=image.shape\nprint(height,width)\nSCALE=2\nfigsize=(width/float(dpi))/SCALE,(height/float(dpi))/SCALE\nimage = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\nfig=plt.figure(figsize=figsize)\nplt.imshow(image,cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:25:17.941856Z","iopub.execute_input":"2023-10-07T07:25:17.942224Z","iopub.status.idle":"2023-10-07T07:25:18.676543Z","shell.execute_reply.started":"2023-10-07T07:25:17.942196Z","shell.execute_reply":"2023-10-07T07:25:18.675498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# BEN GRAHAM'S LIGTHING METHOD","metadata":{}},{"cell_type":"code","source":"fig=plt.figure(figsize=(25,16))\nfor i in sorted(y_train.unique()):\n    for j,(idx,row) in enumerate(df_train.loc[df_train[\"diagnosis\"]==i].sample(5,random_state=42).iterrows()):\n        \n        ax = fig.add_subplot(5, 5, i*5+j+1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n\n        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n        image=cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , IMG_SIZE/10) ,-4 ,128) \n\n        plt.imshow(image, cmap='gray')\n        ax.set_title('Label: %d-%d-%s' % (i, idx, row['id_code']) )","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:26:07.979946Z","iopub.execute_input":"2023-10-07T07:26:07.980328Z","iopub.status.idle":"2023-10-07T07:26:22.696186Z","shell.execute_reply.started":"2023-10-07T07:26:07.980299Z","shell.execute_reply":"2023-10-07T07:26:22.695133Z"},"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","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:27:07.249623Z","iopub.execute_input":"2023-10-07T07:27:07.250028Z","iopub.status.idle":"2023-10-07T07:27:07.259434Z","shell.execute_reply.started":"2023-10-07T07:27:07.249997Z","shell.execute_reply":"2023-10-07T07:27:07.258134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_ben_color(path, sigmaX=10):\n    image = cv2.imread(path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = crop_image_from_gray(image)\n    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n    image=cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , sigmaX) ,-4 ,128)\n        \n    return image","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:27:17.228110Z","iopub.execute_input":"2023-10-07T07:27:17.228526Z","iopub.status.idle":"2023-10-07T07:27:17.236197Z","shell.execute_reply.started":"2023-10-07T07:27:17.228495Z","shell.execute_reply":"2023-10-07T07:27:17.234740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUM_SAMP=7\nfig = plt.figure(figsize=(25, 16))\nfor i in sorted(y_train.unique()):\n    for j, (idx, row) in enumerate(df_train.loc[df_train['diagnosis'] == i].sample(NUM_SAMP, random_state=42).iterrows()):\n        ax = fig.add_subplot(5, NUM_SAMP, i* NUM_SAMP + j + 1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = load_ben_color(path,sigmaX=30)\n\n        plt.imshow(image)\n        ax.set_title('%d-%d-%s' % (i, idx, row['id_code']) )","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:27:33.404850Z","iopub.execute_input":"2023-10-07T07:27:33.405235Z","iopub.status.idle":"2023-10-07T07:27:51.378480Z","shell.execute_reply.started":"2023-10-07T07:27:33.405205Z","shell.execute_reply":"2023-10-07T07:27:51.376846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_ben_color2(path, sigmaX=10):\n    image = cv2.imread(path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    image = crop_image_from_gray(image)\n    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n  \n        \n    return image","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:27:51.380194Z","iopub.execute_input":"2023-10-07T07:27:51.380756Z","iopub.status.idle":"2023-10-07T07:27:51.386969Z","shell.execute_reply.started":"2023-10-07T07:27:51.380725Z","shell.execute_reply":"2023-10-07T07:27:51.385418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dpi = 80 #inch\n\npath=f\"../input/aptos2019-blindness-detection/train_images/838c87c63422.png\" \nimage = load_ben_color(path,sigmaX=10)\n\nheight, width = IMG_SIZE, IMG_SIZE\nprint(height, width)\n\nSCALE=1\nfigsize = (width / float(dpi))/SCALE, (height / float(dpi))/SCALE\n\nfig = plt.figure(figsize=figsize)\nplt.imshow(image, cmap='gray')\n\ndpi = 80 #inch\n\npath=f\"../input/aptos2019-blindness-detection/train_images/838c87c63422.png\" \nimage = load_ben_color2(path,sigmaX=10)\n\nheight, width = IMG_SIZE, IMG_SIZE\nprint(height, width)\n\nSCALE=1\nfigsize = (width / float(dpi))/SCALE, (height / float(dpi))/SCALE\n\nfig = plt.figure(figsize=figsize)\nplt.imshow(image, cmap='gray')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:28:45.329284Z","iopub.execute_input":"2023-10-07T07:28:45.329849Z","iopub.status.idle":"2023-10-07T07:28:46.541884Z","shell.execute_reply.started":"2023-10-07T07:28:45.329809Z","shell.execute_reply":"2023-10-07T07:28:46.540850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dpi = 80 #inch\n\npath=f\"../input/aptos2019-blindness-detection/train_images/78937523f7a8.png\" \nimage = load_ben_color(path,sigmaX=10)\n\nheight, width = IMG_SIZE, IMG_SIZE\nprint(height, width)\n\nSCALE=1\nfigsize = (width / float(dpi))/SCALE, (height / float(dpi))/SCALE\n\nfig = plt.figure(figsize=figsize)\nplt.imshow(image, cmap='gray')\ndpi = 80 #inch\n\n\npath=f\"../input/aptos2019-blindness-detection/train_images/838c87c63422.png\" \nimage = load_ben_color(path,sigmaX=10)\n\nheight, width = IMG_SIZE, IMG_SIZE\nprint(height, width)\n\nSCALE=1\nfigsize = (width / float(dpi))/SCALE, (height / float(dpi))/SCALE\n\nfig = plt.figure(figsize=figsize)\nplt.imshow(image, cmap='gray')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:29:13.363606Z","iopub.execute_input":"2023-10-07T07:29:13.364560Z","iopub.status.idle":"2023-10-07T07:29:14.589152Z","shell.execute_reply.started":"2023-10-07T07:29:13.364526Z","shell.execute_reply":"2023-10-07T07:29:14.588097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Diabetic Retinopathy Detection with Logistic Regression¶\n- Logistic regression is the basis of artificial neural networks. Therefore, I will use logistic regression.\n- Logistic regression is a binary classification algorithm.So I'm going to take the illness level 4 and level 0 ones.","metadata":{}},{"cell_type":"code","source":"train=pd.read_csv(\"../input/aptos2019-blindness-detection/train.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:31:18.485197Z","iopub.execute_input":"2023-10-07T07:31:18.485607Z","iopub.status.idle":"2023-10-07T07:31:18.495688Z","shell.execute_reply.started":"2023-10-07T07:31:18.485577Z","shell.execute_reply":"2023-10-07T07:31:18.494839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:31:22.426095Z","iopub.execute_input":"2023-10-07T07:31:22.426483Z","iopub.status.idle":"2023-10-07T07:31:22.436661Z","shell.execute_reply.started":"2023-10-07T07:31:22.426455Z","shell.execute_reply":"2023-10-07T07:31:22.435477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient = train[train[\"diagnosis\"]==4]\nhealt = train[train[\"diagnosis\"] == 0]\ntrain_df=pd.concat([patient,healt])","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:34:30.086537Z","iopub.execute_input":"2023-10-07T07:34:30.086875Z","iopub.status.idle":"2023-10-07T07:34:30.095164Z","shell.execute_reply.started":"2023-10-07T07:34:30.086851Z","shell.execute_reply":"2023-10-07T07:34:30.094429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"diagnosis\"]=[1 if i==4 else 0 for i in train_df.diagnosis]","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:35:11.368872Z","iopub.execute_input":"2023-10-07T07:35:11.369239Z","iopub.status.idle":"2023-10-07T07:35:11.375189Z","shell.execute_reply.started":"2023-10-07T07:35:11.369210Z","shell.execute_reply":"2023-10-07T07:35:11.373869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:35:28.249471Z","iopub.execute_input":"2023-10-07T07:35:28.249848Z","iopub.status.idle":"2023-10-07T07:35:28.255834Z","shell.execute_reply.started":"2023-10-07T07:35:28.249819Z","shell.execute_reply":"2023-10-07T07:35:28.254818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=train_df.drop(columns=[\"diagnosis\"])\ny=train_df.diagnosis","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:36:09.804678Z","iopub.execute_input":"2023-10-07T07:36:09.805035Z","iopub.status.idle":"2023-10-07T07:36:09.811837Z","shell.execute_reply.started":"2023-10-07T07:36:09.805010Z","shell.execute_reply":"2023-10-07T07:36:09.810769Z"},"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(x,y,test_size=0.30,random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:37:28.700732Z","iopub.execute_input":"2023-10-07T07:37:28.701081Z","iopub.status.idle":"2023-10-07T07:37:28.708212Z","shell.execute_reply.started":"2023-10-07T07:37:28.701053Z","shell.execute_reply":"2023-10-07T07:37:28.707207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dn_x = [ Image.open('../input/aptos2019-blindness-detection/train_images/'+i+'.png') for i in x_train.id_code[:5]]","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:37:57.635621Z","iopub.execute_input":"2023-10-07T07:37:57.635984Z","iopub.status.idle":"2023-10-07T07:37:57.811680Z","shell.execute_reply.started":"2023-10-07T07:37:57.635955Z","shell.execute_reply":"2023-10-07T07:37:57.810970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dn_x","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:38:11.548843Z","iopub.execute_input":"2023-10-07T07:38:11.549260Z","iopub.status.idle":"2023-10-07T07:38:11.556187Z","shell.execute_reply.started":"2023-10-07T07:38:11.549222Z","shell.execute_reply":"2023-10-07T07:38:11.555117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in dn_x:\n    plt.figure(figsize=(5,3))\n    i = cv2.resize(np.asarray(i),(64,64))\n    i = cv2.cvtColor(i,cv2.COLOR_BGR2GRAY)\n    plt.imshow(i,cmap=\"gray\")\n    plt.axis(\"off\")\n    plt.show","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:41:25.295585Z","iopub.execute_input":"2023-10-07T07:41:25.295993Z","iopub.status.idle":"2023-10-07T07:41:25.646966Z","shell.execute_reply.started":"2023-10-07T07:41:25.295958Z","shell.execute_reply":"2023-10-07T07:41:25.645452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = [cv2.resize(np.asarray(Image.open('../input/aptos2019-blindness-detection/train_images/'+i+'.png').convert(\"L\")),(64,64)) for i in x_train.id_code]","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:44:27.961493Z","iopub.execute_input":"2023-10-07T07:44:27.961832Z","iopub.status.idle":"2023-10-07T07:46:38.116309Z","shell.execute_reply.started":"2023-10-07T07:44:27.961806Z","shell.execute_reply":"2023-10-07T07:46:38.114928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = [cv2.resize(np.asarray(Image.open('../input/aptos2019-blindness-detection/train_images/'+i+'.png').convert(\"L\")),(64,64)) for i in x_test.id_code]","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:46:59.615174Z","iopub.execute_input":"2023-10-07T07:46:59.615527Z","iopub.status.idle":"2023-10-07T07:48:15.564133Z","shell.execute_reply.started":"2023-10-07T07:46:59.615502Z","shell.execute_reply":"2023-10-07T07:48:15.563203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test=np.array(x_test)\nx_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:48:30.242619Z","iopub.execute_input":"2023-10-07T07:48:30.242946Z","iopub.status.idle":"2023-10-07T07:48:30.251534Z","shell.execute_reply.started":"2023-10-07T07:48:30.242922Z","shell.execute_reply":"2023-10-07T07:48:30.250356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=np.array(x_train)\nx_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:48:30.517229Z","iopub.execute_input":"2023-10-07T07:48:30.517755Z","iopub.status.idle":"2023-10-07T07:48:30.525520Z","shell.execute_reply.started":"2023-10-07T07:48:30.517728Z","shell.execute_reply":"2023-10-07T07:48:30.524575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_flatten=x_train.reshape(x_train.shape[0],x_train.shape[1]*x_train.shape[2])\nx_test_flatten=x_test.reshape(x_test.shape[0],x_test.shape[1]*x_test.shape[2])","metadata":{"execution":{"iopub.status.busy":"2023-10-07T07:48:47.409887Z","iopub.execute_input":"2023-10-07T07:48:47.410278Z","iopub.status.idle":"2023-10-07T07:48:47.415755Z","shell.execute_reply.started":"2023-10-07T07:48:47.410248Z","shell.execute_reply":"2023-10-07T07:48:47.414526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=x_train_flatten.T\ny_train=y_train.T\nx_test=x_test_flatten.T\ny_test=y_test.T\nprint(\"x_train: \",x_train.shape)\nprint(\"x_test: \",x_test.shape)\nprint(\"y_train: \",y_train.shape)\nprint(\"y_test: \",y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T08:29:54.134664Z","iopub.execute_input":"2023-10-07T08:29:54.135069Z","iopub.status.idle":"2023-10-07T08:29:54.141809Z","shell.execute_reply.started":"2023-10-07T08:29:54.135016Z","shell.execute_reply":"2023-10-07T08:29:54.140549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train=np.array(y_train)\ny_test=np.array(y_test)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T08:30:08.008175Z","iopub.execute_input":"2023-10-07T08:30:08.008682Z","iopub.status.idle":"2023-10-07T08:30:08.013257Z","shell.execute_reply.started":"2023-10-07T08:30:08.008647Z","shell.execute_reply":"2023-10-07T08:30:08.012208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train=y_train.reshape(-1,1)\ny_test=y_test.reshape(-1,1)\nprint(\"y_train: \",y_train.shape)\nprint(\"y_test: \",y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T08:30:12.809424Z","iopub.execute_input":"2023-10-07T08:30:12.809758Z","iopub.status.idle":"2023-10-07T08:30:12.816287Z","shell.execute_reply.started":"2023-10-07T08:30:12.809723Z","shell.execute_reply":"2023-10-07T08:30:12.814937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Initialize Parameters","metadata":{}},{"cell_type":"code","source":"def initialize_weights_and_bias(dimension):\n    w = np.full((dimension, 1),0.01)\n    b=0.0 \n    return w,b","metadata":{"execution":{"iopub.status.busy":"2023-10-07T08:32:55.323229Z","iopub.execute_input":"2023-10-07T08:32:55.323601Z","iopub.status.idle":"2023-10-07T08:32:55.328802Z","shell.execute_reply.started":"2023-10-07T08:32:55.323573Z","shell.execute_reply":"2023-10-07T08:32:55.327489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sigmoid Function","metadata":{}},{"cell_type":"code","source":"def sigmoid(z):\n    y_head=1 / (1+np.exp(-z))\n    return y_head","metadata":{"execution":{"iopub.status.busy":"2023-10-07T08:33:50.213649Z","iopub.execute_input":"2023-10-07T08:33:50.213997Z","iopub.status.idle":"2023-10-07T08:33:50.219239Z","shell.execute_reply.started":"2023-10-07T08:33:50.213970Z","shell.execute_reply":"2023-10-07T08:33:50.218069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Forward Propagation","metadata":{}},{"cell_type":"code","source":"def forward_propagation(w,b,x_train,y_train):\n    z=np.dot(w.T,x_train)+b\n    y_head = sigmoid(z)\n    loss = -y_train*np.log(y_head)-(1-y_train)*np.log(1-y_head)\n    cost = (np.sum(loss)) / x_train.shape[1]\n    return cost","metadata":{"execution":{"iopub.status.busy":"2023-10-07T08:35:54.870271Z","iopub.execute_input":"2023-10-07T08:35:54.870619Z","iopub.status.idle":"2023-10-07T08:35:54.876379Z","shell.execute_reply.started":"2023-10-07T08:35:54.870594Z","shell.execute_reply":"2023-10-07T08:35:54.875632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Optimization Algorithm with Gradient Descent","metadata":{}},{"cell_type":"code","source":"def forward_backward_propagation(w,b,x_train,y_train):\n    # forward propagation\n    z=np.dot(w.T,x_train)+b\n    y_head=sigmoid(z)\n    loss=-y_train*np.log(y_head)-(1-y_train)*np.log(1-y_head)\n    cost=(np.sum(loss))/x_train.shape[1]\n    #backward propagation\n    derivative_weight = (np.dot(x_train,((y_head-y_train).T)))/x_train.shape[1]\n    derivative_bias=np.sum(y_head-y_train)/x_train.shape[1]\n    gradients={\"derivative_weight\":derivative_weight,\"derivative_bias\":derivative_bias}\n    return cost,gradients\n    \n    return cost,gradients","metadata":{"execution":{"iopub.status.busy":"2023-10-07T08:42:40.337027Z","iopub.execute_input":"2023-10-07T08:42:40.337458Z","iopub.status.idle":"2023-10-07T08:42:40.344978Z","shell.execute_reply.started":"2023-10-07T08:42:40.337427Z","shell.execute_reply":"2023-10-07T08:42:40.344119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#updating learning parameters\ndef update(w,b,x_train,y_train,learning_rate,number_of_iteration):\n    cost_list = []\n    cost_list2 = []\n    index = []\n    for i in range(number_of_iteration):\n        cost,gradients=forward_backward_propagation(w,b,x_train,y_train)\n        cost_list.append(cost)\n        w=w-learning_rate*gradients[\"derivative_weight\"]\n        b=b-learning_rate*gradients[\"derivative_bias\"]\n        parameters = {\"weight\":w, \"bias\":b}\n        return parameters,gradients, cost_list","metadata":{"execution":{"iopub.status.busy":"2023-10-07T08:47:21.576522Z","iopub.execute_input":"2023-10-07T08:47:21.576893Z","iopub.status.idle":"2023-10-07T08:47:21.583944Z","shell.execute_reply.started":"2023-10-07T08:47:21.576866Z","shell.execute_reply":"2023-10-07T08:47:21.582898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#prediction\ndef predict(w,b,x_test):\n    z=sigmoid(np.dot(w.T,x_test)+b)\n    Y_prediction=np.zeros((1,x_test.shape[1]))\n    for i in range(z.shape[1]):\n        if z[0,i]<=0.5:\n            Y_prediction[0,i]=0\n        else:\n            Y_prediction[0,i]=1\n    return Y_prediction","metadata":{"execution":{"iopub.status.busy":"2023-10-07T08:55:14.398093Z","iopub.execute_input":"2023-10-07T08:55:14.398488Z","iopub.status.idle":"2023-10-07T08:55:14.404594Z","shell.execute_reply.started":"2023-10-07T08:55:14.398459Z","shell.execute_reply":"2023-10-07T08:55:14.403443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Logistic regression initialize\ndef logistic_regression(x_train,y_train,x_test,y_test,learning_rate,num_iterations):\n    dimension=x_train.shape[0]\n    w,b=initialize_weights_and_bias(dimension)\n    parameters,gradients,cost_list=update(w,b,x_train,y_train,learning_rate,num_iterations)\n    y_prediction_test=predict(parameters[\"weight\"],parameters[\"bias\"],x_test)\n    y_prediction_train=predict(parameters[\"weight\"],parameters[\"bias\"],x_train)\n    print(\"train accuracy: {} %\".format(100 - np.mean(np.abs(y_prediction_train - y_train)) * 100))\n    print(\"test accuracy: {} %\".format(100 - np.mean(np.abs(y_prediction_test - y_test)) * 100))\nlogistic_regression(x_train, y_train, x_test, y_test,learning_rate = 0.01, num_iterations = 50)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T08:55:14.636715Z","iopub.execute_input":"2023-10-07T08:55:14.637322Z","iopub.status.idle":"2023-10-07T08:55:15.712834Z","shell.execute_reply.started":"2023-10-07T08:55:14.637283Z","shell.execute_reply":"2023-10-07T08:55:15.711804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Scikit Learn with Logistic Regression","metadata":{}},{"cell_type":"code","source":"from sklearn import linear_model\nlogreg = linear_model.LogisticRegression(random_state=42,max_iter = 150)\nprint(\"test accuracy: {}\".format(logreg.fit(x_train.T,y_train).score(x_test.T, y_test)))","metadata":{"execution":{"iopub.status.busy":"2023-10-07T08:58:57.924817Z","iopub.execute_input":"2023-10-07T08:58:57.925207Z","iopub.status.idle":"2023-10-07T08:58:59.050479Z","shell.execute_reply.started":"2023-10-07T08:58:57.925178Z","shell.execute_reply":"2023-10-07T08:58:59.049005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}