Multithreading Flask App Throws Error with MRCNN

Viewed 174

I have a flask app running in thread true mode.

if __name__ == '__main__':
    setup.check_director()
    web_app.secret = "image-service"
    web_app.run(host='0.0.0.0', port=8084, debug=True, threaded=True)

I'm triggering mrcnn(https://github.com/matterport/Mask_RCNN) task using api call, The Predictor class looks like this

class Predictor(object):
    def __init__(self, config=None, weights=WEIGHTS_PATH, device=DEVICE):
        self.weights=weights
        self.config = ElementConfig() if config is None else config
        # self.el = Element_linker()

        self.logger.info("Loading Mask R-CNN model")
        print("Loading Mask R-CNN model and weights")
        start = time.time()

        self._graph = tf.Graph()

        with self._graph.as_default():
            self.model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=self.config)
            self.logger.info("Loading saved weights from {}".format(weights))
            self.model.load_weights(weights, by_name=True)
            self.init_g = tf.global_variables_initializer()
            self.init_l = tf.local_variables_initializer()
            print("Loading completed in %s seconds" % ((time.time() - start)))

    ....
    ....
    ....

    def predict(self):
        with tf.Session(graph=self._graph) as _session:
           _session.run(self.init_g)
           _session.run(self.init_l)
           results = _session.run(self.model.detect([image], verbose=0))

    ....
    ....
    ....
    ....

The code works fine when there is one api call at a time. When I have multiple api hitting at the same time, the code throws error as:

ValueError: Tensor Tensor("Placeholder:0", shape=(7, 7, 3, 64), dtype=float32) is not an element of this graph.

Detailed Log: https://gist.github.com/amandalmia14/70b8b3fb13bef8f545d9963bd609f2f2

I have also tried using different ways of organizing sessions and graphs. Have also tried all the solutions present on mrcnn github issues page, but nothing helped.

Is there an issue with threading or the approach mentioned above ?

0 Answers
Related