Keras + Flask 提供接口服务的坑~~~
最近在搞Keras,训练完的模型要提供个预测服务出来。就想了个办法,通过Flask提供一个http服务,后来发现也能正常跑,但是每次预测都需要加载模型,效率非常低。
然后就把模型加载到全局,每次要用的时候去拿来用就行了,可是每次去拿的时候,都会报错.
如:
ValueError: Tensor Tensor(**************) is not an element of this graph.
这个问题就是在你做预测的时候,他加载的图,不是你第一次初始化模型时候的图,所以图里面没有模型里的那些参数和节点
在网上找了个靠谱的解决方案,亲测有效,原文:https://wolfx.cn/flask-keras-server/
解决方式如下:
When you create a Model, the session hasn't been restored yet. All placeholders, variables and ops that are defined in Model.init are placed in a new graph, which makes itself a default graph inside with block. This is the key line:
with tf.Graph().as_default():
...
This means that this instance of tf.Graph() equals to tf.get_default_graph() instance inside with block, but not before or after it. From this moment on, there exist two different graphs.
When you later create a session and restore a graph into it, you can't access the previous instance of tf.Graph() in that session. Here's a short example:
with tf.Graph().as_default() as graph:
var = tf.get_variable("var", shape=[3], initializer=tf.zeros_initializer)
This works
with tf.Session(graph=graph) as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(var)) # ok because `sess.graph == graph`
This fails
saver = tf.train.import_meta_graph('/tmp/model.ckpt.meta')
with tf.Session() as sess:
saver.restore(sess, "/tmp/model.ckpt")
print(sess.run(var)) # var is from `graph`, not `sess.graph`!
The best way to deal with this is give names to all nodes, e.g. 'input', 'target', etc, save the model and then look up the nodes in the restored graph by name, something like this:
saver = tf.train.import_meta_graph('/tmp/model.ckpt.meta')
with tf.Session() as sess:
saver.restore(sess, "/tmp/model.ckpt")
input_data = sess.graph.get_tensor_by_name('input')
target = sess.graph.get_tensor_by_name('target')
This method guarantees that all nodes will be from the graph in session.
Try to start with:
import tensorflow as tf
global graph,model
graph = tf.get_default_graph()
When you need to use predict:
with graph.as_default():
y = model.predict(X)
------------------------------------------------------------华丽的分割线------------------------------------------------------------
下面就上我自己的代码,解释一下如何使用:
我原来的代码是这样的:
def get_angiogram_time(video_path):
start = time.time()
global _MODEL_MA,_MODEL_TIME,_GRAPH_MA, _GRAPH_TIME
if _MODEL_MA == None:
model_ma = ma_ocr.Training_Predict()
model_time = time_ocr.Training_Predict() model_ma.build_model()
model_time.build_model() model_ma.load_model("./model/ma_gur_ctc_model.h5base")
model_time.load_model("./model/time_gur_ctc_model.h5base") _MODEL_MA = model_ma
_MODEL_TIME = model_time indexes = _MODEL_MA.predict(video_path)
time_dict = _MODEL_TIME.predict(video_path,indexes)
end = time.time()
print("耗时:%.2f s" % (end-start))
return json.dumps(time_dict)
def predict(self, video_path):
start = time.time() vid = cv2.VideoCapture(video_path)
if not vid.isOpened():
raise IOError("Couldn't open webcam or video")
# video_fps = vid.get(cv2.CAP_PROP_FPS) X = self.load_video_data(vid)
y_pred = self.base_model.predict(X)
shape = y_pred[:, :, :].shape # 2:
out = K.get_value(K.ctc_decode(y_pred[:, :, :], input_length=np.ones(shape[0]) * shape[1])[0][0])[:,
:seq_len] # 2:
print()
当实行到第10行 :y_pred = self.base_model.predict(X)
就会抛错:Cannot use the given session to evaluate tensor: the tensor's graph is different from the session's graph.
大致意思就是:当前session里的图和模型中的图的各种参数不匹配
修改后代码:
def get_angiogram_time(video_path):
start = time.time()
global _MODEL_MA,_MODEL_TIME,_GRAPH_MA, _GRAPH_TIME
if _MODEL_MA == None:
model_ma = ma_ocr.Training_Predict()
model_time = time_ocr.Training_Predict() model_ma.build_model()
model_time.build_model() model_ma.load_model("./model/ma_gur_ctc_model.h5base")
model_time.load_model("./model/time_gur_ctc_model.h5base") _MODEL_MA = model_ma
_MODEL_TIME = model_time
_GRAPH_MA = tf.get_default_graph()
_GRAPH_TIME = tf.get_default_graph() with _GRAPH_MA.as_default():
indexes = _MODEL_MA.predict(video_path)
with _GRAPH_TIME.as_default():
time_dict = _MODEL_TIME.predict(video_path,indexes)
end = time.time()
print("耗时:%.2f s" % (end-start))
return json.dumps(time_dict)
主要修改在第16,17,19,21行
定义了一个全局的图,每次都用这个图
完美解决~
PS:问了一下专门做AI的朋友,他们公司是用TensorFlow Server提供对外服务的,我最近也要研究一下Tensorflow Server,本人是个AI小白,刚刚入门,写的不对还请指正,谢谢!
Keras + Flask 提供接口服务的坑~~~的更多相关文章
- 开发FTP服务接口,对外提供接口服务
注意:本文只适合小文本文件的上传下载,因为post请求是有大小限制的.默认大小是2m,虽然具体数值可以调节,但不适合做大文件的传输 最近公司有这么个需求:以后所有的项目开发中需要使用ftp服务器的地方 ...
- 获取 exception 对象的字符串形式(接口服务返回给调用者)
工具类: package com.taotao.common.utils; import java.io.PrintWriter; import java.io.StringWriter; publi ...
- Python flask 基于 Flask 提供 RESTful Web 服务
转载自 http://python.jobbole.com/87118/ 什么是 REST REST 全称是 Representational State Transfer,翻译成中文是『表现层状态转 ...
- windows下apache + mod_wsgi + python部署flask接口服务
windows下apache + mod_wsgi + python部署flask接口服务 用python3安装虚拟环境 为啥要装虚拟环境? 原因1:安装虚拟环境是为了使项目的环境和全局环境隔离开,在 ...
- docker&flask快速构建服务接口(二)
系列其他内容 docker快速创建轻量级的可移植的容器✓ docker&flask快速构建服务接口✓ docker&uwsgi高性能WSGI服务器生产部署必备 docker&g ...
- 怎样提供一个好的移动API接口服务/从零到一[开发篇]
引语:现在互联网那么热,你手里没几个APP都不好意思跟别人打招呼!但是,难道APP就是全能的神吗?答案是否定的,除了优雅的APP前端展示,其实核心还是服务器端.数据的保存.查询.消息的推送,无不是在服 ...
- FF.PyAdmin 接口服务/后台管理微框架 (Flask+LayUI)
源码(有兴趣的朋友请Star一下) github: https://github.com/fufuok/FF.PyAdmin gitee: https://gitee.com/fufuok/FF.Py ...
- 亿级用户下的新浪微博平台架构 前端机(提供 API 接口服务),队列机(处理上行业务逻辑,主要是数据写入),存储(mc、mysql、mcq、redis 、HBase等)
https://mp.weixin.qq.com/s/f319mm6QsetwxntvSXpKxg 亿级用户下的新浪微博平台架构 炼数成金前沿推荐 2014-12-04 序言 新浪微博在2014年3月 ...
- springboot+CXF开发webservice对外提供接口(转)
文章来源:http://www.leftso.com/blog/144.html 1.项目要对外提供接口,用webservcie的方式实现 2.添加的jar包 maven: <dependenc ...
随机推荐
- bugku love
emmm....控制台运行一下 接着查一下是否有壳 显示是没有壳的,接着查看一下结构 也没有什么发现,上ida看一下吧,顺便说一句每个人的解题思路都不一样.面对一开始都不清楚的结构我会选择交叉引用这样 ...
- Day7 - E - Strange Way to Express Integers POJ - 2891
Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative ...
- java核心-多线程(4)-线程类基础知识
1.并发 <1>使用并发的一个重要原因是提高执行效率.由于I/O等情况阻塞,单个任务并不能充分利用CPU时间.所以在单处理器的机器上也应该使用并发. <2>为了实现并发,操作系 ...
- SpringBoot-属性直接注入
SpringBoot-属性直接注入 SpringBoot-属性直接注入 上面我们说到,如果公共的属性,我们可以使用Java类加载Properties文件,来达到复用的目的,在SpringBoot中,我 ...
- linux内核链表的实现
.\linux-2.6.22.6_vscode\include\linux\list.h #ifndef _LINUX_LIST_H#define _LINUX_LIST_H #ifdef __KER ...
- 觉醒 闭包/this
两条非常重要的知识点 首先上题 var a = '322' var preview = function() { var a = '123' console.log(a, this, this.a) ...
- 73.Python中ORM聚合函数详解:Count
Count:用来求某个数据的个数. 在以下所有的示例中所采用的模型为: from django.db import models # 定义作者模型 class Author(models.Model) ...
- 一百零六、SAP的OOP面向对象编程,OO-ALV的简介
面向对象编程,如图 基本概念: 1.对象(Object)是一个现实实体的抽象.一个对象可被认为是一个把数据(属性)和程序(方法)封装在一起的实体,这个程序产生该对象的动作或对它接受到的外界信号的反应. ...
- 笔记本如何不按Fn键就能实现F键的功能
笔记本的F1~F12键的附带功能如何改成 不用按Fn键就能实现F1~F12的功能 本人现在使用的是一款ThinkPad的本本,之前在台式机上愉快的玩耍的时候键盘上的F键直接按一下就可以实现相应的功能, ...
- Spring开发环境搭建(Eclipse)
开发环境搭建,主要包含2部分: Java安装 Eclipse安装 为易于学习,我们只安装这2个部分,对于一般开发学习也足够了.如果你有其他要安装的,酌情添加. Java安装 我们使用Java8: 下载 ...