Linux上的load_model上的Keras分段错误而不是Windows

时间:2018-01-06 21:13:59

标签: linux python-3.x tensorflow segmentation-fault keras

我在Windows上编写了一段Python深度学习代码的原型,我无法在Linux上运行它。我发现问题来自load_model。 以下是在Windows和Linux中表现不同的Python代码。

两个Keras安装都是从Keras Team的github源代码库创建的,因为标准Keras软件包无法识别模型格式,最近针对Github源代码中的字符格式进行了修补。

你知道发生了什么吗?

代码:

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输出:

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输出:

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

法语 Erreur de segmentation 表示细分错误

感谢您的帮助!

Glassfrog

2 个答案:

答案 0 :(得分:2)

我只找到了解决方法。

由于模型文件是另一种格式的另一个权重文件的数据转换,我去为Keras的最新版本重新生成了Keras模型。

现在可行。

但我仍然不知道导致分段错误的原因。

答案 1 :(得分:0)

据我所知,段错误发生在模型创建时,但是我不知道为什么。 我可以通过独立保存模型和权重来调试它:

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')

在另一台机器上,我尝试从JSON文件加载模型,但也得到了segmentation fault

from keras.models import model_from_json
with open('model.json', 'r') as fp:
    model = model_from_json(fp.read())  # segfaults here

如果可以通过再次创建顺序模型在目标计算机上简单地重新创建模型,则可以简单地在模型中加载权重:

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