[Python] Requested the deserialization of a Lambda layer with a Python `lambda` inside it. you...

Discussão em 'Python' iniciado por Stack, Fevereiro 1, 2025.

  1. Stack

    Stack Membro Participativo

    This is a Flask project and keras model, I'm getting the following error when running

    "C:\Users\winne\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\layers\core\lambda_layer.py", line 394, in _parse_function_from_config
    raise ValueError(
    ValueError: Requested the deserialization of a Lambda layer with a Python `lambda` inside it. This carries a potential risk of arbitrary code execution and thus it is disallowed by default. If you trust the source of the saved model, you can pass `safe_mode=False` to the loading function in order to allow Lambda layer loading.
    LOAD MODEL ...


    when loading model using model_from_jason I defined custom object with safe_mode=False but still getting same error

    class ImagenetClassifier(object):
    def __init__(self):
    print('LOAD MODEL ...')
    with open('../Resource/line_model_predict.json', 'r') as f:
    self.l_model_predict = model_from_json(f.read(),custom_objects={"safe_mode": False})
    self.l_model_predict.load_weights('../Resource/iam_lines--12--17.373.h5')
    with open('../Resource/word_model_predict.json', 'r') as f:
    self.w_model_predict = model_from_json(f.read(),custom_objects={"safe_mode": False})
    self.w_model_predict.load_weights('../Resource/iam_words--15--1.791.h5')
    def predict_image(self, image_filename):
    try:
    with graph.as_default():
    pred_text_model_word, pred_text_model_line = predict(self.w_model_predict, self.l_model_predict, image_filename)
    return pred_text_model_word, pred_text_model_line
    except Exception as err:
    print('Prediction error: ', err)
    return (False, 'Something went wrong when predict the '
    'image. Maybe try another one?')

    Continue reading...

Compartilhe esta Página