tensorflow / datasets

TFDS is a collection of datasets ready to use with TensorFlow, Jax, ...
https://www.tensorflow.org/datasets
Apache License 2.0
4.27k stars 1.53k forks source link

'tfds.as_numpy' throws 'You must feed a value for placeholder tensor 'args_0' with dtype uint8 ...' error #5515

Closed yCobanoglu closed 2 weeks ago

yCobanoglu commented 1 month ago

Short description tfds.as_numpy not working correctly.

Additional context After following the flax tutorial in step 2 the mnist data is loaded. It requires you to install tensorflow just to use a single function, namely tf.cast. I tried to get rid of the tensorflow dependency and stumbled upon this bug.

Environment information

Problem exists with tfds-nightly version.

Reproduction instructions

import tensorflow_datasets as tfds
import tensorflow as tf
import numpy as np

def preprocess(sample):
  image = tfds.as_numpy(sample["image"])
  return {'image': image, 'label': sample['label']}

train_ds = tfds.load('mnist', split='train')

#this code errors
train_ds = train_ds.map(preprocess) 

# this works fine
a = np.random.rand(4, 10, 10, 3)
a = tf.convert_to_tensor(a)
tfds.as_numpy(a)

Error

/home/yunus/PycharmProjects/DeepLearning1/.venvJax/bin/python /home/yunus/PycharmProjects/DeepLearning1/src/autoencoder/debug.py 
2024-07-12 16:23:19.641609: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-07-12 16:23:19.656945: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-07-12 16:23:19.661552: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-07-12 16:23:19.672850: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-07-12 16:23:20.278840: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1720794201.270601   22409 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-07-12 16:23:21.294972: W tensorflow/core/common_runtime/gpu/gpu_device.cc:2343] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2024-07-12 16:23:21.444044: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:388] MLIR V1 optimization pass is not enabled
2024-07-12 16:23:21.444998: I tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: INVALID_ARGUMENT: You must feed a value for placeholder tensor 'args_0' with dtype uint8 and shape [28,28,1]
     [[{{node args_0}}]]
