Repository: keras-team/keras

Issue:
`import keras` with tensorflow>=1.2.0 on windows is very slow
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.

- [x] 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

- [x] If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found [here](https://www.tensorflow.org/get_started/os_setup).

- [x] 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).



Discussion:
I can reproduce this issue on macOS, it takes 10 seconds for me to do `from keras.models import Sequential`.

Tensorflow version: 1.2.1
Keras version: master branch
Python version: 3.6.2
macOS version: Sierra (10.12.5)

Script used to reproduce the issue:
```
import time
before = time.time()
from keras.models import Sequential
print(time.time() - before)
```

I happened to be using keras version 2.0.2 where the import went well (under 5s), but after updating to 2.0.6, the script of @patrikerdes took over 15 seconds.

Tensorflow version: 1.2.1
Keras version: 2.0.6
Python version: 3.5.2
macOs version: Sierra 10.12.6
I'm facing a similar issue on Ubuntu (using 14.04 currently):

<pre>
# 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

<strong># Keras==2.0.6, tensorflow==1.2.1
%time import keras
Wall time: 11.8 s</strong>

# Keras==2.0.2, tensorflow==1.2.1
%time import keras
Wall time: 1.34 s
</pre>

Resolving patch:
diff --git a/keras/callbacks.py b/keras/callbacks.py
--- a/keras/callbacks.py
+++ b/keras/callbacks.py
@@ -21,10 +21,6 @@
 except ImportError:
     requests = None
 
-if K.backend() == 'tensorflow':
-    import tensorflow as tf
-    from tensorflow.contrib.tensorboard.plugins import projector
-
 
 class CallbackList(object):
     """Container abstracting a list of callbacks.
@@ -635,6 +631,9 @@ def __init__(self, log_dir='./logs',
         if K.backend() != 'tensorflow':
             raise RuntimeError('TensorBoard callback only works '
                                'with the TensorFlow backend.')
+        global tf, projector
+        import tensorflow as tf
+        from tensorflow.contrib.tensorboard.plugins import projector
         self.log_dir = log_dir
         self.histogram_freq = histogram_freq
         self.merged = None


Test patch: