{
  "id": 198721,
  "title": "Making predictions using TF datasets on GPU",
  "url": "/competitions/cassava-leaf-disease-classification/discussion/198721",
  "author_name": "DimitreOliveira",
  "post_date": "2020-11-22T17:15:48.826000",
  "votes": 2,
  "comment_count": 0,
  "views": 0,
  "content": "<p>I had some problems trying to make the prediction using the Tensorflow dataset module on GPU, but I finally got it right, I saw that some other people also made it work but using some other tricks, so it might be worth it to share what I did.</p>\n<p>Basically, to make sure you have control over the files I am predicting, I am loading the <code>.jpg</code> files.</p>\n<pre><code>def get_name(file_path):\n    parts = tf.strings.split(file_path, os.path.sep)\n    name = parts[-1]\n    return name\n\ndef 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, [HEIGHT, WIDTH])\n    image = tf.reshape(image, [HEIGHT, WIDTH, 3])\n    return image\n\ndef process_path(file_path):\n    name = get_name(file_path)\n    img = tf.io.read_file(file_path)\n    img = decode_image(img)\n    return img, name\n\ndef get_dataset(files_path, shuffled=False, extension='jpg'):\n    dataset = tf.data.Dataset.list_files(f'{files_path}*{extension}', shuffle=shuffled)\n    dataset = dataset.map(process_path, num_parallel_calls=AUTO)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO)\n    return dataset\n\n# Then create the dataset passing the path to the files\ntest_ds = get_dataset('/kaggle/input/cassava-leaf-disease-classification/test_images/')\n</code></pre>\n<p>You can find the full working <a href=\"https://www.kaggle.com/dimitreoliveira/cassava-leaf-disease-tpu-tensorflow-inference\" target=\"_blank\">code here</a></p>\n<p>You might also want to check the <a href=\"https://www.tensorflow.org/tutorials/load_data/images#using_tfdata_for_finer_control\" target=\"_blank\">TF reference</a>.</p>",
  "messages": [
    {
      "id": 1087403,
      "postDate": "2020-11-22T17:15:48.827Z",
      "content": "<p>I had some problems trying to make the prediction using the Tensorflow dataset module on GPU, but I finally got it right, I saw that some other people also made it work but using some other tricks, so it might be worth it to share what I did.</p>\n<p>Basically, to make sure you have control over the files I am predicting, I am loading the <code>.jpg</code> files.</p>\n<pre><code>def get_name(file_path):\n    parts = tf.strings.split(file_path, os.path.sep)\n    name = parts[-1]\n    return name\n\ndef 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, [HEIGHT, WIDTH])\n    image = tf.reshape(image, [HEIGHT, WIDTH, 3])\n    return image\n\ndef process_path(file_path):\n    name = get_name(file_path)\n    img = tf.io.read_file(file_path)\n    img = decode_image(img)\n    return img, name\n\ndef get_dataset(files_path, shuffled=False, extension='jpg'):\n    dataset = tf.data.Dataset.list_files(f'{files_path}*{extension}', shuffle=shuffled)\n    dataset = dataset.map(process_path, num_parallel_calls=AUTO)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO)\n    return dataset\n\n# Then create the dataset passing the path to the files\ntest_ds = get_dataset('/kaggle/input/cassava-leaf-disease-classification/test_images/')\n</code></pre>\n<p>You can find the full working <a href=\"https://www.kaggle.com/dimitreoliveira/cassava-leaf-disease-tpu-tensorflow-inference\" target=\"_blank\">code here</a></p>\n<p>You might also want to check the <a href=\"https://www.tensorflow.org/tutorials/load_data/images#using_tfdata_for_finer_control\" target=\"_blank\">TF reference</a>.</p>",
      "rawMarkdown": "I had some problems trying to make the prediction using the Tensorflow dataset module on GPU, but I finally got it right, I saw that some other people also made it work but using some other tricks, so it might be worth it to share what I did.\n\nBasically, to make sure you have control over the files I am predicting, I am loading the `.jpg` files.\n\n```\ndef get_name(file_path):\n    parts = tf.strings.split(file_path, os.path.sep)\n    name = parts[-1]\n    return name\n\ndef 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, [HEIGHT, WIDTH])\n    image = tf.reshape(image, [HEIGHT, WIDTH, 3])\n    return image\n\ndef process_path(file_path):\n    name = get_name(file_path)\n    img = tf.io.read_file(file_path)\n    img = decode_image(img)\n    return img, name\n\ndef get_dataset(files_path, shuffled=False, extension='jpg'):\n    dataset = tf.data.Dataset.list_files(f'{files_path}*{extension}', shuffle=shuffled)\n    dataset = dataset.map(process_path, num_parallel_calls=AUTO)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO)\n    return dataset\n\n# Then create the dataset passing the path to the files\ntest_ds = get_dataset('/kaggle/input/cassava-leaf-disease-classification/test_images/')\n```\n\nYou can find the full working [code here](https://www.kaggle.com/dimitreoliveira/cassava-leaf-disease-tpu-tensorflow-inference)\n\nYou might also want to check the [TF reference](https://www.tensorflow.org/tutorials/load_data/images#using_tfdata_for_finer_control).",
      "votes": 2
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "1087403": "I had some problems trying to make the prediction using the Tensorflow dataset module on GPU, but I finally got it right, I saw that some other people also made it work but using some other tricks, so it might be worth it to share what I did.\n\nBasically, to make sure you have control over the files I am predicting, I am loading the `.jpg` files.\n\n```\ndef get_name(file_path):\n    parts = tf.strings.split(file_path, os.path.sep)\n    name = parts[-1]\n    return name\n\ndef 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, [HEIGHT, WIDTH])\n    image = tf.reshape(image, [HEIGHT, WIDTH, 3])\n    return image\n\ndef process_path(file_path):\n    name = get_name(file_path)\n    img = tf.io.read_file(file_path)\n    img = decode_image(img)\n    return img, name\n\ndef get_dataset(files_path, shuffled=False, extension='jpg'):\n    dataset = tf.data.Dataset.list_files(f'{files_path}*{extension}', shuffle=shuffled)\n    dataset = dataset.map(process_path, num_parallel_calls=AUTO)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO)\n    return dataset\n\n# Then create the dataset passing the path to the files\ntest_ds = get_dataset('/kaggle/input/cassava-leaf-disease-classification/test_images/')\n```\n\nYou can find the full working [code here](https://www.kaggle.com/dimitreoliveira/cassava-leaf-disease-tpu-tensorflow-inference)\n\nYou might also want to check the [TF reference](https://www.tensorflow.org/tutorials/load_data/images#using_tfdata_for_finer_control)."
  }
}