{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_addons","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# from tensorflow.keras import layers\n# augmentor = tf.keras.Sequential([\n#     layers.experimental.preprocessing.RandomContrast(.2),\n#     layers.experimental.preprocessing.RandomZoom(.2, .2),\n#     layers.experimental.preprocessing.RandomRotation(.25),\n#     layers.experimental.preprocessing.RandomTranslation(.2, .2)\n# ])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_size = 224\n@tf.function\ndef parse_function(filename):\n    image_string = tf.io.read_file(filename)\n    image = tf.io.decode_image(image_string, channels=3, expand_animations=False)\n    image = tf.image.resize(image, (image_size, image_size))\n    image = tf.cast(image, tf.float32) / 255.\n    image = tf.image.adjust_saturation(image, 1.5)\n    image = tf.clip_by_value(image, 0, 1)\n    return image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filenames = tf.io.gfile.glob('../input/plant-pathology-2021-fgvc8/test_images/*.jpg')\ndataset = (tf.data.Dataset.from_tensor_slices(filenames)\n                .map(parse_function).batch(4).prefetch(tf.data.AUTOTUNE))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tf.keras.models.load_model(\n    '../input/mobilenet040820/mobilenet_v3.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"one_hot = {'healthy': [0, 0, 0, 0, 0, 1],\n           'scab': [0, 1, 0, 0, 0, 0],\n           'scab frog_eye_leaf_spot': [0, 1, 1, 0, 0, 0],\n           'frog_eye_leaf_spot': [0, 0, 1, 0, 0, 0],\n           'rust': [0, 0, 0, 1, 0, 0],\n           'complex': [1, 0, 0, 0, 0, 0],\n           'powdery_mildew': [0, 0, 0, 0, 1, 0],\n           'rust frog_eye_leaf_spot': [0, 0, 1, 1, 0, 0],\n           'frog_eye_leaf_spot complex': [1, 0, 1, 0, 0, 0],\n           'scab frog_eye_leaf_spot complex': [1, 1, 1, 0, 0, 0],\n           'powdery_mildew complex': [1, 0, 0, 0, 1, 0],\n           'rust complex': [1, 0, 0, 1, 0, 0]}\nnum_classes = 6\ndecode_one_hot = {tuple(value): key for key, value in one_hot.items()}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = model.predict(dataset).round()\nlabels = []\nfor prediction in predictions:\n    print(prediction)\n    if prediction.sum() == 0.:\n        labels.append('complex')\n    else:\n        for i in range(len(prediction), -2, -1):\n            new_prediction = list(map(lambda x: int(x), prediction.copy()))\n            if 0 <= i <= len(prediction)-1:   new_prediction[i] = 0     \n            if 0 <= i+1 <= len(prediction)-1: new_prediction[i+1] = 0\n            try: labels.append(decode_one_hot[tuple(new_prediction)])\n            except: continue\n            break\nlabels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas\nsubmission = pandas.DataFrame({'image' : filenames, 'labels' : labels})\nsubmission['image'] = submission['image'].apply(lambda filename: filename.split('/')[-1])\nsubmission.to_csv('submission.csv', index=False)\nsubmission.head()","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}