Configuring Keras to use Tensorflow instead of Theano - tensorflow

I'm trying to configure Keras install under an Anaconda virtualenv with all of that running under Ubuntu 17.04. I've installed keras-gpu via conda, and have generated a bootstrap ~/.keras directory by running python -c 'import keras'; finally, I've updated my keras.json within that directory to include tensorflow as the backend rather than theano.
I've also tried those steps with the regular, non-GPU keras available on conda.
The issue I'm getting is that the backend option in my keras.json is being read (since invalid values raise exceptions), but is being overruled by an environment variable that gets exported by Anaconda itself -- according to grep, there are a few instances of:
export KERAS_BACKEND=tensorflow
export KERAS_BACKEND=theano
... Scattered around a number of files in ~/miniconda3/pkgs/keras-2.0.2-py36_1/.
I'm hesitant to manually edit these files since they're automatically put there by the package manager, but I also want to avoid explicitly specifying KERAS_BACKEND=tensorflow at the start of each session, and I'd like to avoid solutions involving tools like direnv.
How can I get conda's keras to use tensorflow by default?

The problem is probably in the file activate.sh of the keras package on conda-forge. The export statements in this file are unnecessary and should be removed IMO. There's just no reason to restrict linux users to use theano as Keras backend (or TensorFlow for Mac OSX).
#!/bin/bash
if [ "$(uname)" == "Darwin" ]
then
# for Mac OSX
export KERAS_BACKEND=tensorflow
elif [ "$(uname)" == "Linux" ]
then
# for Linux
export KERAS_BACKEND=theano
fi
You could solve the problem by:
Removing those environment settings from activate.sh.
Removing currently installed keras and keras-gpu, and then
install keras with conda install -c defaults keras: the
non-conda-forge version of keras seems to be okay. I didn't find any of those env settings on my machine.
pip install keras: removing currently installed keras and keras-gpu, and then install the python package only.

Related

Why is Mozilla Deepspeech using Tensorflow 0.11 when I have 0.12 installed?

I use Anaconda3 with python 3.6 and use pip install tensorflow deepspeech.
When I run the following command I get errors:
deepspeech --model models/output_graph.pb --alphabet models/alphabet.txt --audio voice.wav
Loading model from file models/output_graph.pb
TensorFlow: v1.11.0-9-g97d851f04e
DeepSpeech: unknown
Warning: reading entire model file into memory. Transform model file into an mmapped graph to reduce heap usage.
2018-12-26 18:19:30.748212: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Note that I am using the pretrained model obtained from the instructions using this command:
wget -O -
https://github.com/mozilla/DeepSpeech/releases/download/v0.3.0/deepspeech-0.3.0-models.tar.gz
| tar xvfz -
Where is the Tensorflow v1.11.0 coming from? pip freeze shows Tensorflow as v0.12.0? How do I get around this?
Just a side note: it seems like the current version of deepspeech on pypi uses tensorflow == 1.11.0. I did not inspected the *.whl packages, but the upload date here indicates that the package was uploaded on Oct 23, while DeepSpeech migrated to tensorflow == 1.12.0rc2 on a later date (Oct 31) in this commit.
Now, here is the list of things for you to check:
Make sure that you're using pip inside the environment, i.e. make sure to do a source activate env-name first.
Make sure as well that DeepSpeech is installed in the same environment. Keep in mind that your Anaconda Environment can still use packages from default Python of your system (in case you're using linux).
If the above doesn't work, try this one. Print out the path of TensorFlow library from your script (that says it is 1.11.0, and from the place where you invoke pip freeze (i.e. from the terminal).

Tensorflow version different in conda env & jupyter notebook

I created a conda env and installed tensorflow 1.4
pip install tensorflow==1.4
This installed, and from within the environment, when i type
import tensorflow as tf
tf.__version__
it prints '1.4.0'
Now, i open jupyter notebook from the same environment, and when i type the same in the notebook, it gives me 1.10.0
How is this possible? Jupyter notebook should take the same libraries within which it is running. How does it have a different version of tensorflow
And btw I'm concerned with this because i wanna use 1.4.0 in my notebook
These are the questions I ask myself in situations like this, and they usually resolve my issue:
Was the kernel of my notebook running while I pipped? Shut down and restart.
Are pip and conda sharing PATH, et cetera? I don't take it for granted that my pip is modifying my conda env. You can check with these commands.
Your conda env is created, but is it active? You list created envs with these, and you can activate/deactivate with these.
Besides checking all of these, where am I launching jupyter from? I like to install jupyter with a simply pip install jupyter and that could add jupyter to the command line globally. Anaconda navigator/command line reliably launches the jupyter that lives in your Anaconda distribution.
To add to Charles Landau:
You likely installed tensorflow in parallel with pip and with conda.
Within your env, just check with pip list tensorflow and conda list tensorflow. Most likely you will see two different versions.
To be consistent do:
pip unistall tensorflow to keep only one.

Why keras always says using theano backend?

I installed keras using conda in my virtual environment and checked $HOME/.keras/config.json file.
{
"image_data_format": "channels_last",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
I already set backend to tensorflow but when I run this in the python console
import keras
It is showing me that keras is using theano backend. Why?
Using Theano backend.
WARNING (theano.configdefaults): install mkl with `conda install mkl-service`: No module named mkl
I added export KERAS_BACKEND=tensorflow at the end of my .bashrc and restart the command line and activate my source. Still seeing the above error again. Can anyone help me with this?
We had also faced the same issue when installed keras using conda environment. Since we already had keras installed using pip, where the backend was set as theano, it was taking that keras. The problem got fixed when we removed the pip version of keras using the command pip uninstall keras
Well you can start your editor with line:
KERAS_BACKEND=tensorflow
KERAS_BACKEND=tensorflow spyder
This would force use the Tensorflow backend.
But before using this ensure that you have tensorflow installed with all the required dependencies.
Source

Theano used as default backend with Keras

I have just installed Tensorflow and Keras in a conda environment (with miniconda).
In the keras.json file, the backend is tensorflow and there is no environment variable KERAS_BACKEND defined, however, when I import keras backend, I am told that Theano is used !
I don't know what's wrong....
Two solutions in fact:
1- When installing Keras with conda install keras, the solution I found is to overwrite the __init.py__ file of the keras.backend module. In the file, I simply commented the portion of code where it checks for an environment variable. However, I have not found where the environment variable was set.
2- The proposed solution in other posts like this one is the solution to find where the environment variable is set. For this, keras has to be installed with the channel of conda-forge conda install -c conda-forge keras.

Install Keras on Anaconda OSX

I am trying to install keras on an anaconda environment (OSX), because I want to use it with spyder - ipython. To do that I just used pip install keras (I already have tensorflow). After the installation when I call python 2.7 from the terminal, keras works fine. But, when I call python 3.5 or spyder and try to import keras I receive:
No module named 'keras'
I assume the issue might be with the PATHS on my MacBook, because which python returns
/usr/local/bin/python2.7
while which python3.5 (or spyder) returns
/Users/georgiospapadopoulos/anaconda/bin/python3.5
/Users/georgiospapadopoulos/anaconda/bin/spyder
Also, during pip install keras shows that
Requirement already satisfied: keras in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
My ~/.bash_profile contains
# added by Anaconda3 2.4.0 installer
export PATH="/Users/georgiospapadopoulos/anaconda/bin:$PATH"
# added by Anaconda3 4.2.0 installer
export PATH="/Users/georgiospapadopoulos/anaconda/bin:$PATH"
export CUDA_HOME=/usr/local/cuda
export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$CUDA_HOME/lib"
export PATH="$CUDA_HOME/bin:$PATH"
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
#PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
PATH="/usr/local/bin/python:$PATH"
export PATH
You are probably mixing up the virtual environments. The best way to handle this is to create a vertual env in Anaconda - say, neural-net-venv, and then open the terminal for that venv, and install keras and other related modules. Then go back to Anaconda dash and select that venv as active environment to work on. Then select Jupyter and Spyder and run your imports.
Note that you also should not mix your Python versions - if you must work on Py2 and Py3 - create separate virtual environments for both, install keras, theanos/tensorflows separately in these environments, and you should be good to go.
I have run this setup on MacOS and it works like a charm.
For installing keras in Anaconda, the best and hassle free way is just use open the anaconda prompt and then type:
conda install keras
Keras runs on either tensorflow or theano backends. Once the keras install is complete, just open the python shell and type
>>>import keras
If some error is thrown, then there must be some problem with the backend. So just open the anaconda prompt, and type
conda import tensorflow
theano also can be used. Nevertheless tensorflow is the default one.
I wanted to insatll keras on Anaconda, tried the above approach, but it still did not work. Specifically, I started Anaconda Navigator and then opened a Mac OS terminal in the base environment. Then I followed the conda install commands for keras and tensorflow. It worked fine for keras. But with tensorflow, I got the following error message:
Downloading and Extracting Packages
_tflow_select-2.3.0 | 3 KB | ######################################################### | 100%
ChecksumMismatchError: Conda detected a mismatch between the expected content and downloaded content
for url 'https://conda.anaconda.org/Anaconda/osx-64/_tflow_select-2.3.0-mkl.tar.bz2'.
download saved to: /Users/dlin/opt/anaconda3/pkgs/_tflow_select-2.3.0-mkl.tar.bz2
expected sha256: cc155b27e7bf91ec5370ce1fd2d5fceccbf13ac19706229674ba971fa3751446
actual sha256: aad248699de112a7a5ead1695dfdf51b5693c2927303844b29dd7d9138dc95b9