You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Same mask rcnn model shows different result between tensorflow and opencv dnn module #14480
I have trained a mask rcnn model, so I tested it with tensorflow and opencv dnn module, but it showed different result between tensorflow and opencv dnn module. The tensorflow result is more accurate than opencv dnn module.
Please note that there are two factors of difference: a bug in TensorFlow's CropAndResize layer (we're working on it: tensorflow/tensorflow#21660) and a .jpg sample. JPEG decoding is not cross platform. Please add a .png sample.
Please note that there are two factors of difference: a bug in TensorFlow's CropAndResize layer (we're working on it: tensorflow/tensorflow#21660) and a .jpg sample. JPEG decoding is not cross platform. Please add a .png sample.
I have test with OpenCV's sample mask_rcnn.py with 7.png it looks to change good, but with small object like grain its location seems to be shifted to the left. This makes me confused.
I test it with 2.png again, the result is as follow.
@stq054188, Still there is no way to reproduce gold results with utils due the following error:
Traceback (most recent call last):
File "mask_rcnn_obj.py", line 9, in <module>
from utils import label_map_util
File "/path/to/utils/label_map_util.py", line 22, in <module>
from object_detection.protos import string_int_label_map_pb2
ImportError: No module named object_detection.protos
Did you solve the problem? And How did you convert h5 to .pb? I've converted with keras_to_tensorflow.py but I get an error when I use readNet in OpenCVDNN.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import warnings
import keras.backend as K
import tensorflow as tf
warnings.filterwarnings('ignore', category=FutureWarning)
# suppress warning and error message tf
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
# Root directory of the project
ROOT_DIR = os.getcwd()
# Import Mask RCNN
sys.path.append(ROOT_DIR) # To find local version of the library
from mrcnn import model as modellib
from mrcnn import utils
import coco
K.clear_session()
K.set_learning_phase(0)
##############################################################################
# Load model
##############################################################################
# Model Directory
MODEL_DIR = ("/logs")
DEFAULT_WEIGHTS = ("/content/drive/MyDrive/thirip2/mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(DEFAULT_WEIGHTS):
utils.download_trained_weights(DEFAULT_WEIGHTS)
##############################################################################
# Load configuration
##############################################################################
class InferenceConfig(coco.CocoConfig):
# Set batch size to 1 since we'll be running inference on
# one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
GPU_COUNT = 1
IMAGES_PER_GPU = 1
##############################################################################
# Save entire model function
##############################################################################
def h5_to_pb(h5_model, output_dir, model_name, out_prefix="output_"):
out_nodes = []
for i in range(len(h5_model.outputs)):
out_nodes.append(out_prefix + str(i + 1))
tf.identity(h5_model.output[i], out_prefix + str(i + 1))
sess = K.get_session()
init_graph = sess.graph.as_graph_def()
main_graph = tf._api.v1.graph_util.convert_variables_to_constants(sess, init_graph, out_nodes)
with tf.gfile.GFile(os.path.join(output_dir, model_name), "wb") as filemodel:
filemodel.write(main_graph.SerializeToString())
print("pb model: ", {os.path.join(output_dir, model_name)})
if __name__ == "__main__":
config = InferenceConfig()
config.display()
# Create model in inference mode
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
# Set path to model weights
weights_path = DEFAULT_WEIGHTS#model.find_last()
# Load weights
print("Loading weights ", weights_path)
model.load_weights(weights_path, by_name=True)
model.keras_model.summary()
# make folder for full model
model_dir = os.path.join(ROOT_DIR, "Model")
if not os.path.exists(model_dir):
os.makedirs(model_dir)
# save h5 full model
name_model = os.path.join(model_dir, "/content/drive/MyDrive/thirip2/mask_rcnn_bottle_0040.h5")
if not os.path.exists(name_model):
model.keras_model.save(name_model)
print("save model: ", name_model)
# export pb model
pb_name_model = "mask_rcnn_landing.pb"
h5_to_pb(model.keras_model, output_dir=model_dir, model_name=pb_name_model)
K.clear_session()
sys.exit()
Activity
stq054188 commentedon May 6, 2019
@dkurt Would you please help me see this problem, I'm really in a hurry. Thanks.
dkurt commentedon May 6, 2019
@stq054188, Please add a configuration file which was used for training and generation of
graph.pbtxt.dkurt commentedon May 6, 2019
BTW, this is the output I got with OpenCV's sample with modified mask threshold:
from
to
Please note that there are two factors of difference: a bug in TensorFlow's CropAndResize layer (we're working on it: tensorflow/tensorflow#21660) and a
.jpgsample. JPEG decoding is not cross platform. Please add a.pngsample.stq054188 commentedon May 6, 2019
@dkurt Thank you for your reply and I upload config file and a png sample here.
config.zip
dkurt commentedon May 6, 2019
@stq054188, Please show how desired TensorFlow output has been obtained. For me the following script gives different output:
stq054188 commentedon May 6, 2019
I have test with OpenCV's sample mask_rcnn.py with 7.png it looks to change good, but with small object like grain its location seems to be shifted to the left. This makes me confused.



I test it with 2.png again, the result is as follow.
stq054188 commentedon May 6, 2019
@dkurt Here is my python code, thanks.
mask_rcnn_obj.zip
dkurt commentedon May 6, 2019
@stq054188, Please make it reproducible. There is no
utilsmodule there.stq054188 commentedon May 6, 2019
@dkurt I am sorry, utils is here.
utils.zip
stq054188 commentedon May 6, 2019
@dkurt How can I get the same effect as the above picture using opencv?
dkurt commentedon May 6, 2019
@stq054188, Still there is no way to reproduce gold results with
utilsdue the following error:However you can try the following:
Modify mask_rcnn.py:
run:
Where 1012x800 is from the following condition for image of size 2592x2048 (see a config file):
TensorFlow code:
TensorFlow output:

stq054188 commentedon May 7, 2019
@dkurt Thank you again. It seems mask_rcnn.py is correct but my C++ code is still have some problems. I will change it and retest, thanks.
sctrueew commentedon Apr 16, 2021
@stq054188 Hi,
Did you solve the problem? And How did you convert h5 to .pb? I've converted with keras_to_tensorflow.py but I get an error when I use readNet in OpenCVDNN.
Jeffin21 commentedon May 8, 2021
@zpmmehrdad use this code