tensorflow / models

Models and examples built with TensorFlow
Other
77.01k stars 45.78k forks source link

Possible None check required in Dataset.map() #3424

Closed FarooqKhan closed 4 years ago

FarooqKhan commented 6 years ago

System information

Describe the problem

Am trying to do a prediction for this tutorial and building on the existing code in train_model.py.

To do this I am trying to reuse the get_input_fn() function for prediction. I think the inner function _parse_tfexample_fn() seems to be coded for use during prediction as well as it has few if conditions checking for mode == PREDICT

I am invoking it as follows:

predict_results = estimator.predict(input_fn=get_input_fn(
          mode=tf.estimator.ModeKeys.PREDICT,
          tfrecord_pattern=FLAGS.predict_temp_file,
          batch_size=FLAGS.batch_size))

_parse_tfexample_fn() returns a tuple of 'parsed_features, labels' and labels is None when mode == PREDICT However this then causes causes a exception as below which is due to the labels being None i think.

File "/Users/farooq/.virtualenvs/tensor1.0/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 486, in add_to_graph
    self._create_definition_if_needed()
  File "/Users/farooq/.virtualenvs/tensor1.0/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 321, in _create_definition_if_needed
    self._create_definition_if_needed_impl()
  File "/Users/farooq/.virtualenvs/tensor1.0/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 338, in _create_definition_if_needed_impl
    outputs = self._func(*inputs)
  File "/Users/farooq/.virtualenvs/tensor1.0/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1579, in tf_map_func
    ret, [t.get_shape() for t in nest.flatten(ret)])
  File "/Users/farooq/.virtualenvs/tensor1.0/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1579, in <listcomp>
    ret, [t.get_shape() for t in nest.flatten(ret)])
AttributeError: 'NoneType' object has no attribute 'get_shape'

Maybe the dataset.map() should check for None value before invoking get_shape() on it?

asimshankar commented 6 years ago

Thanks for the note.

I don't think we want dataset.map() to handle invalid tensors. Instead, we can probably make _parse_tf_example_fn return just the features and no labels in PREDICT mode. Specifically, change implementation of _parse_tf_example_fn from:

    labels = None
    if mode != tf.estimator.ModeKeys.PREDICT:
      labels = parsed_features["class_index"]
    parsed_features["ink"] = tf.sparse_tensor_to_dense(parsed_features["ink"])
    return parsed_features, labels

to:

    parsed_features["ink"] = tf.sparse_tensor_to_dense(parsed_features["ink"])
    if mode != tf.estimator.ModeKeys.PREDICT:
      labels = parsed_features["class_index"]
      return parsed_features, labels
    return parsed_features

Could you give that a spin? If that works, then we'd be more than happy to accept a pull request with that change. Thanks!

FarooqKhan commented 6 years ago

I totally agree with your reply, I have raised a PR 3440. I have tested this change and this part works fine. However, I do want to mention that I have been trying out predict and so far have not succeeded completely so cannot be 100% sure about this change.

tensorflowbutler commented 4 years ago

Hi There, We are checking to see if you still need help on this, as this seems to be considerably old issue. Please update this issue with the latest information, code snippet to reproduce your issue and error you are seeing. If we don't hear from you in the next 7 days, this issue will be closed automatically. If you don't need help on this issue any more, please consider closing this.