1

I have a model which applies a custom function in the output layer. But the path to this function is static. Whenever I try to load the model on a different system it can not find the function because it searches the wrong path. Actually it uses the path in which the function was located at on the system I saved the model in the first place.

Here a example of the simplyfied Model:

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

The function I am speaking of is poly_transfer in the outpul layer. If I load my model with tensorflow.keras.models.load_model it searches as described in the wrong dir for poly_transfer and I get the error SystemError: unknown opcode. Is there a way to tell tensorflow.keras.models.load_model where helper_function.py (the skript of poly_transfer) lyes on a different system?

I use tensorflow 2.0.0.

Edit This is the error. Please note that the path d:/test_data_pros/restructured/helper_functions.py did only exist on the system the model was trained on. The system on which I load the model has the same skript with the same function but naturally on a different path.

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
7
  • What makes you think that the problem is "searching in the wrong dir"?
    – Dr. Snoopy
    Commented Dec 22, 2020 at 18:10
  • I train my model on a Windows-PC and load the model on a ubuntu system eventually. When I try to load the model on the ubuntu system it tells me that it opens the function poly_transfer form the path it was on the windows-pc. This path does not exist on the ubuntu-system.
    – Pm740
    Commented Dec 22, 2020 at 18:40
  • 1
    You would get a different error in that case, maybe you should include the full traceback in your question, more details always help.
    – Dr. Snoopy
    Commented Dec 22, 2020 at 18:41
  • Ok I updated my question. from pyproj import Proj, transform are the first lines of the helper_functions.py on my ubuntu-system. It differs a bit from the helper_functions.py on my windows-pc (the system the model was trained on). The helper_functions.py on the ubuntu system contains a some other functions but poly_transfer is located in both skripts and is exactly the same.
    – Pm740
    Commented Dec 22, 2020 at 18:54
  • 2
    You are probably using different python versions, since keras serialized the code in your custom function, it can only be loaded in the same python version, else you get the unknown opcode error
    – Dr. Snoopy
    Commented Dec 22, 2020 at 18:59

1 Answer 1

2

The problem has nothing to do with paths, when you saved your model, your custom function was serialized and saved inside the HDF5 by Keras, but this format is specific to a python version, so the file can only be loaded with the same python version (it could work with newer versions, but not with older versions of python).

So if you load your model on the same version of python, it should work fine.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.