2

I am using the summary operation in Tensorflow 1.4. I created a summary operation and can get the output when running the session. However, I look into the value of the summary output,which is a string variable, I cannot get the values from it directly. I want to know if there is any solution to extract those values into a readable format. Thanks.

Code snippet:

with tf.train.MonitoredTrainingSession(
        hooks=[summary_hook, saver_hook]) as sess:

    while not sess.should_stop():
        try:
            summary, _ = sess.run([merged, train_ops])
        except tf.errors.OutOfRangeError:
            print('Finish training.')
            break

Debugger output:

enter image description here

Yuan Ma
  • 858
  • 2
  • 11
  • 23
  • Have you seen this question (and the answers therein): https://stackoverflow.com/questions/37304461/tensorflow-importing-data-from-a-tensorboard-tfevent-file ? That's usually where I begin with inspecting a summary op. – muskrat Jan 10 '18 at 21:34
  • @muskrat Thank you for your help. In fact, my question is just related to decoding the summary string. Since I didn't save the log file, I cannot use the summary_iterator to analysis it. I am able to inspect some values using TensorBoard, but I would like to do some customized analysis. – Yuan Ma Jan 10 '18 at 22:02

1 Answers1

0

I have done something similar on my example. Let define three nodes with summaries and merge them into one summary node:

_, accuracy = tf.metrics.accuracy(labels, logits)
_, precision = tf.metrics.precision(labels, logits)
_, recall = tf.metrics.recall(labels, logits)
acc_sum = tf.summary.scalar('accuracy', accuracy)
pred_sum = tf.summary.scalar('precision', precision)
rec_sum = tf.summary.scalar('recall', recall)
summary_acc_op = tf.summary.merge([acc_sum, pred_sum, rec_sum])

Then run it within the session:

summ_str = sess.run(summary_acc_op, feed_dict=feed_dict)

I saved it to a binary file and tried to find out its structure. The function below construct the dictionary of summaries names and values:

def summaryStringToDict(self, summ_str):
    idx = 0
    ret_dict = {}
    while idx < len(summ_str):
        item_len = struct.unpack('B', summ_str[idx+1])[0]
        name_len = struct.unpack('B', summ_str[idx+3])[0]
        name = summ_str[idx+4:idx+4+name_len]
        value = struct.unpack('<f', summ_str[idx+5+name_len:idx+9+name_len])[0]
        ret_dict[name] = value
        idx += item_len + 2
    return ret_dict

This should work for any summary containing floats. The'<f' is for little-endian float decoding. My example output from the function above is:

{"accuracy_1": 0.734375, "precision_1": 1.0, "recall_1": 0.291666656733}

I hope that the similar structure is used for more advanced summaries.