{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importing all required libraries\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nimport os\nfrom zipfile import ZipFile\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\n\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\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-23T03:25:16.113182Z","iopub.execute_input":"2022-10-23T03:25:16.113861Z","iopub.status.idle":"2022-10-23T03:25:22.797985Z","shell.execute_reply.started":"2022-10-23T03:25:16.113761Z","shell.execute_reply":"2022-10-23T03:25:22.796596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(\"../input\"))","metadata":{"execution":{"iopub.status.busy":"2022-10-23T03:26:07.192993Z","iopub.execute_input":"2022-10-23T03:26:07.193653Z","iopub.status.idle":"2022-10-23T03:26:07.199808Z","shell.execute_reply.started":"2022-10-23T03:26:07.193614Z","shell.execute_reply":"2022-10-23T03:26:07.198714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = []\nlabels = []","metadata":{"execution":{"iopub.status.busy":"2022-10-23T03:26:43.001458Z","iopub.execute_input":"2022-10-23T03:26:43.001956Z","iopub.status.idle":"2022-10-23T03:26:43.008167Z","shell.execute_reply.started":"2022-10-23T03:26:43.001920Z","shell.execute_reply":"2022-10-23T03:26:43.007213Z"},"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\n    dataset.append(img_array)\n    labels.append(str(label))","metadata":{"execution":{"iopub.status.busy":"2022-10-23T03:27:00.504822Z","iopub.execute_input":"2022-10-23T03:27:00.505244Z","iopub.status.idle":"2022-10-23T03:27:00.512276Z","shell.execute_reply.started":"2022-10-23T03:27:00.505210Z","shell.execute_reply":"2022-10-23T03:27:00.510714Z"},"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":"2022-10-23T03:27:29.873642Z","iopub.execute_input":"2022-10-23T03:27:29.874228Z","iopub.status.idle":"2022-10-23T03:27:29.926994Z","shell.execute_reply.started":"2022-10-23T03:27:29.874176Z","shell.execute_reply":"2022-10-23T03:27:29.925572Z"},"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":"2022-10-23T03:28:25.283582Z","iopub.execute_input":"2022-10-23T03:28:25.284447Z","iopub.status.idle":"2022-10-23T03:28:25.291483Z","shell.execute_reply.started":"2022-10-23T03:28:25.284401Z","shell.execute_reply":"2022-10-23T03:28:25.289327Z"},"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":"2022-10-23T03:28:55.198651Z","iopub.execute_input":"2022-10-23T03:28:55.199257Z","iopub.status.idle":"2022-10-23T03:36:29.810565Z","shell.execute_reply.started":"2022-10-23T03:28:55.199196Z","shell.execute_reply":"2022-10-23T03:36:29.808858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = np.array(dataset)\nlabel_arr = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2022-10-23T03:38:12.600072Z","iopub.execute_input":"2022-10-23T03:38:12.600567Z","iopub.status.idle":"2022-10-23T03:38:12.671703Z","shell.execute_reply.started":"2022-10-23T03:38:12.600530Z","shell.execute_reply":"2022-10-23T03:38:12.670510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Splitting the dataset \n\nfrom sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test = train_test_split(images,label_arr,test_size=0.20,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-10-23T03:38:42.467100Z","iopub.execute_input":"2022-10-23T03:38:42.467903Z","iopub.status.idle":"2022-10-23T03:38:43.128211Z","shell.execute_reply.started":"2022-10-23T03:38:42.467856Z","shell.execute_reply":"2022-10-23T03:38:43.126521Z"},"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":"2022-10-23T03:39:17.689553Z","iopub.execute_input":"2022-10-23T03:39:17.690636Z","iopub.status.idle":"2022-10-23T03:39:17.698034Z","shell.execute_reply.started":"2022-10-23T03:39:17.690595Z","shell.execute_reply":"2022-10-23T03:39:17.696730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#CNN MODEL\n\nmodel=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()\n","metadata":{"execution":{"iopub.status.busy":"2022-10-23T03:39:46.512679Z","iopub.execute_input":"2022-10-23T03:39:46.513090Z","iopub.status.idle":"2022-10-23T03:39:46.730086Z","shell.execute_reply.started":"2022-10-23T03:39:46.513056Z","shell.execute_reply":"2022-10-23T03:39:46.728509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nhist = model.fit(x_train,y_train,batch_size=64,epochs=10,verbose=1,validation_data=(x_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2022-10-23T03:41:00.446230Z","iopub.execute_input":"2022-10-23T03:41:00.446680Z","iopub.status.idle":"2022-10-23T03:41:22.027089Z","shell.execute_reply.started":"2022-10-23T03:41:00.446646Z","shell.execute_reply":"2022-10-23T03:41:22.025690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#prediction \npred = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-10-23T03:42:07.799329Z","iopub.execute_input":"2022-10-23T03:42:07.799721Z","iopub.status.idle":"2022-10-23T03:42:08.047419Z","shell.execute_reply.started":"2022-10-23T03:42:07.799689Z","shell.execute_reply":"2022-10-23T03:42:08.045774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#printing the accuracy score\nfrom 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)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-23T03:42:42.298210Z","iopub.execute_input":"2022-10-23T03:42:42.298810Z","iopub.status.idle":"2022-10-23T03:42:42.309170Z","shell.execute_reply.started":"2022-10-23T03:42:42.298759Z","shell.execute_reply":"2022-10-23T03:42:42.307231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#printing the report - precision, recall, f1-score, accuracy\n\nreport = classification_report(y_test.argmax(axis=1), pred.argmax(axis=1))\nprint(report)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-23T03:43:15.079535Z","iopub.execute_input":"2022-10-23T03:43:15.079971Z","iopub.status.idle":"2022-10-23T03:43:15.096998Z","shell.execute_reply.started":"2022-10-23T03:43:15.079936Z","shell.execute_reply":"2022-10-23T03:43:15.095355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#confusion matrix\nconMat = confusion_matrix(y_test.argmax(axis=1),pred.argmax(axis=1))\nprint(conMat)","metadata":{"execution":{"iopub.status.busy":"2022-10-23T03:44:02.036759Z","iopub.execute_input":"2022-10-23T03:44:02.037218Z","iopub.status.idle":"2022-10-23T03:44:02.048015Z","shell.execute_reply.started":"2022-10-23T03:44:02.037163Z","shell.execute_reply":"2022-10-23T03:44:02.045958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_train = hist.history['loss']\nloss_val = hist.history['val_loss']\nepochs = range(1,11)\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()\n","metadata":{"execution":{"iopub.status.busy":"2022-10-23T03:44:39.153924Z","iopub.execute_input":"2022-10-23T03:44:39.154483Z","iopub.status.idle":"2022-10-23T03:44:39.392342Z","shell.execute_reply.started":"2022-10-23T03:44:39.154443Z","shell.execute_reply":"2022-10-23T03:44:39.390968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nloss_train = hist.history['accuracy']\nloss_val = hist.history['val_accuracy']\nepochs = range(1,11)\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()\n","metadata":{"execution":{"iopub.status.busy":"2022-10-23T03:45:14.425455Z","iopub.execute_input":"2022-10-23T03:45:14.426104Z","iopub.status.idle":"2022-10-23T03:45:14.683870Z","shell.execute_reply.started":"2022-10-23T03:45:14.426052Z","shell.execute_reply":"2022-10-23T03:45:14.682059Z"},"trusted":true},"execution_count":null,"outputs":[]}]}