【问题标题】:Keras segmentation fault on load_model on Linux and not on WindowsLinux 上 load_model 上的 Keras 分段错误,而不是 Windows
【发布时间】:2018-01-06 21:13:59
【问题描述】:

我制作了一段在 Windows 上运行的 Python 深度学习代码原型,但我无法让它在 Linux 上运行。我发现问题来自 load_model。 这是一段 Python 代码,它在 Windows 和 Linux 上的行为不同。

两个 Keras 安装均来自 Keras 团队的 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的意思是Segmentation fault

感谢您的帮助!

玻璃蛙

【问题讨论】:

  • 如果你能为此在 github 上提交一个 bug,那就太好了。它需要包含一些重现问题的方法或至少一些非常详细的日志,因为没有这些信息很难调试。
  • 我可以提交一个错误并提供模型文件,但它大约有 200 Mb。如果有方便的方式在某处发布它,请告诉我。
  • 我不知道这个有什么习惯的地方。

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


【解决方案1】:

我只找到了一种解决方法。

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

现在可以了。

但我仍然不知道是什么导致了分段错误。

【讨论】:

  • 嘿,我也面临同样的问题。您能否解释一下您实施的解决方案,因为我无法理解您上面解释的内容。谢谢!
  • 模型文件是从现有权重文件完成的转换。它没有针对我使用的 Keras 版本进行转换。我必须获取原始的权重文件并自己将其转换为我正在使用的 Keras 版本。
  • 这里遇到了同样的问题。我可以通过在两台机器上安装完全相同版本的 tf-nightly 来摆脱段错误,但现在我得到了一些其他错误。所以安装相同的版本会有所帮助,但前面可能还有其他问题......
【解决方案2】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2020-02-25
    • 2011-03-13
    • 2011-02-05
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 2018-07-31
    • 2018-12-20
    • 2020-04-25
    相关资源
    最近更新 更多