我正在重建DnCNN,即高斯降噪器,它用一系列卷积层进行图像到图像的预测。它训练完美,但当我尝试做列表(model.predict(..)),我得到错误:
标签不得为无
我实际上将我的EstimatorSpec的所有specs参数明确地放在那里,因为它们根据估算器调用的方法(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.
来自estimator.predict raises "ValueError: None values not supported":
“在你的model_fn中,你可以定义每种模式下的损失(训练/评估/预测)。这意味着即使在预测模式下,也会使用并需要提供标签。
当你处于预测模式时,你实际上只需要返回预测,这样你就可以从函数中提前返回:“
def model_fn(features, labels, mode):
#...
y = ...
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=y)
#...
我不确定确切的错误是什么,但我设法让我的模型预测。
我改变了什么(除了添加批量标准UPDATE_OPS
,它没有解决我的问题)在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发现的doc语句(或者我没有正确理解)似乎有问题:
model_fn可以独立于mode填充所有参数。在这种情况下,Estimator会忽略一些参数。例如。在eval和推断模式中将忽略train_op。
顺便说一句:鉴于mode
是预测的,在某些情况下,标签在任何情况下都会自动替换为None。