Tensorflow,Flask和TFLearn内存泄漏

时间:2016-07-31 17:35:47

标签: memory flask tensorflow

我正在运行以下程序,每次点击'build'API调用时,我都会看到在该过程完成后又占用了1 GB的内存。我试图从记忆中消除一切,但我不确定剩下的是什么。

import tensorflow as tf
import tflearn
from flask import Flask, jsonify
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression

app = Flask(__name__)

keep_prob = .8
num_labels = 3
batch_size = 64

class AlexNet():

    def __init__(self):

        @app.route('/build')
        def build():
            g = tf.Graph()
            with g.as_default():
                sess = tf.Session()

                # Building 'AlexNet'
                network = input_data(shape=[None, 227, 227, 3])
                network = conv_2d(network, 96, 11, strides=4, activation='relu')
                network = max_pool_2d(network, 3, strides=2)
                network = local_response_normalization(network)
                network = conv_2d(network, 256, 5, activation='relu')
                network = max_pool_2d(network, 3, strides=2)
                network = local_response_normalization(network)
                network = conv_2d(network, 384, 3, activation='relu')
                network = conv_2d(network, 384, 3, activation='relu')
                network = conv_2d(network, 256, 3, activation='relu')
                network = max_pool_2d(network, 3, strides=2)
                network = local_response_normalization(network)
                network = fully_connected(network, 4096, activation='tanh')
                network = dropout(network, keep_prob)
                network = fully_connected(network, 4096, activation='tanh')
                network = dropout(network, keep_prob)
                network = fully_connected(network, num_labels, activation='softmax')
                network = regression(network, optimizer="adam",
                                     loss='categorical_crossentropy',
                                     learning_rate=0.001, batch_size=batch_size)

                model = tflearn.DNN(network, tensorboard_dir="./tflearn_logs/",
                                    checkpoint_path=None, tensorboard_verbose=0, session=sess)

                sess.run(tf.initialize_all_variables())
                sess.close()

            tf.reset_default_graph()

            del g
            del sess
            del model
            del network
            return jsonify(status=200)


if __name__ == "__main__":
    AlexNet()
    app.run(host='0.0.0.0', port=5000, threaded=True)

1 个答案:

答案 0 :(得分:2)

我不确定您是否找到了答案,但恕我直言,您不应该在HTTP请求处理程序中放置长时间运行的任务。因为HTTP是无状态的,并且应该几乎立即响应呼叫。这就是我们拥有概念任务队列,异步任务等的原因。服务器端开发的经验法则是尽快响应请求。如果你试图在HTTP请求中构建一个卷积深度神经网络,它是不正常的,它是不可行的。 因为理想的HTTP请求应该在几秒钟内响应。您的DNN分类器会话运行可能需要太多秒(需要尝试)。

最讨厌的解决方案是在请求中创建一个python线程,让请求响应HTTP调用而不会阻塞。同时你的线程可以继续构建你的模型。然后你可以在某处写你的模型或发送邮件通知等。

你走了:

How can I add a background thread to flask?