{"cells":[{"metadata":{"execution":{"iopub.execute_input":"2021-02-18T02:02:29.748745Z","iopub.status.busy":"2021-02-18T02:02:29.748052Z","iopub.status.idle":"2021-02-18T02:02:35.316312Z","shell.execute_reply":"2021-02-18T02:02:35.315134Z"},"papermill":{"duration":5.583526,"end_time":"2021-02-18T02:02:35.31656","exception":false,"start_time":"2021-02-18T02:02:29.733034","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"import os\nimport tensorflow as tf\nimport tensorflow_hub as hub\nimport numpy as np\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-02-18T02:02:35.354007Z","iopub.status.busy":"2021-02-18T02:02:35.342217Z","iopub.status.idle":"2021-02-18T02:02:36.641337Z","shell.execute_reply":"2021-02-18T02:02:36.640873Z"},"papermill":{"duration":1.314349,"end_time":"2021-02-18T02:02:36.641503","exception":false,"start_time":"2021-02-18T02:02:35.327154","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"!cp -R ../input/cassava-layer/ /kaggle/working/cassava-layer/","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-02-18T02:02:36.666715Z","iopub.status.busy":"2021-02-18T02:02:36.666105Z","iopub.status.idle":"2021-02-18T02:02:36.670353Z","shell.execute_reply":"2021-02-18T02:02:36.669914Z"},"papermill":{"duration":0.018569,"end_time":"2021-02-18T02:02:36.670472","exception":false,"start_time":"2021-02-18T02:02:36.651903","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"os.environ[\"TFHUB_CACHE_DIR\"] = \"/kaggle/working/cassava-layer/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import smart_resize\n## Load model. Use \"load_weights\" if you only save your model weights.\n\nmodel = keras.models.load_model(\"../input/vgg8649/VGG_8649.h5\")","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-02-18T02:02:43.273884Z","iopub.status.busy":"2021-02-18T02:02:43.2733Z","iopub.status.idle":"2021-02-18T02:02:43.276968Z","shell.execute_reply":"2021-02-18T02:02:43.277342Z"},"papermill":{"duration":0.018084,"end_time":"2021-02-18T02:02:43.2775","exception":false,"start_time":"2021-02-18T02:02:43.259416","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"AUTO = tf.data.experimental.AUTOTUNE\nBATCH_SIZE = 32\nIMAGE_SIZE = [512, 512]","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-02-18T02:02:43.307181Z","iopub.status.busy":"2021-02-18T02:02:43.305403Z","iopub.status.idle":"2021-02-18T02:02:43.307753Z","shell.execute_reply":"2021-02-18T02:02:43.308187Z"},"papermill":{"duration":0.020611,"end_time":"2021-02-18T02:02:43.308327","exception":false,"start_time":"2021-02-18T02:02:43.287716","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def _parse_function(proto):\n    # feature_description needs to be defined since datasets use graph-execution\n    # - its used to build their shape and type signature\n    feature_description = {\n        'image': tf.io.FixedLenFeature([], tf.string, default_value=''),\n        'image_name': tf.io.FixedLenFeature([], tf.string, default_value=''),\n        'target': tf.io.FixedLenFeature([], tf.int64, default_value=-1)\n    }\n\n    parsed_features = tf.io.parse_single_example(proto, feature_description)\n    image = tf.image.decode_jpeg(parsed_features['image'], channels=3)\n    image = tf.cast(image, tf.float32) # :: [0.0, 255.0]\n    image = tf.reshape(image, [*IMAGE_SIZE, 3])\n    target = tf.one_hot(parsed_features['target'], depth=5)\n    image_id = parsed_features['image_name']\n    return image, target, image_id","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-02-18T02:02:43.336881Z","iopub.status.busy":"2021-02-18T02:02:43.335016Z","iopub.status.idle":"2021-02-18T02:02:43.337499Z","shell.execute_reply":"2021-02-18T02:02:43.337903Z"},"papermill":{"duration":0.019262,"end_time":"2021-02-18T02:02:43.338043","exception":false,"start_time":"2021-02-18T02:02:43.318781","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def _preprocess_fn(image, label, image_id):\n    image = image / 255.0\n    image = tf.image.resize(image, (336, 336))\n    label = tf.concat([label, [0]], axis=0)\n    return image, label, image_id","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-02-18T02:02:43.364218Z","iopub.status.busy":"2021-02-18T02:02:43.363696Z","iopub.status.idle":"2021-02-18T02:02:43.367748Z","shell.execute_reply":"2021-02-18T02:02:43.367247Z"},"papermill":{"duration":0.019238,"end_time":"2021-02-18T02:02:43.367868","exception":false,"start_time":"2021-02-18T02:02:43.34863","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def load_dataset(tfrecords_fnames):\n    raw_ds = tf.data.TFRecordDataset(tfrecords_fnames, num_parallel_reads=AUTO)\n    parsed_ds = raw_ds.map(_parse_function, num_parallel_calls=AUTO)\n    parsed_ds = parsed_ds.map(_preprocess_fn, num_parallel_calls=AUTO)\n    return parsed_ds","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-02-18T02:02:43.394658Z","iopub.status.busy":"2021-02-18T02:02:43.392923Z","iopub.status.idle":"2021-02-18T02:02:43.395216Z","shell.execute_reply":"2021-02-18T02:02:43.395631Z"},"papermill":{"duration":0.017416,"end_time":"2021-02-18T02:02:43.395769","exception":false,"start_time":"2021-02-18T02:02:43.378353","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def build_valid_ds(valid_fnames):\n    ds = load_dataset(valid_fnames)\n    ds = ds.batch(BATCH_SIZE).prefetch(AUTO)\n    return ds","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-02-18T02:02:43.42196Z","iopub.status.busy":"2021-02-18T02:02:43.421127Z","iopub.status.idle":"2021-02-18T02:02:43.427307Z","shell.execute_reply":"2021-02-18T02:02:43.426732Z"},"papermill":{"duration":0.020184,"end_time":"2021-02-18T02:02:43.427434","exception":false,"start_time":"2021-02-18T02:02:43.40725","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"TEST_PATH = '../input/cassava-leaf-disease-classification/test_tfrecords/'\nvalid_fnames = [TEST_PATH + fname for fname in os.listdir(TEST_PATH)]","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-02-18T02:02:43.454373Z","iopub.status.busy":"2021-02-18T02:02:43.453731Z","iopub.status.idle":"2021-02-18T02:02:43.632982Z","shell.execute_reply":"2021-02-18T02:02:43.632514Z"},"papermill":{"duration":0.19384,"end_time":"2021-02-18T02:02:43.633113","exception":false,"start_time":"2021-02-18T02:02:43.439273","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"test_ds = build_valid_ds(valid_fnames)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-02-18T02:02:43.663084Z","iopub.status.busy":"2021-02-18T02:02:43.662241Z","iopub.status.idle":"2021-02-18T02:02:47.475306Z","shell.execute_reply":"2021-02-18T02:02:47.474259Z"},"papermill":{"duration":3.831242,"end_time":"2021-02-18T02:02:47.475464","exception":false,"start_time":"2021-02-18T02:02:43.644222","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"preds = model.predict(test_ds)\nlabels = tf.argmax(preds, axis=-1)\nlabels = labels.numpy()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-02-18T02:02:47.503391Z","iopub.status.busy":"2021-02-18T02:02:47.502314Z","iopub.status.idle":"2021-02-18T02:02:47.541062Z","shell.execute_reply":"2021-02-18T02:02:47.540613Z"},"papermill":{"duration":0.054659,"end_time":"2021-02-18T02:02:47.541193","exception":false,"start_time":"2021-02-18T02:02:47.486534","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"test_ds = build_valid_ds(valid_fnames)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-02-18T02:02:47.568497Z","iopub.status.busy":"2021-02-18T02:02:47.567697Z","iopub.status.idle":"2021-02-18T02:02:47.6101Z","shell.execute_reply":"2021-02-18T02:02:47.609507Z"},"papermill":{"duration":0.057975,"end_time":"2021-02-18T02:02:47.610227","exception":false,"start_time":"2021-02-18T02:02:47.552252","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"names = []\nfor item in test_ds:\n    names.append(item[2].numpy())\nnames = np.concatenate(names)\nnames = [name.decode() for name in names]","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-02-18T02:02:47.641787Z","iopub.status.busy":"2021-02-18T02:02:47.640884Z","iopub.status.idle":"2021-02-18T02:02:47.750419Z","shell.execute_reply":"2021-02-18T02:02:47.749918Z"},"papermill":{"duration":0.129141,"end_time":"2021-02-18T02:02:47.750577","exception":false,"start_time":"2021-02-18T02:02:47.621436","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"submission_df = pd.DataFrame({'image_id':names, 'label':labels})\nsubmission_df.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df","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}