{"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":"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.preprocessing.image import ImageDataGenerator\nfrom keras.utils import np_utils\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom keras.models import Sequential\nfrom keras.applications import resnet","metadata":{"execution":{"iopub.status.busy":"2022-12-08T03:03:24.056421Z","iopub.execute_input":"2022-12-08T03:03:24.057535Z","iopub.status.idle":"2022-12-08T03:03:31.753211Z","shell.execute_reply.started":"2022-12-08T03:03:24.057431Z","shell.execute_reply":"2022-12-08T03:03:31.751982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = []\nlabels = []","metadata":{"execution":{"iopub.status.busy":"2022-12-08T03:03:31.755593Z","iopub.execute_input":"2022-12-08T03:03:31.756513Z","iopub.status.idle":"2022-12-08T03:03:31.761955Z","shell.execute_reply.started":"2022-12-08T03:03:31.756464Z","shell.execute_reply":"2022-12-08T03:03:31.760822Z"},"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\n    dataset.append(img_array)\n    labels.append(str(label))","metadata":{"execution":{"iopub.status.busy":"2022-12-08T03:03:31.763636Z","iopub.execute_input":"2022-12-08T03:03:31.764332Z","iopub.status.idle":"2022-12-08T03:03:31.791396Z","shell.execute_reply.started":"2022-12-08T03:03:31.764289Z","shell.execute_reply":"2022-12-08T03:03:31.790415Z"},"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-08T03:03:31.793682Z","iopub.execute_input":"2022-12-08T03:03:31.794215Z","iopub.status.idle":"2022-12-08T03:03:31.835198Z","shell.execute_reply.started":"2022-12-08T03:03:31.794180Z","shell.execute_reply":"2022-12-08T03:03:31.834431Z"},"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-08T03:03:31.836596Z","iopub.execute_input":"2022-12-08T03:03:31.837167Z","iopub.status.idle":"2022-12-08T03:03:31.845420Z","shell.execute_reply.started":"2022-12-08T03:03:31.837134Z","shell.execute_reply":"2022-12-08T03:03:31.844038Z"},"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-08T03:03:31.847166Z","iopub.execute_input":"2022-12-08T03:03:31.847520Z","iopub.status.idle":"2022-12-08T03:10:43.332298Z","shell.execute_reply.started":"2022-12-08T03:03:31.847487Z","shell.execute_reply":"2022-12-08T03:10:43.330760Z"},"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-08T03:10:43.334338Z","iopub.execute_input":"2022-12-08T03:10:43.334785Z","iopub.status.idle":"2022-12-08T03:10:44.250933Z","shell.execute_reply.started":"2022-12-08T03:10:43.334742Z","shell.execute_reply":"2022-12-08T03:10:44.249664Z"},"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=42)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T03:10:44.252460Z","iopub.execute_input":"2022-12-08T03:10:44.252894Z","iopub.status.idle":"2022-12-08T03:10:46.115388Z","shell.execute_reply.started":"2022-12-08T03:10:44.252859Z","shell.execute_reply":"2022-12-08T03:10:46.114069Z"},"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-08T03:10:46.117416Z","iopub.execute_input":"2022-12-08T03:10:46.118107Z","iopub.status.idle":"2022-12-08T03:10:46.125380Z","shell.execute_reply.started":"2022-12-08T03:10:46.118067Z","shell.execute_reply":"2022-12-08T03:10:46.124134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom keras.applications.resnet import ResNet50\nmodel = resnet.ResNet50(include_top=True,\n    weights=None,\n    input_tensor=None,\n    input_shape=None,\n    pooling=max,\n    classes=5)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T03:10:46.129142Z","iopub.execute_input":"2022-12-08T03:10:46.130377Z","iopub.status.idle":"2022-12-08T03:10:47.604799Z","shell.execute_reply.started":"2022-12-08T03:10:46.130326Z","shell.execute_reply":"2022-12-08T03:10:47.603500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss ='categorical_crossentropy',\n              optimizer = 'adam',\n              metrics =['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-12-08T03:10:47.606129Z","iopub.execute_input":"2022-12-08T03:10:47.606469Z","iopub.status.idle":"2022-12-08T03:10:47.627060Z","shell.execute_reply.started":"2022-12-08T03:10:47.606437Z","shell.execute_reply":"2022-12-08T03:10:47.625943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=model.fit(x_train,y_train,batch_size=64,epochs=30,validation_data=(x_test,y_test))","metadata":{"execution":{"iopub.status.busy":"2022-12-08T03:10:47.628416Z","iopub.execute_input":"2022-12-08T03:10:47.628791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(x_test)","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]}]}