{
  "id": 214747,
  "title": "how to read tfrecord",
  "url": "/competitions/cassava-leaf-disease-classification/discussion/214747",
  "author_name": "",
  "post_date": "2021-01-27T14:45:14.634340900Z",
  "votes": null,
  "comment_count": 5,
  "views": 0,
  "content": "<p>how to read label from tfrecord file?</p>",
  "messages": [
    {
      "id": "1172772",
      "postDate": "01/27/2021 14:45:14",
      "content": "<p>how to read label from tfrecord file?</p>",
      "rawMarkdown": "how to read label from tfrecord file?",
      "votes": null
    },
    {
      "id": "1172885",
      "postDate": "01/27/2021 15:32:08",
      "content": "<p>Hello!</p>\n<p>If you have literally no idea of how to work with TFRecords, consider looking through this <strong><a href=\"https://www.kaggle.com/jessemostipak/getting-started-tpus-cassava-leaf-disease\" target=\"_blank\">community notebook</a></strong> for a quick start.</p>\n<p>Generally speaking, you have to know the feature map for a TFRecord you are trying to read, which in this case is:<br>\n<code>feature_map = {</code><br>\n<code>\"image\": tf.io.FixedLenFeature([], tf.string),</code><br>\n<code>\"label\": tf.io.FixedLenFeature([], tf.int64)}</code><br>\nWith this feature map, you can write a function to read a TFRecord file <code>filename</code> like this:<br>\n<code>def read_tfrecord(filename):</code><br>\n<code>example = tf.io.parse_single_example(filename, feature_map)</code><br>\n<code>image = tf.image.decode_jpeg(image, channels=3)</code><br>\n<code>label = tf.cast(example['label'], tf.int32)</code><br>\n<code>return image, label</code><br>\nThen you can create a TFRecord dataset and map it with this function to read all the records in the file or list of files <code>filenames</code>:<br>\n<code>dataset = tf.data.TFRecordDataset(filenames).map(lambda x: read_tfrecord(x))</code></p>",
      "rawMarkdown": "Hello!\n\nIf you have literally no idea of how to work with TFRecords, consider looking through this **[community notebook](https://www.kaggle.com/jessemostipak/getting-started-tpus-cassava-leaf-disease)** for a quick start.\n\nGenerally speaking, you have to know the feature map for a TFRecord you are trying to read, which in this case is:\n`feature_map = {`\n`    \"image\": tf.io.FixedLenFeature([], tf.string),`\n`    \"label\": tf.io.FixedLenFeature([], tf.int64)}`\nWith this feature map, you can write a function to read a TFRecord file `filename` like this:\n`def read_tfrecord(filename):`\n`    example = tf.io.parse_single_example(filename, feature_map)`\n`    image = tf.image.decode_jpeg(image, channels=3)`\n`    label = tf.cast(example['label'], tf.int32)`\n`    return image, label`\nThen you can create a TFRecord dataset and map it with this function to read all the records in the file or list of files `filenames`:\n`dataset = tf.data.TFRecordDataset(filenames).map(lambda x: read_tfrecord(x))`",
      "votes": null
    },
    {
      "id": "1173087",
      "postDate": "01/27/2021 17:15:41",
      "content": "<p>it is good to read pictures and label the CSV files directly</p>",
      "rawMarkdown": "it is good to read pictures and label the CSV files directly",
      "votes": null
    },
    {
      "id": "1173202",
      "postDate": "01/27/2021 18:16:46",
      "content": "<p>That's arguable, as using tfrecords, despite some technical difficulties (which are many), results in remarkable performance and shorter computation time.</p>",
      "rawMarkdown": "That's arguable, as using tfrecords, despite some technical difficulties (which are many), results in remarkable performance and shorter computation time.",
      "votes": null
    },
    {
      "id": "1173209",
      "postDate": "01/27/2021 18:22:52",
      "content": "<p>oh，I didn't know the meaning of this tfrecords file, but now I do.Thank you for reminding me.</p>",
      "rawMarkdown": "oh，I didn't know the meaning of this tfrecords file, but now I do.Thank you for reminding me.",
      "votes": null
    },
    {
      "id": "1174205",
      "postDate": "01/28/2021 10:41:59",
      "content": "<p><a href=\"https://www.kaggle.com/ashish2001/using-tfrecords\" target=\"_blank\">https://www.kaggle.com/ashish2001/using-tfrecords</a><br>\nYou can go through this notebook for some explanation!</p>",
      "rawMarkdown": "https://www.kaggle.com/ashish2001/using-tfrecords\nYou can go through this notebook for some explanation!",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1172885,
      "author_name": "nickuzmenkov",
      "author_url": "",
      "post_date": "01/27/2021 15:32:08",
      "content": "<p>Hello!</p>\n<p>If you have literally no idea of how to work with TFRecords, consider looking through this <strong><a href=\"https://www.kaggle.com/jessemostipak/getting-started-tpus-cassava-leaf-disease\" target=\"_blank\">community notebook</a></strong> for a quick start.</p>\n<p>Generally speaking, you have to know the feature map for a TFRecord you are trying to read, which in this case is:<br>\n<code>feature_map = {</code><br>\n<code>\"image\": tf.io.FixedLenFeature([], tf.string),</code><br>\n<code>\"label\": tf.io.FixedLenFeature([], tf.int64)}</code><br>\nWith this feature map, you can write a function to read a TFRecord file <code>filename</code> like this:<br>\n<code>def read_tfrecord(filename):</code><br>\n<code>example = tf.io.parse_single_example(filename, feature_map)</code><br>\n<code>image = tf.image.decode_jpeg(image, channels=3)</code><br>\n<code>label = tf.cast(example['label'], tf.int32)</code><br>\n<code>return image, label</code><br>\nThen you can create a TFRecord dataset and map it with this function to read all the records in the file or list of files <code>filenames</code>:<br>\n<code>dataset = tf.data.TFRecordDataset(filenames).map(lambda x: read_tfrecord(x))</code></p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1173087,
      "author_name": "darknesszx",
      "author_url": "",
      "post_date": "01/27/2021 17:15:41",
      "content": "<p>it is good to read pictures and label the CSV files directly</p>",
      "votes": null,
      "replies": [
        {
          "id": 1173202,
          "author_name": "nickuzmenkov",
          "author_url": "",
          "post_date": "01/27/2021 18:16:46",
          "content": "<p>That's arguable, as using tfrecords, despite some technical difficulties (which are many), results in remarkable performance and shorter computation time.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 1173209,
          "author_name": "darknesszx",
          "author_url": "",
          "post_date": "01/27/2021 18:22:52",
          "content": "<p>oh，I didn't know the meaning of this tfrecords file, but now I do.Thank you for reminding me.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1174205,
      "author_name": "ashish2001",
      "author_url": "",
      "post_date": "01/28/2021 10:41:59",
      "content": "<p><a href=\"https://www.kaggle.com/ashish2001/using-tfrecords\" target=\"_blank\">https://www.kaggle.com/ashish2001/using-tfrecords</a><br>\nYou can go through this notebook for some explanation!</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1172772": "how to read label from tfrecord file?",
    "1172885": "Hello!\n\nIf you have literally no idea of how to work with TFRecords, consider looking through this **[community notebook](https://www.kaggle.com/jessemostipak/getting-started-tpus-cassava-leaf-disease)** for a quick start.\n\nGenerally speaking, you have to know the feature map for a TFRecord you are trying to read, which in this case is:\n`feature_map = {`\n`    \"image\": tf.io.FixedLenFeature([], tf.string),`\n`    \"label\": tf.io.FixedLenFeature([], tf.int64)}`\nWith this feature map, you can write a function to read a TFRecord file `filename` like this:\n`def read_tfrecord(filename):`\n`    example = tf.io.parse_single_example(filename, feature_map)`\n`    image = tf.image.decode_jpeg(image, channels=3)`\n`    label = tf.cast(example['label'], tf.int32)`\n`    return image, label`\nThen you can create a TFRecord dataset and map it with this function to read all the records in the file or list of files `filenames`:\n`dataset = tf.data.TFRecordDataset(filenames).map(lambda x: read_tfrecord(x))`",
    "1173087": "it is good to read pictures and label the CSV files directly",
    "1173202": "That's arguable, as using tfrecords, despite some technical difficulties (which are many), results in remarkable performance and shorter computation time.",
    "1173209": "oh，I didn't know the meaning of this tfrecords file, but now I do.Thank you for reminding me.",
    "1174205": "https://www.kaggle.com/ashish2001/using-tfrecords\nYou can go through this notebook for some explanation!"
  },
  "source": "meta"
}