question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`import keras` with tensorflow>=1.2.0 on windows is very slow

See original GitHub issue

Running 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:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
patrikerdescommented, Jul 27, 2017

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.

1reaction
jayantjcommented, Jul 24, 2017

I’m facing a similar issue on Ubuntu (using 14.04 currently):

# python3.5.2

# Keras==2.0.6, tensorflow==1.1.0
%time import keras
Wall time: 2.04 s

# Keras==2.0.2, tensorflow==1.1.0
%time import keras
Wall time: 1.42 s

# Keras==2.0.6, tensorflow==1.2.1
%time import keras
Wall time: 11.8 s

# Keras==2.0.2, tensorflow==1.2.1
%time import keras
Wall time: 1.34 s
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is TensorFlow 2 much slower than TensorFlow 1?
The behavior, however, is inconsistent: sometimes running in graph mode helps considerably, other times it runs slower relative to Eager.
Read more >
Keras vs. tf.keras: What's the difference in TensorFlow 2.0?
Keras started supporting TensorFlow as a backend, and slowly but surely, TensorFlow became the most popular backend, resulting in TensorFlow ...
Read more >
Better performance with tf.function | TensorFlow Core
If your Function retraces a new graph for every call, you'll find that your code executes more slowly than if you didn't use...
Read more >
How to build CNN in TensorFlow: examples, code ... - Cnvrg.io
A Convolution Neural Network is a multi-layered artificial neural network that ... from tensorflow.keras.preprocessing import image import numpy as np !wget ...
Read more >
Keras FAQ
How can I train a Keras model on multiple GPUs (on a single machine)?; How can I ... model reconstruction from JSON: from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found