Search code examples
predicttensorflow-estimator

Tensorflow Estimator.predict() fails


I am recreating the DnCNN, i.e. Gaussian Denoiser, which does image to image prediction with a series of convolutional layers. And it trains perfectly fine, but when i try to do the list(model.predict(..)), i get the error:

Labels must not be none

I actually put all of the specs arguments of my EstimatorSpec explicitly in there, as they are lazily evaluated depending on the method (train/eval/predict) that is called upon the Estimator.

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)}
      )

Putting it into an estimator:

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)
)

After training the model i do the predictions as follows:

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

The traceback says, that tf.losses.mean_squared_error is causing this.

    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.

Solution

  • From estimator.predict raises "ValueError: None values not supported":

    "In your model_fn, you define the loss in every mode (train / eval / predict). This means that even in predict mode, the labels will be used and need to be provided.

    When you are in predict mode, you actually just need to return the predictions so you can return early from the function:"

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