I prototyped a Python deep learning piece of code working on Windows and I can't make it work on Linux. I identified that the problem comes from load_model.
Here is the piece of Python code that behaves differently on Windows and in Linux.
Both Keras installations were made from the github source repository from Keras Team because the model format is not recognized by the standard Keras package, a patch was done very recently for the characters format in the Github source code.
Do you have an idea of what's going on?
The code:
from keras.models import load_model, Model
import sys
import keras
import tensorflow as tf
import os
import platform
print("----------------------------------------------")
print("Operating system:")
print (os.name)
print(platform.system())
print(platform.release())
print("----------------------------------------------")
print("Python version:")
print(sys.version)
print("----------------------------------------------")
print("Tensorflow version: ", tf.__version__)
print("----------------------------------------------")
print("Keras version : ", keras.__version__)
print("----------------------------------------------")
yolo_model = load_model("model.h5")
Windows output:
Using TensorFlow backend.
----------------------------------------------
Operating system:
nt
Windows
7
----------------------------------------------
Python version:
3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)]
----------------------------------------------
Tensorflow version: 1.4.0
----------------------------------------------
Keras version : 2.1.2
----------------------------------------------
2018-01-06 21:54:37.700794: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instruc
ions that this TensorFlow binary was not compiled to use: AVX AVX2
C:\Users\David\AppData\Local\Programs\Python\Python36\lib\site-packages\keras-2.1.2-py3.6.egg\keras\models.py:252: UserWarning: No training configuration found
in save file: the model was *not* compiled. Compile it manually.
Linux output:
Using TensorFlow backend.
----------------------------------------------
Operating system:
posix
Linux
4.9.0-5-amd64
----------------------------------------------
Python version:
3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170118]
----------------------------------------------
Tensorflow version: 1.4.1
----------------------------------------------
Keras version : 2.1.2
----------------------------------------------
----------------------------------------------
2018-01-06 21:47:58.099715: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX
Erreur de segmentation
The french Erreur de segmentation means Segmentation fault
Thank you for your help!
Glassfrog
I only found a workaround.
As the model file was a data conversion from another weights file in another format, I went and regenerated the Keras model for the latest version of Keras.
Now It works.
But I still don't know what caused the segmentation fault.
From what I can tell, the segfault happens at the model creation, but I have no idea why.
I could debug this by saving the model and weights independently:
from keras.models import load_model
x = load_model('combined_model.h5') # runs only on the source machine
with open('model.json', 'w') as fp:
fp.write(x.to_json())
x.save_weights('weights.h5')
on the other machine I tried to load the model from the JSON file, but got the segmentation fault as well:
from keras.models import model_from_json
with open('model.json', 'r') as fp:
model = model_from_json(fp.read()) # segfaults here
If it is possible to simply re-create the model on the target machine by creating the Sequential model again, you can simply load the weights in the model:
from keras import Sequential
# ...
new_model = Sequential()
# [...] run your model creation here...
new_model.load_weights('weights.h5')
new_model.predict(...) # this should work now
Related
Would you help me to achieve reproducible results with Tensorflow 1.15 without restarting Python kernel. And why the output results in TF 2.0 and TF 1.5 are different with absolutely identical parameters and dataset? Is it possible to achieve identical output?
More details:
I tried to interpret model results in TF 2.0 by:
import shap
background = df3.iloc[np.random.choice(df3.shape[0], 100, replace=False)]
explainer = shap.DeepExplainer(model, background)
I recieved an error:
`get_session` is not available when using TensorFlow 2.0.`get_session` is not available when using TensorFlow 2.0.
According to the SO topic, I tried to setup TF 2.0 compatibility with TF 1 by using in the front of my code:
import tensorflow.compat.v1 as tf
But the error appeared again.
Following advice by many users, I downgraded TF2 to TF 1.15 it solved the problem, and shap module interprets the results but:
1) to make results reproducible now I have to change tf.random.set_seed(7) on tf.random.set_random_seed(7) and restart the Python kernel every time! In TF2 I didn't have to restart the kernel.
2) prediction results has been changed, especially, Economical efficiency (that is, TF1.5. wrongly classifies more important samples than TF2.0).
TF 2:
Accuracy: 94.95%, Economical efficiency = 64%
TF 1:
Accuracy: 94.85%, Economical efficiency = 56%
The code of the model is here
First, results differ from each other not only in TF1 and TF2 versions, but also in TF2.0 and TF2.2 versions. Probably, it depends on diffenent internal parameters in the packages.
Second, TensorFlow2 works with DeepExplainer in the following versions:
import tensorflow
import pandas as pd
import keras
import xgboost
import numpy
import shap
print(tensorflow.__version__)
print(pd.__version__)
print(keras.__version__)
print(xgboost.__version__)
print(numpy.__version__)
print(shap.__version__)
output:
2.2.0
0.24.2
2.3.1
0.90
1.17.5
0.35.0
But you will face some difficulties in updating the libraries.
In Python 3.5, running TF2.2, you will face the error 'DLL load failed: The specified module could not be found'.
It 100% can be solved by installing newer C++ package. See this:https://github.com/tensorflow/tensorflow/issues/22794#issuecomment-573297027
Link to download the package:https://support.microsoft.com/ru-ru/help/2977003/the-latest-supported-visual-c-downloads
In Python 3.7 you will not find the shap 0.35.0 version with whl extention. Only tar.gz extension which gives the error: "Install visual c++ package". But installation doesn't help.
Then download shap 0.35.0 for Python 3.7 here: https://anaconda.org/conda-forge/shap/files. Run Anaconda shell. Type: conda install -c conda-forge C:\shap-0.35.0-py37h3bbf574_0.tar.bz2.
I'm trying to import keras using TensorFlow 2.0 RC and using Python 3.6.3.
import tensorflow as tf
from tensorflow import keras
I'm getting an error when using the SPYDER IDE:
ImportError: cannot import name 'keras' from 'tensorflow'
I've tried searching on google for an answer but no avail. Any help would be appreciated :) !
From Keras repo.:
Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano.Before installing Keras, please install one of its backend engines: TensorFlow, Theano, or CNTK. We recommend the TensorFlow backend.
So Keras is a skin (an API). TensorFlow has decided to include this skin inside itself as tf.keras. Since Keras provides APIs that TensorFlow has already implemented (unless CNTK and Theano overtake TensorFlow which is unlikely).
So, we can use simply install keras by using pip
pip install keras
kerasis an additional package which uses tensorflow (or tensorflow-gpu) as backend. You have to install keras too
python -m pip install keras
Now you can use it
>>> import keras
Using TensorFlow backend.
>>> print(keras.__version__)
2.2.4
>>>
It seems to be kinda new that keras is delivered with tensorflow (or my information are old...). Anyway I tested your way and it works for me:
python -m pip install tensorflow==2.0RC
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>> from tensorflow import keras
>>> print(keras.__version__)
2.2.4-tf
>>> print(tensorflow.__version__)
2.0.0-rc0
Have you tried try to upgrade / reinstall your packages?
I'm facing a weird issue.
I'm working in Pycharm IDE and I want to use keras with tensorflow backend.
When I try to import keras or tensorflow pycharm process finished with exit code -1073741819 (0xC0000005).
For example
import tensorflow as tf
n_input = 2
print (n_input)
returns message described above without printing anything.
If i comment the tf import it works.
Keras and tensorflow are correctly installed under the python interpreter of my choice.
I'm on windows machine, Python 3.6.8
Thanks
I have installed tensorflow on a Raspberry PI 3 and it worked correctly (following these instructions). After installing a number of libraries that I needed for loading a Keras model trained on my PC (numpy, scipy etc) I have observed the following error while importing tensorflow:
Runtime Error: module compiled against API version 0xc but this version of numpy is 0xa
Segmentation fault
I have updated all the necessary libraries, and importing any of them doesn't result in an error in a Python 3 script. Also the error is not keras related, because the statement import tensorflow raises an error no matter what follows after it. Should I uninstall tensorflow and start all over again?
The version of linux I am using is ubuntu 14.04. I wanted to know my tensorflow version, so I ran a script given in the tensorflow directory tf_version_script.lds with the command ld tf_version_script.lds and it gave this error-
ld:/home/me/tensorflow/tensorflow/tf_version_script.lds:1: syntax error
I had the directory mounted. Why is this happening?
tf_version_script.lds is a linker version script used to specify to the GNU linker what symbols to make global, and what symbols to keep local. It has nothing to do with TensorFlow version, although I agree that its name is somewhat misleading.
If you have installed TensorFlow via the pip package, you can run the following to know what version of TensorFlow is installed :
$ pip freeze | grep tensorflow
# prints tensorflow==0.9.0
Or, if you have installed TensorFlow from sources, you can also print out the version in Python as follows :
import tensorflow as tf
print(tf.__version__)
# print '0.9.0'
In >jupyter notebook
import tensorflow as tf
import keras as ks
print("TensorFlow version : ")
print(tf.__version__)
print("Keras version : ")
print(ks.__version__)
or
print("TensorFlow version : " + tf.__version__)
print("Keras version : " + ks.__version__)
Out >Output after run
TensorFlow version :
1.14.0
Keras version :
2.2.4
or
TensorFlow version : 1.14.0
Keras version : 2.2.4