1

I'm setting up a flask server which loads my mxnet model and has a predict-Api-method.

While testing the api I noticed, that the prediction has a timeout on the second call in the mxnet api. By timeout I mean that python is stuck in one mxnet method and seems to run endless.

I am using python-flask and mxnet v. 1.4.1. I tried upgrading to mxnet v. 1.5.0 but nothing chaned and the error persists.

I already tried different implementations for the predict method (see below), but both timeout. When I switch to a keras backend, everything works fine, but I need to use mxnet.

I used this guide for the forward-prediction: https://mxnet.incubator.apache.org/versions/master/tutorials/python/predict_image.html

import mxnet as mx
from collections import namedtuple

Batch = namedtuple('Batch', ['data'])

class MxnetBackend:
    def __init__(self):
        print("MXNet Version:", mx.__version__)
        self.sym, self.arg_params, self.aux_params = mx.model.load_checkpoint(prefix='models/kc_mxnet', epoch=0)
        self.mod = mx.mod.Module(symbol=self.sym, 
                    data_names=['/dense_1_input1'], 
                    context=mx.cpu(), 
                    label_names=None)
        self.mod.bind(for_training=False, 
                data_shapes=[('/dense_1_input1', (1, 1, 512, 3010))], 
                label_shapes=self.mod._label_shapes)
        self.mod.set_params(self.arg_params, self.aux_params, allow_missing=True)

    def predict(self, X):#this timeouts on second call
        """
        gets ndarray
        returns ndarray
        """
        X = mx.nd.array(X)
        self.mod.forward(Batch(X))
        res = self.mod.get_outputs()[0].asnumpy()
        return res

    def predict2(self, X):#this timeouts on second call, too
        """
        gets ndarray
        returns ndarray
        """
        return self.mod.predict(X).asnumpy()

On the first call mxnet works fine. I expect a return value on the second call to mxnet. How do I fix this?

M.Winkens
  • 140
  • 2
  • 11

0 Answers0