我有一个非常简单的问题。我为分类定义了Keras模型(TF后端)。我想在训练期间转储输入到模型中的训练图像以进行调试。我正在尝试创建一个自定义回调来为此编写Tensorboard图像摘要。
但是如何获取回调中的真实训练数据?
目前我正在尝试:
class TensorboardKeras(Callback):
def __init__(self, model, log_dir, write_graph=True):
self.model = model
self.log_dir = log_dir
self.session = K.get_session()
tf.summary.image('input_image', self.model.input)
self.merged = tf.summary.merge_all()
if write_graph:
self.writer = tf.summary.FileWriter(self.log_dir, K.get_session().graph)
else:
self.writer = tf.summary.FileWriter(self.log_dir)
def on_batch_end(self, batch, logs=None):
summary = self.session.run(self.merged, feed_dict={})
self.writer.add_summary(summary, batch)
self.writer.flush()
但是我得到了错误: InvalidArgumentError(请参见上面的回溯):您必须使用dtype float和形状[?,224,224,3]
输入占位符张量'input_1'的值。必须有一种方法可以查看输入的模型,对吗?
或者也许我应该尝试另一种调试方式?
答案 0 :(得分:1)
您不需要为此的回调。您需要做的就是实现一个生成图像及其标签为元组的函数。 flow_from_directory
函数具有一个名为save_to_dir
的参数,可以满足您的所有需求,如果不满足,您可以执行以下操作:
def trainGenerator(batch_size,train_path, image_size)
#preprocessing see https://keras.io/preprocessing/image/ for details
image_datagen = ImageDataGenerator(horizontal_flip=True)
#create image generator see https://keras.io/preprocessing/image/#flow_from_directory for details
train_generator = image_datagen.flow_from_directory(
train_path,
class_mode = "categorical",
target_size = image_size,
batch_size = batch_size,
save_prefix = "augmented_train",
seed = seed)
for (batch_imgs, batch_labels) in train_generator:
#do other stuff such as dumping images or further augmenting images
yield (batch_imgs,batch_labels)
t_generator = trainGenerator(32, "./train_data", (224,224,3))
model.fit_generator(t_generator,steps_per_epoch=10,epochs=1)