{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport pandas as pd\nimport matplotlib.image as image\nimport os\nimport itertools","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''Utility Methods and objects needed for writing data to file'''\ndef _bytes_feature(value):\n  \"\"\"Returns a bytes_list from a string / byte.\"\"\"\n  if isinstance(value, type(tf.constant(0))):\n    value = value.numpy() # BytesList won't unpack a string from an EagerTensor.\n  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))\n\ndef _float_feature(value):\n  \"\"\"Returns a float_list from a float / double.\"\"\"\n  return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))\n\ndef _int64_feature(value):\n  \"\"\"Returns an int64_list from a bool / enum / int / uint.\"\"\"\n  return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))\n\ndef convert_to_image_example(image_string,image_name, target):\n    feature = {\n      'image': _bytes_feature(image_string),\n      'image_name': _bytes_feature(image_name),        \n      'target': _int64_feature(target)\n    }\n    return tf.train.Example(features=tf.train.Features(feature=feature))\n\n\ndef write_file(dataset,outputfilename):\n    record_file = outputfilename\n    base_image_path = '../input/cassava-leaf-disease-classification/train_images/'\n    with tf.io.TFRecordWriter(record_file) as writer:\n        for row in dataset.itertuples(index=False):\n            img = open(base_image_path + row[0], 'rb').read()\n            tf_example = convert_to_image_example(img,row[0].encode(),row[1])\n            writer.write(tf_example.SerializeToString())\n            \n            ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"overall_dataset = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\nsampleds = overall_dataset[0:1]\nwrite_file(sampleds,'-1_generated.tfrec')\n#write_file(sampleds,'1_generated.tfrec')","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}