Python 从烧瓶中得到结果
我已经用keras训练了一个深度学习模型(lstm),用Python 从烧瓶中得到结果,python,machine-learning,flask,keras,deep-learning,Python,Machine Learning,Flask,Keras,Deep Learning,我已经用keras训练了一个深度学习模型(lstm),用h5保存它,现在我想“点击”一个web服务以获得一个类别。这是我第一次尝试这样做,所以我有点困惑。我想不出该如何取回分类。当我向http://localhost:8000/predict我发现以下错误 The server encountered an internal error and was unable to complete your request. Either the server is overloaded or the
h5
保存它,现在我想“点击”一个web服务以获得一个类别。这是我第一次尝试这样做,所以我有点困惑。我想不出该如何取回分类。当我向http://localhost:8000/predict
我发现以下错误
The server encountered an internal error and was unable to complete your
request. Either the server is overloaded or there is an error in the
application.
在笔记本上
ValueError: Tensor Tensor("dense_3/Softmax:0", shape=(?, 6), dtype=float32)
is not an element of this graph.
我尝试了来自的解决方案,但不起作用
目前为止的代码如下
from flask import Flask,request, jsonify#--jsonify will return the data
import os
from keras.models import load_model
app = Flask(__name__)
model=load_model('lstm-final-five-Copy1.h5')
@app.route('/predict', methods= ["GET","POST"])
def predict():
df_final = pd.read_csv('flask.csv')
activities = df_final['activity'].value_counts().index
label = LabelEncoder()
df_final['label'] = label.fit_transform(df_final['activity'])
X = df_final[['accx', 'accy', 'accz', 'gyrx', 'gyry', 'gyrz']]
y = df_final['label']
scaler = StandardScaler()
X = scaler.fit_transform(X)
df_final = pd.DataFrame(X, columns = ['accx', 'accy', 'accz', 'gyrx',
'gyry', 'gyrz'])
df_final['label'] = y.values
Fs = 50
frame_size = Fs*2 # 200 samples
hop_size = frame_size # 40 samples
def get_frames(df_final, frame_size, hop_size):
N_FEATURES = 6 #x,y,z (acc,gut)
frames = []
labels = []
for i in range(0, len(df_final) - frame_size, hop_size):
accx = df_final['accx'].values[i: i + frame_size]
accy = df_final['accy'].values[i: i + frame_size]
accz = df_final['accz'].values[i: i + frame_size]
gyrx = df_final['gyrx'].values[i: i + frame_size]
gyry = df_final['gyry'].values[i: i + frame_size]
gyrz = df_final['gyrz'].values[i: i + frame_size]
# Retrieve the most often used label in this segment
label = stats.mode(df_final['label'][i: i + frame_size])[0][0]
frames.append([accx, accy, accz, gyrx, gyry, gyrz])
labels.append(label)
# Bring the segments into a better shape
frames = np.asarray(frames).reshape(-1, frame_size, N_FEATURES)
labels = np.asarray(labels)
return frames, labels
X, y = get_frames(df_final, frame_size, hop_size)
pred = model.predict_classes(X)
return jsonify({"Prediction": pred}), 201
if __name__ == '__main__':
app.run(host="localhost", port=8000, debug=False)
在“/predict”POST端点中,似乎没有返回任何值,这就是为什么没有按预期返回类别的原因 如果您想添加GET方法,您可以添加如下所述的内容
@app.route('/', methods=['GET'])
def check_server_status():
return ("Server Running!")
在POST方法中,您可以在端点返回您的预测
@app.route('/predict', methods=['POST'])
def predict():
# Add in other steps here
pred = model.predict_classes(X)
return jsonify({"Prediction": pred}), 201
据我所知,如果您没有安装pandas,您需要通过执行
pip install pandas
并将其作为导入pandas As pd
您还可以在/prediction
端点中添加“GET”方法,如:
@app.route("/predict", methods=["GET", "POST"])
尝试在方法列表中添加“GET”谢谢。当我添加“GET”时,此错误会发生内部服务器错误服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序出错。您能在问题中发布错误日志吗?谢谢您的回答。但是,我再次返回不允许使用的方法请求的URL不允许使用该方法。我怎样才能把预测作为回应返回呢?哦,是的。我通过添加“GET”对代码进行了编辑。您可以参考下面@satilog给出的答案,将预测作为响应返回。它不起作用,我按照您的建议和satilog的预测功能进行操作,但我返回内部服务器错误服务器遇到内部错误,无法完成您的请求。要么是服务器过载,要么是应用程序出错。@AKA我能问点什么吗。我认为我误导了整个过程。我在这里加载的数据与我在这里加载的模型相同。这样行吗?或者maybay我必须加载模型以前从未见过的数据?另外,我认为@app.route('/')def getmodel():return str(model)Right不需要此路由。我认为getmodel函数不是必需的。尝试删除/注释它,并提出类似“谢谢你的回答”的请求。是否有必要使用get函数?否。我刚才提到了它,以防您想使用GET而不是POST。感谢您的努力,但这仍然不起作用,我返回内部服务器错误服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序出错。您可以将所有预处理和数据读取步骤移到predict()端点函数之外(还可以将最终数据帧保存到csv并直接加载)在端点中可能只有与预测相关的处理,这可能有助于调试。我将所有预处理步骤移到上面的shell中。“现在预测”是您在上面指定的,但当我转到时,再次返回服务器上未找到请求的URL。如果您手动输入URL,请检查拼写并重试。