{"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":"## Import thư viện","metadata":{}},{"cell_type":"code","source":"!pip install pyngrok","metadata":{"execution":{"iopub.status.busy":"2021-12-03T07:58:40.641424Z","iopub.execute_input":"2021-12-03T07:58:40.642154Z","iopub.status.idle":"2021-12-03T07:58:51.969701Z","shell.execute_reply.started":"2021-12-03T07:58:40.642067Z","shell.execute_reply":"2021-12-03T07:58:51.96877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install streamlit","metadata":{"execution":{"iopub.status.busy":"2021-12-03T07:58:51.973157Z","iopub.execute_input":"2021-12-03T07:58:51.973409Z","iopub.status.idle":"2021-12-03T07:59:02.939228Z","shell.execute_reply.started":"2021-12-03T07:58:51.97338Z","shell.execute_reply":"2021-12-03T07:59:02.938429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.system('pip install /kaggle/input/kerasapplications -q')\nos.system('pip install /kaggle/input/efficientnet-keras-source-code/ -q --no-deps')","metadata":{"execution":{"iopub.status.busy":"2021-12-03T07:59:02.941611Z","iopub.execute_input":"2021-12-03T07:59:02.943677Z","iopub.status.idle":"2021-12-03T07:59:14.879177Z","shell.execute_reply.started":"2021-12-03T07:59:02.943632Z","shell.execute_reply":"2021-12-03T07:59:14.878374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile app.py\n## import\nimport streamlit as st\nimport tensorflow as tf\nimport streamlit as st\nimport cv2\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport tensorflow_hub as hub\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.optimizers import *\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.preprocessing import MultiLabelBinarizer\nimport matplotlib.pyplot as plt\n\n## import model using\nimport efficientnet.tfkeras as efn\nfrom tensorflow.keras.applications import VGG16\nfrom tensorflow.keras.applications import VGG19\n\n## config\nHEIGHT = 480\nWIDTH = 480\nCHANNELS = 3\nCLASSES = 6\ntop_dropout_rate = 0.2\nAUTO = tf.data.experimental.AUTOTUNE\n\nweights_path_b0 = \"../input/efficientnetb0/FGVC8-efn-b0.h5\"\nweights_path_vgg16 = \"../input/fgvc8vgg16/FGVC8-VGG16.h5\"\nweights_path_vgg19 = \"../input/fgvc8vgg19/FGVC8-VGG19t.h5\"\nweights_path_b7 = \"../input/b7tpu/B7-tpu.h5\"\n\n## Get models\ndef get_model_b0():\n    base_model = efn.EfficientNetB0(include_top=False, weights=None, input_shape=(HEIGHT, WIDTH, 3))\n\n    x = base_model.output\n    x = GlobalAveragePooling2D()(x)\n    x = Dropout(top_dropout_rate)(x)\n    outputs = Dense(CLASSES, activation='sigmoid')(x)\n    \n    return Model(base_model.input, outputs)\n\ndef get_model_vgg19():\n    VGG19_MODEL = tf.keras.applications.VGG19(weights=None ,include_top=False, input_shape=(HEIGHT, WIDTH, 3))\n    \n    x=VGG19_MODEL.output\n    x=GlobalAveragePooling2D()(x)\n    x=Dense(256,activation='relu')(x)\n    x=Dropout(top_dropout_rate)(x)\n    x=Dense(128,activation='relu')(x)\n    prediction=Dense(6,activation='sigmoid')(x)\n\n    model=Model(inputs=VGG19_MODEL.input, outputs=prediction)\n    \n    return model\n\ndef get_model_vgg16():\n    VGG16_MODEL = tf.keras.applications.VGG16(weights=None ,include_top=False, input_shape=(HEIGHT, WIDTH, 3))\n    \n    x=VGG16_MODEL.output\n    x=GlobalAveragePooling2D()(x)\n    x=Dense(256,activation='relu')(x)\n    x=Dropout(top_dropout_rate)(x)\n    x=Dense(128,activation='relu')(x)\n    prediction=Dense(6,activation='sigmoid')(x)\n\n    model=Model(inputs=VGG16_MODEL.input, outputs=prediction)\n    \n    return model\n\n## efn B7\ndef get_model_b7():\n    model = tf.keras.models.Sequential(name='EfficientNetB7')\n    \n    model.add(efn.EfficientNetB7(\n        include_top=False,\n        input_shape=(768, 768, 3),\n        weights=None,\n        pooling='avg'))\n    \n    model.add(tf.keras.layers.Dense(CLASSES, \n        kernel_initializer=tf.keras.initializers.RandomUniform(seed=32),\n        bias_initializer=tf.keras.initializers.Zeros(), name='dense_top'))\n    model.add(tf.keras.layers.Activation('sigmoid', dtype='float32'))\n    \n    return model\n\n\n## Load models\n@st.cache(allow_output_mutation=True)\ndef load_model_b0():\n    ## efn_b0\n    model_b0 = get_model_b0()\n    model_b0.load_weights(weights_path_b0)\n    \n    return model_b0\n\ndef load_model_vgg16():\n    ## vgg16\n    model_vgg16 = get_model_vgg16()\n    model_vgg16.load_weights(weights_path_vgg16)\n    \n    return model_vgg16\n\ndef load_model_vgg19():\n    ## vgg19\n    model_vgg19 = get_model_vgg19()\n    model_vgg19.load_weights(weights_path_vgg19)\n    \n    return model_vgg19\n\ndef load_model_b7():\n    model_b7 = get_model_b7()\n    model_b7.load_weights(weights_path_b7)\n    \n    return model_b7\n\nwith st.spinner('Model is being loaded..'):\n    model_b0 = load_model_b0()\n    model_vgg16 = load_model_vgg16()\n    model_vgg19 = load_model_vgg19()\n#     model_b7 = load_model_b7()\n    \n## Header\nst.write(\"\"\"\n         # Plant Pathology 2021 - FGVC8\n         \"\"\"\n         )\n\nfile = st.file_uploader(\"Please upload an brain scan file\", type=[\"jpg\", \"png\"])\nimport cv2\nfrom PIL import Image, ImageOps\nimport numpy as np\nst.set_option('deprecation.showfileUploaderEncoding', False)\n\n## Augmentation\ndef data_full_augment(image):\n    \n    p_spatial = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    p_rotate = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    p_crop = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    p_pixel_1 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    p_pixel_2 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    p_pixel_3 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    \n    flag = False\n\n    # Pixel-level transforms\n    if p_pixel_1 >= .4:\n        image = tf.image.random_saturation(image, lower=.7, upper=1.3)\n        flag = True\n    if p_pixel_2 >= .4:\n        image = tf.image.random_contrast(image, lower=.8, upper=1.2)\n        flag = True\n    if p_pixel_3 >= .4:\n        image = tf.image.random_brightness(image, max_delta=.1)\n        flag = True\n\n\n    # Flips\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_flip_up_down(image)\n    if p_spatial > .75:\n        image = tf.image.transpose(image)\n        flag = True\n        \n    # Rotates\n    if p_rotate > .75:\n        image = tf.image.rot90(image, k=3) # rotate 270º\n        flag = True\n    elif p_rotate > .5:\n        image = tf.image.rot90(image, k=2) # rotate 180º\n        flag = True\n    elif p_rotate > .25:\n        image = tf.image.rot90(image, k=1) # rotate 90\n        flag = True\n        \n    # Crops\n    if p_crop > .7:\n        if p_crop > .9:\n            image = tf.image.central_crop(image, central_fraction=.7)\n        elif p_crop > .8:\n            image = tf.image.central_crop(image, central_fraction=.8)\n        else:\n            image = tf.image.central_crop(image, central_fraction=.9)\n        flag = True\n    elif p_crop > .4:\n        HEIGHT1 = image.shape[0]\n        WIDTH1 = image.shape[1]\n        crop_size_h = tf.random.uniform([], int(HEIGHT1*.8), HEIGHT1, dtype=tf.float32)\n        crop_size_w = tf.random.uniform([], int(WIDTH1*.8), WIDTH1, dtype=tf.float32)\n        image = tf.image.random_crop(image, size=[crop_size_h, crop_size_w, 3])\n        flag = True\n    \n    return image\n\ndef process(img):\n    img = data_full_augment(img)\n    img = tf.cast(img, tf.float32) / 255.0\n    img = np.array(img)\n    return cv2.resize(img , (480, 480)).reshape(-1, 480, 480, 3)\n\ndef extract(preds, thres):\n    preds = np.array(preds)\n    preds = (preds>thres)\n    n_label = ['complex', 'frog_eye_leaf_spot', 'healthy', 'powdery_mildew', 'rust', 'scab']\n    temp = []\n    for i, k in enumerate(n_label):\n        if preds[i]:\n            temp.append(k)\n    if len(temp) > 1 and \"healthy\" in temp:\n        temp.remove(\"healthy\")\n    elif len(temp) == 0:\n        temp.append('healthy')\n    \n    return \" \".join(temp)\n\n## import and predict\ndef import_and_predict(image_data, model_b0, model_vgg16, model_vgg19):\n        image = np.asarray(image_data)\n        image = process(image)\n        \n        result = {}\n        result['models'] = ['EfficientNet B0', 'VGG16', 'VGG19']\n        \n        ##predict efn b0\n        pred_b0 = model_b0.predict(image)[0]\n        output_b0 = extract(pred_b0, 0.3)\n        \n        ##predict vgg16\n        pred_vgg16 = model_vgg16.predict(image)[0]\n        output_vgg16 = extract(pred_vgg16, 0.27)\n        \n        ##predict vgg19\n        pred_vgg19 = model_vgg19.predict(image)[0]\n        output_vgg19 = extract(pred_vgg19, 0.27)\n        \n        ##predict efn b7\n#         pred_b7 = model_b7.predict(image)[0]\n#         output_b7 = extract(pred_b7, 0.35)\n        \n        result['result'] = [output_b0, output_vgg16, output_vgg19]\n        \n        df_result = pd.DataFrame(data=result)\n        \n        return df_result\n    \n\nif file is None:\n    st.text(\"Please upload an image file\")\nelse:\n    image = Image.open(file)\n    st.image(image, use_column_width=True)\n    df_result = import_and_predict(image, model_b0, model_vgg16, model_vgg19, model_b7)\n    \n    st.write(\"\"\"\n         ## Kết quả:\n         \"\"\"\n         )\n    \n    st.dataframe(data=df_result)","metadata":{"execution":{"iopub.status.busy":"2021-12-03T08:24:11.743777Z","iopub.execute_input":"2021-12-03T08:24:11.744034Z","iopub.status.idle":"2021-12-03T08:24:11.754173Z","shell.execute_reply.started":"2021-12-03T08:24:11.744004Z","shell.execute_reply":"2021-12-03T08:24:11.753202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ngrok authtoken 21igYrFjznjBBTZbCPStCsjKsMi_NBeR4LsfyZakDoNZKzfg","metadata":{"execution":{"iopub.status.busy":"2021-12-03T08:02:55.577982Z","iopub.execute_input":"2021-12-03T08:02:55.578622Z","iopub.status.idle":"2021-12-03T08:02:57.772279Z","shell.execute_reply.started":"2021-12-03T08:02:55.578588Z","shell.execute_reply":"2021-12-03T08:02:57.771545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pyngrok import ngrok\n \npublic_url = ngrok.connect('8501')\npublic_url","metadata":{"execution":{"iopub.status.busy":"2021-12-03T08:24:14.880038Z","iopub.execute_input":"2021-12-03T08:24:14.880288Z","iopub.status.idle":"2021-12-03T08:24:15.419273Z","shell.execute_reply.started":"2021-12-03T08:24:14.880262Z","shell.execute_reply":"2021-12-03T08:24:15.418434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!streamlit run app.py >/dev/null","metadata":{"execution":{"iopub.status.busy":"2021-12-03T08:24:15.421094Z","iopub.execute_input":"2021-12-03T08:24:15.421446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}