{"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":"\nimport 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\n\n","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:15:00.217900Z","iopub.execute_input":"2022-12-27T06:15:00.219131Z","iopub.status.idle":"2022-12-27T06:15:07.480719Z","shell.execute_reply.started":"2022-12-27T06:15:00.218987Z","shell.execute_reply":"2022-12-27T06:15:07.479425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = []\nlabels = []","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:15:07.483134Z","iopub.execute_input":"2022-12-27T06:15:07.484201Z","iopub.status.idle":"2022-12-27T06:15:07.490226Z","shell.execute_reply.started":"2022-12-27T06:15:07.484153Z","shell.execute_reply":"2022-12-27T06:15:07.488849Z"},"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,(224,224))\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":"2022-12-27T06:15:07.491441Z","iopub.execute_input":"2022-12-27T06:15:07.491743Z","iopub.status.idle":"2022-12-27T06:15:07.503761Z","shell.execute_reply.started":"2022-12-27T06:15:07.491715Z","shell.execute_reply":"2022-12-27T06:15:07.502719Z"},"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-12-27T06:15:07.506434Z","iopub.execute_input":"2022-12-27T06:15:07.507197Z","iopub.status.idle":"2022-12-27T06:15:07.542174Z","shell.execute_reply.started":"2022-12-27T06:15:07.507149Z","shell.execute_reply":"2022-12-27T06:15:07.540775Z"},"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-12-27T06:15:07.543547Z","iopub.execute_input":"2022-12-27T06:15:07.543969Z","iopub.status.idle":"2022-12-27T06:15:07.551669Z","shell.execute_reply.started":"2022-12-27T06:15:07.543935Z","shell.execute_reply":"2022-12-27T06:15:07.550903Z"},"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-12-27T06:15:07.552867Z","iopub.execute_input":"2022-12-27T06:15:07.553770Z","iopub.status.idle":"2022-12-27T06:23:34.755089Z","shell.execute_reply.started":"2022-12-27T06:15:07.553723Z","shell.execute_reply":"2022-12-27T06:23:34.753489Z"},"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-12-27T06:23:34.757098Z","iopub.execute_input":"2022-12-27T06:23:34.757485Z","iopub.status.idle":"2022-12-27T06:23:35.684135Z","shell.execute_reply.started":"2022-12-27T06:23:34.757445Z","shell.execute_reply":"2022-12-27T06:23:35.682945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom 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":"2022-12-27T06:23:35.685564Z","iopub.execute_input":"2022-12-27T06:23:35.686042Z","iopub.status.idle":"2022-12-27T06:23:37.516656Z","shell.execute_reply.started":"2022-12-27T06:23:35.685994Z","shell.execute_reply":"2022-12-27T06:23:37.515772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:23:37.518200Z","iopub.execute_input":"2022-12-27T06:23:37.519371Z","iopub.status.idle":"2022-12-27T06:23:37.526715Z","shell.execute_reply.started":"2022-12-27T06:23:37.519326Z","shell.execute_reply":"2022-12-27T06:23:37.525540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:23:37.530527Z","iopub.execute_input":"2022-12-27T06:23:37.531664Z","iopub.status.idle":"2022-12-27T06:23:37.541196Z","shell.execute_reply.started":"2022-12-27T06:23:37.531623Z","shell.execute_reply":"2022-12-27T06:23:37.540006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:23:37.543270Z","iopub.execute_input":"2022-12-27T06:23:37.544053Z","iopub.status.idle":"2022-12-27T06:23:37.554280Z","shell.execute_reply.started":"2022-12-27T06:23:37.543995Z","shell.execute_reply":"2022-12-27T06:23:37.552291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:23:37.555855Z","iopub.execute_input":"2022-12-27T06:23:37.556244Z","iopub.status.idle":"2022-12-27T06:23:37.565037Z","shell.execute_reply.started":"2022-12-27T06:23:37.556209Z","shell.execute_reply":"2022-12-27T06:23:37.563749Z"},"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-12-27T06:23:37.566525Z","iopub.execute_input":"2022-12-27T06:23:37.569123Z","iopub.status.idle":"2022-12-27T06:23:37.574862Z","shell.execute_reply.started":"2022-12-27T06:23:37.569084Z","shell.execute_reply":"2022-12-27T06:23:37.573985Z"},"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=(224,224,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(5,activation=\"softmax\"))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:23:37.576335Z","iopub.execute_input":"2022-12-27T06:23:37.577374Z","iopub.status.idle":"2022-12-27T06:23:37.969984Z","shell.execute_reply.started":"2022-12-27T06:23:37.577342Z","shell.execute_reply":"2022-12-27T06:23:37.968706Z"},"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=64,epochs=30,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:23:37.971593Z","iopub.execute_input":"2022-12-27T06:23:37.972816Z","iopub.status.idle":"2022-12-27T06:37:36.887061Z","shell.execute_reply.started":"2022-12-27T06:23:37.972766Z","shell.execute_reply":"2022-12-27T06:37:36.885939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:37:36.888598Z","iopub.execute_input":"2022-12-27T06:37:36.889049Z","iopub.status.idle":"2022-12-27T06:37:38.894814Z","shell.execute_reply.started":"2022-12-27T06:37:36.889014Z","shell.execute_reply":"2022-12-27T06:37:38.893814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:37:38.896142Z","iopub.execute_input":"2022-12-27T06:37:38.896678Z","iopub.status.idle":"2022-12-27T06:37:41.027816Z","shell.execute_reply.started":"2022-12-27T06:37:38.896647Z","shell.execute_reply":"2022-12-27T06:37:41.026520Z"},"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)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:37:41.029192Z","iopub.execute_input":"2022-12-27T06:37:41.029553Z","iopub.status.idle":"2022-12-27T06:37:41.038388Z","shell.execute_reply.started":"2022-12-27T06:37:41.029519Z","shell.execute_reply":"2022-12-27T06:37:41.036992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nreport = classification_report(y_test.argmax(axis=1), pred.argmax(axis=1))\nprint(report)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:37:41.039978Z","iopub.execute_input":"2022-12-27T06:37:41.040356Z","iopub.status.idle":"2022-12-27T06:37:41.055490Z","shell.execute_reply.started":"2022-12-27T06:37:41.040321Z","shell.execute_reply":"2022-12-27T06:37:41.054457Z"},"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":"2022-12-27T06:37:41.056963Z","iopub.execute_input":"2022-12-27T06:37:41.057334Z","iopub.status.idle":"2022-12-27T06:37:41.066949Z","shell.execute_reply.started":"2022-12-27T06:37:41.057302Z","shell.execute_reply":"2022-12-27T06:37:41.065674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.axis('off')\nplt.imshow(x_train[0])","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:37:41.068493Z","iopub.execute_input":"2022-12-27T06:37:41.069628Z","iopub.status.idle":"2022-12-27T06:37:41.304331Z","shell.execute_reply.started":"2022-12-27T06:37:41.069580Z","shell.execute_reply":"2022-12-27T06:37:41.303191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[0]","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:37:41.305425Z","iopub.execute_input":"2022-12-27T06:37:41.305743Z","iopub.status.idle":"2022-12-27T06:37:41.312754Z","shell.execute_reply.started":"2022-12-27T06:37:41.305713Z","shell.execute_reply":"2022-12-27T06:37:41.311909Z"},"trusted":true},"execution_count":null,"outputs":[]}]}