问题描述
我有一个Tensorflow python类,它从REST端点调用并传入一个图像的URL。
每次启动新请求时,它都会调用create_graph
方法,该方法读取本地.pb
文件。
此文件不会从请求更改为请求。
所以,我觉得在每次请求时都不能很好地利用资源和时间来读取这个文件。
代码如下:
import numpy as np
import tensorflow as tf
import urllib2
class MyTensorflow:
def __init__(self, url):
self.imageUrl = imageUrl
def create_graph(self):
with tf.gfile.FastGFile("/path/to/model.pb", 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
def run_inference_on_image(self):
image_string = urllib2.urlopen(self.imageUrl).read()
with tf.Session() as sess:
...
...
return a_text_value
上面的代码是从flask_restful
调用的,如下所示:
c = my_tensorflow.MyTensorflow(args['url'])
c.create_graph()
returned = c.run_inference_on_image()
题
有没有办法只在第一次请求时调用create_graph
,然后在重新启动服务之前不调用它?
1楼
用于服务:每个进程仅创建一次会话。 您可以多次调用Session.run()。