{"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 numpy as np\nimport pandas as pd\nimport os\nimport random, re, math\nimport tensorflow as tf, tensorflow.keras.backend as K\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras import optimizers\nfrom kaggle_datasets import KaggleDatasets\n\nprint(tf.__version__)\nprint(tf.keras.__version__)","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2021-05-26T15:59:02.387905Z","iopub.execute_input":"2021-05-26T15:59:02.388752Z","iopub.status.idle":"2021-05-26T15:59:08.689765Z","shell.execute_reply.started":"2021-05-26T15:59:02.388663Z","shell.execute_reply":"2021-05-26T15:59:08.688466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### TPU","metadata":{}},{"cell_type":"code","source":"import pathlib","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:08.694090Z","iopub.execute_input":"2021-05-26T15:59:08.694402Z","iopub.status.idle":"2021-05-26T15:59:08.700476Z","shell.execute_reply.started":"2021-05-26T15:59:08.694374Z","shell.execute_reply":"2021-05-26T15:59:08.699533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_image(filename, label=None, image_size=(512, 512)):\n    bits = tf.io.read_file(filename)\n    image = tf.image.decode_jpeg(bits, channels=3)\n    image = tf.cast(image, tf.float32) / 255.0\n    image = tf.image.resize(image, image_size)\n    if label is None:\n        return image\n    else:\n        return image, label","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:08.701822Z","iopub.execute_input":"2021-05-26T15:59:08.702363Z","iopub.status.idle":"2021-05-26T15:59:08.714983Z","shell.execute_reply.started":"2021-05-26T15:59:08.702331Z","shell.execute_reply":"2021-05-26T15:59:08.713819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 32","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:08.716461Z","iopub.execute_input":"2021-05-26T15:59:08.716887Z","iopub.status.idle":"2021-05-26T15:59:08.729927Z","shell.execute_reply.started":"2021-05-26T15:59:08.716843Z","shell.execute_reply":"2021-05-26T15:59:08.728717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source =  \"../input/plant-pathology-2021-fgvc8/test_images\"\nIMAGE_PATHS = [source + \"/\" + f for f in os.listdir(source)\n              if re.search(r'([a-zA-Z0-9\\s_\\\\.\\-\\(\\):])+(.jpg|.jpeg|.png)$', f)]\n","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:08.733795Z","iopub.execute_input":"2021-05-26T15:59:08.734273Z","iopub.status.idle":"2021-05-26T15:59:08.750319Z","shell.execute_reply.started":"2021-05-26T15:59:08.734221Z","shell.execute_reply":"2021-05-26T15:59:08.749073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_PATHS","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:08.751973Z","iopub.execute_input":"2021-05-26T15:59:08.752310Z","iopub.status.idle":"2021-05-26T15:59:08.761444Z","shell.execute_reply.started":"2021-05-26T15:59:08.752281Z","shell.execute_reply":"2021-05-26T15:59:08.760605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"AUTO = tf.data.experimental.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:08.762884Z","iopub.execute_input":"2021-05-26T15:59:08.763180Z","iopub.status.idle":"2021-05-26T15:59:08.773679Z","shell.execute_reply.started":"2021-05-26T15:59:08.763154Z","shell.execute_reply":"2021-05-26T15:59:08.772780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = (\n    tf.data.Dataset\n    .from_tensor_slices(IMAGE_PATHS)\n    .map(decode_image, num_parallel_calls=AUTO)\n    .batch(BATCH_SIZE)\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:08.774594Z","iopub.execute_input":"2021-05-26T15:59:08.774853Z","iopub.status.idle":"2021-05-26T15:59:08.946489Z","shell.execute_reply.started":"2021-05-26T15:59:08.774827Z","shell.execute_reply":"2021-05-26T15:59:08.945608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:08.948029Z","iopub.execute_input":"2021-05-26T15:59:08.948917Z","iopub.status.idle":"2021-05-26T15:59:08.953612Z","shell.execute_reply.started":"2021-05-26T15:59:08.948869Z","shell.execute_reply":"2021-05-26T15:59:08.952573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class FixedDropout(tf.keras.layers.Dropout):\n    def _get_noise_shape(self, inputs):\n        if self.noise_shape is None:\n            return self.noise_shape\n        symbolic_shape = K.shape(inputs)\n        noise_shape = [symbolic_shape[axis] if shape is None else shape\n            for axis, shape in enumerate(self.noise_shape)]\n        return tuple(noise_shape)\n\n","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:08.954610Z","iopub.execute_input":"2021-05-26T15:59:08.955019Z","iopub.status.idle":"2021-05-26T15:59:08.966504Z","shell.execute_reply.started":"2021-05-26T15:59:08.954977Z","shell.execute_reply":"2021-05-26T15:59:08.965572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model('../input/finalpp2021/5SMResNet50.h5'\n                                   ,compile=False,custom_objects = {'FixedDropout':FixedDropout})","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:08.967950Z","iopub.execute_input":"2021-05-26T15:59:08.968305Z","iopub.status.idle":"2021-05-26T15:59:12.596588Z","shell.execute_reply.started":"2021-05-26T15:59:08.968277Z","shell.execute_reply":"2021-05-26T15:59:12.595336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nprobs = model.predict(test_dataset)\ntemp_probs = probs\n","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:12.599313Z","iopub.execute_input":"2021-05-26T15:59:12.599790Z","iopub.status.idle":"2021-05-26T15:59:15.524039Z","shell.execute_reply.started":"2021-05-26T15:59:12.599739Z","shell.execute_reply":"2021-05-26T15:59:15.522935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nprobs","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:15.525310Z","iopub.execute_input":"2021-05-26T15:59:15.525608Z","iopub.status.idle":"2021-05-26T15:59:15.531762Z","shell.execute_reply.started":"2021-05-26T15:59:15.525581Z","shell.execute_reply":"2021-05-26T15:59:15.531102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name = {0: 'scab',\n        1: 'frog_eye_leaf_spot',\n        2: 'complex',\n        3: 'rust',\n        4: 'powdery_mildew',\n        6: 'healthy'}\n\nthreshold = {0: 0.35,\n             1: 0.35,\n             2: 0.35,\n             3: 0.35,\n             4: 0.35}\nthreshold2 = {0: 0.25,\n             1: 0.25,\n             2: 0.25,\n             3: 0.25,\n             4: 0.25}\ndef get_key(val):\n    for key, value in name.items():\n        if val == value:\n            return key\n \n    return \"key doesn't exist\"\n\npred_string = []\nfor line in temp_probs:\n    s = ''\n    count = 0\n    for i in range(5):\n        if line[i] > threshold[i]:\n            s = s + name[i] + ' '  \n    \n    for i in range(5):\n        if line[i] > threshold2[i]: \n            count += 1\n            \n    if count >= 2:\n        notComplex = True\n        for i in range(5):\n            if line[i] > threshold[i] and name[i] == 'complex':\n                notComplex = False\n                break\n        if  notComplex == True:\n            s = s + 'complex' + ' '\n        \n    \n    \n    if s == '': \n        s = name[6]\n    pred_string.append(s)\n    \n\n","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:15.533011Z","iopub.execute_input":"2021-05-26T15:59:15.533296Z","iopub.status.idle":"2021-05-26T15:59:15.544817Z","shell.execute_reply.started":"2021-05-26T15:59:15.533268Z","shell.execute_reply":"2021-05-26T15:59:15.543605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_string","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:15.546567Z","iopub.execute_input":"2021-05-26T15:59:15.547205Z","iopub.status.idle":"2021-05-26T15:59:15.562507Z","shell.execute_reply.started":"2021-05-26T15:59:15.547159Z","shell.execute_reply":"2021-05-26T15:59:15.561481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({\n'image': os.listdir(source),\n'labels': pred_string})\ndf.to_csv('submission.csv', index=False)\ndisplay(df.head())","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:59:15.563810Z","iopub.execute_input":"2021-05-26T15:59:15.564412Z","iopub.status.idle":"2021-05-26T15:59:15.607429Z","shell.execute_reply.started":"2021-05-26T15:59:15.564370Z","shell.execute_reply":"2021-05-26T15:59:15.606272Z"},"trusted":true},"execution_count":null,"outputs":[]}]}