【问题标题】:Tensorflow.keras.Model served by a Flask app + uwsgi gets stuck in model.predictFlask 应用程序 + uwsgi 提供的 Tensorflow.keras.Model 卡在 model.predict 中
【发布时间】:2019-11-18 17:21:41
【问题描述】:

我正在尝试使用 Tensorflow v1.14 在 Flask + nginx + uwsgi 应用程序中提供 tensorflow.keras.Model

我在 Flask 的应用程序工厂函数中名为 Prediction 的类的构造函数中加载模型,并将图形保存为 Flask 应用程序,建议 here

然后我通过在我的名为 _process 的路由中调用方法 Prediction.process 来运行预测Flask 应用程序,但在调用 tf.keras.Model.predict 期间卡住了(执行 predict.py 中的self.model.summary(),即显示摘要,但不显示print("Never gets here :("))。

如果我在 _process 中初始化我的类 Prediction(我想避免不必加载每个预测的模型),一切正常。

如果我使用 Flask 服务器,它也可以正常工作。所以看起来和uwsgi config有关。

有什么建议吗?

init.py

def create_app():
    app = Flask(__name__)
    #(...)
    app.register_blueprint(bp)
    load_tf_model(app)
    return app


def load_tf_model(app):

    sess = tf.Session(graph=tf.Graph())
    app.sess = sess

    with sess.graph.as_default():
        weights =  os.path.join(app.static_folder, 'weights/model.32-0.81.h5')
        app.prediction = Prediction(weights)

predict.py

class Prediction:

    def __init__(self,  weights):

        # build model and set weights
        inputs = tf.keras.Input(shape=SHAPE, batch_size=1)
        outputs = simple_cnn.build_model(inputs, N_CLASSES)
        self.model = tf.keras.Model(inputs=inputs, outputs=outputs)
        self.model.load_weights(weights)
        self.model._make_predict_function()

        # create TF mel extractor
        self.melspec_ex = tf_feature_utils.MelSpectrogram()


    def process(self, audio, sr):

        # compute features (in NCHW format) and labels
        data = audio2data(
            audio,
            sr,
            class_list=np.arange(N_CLASSES))
        features = np.asarray([d[0] for d in data])
        features = tf.reshape(features, (features.shape[0], 1, features.shape[1], features.shape[2]))
        labels = np.asarray([d[1] for d in data])

        # make tf.data.Dataset
        dataset = tf.data.Dataset.from_tensor_slices((features, labels))
        dataset = dataset.batch(1)
        dataset = dataset.map(lambda data, labels: (
            tf.expand_dims(self.melspec_ex.process(tf.squeeze(data, axis=[1,2])), 1)))

        # show model (debug)
        self.model.summary()

        # run prediction
        predictions = self.model.predict(dataset)

        print("Never gets here :(")

        # integrate predictions over time
        return np.mean(predictions, axis=0)

routes.py

@bp.route('/_process', methods=['POST'])
def _process():

    with current_app.graph.as_default():

        # load audio
        filepath = session['filepath']
        audio, sr = librosa.load(filepath)

        # predict
        predictions =  current_app.prediction.process(audio, sr)

        # delete file
        os.remove(filepath)

        return jsonify(prob=predictions.tolist())

【问题讨论】:

    标签: python tensorflow flask uwsgi


    【解决方案1】:

    这是一个线程问题。我必须使用以下选项添加配置 uwsgi:

    master = false 
    processes = 1
    cheaper = 0
    

    【讨论】:

      猜你喜欢
      • 2015-09-29
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 2015-09-18
      • 2023-03-09
      • 2012-12-26
      • 2019-07-13
      • 2016-01-19
      相关资源
      最近更新 更多