Created
June 8, 2018 15:29
-
-
Save mrajchl/a436f55ede34b55f65cdbda1c7e9ae28 to your computer and use it in GitHub Desktop.
Write .nii data to a TFRecords database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _int64_feature(value): | |
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) | |
def _float_feature(value): | |
return tf.train.Feature(float_list=tf.train.FloatList(value=value)) | |
# path to save the TFRecords file | |
train_filename = 'train.tfrecords' | |
# open the file | |
writer = tf.python_io.TFRecordWriter(train_filename) | |
# iterate through all .nii files: | |
for meta_data in all_filenames: | |
# Load the image and label | |
img, label = load_img(meta_data, reader_params) | |
# Create a feature | |
feature = {'train/label': _int64_feature(label), | |
'train/image': _float_feature(img.ravel())} | |
# Create an example protocol buffer | |
example = tf.train.Example(features=tf.train.Features(feature=feature)) | |
# Serialize to string and write on the file | |
writer.write(example.SerializeToString()) | |
writer.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment