matterport / Mask_RCNN

Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow
Other
24.7k stars 11.71k forks source link

Using with Flask and TF 2.5.0 rc3 #2690

Open TACIXAT opened 3 years ago

TACIXAT commented 3 years ago
# app.py
# run with: flask run
from flask import Flask

app = Flask(__name__)

import tensorflow as tf
import numpy as np

from mrcnn import model as modellib
from mrcnn.config import Config
import cv2

class BaseConfig(Config):
    # give the configuration a recognizable name
    NAME = "IMGs"

    # set the number of GPUs to use training along with the number of
    # images per GPU (which may have to be tuned depending on how
    # much memory your GPU has)
    GPU_COUNT = 1
    IMAGES_PER_GPU = 2

    # number of classes (+1 for the background)
    NUM_CLASSES = 1 + 1

    # Most objects possible in an image
    TRAIN_ROIS_PER_IMAGE = 100

class InferenceConfig(BaseConfig):
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

    # Most objects possible in an image
    DETECTION_MAX_INSTANCES = 200

    DETECTION_MIN_CONFIDENCE = 0.7

def load_model_for_inference(weights_path):
    """Initialize a Mask R-CNN model with our InferenceConfig and the specified weights"""
    inference_config = InferenceConfig()
    model = modellib.MaskRCNN(mode="inference", config=inference_config, model_dir=".")
    model.load_weights(weights_path, by_name=True)
    # model.keras_model._make_predict_function()
    return model

detection_model = load_model_for_inference("mask_rcnn_0427.h5")

@app.route('/detect')
def ic_model_endpoint():
    image = cv2.imread('img.jpg')
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    results = detection_model.detect([image], verbose=1)
    print(results)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

Using the fork at https://github.com/sabderra/Mask_RCNN, but there are no issues there, so I figured I might ask here.

Hitting the /detect endpoint gives the following.

ValueError: Tensor Tensor("mrcnn_detection/Reshape_1:0", shape=(1, 200, 6), dtype=float32) is not an element of this graph.

If we uncomment model.keras_model._make_predict_function() I get the following.

tensorflow.python.framework.errors_impl.InvalidArgumentError: Tensor input_image:0, specified in either feed_devices or fetch_devices was not found in the Graph

Now, I've seen a lot of solutions involving a session, but there are no more sessions in this version. As well, a lot say to use tf.get_default_graph() but that call also does not exist. There is something I am not understanding here about TensorFlow 2 and how async functions interact.

Any guidance is appreciated!

fhdbbk commented 3 years ago

Try commenting tf.compat.v1.disable_eager_execution() in model.py. It worked for me. I was getting the same error.