Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改验证步骤的Keras模型_Python_Tensorflow_Keras - Fatal编程技术网

Python 更改验证步骤的Keras模型

Python 更改验证步骤的Keras模型,python,tensorflow,keras,Python,Tensorflow,Keras,我有一个在训练和推理过程中不同的模型。更准确地说,它是一个SSD(单激发检测器),需要在其训练对应物的顶部添加额外的DetectionOutput层。在Caffe中,可以使用图层定义中的“include”参数打开/关闭图层 但是,如果我希望在每个历元之后(在回调中)运行验证,那么在定义和编译模型之后应该怎么做 我无法在培训期间添加DetectionOutput,因为它与loss的输入不兼容 我还希望避免在回调或自定义度量中的某个位置创建DetectionOutput层,因为它需要合理的超参数,并

我有一个在训练和推理过程中不同的模型。更准确地说,它是一个SSD(单激发检测器),需要在其训练对应物的顶部添加额外的DetectionOutput层。在Caffe中,可以使用图层定义中的“include”参数打开/关闭图层

但是,如果我希望在每个历元之后(在回调中)运行验证,那么在定义和编译模型之后应该怎么做

我无法在培训期间添加DetectionOutput,因为它与loss的输入不兼容

我还希望避免在回调或自定义度量中的某个位置创建DetectionOutput层,因为它需要合理的超参数,并且我希望将模型创建逻辑保留在专用模块中

在下面的示例中,创建了用于推断的代码模型,其中存在DetectionOutput层。因此,评估运行良好:

model, _, _ = build_model(input_shape=(args.input_height, args.input_width, 3),
                          n_classes=num_classes,
                          mode='inference')
model.load_weights(args.model, by_name=True)

evaluation = SSDEvaluation(model=model,
                           evaluator=PascalDetectionEvaluator(categories),
                           data_files=[args.eval_data])
metrics = evaluation.evaluate()
但此回调无法正常工作,因为在训练期间,模型没有DetectionOutput:

class SSDTensorboard(Callback):
    def __init__(self, evaluator, eval_data):
        self.evaluator = evaluator
        self.eval_data = eval_data

    def on_train_begin(self, logs={}):
        self.metrics = []

    def on_epoch_end(self, epoch, logs={}):
        evaluation = SSDEvaluation(self.model, self.evaluator, self.eval_data)
        metrics = evaluation.evaluate()
        self.metrics.append(metrics)

正常进行训练,但在相同重量的修改模型上执行验证步骤的正确方法(肾盂、角膜等)是什么?也许,有一个单独的模型来验证共享权重

您应该使用headless(无检测输出)模型进行培训,但提供一个顶层模型用于评估:

def add_detection_output(model):
    # make validation/inference model here

...
evaluation = SSDEvaluation(model=add_detection_output(model),
                           evaluator=PascalDetectionEvaluator(categories),
                           data_files=[args.eval_data])
避免在回调中使用培训模型,让评估对象保留对验证模型的引用:

class SSDTensorboard(Callback):
    def __init__(self, evaluation):
        self.evaluation = evaluation

    def on_epoch_end(self, epoch, logs={}):
        metrics = self.evaluation.evaluate()