Closed mikuchels closed 3 years ago
me too
any work around for this issue
I'm encountering this issue on both my local machine and in a colab.research.google.com notebook
Here is a notebook that reproduces the error https://colab.research.google.com/drive/1k-dz6lBFAd560_cNiUFtiQqSKIv8yJv9?usp=sharing
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)
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!
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!!!
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
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.
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.
Same error.
Ubuntu - 20.04 Keras - 2.4.3 keras-utils - 1.0.13 tensorflow-gpu - 2.4.1
Same problem as above
Ubuntu - 20.04 Keras - 2.4.3 keras-utils - 1.0.13 tensorflow-gpu - 2.4.1 CUDA 11.0
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.
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 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
This function is deprecated and was removed on 2020-04-01. You can use tf.distribute.MirroredStrategy instead.
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
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
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.
The solution for me was using an old version of Keras (Version 2.0.0)
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
@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!
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.
Closing as stale. Please reopen if you'd like to work on this further.
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(...)
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!
Plz tell me the version of ur tensor-flow. It's said that my tensor-flow is not compatible with Keras. thanks!!
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: @.***>
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>, > or unsubscribe > <https://github.com/notifications/unsubscribe-auth/AY2GYVTHJO53WZ7ECA4S4K3WXUZKJANCNFSM4XHQKUTQ> > . > 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: @.***>
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.
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: @.***>
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: @.***>
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! 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: > @.> >
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
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"
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
.
👍
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: @.***>
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()`
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: @.***>
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'