{"cells":[{"metadata":{},"cell_type":"markdown","source":"This is the second of two parts time-saving quickstart baseline. Submission accuracy is 0.88 in public dataset. In this notebook, there are three points which may benefit you. \n* The nearest interpolation of load_img method is consistent with [part I training](https://www.kaggle.com/electro/keras-baseline-0-88-quickstart-i-training-saving).\n* A for loop to predict a batch instead of one image input at a time.\n* Generation of submission file by appending batch predictions."},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport tensorflow as tf\n\n\nCassava_dir = \"../input/cassava-leaf-disease-classification/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow import keras\nmodel = keras.models.load_model('../input/cassava-baseline-weights/best_weights.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.DataFrame(columns=['image_id','label'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def predict_on_batch(test_list, wpath=Cassava_dir, target_size=(380,380)):\n    input_batch=[]\n    for IMAGE_ID in test_list:\n        image = tf.keras.preprocessing.image.load_img(os.path.join(wpath, \"test_images\",IMAGE_ID), \n                                                      grayscale=False, \n                                                      color_mode=\"rgb\", \n                                                      target_size=target_size, \n                                                      interpolation=\"nearest\")\n        input_arr = keras.preprocessing.image.img_to_array(image)\n        input_batch.append(input_arr)\n    return np.array(input_batch)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import math\n\nTEST_DIR = '../input/cassava-leaf-disease-classification/test_images/'\ntest_images = os.listdir(TEST_DIR)\n\nN = len(test_images)\nif N == 1:\n    BATCH_SIZE = 1\nelse:\n    BATCH_SIZE = 16","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for batch_index in range(math.ceil(N/BATCH_SIZE)):\n    if batch_index*BATCH_SIZE+BATCH_SIZE < N:\n        test_X = predict_on_batch(test_images[batch_index*BATCH_SIZE:batch_index*BATCH_SIZE+BATCH_SIZE])\n        predictions = model.predict(test_X).argmax(axis = 1)\n        sub_batch = pd.DataFrame({'image_id':test_images[batch_index*BATCH_SIZE:batch_index*BATCH_SIZE+BATCH_SIZE],\n                    'label':list(predictions)})\n    else:\n        test_X = predict_on_batch(test_images[batch_index*BATCH_SIZE:])\n        predictions = model.predict(test_X).argmax(axis = 1)\n        sub_batch = pd.DataFrame({'image_id':test_images[batch_index*BATCH_SIZE:],\n                    'label':list(predictions)})\n        \n    sub = sub.append(sub_batch, ignore_index=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub['label'] = sub['label'].astype('int64')\nsub","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.to_csv('submission.csv', index = False)","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}