5

I've been using tensorflow for cpu on my laptop and due to it been so slow I decided to move to my desktop pc and use tensorflow for gpu.

The problem is that in my desktop computer I can't import like this, which I'm able to do on my laptop:

from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Dropout, Flatten, Dense
from tensorflow.keras.applications import MobileNetV2

So I decided to use the keras module with tensorflow-gpu as backend, so my imports on desktop look like this:

from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dropout, Flatten, Dense
from keras.applications import MobileNetV2

Also, my conda list on laptop looks like this:

keras                     2.3.1                    pypi_0    pypi
keras-applications        1.0.8                      py_0
keras-preprocessing       1.1.0                      py_1
tensorboard               2.1.0                     py3_0
tensorflow                2.1.0           eigen_py36hdbbabfe_0
tensorflow-base           2.1.0           eigen_py36h49b2757_0
tensorflow-estimator      2.1.0              pyhd54b08b_0

And my conda list on desktop looks like this:

keras                     2.3.1                    pypi_0    pypi
keras-applications        1.0.8                      py_0
keras-preprocessing       1.1.0                      py_1
tensorboard               2.1.0                     py3_0
tensorflow                2.1.0           gpu_py36h3346743_0
tensorflow-base           2.1.0           gpu_py36h55f5790_0
tensorflow-estimator      2.1.0              pyhd54b08b_0
tensorflow-gpu            2.1.0                h0d30ee6_0

So, what's the difference between using the imports like tensorflow.keras.applications and using directly keras.applications and which one is better or worse? I looked everywhere and I couldn't find a solution for the imports on my desktop, which I'd like to fix because I like using imports like tensorflow.keras.

3 Answers3

5

tensorflow.keras imports use TensorFlow repository code, whereas keras imports use Keras repository code. The two use independent method/class implementations, even if keras imports from tensorflow.

Depending on your use, functionality may differ significantly, or not much. tensorflow.keras is recommended per being better maintaned and more up-to-date - unless you use TF <2, where the two are on par for most part, except with performance considerations.

OverLordGoldDragon
  • 1
  • 9
  • 53
  • 101
  • Thanks for the information! Do you know by chance any way of fixing my imports on desktop? Using tensorflow.keras seems to be the best option and when I try to do tensorflow.keras on desktop it just doesn't work and I can't find a solution anywhere – Ignacio Frizzera Apr 06 '20 at 23:18
  • @IgnacioFrizzera Why doesn't it work? Include any error you see in your question. – OverLordGoldDragon Apr 07 '20 at 07:43
  • 1
    I fixed the imports thing, it was a bug from PyCharm. Had to update to the latest version and it fixed it. Thanks! – Ignacio Frizzera Apr 07 '20 at 19:39
2

There is not much of a difference now. Keras is inside TensorFlow now because google choose to maintain it to complement TensorFlow because Keras is a high-level API. I suggest using tensorflow.keras because in the future it will have more support because of the google team.

reference

Bruno Mello
  • 4,448
  • 1
  • 9
  • 39
1

AFAIK, (1) keras module is in a different development path from tensorflow.Keras. You can check it by install the latest version of both and run this terminal command:

> pip list | grep -i keras
Keras                2.3.1       
Keras-Applications   1.0.8       
Keras-Preprocessing  1.1.0

compare with

> python -c "import tensorflow; print(tensorflow.keras.__version__)"
2.2.4-tf

> pip list | grep tensorflow                        
tensorflow           2.1.0

(2) From my experiences, tensorflow.keras has fewer flexibility to access certain modules. For example, let's say we want to create custom convolution/pooling layer by modifying compute_output_shape(). Then we've to import it by from keras.utils.conv_utils import conv_output_length. But I've not found a way at this time to import conv_output_length() in Tensorflow v2.x.

But, going forward, tensorflow.keras will get more better support from Google. Look at this graph Keras contrib graph compare with Tensorflow contrib graph

  • Note that since this answer the import strategy of tf has changed, and it uses the keras version if installed. – arivero May 12 '23 at 11:55