갈루아의 반서재

Tensorflow 2.0 환경에서 다음과 같은 에러가 발생하는 경우

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

(cooke) founder@casey:~/mandart/glass$ python run_keras_server.py
Using TensorFlow backend.
* Loading Keras model and Flask starting server...please wait until server has fully started
Traceback (most recent call last):
  File "run_keras_server.py", line 82, in <module>
    load_model()
  File "run_keras_server.py", line 27, in load_model
    model = ResNet50(weights="imagenet")
  File "/home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages/keras/applications/__init__.py", line 28, in wrapper
    return base_fun(*args, **kwargs)
  File "/home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages/keras/applications/resnet50.py", line 11, in ResNet50
    return resnet50.ResNet50(*args, **kwargs)
  File "/home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages/keras_applications/resnet50.py", line 214, in ResNet50
    img_input = layers.Input(shape=input_shape)
  File "/home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages/keras/engine/input_layer.py", line 178, in Input
    input_tensor=tensor)
  File "/home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages/keras/engine/input_layer.py", line 39, in __init__
    name = prefix + '_' + str(K.get_uid(prefix))
  File "/home/founder/anaconda3/envs/cooke/lib/python3.6/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'

설치된 텐서플로우 버전을 확인해보면 2.0.0a0 임을 알 수 있다.

(cooke) founder@casey:~/mandart/glass$ pip list
Package              Version
-------------------- --------------------

tb-nightly           1.14.0a20190301
tensorflow           2.0.0a0
termcolor            1.1.0
아래 스택오버플로우의 답변을 보면 텐서플로우어 버전에 관련된 문제로, 설치된 2.0을 제거하고 1.13.1 버전으로 재설치한다. 

https://stackoverflow.com/questions/55496289/how-to-fix-attributeerror-module-tensorflow-has-no-attribute-get-default-gr

2.0 버전은 다음과 같이 제거한다.

