jeudi 6 juin 2019

how can fix the problem "Memory cannot be recycled" in my web service build by tensorflow + flask

I am building a flask app with facenet, because of some situation,I could not use tensorflow serving to load my model, and it memory grow after every request .

import numpy as np
from flask import Flask, request
import tensorflow as tf

from tensorflow.python.platform import gfile
from app.utils.log_util import LogUtils
log = LogUtils.get_stream_logger(__name__)

app = Flask(__name__)

with gfile.FastGFile('../20190517-152605.pb', 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

graph = tf.Graph()
with graph.as_default():
    tf.import_graph_def(graph_def, input_map=None, name='')
    # print(tf.get_default_graph().as_graph_def().node)
    images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
    embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
    phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
    model = {
        'images_placeholder': images_placeholder,
        'embeddings': embeddings,
        'phase_train_placeholder': phase_train_placeholder
    }

sess = tf.Session(graph=graph)

@app.route('/', methods=['POST'])
def classify():
    log.info('ss')
    # data = request.files.get('data').read()
    face = np.load("test.npy")
    images_placeholder = model['images_placeholder']
    embeddings = model['embeddings']
    phase_train_placeholder = model['phase_train_placeholder']
    result = sess.run(embeddings, feed_dict={images_placeholder: face, phase_train_placeholder: False})
    print(result)
    return str(200)

app.run

memory grow situation : request times: |1 | 2 |4 ... |100 ... n memory: |400+MB |600+MB | 700MB ... |2GB ... 2GB

Aucun commentaire:

Enregistrer un commentaire