Python 3.x AttributeError:模块“tensorflow.python.keras.api.\u v2.keras.backend”没有属性“set\u image\u dim\u ordering”

Python 3.x AttributeError:模块“tensorflow.python.keras.api.\u v2.keras.backend”没有属性“set\u image\u dim\u ordering”,python-3.x,tensorflow,keras,Python 3.x,Tensorflow,Keras,最近,我尝试使用python在github中运行代码。代码位于: 我使用了以下软件 TensorFlow-gpu-2.0-beta1 Keras:2.2.4 操作系统:Windows 10 python:3.5 CUDA:10.0 cuDNN:10.0 我在网上遇到了一些人已经提出的同样的问题。即: AttributeError:模块“tensorflow”没有属性“get\u default\u graph” 在我尝试更改代码后,如下所示 从keras导入后端 改为: 从tensorflow.

最近,我尝试使用python在github中运行代码。代码位于: 我使用了以下软件

TensorFlow-gpu-2.0-beta1 Keras:2.2.4 操作系统:Windows 10 python:3.5 CUDA:10.0 cuDNN:10.0

我在网上遇到了一些人已经提出的同样的问题。即: AttributeError:模块“tensorflow”没有属性“get\u default\u graph” 在我尝试更改代码后,如下所示

从keras导入后端

改为:

从tensorflow.keras导入后端

我遇到了另一个问题。即: AttributeError:模块“tensorflow.python.keras.api.\u v2.keras.backend”没有属性“set\u image\u dim\u ordering”

我不知道现在该怎么办

#Here are some codes which are related to the problem above.

import os
import keras
from tensorflow.keras import backend as K
from keras import objectives
from keras.layers import Input, merge
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import Convolution2D, Deconvolution2D
from keras.layers.core import Activation, Dropout
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras.optimizers import Adam

KERAS_2 = keras.__version__[0] == '2'
try:
    # keras 2 imports
    from keras.layers.convolutional import Conv2DTranspose
    from keras.layers.merge import Concatenate
except ImportError:
    print("keras 2 layers could not be imported defaulting to keras1")
    KERAS_2 = False

K.set_image_dim_ordering('th') #here is where the problem occurs at

#The first problem.
Traceback (most recent call last):
  File "C:\zzProject_ML\vess2ret-master\train.py", line 326, in <module>
    batch_size=params.batch_size, is_binary=params.is_b_binary)
  File "C:\zzProject_ML\vess2ret-master\models.py", line 378, in g_unet
    i = Input(shape=(in_ch, 512, 512))
  File "C:\Users\10580\Anaconda3\envs\project_ML\lib\site-packages\keras\engine\input_layer.py", line 178, in Input
    input_tensor=tensor)
  File "C:\Users\10580\Anaconda3\envs\project_ML\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\10580\Anaconda3\envs\project_ML\lib\site-packages\keras\engine\input_layer.py", line 39, in __init__
    name = prefix + '_' + str(K.get_uid(prefix))
  File "C:\Users\10580\Anaconda3\envs\project_ML\lib\site-packages\keras\backend\tensorflow_backend.py", line 74, in get_uid
    graph = tf.get_default_graph()
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

#The second problem.
Using TensorFlow backend.
Traceback (most recent call last):
  File "C:\zzProject_ML\vess2ret-master\train.py", line 7, in <module>
    import models as m
  File "C:\zzProject_ML\vess2ret-master\models.py", line 25, in <module>
    K.set_image_dim_ordering('th')
AttributeError: module 'tensorflow.python.keras.api._v2.keras.backend' has no attribute 'set_image_dim_ordering'
您在导入中混合了tf.keras和keras,它们不兼容,而且keras目前不支持tensorflow 2.0,尚未发布稳定版本


如果必须使用tensorflow 2.0,则必须使用该版本中包含的tf.keras。如果你想使用keras,那么你需要降级到一个稳定的tensorflow版本。

如果我的tensorflow 2.0.0-alpha0版本没有keras怎么办?@OmarCusmaFait你是什么意思,没有keras AFAIK就没有版本了。你可能会有一个坏版本。