{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"# Importing tensorflow and its helper to display images\n\nimport tensorflow as tf\nimport IPython.display as display","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Let's select one TFRecord and read it using tf.data.TFRecordDataset class in tensorflow\n\nfile_path = '../input/flower-classification-with-tpus/tfrecords-jpeg-192x192/train/00-192x192-798.tfrec'\nraw_data = tf.data.TFRecordDataset(file_path)\n\n# Now we have TFRecord data in the raw_data variable but to read and display images,\n# we need to know with what features the image was encoded into this TFRecord file.\n\n# So in the raw_data we will iterate over one example/image\nfor data in raw_data.take(1):\n    example = tf.train.Example()\n    example.ParseFromString(data.numpy())\n    print(example)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# We see that it has three features namely id, class, and image of type int64, string, and string respectively.\n# byte_list genric type can be coerced into string type\n# Let's quickly make a feature dictionary and parse the raw_data using .map\n\nfeature_descp = {\n    'class': tf.io.FixedLenFeature([], tf.int64),\n    'image': tf.io.FixedLenFeature([], tf.string),\n    'id': tf.io.FixedLenFeature([], tf.string),\n}\n\ndef parse_example(example):\n    return tf.io.parse_single_example(example, feature_descp)\n\nparsed_data = raw_data.map(parse_example)\n\nparsed_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# In this step we can send the image data to TPU's or as I do here simply display images.\nfor example_image in parsed_data:\n    images = example_image['image'].numpy()\n    display.display(display.Image(data = images))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}