Open GLohita opened 5 years ago
How did you get a weights file separately from the h5 file?
I used this code to create a pb file: `
import tensorflow as tf
from keras import backend as K
from tensorflow.python.framework import graph_util
model_keras = model.keras_model
# All new operations will be in test mode from now on.
K.set_learning_phase(0)
# Create output layer with customized names
num_output = 7
pred_node_names = ["detections", "mrcnn_class", "mrcnn_bbox", "mrcnn_mask",
"rois", "rpn_class", "rpn_bbox"]
pred_node_names = ["output_" + name for name in pred_node_names]
pred = [tf.identity(model_keras.outputs[i], name=pred_node_names[i])
for i in range(num_output)]
sess = K.get_session()
# Get the object detection graph
od_graph_def = graph_util.convert_variables_to_constants(sess,
sess.graph.as_graph_def(),
pred_node_names)
model_dirpath = os.path.dirname("model/")
if ~os.path.exists(model_dirpath):
os.mkdir(model_dirpath)
filename = 'mrcnn_model.pb'
pb_filepath = os.path.join(model_dirpath, filename)
print('Saving frozen graph {} ...'.format(os.path.basename(pb_filepath)))
frozen_graph_path = pb_filepath
with tf.gfile.GFile(frozen_graph_path, 'wb') as f:
f.write(od_graph_def.SerializeToString())`
Thank you can you tell me how do I get the anchors file?
How did you get a weights file separately from the h5 file?
I used this code to create a pb file: `
import tensorflow as tf from keras import backend as K from tensorflow.python.framework import graph_util model_keras = model.keras_model # All new operations will be in test mode from now on. K.set_learning_phase(0) # Create output layer with customized names num_output = 7 pred_node_names = ["detections", "mrcnn_class", "mrcnn_bbox", "mrcnn_mask", "rois", "rpn_class", "rpn_bbox"] pred_node_names = ["output_" + name for name in pred_node_names] pred = [tf.identity(model_keras.outputs[i], name=pred_node_names[i]) for i in range(num_output)] sess = K.get_session() # Get the object detection graph od_graph_def = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names) model_dirpath = os.path.dirname("model/") if ~os.path.exists(model_dirpath): os.mkdir(model_dirpath) filename = 'mrcnn_model.pb' pb_filepath = os.path.join(model_dirpath, filename) print('Saving frozen graph {} ...'.format(os.path.basename(pb_filepath))) frozen_graph_path = pb_filepath with tf.gfile.GFile(frozen_graph_path, 'wb') as f: f.write(od_graph_def.SerializeToString())`
can you able to share this file? and how to run on command line??
How did you get a weights file separately from the h5 file? I used this code to create a pb file: `
import tensorflow as tf from keras import backend as K from tensorflow.python.framework import graph_util model_keras = model.keras_model # All new operations will be in test mode from now on. K.set_learning_phase(0) # Create output layer with customized names num_output = 7 pred_node_names = ["detections", "mrcnn_class", "mrcnn_bbox", "mrcnn_mask", "rois", "rpn_class", "rpn_bbox"] pred_node_names = ["output_" + name for name in pred_node_names] pred = [tf.identity(model_keras.outputs[i], name=pred_node_names[i]) for i in range(num_output)] sess = K.get_session() # Get the object detection graph od_graph_def = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names) model_dirpath = os.path.dirname("model/") if ~os.path.exists(model_dirpath): os.mkdir(model_dirpath) filename = 'mrcnn_model.pb' pb_filepath = os.path.join(model_dirpath, filename) print('Saving frozen graph {} ...'.format(os.path.basename(pb_filepath))) frozen_graph_path = pb_filepath with tf.gfile.GFile(frozen_graph_path, 'wb') as f: f.write(od_graph_def.SerializeToString())`
can you able to share this file? and how to run on command line??
Hello, I'm a beginner. I just want to convert this mask_rcnn_coco.h5 files. Can you give me a copy of your source file? thanks!
convert-to pb file
#!encoding=utf-8
'''
#-----------------
Authors:邹九
Time:2019-11-21
#-----------------
'''
"""
Copyright (c) 2019, by the Authors: Amir H. Abdi
This script is freely available under the MIT Public License.
Please see the License file in the root for details.
The following code snippet will convert the keras model files
to the freezed .pb tensorflow weight file. The resultant TensorFlow model
holds both the model architecture and its associated weights.
"""
import tensorflow as tf
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io
from pathlib import Path
from absl import app
from absl import flags
from absl import logging
from mrcnn import model as modellib
from mrcnn.config import Config
import keras
import os
from keras import backend as K
from keras.models import model_from_json, model_from_yaml
from keras.utils.vis_utils import plot_model
COCO_MODEL_PATH = r'../logs/shapes20191113T1540_mask_rcnn_shapes_0199.h5'
K.set_learning_phase(0)
FLAGS = flags.FLAGS
flags.DEFINE_string('input_model', default=r'', help='Path to the input model.')
flags.DEFINE_string('input_model_json', None, 'Path to the input model '
'architecture in json format.')
flags.DEFINE_string('input_model_yaml', None, 'Path to the input model architecture in yaml format.')
flags.DEFINE_string('output_model', default=r'./shapes20191113T1540_mask_rcnn_shapes_0199.pb', help='Path where the converted model will be stored.')
flags.DEFINE_boolean('save_graph_def', False,
'Whether to save the graphdef.pbtxt file which contains '
'the graph definition in ASCII format.')
flags.DEFINE_string('output_nodes_prefix', None,
'If set, the output nodes will be renamed to '
'`output_nodes_prefix`+i, where `i` will numerate the '
'number of of output nodes of the network.')
flags.DEFINE_boolean('quantize', False,
'If set, the resultant TensorFlow graph weights will be '
'converted from float into eight-bit equivalents. See '
'documentation here: '
'https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/graph_transforms')
flags.DEFINE_boolean('channels_first', False,
'Whether channels are the first dimension of a tensor. '
'The default is TensorFlow behaviour where channels are '
'the last dimension.')
flags.DEFINE_boolean('output_meta_ckpt', False,
'If set to True, exports the model as .meta, .index, and '
'.data files, with a checkpoint file. These can be later '
'loaded in TensorFlow to continue training.')
flags.mark_flag_as_required('input_model')
flags.mark_flag_as_required('output_model')
def load_model(input_model_path, input_json_path=None, input_yaml_path=None):
if not Path(input_model_path).exists():
raise FileNotFoundError(
'Model file `{}` does not exist.'.format(input_model_path))
try:
model = keras.models.load_model(input_model_path)
return model
except FileNotFoundError as err:
logging.error('Input mode file (%s) does not exist.', FLAGS.input_model)
raise err
except ValueError as wrong_file_err:
if input_json_path:
if not Path(input_json_path).exists():
raise FileNotFoundError(
'Model description json file `{}` does not exist.'.format(
input_json_path))
try:
model = model_from_json(open(str(input_json_path)).read())
model.load_weights(input_model_path)
return model
except Exception as err:
logging.error("Couldn't load model from json.")
raise err
elif input_yaml_path:
if not Path(input_yaml_path).exists():
raise FileNotFoundError(
'Model description yaml file `{}` does not exist.'.format(
input_yaml_path))
try:
model = model_from_yaml(open(str(input_yaml_path)).read())
model.load_weights(input_model_path)
return model
except Exception as err:
logging.error("Couldn't load model from yaml.")
raise err
else:
logging.error(
'Input file specified only holds the weights, and not '
'the model definition. Save the model using '
'model.save(filename.h5) which will contain the network '
'architecture as well as its weights. '
'If the model is saved using the '
'model.save_weights(filename) function, either '
'input_model_json or input_model_yaml flags should be set to '
'to import the network architecture prior to loading the '
'weights. \n'
'Check the keras documentation for more details '
'(https://keras.io/getting-started/faq/)')
raise wrong_file_err
class ShapesConfig(Config):
"""Configuration for training on the toy shapes dataset.
Derives from the base Config class and overrides values specific
to the toy shapes dataset.
"""
# Give the configuration a recognizable name
NAME = "shapes"
# Number of classes (including background)
NUM_CLASSES = 1 + 14 # background + 15 object
# Choose the number of GPU devices
# os.environ['CUDA_VISIBLE_DEVICES'] = '0'
# Use small images for faster training. Set the limits of the small side
# the large side, and that determines the image shape.
IMAGE_RESIZE_MODE = "square"
IMAGE_MAX_DIM = 896
RPN_ANCHOR_SCALES = (8 * 6, 16 * 6, 32 * 6, 64 * 6, 128 * 6) # anchor side in pixels
# RPN_ANCHOR_SCALES = (8*5, 16*5, 32*5, 64*5, 128*5) # anchor side in pixels
# Reduce training ROIs per image because the images are small and have
# few objects. Aim to allow ROI sampling to pick 33% positive ROIs.
TRAIN_ROIS_PER_IMAGE = 100
# Use a small epoch since the data is simple
# STEPS_PER_EPOCH = 1000
STEPS_PER_EPOCH = 1000
# use small validation steps since the epoch is small
VALIDATION_STEPS = 25
def main(args):
# If output_model path is relative and in cwd, make it absolute from root
output_model = FLAGS.output_model
if str(Path(output_model).parent) == '.':
output_model = str((Path.cwd() / output_model))
output_fld = Path(output_model).parent
output_model_name = Path(output_model).name
output_model_stem = Path(output_model).stem
output_model_pbtxt_name = output_model_stem + '.pbtxt'
# Create output directory if it does not exist
Path(output_model).parent.mkdir(parents=True, exist_ok=True)
if FLAGS.channels_first:
K.set_image_data_format('channels_first')
else:
K.set_image_data_format('channels_last')
# model = load_model(FLAGS.input_model, FLAGS.input_model_json, FLAGS.input_model_yaml)
##--------------------------------------------------------------------------------------#
config = ShapesConfig()
config.display()
MODEL_DIR = r'E:\Desktop\Projects\Mask_RCNN-master\logs'
model = modellib.MaskRCNN(mode="inference", config=config,\
model_dir=MODEL_DIR)
model.load_weights(COCO_MODEL_PATH, by_name=True)#exclude=["mrcnn_class_logits", "mrcnn_bbox_fc",\
# "mrcnn_bbox", "mrcnn_mask"])
# print(model.summary())
# plot_model(model, to_file='model1.png', show_shapes=True)
# model_json = model.to_json()
# with open(r'./modle.json', 'w') as file:
# file.write(model_json)
print('loaded model and saved json file')
##--------------------------------------------------------------------------------------#
# TODO(amirabdi): Support networks with multiple inputs
# orig_output_node_names = [node.op.name for node in model.outputs]
orig_output_node_names = ['mrcnn_detection/Reshape_1', 'mrcnn_class/Softmax', 'mrcnn_bbox/Reshape',\
'mrcnn_mask/Sigmoid', 'ROI/packed_2', 'rpn_class/concat', 'rpn_bbox/concat']
if FLAGS.output_nodes_prefix:
num_output = len(orig_output_node_names)
pred = [None] * num_output
converted_output_node_names = [None] * num_output
# Create dummy tf nodes to rename output
for i in range(num_output):
converted_output_node_names[i] = '{}{}'.format(
FLAGS.output_nodes_prefix, i)
pred[i] = tf.identity(model.outputs[i],
name=converted_output_node_names[i])
else:
converted_output_node_names = orig_output_node_names
logging.info('Converted output node names are: %s',
str(converted_output_node_names))
sess = K.get_session()
if FLAGS.output_meta_ckpt:
saver = tf.train.Saver()
saver.save(sess, str(output_fld / output_model_stem))
if FLAGS.save_graph_def:
tf.train.write_graph(sess.graph.as_graph_def(), str(output_fld),
output_model_pbtxt_name, as_text=True)
logging.info('Saved the graph definition in ascii format at %s',
str(Path(output_fld) / output_model_pbtxt_name))
if FLAGS.quantize:
from tensorflow.tools.graph_transforms import TransformGraph
transforms = ["quantize_weights", "quantize_nodes"]
transformed_graph_def = TransformGraph(sess.graph.as_graph_def(), [],
converted_output_node_names,
transforms)
constant_graph = graph_util.convert_variables_to_constants(
sess,
transformed_graph_def,
converted_output_node_names)
else:
constant_graph = graph_util.convert_variables_to_constants(
sess,
sess.graph.as_graph_def(),
converted_output_node_names)
graph_io.write_graph(constant_graph, str(output_fld), output_model_name,
as_text=False)
logging.info('Saved the freezed graph at %s',
str(Path(output_fld) / output_model_name))
if __name__ == "__main__":
app.run(main)
load pb model
def load_detection_model(model):
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(model, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
input_image = tf.get_default_graph().get_tensor_by_name('input_image:0')
input_image_meta = tf.get_default_graph().get_tensor_by_name('input_image_meta:0')
input_anchors = tf.get_default_graph().get_tensor_by_name('input_anchors:0')
detections = tf.get_default_graph().get_tensor_by_name('mrcnn_detection/Reshape_1:0')
mrcnn_mask = tf.get_default_graph().get_tensor_by_name('mrcnn_mask/Sigmoid:0')
sessd=tf.Session(config=config,graph=detection_graph)
print('Loaded detection model from file "%s"' % model)
return sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask
sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask = load_detection_model(model_path)
results = model.detect_pb([image], sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask,verbose=1)
use model, add to mrcnn/model.py
def detect_pb(self, images, sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask, verbose=1):
"""Runs the detection pipeline.
images: List of images, potentially of different sizes.
Returns a list of dicts, one dict per image. The dict contains:
rois: [N, (y1, x1, y2, x2)] detection bounding boxes
class_ids: [N] int class IDs
scores: [N] float probability scores for the class IDs
masks: [H, W, N] instance binary masks
"""
assert self.mode == "inference", "Create model in inference mode."
assert len(
images) == self.config.BATCH_SIZE, "len(images) must be equal to BATCH_SIZE"
# if verbose:
# log("Processing {} images".format(len(images)))
# for image in images:
# log("image", image)
# Mold inputs to format expected by the neural network
molded_images, image_metas, windows = self.mold_inputs(images)
# Validate image sizes
# All images in a batch MUST be of the same size
image_shape = molded_images[0].shape
# print(image_shape, molded_images.shape)
for g in molded_images[1:]:
assert g.shape == image_shape,\
"After resizing, all images must have the same size. Check IMAGE_RESIZE_MODE and image sizes."
# Anchors
anchors = self.get_anchors(image_shape)
# Duplicate across the batch dimension because Keras requires it
# TODO: can this be optimized to avoid duplicating the anchors?
anchors = np.broadcast_to(anchors, (self.config.BATCH_SIZE,) + anchors.shape)
# if verbose:
# log("molded_images", molded_images)
# log("image_metas", image_metas)
# log("anchors", anchors)
# Run object detection
# detections, _, _, mrcnn_mask, _, _, _ =\
# self.keras_model.predict([molded_images, image_metas, anchors], verbose=0)
detectionsed, mrcnn_masked = sessd.run([detections, mrcnn_mask], feed_dict = {input_image: molded_images, \
input_image_meta: image_metas, \
input_anchors: anchors})
mrcnn_masked = np.expand_dims(mrcnn_masked, 0)
detections = np.array(detectionsed)
mrcnn_mask = np.array(mrcnn_masked)
# Process detections
results = []
for i, image in enumerate(images):
xi = detections[i]
yi = mrcnn_mask[i]
moldedi = molded_images[i]
windowsi = windows[i]
final_rois, final_class_ids, final_scores, final_masks =\
self.unmold_detections(detections[i], mrcnn_mask[i],
image.shape, molded_images[i].shape,
windows[i])
results.append({
"rois": final_rois,
"class_ids": final_class_ids,
"scores": final_scores,
"masks": final_masks,
})
return results
convert-to pb file
#!encoding=utf-8 ''' #----------------- Authors:邹九 Time:2019-11-21 #----------------- ''' """ Copyright (c) 2019, by the Authors: Amir H. Abdi This script is freely available under the MIT Public License. Please see the License file in the root for details. The following code snippet will convert the keras model files to the freezed .pb tensorflow weight file. The resultant TensorFlow model holds both the model architecture and its associated weights. """ import tensorflow as tf from tensorflow.python.framework import graph_util from tensorflow.python.framework import graph_io from pathlib import Path from absl import app from absl import flags from absl import logging from mrcnn import model as modellib from mrcnn.config import Config import keras import os from keras import backend as K from keras.models import model_from_json, model_from_yaml from keras.utils.vis_utils import plot_model COCO_MODEL_PATH = r'../logs/shapes20191113T1540_mask_rcnn_shapes_0199.h5' K.set_learning_phase(0) FLAGS = flags.FLAGS flags.DEFINE_string('input_model', default=r'', help='Path to the input model.') flags.DEFINE_string('input_model_json', None, 'Path to the input model ' 'architecture in json format.') flags.DEFINE_string('input_model_yaml', None, 'Path to the input model architecture in yaml format.') flags.DEFINE_string('output_model', default=r'./shapes20191113T1540_mask_rcnn_shapes_0199.pb', help='Path where the converted model will be stored.') flags.DEFINE_boolean('save_graph_def', False, 'Whether to save the graphdef.pbtxt file which contains ' 'the graph definition in ASCII format.') flags.DEFINE_string('output_nodes_prefix', None, 'If set, the output nodes will be renamed to ' '`output_nodes_prefix`+i, where `i` will numerate the ' 'number of of output nodes of the network.') flags.DEFINE_boolean('quantize', False, 'If set, the resultant TensorFlow graph weights will be ' 'converted from float into eight-bit equivalents. See ' 'documentation here: ' 'https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/graph_transforms') flags.DEFINE_boolean('channels_first', False, 'Whether channels are the first dimension of a tensor. ' 'The default is TensorFlow behaviour where channels are ' 'the last dimension.') flags.DEFINE_boolean('output_meta_ckpt', False, 'If set to True, exports the model as .meta, .index, and ' '.data files, with a checkpoint file. These can be later ' 'loaded in TensorFlow to continue training.') flags.mark_flag_as_required('input_model') flags.mark_flag_as_required('output_model') def load_model(input_model_path, input_json_path=None, input_yaml_path=None): if not Path(input_model_path).exists(): raise FileNotFoundError( 'Model file `{}` does not exist.'.format(input_model_path)) try: model = keras.models.load_model(input_model_path) return model except FileNotFoundError as err: logging.error('Input mode file (%s) does not exist.', FLAGS.input_model) raise err except ValueError as wrong_file_err: if input_json_path: if not Path(input_json_path).exists(): raise FileNotFoundError( 'Model description json file `{}` does not exist.'.format( input_json_path)) try: model = model_from_json(open(str(input_json_path)).read()) model.load_weights(input_model_path) return model except Exception as err: logging.error("Couldn't load model from json.") raise err elif input_yaml_path: if not Path(input_yaml_path).exists(): raise FileNotFoundError( 'Model description yaml file `{}` does not exist.'.format( input_yaml_path)) try: model = model_from_yaml(open(str(input_yaml_path)).read()) model.load_weights(input_model_path) return model except Exception as err: logging.error("Couldn't load model from yaml.") raise err else: logging.error( 'Input file specified only holds the weights, and not ' 'the model definition. Save the model using ' 'model.save(filename.h5) which will contain the network ' 'architecture as well as its weights. ' 'If the model is saved using the ' 'model.save_weights(filename) function, either ' 'input_model_json or input_model_yaml flags should be set to ' 'to import the network architecture prior to loading the ' 'weights. \n' 'Check the keras documentation for more details ' '(https://keras.io/getting-started/faq/)') raise wrong_file_err class ShapesConfig(Config): """Configuration for training on the toy shapes dataset. Derives from the base Config class and overrides values specific to the toy shapes dataset. """ # Give the configuration a recognizable name NAME = "shapes" # Number of classes (including background) NUM_CLASSES = 1 + 14 # background + 15 object # Choose the number of GPU devices # os.environ['CUDA_VISIBLE_DEVICES'] = '0' # Use small images for faster training. Set the limits of the small side # the large side, and that determines the image shape. IMAGE_RESIZE_MODE = "square" IMAGE_MAX_DIM = 896 RPN_ANCHOR_SCALES = (8 * 6, 16 * 6, 32 * 6, 64 * 6, 128 * 6) # anchor side in pixels # RPN_ANCHOR_SCALES = (8*5, 16*5, 32*5, 64*5, 128*5) # anchor side in pixels # Reduce training ROIs per image because the images are small and have # few objects. Aim to allow ROI sampling to pick 33% positive ROIs. TRAIN_ROIS_PER_IMAGE = 100 # Use a small epoch since the data is simple # STEPS_PER_EPOCH = 1000 STEPS_PER_EPOCH = 1000 # use small validation steps since the epoch is small VALIDATION_STEPS = 25 def main(args): # If output_model path is relative and in cwd, make it absolute from root output_model = FLAGS.output_model if str(Path(output_model).parent) == '.': output_model = str((Path.cwd() / output_model)) output_fld = Path(output_model).parent output_model_name = Path(output_model).name output_model_stem = Path(output_model).stem output_model_pbtxt_name = output_model_stem + '.pbtxt' # Create output directory if it does not exist Path(output_model).parent.mkdir(parents=True, exist_ok=True) if FLAGS.channels_first: K.set_image_data_format('channels_first') else: K.set_image_data_format('channels_last') # model = load_model(FLAGS.input_model, FLAGS.input_model_json, FLAGS.input_model_yaml) ##--------------------------------------------------------------------------------------# config = ShapesConfig() config.display() MODEL_DIR = r'E:\Desktop\Projects\Mask_RCNN-master\logs' model = modellib.MaskRCNN(mode="inference", config=config,\ model_dir=MODEL_DIR) model.load_weights(COCO_MODEL_PATH, by_name=True)#exclude=["mrcnn_class_logits", "mrcnn_bbox_fc",\ # "mrcnn_bbox", "mrcnn_mask"]) # print(model.summary()) # plot_model(model, to_file='model1.png', show_shapes=True) # model_json = model.to_json() # with open(r'./modle.json', 'w') as file: # file.write(model_json) print('loaded model and saved json file') ##--------------------------------------------------------------------------------------# # TODO(amirabdi): Support networks with multiple inputs # orig_output_node_names = [node.op.name for node in model.outputs] orig_output_node_names = ['mrcnn_detection/Reshape_1', 'mrcnn_class/Softmax', 'mrcnn_bbox/Reshape',\ 'mrcnn_mask/Sigmoid', 'ROI/packed_2', 'rpn_class/concat', 'rpn_bbox/concat'] if FLAGS.output_nodes_prefix: num_output = len(orig_output_node_names) pred = [None] * num_output converted_output_node_names = [None] * num_output # Create dummy tf nodes to rename output for i in range(num_output): converted_output_node_names[i] = '{}{}'.format( FLAGS.output_nodes_prefix, i) pred[i] = tf.identity(model.outputs[i], name=converted_output_node_names[i]) else: converted_output_node_names = orig_output_node_names logging.info('Converted output node names are: %s', str(converted_output_node_names)) sess = K.get_session() if FLAGS.output_meta_ckpt: saver = tf.train.Saver() saver.save(sess, str(output_fld / output_model_stem)) if FLAGS.save_graph_def: tf.train.write_graph(sess.graph.as_graph_def(), str(output_fld), output_model_pbtxt_name, as_text=True) logging.info('Saved the graph definition in ascii format at %s', str(Path(output_fld) / output_model_pbtxt_name)) if FLAGS.quantize: from tensorflow.tools.graph_transforms import TransformGraph transforms = ["quantize_weights", "quantize_nodes"] transformed_graph_def = TransformGraph(sess.graph.as_graph_def(), [], converted_output_node_names, transforms) constant_graph = graph_util.convert_variables_to_constants( sess, transformed_graph_def, converted_output_node_names) else: constant_graph = graph_util.convert_variables_to_constants( sess, sess.graph.as_graph_def(), converted_output_node_names) graph_io.write_graph(constant_graph, str(output_fld), output_model_name, as_text=False) logging.info('Saved the freezed graph at %s', str(Path(output_fld) / output_model_name)) if __name__ == "__main__": app.run(main)
load pb model
def load_detection_model(model): config = tf.ConfigProto() config.gpu_options.allow_growth = True detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(model, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') input_image = tf.get_default_graph().get_tensor_by_name('input_image:0') input_image_meta = tf.get_default_graph().get_tensor_by_name('input_image_meta:0') input_anchors = tf.get_default_graph().get_tensor_by_name('input_anchors:0') detections = tf.get_default_graph().get_tensor_by_name('mrcnn_detection/Reshape_1:0') mrcnn_mask = tf.get_default_graph().get_tensor_by_name('mrcnn_mask/Sigmoid:0') sessd=tf.Session(config=config,graph=detection_graph) print('Loaded detection model from file "%s"' % model) return sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask = load_detection_model(model_path) results = model.detect_pb([image], sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask,verbose=1)
use model, add to mrcnn/model.py
def detect_pb(self, images, sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask, verbose=1): """Runs the detection pipeline. images: List of images, potentially of different sizes. Returns a list of dicts, one dict per image. The dict contains: rois: [N, (y1, x1, y2, x2)] detection bounding boxes class_ids: [N] int class IDs scores: [N] float probability scores for the class IDs masks: [H, W, N] instance binary masks """ assert self.mode == "inference", "Create model in inference mode." assert len( images) == self.config.BATCH_SIZE, "len(images) must be equal to BATCH_SIZE" # if verbose: # log("Processing {} images".format(len(images))) # for image in images: # log("image", image) # Mold inputs to format expected by the neural network molded_images, image_metas, windows = self.mold_inputs(images) # Validate image sizes # All images in a batch MUST be of the same size image_shape = molded_images[0].shape # print(image_shape, molded_images.shape) for g in molded_images[1:]: assert g.shape == image_shape,\ "After resizing, all images must have the same size. Check IMAGE_RESIZE_MODE and image sizes." # Anchors anchors = self.get_anchors(image_shape) # Duplicate across the batch dimension because Keras requires it # TODO: can this be optimized to avoid duplicating the anchors? anchors = np.broadcast_to(anchors, (self.config.BATCH_SIZE,) + anchors.shape) # if verbose: # log("molded_images", molded_images) # log("image_metas", image_metas) # log("anchors", anchors) # Run object detection # detections, _, _, mrcnn_mask, _, _, _ =\ # self.keras_model.predict([molded_images, image_metas, anchors], verbose=0) detectionsed, mrcnn_masked = sessd.run([detections, mrcnn_mask], feed_dict = {input_image: molded_images, \ input_image_meta: image_metas, \ input_anchors: anchors}) mrcnn_masked = np.expand_dims(mrcnn_masked, 0) detections = np.array(detectionsed) mrcnn_mask = np.array(mrcnn_masked) # Process detections results = [] for i, image in enumerate(images): xi = detections[i] yi = mrcnn_mask[i] moldedi = molded_images[i] windowsi = windows[i] final_rois, final_class_ids, final_scores, final_masks =\ self.unmold_detections(detections[i], mrcnn_mask[i], image.shape, molded_images[i].shape, windows[i]) results.append({ "rois": final_rois, "class_ids": final_class_ids, "scores": final_scores, "masks": final_masks, }) return results
Hello, I just started to contact the mask RCNN network and used materport's mask RCNN training to get the H5 weight file, then how to convert it into a Pb file that opencv DNN can call. (I used your conversion program to get a Pb file, but I don't know whether the Pb file contains the network structure, And I'm not sure how to use the last two programs you provided. If it is convenient for you, can you add a contact information, thank you!( My email: xhu_ hqu@163.com; Wechat: x0219zi)
@Hu-Xiang-Male,for the origin repository mask_rcnn ,firstly, you can use this script to convert mask_rcnn_coco.h5 to mask_rcnn_coco.pb;
#!encoding=utf-8
'''
#-----------------
Authors:邹九
Time:2021-6-26
#-----------------
'''
"""
Copyright (c) 2019, by the Authors: Amir H. Abdi
This script is freely available under the MIT Public License.
Please see the License file in the root for details.
The following code snippet will convert the keras model files
to the freezed .pb tensorflow weight file. The resultant TensorFlow model
holds both the model architecture and its associated weights.
"""
import tensorflow as tf
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io
from pathlib import Path
from absl import app
from absl import flags
from absl import logging
from mrcnn import model as modellib
from mrcnn.config import Config
import keras
import os
from keras import backend as K
from keras.models import model_from_json, model_from_yaml
from keras.utils.vis_utils import plot_model
from samples.coco import coco
from mrcnn import utils
COCO_MODEL_PATH = './mask_rcnn_coco.h5'
if not os.path.exists(COCO_MODEL_PATH):
utils.download_trained_weights(COCO_MODEL_PATH)
K.set_learning_phase(0)
FLAGS = flags.FLAGS
flags.DEFINE_string('input_model', default=r'', help='Path to the input model.')
flags.DEFINE_string('input_model_json', None, 'Path to the input model '
'architecture in json format.')
flags.DEFINE_string('input_model_yaml', None, 'Path to the input model architecture in yaml format.')
flags.DEFINE_string('output_model', default=r'./mask_rcnn_coco.pb', help='Path where the converted model will be stored.')
flags.DEFINE_boolean('save_graph_def', False,
'Whether to save the graphdef.pbtxt file which contains '
'the graph definition in ASCII format.')
flags.DEFINE_string('output_nodes_prefix', None,
'If set, the output nodes will be renamed to '
'`output_nodes_prefix`+i, where `i` will numerate the '
'number of of output nodes of the network.')
flags.DEFINE_boolean('quantize', False,
'If set, the resultant TensorFlow graph weights will be '
'converted from float into eight-bit equivalents. See '
'documentation here: '
'https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/graph_transforms')
flags.DEFINE_boolean('channels_first', False,
'Whether channels are the first dimension of a tensor. '
'The default is TensorFlow behaviour where channels are '
'the last dimension.')
flags.DEFINE_boolean('output_meta_ckpt', False,
'If set to True, exports the model as .meta, .index, and '
'.data files, with a checkpoint file. These can be later '
'loaded in TensorFlow to continue training.')
flags.mark_flag_as_required('input_model')
flags.mark_flag_as_required('output_model')
def load_model(input_model_path, input_json_path=None, input_yaml_path=None):
if not Path(input_model_path).exists():
raise FileNotFoundError(
'Model file `{}` does not exist.'.format(input_model_path))
try:
model = keras.models.load_model(input_model_path)
return model
except FileNotFoundError as err:
logging.error('Input mode file (%s) does not exist.', FLAGS.input_model)
raise err
except ValueError as wrong_file_err:
if input_json_path:
if not Path(input_json_path).exists():
raise FileNotFoundError(
'Model description json file `{}` does not exist.'.format(
input_json_path))
try:
model = model_from_json(open(str(input_json_path)).read())
model.load_weights(input_model_path)
return model
except Exception as err:
logging.error("Couldn't load model from json.")
raise err
elif input_yaml_path:
if not Path(input_yaml_path).exists():
raise FileNotFoundError(
'Model description yaml file `{}` does not exist.'.format(
input_yaml_path))
try:
model = model_from_yaml(open(str(input_yaml_path)).read())
model.load_weights(input_model_path)
return model
except Exception as err:
logging.error("Couldn't load model from yaml.")
raise err
else:
logging.error(
'Input file specified only holds the weights, and not '
'the model definition. Save the model using '
'model.save(filename.h5) which will contain the network '
'architecture as well as its weights. '
'If the model is saved using the '
'model.save_weights(filename) function, either '
'input_model_json or input_model_yaml flags should be set to '
'to import the network architecture prior to loading the '
'weights. \n'
'Check the keras documentation for more details '
'(https://keras.io/getting-started/faq/)')
raise wrong_file_err
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
def main(args):
# If output_model path is relative and in cwd, make it absolute from root
output_model = FLAGS.output_model
if str(Path(output_model).parent) == '.':
output_model = str((Path.cwd() / output_model))
output_fld = Path(output_model).parent
output_model_name = Path(output_model).name
output_model_stem = Path(output_model).stem
output_model_pbtxt_name = output_model_stem + '.pbtxt'
# Create output directory if it does not exist
Path(output_model).parent.mkdir(parents=True, exist_ok=True)
if FLAGS.channels_first:
K.set_image_data_format('channels_first')
else:
K.set_image_data_format('channels_last')
# model = load_model(FLAGS.input_model, FLAGS.input_model_json, FLAGS.input_model_yaml)
##--------------------------------------------------------------------------------------#
config = InferenceConfig()
config.display()
MODEL_DIR = r'E:\Desktop\Projects\Mask_RCNN-master\logs'
model = modellib.MaskRCNN(mode="inference", config=config,\
model_dir=MODEL_DIR)
model.load_weights(COCO_MODEL_PATH, by_name=True)#exclude=["mrcnn_class_logits", "mrcnn_bbox_fc",\
# "mrcnn_bbox", "mrcnn_mask"])
# print(model.summary())
# plot_model(model, to_file='model1.png', show_shapes=True)
# model_json = model.to_json()
# with open(r'./modle.json', 'w') as file:
# file.write(model_json)
print('loaded model and saved json file')
##--------------------------------------------------------------------------------------#
# TODO(amirabdi): Support networks with multiple inputs
# orig_output_node_names = [node.op.name for node in model.outputs]
orig_output_node_names = ['mrcnn_detection/Reshape_1', 'mrcnn_class/Softmax', 'mrcnn_bbox/Reshape',\
'mrcnn_mask/Sigmoid', 'ROI/packed_2', 'rpn_class/concat', 'rpn_bbox/concat']
if FLAGS.output_nodes_prefix:
num_output = len(orig_output_node_names)
pred = [None] * num_output
converted_output_node_names = [None] * num_output
# Create dummy tf nodes to rename output
for i in range(num_output):
converted_output_node_names[i] = '{}{}'.format(
FLAGS.output_nodes_prefix, i)
pred[i] = tf.identity(model.outputs[i],
name=converted_output_node_names[i])
else:
converted_output_node_names = orig_output_node_names
logging.info('Converted output node names are: %s',
str(converted_output_node_names))
sess = K.get_session()
if FLAGS.output_meta_ckpt:
saver = tf.train.Saver()
saver.save(sess, str(output_fld / output_model_stem))
if FLAGS.save_graph_def:
tf.train.write_graph(sess.graph.as_graph_def(), str(output_fld),
output_model_pbtxt_name, as_text=True)
logging.info('Saved the graph definition in ascii format at %s',
str(Path(output_fld) / output_model_pbtxt_name))
if FLAGS.quantize:
from tensorflow.tools.graph_transforms import TransformGraph
transforms = ["quantize_weights", "quantize_nodes"]
transformed_graph_def = TransformGraph(sess.graph.as_graph_def(), [],
converted_output_node_names,
transforms)
constant_graph = graph_util.convert_variables_to_constants(
sess,
transformed_graph_def,
converted_output_node_names)
else:
constant_graph = graph_util.convert_variables_to_constants(
sess,
sess.graph.as_graph_def(),
converted_output_node_names)
graph_io.write_graph(constant_graph, str(output_fld), output_model_name,
as_text=False)
logging.info('Saved the freezed graph at %s',
str(Path(output_fld) / output_model_name))
if __name__ == "__main__":
app.run(main)
Secondly, you should add the function "detect_pb" mentioned above to line 2539 in the mrcnn/model.py file after the "detect" function;
Thirdly,you should create a script "detection.py" at the root directory, then use this script "detection.py" to detect and segment object.
'''
#-----------------
Authors:邹九
Time:2021-6-26
#-----------------
'''
import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
# Root directory of the project
ROOT_DIR = os.path.abspath("")
# Import Mask RCNN
sys.path.append(ROOT_DIR) # To find local version of the library
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "samples/coco/")) # To find local version
from samples.coco import coco
# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")
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
config = InferenceConfig()
config.display()
# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
# COCO Class names
# Index of the class in the list is its ID. For example, to get ID of
# the teddy bear class, use: class_names.index('teddy bear')
class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
'kite', 'baseball bat', 'baseball glove', 'skateboard',
'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
'teddy bear', 'hair drier', 'toothbrush']
# Load a random image from the images folder
file_names = next(os.walk(IMAGE_DIR))[2]
image = skimage.io.imread(os.path.join(IMAGE_DIR, random.choice(file_names)))
# Run detection
import tensorflow as tf
def load_detection_model(model):
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(model, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
input_image = tf.get_default_graph().get_tensor_by_name('input_image:0')
input_image_meta = tf.get_default_graph().get_tensor_by_name('input_image_meta:0')
input_anchors = tf.get_default_graph().get_tensor_by_name('input_anchors:0')
detections = tf.get_default_graph().get_tensor_by_name('mrcnn_detection/Reshape_1:0')
mrcnn_mask = tf.get_default_graph().get_tensor_by_name('mrcnn_mask/Sigmoid:0')
sessd=tf.Session(config=config,graph=detection_graph)
print('Loaded detection model from file "%s"' % model)
return sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask
sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask = load_detection_model('mask_rcnn_coco.pb')
results = model.detect_pb([image], sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask,verbose=1)
# Visualize results
res = results[0]
visualize.display_instances(image, res['rois'], res['masks'], res['class_ids'],
class_names, res['scores'])
@ZouJiu1 Hello, I changed H5 to Pb according to your prompt, and added "detect pb" in model.py ", but the following error occurred while running" detection. py ". What is the reason? By the way, when saving H5 files,the program seems to use save weights_ Only = true. Does this mean that there is only weight and no network structure in the H5 file? How did you solve it? thank you
@ZouJiu1 the following error occurred while running" detection. py ". Traceback (most recent call last): File "C:\ProgramData\Anaconda3\envs\python36-gpu\lib\site-packages\tensorflow\python\client\session.py", line 1334, in _do_call return fn(*args) File "C:\ProgramData\Anaconda3\envs\python36-gpu\lib\site-packages\tensorflow\python\client\session.py", line 1319, in _run_fn options, feed_dict, fetch_list, target_list, run_metadata) File "C:\ProgramData\Anaconda3\envs\python36-gpu\lib\site-packages\tensorflow\python\client\session.py", line 1407, in _call_tf_sessionrun run_metadata) tensorflow.python.framework.errors_impl.InvalidArgumentError: slice index 1 of dimension 0 out of bounds. [[{{node ROI/strided_slice_12}} = StridedSlice[Index=DT_INT32, T=DT_FLOAT, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_input_anchors_0_0/_23, mrcnn_detection/strided_slice_8/stack_1, mrcnn_detection/strided_slice_31/stack_1, mrcnn_detection/strided_slice_8/stack_1)]] [[{{node ROI/strided_slice_41/_35}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_1712_ROI/strided_slice_41", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:/huxiang/mask rcnn/rgb/detection.py", line 121, in
Caused by op 'ROI/strided_slice_12', defined at:
File "d:/huxiang/mask rcnn/rgb/detection.py", line 120, in
InvalidArgumentError (see above for traceback): slice index 1 of dimension 0 out of bounds. [[node ROI/strided_slice_12 (defined at d:/huxiang/mask rcnn/rgb/detection.py:110) = StridedSlice[Index=DT_INT32, T=DT_FLOAT, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_input_anchors_0_0/_23, mrcnn_detection/strided_slice_8/stack_1, mrcnn_detection/strided_slice_31/stack_1, mrcnn_detection/strided_slice_8/stack_1)]] [[{{node ROI/strided_slice_41/_35}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_1712_ROI/strided_slice_41", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
@Hu-Xiang-Male You should check the InferenceConfig of detection.py and the environment of running, the environment I used is tensorflow==1.3.0、keras==2.0.8, the InferenceConfig mentioned above is for coco dataset,you should change it to the same like training
class InferenceConfig(coco.CocoConfig)
...
config = InferenceConfig()
config.display()
@ZouJiu1 I modified the configuration and directly copied the parameters set during training. But I use tensorflow version 1.14.0, does it affect the results?
class ShapesConfig(Config): """Configuration for training on the toy shapes dataset. Derives from the base Config class and overrides values specific to the toy shapes dataset. """
NAME = "cdw"
# Number of classes (including background)
NUM_CLASSES = 1 + 1 # background + 15 object
# Choose the number of GPU devices
# os.environ['CUDA_VISIBLE_DEVICES'] = '0'
# Use small images for faster training. Set the limits of the small side
# the large side, and that determines the image shape.
IMAGE_RESIZE_MODE = "square"
IMAGE_MAX_DIM = 512
#RPN_ANCHOR_SCALES = (8 * 6, 16 * 6, 32 * 6, 64 * 6, 128 * 6) # anchor side in pixels
# RPN_ANCHOR_SCALES = (8*5, 16*5, 32*5, 64*5, 128*5) # anchor side in pixels
RPN_ANCHOR_SCALES = (16, 32, 64, 128, 256)
# Reduce training ROIs per image because the images are small and have
# few objects. Aim to allow ROI sampling to pick 33% positive ROIs.
TRAIN_ROIS_PER_IMAGE = 100
# Use a small epoch since the data is simple
# STEPS_PER_EPOCH = 1000
STEPS_PER_EPOCH = 500
# use small validation steps since the epoch is small
# VALIDATION_STEPS = 25
VALIDATION_STEPS = 50
I want a mask rcnn .pb file and anchors file to be used in android, can anyone tell me how do I generate them as I only have a .h5 file