Tony Beta Lambda
Tony Beta Lambda

Reputation: 571

Importing tflearn is very slow

Importing tflearn takes a large amount of time compared to other modules.

On a newly created virtual environment with only tflearn and minimal dependencies installed with pip:

After installing h5py and scipy as suggested, time consumed by import tflearn goes up to ~3.5s. How can I reduce importing time?

(All tests above are done by putting the statement in test.py and run time python test.py several times. The reported time is the "real" time as reported by time builtin of bash.)

Upvotes: 2

Views: 695

Answers (2)

Andriy Ivaneyko
Andriy Ivaneyko

Reputation: 22071

Some imported modules initialize when you start using them, some aren't. The tflearn initialise when you import it and since it has a lot of dependencies it's not surprisingly that it takes that much time.

The SO QA Below will give you idea of optimization that long time.

improving speed of Python module import

Good luck

Upvotes: 0

AKX
AKX

Reputation: 169416

The short answer

Do your initial work in Jupyter or something else that has a long-lived interpreter session, so you don't need to wait around for imports that much.

The long answer

You can use python -v to trace imports. I installed moreutils from apt and tensorflow and tflearn from pip in a pristine python:3.6-stretch Docker container...

root@10e4bcd91377:/# python -v -c 'import tensorflow' 2>&1 | ts '%H:%M:%.S' | grep 'import ' | wc -l
954
root@10e4bcd91377:/# python -v -c 'import tflearn' 2>&1 | ts '%H:%M:%.S' | grep 'import ' | wc -l
1768

It's immediately clear that importing tflearn imports a whole lot of packages. Which ones?

# python -v -c 'import tflearn' 2>&1 | grep 'import ' | cut -f1 -d# | sort | uniq > tflearn-imports.txt
# python -v -c 'import tensorflow' 2>&1 | grep 'import ' | cut -f1 -d# | sort | uniq > tensorflow-imports.txt
# diff --suppress-common-lines tensorflow-imports.txt tflearn-imports.txt

I'll spare the 831 lines of output, but it looks like tflearn imports all of tensorflow.contrib, which is taking quite a while, and it's not something importing tensorflow itself does. Armed with this info, we can look at the original python -v -c 'import tflearn' 2>&1 output – it looks like tflearn.variables is the module importing tensorflow.contrib...

# <snip>
import 'tensorflow.contrib.summary.summary'
import 'tensorflow.contrib'
import 'tflearn.variables'
import 'tflearn.config'
# <snip>

Could it be this from tensorflow.contrib.framework.python.ops import add_arg_scope as contrib_add_arg_scope statement? Let's find out...

# time python -v -c 'from tensorflow.contrib.framework.python.ops import add_arg_scope as contrib_add_arg_scope' 2>&1 | grep 'import ' | wc -l
1727

real    0m4.010s
user    0m3.820s
sys 0m0.410s
root@10e4bcd91377:/#

Ayup, looks like it! Due to the way Python's importing works, importing a submodule has to evaluate the whole package, and since tensorflow.contrib doesn't use Tensorflow's Python lazy loader (which does mention contrib), it takes a while.

(There used to be discussion about vendoring in the module here, but that's irrelevant, because:)

Unfortunately there are other places within tflearn that also import bits and pieces from contrib, so swatting out this dependency won't help much.

Upvotes: 3

Related Questions