{"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\n#for 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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Diabetic Retinopathy\nDiabetic retinopathy (DR), also known as diabetic eye disease, is a medical condition in which damage occurs to the retina due to diabetes mellitus. It is a leading cause of blindness. Diabetic retinopathy affects up to 80 percent of those who have had diabetes for 20 years or more. Diabetic retinopathy often has no early warning signs. Retinal (fundus) photography with manual interpretation is a widely accepted screening tool for diabetic retinopathy, with performance that can exceed that of in-person dilated eye examinations.\n\nAn automated tool for grading severity of diabetic retinopathy would be very useful for accerelating detection and treatment.\n\nClearly, this dataset and deep learning problem is quite important.","metadata":{}},{"cell_type":"markdown","source":"## A look at the data:\nData description from the competition:\n\nYou are provided with a large set of high-resolution retina images taken under a variety of imaging conditions. A left and right field is provided for every subject. >Images are labeled with a subject id as well as either left or right (e.g. 1_left.jpeg is the left eye of patient id 1).\n\nA clinician has rated the presence of diabetic retinopathy in each image on a scale of 0 to 4, according to the following scale:\n\n0 - No DR\n\n1 - Mild\n\n2 - Moderate\n\n3 - Severe\n\n4 - Proliferative DR\n\n","metadata":{}},{"cell_type":"code","source":"os.listdir()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T16:02:40.334765Z","iopub.execute_input":"2023-01-08T16:02:40.335138Z","iopub.status.idle":"2023-01-08T16:02:40.341341Z","shell.execute_reply.started":"2023-01-08T16:02:40.335112Z","shell.execute_reply":"2023-01-08T16:02:40.340562Z"},"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 keras.preprocessing.image import img_to_array\nfrom keras.utils import np_utils\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom keras.models import Sequential\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:05:33.576321Z","iopub.execute_input":"2023-01-08T15:05:33.576893Z","iopub.status.idle":"2023-01-08T15:05:35.187875Z","shell.execute_reply.started":"2023-01-08T15:05:33.576814Z","shell.execute_reply":"2023-01-08T15:05:35.186613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = []\nlabels = []","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:05:38.939106Z","iopub.execute_input":"2023-01-08T15:05:38.939787Z","iopub.status.idle":"2023-01-08T15:05:38.944896Z","shell.execute_reply.started":"2023-01-08T15:05:38.939752Z","shell.execute_reply":"2023-01-08T15:05:38.943882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_Images(label,path):\n    img=cv2.imread(path,cv2.IMREAD_COLOR)\n    img_res=cv2.resize(img,(50,50))\n    img_array = img_to_array(img_res)\n    img_array = img_array/255.0\n    dataset.append(img_array)\n    labels.append(str(label))","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:05:39.289847Z","iopub.execute_input":"2023-01-08T15:05:39.290191Z","iopub.status.idle":"2023-01-08T15:05:39.295977Z","shell.execute_reply.started":"2023-01-08T15:05:39.290165Z","shell.execute_reply":"2023-01-08T15:05:39.294773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_Data = pd.read_csv(\"../input/aptos2019-blindness-detection/train.csv\")\ntrain_Data.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:05:45.752262Z","iopub.execute_input":"2023-01-08T15:05:45.752615Z","iopub.status.idle":"2023-01-08T15:05:45.772228Z","shell.execute_reply.started":"2023-01-08T15:05:45.75259Z","shell.execute_reply":"2023-01-08T15:05:45.771456Z"},"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-01-08T15:05:54.001821Z","iopub.execute_input":"2023-01-08T15:05:54.002142Z","iopub.status.idle":"2023-01-08T15:05:54.006611Z","shell.execute_reply.started":"2023-01-08T15:05:54.002118Z","shell.execute_reply":"2023-01-08T15:05:54.005801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diagnosis_Data.value_counts()","metadata":{"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('../input/aptos2019-blindness-detection/train_images','{}.png'.format(id_code))\n    prepare_Images(diagnosis,path)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:06:11.716254Z","iopub.execute_input":"2023-01-08T15:06:11.716584Z","iopub.status.idle":"2023-01-08T15:12:07.134839Z","shell.execute_reply.started":"2023-01-08T15:06:11.716559Z","shell.execute_reply":"2023-01-08T15:12:07.1334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot as plt\nplt.hist(labels)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:12:07.13739Z","iopub.execute_input":"2023-01-08T15:12:07.137722Z","iopub.status.idle":"2023-01-08T15:12:07.330325Z","shell.execute_reply.started":"2023-01-08T15:12:07.137694Z","shell.execute_reply":"2023-01-08T15:12:07.329611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = np.array(dataset)\nlabel_arr = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:12:07.331324Z","iopub.execute_input":"2023-01-08T15:12:07.332332Z","iopub.status.idle":"2023-01-08T15:12:07.377631Z","shell.execute_reply.started":"2023-01-08T15:12:07.3323Z","shell.execute_reply":"2023-01-08T15:12:07.376921Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:12:07.379457Z","iopub.execute_input":"2023-01-08T15:12:07.380154Z","iopub.status.idle":"2023-01-08T15:12:08.006339Z","shell.execute_reply.started":"2023-01-08T15:12:07.380128Z","shell.execute_reply":"2023-01-08T15:12:08.005379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    featurewise_center=True,\n    featurewise_std_normalization=True,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    horizontal_flip=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T16:09:32.177917Z","iopub.execute_input":"2023-01-08T16:09:32.178299Z","iopub.status.idle":"2023-01-08T16:09:32.183456Z","shell.execute_reply.started":"2023-01-08T16:09:32.178271Z","shell.execute_reply":"2023-01-08T16:09:32.182536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen.fit(x_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:12:08.013387Z","iopub.execute_input":"2023-01-08T15:12:08.013747Z","iopub.status.idle":"2023-01-08T15:12:08.672814Z","shell.execute_reply.started":"2023-01-08T15:12:08.013696Z","shell.execute_reply":"2023-01-08T15:12:08.67193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:12:08.673894Z","iopub.execute_input":"2023-01-08T15:12:08.674198Z","iopub.status.idle":"2023-01-08T15:12:08.679142Z","shell.execute_reply.started":"2023-01-08T15:12:08.674169Z","shell.execute_reply":"2023-01-08T15:12:08.678538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:12:08.680249Z","iopub.execute_input":"2023-01-08T15:12:08.680691Z","iopub.status.idle":"2023-01-08T15:12:08.691317Z","shell.execute_reply.started":"2023-01-08T15:12:08.680658Z","shell.execute_reply":"2023-01-08T15:12:08.690621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:12:08.692654Z","iopub.execute_input":"2023-01-08T15:12:08.694202Z","iopub.status.idle":"2023-01-08T15:12:08.702937Z","shell.execute_reply.started":"2023-01-08T15:12:08.694111Z","shell.execute_reply":"2023-01-08T15:12:08.702318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:12:08.705211Z","iopub.execute_input":"2023-01-08T15:12:08.705618Z","iopub.status.idle":"2023-01-08T15:12:08.714505Z","shell.execute_reply.started":"2023-01-08T15:12:08.705594Z","shell.execute_reply":"2023-01-08T15:12:08.713704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train= np_utils.to_categorical(y_train, num_classes=5)\ny_test = np_utils.to_categorical(y_test, num_classes=5)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:12:08.715305Z","iopub.execute_input":"2023-01-08T15:12:08.715536Z","iopub.status.idle":"2023-01-08T15:12:08.725098Z","shell.execute_reply.started":"2023-01-08T15:12:08.715514Z","shell.execute_reply":"2023-01-08T15:12:08.724433Z"},"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=(50,50,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(Dropout(0.2))\nmodel.add(Flatten())\nmodel.add(Dense(500,activation=\"relu\"))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(5,activation=\"softmax\"))#5 represent output layer neurons\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:12:08.725903Z","iopub.execute_input":"2023-01-08T15:12:08.726624Z","iopub.status.idle":"2023-01-08T15:12:08.889761Z","shell.execute_reply.started":"2023-01-08T15:12:08.726599Z","shell.execute_reply":"2023-01-08T15:12:08.889128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#model.compile(loss='categorical_crossentropy',\n              optimizer='adam', metrics=['accuracy'])\n#hist = model.fit(x_train,y_train,batch_size=64,epochs=30,verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n              optimizer='adam', metrics=['accuracy'])\nhist = model.fit(datagen.flow(x_train, y_train, batch_size=64,\n         subset='training'),\n         validation_data=datagen.flow(x_train, y_train,\n         batch_size=64, subset='validation'), epochs=30)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:48:14.230456Z","iopub.execute_input":"2023-01-08T15:48:14.230899Z","iopub.status.idle":"2023-01-08T15:49:45.736306Z","shell.execute_reply.started":"2023-01-08T15:48:14.230865Z","shell.execute_reply":"2023-01-08T15:49:45.73567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#hist = model.fit(x_train,y_train,batch_size=64,epochs=30,verbose=1,validation_data=(x_test, y_test))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:55:57.206879Z","iopub.execute_input":"2023-01-08T15:55:57.207278Z","iopub.status.idle":"2023-01-08T15:55:57.387169Z","shell.execute_reply.started":"2023-01-08T15:55:57.207248Z","shell.execute_reply":"2023-01-08T15:55:57.386482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:55:58.943449Z","iopub.execute_input":"2023-01-08T15:55:58.944279Z","iopub.status.idle":"2023-01-08T15:55:59.144797Z","shell.execute_reply.started":"2023-01-08T15:55:58.944251Z","shell.execute_reply":"2023-01-08T15:55:59.143806Z"},"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)\nreport = classification_report(y_test.argmax(axis=1), pred.argmax(axis=1))\nprint(report)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:56:03.371811Z","iopub.execute_input":"2023-01-08T15:56:03.372151Z","iopub.status.idle":"2023-01-08T15:56:03.383496Z","shell.execute_reply.started":"2023-01-08T15:56:03.372125Z","shell.execute_reply":"2023-01-08T15:56:03.382362Z"},"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-01-08T15:56:08.356591Z","iopub.execute_input":"2023-01-08T15:56:08.356941Z","iopub.status.idle":"2023-01-08T15:56:08.3633Z","shell.execute_reply.started":"2023-01-08T15:56:08.356915Z","shell.execute_reply":"2023-01-08T15:56:08.362412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_train = hist.history['loss']\nloss_val = hist.history['val_loss']\nepochs = range(1,31)\nplt.plot(epochs, loss_train, 'g', label='Training loss')\nplt.plot(epochs, loss_val, 'b', label='validation loss')\nplt.title('Training and Validation loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:56:41.997824Z","iopub.execute_input":"2023-01-08T15:56:41.998163Z","iopub.status.idle":"2023-01-08T15:56:42.152629Z","shell.execute_reply.started":"2023-01-08T15:56:41.998137Z","shell.execute_reply":"2023-01-08T15:56:42.151628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_train = hist.history['accuracy']\nloss_val = hist.history['val_accuracy']\nepochs = range(1,31)\nplt.plot(epochs, loss_train, 'g', label='Training accuracy')\nplt.plot(epochs, loss_val, 'b', label='validation accuracy')\nplt.title('Training and Validation accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:56:50.829457Z","iopub.execute_input":"2023-01-08T15:56:50.829861Z","iopub.status.idle":"2023-01-08T15:56:50.958569Z","shell.execute_reply.started":"2023-01-08T15:56:50.829831Z","shell.execute_reply":"2023-01-08T15:56:50.957634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}