datasets icon indicating copy to clipboard operation
datasets copied to clipboard

AttributeError: module 'tree' has no attribute 'map_structure'

Open rafadotnechi opened this issue 1 year ago • 3 comments

/!\ PLEASE INCLUDE THE FULL STACKTRACE AND CODE SNIPPET

Short description Description of the bug.

Environment information

  • Operating System: Windows 10

  • Python version: Python 3.12.4

  • tensorflow-datasets/tfds-nightly tensorflow-datasets / version: Version: 4.9.6

  • tensorflow/tf-nightly version: tensorflow / Version: 2.16.1

  • Does the issue still exists with the last tfds-nightly package (pip install --upgrade tfds-nightly) ?

Yes

Reproduction instructions load any dataset


import tensorflow_datasets as tfds
import tensorflow as tf

# Construct a tf.data.Dataset
ds = tfds.load('mnist', split='train', as_supervised=True, shuffle_files=True)

Stacktrace

Traceback (most recent call last):
  File "C:/Users/rafadotnechi/Documents/PythonDev/load_test.py", line 5, in <module>
    ds = tfds.load('mnist', split='train', as_supervised=True, shuffle_files=True)
  File "C:\Users\rafadotnechi\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow_datasets\core\logging\__init__.py", line 176, in __call__
    return function(*args, **kwargs)
  File "C:\Users\rafadotnechi\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow_datasets\core\load.py", line 647, in load
    _download_and_prepare_builder(dbuilder, download, download_and_prepare_kwargs)
  File "C:\Users\rafadotnechi\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow_datasets\core\load.py", line 506, in _download_and_prepare_builder
    dbuilder.download_and_prepare(**download_and_prepare_kwargs)
  File "C:\Users\rafadotnechi\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow_datasets\core\logging\__init__.py", line 176, in __call__
    return function(*args, **kwargs)
  File "C:\Users\rafadotnechi\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow_datasets\core\dataset_builder.py", line 699, in download_and_prepare
    self._download_and_prepare(
  File "C:\Users\rafadotnechi\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow_datasets\core\dataset_builder.py", line 1669, in _download_and_prepare
    split_infos = self._generate_splits(dl_manager, download_config)
  File "C:\Users\rafadotnechi\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow_datasets\core\dataset_builder.py", line 1620, in _generate_splits
    split_generators = self._split_generators(  # pylint: disable=unexpected-keyword-arg
  File "C:\Users\rafadotnechi\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow_datasets\image_classification\mnist.py", line 119, in _split_generators
    mnist_files = dl_manager.download_and_extract(
  File "C:\Users\rafadotnechi\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow_datasets\core\download\download_manager.py", line 694, in download_and_extract
    return _map_promise(self._download_extract, url_or_urls)
  File "C:\Users\rafadotnechi\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow_datasets\core\download\download_manager.py", line 790, in _map_promise
    all_promises = tree.map_structure(map_fn, all_inputs)  # Apply the function
  File "C:\Users\rafadotnechi\AppData\Local\Programs\Python\Python312\Lib\site-packages\etils\epy\lazy_imports_utils.py", line 109, in __getattr__
    return getattr(self._module, name)
AttributeError: module 'tree' has no attribute 'map_structure'

rafadotnechi avatar Jun 08 '24 06:06 rafadotnechi

The error message indicates that the tree module does not have the map_structure attribute. This could happen if tree is not properly installed or if there’s an incompatible version.

Here’s a step-by-step approach to help resolve this issue:

  1. Check Dependencies: Ensure that the tree module (which is part of dm-tree) is correctly installed. You can install or upgrade it with the following command: pip install -U dm-tree

  2. Check Version Compatibility: Some versions of TensorFlow and TensorFlow Datasets may require a specific version of dm-tree. If you’ve updated TensorFlow or TensorFlow Datasets recently, it’s possible that dm-tree also needs an update. Make sure all packages are compatible by running: pip install --upgrade tensorflow tensorflow-datasets dm-tree

  3. Reinstall TensorFlow Datasets and TensorFlow: In case the issue persists, try reinstalling both tensorflow and tensorflow-datasets. This can help resolve any hidden compatibility issues or corrupted installations: pip uninstall tensorflow tensorflow-datasets dm-tree pip install tensorflow tfds-nightly dm-tree

  4. Check Alternative Imports: If the above steps don't resolve the issue, you could check for alternative ways to load the dataset: import tensorflow as tf import tensorflow_datasets as tfds

try: ds = tfds.load('mnist', split='train', as_supervised=True, shuffle_files=True) except AttributeError as e: print(f"Error loading dataset: {e}")

  1. Restart the Environment: Sometimes, restarting your Python environment can help clear out any lingering issues.

srisridasari avatar Oct 14 '24 12:10 srisridasari

I have the exact problem. Turns out there is tree.py in the same folder. After delete it, then it works.

yodiaditya avatar Jan 05 '25 06:01 yodiaditya

I have the exact problem. Turns out there is tree.py in the same folder. After delete it, then it works. Hello, I encountered a similar problem. Could you provide more specific details. assert_same_structure = dm_tree.assert_same_structure AttributeError: module 'tree' has no attribute 'assert_same_structure'

ChenMinhee avatar Jan 15 '25 06:01 ChenMinhee