{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Importing Libraries."},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport tensorflow as tf \nfrom tensorflow import keras \nfrom keras.preprocessing.image import load_img,img_to_array,smart_resize\nimport matplotlib.pyplot as plt\nimport cv2\nimport pandas as pd\nimport json\nimport numpy as np \n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Loading Pretrained model.\n**For the un-initiated, the model that you saved in the training kernel(if you commmited and saved the kernel) can be accessed by clicking the 'Add data' option in the upper right corner and then accesing the 'notebook output files' option. Otherwise you can also choose to download the saved model from the output directory of training kernel.**"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#loading pretarined model.\nmodel1=keras.models.load_model('../input/notebook841c84bbfb/IncepResNetV2.h5')\nmodel2=keras.models.load_model('../input/notebook841c84bbfb/EfficientNetB_V2.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dir='../input/cassava-leaf-disease-classification/train_images'\ntest_dir='../input/cassava-leaf-disease-classification/test_images'\ntrain=pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\nsample_sub=pd.read_csv('../input/cassava-leaf-disease-classification/sample_submission.csv')\n\nsample_sub","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Testing the trained model on randomly selected examples from the training set."},{"metadata":{"trusted":true},"cell_type":"code","source":"def sample_df(sample_size=100):\n    df= train.sample(sample_size)\n    df=df.reset_index()\n    return df\n\ndfs=sample_df(sample_size=50)\n\npreds=[]\ny_true=dfs['label']\nfor im_id in dfs.image_id: \n    img=load_img(train_dir + '/' + im_id)\n    img=img_to_array(img)\n    img=smart_resize(img,(512,512))\n    img=np.expand_dims(img,axis=0)\n    img=img/255\n    pred=np.argmax((0.5*model1.predict(img)) + (0.5 * model2.predict(img)))\n    preds.append(pred)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_test=pd.DataFrame({'Prediction':preds, 'Actual':y_true})\nsample_test.head(30)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**The model is doing pretty well with the data it has probably seen.Lets see what score it gets on the test set.**"},{"metadata":{},"cell_type":"markdown","source":"# Predictions."},{"metadata":{"trusted":true},"cell_type":"code","source":"#Prediction\n\npredictions=[]\nfor img_id in sample_sub.image_id:\n    img=load_img(test_dir + '/' + img_id)   #loading image\n    img=img_to_array(img)                   #image to array\n    img=smart_resize(img,(512,512))         #resizing image (size used in training model)\n    img=np.expand_dims(img,axis=0)\n    img=img/255                                             \n    #predicting:\n    lab=np.argmax((model1.predict(img) * 0.5) + (model2.predict(img)*0.5))                                \n    predictions.append(lab)\n\nsubmission=pd.DataFrame({'image_id':sample_sub.image_id , 'label':predictions})\nprint(submission)\nsubmission.to_csv('submission.csv',index=False)    ","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}