我在Windows上制作了一段Python深度学习代码的原型,但我无法让它在Linux上运行。我确定问题来自load_model。这是一段在Windows和Linux上行为不同的Python代码。
这两个 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")
窗口输出:
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_jenkinshomeworkspacerel-winMwindowsPY36tensorflowcoreplatformcpu_feature_guard.cc:137] Your CPU supports instruc
ions that this TensorFlow binary was not compiled to use: AVX AVX2
C:UsersDavidAppDataLocalProgramsPythonPython36libsite-packageskeras-2.1.2-py3.6.eggkerasmodels.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 错误
感谢您的帮助!
玻璃蛙
我只找到了解决方法。
由于模型文件是从另一种格式的另一个权重文件转换而来的,因此我为最新版本的 Keras 重新生成了 Keras 模型。
现在它工作了。
但我仍然不知道是什么导致了分段错误。
据我所知,段错误发生在模型创建时,但我不知道为什么。我可以通过独立保存模型和权重来调试它:
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