【问题标题】:Loading multiple models in Python to do comparison between results of model在 Python 中加载多个模型以比较模型的结果
【发布时间】:2018-11-07 16:36:05
【问题描述】:

我正在尝试使用两种不同的模型来预测类别。 这些模型使用 .json 和 .h5 文件保存和加载。

我遇到的问题是 50% 的时间一切正常,两个模型都正确创建并且能够进行预测。

有一半的时间,模型的创建似乎出了点问题,因为那时,当我尝试预测某些事情时,它会给我一个像这样的错误

InvalidArgumentError: You must feed a value for placeholder tensor 'input_1_1' with dtype float and shape [?,10693]
 [[{{node input_1_1}} = Placeholder[dtype=DT_FLOAT, shape=[?,10693], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

单独/不同时运行两个模型效果很好。没有任何问题。

经过一番谷歌搜索后,我发现 Keras 保留了模型的全局图,这可能会导致错误。我觉得我已经尝试了我在网上找到的大部分建议,但我似乎仍然无法解决在一个程序中同时拥有两个不同模型的问题。

因此,根据给定的参数,我想使用模型 1 或模型 2 进行预测:

def predict_using_cnn(text, model, tokenizer, to_predict):
if to_predict == 'functiegroep':
    with graph.as_default():
        sequences_test = tokenizer.texts_to_sequences([text])
        X_test = pad_sequences(sequences_test, maxlen=6616)  # TODO fix this hardcoded thingy
        y_pred = model.predict(X_test)
    return build_response(y_pred, to_predict)
elif to_predict == 'kernfunctie':
    with graph.as_default():
        sequences_test = tokenizer.texts_to_sequences([text])
        X_test = pad_sequences(sequences_test, maxlen=10693)  # TODO fix this hardcoded thingy
        y_pred = model.predict(X_test)
    return build_response(y_pred, to_predict)

这是有时它进入第一个 if 语句的地方,认为它需要输入 10693,这是 other 模型。所以这个程序似乎把这些东西随意混在一起了。

两个模型都使用相同的函数加载(当然它们被保存到两个不同的变量中)

def load_cnn_model(model_name):
print("loading" + str(model_name))
json_file = open(str(model_name) + '.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights(str(model_name) + '.h5')
return model

非常感谢任何帮助我解决此问题的方法!

【问题讨论】:

    标签: python-2.7 tensorflow flask keras artificial-intelligence


    【解决方案1】:

    我最终让它像这样工作:

    graph = tf.Graph()
    with graph.as_default():
        session = tf.Session()
        with session.as_default():
            graph_fg = graph
            session_fg = session
            cnn_fg_model = predict.load_cnn_model('models/cnn/new_model_fg/new_model_fg')
    
    with graph.as_default():
        session = tf.Session()
        with session.as_default():
            graph_kf = graph
            session_kf = session
            cnn_kf_model = predict.load_cnn_model('models/cnn/new_model_kf/new_model_kf')
    
    with graph.as_default():
        with session.as_default():
            if to_predict == 'functiegroep':
                sequences_test = tokenizer.texts_to_sequences([text])
                X_test = pad_sequences(sequences_test, maxlen=6616)  # TODO fix this hardcoded thingy
                y_pred = model.predict(X_test)
                return build_response_cnn(y_pred, to_predict)
            elif to_predict == 'kernfunctie':
                sequences_test = tokenizer.texts_to_sequences([text])
                X_test = pad_sequences(sequences_test, maxlen=10693)  # TODO fix this hardcoded thingy
                y_pred = model.predict(X_test)
                return build_response_cnn(y_pred, to_predict)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 2020-03-18
      • 2017-01-24
      • 2018-12-31
      相关资源
      最近更新 更多