def main(unused_argv): tf.logging.set_verbosity(tf.logging.INFO) tf.logging.info('Prepare to export model to: %s', FLAGS.export_path) with tf.Graph().as_default() as graph: trainable = tf.constant(False,tf.bool) net_input = tf.placeholder(tf.float32, [1, input_height, input_width, 3], name=_INPUT_NAME) net = Mobilenetv2Network({'image': net_input}, conv_width=1.0, conv_width2=1.0, trainable=trainable) #net = EfficientNetwork({'image':net_input},trainable=trainable) #net, pretrain_path, last_layer = get_network('efficient_b0', q_inp_split_id, trainable=istrain) #outtensor = net.layers['concat_stage7'] outtensor = net.layers['concat_stage7'] semantic_predictions = tf.identity(outtensor, name=_OUTPUT_NAME) outtensor = tf.transpose(outtensor,[0,3,1,2],name = 'Openpose/nchw_concat_stage7') #net, _, _ = get_network(net_input,is_training=False) #semantic_predictions = tf.identity(network, name=_OUTPUT_NAME) #tf.model_variables() saver = tf.train.Saver() #os.environ['CUDA_VISIBLE_DEVICES'] = '4' tf.gfile.MakeDirs(os.path.dirname(FLAGS.export_path)) freeze_graph.freeze_graph_with_def_protos( tf.get_default_graph().as_graph_def(add_shapes=True), saver.as_saver_def(), FLAGS.checkpoint_path, 'Openpose/nchw_concat_stage7', restore_op_name=None, filename_tensor_name=None, output_graph=FLAGS.export_path, clear_devices=True, initializer_nodes=None) #float_opers = tf.profiler.ProfileOptionBuilder.float_operation() #flops = tf.profiler.profile(None, cmd='graph', options=float_opers) flops = tf.profiler.profile(graph, options=tf.profiler.ProfileOptionBuilder.float_operation()) print('FLOP = ', flops)
def get_network(type, placeholder_input, sess_for_load=None, trainable=True): if type == 'mobilenet': net = MobilenetNetwork({'image': placeholder_input}, conv_width=0.75, conv_width2=1.00, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_fast': net = MobilenetNetwork({'image': placeholder_input}, conv_width=0.5, conv_width2=0.5, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_accurate': net = MobilenetNetwork({'image': placeholder_input}, conv_width=1.00, conv_width2=1.00, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_1.0_224_2017_06_14/mobilenet_v1_1.0_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_thin': net = MobilenetNetworkThin({'image': placeholder_input}, conv_width=0.75, conv_width2=0.50, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type in ['mobilenet_v2_w1.4_r1.0', 'mobilenet_v2_large', 'mobilenet_v2_large_quantize']: # m_v2_large net = Mobilenetv2Network({'image': placeholder_input}, conv_width=1.4, conv_width2=1.0, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v2_1.4_224/mobilenet_v2_1.4_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_v2_w1.4_r0.5': net = Mobilenetv2Network({'image': placeholder_input}, conv_width=1.4, conv_width2=0.5, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v2_1.4_224/mobilenet_v2_1.4_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_v2_w1.0_r1.0': net = Mobilenetv2Network({'image': placeholder_input}, conv_width=1.0, conv_width2=1.0, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v2_1.0_224/mobilenet_v2_1.0_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_v2_w1.0_r0.75': net = Mobilenetv2Network({'image': placeholder_input}, conv_width=1.0, conv_width2=0.75, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v2_1.0_224/mobilenet_v2_1.0_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_v2_w1.0_r0.5': net = Mobilenetv2Network({'image': placeholder_input}, conv_width=1.0, conv_width2=0.5, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v2_1.0_224/mobilenet_v2_1.0_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_v2_w0.75_r0.75': net = Mobilenetv2Network({'image': placeholder_input}, conv_width=0.75, conv_width2=0.75, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v2_0.75_224/mobilenet_v2_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_v2_w0.5_r0.5' or type == 'mobilenet_v2_small': # m_v2_fast net = Mobilenetv2Network({'image': placeholder_input}, conv_width=0.5, conv_width2=0.5, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v2_0.5_224/mobilenet_v2_0.5_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_v2_1.4': net = Mobilenetv2Network({'image': placeholder_input}, conv_width=1.4, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v2_1.4_224/mobilenet_v2_1.4_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_v2_1.0': net = Mobilenetv2Network({'image': placeholder_input}, conv_width=1.0, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v2_1.0_224/mobilenet_v2_1.0_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_v2_0.75': net = Mobilenetv2Network({'image': placeholder_input}, conv_width=0.75, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v2_0.75_224/mobilenet_v2_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_v2_0.5': net = Mobilenetv2Network({'image': placeholder_input}, conv_width=0.5, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v2_0.5_224/mobilenet_v2_0.5_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type in ['cmu', 'openpose']: net = CmuNetwork({'image': placeholder_input}, trainable=trainable) pretrain_path = 'numpy/openpose_coco.npy' last_layer = 'Mconv7_stage6_L{aux}' elif type in ['cmu_quantize', 'openpose_quantize']: net = CmuNetwork({'image': placeholder_input}, trainable=trainable) pretrain_path = 'train/cmu/bs8_lr0.0001_q_e80/model_latest-18000' last_layer = 'Mconv7_stage6_L{aux}' elif type == 'vgg': net = CmuNetwork({'image': placeholder_input}, trainable=trainable) pretrain_path = 'numpy/openpose_vgg16.npy' last_layer = 'Mconv7_stage6_L{aux}' else: raise Exception('Invalid Model Name.') pretrain_path_full = os.path.join(_get_base_path(), pretrain_path) if sess_for_load is not None: if type in ['cmu', 'vgg', 'openpose']: if not os.path.isfile(pretrain_path_full): raise Exception('Model file doesn\'t exist, path=%s' % pretrain_path_full) net.load(os.path.join(_get_base_path(), pretrain_path), sess_for_load) else: try: s = '%dx%d' % (placeholder_input.shape[2], placeholder_input.shape[1]) except: s = '' ckpts = { 'mobilenet': 'trained/mobilenet_%s/model-246038' % s, 'mobilenet_thin': 'trained/mobilenet_thin_%s/model-449003' % s, 'mobilenet_fast': 'trained/mobilenet_fast_%s/model-189000' % s, 'mobilenet_accurate': 'trained/mobilenet_accurate/model-170000', 'mobilenet_v2_w1.4_r0.5': 'trained/mobilenet_v2_w1.4_r0.5/model_latest-380401', 'mobilenet_v2_large': 'trained/mobilenet_v2_w1.4_r1.0/model-570000', 'mobilenet_v2_small': 'trained/mobilenet_v2_w0.5_r0.5/model_latest-380401', } ckpt_path = os.path.join(_get_base_path(), ckpts[type]) loader = tf.train.Saver() try: loader.restore(sess_for_load, ckpt_path) except Exception as e: raise Exception('Fail to load model files. \npath=%s\nerr=%s' % (ckpt_path, str(e))) return net, pretrain_path_full, last_layer
def get_network(type, placeholder_input, sess_for_load=None, trainable=True): if type == "mobilenet": net = MobilenetNetwork( {"image": placeholder_input}, conv_width=0.75, conv_width2=1.00, trainable=trainable, ) pretrain_path = ( "pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt" ) last_layer = "MConv_Stage6_L{aux}_5" elif type == "mobilenet_fast": net = MobilenetNetwork( {"image": placeholder_input}, conv_width=0.5, conv_width2=0.5, trainable=trainable, ) pretrain_path = ( "pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt" ) last_layer = "MConv_Stage6_L{aux}_5" elif type == "mobilenet_accurate": net = MobilenetNetwork( {"image": placeholder_input}, conv_width=1.00, conv_width2=1.00, trainable=trainable, ) pretrain_path = ( "pretrained/mobilenet_v1_1.0_224_2017_06_14/mobilenet_v1_1.0_224.ckpt" ) last_layer = "MConv_Stage6_L{aux}_5" elif type == "mobilenet_thin": net = MobilenetNetworkThin( {"image": placeholder_input}, conv_width=0.75, conv_width2=0.50, trainable=trainable, ) pretrain_path = ( "pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt" ) last_layer = "MConv_Stage6_L{aux}_5" elif type in [ "mobilenet_v2_w1.4_r1.0", "mobilenet_v2_large", "mobilenet_v2_large_quantize", ]: # m_v2_large net = Mobilenetv2Network( {"image": placeholder_input}, conv_width=1.4, conv_width2=1.0, trainable=trainable, ) pretrain_path = "pretrained/mobilenet_v2_1.4_224/mobilenet_v2_1.4_224.ckpt" last_layer = "MConv_Stage6_L{aux}_5" elif type == "mobilenet_v2_w1.4_r0.5": net = Mobilenetv2Network( {"image": placeholder_input}, conv_width=1.4, conv_width2=0.5, trainable=trainable, ) pretrain_path = "pretrained/mobilenet_v2_1.4_224/mobilenet_v2_1.4_224.ckpt" last_layer = "MConv_Stage6_L{aux}_5" elif type == "mobilenet_v2_w1.0_r1.0": net = Mobilenetv2Network( {"image": placeholder_input}, conv_width=1.0, conv_width2=1.0, trainable=trainable, ) pretrain_path = "pretrained/mobilenet_v2_1.0_224/mobilenet_v2_1.0_224.ckpt" last_layer = "MConv_Stage6_L{aux}_5" elif type == "mobilenet_v2_w1.0_r0.75": net = Mobilenetv2Network( {"image": placeholder_input}, conv_width=1.0, conv_width2=0.75, trainable=trainable, ) pretrain_path = "pretrained/mobilenet_v2_1.0_224/mobilenet_v2_1.0_224.ckpt" last_layer = "MConv_Stage6_L{aux}_5" elif type == "mobilenet_v2_w1.0_r0.5": net = Mobilenetv2Network( {"image": placeholder_input}, conv_width=1.0, conv_width2=0.5, trainable=trainable, ) pretrain_path = "pretrained/mobilenet_v2_1.0_224/mobilenet_v2_1.0_224.ckpt" last_layer = "MConv_Stage6_L{aux}_5" elif type == "mobilenet_v2_w0.75_r0.75": net = Mobilenetv2Network( {"image": placeholder_input}, conv_width=0.75, conv_width2=0.75, trainable=trainable, ) pretrain_path = "pretrained/mobilenet_v2_0.75_224/mobilenet_v2_0.75_224.ckpt" last_layer = "MConv_Stage6_L{aux}_5" elif type == "mobilenet_v2_w0.5_r0.5" or type == "mobilenet_v2_small": # m_v2_fast net = Mobilenetv2Network( {"image": placeholder_input}, conv_width=0.5, conv_width2=0.5, trainable=trainable, ) pretrain_path = "pretrained/mobilenet_v2_0.5_224/mobilenet_v2_0.5_224.ckpt" last_layer = "MConv_Stage6_L{aux}_5" elif type == "mobilenet_v2_1.4": net = Mobilenetv2Network({"image": placeholder_input}, conv_width=1.4, trainable=trainable) pretrain_path = "pretrained/mobilenet_v2_1.4_224/mobilenet_v2_1.4_224.ckpt" last_layer = "MConv_Stage6_L{aux}_5" elif type == "mobilenet_v2_1.0": net = Mobilenetv2Network({"image": placeholder_input}, conv_width=1.0, trainable=trainable) pretrain_path = "pretrained/mobilenet_v2_1.0_224/mobilenet_v2_1.0_224.ckpt" last_layer = "MConv_Stage6_L{aux}_5" elif type == "mobilenet_v2_0.75": net = Mobilenetv2Network({"image": placeholder_input}, conv_width=0.75, trainable=trainable) pretrain_path = "pretrained/mobilenet_v2_0.75_224/mobilenet_v2_0.75_224.ckpt" last_layer = "MConv_Stage6_L{aux}_5" elif type == "mobilenet_v2_0.5": net = Mobilenetv2Network({"image": placeholder_input}, conv_width=0.5, trainable=trainable) pretrain_path = "pretrained/mobilenet_v2_0.5_224/mobilenet_v2_0.5_224.ckpt" last_layer = "MConv_Stage6_L{aux}_5" elif type in ["cmu", "openpose"]: net = CmuNetwork({"image": placeholder_input}, trainable=trainable) pretrain_path = "numpy/openpose_coco.npy" last_layer = "Mconv7_stage6_L{aux}" elif type in ["cmu_quantize", "openpose_quantize"]: net = CmuNetwork({"image": placeholder_input}, trainable=trainable) pretrain_path = "train/cmu/bs8_lr0.0001_q_e80/model_latest-18000" last_layer = "Mconv7_stage6_L{aux}" elif type == "vgg": net = CmuNetwork({"image": placeholder_input}, trainable=trainable) pretrain_path = "numpy/openpose_vgg16.npy" last_layer = "Mconv7_stage6_L{aux}" else: raise Exception("Invalid Model Name.") pretrain_path_full = os.path.join(_get_base_path(), pretrain_path) if sess_for_load is not None: if type in ["cmu", "vgg", "openpose"]: if not os.path.isfile(pretrain_path_full): raise Exception("Model file doesn't exist, path=%s" % pretrain_path_full) net.load(os.path.join(_get_base_path(), pretrain_path), sess_for_load) else: try: s = "%dx%d" % (placeholder_input.shape[2], placeholder_input.shape[1]) except: s = "" ckpts = { "mobilenet": "trained/mobilenet_%s/model-246038" % s, "mobilenet_thin": "trained/mobilenet_thin_%s/model-449003" % s, "mobilenet_fast": "trained/mobilenet_fast_%s/model-189000" % s, "mobilenet_accurate": "trained/mobilenet_accurate/model-170000", "mobilenet_v2_w1.4_r0.5": "trained/mobilenet_v2_w1.4_r0.5/model_latest-380401", "mobilenet_v2_large": "trained/mobilenet_v2_w1.4_r1.0/model-570000", "mobilenet_v2_small": "trained/mobilenet_v2_w0.5_r0.5/model_latest-380401", } ckpt_path = os.path.join(_get_base_path(), ckpts[type]) loader = tf.train.Saver() try: loader.restore(sess_for_load, ckpt_path) except Exception as e: raise Exception("Fail to load model files. \npath=%s\nerr=%s" % (ckpt_path, str(e))) return net, pretrain_path_full, last_layer