keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
62.14k stars 19.49k forks source link

ImportError: cannot import name 'multi_gpu_model' #14440

Closed mikuchels closed 3 years ago

mikuchels commented 3 years ago

2021-02-07 14:36:35.290924: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.10.1 Traceback (most recent call last): File "Detector.py", line 21, in from keras_yolo3.yolo import YOLO File "/content/drive/My Drive/TrainYourOwnYOLO/2_Training/src/keras_yolo3/yolo.py", line 19, in from keras.utils import multi_gpu_model ImportError: cannot import name 'multi_gpu_model'

maximli commented 3 years ago

me too

balijepalli commented 3 years ago

any work around for this issue

mensly commented 3 years ago

I'm encountering this issue on both my local machine and in a colab.research.google.com notebook

mensly commented 3 years ago

Here is a notebook that reproduces the error https://colab.research.google.com/drive/1k-dz6lBFAd560_cNiUFtiQqSKIv8yJv9?usp=sharing

abstractdonut commented 3 years ago

I obtain the same error when trying to import keras.utils.multi_gpu_model. Using Ubuntu 20.04.2 LTS.

$ pip3 show tensorflow-gpu
Name: tensorflow-gpu
Version: 2.4.1
...
$ pip3 show keras
Name: Keras
Version: 2.4.3
...
$ python3
>>> from keras.utils import multi_gpu_model
2021-03-18 14:47:01.810271: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
2021-03-18 14:47:01.811609: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'multi_gpu_model' from 'keras.utils' (/home/abstract/.local/lib/python3.8/site-packages/keras/utils/__init__.py)
sellenth commented 3 years ago

Same problem as above

Ubuntu - 20.04 Keras - 2.4.3 keras-utils - 1.0.13 tensorflow-gpu - 2.4.1

Hoping someone finds a fix!

227BaronChen commented 3 years ago

I obtain the same error when trying to import keras.utils.multi_gpu_model. Using Ubuntu 20.04.2 LTS.

$ pip3 show tensorflow-gpu
Name: tensorflow-gpu
Version: 2.4.1
...
$ pip3 show keras
Name: Keras
Version: 2.4.3
...
$ python3
>>> from keras.utils import multi_gpu_model
2021-03-18 14:47:01.810271: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
2021-03-18 14:47:01.811609: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'multi_gpu_model' from 'keras.utils' (/home/abstract/.local/lib/python3.8/site-packages/keras/utils/__init__.py)

I can import keras and keras.utils successfully but encountered the same error, hoping someone finds a fix!!!

philnguyenresson commented 3 years ago

