Search code examples
python-3.xtensorflowkeras

AttributeError: module 'tensorflow.python.keras.api._v2.keras.backend' has no attribute 'set_image_dim_ordering'


Recently, I tried to use python to run a code in github. The code is located in: https://github.com/costapt/vess2ret and I used the following softwares

TensorFlow-gpu-2.0-beta1 Keras :2.2.4 OS:Windows 10 python:3.5 CUDA:10.0 cuDNN:10.0

And I met the same problem already showed by some guys online. That is: AttributeError: module 'tensorflow' has no attribute 'get_default_graph' After I tried to change the code as the following

from keras import backend

change to:

from tensorflow.keras import backend

I met another problem. That is: AttributeError: module 'tensorflow.python.keras.api._v2.keras.backend' has no attribute 'set_image_dim_ordering'

And I don't know what to do now

#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'

Solution

  • You are mixing tf.keras and keras in your imports (they aren't compatible), and keras does not currently support tensorflow 2.0 (no stable version has been released).

    If you have to use tensorflow 2.0, then you have to use tf.keras included in that version. If you want to use keras, then you need to downgrade to a stable tensorflow version.