`import keras` with tensorflow>=1.2.0 on windows is very slow
See original GitHub issueRunning import keras
on Python 3.5.2 on Windows with tensorflow version 1.2.0 and above takes over 10 seconds (both tensorflow and tensorflow-gpu).
Downgrading to tensorflow 1.1.0 reduces the import command time to less than 3 seconds.
Profiling the import keras
method I see a huge bottleneck due to the following import tree:
keras
-> keras.activations
-> keras.engine
-> keras.engine.training
-> keras.callbacks
-> tensorflow.contrib
With the import tensorflow.contrib
statement taking up 67% inclusive time. Digging deeper than that is a bit tricky since the bottlenecks are not so significant any more.
A follow up question is why are we importing so many modules as a default, and whether a leaner import can be enabled (e.g. import only stuff required for inference without training). I’m asking since running from keras.models import Sequential
is enough to trigger the entire import tree above and still take >10 seconds.
On my linux box import times are more acceptable (<5 seconds) with any tf version.
-
Check that you are up-to-date with the master branch of Keras. You can update with: pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps
-
If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.
-
Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:5 (3 by maintainers)
I found out why tensorflow.contrib is slow to import and submitted a PR to tensorflow, see https://github.com/tensorflow/tensorflow/issues/11829 and https://github.com/tensorflow/tensorflow/pull/11830 If this PR is accepted, the time to import keras would go from ~10 to ~3 seconds.
I also implemented a different fix in Keras, where tensorflow.contrib is imported in the __init__ method of the Tensorboard callback, since it is only needed if the Tensorboard callback is instantiated. https://github.com/patrikerdes/keras/commit/ef41d4b9265440857b5eb1f4a5d13e2a326b4e4f
If the Tensorflow PR is not accepted, I could created a PR of the Keras fix. If the Tensorflow PR is accepted, I’m not sure the Keras fix is necessary, it would then take the time to import keras from ~3 to ~2 seconds.
I’m facing a similar issue on Ubuntu (using 14.04 currently):