How to fix AttributeError: module 'tensorflow' has no attribute 'space_to_depth'

How to fix AttributeError: module 'tensorflow' has no attribute 'space_to_depth'

我在 Windows 10 x64 上安装了 python 3.7.4 和 tensorflow 版本 2.2.0。 我正在尝试执行此操作:

yolo_model = load_model("model_data/yolo.h5")

它给出了提到的错误

这是堆栈跟踪:

Traceback (most recent call last):

 File "Object Detection.py", line 78, in <module>
    yolo_model = load_model("model_data/yolo.h5")
  File "E:\Python37\lib\site-packages\keras\engine\saving.py", line 492, in load_wrapper
    return load_function(*args, **kwargs)
  File "E:\Python37\lib\site-packages\keras\engine\saving.py", line 584, in load_model
    model = _deserialize_model(h5dict, custom_objects, compile)
  File "E:\Python37\lib\site-packages\keras\engine\saving.py", line 274, in _deserialize_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "E:\Python37\lib\site-packages\keras\engine\saving.py", line 627, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "E:\Python37\lib\site-packages\keras\layers\__init__.py", line 168, in deserialize
    printable_module_name='layer')
  File "E:\Python37\lib\site-packages\keras\utils\generic_utils.py", line 147, in deserialize_keras_object
    list(custom_objects.items())))
  File "E:\Python37\lib\site-packages\keras\engine\network.py", line 1075, in from_config
    process_node(layer, node_data)
  File "E:\Python37\lib\site-packages\keras\engine\network.py", line 1025, in process_node
    layer(unpack_singleton(input_tensors), **kwargs)
  File "E:\Python37\lib\site-packages\keras\backend\tensorflow_backend.py", line 75, in symbolic_fn_wrapper
    return func(*args, **kwargs)
  File "E:\Python37\lib\site-packages\keras\engine\base_layer.py", line 489, in __call__
    output = self.call(inputs, **kwargs)
  File "E:\Python37\lib\site-packages\keras\layers\core.py", line 716, in call
    return self.function(inputs, **arguments)
  File "/Users/kian/Desktop/floydhub/yolo-03-oct/YAD2K/yad2k/models/keras_yolo.py", line 32, in space_to_depth_x2
AttributeError: module 'tensorflow' has no attribute 'space_to_depth'

好像是tensorflow版本和keras版本冲突。将 tensorflow 降级到 1.14.0 和 keras 降级到 2.3.1 修复问题

我认为模块的位置不同,仅供参考

  • 张量流 1.x : tensorflow.space_to_depth
  • 张量流 2.x : tensorflow.nn.space_to_depth

认为模块的位置不同,仅供参考:

  • tensorflow 1.x: tensorflow.space_to_depth
  • 张量流 2.: tensorflow.nn.space_to_depth

是的,它起作用了。