{"cells":[{"metadata":{},"cell_type":"markdown","source":"**The following needs to be done for submission**\n\n1. Either the model downloaded in training must be uploaded(I have uploaded in weight folder u can create new one to test yours) or notebook dataset can be refered from Add data --> under Notebook Output    Files and accordingly path of model needs to be changed (tf.keras.models.load_model('../input/weight/model_Resnet50.h5', custom_objects = None )).\n2. Switch on the Accelerator --> GPU\n3. Internet must be disabled\n4. Try Run All \n5. Onc successfully completed click on Save Version in top right corner to confirm 3 things -  whether under advanced setting (1) always save output is checked along with (2) Run it with GPU and (3) Save and Run All options is selected. Once it is confirmed click on Save\n6. Once the process is completed now you can submit your work either from submit predictions in cassave competition main page or click on the version number (after Save Version) and go to viewer and under 3 dots submit your work. Within few minutes you must get score >= 68%"},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport re\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DIM = 128\nBATCH_SIZE = 64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TEST_FILENAMES = tf.io.gfile.glob('../input/cassava-leaf-disease-classification/test_tfrecords/*.tfrec') \nprint(TEST_FILENAMES)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"def decode_image(image_data):\n    image = tf.image.decode_jpeg(image_data, channels=3)\n    image = tf.cast(image, tf.float32) / 255.0  \n    image = tf.image.resize(image, [DIM, DIM])\n    image = tf.reshape(image, [DIM, DIM, 3])\n    return image\n\ndef read_unlabeled_tfrecord(example):\n    UNLABELED_TFREC_FORMAT = {\n       \"image\": tf.io.FixedLenFeature([], tf.string), \n       \"image_name\": tf.io.FixedLenFeature([], tf.string), \n       \n    }\n    example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)\n    image = decode_image(example['image'])\n    idnum = example['image_name']\n    return image, idnum \n\ndef load_dataset(filenames, labeled = True, ordered = False):\n    ignore_order = tf.data.Options()\n    if not ordered:\n        ignore_order.experimental_deterministic = False \n        \n    dataset = tf.data.TFRecordDataset(filenames) \n    dataset = dataset.with_options(ignore_order) \n    dataset = dataset.map(read_unlabeled_tfrecord) \n    return dataset\n\ndef get_test_dataset(ordered=True):\n    dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)\n    dataset = dataset.batch(BATCH_SIZE)\n    return dataset\n\ndef count_data_items(filenames):\n    n = [int(re.compile(r\"-([0-9]*)\\.\").search(filename).group(1)) for filename in filenames]\n    return np.sum(n)\n\nNUM_TEST_IMAGES = int( count_data_items(TEST_FILENAMES) )\n\ndef predict():\n    model = tf.keras.models.load_model('../input/weight/model_Resnet50.h5', custom_objects = None ) \n    print(model.summary())\n    test_ds = get_test_dataset(ordered=True) \n    test_images_ds = test_ds.map(lambda image, idnum: image)\n    prob = model.predict(test_images_ds)\n    predictions = np.argmax(prob, axis=-1)\n    print(predictions)\n    test_ids_ds = test_ds.map(lambda image, idnum: idnum).unbatch()\n    test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES))).numpy().astype('U') \n    np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='image_id,label', comments='')\npredict()\nprint(\"Done\")","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}