-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5d0b468
Showing
5 changed files
with
3,953 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import numpy as np | ||
|
||
|
||
class mask_config(): | ||
def __init__(self, NUMBER_OF_CLASSES): | ||
self.NAME = "tags" | ||
self.IMAGES_PER_GPU = 2 | ||
self.NUM_CLASSES = 1 + NUMBER_OF_CLASSES # Background + tags | ||
self.STEPS_PER_EPOCH = 100 | ||
self.DETECTION_MIN_CONFIDENCE = 0.9 | ||
self.GPU_COUNT = 1 | ||
self.IMAGES_PER_GPU = 1 | ||
self.NAME = None # Override in sub-classes | ||
self.GPU_COUNT = 1 | ||
self.IMAGES_PER_GPU = 1 | ||
self.STEPS_PER_EPOCH = 1000 | ||
self.VALIDATION_STEPS = 50 | ||
self.BACKBONE = "resnet101" | ||
self.COMPUTE_BACKBONE_SHAPE = None | ||
self.BACKBONE_STRIDES = [4, 8, 16, 32, 64] | ||
self.FPN_CLASSIF_FC_LAYERS_SIZE = 1024 | ||
self.TOP_DOWN_PYRAMID_SIZE = 256 | ||
self.RPN_ANCHOR_SCALES = (32, 64, 128, 256, 512) | ||
self.RPN_ANCHOR_RATIOS = [0.5, 1, 2] | ||
self.RPN_ANCHOR_STRIDE = 1 | ||
self.RPN_NMS_THRESHOLD = 0.7 | ||
self.RPN_TRAIN_ANCHORS_PER_IMAGE = 256 | ||
self.POST_NMS_ROIS_TRAINING = 2000 | ||
self.POST_NMS_ROIS_INFERENCE = 1000 | ||
self.USE_MINI_MASK = True | ||
self.MINI_MASK_SHAPE = (56, 56) # (height, width) of the mini-mask | ||
self.IMAGE_RESIZE_MODE = "square" | ||
self.IMAGE_MIN_DIM = 800 | ||
self.IMAGE_MAX_DIM = 1024 | ||
self.IMAGE_MIN_SCALE = 0 | ||
self.MEAN_PIXEL = np.array([123.7, 116.8, 103.9]) | ||
self.TRAIN_ROIS_PER_IMAGE = 200 | ||
self.ROI_POSITIVE_RATIO = 0.33 | ||
self.POOL_SIZE = 7 | ||
self.MASK_POOL_SIZE = 14 | ||
self.MASK_SHAPE = [28, 28] | ||
self.MAX_GT_INSTANCES = 100 | ||
self.RPN_BBOX_STD_DEV = np.array([0.1, 0.1, 0.2, 0.2]) | ||
self.BBOX_STD_DEV = np.array([0.1, 0.1, 0.2, 0.2]) | ||
self.DETECTION_MAX_INSTANCES = 100 | ||
self.DETECTION_MIN_CONFIDENCE = 0.7 | ||
self.DETECTION_NMS_THRESHOLD = 0.3 | ||
self.LEARNING_RATE = 0.001 | ||
self.LEARNING_MOMENTUM = 0.9 | ||
self.WEIGHT_DECAY = 0.0001 | ||
self.LOSS_WEIGHTS = {"rpn_class_loss": 1., "rpn_bbox_loss": 1., "mrcnn_class_loss": 1., "mrcnn_bbox_loss": 1., | ||
"mrcnn_mask_loss": 1.} | ||
self.USE_RPN_ROIS = True | ||
self.TRAIN_BN = False # Defaulting to False since batch size is often small | ||
self.GRADIENT_CLIP_NORM = 5.0 | ||
|
||
self.BATCH_SIZE = self.IMAGES_PER_GPU * self.GPU_COUNT | ||
|
||
# Input image size | ||
if self.IMAGE_RESIZE_MODE == "crop": | ||
self.IMAGE_SHAPE = np.array([self.IMAGE_MIN_DIM, self.IMAGE_MIN_DIM, 3]) | ||
else: | ||
self.IMAGE_SHAPE = np.array([self.IMAGE_MAX_DIM, self.IMAGE_MAX_DIM, 3]) | ||
|
||
# Image meta data length | ||
# See compose_image_meta() for details | ||
self.IMAGE_META_SIZE = 1 + 3 + 3 + 4 + 1 + self.NUM_CLASSES |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from user_config import * | ||
import tensorflow as tf | ||
import keras.backend as K | ||
from tensorflow.python.saved_model import signature_constants | ||
from tensorflow.python.saved_model import tag_constants | ||
import os | ||
from config import mask_config | ||
from model import MaskRCNN | ||
|
||
sess = tf.Session() | ||
K.set_session(sess) | ||
|
||
|
||
def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True): | ||
graph = sess.graph | ||
|
||
with graph.as_default(): | ||
freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or [])) | ||
|
||
output_names = output_names or [] | ||
input_graph_def = graph.as_graph_def() | ||
|
||
if clear_devices: | ||
for node in input_graph_def.node: | ||
node.device = "" | ||
|
||
frozen_graph = tf.graph_util.convert_variables_to_constants( | ||
session, input_graph_def, output_names, freeze_var_names) | ||
return frozen_graph | ||
|
||
|
||
def freeze_model(model, name): | ||
frozen_graph = freeze_session( | ||
sess, | ||
output_names=[out.op.name for out in model.outputs][:4]) | ||
directory = PATH_TO_SAVE_FROZEN_PB | ||
tf.train.write_graph(frozen_graph, directory, name , as_text=False) | ||
print("*"*80) | ||
print("Finish converting keras model to Frozen PB") | ||
print('PATH: ', PATH_TO_SAVE_FROZEN_PB) | ||
print("*" * 80) | ||
|
||
|
||
def make_serving_ready(model_path, save_serve_path, version_number): | ||
import tensorflow as tf | ||
|
||
export_dir = os.path.join(save_serve_path, str(version_number)) | ||
graph_pb = model_path | ||
|
||
builder = tf.saved_model.builder.SavedModelBuilder(export_dir) | ||
|
||
with tf.gfile.GFile(graph_pb, "rb") as f: | ||
graph_def = tf.GraphDef() | ||
graph_def.ParseFromString(f.read()) | ||
|
||
sigs = {} | ||
|
||
with tf.Session(graph=tf.Graph()) as sess: | ||
# name="" is important to ensure we don't get spurious prefixing | ||
tf.import_graph_def(graph_def, name="") | ||
g = tf.get_default_graph() | ||
input_image = g.get_tensor_by_name("input_image:0") | ||
input_image_meta = g.get_tensor_by_name("input_image_meta:0") | ||
input_anchors = g.get_tensor_by_name("input_anchors:0") | ||
|
||
output_detection = g.get_tensor_by_name("mrcnn_detection/Reshape_1:0") | ||
output_mask = g.get_tensor_by_name("mrcnn_mask/Reshape_1:0") | ||
|
||
sigs[signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY] = \ | ||
tf.saved_model.signature_def_utils.predict_signature_def( | ||
{"input_image": input_image, 'input_image_meta': input_image_meta, 'input_anchors': input_anchors}, | ||
{"mrcnn_detection/Reshape_1": output_detection, 'mrcnn_mask/Reshape_1': output_mask}) | ||
|
||
builder.add_meta_graph_and_variables(sess, | ||
[tag_constants.SERVING], | ||
signature_def_map=sigs) | ||
|
||
builder.save() | ||
print("*" * 80) | ||
print("FINISH CONVERTING FROZEN PB TO SERVING READY") | ||
print("PATH:", PATH_TO_SAVE_TENSORFLOW_SERVING_MODEL) | ||
print("*" * 80) | ||
|
||
|
||
# LOAD MODEL | ||
config = mask_config(NUMBER_OF_CLASSES) | ||
model = MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) | ||
model.load_weights(H5_WEIGHT_PATH, by_name=True) | ||
|
||
# Converting keras model to PB frozen graph | ||
freeze_model(model.keras_model, FROZEN_NAME) | ||
|
||
# Now convert frozen graph to Tensorflow Serving Ready | ||
make_serving_ready(os.path.join(PATH_TO_SAVE_FROZEN_PB, FROZEN_NAME), | ||
PATH_TO_SAVE_TENSORFLOW_SERVING_MODEL, | ||
VERSION_NUMBER) | ||
|
||
print("COMPLETED") |
Oops, something went wrong.