# model init, loading data
cifar10_recognizer = Cifar10_Recognizer()
cifar10_recognizer.load('data/c10_model.ckpt')
@app.route('/tf/api/v1/SomePath', methods=['GET', 'POST'])
def upload():
X = []
if request.method == 'POST':
if 'photo' in request.files:
# place for uploading process workaround, obtaining input for tf
X = generate_X_c10(f)
if len(X) != 0:
# designing desired result here
answer = np.squeeze(cifar10_recognizer.predict(X))
top3 = (-answer).argsort()[:3]
res = ([cifar10_labels[i] for i in top3], [answer[i] for i in top3])
# you can simply print this to console
# return 'Prediction answer: {}'.format(res)
# or generate some html with result
return fk.render_template('demos/c10_show_result.html',
name=file,
result=res)
if request.method == 'GET':
# in html I have simple form to upload img file
return fk.render_template('demos/c10_classifier.html')
3 回答
有不同的方法来做到这一点 . 纯粹地说,使用张量流不是很灵活,但相对简单 . 此方法的缺点是您必须重建图形并初始化恢复模型的代码中的变量 . tensorflow skflow/contrib learn中显示的方式更优雅,但目前似乎没有功能,文档已过时 .
我在github here上放了一个简短的例子,它显示了如何将GET或POST参数命名为一个REST部署的tensorflow模型 .
然后主代码在一个函数中,该函数根据POST / GET数据获取字典:
这个github project显示了恢复模型检查点和使用Flask的工作示例 .
在线demo似乎很快 .
我不喜欢在flask restful文件中添加大量数据/模型处理代码 . 我通常有tf模型类等等 . 即它可能是这样的:
cifar10_recognizer.predict(X)是简单的func,它在tf会话中运行预测操作:
附:从文件中保存/恢复模型是一个非常漫长的过程,在提供post / get请求时尽量避免这种情况