flask keras h5模型部署加载预测;遇到问题错误解决

flask keras运行预测结果不一致

参考:https://blog.csdn.net/qq_37781464/article/details/108842854

from flask import Flask
from flask import request
import numpy as np
import keras
from keras import models
import tensorflow as tf



#将network定义为全局变量
global sess,graph
#tf2.x中为sess = tf.compat.v1.keras.backend.get_session()
sess=keras.backend.get_session()
graph=tf.get_default_graph()

#在默认会话与计算图中进行模型的预测
with sess.as_default():
    with graph.as_default():
        output=network.predict(test_img)

self.sess = keras.backend.get_session()
self.graph = tf.get_default_graph()


with self.graph.as_default():
    with self.sess.as_default():
        # session.run(tf.global_variables_initializer())
        vec = self.encoder.predict([[token_ids], [segment_ids]])[0]
        # session.run(vec)
        print(vec)

flask keras h5模型部署加载预测;遇到问题错误解决_第1张图片

tf版本:1.15

from tensorflow.python.keras.models import  save_model,load_model
from deepctr.layers import custom_objects
from flask import Flask
from flask import render_template, request
import tensorflow as tf


graph = tf.get_default_graph()
model_fm = load_model(r'D:\用***epFM3.h5', custom_objects)

@app.route('/', methods=['GET', 'POST'])
def build_plot():
	with graph.as_default():
	  with tf.Session() as sess:
	    sess.run(tf.global_variables_initializer())
	    rank_predict2 = model_fm.predict(test_data1.rename(columns={'p_six':'p_sidx'}).to_dict('series'))
	    # print(rank_predict2)
	    ranks2 = list(np.argsort(rank_predict2[:,0])[::-1])
	    print(ranks2)
    return render_template('display.html', query=query, lis1=titles[:10], lis2=pics[:10], lis3=ranks1, lis5=ranks2)

if __name__ == '__main__':
  # app.debug = False
  # CORS(app, supports_credentials=True)
  # app.run()
  app.run("0.0.0.0", 5000, debug=True, threaded=True)
不用flask 直接调用

from tensorflow.python.keras.models import  save_model,load_model
from deepctr.layers import custom_objects
import pandas as pd

model_fm1 = load_model(r'D*****M3.h5', custom_objects)# load_model,just add a parameter

ts2 = pd.read_csv(r'D*****数据a.csv')
ks1 = model_fm1.predict(ts2.to_dict('series'))

print(ks1)

flask keras h5模型部署加载预测;遇到问题错误解决_第2张图片

你可能感兴趣的:(知识点)