{"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 REQUIRED LIBRARIEs\nfrom time import time\nimport numpy as np\nimport pandas as pd\nimport os\nfrom re import search\nimport shutil\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom tqdm import tqdm\nimport cv2\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.callbacks import ModelCheckpoint,EarlyStopping\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense,Activation,Flatten, Conv2D, MaxPooling2D,Dropout\nfrom  tensorflow.keras.applications import InceptionV3","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:42:46.505758Z","iopub.execute_input":"2022-02-06T09:42:46.506029Z","iopub.status.idle":"2022-02-06T09:42:51.979545Z","shell.execute_reply.started":"2022-02-06T09:42:46.505998Z","shell.execute_reply":"2022-02-06T09:42:51.978762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tensorflow_addons\nimport tensorflow_addons as tfa","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:42:51.981069Z","iopub.execute_input":"2022-02-06T09:42:51.981337Z","iopub.status.idle":"2022-02-06T09:43:20.452245Z","shell.execute_reply.started":"2022-02-06T09:42:51.981301Z","shell.execute_reply":"2022-02-06T09:43:20.451499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preparation","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')\nsubmission.head()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-06T09:43:20.453538Z","iopub.execute_input":"2022-02-06T09:43:20.453796Z","iopub.status.idle":"2022-02-06T09:43:20.485711Z","shell.execute_reply.started":"2022-02-06T09:43:20.453757Z","shell.execute_reply":"2022-02-06T09:43:20.485031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(\n    rescale = 1./255\n)\nINPUT_SIZE = (124,124,3)\ntest_generator =  test_datagen.flow_from_dataframe(\n    submission,\n    directory=\"../input/plant-pathology-2021-fgvc8/test_images\",\n    x_col='image',\n    y_col=None,\n    class_mode=None,\n    target_size=INPUT_SIZE[:2]\n) ","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:43:20.48778Z","iopub.execute_input":"2022-02-06T09:43:20.488028Z","iopub.status.idle":"2022-02-06T09:43:20.506748Z","shell.execute_reply.started":"2022-02-06T09:43:20.487994Z","shell.execute_reply":"2022-02-06T09:43:20.50609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model construction","metadata":{}},{"cell_type":"code","source":"#initialise base model\nIMG_SHAPE = (124, 124, 3)\n\nbase_model=  InceptionV3(\n    weights='../input/keras-pretrained-models/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5',\n    input_shape= IMG_SHAPE,\n    include_top=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:43:20.508908Z","iopub.execute_input":"2022-02-06T09:43:20.509607Z","iopub.status.idle":"2022-02-06T09:43:26.871174Z","shell.execute_reply.started":"2022-02-06T09:43:20.509569Z","shell.execute_reply":"2022-02-06T09:43:26.870387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.trainable = True\n\nfine_tune_at = 200\n\n# Freeze all the layers before the 'fine_tune_at' layer\nfor layer in base_model.layers[:fine_tune_at]:\n  layer.trainable =  False","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:43:26.872528Z","iopub.execute_input":"2022-02-06T09:43:26.872774Z","iopub.status.idle":"2022-02-06T09:43:26.893588Z","shell.execute_reply.started":"2022-02-06T09:43:26.872741Z","shell.execute_reply":"2022-02-06T09:43:26.892924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel = tf.keras.Sequential()\nmodel.add(base_model)\nmodel.add(tf.keras.layers.GlobalAveragePooling2D())\nmodel.add(tf.keras.layers.Dense(320, activation='relu'))\nmodel.add(tf.keras.layers.Dropout(0.2))\nmodel.add(tf.keras.layers.Dense(12, activation='sigmoid'))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:43:26.895045Z","iopub.execute_input":"2022-02-06T09:43:26.895315Z","iopub.status.idle":"2022-02-06T09:43:27.629586Z","shell.execute_reply.started":"2022-02-06T09:43:26.895279Z","shell.execute_reply":"2022-02-06T09:43:27.628793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the Model\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001/10),\n    loss='categorical_crossentropy',\n    metrics = [tfa.metrics.F1Score(num_classes = 12,average = \"macro\",name = \"f1_score\")])","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:43:27.630868Z","iopub.execute_input":"2022-02-06T09:43:27.63112Z","iopub.status.idle":"2022-02-06T09:43:27.654298Z","shell.execute_reply.started":"2022-02-06T09:43:27.631073Z","shell.execute_reply":"2022-02-06T09:43:27.653645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import a model","metadata":{}},{"cell_type":"code","source":"model.load_weights(\"../input/best-model/Best_model_inceptionV3 (1).h5\")","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:48:56.734001Z","iopub.execute_input":"2022-02-06T09:48:56.734395Z","iopub.status.idle":"2022-02-06T09:48:57.08441Z","shell.execute_reply.started":"2022-02-06T09:48:56.734352Z","shell.execute_reply":"2022-02-06T09:48:57.083651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictions","metadata":{}},{"cell_type":"code","source":"preds = model.predict(test_generator)\nprint(preds)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:49:20.415891Z","iopub.execute_input":"2022-02-06T09:49:20.416176Z","iopub.status.idle":"2022-02-06T09:49:28.430314Z","shell.execute_reply.started":"2022-02-06T09:49:20.416145Z","shell.execute_reply":"2022-02-06T09:49:28.429569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Classes={'complex': 0,\n 'frog_eye_leaf_spot': 1,\n 'frog_eye_leaf_spot complex': 2,\n 'healthy': 3,\n 'powdery_mildew': 4,\n 'powdery_mildew complex': 5,\n 'rust': 6,\n 'rust complex': 7,\n 'rust frog_eye_leaf_spot': 8,\n 'scab': 9,\n 'scab frog_eye_leaf_spot': 10,\n 'scab frog_eye_leaf_spot complex': 11}","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:49:36.150423Z","iopub.execute_input":"2022-02-06T09:49:36.151004Z","iopub.status.idle":"2022-02-06T09:49:36.156169Z","shell.execute_reply.started":"2022-02-06T09:49:36.150962Z","shell.execute_reply":"2022-02-06T09:49:36.155223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_key(val):\n    for key, value in Classes.items():\n        if val == value:\n            return key","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:49:43.785892Z","iopub.execute_input":"2022-02-06T09:49:43.786573Z","iopub.status.idle":"2022-02-06T09:49:43.791098Z","shell.execute_reply.started":"2022-02-06T09:49:43.786532Z","shell.execute_reply":"2022-02-06T09:49:43.790127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_preds_to_labels(preds):\n    pred_lists = []\n    for pred in preds:\n        pred_list = []\n        health = (pred>=0.4)\n        \n        # get healthy\n        if health.sum()==0:\n            label = 'healthy'\n            pred_list.append(label)\n            \n        elif pred[2]>=0.5:\n            label = 'healthy'\n            pred_list.append(label)\n            \n        # get eles label\n        else:\n            for j, sub in enumerate(pred):\n                if sub>=0.28:\n                    label = get_key(j)\n                    pred_list.append(label)\n                               \n        pred_lists.append(' '.join(pred_list))\n    return pred_lists","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:49:47.507985Z","iopub.execute_input":"2022-02-06T09:49:47.508761Z","iopub.status.idle":"2022-02-06T09:49:47.515704Z","shell.execute_reply.started":"2022-02-06T09:49:47.508722Z","shell.execute_reply":"2022-02-06T09:49:47.514969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_list = get_preds_to_labels(preds)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:49:56.350007Z","iopub.execute_input":"2022-02-06T09:49:56.350788Z","iopub.status.idle":"2022-02-06T09:49:56.356373Z","shell.execute_reply.started":"2022-02-06T09:49:56.350741Z","shell.execute_reply":"2022-02-06T09:49:56.355652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = preds_list\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:50:00.079004Z","iopub.execute_input":"2022-02-06T09:50:00.079598Z","iopub.status.idle":"2022-02-06T09:50:00.089123Z","shell.execute_reply.started":"2022-02-06T09:50:00.079562Z","shell.execute_reply":"2022-02-06T09:50:00.088441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SUBMISSION OF FILE","metadata":{}},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T09:50:07.258186Z","iopub.execute_input":"2022-02-06T09:50:07.25883Z","iopub.status.idle":"2022-02-06T09:50:07.268296Z","shell.execute_reply.started":"2022-02-06T09:50:07.258789Z","shell.execute_reply":"2022-02-06T09:50:07.267426Z"},"trusted":true},"execution_count":null,"outputs":[]}]}