{"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":"markdown","source":"**Link to Training Notebook** [ https://www.kaggle.com/virajkadam/plant-pathology-apple-leaf-diseases ]","metadata":{}},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os \nimport gc \nfrom PIL import Image\n\n\nfrom tensorflow.keras.models import load_model\nfrom keras.preprocessing.image import load_img,img_to_array\n\nfrom tensorflow_addons.metrics import F1Score","metadata":{"execution":{"iopub.status.busy":"2021-05-26T13:42:46.664004Z","iopub.execute_input":"2021-05-26T13:42:46.664523Z","iopub.status.idle":"2021-05-26T13:42:53.788982Z","shell.execute_reply.started":"2021-05-26T13:42:46.664436Z","shell.execute_reply":"2021-05-26T13:42:53.788194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**test Directory**","metadata":{}},{"cell_type":"code","source":"test_dir='../input/plant-pathology-2021-fgvc8/test_images'\n\nimg_size=(256,256)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T13:42:53.790202Z","iopub.execute_input":"2021-05-26T13:42:53.790575Z","iopub.status.idle":"2021-05-26T13:42:53.794733Z","shell.execute_reply.started":"2021-05-26T13:42:53.790543Z","shell.execute_reply":"2021-05-26T13:42:53.793466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub=pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')\nsample_sub","metadata":{"execution":{"iopub.status.busy":"2021-05-26T13:42:53.796273Z","iopub.execute_input":"2021-05-26T13:42:53.796603Z","iopub.status.idle":"2021-05-26T13:42:53.848149Z","shell.execute_reply.started":"2021-05-26T13:42:53.796569Z","shell.execute_reply":"2021-05-26T13:42:53.847133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**metric**","metadata":{}},{"cell_type":"code","source":"f1_score=F1Score(num_classes=6,average='macro',name='f1_score')","metadata":{"execution":{"iopub.status.busy":"2021-05-26T13:42:53.850030Z","iopub.execute_input":"2021-05-26T13:42:53.850335Z","iopub.status.idle":"2021-05-26T13:42:53.902761Z","shell.execute_reply.started":"2021-05-26T13:42:53.850305Z","shell.execute_reply":"2021-05-26T13:42:53.901565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Load Image**","metadata":{}},{"cell_type":"code","source":"def load_images(test_path,image):\n    '''load image from given path'''\n    img=load_img(test_path +'/' + image)\n    img=img.resize(img_size)\n    img=img_to_array(img)\n    img=np.expand_dims(img,axis=0)\n    img=img/255\n    \n    return img","metadata":{"execution":{"iopub.status.busy":"2021-05-26T13:42:53.903812Z","iopub.execute_input":"2021-05-26T13:42:53.904046Z","iopub.status.idle":"2021-05-26T13:42:53.910084Z","shell.execute_reply.started":"2021-05-26T13:42:53.904022Z","shell.execute_reply":"2021-05-26T13:42:53.908731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_classes=['complex', 'frog_eye_leaf_spot', 'healthy', 'powdery_mildew', 'rust', 'scab']","metadata":{"execution":{"iopub.status.busy":"2021-05-26T13:42:53.911178Z","iopub.execute_input":"2021-05-26T13:42:53.911405Z","iopub.status.idle":"2021-05-26T13:42:53.925462Z","shell.execute_reply.started":"2021-05-26T13:42:53.911382Z","shell.execute_reply":"2021-05-26T13:42:53.924186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading pre-trained models (on folds)","metadata":{}},{"cell_type":"code","source":"# loading pretrained models\nmodel1=load_model('../input/plant-pathology-apple-leaf-diseases/model_0.h5')\nmodel2=load_model('../input/plant-pathology-apple-leaf-diseases/model_1.h5')\nmodel3=load_model('../input/plant-pathology-apple-leaf-diseases/model_2.h5')","metadata":{"execution":{"iopub.status.busy":"2021-05-26T13:42:53.926936Z","iopub.execute_input":"2021-05-26T13:42:53.927286Z","iopub.status.idle":"2021-05-26T13:43:22.924656Z","shell.execute_reply.started":"2021-05-26T13:42:53.927261Z","shell.execute_reply":"2021-05-26T13:43:22.923725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_label(prediction_prob,thresh=0.3):\n    '''get label for a class that satisfies given threshold'''\n    \n    prediction_prob=prediction_prob[0]\n    prediction_prob=list(prediction_prob)\n    labels=[label_classes[x] for x,prob in enumerate(prediction_prob) if prob >=thresh]\n    \n    labels=' '.join(labels)\n    \n    return labels","metadata":{"execution":{"iopub.status.busy":"2021-05-26T13:43:22.926872Z","iopub.execute_input":"2021-05-26T13:43:22.927240Z","iopub.status.idle":"2021-05-26T13:43:22.931690Z","shell.execute_reply.started":"2021-05-26T13:43:22.927208Z","shell.execute_reply":"2021-05-26T13:43:22.930864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(test_path,threshold):\n    '''predict on test set, using given threshold'''\n    \n    images,labels=[],[]\n    \n    for image in os.listdir(test_path):\n        \n        #loading images         \n        img=load_images(test_path,image)\n        \n        #mean prediction from 3 models\n        pred_prob=(1/3* model1.predict(img)) + (1/3* model2.predict(img)) + (1/3* model3.predict(img))\n        \n        #getting prediction\n        preds=get_label(pred_prob,thresh=threshold)\n        \n        #saving image name and predicted label\n        images.append(image)\n        labels.append(preds)\n        \n    return images,labels\n        \n","metadata":{"execution":{"iopub.status.busy":"2021-05-26T13:43:22.937887Z","iopub.execute_input":"2021-05-26T13:43:22.938125Z","iopub.status.idle":"2021-05-26T13:43:22.950107Z","shell.execute_reply.started":"2021-05-26T13:43:22.938101Z","shell.execute_reply":"2021-05-26T13:43:22.949157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predicting using above func\nimage_ids,labels=predict(test_dir,threshold=0.2)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T13:44:58.723252Z","iopub.execute_input":"2021-05-26T13:44:58.723583Z","iopub.status.idle":"2021-05-26T13:45:01.595242Z","shell.execute_reply.started":"2021-05-26T13:44:58.723555Z","shell.execute_reply":"2021-05-26T13:45:01.593589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission file:\nsubmission_file=pd.DataFrame({'image':image_ids,'labels':labels})\n\n#saving submissions\nsubmission_file.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T13:44:48.673206Z","iopub.execute_input":"2021-05-26T13:44:48.673415Z","iopub.status.idle":"2021-05-26T13:44:48.683253Z","shell.execute_reply.started":"2021-05-26T13:44:48.673394Z","shell.execute_reply":"2021-05-26T13:44:48.681695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_file","metadata":{"execution":{"iopub.status.busy":"2021-05-26T13:44:48.685308Z","iopub.execute_input":"2021-05-26T13:44:48.685701Z","iopub.status.idle":"2021-05-26T13:44:48.703948Z","shell.execute_reply.started":"2021-05-26T13:44:48.685661Z","shell.execute_reply":"2021-05-26T13:44:48.702359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}