Tensorflow Estimator.predict() 失败

Tensorflow Estimator.predict() fails

我正在重新创建 DnCNN,即高斯降噪器,它使用一系列卷积层进行图像到图像的预测。它训练得很好,但是当我尝试执行列表时(model.predict(..)), 我收到错误:

Labels must not be none

我实际上将我的 EstimatorSpec 的所有规范参数明确地放在那里,因为它们是根据 Estimator 调用的方法 (train/eval/predict) 延迟计算的。

def DnCNN_model_fn (features, labels, mode):
   # some convolutinons here
   return tf.estimator.EstimatorSpec(
        mode=mode,
        predictions=conv_last + input_layer,
        loss=tf.losses.mean_squared_error(
            labels=labels, 
            predictions=conv_last + input_layer),
        train_op=tf.train.AdamOptimizer(learning_rate=0.001, epsilon=1e-08).minimize(
            loss=tf.losses.mean_squared_error(
                labels=labels,
                predictions=conv_last + input_layer),
            global_step=tf.train.get_global_step()),
        eval_metric_ops={
            "accuracy": tf.metrics.mean_absolute_error(
                labels=labels,
                predictions=conv_last + input_layer)}
      )

将其放入估算器中:

d = datetime.datetime.now()

DnCNN = tf.estimator.Estimator(
    model_fn=DnCNN_model_fn,
    model_dir=root + 'model/' +
              "DnCNN_{}_{}_{}_{}".format(d.month, d.day, d.hour, d.minute),
    config=tf.estimator.RunConfig(save_summary_steps=2,
                                  log_step_count_steps=10)
)

训练模型后,我按如下方式进行预测:

test_input_fn = tf.estimator.inputs.numpy_input_fn(
    x= test_data[0:2,:,:,:],
    y= None,
    batch_size=1,
    num_epochs=1,
    shuffle=False)

predicted = DnCNN.predict(input_fn=test_input_fn) 
list(predicted) # this is where the error occurs

回溯表明,tf.losses.mean_squared_error 导致了此问题。

    Traceback (most recent call last):
      File "<input>", line 16, in <module>
      File "...\venv2\lib\site-packages\tensorflow\python\estimator\estimator.py", line 551, in predict
        features, None, model_fn_lib.ModeKeys.PREDICT, self.config)
      File "...\venv2\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1169, in _call_model_fn
        model_fn_results = self._model_fn(features=features, **kwargs)
      File "<input>", line 95, in DnCNN_model_fn
      File "...\venv2\lib\site-packages\tensorflow\python\ops\losses\losses_impl.py", line 663, in mean_squared_error
        raise ValueError("labels must not be None.")
    ValueError: labels must not be None.

我不完全确定确切的错误是什么,但我设法让我的模型进行预测。

我改变的(除了添加 Batch norm UPDATE_OPS,这并没有解决我的问题)是短路(即早期和单独 return)中的 tf.estimator.EstimatorSpec tf.estimator.ModeKeys.PREDICT 的情况:

if mode == tf.estimator.ModeKeys.PREDICT:
    return tf.estimator.EstimatorSpec(
        mode=mode,
        predictions=conv_last + input_layer
    )

显然 tf.estimator.EstimatorSpec 上的文档声明似乎有问题(或者我没有正确理解):

model_fn can populate all arguments independent of mode. In this case, some arguments will be ignored by an Estimator. E.g. train_op will be ignored in eval and infer modes.

顺便说一句:假设 mode 是预测值,在某些情况下,标签会自动替换为 None。

来自

“在你的model_fn中,你定义了每种模式(训练/评估/预测)的损失。这意味着即使在预测模式下,标签也会被使用并且需要提供。

当您处于预测模式时,您实际上只需要 return 预测,这样您就可以 return 尽早从函数:"

def model_fn(features, labels, mode):
#...
y = ...
if mode == tf.estimator.ModeKeys.PREDICT:
    return tf.estimator.EstimatorSpec(mode=mode, predictions=y)
#...