{"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":"\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom keras.layers import Dense, Input, Dropout, GlobalAveragePooling2D, Flatten, Conv2D, BatchNormalization, Activation, MaxPooling2D\nfrom keras.models import Model, Sequential\nfrom keras.optimizers import Adam\nimport tensorflow as tf\n# number of possible label values\n\nfrom keras.applications import VGG16\n# instantiate a distribution strategy\n\nfrom keras.callbacks import ReduceLROnPlateau\nfrom tensorflow.keras import layers, models, Model, optimizers\nfrom keras.callbacks import ModelCheckpoint\nimport keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.utils import to_categorical\nfrom keras.preprocessing import image\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom keras.layers import Dense, Input, Dropout, GlobalAveragePooling2D, Flatten, Conv2D, BatchNormalization, Activation, MaxPooling2D\nfrom keras.models import Model, Sequential\nfrom keras.optimizers import Adam\nimport tensorflow as tf\nfrom keras.applications.resnet50 import ResNet50\nfrom keras.models import Model\nimport keras\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, InputLayer,GlobalAveragePooling2D\nfrom keras.models import Sequential\nimport tensorflow_addons as tfa\nfrom keras import optimizers\nfrom keras.optimizers import SGD\nimport os\n# number of possible label values\nfrom keras_preprocessing.image import ImageDataGenerator\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-26T17:15:13.577023Z","iopub.execute_input":"2021-05-26T17:15:13.577529Z","iopub.status.idle":"2021-05-26T17:15:20.189249Z","shell.execute_reply.started":"2021-05-26T17:15:13.577418Z","shell.execute_reply":"2021-05-26T17:15:20.188462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrains = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')   \ntrains.head()   \n\ntrain = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')    # reading the csv file\ntrain.head()   \n","metadata":{"execution":{"iopub.status.busy":"2021-05-26T17:15:32.885554Z","iopub.execute_input":"2021-05-26T17:15:32.886046Z","iopub.status.idle":"2021-05-26T17:15:32.981419Z","shell.execute_reply.started":"2021-05-26T17:15:32.886014Z","shell.execute_reply":"2021-05-26T17:15:32.980725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"labels\"]=train[\"labels\"].apply(lambda x:x.split(\" \"))","metadata":{"execution":{"iopub.status.busy":"2021-05-26T17:15:34.702015Z","iopub.execute_input":"2021-05-26T17:15:34.702519Z","iopub.status.idle":"2021-05-26T17:15:34.723419Z","shell.execute_reply.started":"2021-05-26T17:15:34.702485Z","shell.execute_reply":"2021-05-26T17:15:34.722517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen=ImageDataGenerator(\n    preprocessing_function = tf.keras.applications.efficientnet.preprocess_input,\n          \n    )\ntest_generator=test_datagen.flow_from_dataframe(\ndataframe=trains,\ndirectory=\"../input/plant-pathology-2021-fgvc8/test_images/\",\nx_col=\"image\",\ny_col=None,\nbatch_size=1,\nseed=42,\nshuffle=False,\nclass_mode=None,\nsave_to_dir=None,\nclasses=['complex','frog_eye_leaf_spot','healthy','powdery_mildew','rust','scab'],\n\ntarget_size=(512,512))\n#test_generator.next()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T17:15:35.468377Z","iopub.execute_input":"2021-05-26T17:15:35.468879Z","iopub.status.idle":"2021-05-26T17:15:35.489643Z","shell.execute_reply.started":"2021-05-26T17:15:35.468845Z","shell.execute_reply":"2021-05-26T17:15:35.488724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"datagen=ImageDataGenerator(rescale=1./255.,validation_split=0.10)\ntrain_gen=test_datagen.flow_from_dataframe(\ndataframe=train,\ndirectory=\"../input/ppchangedimagesize/kaggle/working/train_images_224*224\",\nx_col=\"image\",\ny_col=\"labels\",\n     color_mode=\"rgb\",\nsubset=\"training\",\nbatch_size=16,\nclasses=['complex','frog_eye_leaf_spot','healthy','powdery_mildew','rust','scab'],\nclass_mode=\"categorical\",\ntarget_size=(512,512))","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:45:54.821967Z","iopub.execute_input":"2021-05-26T15:45:54.822295Z","iopub.status.idle":"2021-05-26T15:46:42.301835Z","shell.execute_reply.started":"2021-05-26T15:45:54.822265Z","shell.execute_reply":"2021-05-26T15:46:42.300902Z"}}},{"cell_type":"code","source":"model_finetuned = keras.models.load_model('../input/effpttt/weights00000014.h5')\n#model_finetuned = keras.models.load_model('../input/models-for-pp/resnet_new_model_50.h5')","metadata":{"execution":{"iopub.status.busy":"2021-05-26T17:48:22.832615Z","iopub.execute_input":"2021-05-26T17:48:22.832997Z","iopub.status.idle":"2021-05-26T17:48:31.618674Z","shell.execute_reply.started":"2021-05-26T17:48:22.832958Z","shell.execute_reply":"2021-05-26T17:48:31.617692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions=model_finetuned.predict(test_generator)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-26T17:48:31.620497Z","iopub.execute_input":"2021-05-26T17:48:31.620857Z","iopub.status.idle":"2021-05-26T17:48:36.610994Z","shell.execute_reply.started":"2021-05-26T17:48:31.620825Z","shell.execute_reply":"2021-05-26T17:48:36.609933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2021-05-26T17:48:36.615236Z","iopub.execute_input":"2021-05-26T17:48:36.615610Z","iopub.status.idle":"2021-05-26T17:48:36.621798Z","shell.execute_reply.started":"2021-05-26T17:48:36.615574Z","shell.execute_reply":"2021-05-26T17:48:36.620811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_idx=[]\nfor pred in predictions:\n    pred=list(pred)\n    temp=[]\n    for i in range(0,len(pred)):\n        if pred[i]>0.3:\n            temp.append(i)\n           \n    if (temp!=[]):\n        class_idx.append(temp)\n    else:\n        temp.append(2)\n        class_idx.append(temp)\nprint(class_idx)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T17:51:06.407266Z","iopub.execute_input":"2021-05-26T17:51:06.407820Z","iopub.status.idle":"2021-05-26T17:51:06.414344Z","shell.execute_reply.started":"2021-05-26T17:51:06.407778Z","shell.execute_reply":"2021-05-26T17:51:06.413660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#class_dict = train_gen.class_indices","metadata":{"execution":{"iopub.status.busy":"2021-05-26T16:58:56.444445Z","iopub.execute_input":"2021-05-26T16:58:56.444790Z","iopub.status.idle":"2021-05-26T16:58:56.450976Z","shell.execute_reply.started":"2021-05-26T16:58:56.444754Z","shell.execute_reply":"2021-05-26T16:58:56.450166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_dict={'complex': 0,\n 'frog_eye_leaf_spot': 1,\n 'healthy': 2,\n 'powdery_mildew': 3,\n 'rust': 4,\n 'scab': 5}","metadata":{"execution":{"iopub.status.busy":"2021-05-26T16:58:56.452170Z","iopub.execute_input":"2021-05-26T16:58:56.452577Z","iopub.status.idle":"2021-05-26T16:58:56.459148Z","shell.execute_reply.started":"2021-05-26T16:58:56.452531Z","shell.execute_reply":"2021-05-26T16:58:56.458357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getkey(val):\n    for key,value in class_dict.items():\n        if (val==value):\n            return key\nprint(class_dict)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T16:58:56.461528Z","iopub.execute_input":"2021-05-26T16:58:56.461920Z","iopub.status.idle":"2021-05-26T16:58:56.470419Z","shell.execute_reply.started":"2021-05-26T16:58:56.461887Z","shell.execute_reply":"2021-05-26T16:58:56.469616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_pred=[]\nfor img in class_idx:\n    img_pred=[]\n    for i in img:\n        print(i)\n        img_pred.append(getkey(i))\n    submission_pred.append( ' '.join(img_pred))","metadata":{"execution":{"iopub.status.busy":"2021-05-26T16:58:56.471413Z","iopub.execute_input":"2021-05-26T16:58:56.473420Z","iopub.status.idle":"2021-05-26T16:58:56.480255Z","shell.execute_reply.started":"2021-05-26T16:58:56.473383Z","shell.execute_reply":"2021-05-26T16:58:56.479208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = trains[['image']]\nresults['labels']=submission_pred\n","metadata":{"execution":{"iopub.status.busy":"2021-05-26T16:58:56.481735Z","iopub.execute_input":"2021-05-26T16:58:56.482147Z","iopub.status.idle":"2021-05-26T16:58:56.490069Z","shell.execute_reply.started":"2021-05-26T16:58:56.482067Z","shell.execute_reply":"2021-05-26T16:58:56.489221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T16:58:56.491443Z","iopub.execute_input":"2021-05-26T16:58:56.491852Z","iopub.status.idle":"2021-05-26T16:58:56.700494Z","shell.execute_reply.started":"2021-05-26T16:58:56.491817Z","shell.execute_reply":"2021-05-26T16:58:56.699649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}