Search code examples
tensorflowtensorflow-datasetstfrecord

Unfurling TFRecords growing slower and slower


I'm trying to convert a TFRecord dataset back to images and I'm using the following code to do so:

def get_im_and_label_helper(parsed_features, im_format, label_format):
    im = tf.image.decode_png(parsed_features['image/encoded'])
    label = tf.image.decode_png(parsed_features['image/segmentation/class/encoded'])
    im, label = im.eval(), label.eval()
    return im, label

for tfr_file_path_name in tfr_files_list:
    tfr_file_path = os.path.join(sub_dataset_dir, tfr_file_path_name)
    record_iterator = tf.python_io.tf_record_iterator(tfr_file_path)

    for string_record in record_iterator:
        parsed_features = tf.parse_single_example(string_record, READ_FEATURES)

        filename = parsed_features['image/filename'].eval().decode("utf-8")
        im, label = get_im_and_label_helper(parsed_features, im_format, label_format)
        imageio.imwrite(os.path.join(target_dir, "images", filename + ".png"), im)
        imageio.imwrite(os.path.join(target_dir, "labels", filename + ".png"), label)

It works fine and does what I expect - extracts the images and labels and saves them in the proper place. It starts fast and it gets slower and slower as it goes on. I'm inexperienced with tensorflow, so I assume I'm causing some computation graph to grow bigger and bigger, but I don't really know.

Any ideas?


Solution

  • Using tf.enable_eager_execution() followed by tf.executing_eagerly(), and replacing all .eval()with .numpy()solved the problem.