"docs/git@developer.sourcefind.cn:change/sglang.git" did not exist on "0f5cb8cae15a76e8795e371f70a3bb87f7cc8bb5"
Commit 32533a06 authored by Marianne Linhares Monteiro's avatar Marianne Linhares Monteiro Committed by GitHub
Browse files

Creating convert_to_tfrecord function

Changing file to follow examples/how_tos/reading_data/convert_to_records.py
parent 751bfb3f
...@@ -58,6 +58,25 @@ def read_pickle_from_file(filename): ...@@ -58,6 +58,25 @@ def read_pickle_from_file(filename):
return data_dict return data_dict
def convert_to_tfrecord(input_file, name):
"""Converts a file to tfrecords."""
print('Generating %s' % output_file)
record_writer = tf.python_io.TFRecordWriter(output_file)
data_dict = read_pickle_from_file(input_file)
data = data_dict['data']
labels = data_dict['labels']
num_entries_in_batch = len(labels)
for i in range(num_entries_in_batch):
example = tf.train.Example(
features=tf.train.Features(feature={
'image': _bytes_feature(data[i].tobytes()),
'label': _int64_feature(labels[i])
}))
record_writer.write(example.SerializeToString())
record_writer.close()
def main(argv): def main(argv):
del argv # Unused. del argv # Unused.
...@@ -65,24 +84,8 @@ def main(argv): ...@@ -65,24 +84,8 @@ def main(argv):
for file_name in file_names: for file_name in file_names:
input_file = os.path.join(FLAGS.input_dir, file_name) input_file = os.path.join(FLAGS.input_dir, file_name)
output_file = os.path.join(FLAGS.output_dir, file_name + '.tfrecords') output_file = os.path.join(FLAGS.output_dir, file_name + '.tfrecords')
# Convert to Examples and write the result to TFRecords.
print('Generating %s' % output_file) convert_to_tfrecord(input_file, output_file)
record_writer = tf.python_io.TFRecordWriter(output_file)
data_dict = read_pickle_from_file(input_file)
data = data_dict['data']
labels = data_dict['labels']
num_entries_in_batch = len(labels)
for i in range(num_entries_in_batch):
example = tf.train.Example(
features=tf.train.Features(feature={
'image': _bytes_feature(data[i].tobytes()),
'label': _int64_feature(labels[i])
}))
record_writer.write(example.SerializeToString())
record_writer.close()
print('Done!') print('Done!')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment