How to change Keras backend (where's the json file)? - python

I have installed Keras, and wanted to switch the backend to Theano. I checked out this post, but still have no idea where to put the created json file. Also, below is the error I got when running import keras in Python Shell:
Using TensorFlow backend.
Traceback (most recent call last): File "", line 1, in
import keras File "C:\Python27\lib\site-packages\keras__init__.py", line 2, in
from . import backend File "C:\Python27\lib\site-packages\keras\backend__init__.py", line 64, in
from .tensorflow_backend import * File "C:\Python27\lib\site-packages\keras\backend\tensorflow_backend.py",
line 1, in
import tensorflow as tf ImportError: No module named tensorflow
When running python -c "import keras; print(keras.__version__)" from Windows command line, I got:
Using TensorFlow backend. Traceback (most recent call last): File
"", line 1, in File
"C:\Python27\lib\site-packages\keras__init__.py", line 2, in
from . import backend File "C:\Python27\lib\site-packages\keras\backend__init__.py", line 64, in
from .tensorflow_backend import * File "C:\Python27\lib\site-packages\keras\backend\tensorflow_backend.py",
line 1, in
import tensorflow as tf ImportError: No module named tensorflow
Can someone please help? Thanks!

After looking at keras sources (this place):
Start up your python-binary and do the following
import os
print(os.path.expanduser('~'))
# >>> C:\\Users\\Sascha' # will look different for different OS
This should be the base-directory
Keras will build an folder .keras there where keras.json resides (if it was already created). If it's not there, create it there
Example: C:\\Users\\Sascha\\.keras\\keras.json'

In case of Ubuntu,the following worked for me:
The '.keras' folder is present in your home directory,but is hidden.So,you need to unhide the hidden files in your home directory.
You can see the hidden files in Ubuntu by
View-> show hidden files or
pressing ctrl+H.
You can now see the '.keras' folder in your home directory.Inside that folder,you will see the 'keras.json' file which you can modify to switch the keras backend to theano according to the official documentation https://keras.io/backend/

"Can’t find your keras.json file? : Windows
On most systems the keras.json file (and associated sub-directories) will not be created until you open up a Python shell and directly import the keras package itself.
If you find that the ~/.keras/keras.json file does not exist on your system, simply open up a shell, (optionally) access your Python virtual environment (if you are using virtual environments), and then import Keras:
$ workon keras_tf
$ python
>>> import keras
>>> quit()
"
Referenced from : keras-with-tensorflow/theano-backend

For those with a python shell open:
import os
with open(os.path.expanduser('~')+'\\.keras\\keras.json','w') as f:
new_settings = """{\r\n
"epsilon": 1e-07,\r\n
"image_data_format": "channels_last",\n
"backend": "theano",\r\n
"floatx": "float32"\r\n
}"""
f.write(new_settings)
import keras

You can directly use,
import os
os.environ['KERAS_BACKEND']='theano'
or
os.environ['KERAS_BACKEND']='tensorflow'

In case you want to change the config, the json is available here: ~/.keras/keras.json
To do this dynamically in python 2.7 you can run:
from keras import backend as K
import os
def set_keras_backend(backend):
if K.backend() != backend:
os.environ['KERAS_BACKEND'] = backend
reload(K)
assert K.backend() == backend
set_keras_backend("theano")

For Linux systems, the hidden .keras directory will be created in the user’s home directory. To observe whether or not it has been created, run the following command from your home directory (the -a allows you to see hidden files and directories).
ls –a
If the directory is there, then cd into it and modify the keras.json file. If it’s not there, then create the directory with
mkdir .keras
Then create the file with
touch keras.json
Then edit the file to make the config changes you referenced to change the backend engine to Theano.
This process is covered fully in this video.

The official Keras Documentation may help you. This link shows how to change the backend of Keras. You have to change the ~/.keras/keras.json from 'backend': 'tensorflow', to 'backend': 'theano'.

Just to add this informative post. I'm using anaconda for my task. And imported keras through my anaconda python. So keras got installed in
C:\Users\username\AppData\Local\Continuum\Anaconda3\Lib\site-packages
There are 2 folders in site-packages: keras & Keras-1.0.8.dist-info.
In Keras-1.0.8.dist-info, there's a file called metadata.json. This by default has "Theano" as backend. So, if you change that to tensorflow, you will get tensoflow backend.
Hope it will help someone who has might type of issues.

Simplest Solution:
Google's TensorFlow is default backend for keras but for example if u want to change it with theano then
First check if the alternate backend you wish to work with is installed successfully by importing it in python shell:
import theano as th
if that works fine
Step 2: if you have installed keras, theano in virtualenv then go to virtualenv directory
e.g virtualenv/virtual/lib/python2.7/site-packages/keras/backend/
open init.py
change line 27
Default backend: TensorFlow.
_BACKEND = 'theano'
and thats it
open python shell
and import keras
import keras as kd

Type following and press enter on command prompt:
%USERPROFILE%/.keras/keras.json
Change backend in the opened text file and save it.

In ubuntu you can use this command to open keras.json file in vi editor and editing and saveing
sudo vi $HOME/.keras/keras.json
or use the following for opening in gedit
sudo gedit $HOME/.keras/keras.json

Related

ImportError: cannot import name 'input_reader_pb2'

working on win10 64-bit
when i trying to train my model by E:\projectx\model-master\models-master>python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/ssd_mobilenet_v1_pets.config
this error appear
File "train.py", line 49, in
from object_detection.builders import dataset_builder
File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\builders\dataset_builder.py",
line 27, in
from object_detection.data_decoders import tf_example_decoder
File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\data_decoders\tf_example_decoder.py",
line 27, in
from object_detection.protos import input_reader_pb2
ImportError: cannot import name 'input_reader_pb2'
i do run protoc 3.4 protoc object_detection/protos/*.proto --python_out=.
but the error still exist
i check all thing and it is should be fine and work, it is going me mad!!
please help....
thanks
As you already compiled all .proto files in object_detection/protos/ . You should see python files which have '_pb2' like eval_pb2. If you can see these then go to models/research directory and run these code one by one:
python setup.py build
python setup.py install
From inside the object_detection folder:
protoc ../object_detection/protos/*.proto --python_out=.
This command will generate a *_pb2.py for each .proto file in the object_detection/protos/ folder.
Note: Is important to specify a path above object_detection, otherwise errors would occur, most likely:
object_detection/protos/ssd_anchor_generator.proto: File not found.
protos/anchor_generator.proto:8:1: Import "object_detection/protos/ssd_anchor_generator.proto" was not found or had errors.
protos/anchor_generator.proto:17:5: "FlexibleGridAnchorGenerator" is not defined.
If the error persists probably you are in the wrong folder, take a look at the output message to see from where it is trying to import the file and execute the command in the right directory.
As last resort: Download the object_detection module from https://github.com/tensorflow/models/tree/master/research
place it in your working directory, enter it and re-execute the command above.
It will surely works as local modules have import priority over sys.path.
If not, the error message would be probably different from the one reported and the problem lies on the tensorflow installation or the protobuf compiler; as here where the problem was caused by the protoc version.
other useful links: https://github.com/tensorflow/models/issues/5264
Maybe you haven't added module slim to PYTHONPATH.It can be done by running below code inside models/research directory.
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
Seems that you didnt compile protobuf.
For solving it:
Download the latest protoc exe here: https://github.com/google/protobuf/releases (in your case should be win32)
Rename that folder to "protoc"
Put that folder inside models/research
in models/research via console, launch:
protoc/bin/protoc object_detection/protos/*.proto --python_out=.
I dont know exactly if that command will work on windows, but you have to be sure that you are using the protoc compiler that you downloaded (v.3.6) instead of the protoc in your enviroment.

ModuleNotFoundError: No module named 'object_detection'

i try to train.py in object_detection in under git url
https://github.com/tensorflow/models/tree/master/research/object_detection
However, the following error occurs.
ModuleNotFoundError: No module named 'object_detection'
So I tried to solve the problem by writing the following code.
import sys
sys.path.append('/home/user/Documents/imgmlreport/inception/models/research/object_detection')
from object_detection.builders import dataset_builder
This problem has not been solved yet.
The directory structure is shown below.
~/object_detection/train.py
~/object_detection/builders/dataset_bulider.py
and here is full error massage
/home/user/anaconda3/lib/python3.6/site-packages/h5py/init.py:34: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated.
In future, it will be treated as np.float64 == np.dtype(float).type.
from ._conv import register_converters as _register_converters
Traceback (most recent call last):
File "train.py", line 52, in
import trainer
File"/home/user/Documents/imgmlreport/inception/models/research/object_detection/trainer.py", line 26, in
from object_detection.builders import optimizer_builder
ModuleNotFoundError: No module named 'object_detection'
how can i import modules?
Try install Tensorflow Object Detection Library Packaged
pip install tensorflow-object-detection-api
Cause of this error is installing object_detection library, So one of the solution which can work is running the below command inside models/research
sudo python setup.py install
If such solution does not work, please execute the below command one by one in the directory models/research
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
sudo python setup.py install
I hope this will work. I also faced the same problem while creating model from export_inference_graph.py. It worked for me.
You need to export the environmental variables every time you open a new terminal in that environment.
Please note that there are are back quotes on each of the pwd in the command as this might not be showing in the command below. Back quote is the same as the tilde key without pressing the shift key (US keyboard).
From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
try this:
python setup.py build
python setup.py install
There are a number of modules in the object_detection folder, and I have created setup.py in the parent directory(research folder) to import all of them.
from setuptools import find_packages
from setuptools import setup
REQUIRED_PACKAGES = ['Pillow>=1.0', 'Matplotlib>=2.1', 'Cython>=0.28.1']
setup(
name='object_detection',
version='0.1',
install_requires=REQUIRED_PACKAGES,
include_package_data=True,
packages=[p for p in find_packages() if p.startswith('object_detection')],
description='Tensorflow Object Detection Library',
)
You did have "sys.path.append()" before you imported the object detection, so I am surprised that you are facing this error!
Please check that the path you have used in sys.path.append() is right.
Well, the only and obvious answer for the error is that the path of the module is not added properly.
Besides the various ways mentioned here, here is a way in which you can add the "object_detection" path permanently to the PYTHONPATH variable.
If you are using a Linux system, here is how you would go about it:
Go to the Home directory. Press Ctrl + H to show hidden files. You will see a file called ".bashrc". Open this file using a code editor (I used Visual Studio).
In the last line of .bashrc file, add the line:
export PYTHONPATH=/your/module/path:/your/other/module/path:your/someother/module/path
Then press "save" in the code editor. Since ".bashrc" is a "Read-only" file the editor will throw a pop-up saying the same. Also in the pop-up there will be an option that says: "Try with sudo". Hit this button and now you are good to go.
All your modules are now permanently added to the PYTHONPATH. This means that you need not run sys.path.append every time you open your terminal and start a session!
Below is the screenshot with no error when I followed the said steps:
Try this. I hope it helps.
And finally, If you've followed all the steps here and are at your wit's end...make sure the file that you're running (the one with your source code in it ya know), isn't named object_detection.py - that would preclude it being searched for as a module.
Certainly I've never done anything like this that led me to add an embarrassing answer on Stack Overflow...
I had to do:
sudo pip3 install -e . (ref)
sudo python3 setup.py install
System:
OS: Ubuntu 16.04, Anaconda (I guess this is why I need to use pip3 and python3 even I made virtual environment with Pyehon 3.8)

ModuleNotFoundError: No module named 'tensorflow.tensorboard.tensorboard'

There seems to be a problem with recent TensorFlow build. The TensorBoard visualization tool would not run when it is compiled from sources to use with GPU. The error is as follows:
$ tensorboard
Traceback (most recent call last):
File "/home/gpu/anaconda3/envs/tensorflow/bin/tensorboard", line 7, in <module>
from tensorflow.tensorboard.tensorboard import main
ModuleNotFoundError: No module named 'tensorflow.tensorboard.tensorboard'
Specs of system: Ubuntu 16.04, NVIDIA GTX 1070, cuda-8.0, cudnn 6.0.
Installed using Bazel from sources as described here:
https://www.tensorflow.org/install/install_sources
Installed into fresh anaconda3 environment 'tensorflow', environment is activated when performing command.
Would appreciate any help!
An easy fix:
python -m tensorboard.main --logdir=/path/to/logs
After some trial and error, I have solved this issue by adapting the file tensorboard-script.py in path/to/conda/envs/myenv/Scripts (Windows) as follows:
if __name__ == '__main__':
import sys
#import tensorflow.tensorboard.tensorboard
import tensorboard.main
#sys.exit(tensorflow.tensorboard.tensorboard.main())
sys.exit(tensorboard.main.main())
Now I can invoke tensorboard as expected:
tensorboard --logdir=log/ --port 6006
Okay, I've found a solution that works and also received some explanation from tensorflower on github.
There might be an issue with tensorboard when compiling tensorflow from sources because tensorboard is now removed to a separate repo and is not a part of tensorflow. The tensorflower said the docs will be updated eventually, but I figured a workaround for the impatient (like myself).
Edit tensorboard file inside tensorflow/bin (/home/gpu/anaconda3/envs/tensorflow/bin/tensorboard in my case) and replace
from tensorflow.tensorboard.tensorboard import main
by
from tensorflow.tensorboard.main import *
Now tensorboard should run from console as usual.
Tensorboard ships with tensorflow. If you are unable to run using tensorboard command, try below approach. tensorboard.py might have been moved to different directory.
Try searching for tensorboard.py in the tensorbard directory where tensorflow is installed. Go to the path and use following line for visualization:
python tensorboard.py --logdir=path
You should priorly launch
pip install tensorflow.tensorboard

ImportError: No module named examples.tutorials.mnist

I am very frustrated by this error, what I did is getting the code from tensor flow tutorial to import moist:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
However when I run the python shows:
File "/Users/kevinling/Desktop/Machine Learning/tensorflow.py", line 2, in
from tensorflow.examples.tutorials.mnist import input_data
ImportError: No module named examples.tutorials.mnist
When I check into the directory, the file is perfectly there:
And the directory is:
enter image description here
The input_data.py is like:
The input_data.py
Just rename your example from "tensorflow.py" to anything else and it will work. The interpreter is trying to import the necessary files from your script.
Did you already install tensorflow? If not, follow their install instructions or simply install using pip:
pip install tensorflow
Now, make sure you are NOT currently in a folder where tensorflow is located, and try running your script.
python your_script.py

Anaconda doesn't find module cv2

I am using Anaconda on OS X Mavericks. When I try loading cv2 I get an import error (see below). Do you know how to fix this?
>>import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/usr/local/Cellar/opencv/2.4.8.2/lib/python2.7/site-packages/cv2.so,
2): Library not loaded: /usr/local/lib/libpng15.15.dylib
Referenced from: /usr/local/Cellar/opencv/2.4.8.2/lib/libopencv_highgui.2.4.dylib
Reason: image not found
I am not sure it's relevant, but in /usr/local/lib/ I have libpng16.16.dylib instead of libpng15.15.dylib.
This is the solution I found:
comment the PYTHONPATH environment in ~/.bash_profile, as suggested by #asmeurer
install opencv using https://binstar.org/jjhelmus/opencv
As suggested in this issue, I fixed this problem by simply executing
conda update hdf5
you could also just add it to your PYTHONPATH. here's how:
you should be able to get it to load through one of the other (non anaconda) python executables. mine were located at:
/usr/bin/python (default system python) and /usr/local/bin/python (brew)
call the python executable using the full path
once you successfully import cv2 run: cv2.__file__
this will give you the file's path which you can then take (full directory path not including filename) and add as the first argument to your PYTHONPATH defined in ~/.bash_profile
after changing the .bash_profile don't forget to run
source ~/.bash_profile to make the changes effective
fire up anaconda python and it should now find cv2

Categories