自定义回调以在样本图像上显示预测

时间:2019-07-19 19:58:52

标签: tensorflow keras callback tensorboard

here  is my custom callback to show some images

class DisplaySamples(tf.keras.callbacks.Callback):
    def __init__(self, log_dir='./logs/tmp/', get_samples=None):
        super(DisplaySamples, self).__init__()
        self.step = 0
        self.samples = get_samples
        self.writer = tf.summary.FileWriter(log_dir)

    def on_batch_end(self, batch, logs=None):
        logs = logs or {}
        self.step += 1
        if self.step % 2 == 0:
            # images using TensorBorad;
            summary_str = []
            for i in range(len(self.samples)):
                image = self.samples[i]
                keypoints = np.squeeze(self.model.predict(image)[0], axis=0)

                summary_str.append(tf.Summary.Value(tag='plot/image/{}'.format(i),
                                                    image=tf.summary.image("image", image)))
                summary_str.append(tf.Summary.Value(tag='plot/keypts/{}'.format(i),
                                                    image=tf.summary.image("keyps", keypoints)))

            self.writer.add_summary(tf.Summary(value=summary_str), global_step=self.step)


def read_images():
    """

    Returns
    -------

    """

    image_list = []
    for filename in glob.glob('../samples/*.jpg'):
        im = cv2.imread(filename)
        im = cv2.resize(im, (512, 512, 3))
        im = np.reshape(im, (1, 512, 512, 3))
        image_list.append(im)
    return image_list

通过在f​​it_generator方法中调用它,我得到了这个错误:

image = tf.summary.image(“ image”,image))) TypeError:MergeFrom()的参数必须是同一类的实例:预期的tensorflow.Summary.Image得到了Tensor。

0 个答案:

没有答案