I get the same thing with the normal keras repo (though it looks like it's been moved to from keras.utils.multi_gpu_utils import multi_gpu_model Though, importing this function also gives a similar error, from the tensorflow-keras ImportError: cannot import name 'multi_gpu_model' from 'tensorflow.keras.utils'

tensorflow==2.4.1, Keras==2.4.3

larsyngvelundin commented 3 years ago

I had a similar error with from tensorflow.keras.utils import multi_gpu_model ImportError: cannot import name 'multi_gpu_model' from 'tensorflow.keras.utils'

After checking I found that I only had multi_gpu_model in keras.utils, NOT tensorflow.keras.utils.

Changed to: from keras.utils import multi_gpu_model and I was able to import and run my code.

Check that the module you're trying to import from actually has multi_gpu_model, might be a different location if the code uses older versions of TF and Keras.

sellenth commented 3 years ago

I ended up solving my issue by using an anaconda virtual environment and then doing something like pip3 install -r requirements.txt

I assumed the problem was with Keras itself but it was just dependency conflicts in my project folder.

misterspook commented 3 years ago

Same error.

Ubuntu - 20.04 Keras - 2.4.3 keras-utils - 1.0.13 tensorflow-gpu - 2.4.1

xujiameng commented 3 years ago

Same problem as above

Ubuntu - 20.04 Keras - 2.4.3 keras-utils - 1.0.13 tensorflow-gpu - 2.4.1 CUDA 11.0

misterspook commented 3 years ago

I have solved my problem by using:

with strategy.scope():
    ...model design, model compile...

The model is written with Keras but this strategy.scope() comes from tensorflow.

clemkofi commented 3 years ago

I realised that this issue has to do with the version of Keras that is used within Tensorflow. I encountered the same problem in Colab when using version 2.4.3.

so my work around was to uninstall Keras 2.4.3 like so: !pip uninstall Keras

and then installing a slightly older version Keras 2.2.4: !pip install keras==2.2.4

This solved the problem problem for me

NB: You might need to restart the runtime for this to take effect

kyle-sama commented 3 years ago

I realised that this issue has to do with the version of Keras that is used within Tensorflow. I encountered the same problem in Colab when using version 2.4.3.

so my work around was to uninstall Keras 2.4.3 like so: !pip uninstall Keras

and then installing a slightly older version Keras 2.2.4: !pip install keras==2.2.4

This solved the problem problem for me

NB: You might need to restart the runtime for this to take effect

I tried this version but then I get an error: AttributeError: module 'tensorflow' has no attribute 'get_default_session' and if I try Keras==2.3.0 and Tensorflow==2.3.2 I get 'tensorflow._api.v2.config' has no attribute 'experimental_list_devices'

With Tensorflow 2.5.0 however, this line seems to work: from tensorflow.python.keras.utils.multi_gpu_utils import multi_gpu_model

Adalsteinnjons commented 3 years ago

This function is deprecated and was removed on 2020-04-01. You can use tf.distribute.MirroredStrategy instead.

netpoetica commented 3 years ago

I have tried some of the solutions posted here but no dice for me - is there a known solution for this? I am unfortunately on Windows, so the env is a bit painful. But I installed the most recent version of Python (3.9) and then installed textgenrnn via pip, and then just trying to run this basic example:

from textgenrnn import textgenrnn

textgen = textgenrnn()
textgen.generate()

and I get this

$ python generator.py
Traceback (most recent call last):
  File "C:\Users\keithl\repos\project\generator.py", line 1, in <module>
    from textgenrnn import textgenrnn
  File "C:\Users\keithl\AppData\Roaming\Python\Python39\site-packages\textgenrnn\__init__.py", line 1, in <module>
    from .textgenrnn import textgenrnn
  File "C:\Users\keithl\AppData\Roaming\Python\Python39\site-packages\textgenrnn\textgenrnn.py", line 5, in <module>
    from tensorflow.keras.utils import multi_gpu_model
ImportError: cannot import name 'multi_gpu_model' from 'tensorflow.keras.utils' (C:\Users\keithl\AppData\Roaming\Python\Python39\site-packages\keras\api\_v2\keras\utils\__init__.py)
$ python --version
Python 3.9.7
netpoetica commented 3 years ago

I was able to get it to run by modifying textgenrnn.py in two ways:

from tensorflow.keras.preprocessing.text import Tokenizer, text_to_word_sequence

# from tensorflow.keras.utils import multi_gpu_model
# from keras.utils import multi_gpu_model

from tensorflow.keras.optimizers import Adam
from tensorflow.keras import backend as K
from tensorflow import config as config
from sklearn.preprocessing import LabelBinarizer
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from sklearn.metrics.pairwise import cosine_similarity
import tensorflow as tf

# from keras.backend.tensorflow_backend import set_session
from keras.backend import set_session

Using change from this SO answer: https://stackoverflow.com/a/69006914/802397 and getting rid of the import for multi_gpu_model

Aillian commented 3 years ago

i was able to solve this issue by going to textgenrnn.py file and change from tensorflow.python.keras.utils import multi_gpu_model line to from tensorflow.python.keras.utils.multi_gpu_utils import multi_gpu_model i guess newer version of tensorflow/keras requires that.

jacobggman commented 3 years ago

The solution for me was using an old version of Keras (Version 2.0.0)

mcarletti commented 3 years ago

Same problem but solved by checking the TF version.

When I import multi_gpu_model from keras utils, I get the following error message:

ImportError: cannot import name 'multi_gpu_model'

I've solved the issue by checking the TF version:

if tf.__version__ == "2.4.0":
    from tensorflow.python.keras.utils.multi_gpu_utils import multi_gpu_model
else:
    from tensorflow.keras.utils import multi_gpu_model

Environment

sushreebarsa commented 3 years ago

@mikuchels Could you please try on the latest stable version of TF 2.6 and let us know if this issue still persists ? Please try to usefrom tensorflow.python.keras.utils.multi_gpu_utils import multi_gpu_model instead of using from tensorflow.keras.utils import multi_gpu_model .Thank you!

google-ml-butler[bot] commented 3 years ago

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.

google-ml-butler[bot] commented 3 years ago

Closing as stale. Please reopen if you'd like to work on this further.

jinzhuer commented 2 years ago

It seems there is a new way to use multi GPU to train in the latest keras. A code example I found https://keras.io/guides/distributed_training/

strategy = tf.distribute.MirroredStrategy()

print('Number of devices: {}'.format(strategy.num_replicas_in_sync))

with strategy.scope():

    model = Model(...)

    model.compile(...)
HassanBinHaroon commented 2 years ago

I realised that this issue has to do with the version of Keras that is used within Tensorflow. I encountered the same problem in Colab when using version 2.4.3.

so my work around was to uninstall Keras 2.4.3 like so: !pip uninstall Keras

and then installing a slightly older version Keras 2.2.4: !pip install keras==2.2.4

This solved the problem problem for me

NB: You might need to restart the runtime for this to take effect

Worked for me. Thanks!

jinzhidajia commented 1 year ago

Plz tell me the version of ur tensor-flow. It's said that my tensor-flow is not compatible with Keras. thanks!!

HassanBinHaroon commented 1 year ago

Please remove this line and try running without multi_gpu_model. It has nothing to do with multi gpu.

As far as I know, it will always give error if you keep this line in your code.

On Thu, 16 Feb 2023, 1:05 am jinzhidajia, @.***> wrote:

Plz tell me the version of ur tensor-flow. It's said that my tensor-flow is not compatible with Keras. thanks!!

— Reply to this email directly, view it on GitHub https://github.com/keras-team/keras/issues/14440#issuecomment-1431960112, or unsubscribe https://github.com/notifications/unsubscribe-auth/AY2GYVTHJO53WZ7ECA4S4K3WXUZKJANCNFSM4XHQKUTQ . You are receiving this because you commented.Message ID: @.***>

jinzhidajia commented 1 year ago

Thanks for your reply!!!!! But Because multi is referenced in the following code  if i remove this line

should I remove this too?

Looking forward to your reply!

------------------ 原始邮件 ------------------ 发件人: "keras-team/keras" @.>; 发送时间: 2023年2月16日(星期四) 中午12:08 @.>; @.**@.>; 主题: Re: [keras-team/keras] ImportError: cannot import name 'multi_gpu_model' (#14440)

Please remove this line and try running without multi_gpu_model. It has nothing to do with multi gpu.

As far as I know, it will always give error if you keep this line in your code.

On Thu, 16 Feb 2023, 1:05 am jinzhidajia, @.***> wrote:

> Plz tell me the version of ur tensor-flow. > It's said that my tensor-flow is not compatible with Keras. thanks!! > > — > Reply to this email directly, view it on GitHub > <https://github.com/keras-team/keras/issues/14440#issuecomment-1431960112&gt;, > or unsubscribe > <https://github.com/notifications/unsubscribe-auth/AY2GYVTHJO53WZ7ECA4S4K3WXUZKJANCNFSM4XHQKUTQ&gt; > . > You are receiving this because you commented.Message ID: > @.***> >

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

HassanBinHaroon commented 1 year ago

I think multi_gpu_model tries to speed up the training. If you do not need that training speed then I suggest you to remove multi_gpu_model entirely from your code. Try to run the code without this method/package.

jinzhidajia commented 1 year ago

thanks for ur reply!!!!! But I find another solution that replace “from keras.utils import multi_gpu_model” with “from keras.utils.multi_gpu_model import multi_gpu_model” It finally works on! Hope it also works on u!

发自我的iPhone

------------------ Original ------------------ From: Hassan Bin Haroon @.> Date: Thu,Feb 16,2023 5:45 PM To: keras-team/keras @.> Cc: jinzhidajia @.>, Comment @.> Subject: Re: [keras-team/keras] ImportError: cannot import name'multi_gpu_model' (#14440)

I think multi_gpu_model tries to speed up the training. If you do not need that training speed then I suggest you to remove multi_gpu_model entirely from your code. Try to run the code without this method/package.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

HassanBinHaroon commented 1 year ago

Thanks for your feedback.

On Fri, 17 Feb 2023, 10:59 am jinzhidajia, @.***> wrote:

thanks for ur reply!!!!! But I find another solution that replace “from keras.utils import multi_gpu_model” with “from keras.utils.multi_gpu_model import multi_gpu_model” It finally works on! Hope it also works on u!

发自我的iPhone

------------------ Original ------------------ From: Hassan Bin Haroon @.> Date: Thu,Feb 16,2023 5:45 PM To: keras-team/keras @.> Cc: jinzhidajia @.>, Comment @.> Subject: Re: [keras-team/keras] ImportError: cannot import name'multi_gpu_model' (#14440)

I think multi_gpu_model tries to speed up the training. If you do not need that training speed then I suggest you to remove multi_gpu_model entirely from your code. Try to run the code without this method/package.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

— Reply to this email directly, view it on GitHub https://github.com/keras-team/keras/issues/14440#issuecomment-1434141563, or unsubscribe https://github.com/notifications/unsubscribe-auth/AY2GYVVC3FXCKFBNWVCCXBDWX4HUHANCNFSM4XHQKUTQ . You are receiving this because you commented.Message ID: @.***>

jinzhidajia commented 1 year ago

Final solution:install protobuf3.20 in the terminal!!!! It finally work on without errors!

发自我的iPhone

------------------ Original ------------------ From: Hassan Bin Haroon @.> Date: Fri,Feb 17,2023 3:39 PM To: keras-team/keras @.> Cc: jinzhidajia @.>, Comment @.> Subject: Re: [keras-team/keras] ImportError: cannot import name'multi_gpu_model' (#14440)

Thanks for your feedback.

On Fri, 17 Feb 2023, 10:59 am jinzhidajia, @.***> wrote:

> thanks for ur reply!!!!! > But I find another solution that replace > “from keras.utils import multi_gpu_model” with > “from keras.utils.multi_gpu_model import multi_gpu_model” > It finally works on!&nbsp;Hope it also works on u! > > > > 发自我的iPhone > > > ------------------ Original ------------------ > From: Hassan Bin Haroon @.&gt; > Date: Thu,Feb 16,2023 5:45 PM > To: keras-team/keras @.&gt; > Cc: jinzhidajia @.&gt;, Comment @.&gt; > Subject: Re: [keras-team/keras] ImportError: cannot import > name'multi_gpu_model' (#14440) > > > > > > > I think multi_gpu_model tries to speed up the training. If you do not need > that training speed then I suggest you to remove multi_gpu_model entirely > from your code. Try to run the code without this method/package. > > — > Reply to this email directly, view it on GitHub, or unsubscribe. > You are receiving this because you commented.Message ID: @.&gt; > > — > Reply to this email directly, view it on GitHub > <https://github.com/keras-team/keras/issues/14440#issuecomment-1434141563&gt;, > or unsubscribe > <https://github.com/notifications/unsubscribe-auth/AY2GYVVC3FXCKFBNWVCCXBDWX4HUHANCNFSM4XHQKUTQ&gt; > . > You are receiving this because you commented.Message ID: > @.> >

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

MaraJadeLives commented 1 year ago

My fix for textgenrnn: Make a multi_gpu_model.py file where it's looking for one, with just a comment in the file. "C:\Users\usr_name\AppData\Local\Programs\Python\Python310\Lib\site-packages\keras\utils\multi_gpu_model.py"

redrover9 commented 1 year ago

fixed by commenting out from tensorflow.keras.utils import multi_gpu_model and replacing from keras.backend.tensorflow_backend import set_session with from keras.backend import set_session in textgenrnn.py.

HassanBinHaroon commented 1 year ago

👍

On Sat, 29 Apr 2023, 10:36 am redrover9, @.***> wrote:

fixed by commenting out from tensorflow.keras.utils import multi_gpu_model and replacing from keras.backend.tensorflow_backend import set_session with from keras.backend import set_session in textgenrnn.py.

— Reply to this email directly, view it on GitHub https://github.com/keras-team/keras/issues/14440#issuecomment-1528662450, or unsubscribe https://github.com/notifications/unsubscribe-auth/AY2GYVWD76BOX2GBBZ6Q2JTXDSSGVANCNFSM4XHQKUTQ . You are receiving this because you commented.Message ID: @.***>

catafest commented 1 year ago

I have tried some of the solutions posted here but no dice for me - is there a known solution for this? I am unfortunately on Windows, so the env is a bit painful. But I installed the most recent version of Python (3.9) and then installed textgenrnn via pip, and then just trying to run this basic example:

I use this in colab google: `!pip3 install git+https://github.com/minimaxir/textgenrnn.git#v1.5.0

import warnings

with warnings.catch_warnings(): warnings.simplefilter("ignore") from textgenrnn import textgenrnn textgen = textgenrnn()`

MaraJadeLives commented 1 year ago

Try my fork of it, which makes a few changes. Also make a blank file where it looks for the multi GPU model, if you only use one GPU

On Fri, Aug 11, 2023, 5:16 PM Cătălin George Feștilă < @.***> wrote:

I have tried some of the solutions posted here but no dice for me - is there a known solution for this? I am unfortunately on Windows, so the env is a bit painful. But I installed the most recent version of Python (3.9) and then installed textgenrnn via pip, and then just trying to run this basic example:

I use this in colab google: `!pip3 install git+https://github.com/minimaxir/textgenrnn.git#v1.5.0

import warnings

with warnings.catch_warnings(): warnings.simplefilter("ignore") from textgenrnn import textgenrnn textgen = textgenrnn()`

— Reply to this email directly, view it on GitHub https://github.com/keras-team/keras/issues/14440#issuecomment-1675410483, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXC4XQ6XFXACHHK43DZ63H3XU2OLNANCNFSM4XHQKUTQ . You are receiving this because you commented.Message ID: @.***>