code: def _nested_to_numpy_graph(
      ds_nested: Tree[TensorflowElem],
  ) -> Tree[NumpyElem]:
      """Convert the nested structure of TF element to numpy."""
      all_ds = []
      all_arrays = []
      flat_ds = tf.nest.flatten(ds_nested)
      for elem in flat_ds:
          # Create an iterator for all datasets
          if isinstance(elem, tf.data.Dataset):
              # Capture the current graph, so calling `iter(ds)` twice will reuse the
              # graph in which `as_numpy` was created.
              graph = tf.compat.v1.get_default_graph()
              ds_iter = tf.compat.v1.data.make_initializable_iterator(
                  elem
              )
              all_ds.append(
                  _IterableDataset(
                      _graph_dataset_iterator, ds_iter, graph
                  )
              )
          else:
              all_arrays.append(elem)
  
      # Then create numpy arrays for all tensors
      if all_arrays:
          with utils.nogpu_session() as sess:  # Shared session for tf.Tensor
              all_arrays = sess.run(all_arrays)
  
      # Merge the dataset iterators and np arrays
      iter_ds = iter(all_ds)
      iter_array = iter(all_arrays)
      return tf.nest.pack_sequence_as(
          ds_nested,
          [
              next(iter_ds)
              if isinstance(ds_el, tf.data.Dataset)
              else next(iter_array)
              for ds_el in flat_ds
          ],
      )
  
imports: from __future__ import annotations
  import collections.abc
  import functools
  import typing
  from typing import Any, Callable, Iterable, Iterator, Union
  from etils import enp
  from etils.etree import nest
  import numpy
  from tensorflow_datasets.core import logging
  from tensorflow_datasets.core import utils
  from tensorflow_datasets.core.utils import tree_utils
  from tensorflow_datasets.core.utils import type_utils
  from tensorflow_datasets.core.utils.lazy_imports_utils import tensorflow
label_desc: Use image node_id 5 for calling a global function with example usage: _nested_to_numpy_graph(ds_nested) and returns: tf