AttributeError:模块“ tensorflow.python.keras.api._v2.keras.backend”没有属性“ set_image_dim_ordering”

时间:2019-07-02 11:54:38

标签: python-3.x tensorflow keras

最近,我尝试使用python在github中运行代码。该代码位于:https://github.com/costapt/vess2ret 我使用了以下软件

TensorFlow-gpu-2.0-beta1 凯拉斯:2.2.4 作业系统:Windows 10 的Python:3.5 CUDA:10.0 cuDNN:10.0

我遇到了网上一些人已经表现出的同样问题。那是: AttributeError:模块“ tensorflow”没有属性“ get_default_graph” 在尝试将代码更改如下之后

从keras导入后端

更改为:

从tensorflow.keras导入后端

我遇到了另一个问题。那是: AttributeError:模块'tensorflow.python.keras.api._v2.keras.backend'没有属性'set_image_dim_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'

1 个答案:

答案 0 :(得分:0)

您正在导入中混用tf.keraskeras(它们不兼容),并且keras当前不支持tensorflow 2.0(尚未发布稳定版本)。

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