Traceback (most recent call last):
  File "/home/yunus/PycharmProjects/DeepLearning1/src/autoencoder/debug.py", line 11, in <module>
    train_ds = train_ds.map(preprocess)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 2311, in map
    return map_op._map_v2(
           ^^^^^^^^^^^^^^^
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/data/ops/map_op.py", line 37, in _map_v2
    return _MapDataset(
           ^^^^^^^^^^^^
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/data/ops/map_op.py", line 107, in __init__
    self._map_func = structured_function.StructuredFunctionWrapper(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/data/ops/structured_function.py", line 265, in __init__
    self._function = fn_factory()
                     ^^^^^^^^^^^^
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 1251, in get_concrete_function
    concrete = self._get_concrete_function_garbage_collected(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 1221, in _get_concrete_function_garbage_collected
    self._initialize(args, kwargs, add_initializers_to=initializers)
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 696, in _initialize
    self._concrete_variable_creation_fn = tracing_compilation.trace_function(
                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py", line 178, in trace_function
    concrete_function = _maybe_define_function(
                        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py", line 283, in _maybe_define_function
    concrete_function = _create_concrete_function(
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py", line 310, in _create_concrete_function
    traced_func_graph = func_graph_module.func_graph_from_py_func(
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/framework/func_graph.py", line 1059, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 599, in wrapped_fn
    out = weak_wrapped_fn().__wrapped__(*args, **kwds)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/data/ops/structured_function.py", line 231, in wrapped_fn
    ret = wrapper_helper(*args)
          ^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/data/ops/structured_function.py", line 161, in wrapper_helper
    ret = autograph.tf_convert(self._func, ag_ctx)(*nested_args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/autograph/impl/api.py", line 693, in wrapper
    raise e.ag_error_metadata.to_exception(e)
tensorflow.python.framework.errors_impl.InvalidArgumentError: in user code:

    File "/home/yunus/PycharmProjects/DeepLearning1/src/autoencoder/debug.py", line 6, in preprocess  *
        image = tfds.as_numpy(sample["image"])
    File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow_datasets/core/logging/__init__.py", line 378, in decorator  **
        return function(*args, **kwargs)
    File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow_datasets/core/dataset_utils.py", line 202, in as_numpy
        return _nested_to_numpy_graph(dataset)
    File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow_datasets/core/dataset_utils.py", line 150, in _nested_to_numpy_graph
        all_arrays = sess.run(all_arrays)

    InvalidArgumentError: Graph execution error:

    Detected at node 'args_0' defined at (most recent call last):
        File "/home/yunus/PycharmProjects/DeepLearning1/src/autoencoder/debug.py", line 11, in <module>
        File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/core/function/polymorphism/function_type.py", line 356, in placeholder_arguments
    Node: 'args_0'
    You must feed a value for placeholder tensor 'args_0' with dtype uint8 and shape [28,28,1]
         [[{{node args_0}}]]

    Original stack trace for 'args_0':
      File "/home/yunus/PycharmProjects/DeepLearning1/src/autoencoder/debug.py", line 11, in <module>
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 2311, in map
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/data/ops/map_op.py", line 37, in _map_v2
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/data/ops/map_op.py", line 107, in __init__
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/data/ops/structured_function.py", line 265, in __init__
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 1251, in get_concrete_function
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 1221, in _get_concrete_function_garbage_collected
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 696, in _initialize
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py", line 178, in trace_function
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py", line 283, in _maybe_define_function
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py", line 303, in _create_concrete_function
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/core/function/polymorphism/function_type.py", line 356, in placeholder_arguments
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/framework/tensor.py", line 1021, in placeholder_value
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/framework/tensor.py", line 1059, in _graph_placeholder
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/framework/func_graph.py", line 670, in _create_op_internal
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/framework/ops.py", line 2682, in _create_op_internal
      File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/framework/ops.py", line 1177, in from_node_def

Original stack trace for 'args_0':
  File "/home/yunus/PycharmProjects/DeepLearning1/src/autoencoder/debug.py", line 11, in <module>
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 2311, in map
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/data/ops/map_op.py", line 37, in _map_v2
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/data/ops/map_op.py", line 107, in __init__
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/data/ops/structured_function.py", line 265, in __init__
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 1251, in get_concrete_function
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 1221, in _get_concrete_function_garbage_collected
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 696, in _initialize
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py", line 178, in trace_function
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py", line 283, in _maybe_define_function
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py", line 303, in _create_concrete_function
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/core/function/polymorphism/function_type.py", line 356, in placeholder_arguments
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/framework/tensor.py", line 1021, in placeholder_value
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/framework/tensor.py", line 1059, in _graph_placeholder
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/framework/func_graph.py", line 670, in _create_op_internal
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/framework/ops.py", line 2682, in _create_op_internal
  File "/home/yunus/PycharmProjects/DeepLearning1/.venvJax/lib/python3.12/site-packages/tensorflow/python/framework/ops.py", line 1177, in from_node_def
yCobanoglu commented 1 month ago

I also have a problem with the following code:

import tensorflow_datasets as tfds  # TFDS for MNIST
import tensorflow as tf             # TensorFlow operations

def process_sample(sample):
  image, label = sample['image'], sample['label']
  image = tf.cast(image, tf.float32) / 255.
  image = image.numpy()
  return {'image': image, 'label': label}

def get_mnist(num_epochs, batch_size):
  """Load MNIST train and test datasets into memory."""
  train_ds = tfds.load('mnist', split='train')
  test_ds = tfds.load('mnist', split='test')

  train_ds = train_ds.map(process_sample)
  test_ds = test_ds.map(process_sample)
  # create shuffled dataset by allocating a buffer size of 1024 to randomly draw elements from
  train_ds = train_ds.repeat(num_epochs).shuffle(1024)
  # group into batches of batch_size and skip incomplete batch, prefetch the next sample to improve latency
  train_ds = train_ds.batch(batch_size, drop_remainder=True).prefetch(1)
  # create shuffled dataset by allocating a buffer size of 1024 to randomly draw elements from
  test_ds = test_ds.shuffle(1024)
  # group into batches of batch_size and skip incomplete batch, prefetch the next sample to improve latency
  test_ds = test_ds.batch(batch_size, drop_remainder=True).prefetch(1)

  return train_ds, test_ds

throws:

File "/home/yunus/PycharmProjects/DeepLearning1/src/datasets.py", line 7, in process_sample  *
        image = image.numpy()

    AttributeError: 'SymbolicTensor' object has no attribute 'numpy'
ccl-core commented 1 month ago

Hi @yCobanoglu , thanks for contacting us and sorry for the late reply.

tfds.as_numpy can't be called on a SymbolicTensor directly...

Have you tried following the function's docstring with something like:

train_ds = tfds.load('mnist', split='train')
train_ds = tfds.as_numpy(train_ds)
ccl-core commented 2 weeks ago

Hi @yCobanoglu , I assume the suggestion above worked? I'm closing the issue for now, feel free to reopen it if you encounter any further problem!