从自定义回调中获取Keras模型输入

Dmy*_*pko 6 python keras tensorflow

我有一个非常简单的问题.我有一个为分类定义的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()
Run Code Online (Sandbox Code Playgroud)

但我收到错误:InvalidArgumentError(请参见上面的回溯):您必须为占位符张量'input_1'提供一个值,其中dtype为float和shape [?,224,224,3]

必须有办法看看哪些模型,作为输入,对吧?

或许我应该尝试另一种方式来调试它?

小智 2

您不需要为此回调。您需要做的就是实现一个函数,该函数生成图像及其标签作为元组。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)
Run Code Online (Sandbox Code Playgroud)