(cooke) founder@casey:~/mandart/glass$ pip uninstall tensorflow
Uninstalling tensorflow-2.0.0a0:
  Would remove:
    /home/founder/anaconda3/envs/cooke/bin/freeze_graph
    /home/founder/anaconda3/envs/cooke/bin/saved_model_cli
    /home/founder/anaconda3/envs/cooke/bin/tensorboard
    /home/founder/anaconda3/envs/cooke/bin/tf_upgrade_v2
    /home/founder/anaconda3/envs/cooke/bin/tflite_convert
    /home/founder/anaconda3/envs/cooke/bin/toco
    /home/founder/anaconda3/envs/cooke/bin/toco_from_protos
    /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages/tensorflow-2.0.0a0.dist-info/*
    /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages/tensorflow/*
Proceed (y/n)? y
  Successfully uninstalled tensorflow-2.0.0a0

1.13.1 버전을 설치한다.
(cooke) founder@casey:~/mandart/glass$ pip install tensorflow
Collecting tensorflow
  Downloading https://files.pythonhosted.org/packages/77/63/a9fa76de8dffe7455304c4ed635be4aa9c0bacef6e0633d87d5f54530c5c/tensorflow-1.13.1-cp36-4.whl (92.5MB)
    100% |████████████████████████████████| 92.5MB 400kB/s
Collecting tensorboard<1.14.0,>=1.13.0 (from tensorflow)
  Downloading https://files.pythonhosted.org/packages/0f/39/bdd75b08a6fba41f098b6cb091b9e8c7a80e1b4d679a581a0ccd17b10373/tensorboard-1.13.1-py3-
    100% |████████████████████████████████| 3.2MB 7.8MB/s
Requirement already satisfied: wheel>=0.26 in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from tensorflow) (0.33.1)
Requirement already satisfied: gast>=0.2.0 in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from tensorflow) (0.2.2)
Requirement already satisfied: numpy>=1.13.3 in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from tensorflow) (1.16.2)
Collecting tensorflow-estimator<1.14.0rc0,>=1.13.0 (from tensorflow)
  Downloading https://files.pythonhosted.org/packages/bb/48/13f49fc3fa0fdf916aa1419013bb8f2ad09674c275b4046d5ee669a46873/tensorflow_estimator-1.whl (367kB)
    100% |████████████████████████████████| 368kB 24.1MB/s
Requirement already satisfied: absl-py>=0.1.6 in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from tensorflow) (0.7.1)
Requirement already satisfied: grpcio>=1.8.6 in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from tensorflow) (1.19.0)
Requirement already satisfied: astor>=0.6.0 in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from tensorflow) (0.7.1)
Requirement already satisfied: keras-applications>=1.0.6 in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from tensorflow) (1.
Requirement already satisfied: protobuf>=3.6.1 in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from tensorflow) (3.7.1)
Requirement already satisfied: keras-preprocessing>=1.0.5 in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from tensorflow) (1
Requirement already satisfied: termcolor>=1.1.0 in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from tensorflow) (1.1.0)
Requirement already satisfied: six>=1.10.0 in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from tensorflow) (1.12.0)
Requirement already satisfied: werkzeug>=0.11.15 in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from tensorboard<1.14.0,>=1.5.2)
Requirement already satisfied: markdown>=2.6.8 in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from tensorboard<1.14.0,>=1.13
Collecting mock>=2.0.0 (from tensorflow-estimator<1.14.0rc0,>=1.13.0->tensorflow)
  Downloading https://files.pythonhosted.org/packages/e6/35/f187bdf23be87092bd0f1200d43d23076cee4d0dec109f195173fd3ebc79/mock-2.0.0-py2.py3-none
    100% |████████████████████████████████| 61kB 20.0MB/s
Requirement already satisfied: h5py in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from keras-applications>=1.0.6->tensorflo
Requirement already satisfied: setuptools in /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages (from protobuf>=3.6.1->tensorflow) (
Collecting pbr>=0.11 (from mock>=2.0.0->tensorflow-estimator<1.14.0rc0,>=1.13.0->tensorflow)
  Downloading https://files.pythonhosted.org/packages/14/09/12fe9a14237a6b7e0ba3a8d6fcf254bf4b10ec56a0185f73d651145e9222/pbr-5.1.3-py2.py3-none-
    100% |████████████████████████████████| 112kB 24.0MB/s
Installing collected packages: tensorboard, pbr, mock, tensorflow-estimator, tensorflow
Successfully installed mock-2.0.0 pbr-5.1.3 tensorboard-1.13.1 tensorflow-1.13.1 tensorflow-estimator-1.13.0
1.13.1 버전이 설치되었음을 확인할 수 있다.
(cooke) founder@casey:~/mandart/glass$ conda list
# packages in environment at /home/founder/anaconda3/envs/cooke:
#
# Name                    Version                   Build  Channel

tensorboard               1.13.1                    <pip>
tensorflow                1.13.1                    <pip>
tensorflow-estimator      1.13.0                    <pip>
서비스를 구동해보면 정상 작동함을 알 수 있다.
(cooke) founder@casey:~/mandart/glass$ python run_keras_server.py
Using TensorFlow backend.
* Loading Keras model and Flask starting server...please wait until server has fully started
WARNING:tensorflow:From /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
2019-04-26 06:56:20.699703: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2500000000 Hz
2019-04-26 06:56:20.700030: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x557acd654f70 executing computations on platform Host. Devices:
2019-04-26 06:56:20.700120: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): <undefined>, <undefined>
 * Serving Flask app "run_keras_server" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
Using TensorFlow backend.
* Loading Keras model and Flask starting server...please wait until server has fully started
WARNING:tensorflow:From /home/founder/anaconda3/envs/cooke/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
2019-04-26 06:56:31.642200: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2500000000 Hz
2019-04-26 06:56:31.642530: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x55661a9e2070 executing computations on platform Host. Devices:
2019-04-26 06:56:31.642620: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): <undefined>, <undefined>
 * Debugger is active!
 * Debugger PIN: 123-456-789