Keras import confusion?

I’m trying to follow along with the code from Saul Dobilas’s blog on DCGANs.. I have a Tensorflow-gpu conda environment that I’m running the code from. When I try & run it thought the following error is thrown,

Traceback (most recent call last):
  File ".\DeepConvGanExample.py", line 10, in <module>
    print('Tensorflow/Keras: %s' % keras.__version__) # print version
  File "C:\ProgramData\Anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\util\lazy_loader.py", line 62, in __getattr__
    module = self._load()
  File "C:\ProgramData\Anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\util\lazy_loader.py", line 45, in _load
    module = importlib.import_module(self.__name__)
  File "C:\ProgramData\Anaconda3\envs\tf-gpu\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'keras'

So I opened up a python interpreter & typed the following lines,

>>>from tensorflow import keras
>>>keras.__version__

So I was fine with the import but when I tried to get the version of keras I get the following error,

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\util\lazy_loader.py", line 62, in __getattr__
    module = self._load()
  File "C:\ProgramData\Anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\util\lazy_loader.py", line 45, in _load
    module = importlib.import_module(self.__name__)
  File "C:\ProgramData\Anaconda3\envs\tf-gpu\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'keras'

OK so I closed the interpreter & opened a new one typed the following & got the expected result,

>>>import tensorflow.keras as keras
>>>keras.__version__
'2.6.0'

I only have one interpreter installed in the evironment.
So what’s going on here?

@DrBwts,

To use Keras, you will need to have the TensorFlow package installed.

Once TensorFlow is installed, just import Keras via:

from tensorflow import keras
keras.__version__

Output:

2.11.0

Please find the gist for reference.

Thank you!

@chunduriv I have TensorFlow installed,

> conda list tensorflow
# packages in environment at C:\ProgramData\Anaconda3\envs\tf-gpu:
#
# Name                    Version                   Build  Channel
tensorflow                2.6.0           gpu_py37h3e8f0e3_0
tensorflow-base           2.6.0           gpu_py37hb3da07e_0
tensorflow-estimator      2.6.0              pyh7b7c402_0
tensorflow-gpu            2.6.0                h17022bd_0

Hi @DrBwts

The author shared a GitHub link, and one of the outputs in the DCGAN notebook -
Art053_NN_DCGAN/DCGAN.ipynb at main · SolClover/Art053_NN_DCGAN · GitHub - contains the versions of packages the original author used in the example:

Tensorflow/Keras: 2.7.0
numpy: 1.21.4
sklearn: 1.0.1
OpenCV: 4.5.5
matplotlib: 3.5.1
graphviz: 0.19.1

Maybe when debugging, you could start with uninstalling/reinstalling TF to match those versions? (virtualenv/venv can also help for Python package management Installing packages using pip and virtual environments — Python Packaging User Guide)

Since the example uses TensorFlow 2.7, you can compare the changes in newer versions up to TensorFlow 2.11 here Releases · tensorflow/tensorflow · GitHub. (And if you use pip, you can upgrade all the packages as descibed here).

Let us know how it goes.

1 Like

Yes that did it thankyou for pointing me in the right direction.

The code now runs from the command line but I still get the same error if I try to run it in Spyder.

1 Like

@DrBwts,

Can you try to uninstall and install the latest version of tensorflow? let us know if the issue still continues.

Please refer to the official tutorial on Deep Convolutional Generative Adversarial Network.

Thank you!

I think the remaining issue is to do with Spyder & not TF

I think the remaining issue is to do with Spyder & not TF

Yes.