2017-02-16 4 views
2

REST 끝점에서 호출되어 이미지의 URL로 전달되는 Tensorflow python 클래스가 있습니다. 새 요청이 시작될 때마다 로컬 .pb 파일을 읽는 create_graph 메서드를 호출합니다. 이 파일은 요청에서 요청으로 변경되지 않습니다. 따라서 각 요청시이 파일을 읽는 것이 리소스와 시간을 잘 사용하지 않는다고 생각합니다.Tensorflow의 각 요청에 대한 그래프 읽기 방지 방법

코드는 다음과 같습니다 :

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 전화에 어떤 식 으로든 먼저 요청한 다음 서비스가 다시 시작될 때까지 호출하지 마십시오.

+0

'before_first_request (f) ' 응용 프로그램의이 인스턴스에 대한 첫 번째 요청 전에 실행되는 함수를 등록합니다. 그게 어때? – metmirr

답변

0

게재 방법 : 프로세스마다 세션을 한 번만 만듭니다. Session.run()을 여러 번 호출 할 수 있습니다.

관련 문제