{"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 os\nimport matplotlib.pyplot as plt\nimport random\n\nimport tensorflow as tf\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.python.keras.preprocessing.image import ImageDataGenerator\n\nfrom sklearn.metrics import classification_report, log_loss, accuracy_score\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm","metadata":{"papermill":{"duration":7.647909,"end_time":"2021-05-21T15:24:37.500217","exception":false,"start_time":"2021-05-21T15:24:29.852308","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T13:23:46.764167Z","iopub.execute_input":"2021-05-24T13:23:46.764497Z","iopub.status.idle":"2021-05-24T13:23:46.770165Z","shell.execute_reply.started":"2021-05-24T13:23:46.764468Z","shell.execute_reply":"2021-05-24T13:23:46.769152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '../input/aptos2019-blindness-detection/train_images'\ntest_dir = '../input/aptos2019-blindness-detection/test_images'","metadata":{"papermill":{"duration":0.029589,"end_time":"2021-05-21T15:24:37.552405","exception":false,"start_time":"2021-05-21T15:24:37.522816","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T13:23:46.771984Z","iopub.execute_input":"2021-05-24T13:23:46.772347Z","iopub.status.idle":"2021-05-24T13:23:46.781176Z","shell.execute_reply.started":"2021-05-24T13:23:46.772311Z","shell.execute_reply":"2021-05-24T13:23:46.780294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv=pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\ntest_csv=pd.read_csv('../input/aptos2019-blindness-detection/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-24T13:23:46.784604Z","iopub.execute_input":"2021-05-24T13:23:46.784907Z","iopub.status.idle":"2021-05-24T13:23:46.802642Z","shell.execute_reply.started":"2021-05-24T13:23:46.784882Z","shell.execute_reply":"2021-05-24T13:23:46.801663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=[]\ntrainlabel=[]\nfor im in tqdm(os.listdir(train_dir)):\n    image=load_img(os.path.join(train_dir,im), grayscale=False, color_mode='rgb', target_size=(60,60))\n    image=img_to_array(image)\n    image=image/255.0\n    train+=[image]\n    trainlabel+=[train_csv[train_csv['id_code']==im[0:-4]]['diagnosis'].iat[0]]","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","papermill":{"duration":27.570617,"end_time":"2021-05-21T15:25:05.269161","exception":false,"start_time":"2021-05-21T15:24:37.698544","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T13:23:46.805090Z","iopub.execute_input":"2021-05-24T13:23:46.805443Z","iopub.status.idle":"2021-05-24T13:30:18.539980Z","shell.execute_reply.started":"2021-05-24T13:23:46.805406Z","shell.execute_reply":"2021-05-24T13:30:18.538603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=[]\nfor im in tqdm(os.listdir(test_dir)):\n    image=load_img(os.path.join(test_dir,im), grayscale=False, color_mode='rgb', target_size=(60,60))\n    image=img_to_array(image)\n    image=image/255.0\n    test+=[image]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T13:30:18.541891Z","iopub.execute_input":"2021-05-24T13:30:18.542325Z","iopub.status.idle":"2021-05-24T13:31:23.530546Z","shell.execute_reply.started":"2021-05-24T13:30:18.542270Z","shell.execute_reply":"2021-05-24T13:31:23.529600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,axs = plt.subplots(3,3,figsize=(14,14))\nfor i in range(9):\n    r=i//3\n    c=i%3\n    ax=axs[r][c].imshow(train[i])\n    ax=axs[r][c].set_title('Diagnosis: '+str(trainlabel[i]))    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-24T13:31:23.532485Z","iopub.execute_input":"2021-05-24T13:31:23.532865Z","iopub.status.idle":"2021-05-24T13:31:24.459630Z","shell.execute_reply.started":"2021-05-24T13:31:23.532826Z","shell.execute_reply":"2021-05-24T13:31:24.458649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=np.array(train)\ntrainlabel=np.array(trainlabel)\ntest=np.array(test)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T13:31:24.461360Z","iopub.execute_input":"2021-05-24T13:31:24.461724Z","iopub.status.idle":"2021-05-24T13:31:24.583469Z","shell.execute_reply.started":"2021-05-24T13:31:24.461685Z","shell.execute_reply":"2021-05-24T13:31:24.582528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainlabel2=to_categorical(trainlabel)","metadata":{"papermill":{"duration":0.034057,"end_time":"2021-05-21T15:25:05.622075","exception":false,"start_time":"2021-05-21T15:25:05.588018","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T13:31:24.585076Z","iopub.execute_input":"2021-05-24T13:31:24.585448Z","iopub.status.idle":"2021-05-24T13:31:24.590121Z","shell.execute_reply.started":"2021-05-24T13:31:24.585408Z","shell.execute_reply":"2021-05-24T13:31:24.589179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainx,testx,trainy,testy=train_test_split(train,trainlabel2,test_size=0.2,random_state=44)","metadata":{"papermill":{"duration":0.069249,"end_time":"2021-05-21T15:25:05.713285","exception":false,"start_time":"2021-05-21T15:25:05.644036","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T13:31:24.591397Z","iopub.execute_input":"2021-05-24T13:31:24.591789Z","iopub.status.idle":"2021-05-24T13:31:24.701358Z","shell.execute_reply.started":"2021-05-24T13:31:24.591752Z","shell.execute_reply":"2021-05-24T13:31:24.700447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(horizontal_flip=True,vertical_flip=True,rotation_range=20,zoom_range=0.2,\n                        width_shift_range=0.2,height_shift_range=0.2,shear_range=0.1,fill_mode=\"nearest\")","metadata":{"papermill":{"duration":0.031436,"end_time":"2021-05-21T15:25:05.82455","exception":false,"start_time":"2021-05-21T15:25:05.793114","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T13:31:24.702865Z","iopub.execute_input":"2021-05-24T13:31:24.703222Z","iopub.status.idle":"2021-05-24T13:31:24.708302Z","shell.execute_reply.started":"2021-05-24T13:31:24.703185Z","shell.execute_reply":"2021-05-24T13:31:24.707345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"densenet_weights_path = '../input/densenet201-imagenet-pretrained-weights/densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5'","metadata":{"execution":{"iopub.status.busy":"2021-05-24T13:31:24.711458Z","iopub.execute_input":"2021-05-24T13:31:24.711844Z","iopub.status.idle":"2021-05-24T13:31:24.719136Z","shell.execute_reply.started":"2021-05-24T13:31:24.711806Z","shell.execute_reply":"2021-05-24T13:31:24.718387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrained_model3 = tf.keras.applications.DenseNet201(input_shape=(60,60,3),include_top=False,weights=densenet_weights_path,pooling='avg')\npretrained_model3.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-05-24T13:31:24.720853Z","iopub.execute_input":"2021-05-24T13:31:24.721251Z","iopub.status.idle":"2021-05-24T13:31:30.950034Z","shell.execute_reply.started":"2021-05-24T13:31:24.721213Z","shell.execute_reply":"2021-05-24T13:31:30.948843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs3 = pretrained_model3.input\nx3 = tf.keras.layers.Dense(128, activation='relu')(pretrained_model3.output)\noutputs3 = tf.keras.layers.Dense(5, activation='softmax')(x3)\nmodel = tf.keras.Model(inputs=inputs3, outputs=outputs3)","metadata":{"papermill":{"duration":0.108919,"end_time":"2021-05-21T15:25:15.707006","exception":false,"start_time":"2021-05-21T15:25:15.598087","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T13:31:30.951648Z","iopub.execute_input":"2021-05-24T13:31:30.951978Z","iopub.status.idle":"2021-05-24T13:31:31.050154Z","shell.execute_reply.started":"2021-05-24T13:31:30.951943Z","shell.execute_reply":"2021-05-24T13:31:31.048717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"papermill":{"duration":0.062633,"end_time":"2021-05-21T15:25:15.796628","exception":false,"start_time":"2021-05-21T15:25:15.733995","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T13:31:31.051948Z","iopub.execute_input":"2021-05-24T13:31:31.052404Z","iopub.status.idle":"2021-05-24T13:31:31.093158Z","shell.execute_reply.started":"2021-05-24T13:31:31.052354Z","shell.execute_reply":"2021-05-24T13:31:31.091989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(trainx.shape)\nprint(trainy.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T13:31:31.098129Z","iopub.execute_input":"2021-05-24T13:31:31.100550Z","iopub.status.idle":"2021-05-24T13:31:31.110883Z","shell.execute_reply.started":"2021-05-24T13:31:31.100488Z","shell.execute_reply":"2021-05-24T13:31:31.109879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(testx.shape)\nprint(testy.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T13:31:31.115542Z","iopub.execute_input":"2021-05-24T13:31:31.117881Z","iopub.status.idle":"2021-05-24T13:31:31.126332Z","shell.execute_reply.started":"2021-05-24T13:31:31.117807Z","shell.execute_reply":"2021-05-24T13:31:31.125073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"his=model.fit(datagen.flow(trainx,trainy,batch_size=32),validation_data=(testx,testy),epochs=50)","metadata":{"papermill":{"duration":534.362111,"end_time":"2021-05-21T15:34:10.186041","exception":false,"start_time":"2021-05-21T15:25:15.82393","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T13:31:31.131396Z","iopub.execute_input":"2021-05-24T13:31:31.132217Z","iopub.status.idle":"2021-05-24T13:35:59.961613Z","shell.execute_reply.started":"2021-05-24T13:31:31.132177Z","shell.execute_reply":"2021-05-24T13:35:59.960723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_acc = his.history['accuracy']\nvalue_acc = his.history['val_accuracy']\nget_loss = his.history['loss']\nvalidation_loss = his.history['val_loss']\n\nepochs = range(len(get_acc))\nplt.plot(epochs, get_acc, 'r', label='Accuracy of Training data')\nplt.plot(epochs, value_acc, 'b', label='Accuracy of Validation data')\nplt.title('Training vs validation accuracy')\nplt.legend(loc=0)\nplt.figure()\nplt.show()","metadata":{"papermill":{"duration":0.716241,"end_time":"2021-05-21T15:34:19.187418","exception":false,"start_time":"2021-05-21T15:34:18.471177","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T13:35:59.963233Z","iopub.execute_input":"2021-05-24T13:35:59.963636Z","iopub.status.idle":"2021-05-24T13:36:00.134580Z","shell.execute_reply.started":"2021-05-24T13:35:59.963588Z","shell.execute_reply":"2021-05-24T13:36:00.133675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = range(len(get_loss))\nplt.plot(epochs, get_loss, 'r', label='Loss of Training data')\nplt.plot(epochs, validation_loss, 'b', label='Loss of Validation data')\nplt.title('Training vs validation loss')\nplt.legend(loc=0)\nplt.figure()\nplt.show()","metadata":{"papermill":{"duration":0.760146,"end_time":"2021-05-21T15:34:20.450507","exception":false,"start_time":"2021-05-21T15:34:19.690361","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T13:36:00.135857Z","iopub.execute_input":"2021-05-24T13:36:00.136224Z","iopub.status.idle":"2021-05-24T13:36:00.292747Z","shell.execute_reply.started":"2021-05-24T13:36:00.136188Z","shell.execute_reply":"2021-05-24T13:36:00.291813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred2=model.predict(test)\nprint(test.shape)\nprint(pred2.shape)\n\nPRED=[]\nfor item in pred2:\n    value2=np.argmax(item)      \n    PRED+=[value2]","metadata":{"papermill":{"duration":4.670931,"end_time":"2021-05-21T15:34:28.822575","exception":false,"start_time":"2021-05-21T15:34:24.151644","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T13:36:00.294115Z","iopub.execute_input":"2021-05-24T13:36:00.294458Z","iopub.status.idle":"2021-05-24T13:36:04.985925Z","shell.execute_reply.started":"2021-05-24T13:36:00.294421Z","shell.execute_reply":"2021-05-24T13:36:04.985090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample=pd.read_csv('../input/aptos2019-blindness-detection/sample_submission.csv')\nsample","metadata":{"execution":{"iopub.status.busy":"2021-05-24T13:36:04.988593Z","iopub.execute_input":"2021-05-24T13:36:04.988868Z","iopub.status.idle":"2021-05-24T13:36:05.027348Z","shell.execute_reply.started":"2021-05-24T13:36:04.988841Z","shell.execute_reply":"2021-05-24T13:36:05.026581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit=sample\nsubmit['diagnosis']=PRED\nsubmit.to_csv('submission.csv',index=False)\nsubmit","metadata":{"execution":{"iopub.status.busy":"2021-05-24T13:36:05.028619Z","iopub.execute_input":"2021-05-24T13:36:05.028974Z","iopub.status.idle":"2021-05-24T13:36:05.830432Z","shell.execute_reply.started":"2021-05-24T13:36:05.028935Z","shell.execute_reply":"2021-05-24T13:36:05.829608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.511451,"end_time":"2021-05-21T15:34:31.952055","exception":false,"start_time":"2021-05-21T15:34:31.440604","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}