保存的模型无法加载包含自定义方法的图层

2024-09-20 23:00:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个在输出层应用自定义函数的模型。但是这个函数的路径是静态的。每当我尝试在不同的系统上加载模型时,它都找不到函数,因为它搜索了错误的路径。实际上,它使用的是我首先保存模型的系统上函数所在的路径

以下是简化模型的示例:

    from tensorflow.keras.models import Model
    from tensorflow.keras.losses import mse, mean_squared_error
    from tensorflow.keras.layers import Input, LSTM, Dense, Lambda
    from tensorflow.keras.optimizers import RMSprop
    from helper_functions import poly_transfer

    Input_layer = Input(shape=(x_train.shape[1],x_train.shape[2]))

    hidden_layer1 = LSTM(units=45, return_sequences=False,stateful=False)(Input_layer)
    hidden_layer3 = Dense(25,activation='relu')(hidden_layer1)

    speed_out = Lambda(poly_transfer)(hidden_layer3 )


    model = Model(inputs=[Input_layer], outputs=[speed_out])

    model.compile(loss=mse,
                optimizer=RMSprop(lr= 0.0005),
                metrics=['mae','mse'])

我所说的函数是outpul层中的poly_transfer。 如果我用tensorflow.keras.models.load_model加载我的模型,它会按照错误目录中所述搜索poly_transfer,然后得到错误SystemError: unknown opcode。 有没有办法告诉tensorflow.keras.models.load_model在不同的系统上helper_function.py(碱液的名称)碱液的位置

我使用tensorflow 2.0.0

编辑 这就是错误所在。请注意,路径d:/test_data_pros/restructured/helper_functions.py仅存在于模型培训的系统上。我加载模型的系统具有相同的SKcript,具有相同的功能,但自然位于不同的路径上

2020-12-22 19:43:10.841197: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-12-22 19:43:10.844407: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2699905000 Hz
2020-12-22 19:43:10.844874: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x475d920 executing computations on platform Host. Devices:
2020-12-22 19:43:10.844906: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): Host, Default Version
XXX lineno: 11, opcode: 160
Traceback (most recent call last):
  File "/home/ebike/workspaces/ebike2x_ws/src/pred_trajectory_pkg/src/trajectory_prediction_node.py", line 122, in <module>
    LSTM = lstm_s_g_model(t_pred)
  File "/home/ebike/workspaces/ebike2x_ws/src/pred_trajectory_pkg/src/vehicle_models.py", line 126, in __init__
    self.model = load_model('/home/ebike/workspaces/ebike2x_ws/src/pred_trajectory_pkg/ml_models/model_test_vivek_150ep.h5')
  File "/home/ebike/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/save.py", line 146, in load_model
    return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
  File "/home/ebike/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 168, in load_model_from_hdf5
    custom_objects=custom_objects)
  File "/home/ebike/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/model_config.py", line 55, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "/home/ebike/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/layers/serialization.py", line 102, in deserialize
    printable_module_name='layer')
  File "/home/ebike/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/utils/generic_utils.py", line 191, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/ebike/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py", line 906, in from_config
    config, custom_objects)
  File "/home/ebike/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py", line 1852, in reconstruct_from_config
    process_node(layer, node_data)
  File "/home/ebike/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py", line 1799, in process_node
    output_tensors = layer(input_tensors, **kwargs)
  File "/home/ebike/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 842, in __call__
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "/home/ebike/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/layers/core.py", line 795, in call
    return self.function(inputs, **arguments)
  File "d:/test_data_pros/restructured/helper_functions.py", line 11, in poly_transfer
    from pyproj import Proj, transform
SystemError: unknown opcode

Tags: infrompycorehomemodellibpackages
1条回答
网友
1楼 · 发布于 2024-09-20 23:00:40

问题与路径无关,当您保存模型时,Keras将自定义函数序列化并保存在HDF5中,但此格式特定于python版本,因此只能使用相同的python版本加载该文件(可以使用较新版本,但不能使用较旧版本的python)

因此,如果在同一版本的python上加载模型,它应该可以正常工作

相关问题 更多 >

    热门问题