Search code examples
pythontensorflowhistogramtensorboardsummary

Tf Summary not giving the histograms but saves the session graph (pre-trained model)


I am carrying out an analysis to visualize the distribution of weights for a pre-trained model available online. Its a Resnet18 model trained on CIFAR10.

I have the following code to restore the model from meta and ckpt and then I try to create a histogram of all the weights and bias of the convolution layers using tf.summary.histogram

`with tf.Session(graph=tf.Graph()) as sess:
            read=tf.train.import_meta_graph(self.paths[0], clear_devices=True)
            try:
                read.restore(sess, tf.train.latest_checkpoint(self.paths[1]))
            except ValueError:
                try:
                    read.restore(sess, self.paths[1])
                except Exception as e:
                    print(e.message)

            # Summaries of weights
            summ_writer = tf.summary.FileWriter(self.sum_path, sess.graph)
            fp_summaries = []
            for lys in tf.trainable_variables():
                lay_nam = lys.name.split("/")[-2]
                if 'kernel' in lys.name:
                    with tf.name_scope(lay_nam+'_hist'):
                        tf_w_hist = tf.summary.histogram('Weights', tf.reshape(lys.eval(), [-1]))
                        fp_summaries.extend([tf_w_hist])
                if 'bias' in lys.name:
                    with tf.name_scope(lay_nam+'_hist'):
                        tf_b_hist = tf.summary.histogram('Bias', lys.eval())
                        fp_summaries.extend([tf_b_hist])
            tf_fp_summaries = tf.summary.merge(fp_summaries)
            # Run the graph
            output, _=sess.run([softmax, tf_fp_summaries], feed_dict={x: self.x_test[0:100, ]})

However, the log events stored in folder is storing only the main graph. The histograms are not seen on tensorboard. What might be going wrong here ?


Solution

  • It's not enough to pass the merged summary node to sess.run. You need to take that evaluated result and pass it to the add_summary method of your FileWriter instance.

    # evaluate the merged summary node in the graph
    output, summ = sess.run([softmax, tf_fp_summaries], ...)
    # explicitly write to file
    summ_writer.add_summary(summ, global_step)
    # optional, force to write to disk
    summ_writer.flush()