def testConvertVariablesToConsts(self):
    with ops.Graph().as_default():
      variable_node = variables.Variable(1.0, name="variable_node")
      _ = variables.Variable(1.0, name="unused_variable_node")
      output_node = math_ops_lib.multiply(
          variable_node, 2.0, name="output_node")
      with session.Session() as sess:
        init = variables.initialize_variables([variable_node])
        sess.run(init)
        output = sess.run(output_node)
        self.assertNear(2.0, output, 0.00001)
        variable_graph_def = sess.graph.as_graph_def()
        # First get the constant_graph_def when variable_names_whitelist is set,
        # note that if variable_names_whitelist is not set an error will be
        # thrown because unused_variable_node is not initialized.
        constant_graph_def = graph_util.convert_variables_to_constants(
            sess,
            variable_graph_def, ["output_node"],
            variable_names_whitelist=set(["variable_node"]))

        # Then initialize the unused variable, and get another
        # constant_graph_def when variable_names_whitelist is not set.
        sess.run(variables.global_variables_initializer())
        constant_graph_def_without_variable_whitelist = (
            graph_util.convert_variables_to_constants(sess, variable_graph_def,
                                                      ["output_node"]))

        # The unused variable should be cleared so the two graphs should be
        # equivalent.
        self.assertEqual(
            str(constant_graph_def),
            str(constant_graph_def_without_variable_whitelist))

        # Test variable name black list. This should result in the variable not
        # being a const.
        sess.run(variables.global_variables_initializer())
        constant_graph_def_with_blacklist = (
            graph_util.convert_variables_to_constants(
                sess,
                variable_graph_def, ["output_node"],
                variable_names_blacklist=set(["variable_node"])))
        variable_node = None
        for node in constant_graph_def_with_blacklist.node:
          if node.name == "variable_node":
            variable_node = node
        self.assertIsNotNone(variable_node)
        self.assertEqual(variable_node.op, "VariableV2")

    # Now we make sure the variable is now a constant, and that the graph still
    # produces the expected result.
    with ops.Graph().as_default():
      _ = importer.import_graph_def(constant_graph_def, name="")
      self.assertEqual(4, len(constant_graph_def.node))
      for node in constant_graph_def.node:
        self.assertNotEqual("Variable", node.op)
        self.assertNotEqual("VariableV2", node.op)
      with session.Session() as sess:
        output_node = sess.graph.get_tensor_by_name("output_node:0")
        output = sess.run(output_node)
        self.assertNear(2.0, output, 0.00001)
Exemple #2
0
def freeze_graph_with_def_protos(
    input_graph_def,
    input_saver_def,
    input_checkpoint,
    output_node_names,
    restore_op_name,
    filename_tensor_name,
    clear_devices,
    initializer_nodes,
    variable_names_blacklist=''):
  """Converts all variables in a graph and checkpoint into constants."""
  del restore_op_name, filename_tensor_name  # Unused by updated loading code.

  # 'input_checkpoint' may be a prefix if we're using Saver V2 format
  if not saver_lib.checkpoint_exists(input_checkpoint):
    raise ValueError(
        'Input checkpoint "' + input_checkpoint + '" does not exist!')

  if not output_node_names:
    raise ValueError(
        'You must supply the name of a node to --output_node_names.')

  # Remove all the explicit device specifications for this node. This helps to
  # make the graph more portable.
  if clear_devices:
    for node in input_graph_def.node:
      node.device = ''

  _ = importer.import_graph_def(input_graph_def, name='')

  with session.Session() as sess:
    if input_saver_def:
      saver = saver_lib.Saver(saver_def=input_saver_def)
      saver.restore(sess, input_checkpoint)
    else:
      var_list = {}
      reader = pywrap_tensorflow.NewCheckpointReader(input_checkpoint)
      var_to_shape_map = reader.get_variable_to_shape_map()
      for key in var_to_shape_map:
        try:
          tensor = sess.graph.get_tensor_by_name(key + ':0')
        except KeyError:
          # This tensor doesn't exist in the graph (for example it's
          # 'global_step' or a similar housekeeping element) so skip it.
          continue
        var_list[key] = tensor
      saver = saver_lib.Saver(var_list=var_list)
      saver.restore(sess, input_checkpoint)
      if initializer_nodes:
        sess.run(initializer_nodes)

    variable_names_blacklist = (variable_names_blacklist.split(',') if
                                variable_names_blacklist else None)
    output_graph_def = graph_util.convert_variables_to_constants(
        sess,
        input_graph_def,
        output_node_names.split(','),
        variable_names_blacklist=variable_names_blacklist)

  return output_graph_def
def graph_def_from_checkpoint(checkpoint_dir, output_node_names):
  """Converts checkpoint data to GraphDef.

  Reads the latest checkpoint data and produces a GraphDef in which the
  variables have been converted to constants.

  Args:
    checkpoint_dir: Path to the checkpoints.
    output_node_names: List of name strings for the result nodes of the graph.

  Returns:
    A GraphDef from the latest checkpoint

  Raises:
    ValueError: if no checkpoint is found
  """
  checkpoint_path = saver_lib.latest_checkpoint(checkpoint_dir)
  if checkpoint_path is None:
    raise ValueError('Could not find a checkpoint at: {0}.'
                     .format(checkpoint_dir))

  saver_for_restore = saver_lib.import_meta_graph(
      checkpoint_path + '.meta', clear_devices=True)
  with session.Session() as sess:
    saver_for_restore.restore(sess, checkpoint_path)
    graph_def = ops.get_default_graph().as_graph_def()
    output_graph_def = graph_util.convert_variables_to_constants(
        sess, graph_def, output_node_names)

  return output_graph_def
  def testConvertVariablesToConstsWithFunctions(self):
    @function.Defun(dtypes.float32)
    def plus_one(x):
      return x + 1.0

    with ops.Graph().as_default():
      variable_node = variables.Variable(1.0, name="variable_node")
      _ = variables.Variable(1.0, name="unused_variable_node")
      defun_node = plus_one(variable_node)
      output_node = math_ops_lib.multiply(
          defun_node, 2.0, name="output_node")

      with session.Session() as sess:
        init = variables.initialize_variables([variable_node])
        sess.run(init)
        output = sess.run(output_node)
        self.assertNear(4.0, output, 0.00001)
        variable_graph_def = sess.graph.as_graph_def()

        # First get the constant_graph_def when variable_names_whitelist is set,
        # note that if variable_names_whitelist is not set an error will be
        # thrown because unused_variable_node is not initialized.
        constant_graph_def = graph_util.convert_variables_to_constants(
            sess,
            variable_graph_def, ["output_node"],
            variable_names_whitelist=set(["variable_node"]))

        self.assertEqual(variable_graph_def.library,
                         constant_graph_def.library)
  def testConvertVariablesToConstsWithEmbeddings(self):
    """Freezes a graph with embeddings."""
    input_data = np.array(np.random.random_sample([1, 1]), dtype=np.int32)

    # Make model.
    state_input = keras.layers.Input(
        shape=(1,), name="state_input", dtype="int32")
    output = keras.layers.Embedding(
        output_dim=16, input_dim=100, input_length=1, name="state")(
            state_input)
    model = keras.models.Model(inputs=[state_input], outputs=[output])
    model.compile(
        loss={"state": "sparse_categorical_crossentropy"}, optimizer="adam")

    # Get associated session.
    sess = keras.backend.get_session()
    variable_graph_def = sess.graph_def
    output_tensor = [tensor.name.split(":")[0] for tensor in model.outputs]
    constant_graph_def = graph_util.convert_variables_to_constants(
        sess, variable_graph_def, output_tensor)

    # Ensure graph has no variables.
    for node in constant_graph_def.node:
      self.assertNotIn(
          node.op, ["Variable", "VariableV2", "VarHandleOp", "ReadVariableOp"])

    # Compare the value of the graphs.
    expected_value = model.predict(input_data)
    actual_value = self._evaluate_graph_def(constant_graph_def, model.inputs,
                                            model.outputs, [input_data])
    np.testing.assert_almost_equal(np.array([expected_value]), actual_value, 5)
def freeze_graph(input_graph, input_saver, input_binary, input_checkpoint,
                 output_node_names, restore_op_name, filename_tensor_name,
                 output_graph, clear_devices, initializer_nodes):
  """Converts all variables in a graph and checkpoint into constants."""

  if not tf.gfile.Exists(input_graph):
    print("Input graph file '" + input_graph + "' does not exist!")
    return -1

  if input_saver and not tf.gfile.Exists(input_saver):
    print("Input saver file '" + input_saver + "' does not exist!")
    return -1

  # 'input_checkpoint' may be a prefix if we're using Saver V2 format
  if not tf.train.checkpoint_exists(input_checkpoint):
    print("Input checkpoint '" + input_checkpoint + "' doesn't exist!")
    return -1

  if not output_node_names:
    print("You need to supply the name of a node to --output_node_names.")
    return -1

  input_graph_def = tf.GraphDef()
  mode = "rb" if input_binary else "r"
  with tf.gfile.FastGFile(input_graph, mode) as f:
    if input_binary:
      input_graph_def.ParseFromString(f.read())
    else:
      text_format.Merge(f.read().decode("utf-8"), input_graph_def)
  # Remove all the explicit device specifications for this node. This helps to
  # make the graph more portable.
  if clear_devices:
    for node in input_graph_def.node:
      node.device = ""
  _ = tf.import_graph_def(input_graph_def, name="")

  with tf.Session() as sess:
    if input_saver:
      with tf.gfile.FastGFile(input_saver, mode) as f:
        saver_def = tf.train.SaverDef()
        if input_binary:
          saver_def.ParseFromString(f.read())
        else:
          text_format.Merge(f.read(), saver_def)
        saver = tf.train.Saver(saver_def=saver_def)
        saver.restore(sess, input_checkpoint)
    else:
      sess.run([restore_op_name], {filename_tensor_name: input_checkpoint})
      if initializer_nodes:
        sess.run(initializer_nodes)

    variable_names_blacklist = (FLAGS.variable_names_blacklist.split(",") if
                                FLAGS.variable_names_blacklist else None)
    output_graph_def = graph_util.convert_variables_to_constants(
        sess, input_graph_def, output_node_names.split(","),
        variable_names_blacklist=variable_names_blacklist)

  with tf.gfile.GFile(output_graph, "wb") as f:
    f.write(output_graph_def.SerializeToString())
  print("%d ops in the final graph." % len(output_graph_def.node))
Exemple #7
0
  def from_session(cls,
                   sess,
                   input_tensors,
                   output_tensors,
                   freeze_variables=False):
    """Creates a TocoConverter class from a TensorFlow Session.

    Args:
      sess: TensorFlow Session.
      input_tensors: List of input tensors. Type and shape are computed using
        `foo.get_shape()` and `foo.dtype`.
      output_tensors: List of output tensors (only .name is used from this).
      freeze_variables: Boolean indicating whether the variables need to be
        converted into constants via the freeze_graph.py script.
        (default False)

    Returns:
      TocoConverter class.
    """

    # Get GraphDef.
    if freeze_variables:
      sess.run(global_variables_initializer())
      output_arrays = [tensor_name(tensor) for tensor in output_tensors]
      graph_def = tf_graph_util.convert_variables_to_constants(
          sess, sess.graph_def, output_arrays)
    else:
      graph_def = sess.graph_def

    # Create TocoConverter class.
    return cls(graph_def, input_tensors, output_tensors)
Exemple #8
0
def freeze_graph_def(sess, input_graph_def, output_node_names):
    for node in input_graph_def.node:
        if node.op == 'RefSwitch':
            node.op = 'Switch'
            for index in xrange(len(node.input)):
                if 'moving_' in node.input[index]:
                    node.input[index] = node.input[index] + '/read'
        elif node.op == 'AssignSub':
            node.op = 'Sub'
            if 'use_locking' in node.attr: del node.attr['use_locking']
        elif node.op == 'AssignAdd':
            node.op = 'Add'
            if 'use_locking' in node.attr: del node.attr['use_locking']
    
    # Get the list of important nodes
    whitelist_names = []
    for node in input_graph_def.node:
        if (node.name.startswith('InceptionResnetV1') or node.name.startswith('embeddings') or 
                node.name.startswith('phase_train') or node.name.startswith('Bottleneck') or node.name.startswith('Logits')):
            whitelist_names.append(node.name)

    # Replace all the variables in the graph with constants of the same values
    output_graph_def = graph_util.convert_variables_to_constants(
        sess, input_graph_def, output_node_names.split(","),
        variable_names_whitelist=whitelist_names)
    return output_graph_def
def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
    """
    Freezes the state of a session into a prunned computation graph.

    Creates a new computation graph where variable nodes are replaced by
    constants taking their current value in the session. The new graph will be
    prunned so subgraphs that are not neccesary to compute the requested
    outputs are removed.
    @param session The TensorFlow session to be frozen.
    @param keep_var_names A list of variable names that should not be frozen,
                          or None to freeze all the variables in the graph.
    @param output_names Names of the relevant graph outputs.
    @param clear_devices Remove the device directives from the graph for better portability.
    @return The frozen graph definition.
    """
    from tensorflow.python.framework.graph_util import convert_variables_to_constants
    graph = session.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 []
        output_names += [v.op.name for v in tf.global_variables()]
        input_graph_def = graph.as_graph_def()
        if clear_devices:
            for node in input_graph_def.node:
                node.device = ""
        frozen_graph = convert_variables_to_constants(session, input_graph_def,
                                                      output_names, freeze_var_names)
        return frozen_graph
def _freeze_graph_with_def_protos(input_graph_def, output_node_names,
                                  initializer_names, shared_init_op_name,
                                  input_saver_def, input_checkpoint):
  """Converts all variables in a graph and checkpoint into constants.

  During this process, we need to retain certain initializer nodes (e.g. table
  initializer nodes). Instead of determining which dependencies
  of the shared initializer node (e.g. group_deps) to keep, we
  reconstruct the connections between the individual initializer nodes and
  the shared node after freezing the graph.

  Args:
    input_graph_def: A GraphDef proto to be frozen.
    output_node_names: Names of output nodes.
    initializer_names: Names of initializer nodes to keep.
    shared_init_op_name: The name of the shared initializer node to connect the
      nodes in initializer names to.
    input_saver_def: A SaverDef proto used for restoring a checkpoint.
    input_checkpoint: A path to a checkpoint to restore.

  Returns:
    A frozen GraphDef.
  """

  with _ops.Graph().as_default():
    _ = _importer.import_graph_def(input_graph_def, name='')

    with _session.Session() as sess:
      saver = _saver_lib.Saver(saver_def=input_saver_def)
      saver.restore(sess, input_checkpoint)
      output_graph_def = _graph_util.convert_variables_to_constants(
          sess, input_graph_def, output_node_names + initializer_names)
      _connect_to_shared_init_op(output_graph_def, shared_init_op_name,
                                 initializer_names)
  return output_graph_def
Exemple #11
0
def freeze_graph(sess, input_tensors, output_tensors):
  """Returns a frozen GraphDef.

  Runs a Grappler pass and freezes a graph with Variables in it. Otherwise the
  existing GraphDef is returned. The Grappler pass is only run on models that
  are frozen in order to inline the functions in the graph.
  If OpHints is present, it will try to convert the OpHint graph.

  Args:
    sess: TensorFlow Session.
    input_tensors: List of input tensors.
    output_tensors: List of output tensors (only .name is used from this).

  Returns:
    Frozen GraphDef.
  """
  # Grappler inline function optimization will break OpHints graph
  # transformation, so if OpHints are present, just convert it.
  hinted_outputs_nodes = find_all_hinted_output_nodes(sess)
  if len(hinted_outputs_nodes) > 0:  #  pylint: disable=g-explicit-length-test
    return _convert_op_hints_if_present(sess, output_tensors)

  # Runs a Grappler pass in order to inline any functions in the graph.
  config = get_grappler_config(function_only=True)
  graph_def = run_graph_optimizations(
      sess.graph_def, input_tensors, output_tensors, config, graph=sess.graph)

  if not is_frozen_graph(sess):
    output_arrays = [get_tensor_name(tensor) for tensor in output_tensors]
    return tf_graph_util.convert_variables_to_constants(sess, graph_def,
                                                        output_arrays)
  else:
    return sess.graph_def
Exemple #12
0
    def saveData(self,step):
        print('{} Saving checkpoint file to: {}'.format(
            datetime.datetime.now().strftime('%m-%d %H:%M:%S'),
            self.output_dir))

        # 保存图的权值
        self.saver.save(
            self.sess, self.ckpt_file, global_step=self.global_step)
        # 保存图的结构
        tf.train.write_graph(self.sess.graph_def,
                             os.path.join(cfg.OUTPUT_DIR, cfg.DATA_VERSION, 'model'),
                             'train.pbtxt')

        # 保存到权值对图,生成可供android使用的.pb文件
        graph_def = tf.get_default_graph().as_graph_def()

        print("global_variables are")
        variables_to_save = []

        for variables in self.sess.graph_def.node:
            print("{}:{}".format(str(variables.name),type(variables)))
            variables_to_save.append(str(variables.name).split(':')[0])

        print("--------------")
        output_graph_def = graph_util.convert_variables_to_constants(  # 模型持久化,将变量值固定
            self.sess,
            graph_def,
            #['yolo/pad_1/paddings']
            variables_to_save
            #self.net.logits
            # ["predictions"]  # 需要保存节点的名字///////////////////////////////////需要再改改
        )
        with tf.gfile.GFile(
                os.path.join(cfg.OUTPUT_DIR, cfg.DATA_VERSION, 'model', 'train.'+step+str(step)+'.pb'),
                "wb") as f:  # 保存模型
            f.write(output_graph_def.SerializeToString())  # 序列化输出
        print("%d ops in the final graph." % len(output_graph_def.node))
        ####################################################################################################


        freezetime = datetime.datetime.now().strftime('%m-%d-%H-%M-%S')
        zu = ZipUtil()
        zipfilename = cfg.DATA_UploadZipFileName +'.'+str(step)+ '.' + freezetime
        # 添加啦step参数,可以按照训练对部分进行压缩,,不用全部压缩了
        zu.zip_dir(os.path.join(cfg.OUTPUT_DIR, cfg.DATA_VERSION),
                   step,
                   zipfilename)
        uploader = Uploader()
        uploader.setQiniuKEY('mMQxjyif6Uk8nSGIn9ZD3I19MBMEK3IUGngcX8_p',
                       'J5gFhdpQ-1O1rkCnlqYnzPiH3XTst2Szlv9GlmQM')

        #uploader.upload2qiniu(cfg.DATA_UploadZipFileName + '.' + freezetime,zipfilename).start()
        sendData = {"state":"prepared",
                "filename":str(zipfilename),
                "filepath":os.path.join(cfg.OUTPUT_DIR, cfg.DATA_VERSION,zipfilename),
                "step":step,
                }

        uploader.notifyForTrans(sendData)
Exemple #13
0
def save_graph_to_file(graph, graph_file_name, model_info, class_count):
  sess, _, _, _, _ = build_eval_session(model_info, class_count)
  graph = sess.graph

  output_graph_def = graph_util.convert_variables_to_constants(
      sess, graph.as_graph_def(), [FLAGS.final_tensor_name])

  with gfile.FastGFile(graph_file_name, 'wb') as f:
    f.write(output_graph_def.SerializeToString())
def train_network(graph, batch_size, num_epochs, pb_file_path):
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        epoch_delta = 2
        for epoch_index in range(num_epochs):
            for i in range(12):
                sess.run([graph['optimize']], feed_dict={
                    graph['x']: np.reshape(x_train[i], (1, 224, 224, 3)),
                    graph['y']: ([[1, 0]] if y_train[i] == 0 else [[0, 1]])
                })
            if epoch_index % epoch_delta == 0:
                total_batches_in_train_set = 0
                total_correct_times_in_train_set = 0
                total_cost_in_train_set = 0.
                for i in range(12):
                    return_correct_times_in_batch = sess.run(graph['correct_times_in_batch'], feed_dict={
                        graph['x']: np.reshape(x_train[i], (1, 224, 224, 3)),
                        graph['y']: ([[1, 0]] if y_train[i] == 0 else [[0, 1]])
                    })
                    mean_cost_in_batch = sess.run(graph['cost'], feed_dict={
                        graph['x']: np.reshape(x_train[i], (1, 224, 224, 3)),
                        graph['y']: ([[1, 0]] if y_train[i] == 0 else [[0, 1]])
                    })
                    total_batches_in_train_set += 1
                    total_correct_times_in_train_set += return_correct_times_in_batch
                    total_cost_in_train_set += (mean_cost_in_batch * batch_size)


                total_batches_in_test_set = 0
                total_correct_times_in_test_set = 0
                total_cost_in_test_set = 0.
                for i in range(3):
                    return_correct_times_in_batch = sess.run(graph['correct_times_in_batch'], feed_dict={
                        graph['x']: np.reshape(x_val[i], (1, 224, 224, 3)),
                        graph['y']: ([[1, 0]] if y_val[i] == 0 else [[0, 1]])
                    })
                    mean_cost_in_batch = sess.run(graph['cost'], feed_dict={
                        graph['x']: np.reshape(x_val[i], (1, 224, 224, 3)),
                        graph['y']: ([[1, 0]] if y_val[i] == 0 else [[0, 1]])
                    })
                    total_batches_in_test_set += 1
                    total_correct_times_in_test_set += return_correct_times_in_batch
                    total_cost_in_test_set += (mean_cost_in_batch * batch_size)

                acy_on_test  = total_correct_times_in_test_set / float(total_batches_in_test_set * batch_size)
                acy_on_train = total_correct_times_in_train_set / float(total_batches_in_train_set * batch_size)
                print('Epoch - {:2d}, acy_on_test:{:6.2f}%({}/{}),loss_on_test:{:6.2f}, acy_on_train:{:6.2f}%({}/{}),loss_on_train:{:6.2f}'.format(epoch_index, acy_on_test*100.0,total_correct_times_in_test_set,
                                                                                                                                                   total_batches_in_test_set * batch_size,
                                                                                                                                                   total_cost_in_test_set,
                                                                                                                                                   acy_on_train * 100.0,
                                                                                                                                                   total_correct_times_in_train_set,
                                                                                                                                                   total_batches_in_train_set * batch_size,
                                                                                                                                                   total_cost_in_train_set))
            constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def, ["output"])
            with tf.gfile.FastGFile(pb_file_path, mode='wb') as f:
                f.write(constant_graph.SerializeToString())
Exemple #15
0
def _convert_op_hints_if_present(sess, output_tensors):
  if is_frozen_graph(sess):
    raise ValueError("Try to convert op hints, needs unfrozen graph.")
  hinted_outputs_nodes = find_all_hinted_output_nodes(sess)
  output_arrays = [get_tensor_name(tensor) for tensor in output_tensors]
  graph_def = tf_graph_util.convert_variables_to_constants(
      sess, sess.graph_def, output_arrays + hinted_outputs_nodes)
  graph_def = convert_op_hints_to_stubs(graph_def=graph_def)
  graph_def = tf_graph_util.remove_training_nodes(graph_def)
  return graph_def
  def _get_initial_outputs(self, output_tensor_names_list):
    with self.session(graph=self.initial_graph) as sess1:
      self._prune_model(sess1)
      reference_outputs = self._get_outputs(sess1, self.initial_graph,
                                            output_tensor_names_list)

      self.initial_graph_def = graph_util.convert_variables_to_constants(
          sess1, sess1.graph.as_graph_def(),
          _get_node_names(output_tensor_names_list))
    return reference_outputs
def freeze_saved_model(saved_model_dir, input_arrays, input_shapes,
                       output_arrays, tag_set, signature_key):
  """Converts a SavedModel to a frozen graph.

  Args:
    saved_model_dir: SavedModel directory to convert.
    input_arrays: List of input tensors to freeze graph with. Uses input arrays
      from SignatureDef when none are provided.
    input_shapes: Dict of strings representing input tensor names to list of
      integers representing input shapes (e.g., {"foo": : [1, 16, 16, 3]}).
      Automatically determined when input shapes is None (e.g., {"foo" : None}).
    output_arrays: List of output tensors to freeze graph with. Uses output
      arrays from SignatureDef when none are provided.
    tag_set: Set of tags identifying the MetaGraphDef within the SavedModel to
      analyze. All tags in the tag set must be present.
    signature_key: Key identifying SignatureDef containing inputs and outputs.

  Returns:
    frozen_graph_def: Frozen GraphDef.
    in_tensors: List of input tensors for the graph.
    out_tensors: List of output tensors for the graph.

  Raises:
    ValueError:
      SavedModel doesn't contain a MetaGraphDef identified by tag_set.
      signature_key is not in the MetaGraphDef.
      assets/ directory is in the MetaGraphDef.
      input_shapes does not match the length of input_arrays.
      input_arrays or output_arrays are not valid.
  """
  # Read SignatureDef.
  meta_graph = _get_meta_graph_def(saved_model_dir, tag_set)
  signature_def = _get_signature_def(meta_graph, signature_key)
  inputs, outputs = _get_inputs_outputs(signature_def)

  # Check SavedModel for assets directory.
  collection_def = meta_graph.collection_def
  if constants.ASSETS_KEY in collection_def:
    raise ValueError("SavedModels with assets/ directory are not supported.")

  graph = ops.Graph()
  with session.Session(graph=graph) as sess:
    loader.load(sess, meta_graph.meta_info_def.tags, saved_model_dir)

    # Gets input and output tensors.
    # TODO(zhixianyan): Use TFLite supported Op list to filter outputs.
    in_tensors = _get_tensors(graph, inputs, input_arrays)
    out_tensors = _get_tensors(graph, outputs, output_arrays)
    set_tensor_shapes(in_tensors, input_shapes)

    output_names = [node.split(":")[0] for node in outputs]
    frozen_graph_def = tf_graph_util.convert_variables_to_constants(
        sess, graph.as_graph_def(), output_names)

    return frozen_graph_def, in_tensors, out_tensors
Exemple #18
0
  def testConvertVariablesToConsts(self):
    with tf.Graph().as_default():
      variable_node = tf.Variable(1.0, name="variable_node")
      _ = tf.Variable(1.0, name="unused_variable_node")
      output_node = tf.mul(variable_node, 2.0, name="output_node")
      with tf.Session() as sess:
        init = tf.initialize_variables([variable_node])
        sess.run(init)
        output = sess.run(output_node)
        self.assertNear(2.0, output, 0.00001)
        variable_graph_def = sess.graph.as_graph_def()
        # First get the constant_graph_def when variable_names_whitelist is set,
        # note that if variable_names_whitelist is not set an error will be
        # thrown because unused_variable_node is not initialized.
        constant_graph_def = graph_util.convert_variables_to_constants(
            sess, variable_graph_def, ["output_node"],
            variable_names_whitelist=set(["variable_node"]))

        # Then initialize the unused variable, and get another
        # constant_graph_def when variable_names_whitelist is not set.
        sess.run(tf.initialize_all_variables())
        constant_graph_def_without_variable_whitelist = (
            graph_util.convert_variables_to_constants(
                sess, variable_graph_def, ["output_node"]))

        # The unused variable should be cleared so the two graphs should be
        # equivalent.
        self.assertEqual(str(constant_graph_def),
                         str(constant_graph_def_without_variable_whitelist))

    # Now we make sure the variable is now a constant, and that the graph still
    # produces the expected result.
    with tf.Graph().as_default():
      _ = tf.import_graph_def(constant_graph_def, name="")
      self.assertEqual(4, len(constant_graph_def.node))
      for node in constant_graph_def.node:
        self.assertNotEqual("Variable", node.op)
      with tf.Session() as sess:
        output_node = sess.graph.get_tensor_by_name("output_node:0")
        output = sess.run(output_node)
        self.assertNear(2.0, output, 0.00001)
def convert_tf_hub_module(module_path, output_dir,
                          signature='default', quantization_dtype=None,
                          skip_op_check=False, strip_debug_ops=False):
  """Freeze the TF-Hub module and check compatibility with Tensorflow.js.

  Optimize and convert the TF-Hub module to Tensorflow.js format, if it passes
  the compatiblity check.

  Args:
    module_path: string Path to the module.
    output_dir: string The name of the output directory. The directory
      will consist of
      - a file named 'tensorflowjs_model.pb'
      - a JSON weights manifest file named 'weights_manifest.json'
      - possibly sharded binary weight files.
    signature: string Signature to load.
    skip_op_check: Bool whether to skip the op check.
    strip_debug_ops: Bool whether to strip debug ops.
  """

  if not os.path.exists(output_dir):
    os.makedirs(output_dir)

  graph, sess, inputs, outputs = load_and_initialize_hub_module(
      module_path, signature)

  input_node_names = []
  output_node_names = []

  for _, input_tensor in inputs.items():
    input_node_names.append(input_tensor.name.split(':')[0])
  for _, output_tensor in outputs.items():
    output_node_names.append(output_tensor.name.split(':')[0])

  print('Creating a model with inputs %s and outputs %s.' % (input_node_names,
                                                             output_node_names))

  frozen_graph_def = graph_util.convert_variables_to_constants(
      sess, graph.as_graph_def(), output_node_names)

  output_graph = os.path.join(output_dir, DEFAULT_MODEL_PB_FILENAME)
  frozen_file = output_graph + '.frozen'
  try:
    with tf.gfile.GFile(frozen_file, 'wb') as f:
      f.write(frozen_graph_def.SerializeToString())

    graph = load_graph(frozen_file, ','.join(output_node_names))
    optimize_graph(graph, output_graph, quantization_dtype=quantization_dtype,
                   skip_op_check=skip_op_check, strip_debug_ops=strip_debug_ops)
  finally:
    # Clean up the temp files.
    if os.path.exists(frozen_file):
      os.remove(frozen_file)
def freeze_graph(session, outputs):
  """Freeze the current graph.

  Args:
    session: Tensorflow sessions containing the graph
    outputs: List of output tensors

  Returns:
    The frozen graph_def.
  """
  return tf_graph_util.convert_variables_to_constants(
      session, session.graph.as_graph_def(), [x.op.name for x in outputs])
Exemple #21
0
def Save_Graft():
    v1 = tf.Variable(tf.constant(1.0, shape=[1]), name="v1")
    v2 = tf.Variable(tf.constant(2.0, shape=[1]), name="v1")
    result = v1 + v2
    init_op = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init_op)
        graph_def = tf.get_default_graph().as_graph_def()
        output_graph_def = graph_util.convert_variables_to_constants(sess, graph_def, ['add'])
        # 这个方法要自己创建一个文件夹
        with tf.gfile.GFile("E:/python/tensorflow_learning/minist/data_persistence/model3/combined_modle.pb",
                            "wb") as f:
            f.write(output_graph_def.SerializeToString())
def save_pb():
    v1 = tf.Variable(tf.constant(1.0, shape=[1]), name="v1")
    v2 = tf.Variable(tf.constant(2.0, shape=[1]), name="v2")
    result = tf.add(v1, v2, name="result")
    init_op = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init_op)
        sess.run(result)
        # for op in tf.get_default_graph().get_operations():
        #     print(op.name, op.values())
        graph_def = tf.get_default_graph().as_graph_def()
        output_graph_def = graph_util.convert_variables_to_constants(
            sess, graph_def, ['result'])
        with tf.gfile.GFile(BASE_PATH + "/model/model.pb", "wb") as f:
            f.write(output_graph_def.SerializeToString())
Exemple #23
0
  def _convert_saved_model(self):
    """Convert the input SavedModel."""
    graph = ops.Graph()
    with session.Session(graph=graph, config=self._session_config) as sess:
      input_meta_graph_def = loader.load(sess, self._input_saved_model_tags,
                                         self._input_saved_model_dir)
      input_signature_def = input_meta_graph_def.signature_def[
          self._input_saved_model_signature_key]

      def _gather_names(tensor_info):
        """Get the node names from a TensorInfo."""
        return set([tensor_info[key].name.split(":")[0] for key in tensor_info])

      # Get input and outputs from all SignatureDef.
      output_node_names = _gather_names(input_signature_def.inputs).union(
          _gather_names(input_signature_def.outputs))

      # Freeze the variables in the SavedModel graph and copy the frozen
      # graph over.
      frozen_graph_def = graph_util.convert_variables_to_constants(
          sess, sess.graph.as_graph_def(add_shapes=True),
          list(output_node_names))
      self._grappler_meta_graph_def = meta_graph_pb2.MetaGraphDef()
      self._grappler_meta_graph_def.graph_def.CopyFrom(frozen_graph_def)

      # Copy the collections that are not variables.
      for key in input_meta_graph_def.collection_def:
        # TODO(laigd): currently we use the collection key to filter out
        # collections that depend on variable ops, but this may miss some
        # other user-defined collections. A better way would be to use
        # CollectionDef::NodeList for the filtering.
        if key not in [
            "variables", "local_variables", "model_variables",
            "trainable_variables", "train_op", "table_initializer"
        ]:
          self._grappler_meta_graph_def.collection_def[key].CopyFrom(
              input_meta_graph_def.collection_def[key])

      self._add_nodes_blacklist()

      # Copy other information.
      self._grappler_meta_graph_def.meta_info_def.CopyFrom(
          input_meta_graph_def.meta_info_def)
      self._grappler_meta_graph_def.signature_def[
          self._input_saved_model_signature_key].CopyFrom(input_signature_def)
      # TODO(laigd): maybe add back AssetFileDef.

    self._run_conversion()
Exemple #24
0
  def _convert_saved_model(self):
    """Convert the input SavedModel."""
    graph = ops.Graph()
    with session.Session(graph=graph, config=self._session_config) as sess:
      input_meta_graph_def = loader.load(sess, self._input_saved_model_tags,
                                         self._input_saved_model_dir)
      input_signature_def = input_meta_graph_def.signature_def[
          self._input_saved_model_signature_key]

      def _gather_names(tensor_info):
        """Get the node names from a TensorInfo."""
        return set([tensor_info[key].name.split(":")[0] for key in tensor_info])

      # Get input and outputs from all SignatureDef.
      output_node_names = _gather_names(input_signature_def.inputs).union(
          _gather_names(input_signature_def.outputs))

      # Preserve nodes in collection
      for collection_key in self._collections_to_keep(
          input_meta_graph_def.collection_def):
        for op in sess.graph.get_collection(collection_key):
          if isinstance(op, ops.Operation):
            output_node_names.add(op.name.split(":")[0])

      # Freeze the variables in the SavedModel graph and copy the frozen
      # graph over.
      frozen_graph_def = graph_util.convert_variables_to_constants(
          sess, sess.graph.as_graph_def(add_shapes=True),
          list(output_node_names))
      self._grappler_meta_graph_def = meta_graph_pb2.MetaGraphDef()
      self._grappler_meta_graph_def.graph_def.CopyFrom(frozen_graph_def)

      # Copy the collections that are not variables.
      for collection_key in self._collections_to_keep(
          input_meta_graph_def.collection_def):
        self._grappler_meta_graph_def.collection_def[collection_key].CopyFrom(
            input_meta_graph_def.collection_def[collection_key])

      self._add_nodes_blacklist()

      # Copy other information.
      self._grappler_meta_graph_def.meta_info_def.CopyFrom(
          input_meta_graph_def.meta_info_def)
      self._grappler_meta_graph_def.signature_def[
          self._input_saved_model_signature_key].CopyFrom(input_signature_def)
      # TODO(laigd): maybe add back AssetFileDef.

    self._run_conversion()
Exemple #25
0
def main(args):
    with tf.Graph().as_default():
        with tf.Session() as sess:
            # Load the model metagraph and checkpoint
            print('Model directory: %s' % args.model_dir)
            meta_file, ckpt_file = facenet.get_model_filenames(os.path.expanduser(args.model_dir))
            
            print('Metagraph file: %s' % meta_file)
            print('Checkpoint file: %s' % ckpt_file)

            model_dir_exp = os.path.expanduser(args.model_dir)
            saver = tf.train.import_meta_graph(os.path.join(model_dir_exp, meta_file), clear_devices=True)
            tf.get_default_session().run(tf.global_variables_initializer())
            tf.get_default_session().run(tf.local_variables_initializer())
            saver.restore(tf.get_default_session(), os.path.join(model_dir_exp, ckpt_file))
            
            # Retrieve the protobuf graph definition and fix the batch norm nodes
            gd = sess.graph.as_graph_def()
            for node in gd.node:            
                if node.op == 'RefSwitch':
                    node.op = 'Switch'
                    for index in xrange(len(node.input)):
                        if 'moving_' in node.input[index]:
                            node.input[index] = node.input[index] + '/read'
                elif node.op == 'AssignSub':
                    node.op = 'Sub'
                    if 'use_locking' in node.attr: del node.attr['use_locking']
                elif node.op == 'AssignAdd':
                    node.op = 'Add'
                    if 'use_locking' in node.attr: del node.attr['use_locking']
            
            # Get the list of important nodes
            output_node_names = 'embeddings'
            whitelist_names = []
            for node in gd.node:
                if node.name.startswith('InceptionResnetV1') or node.name.startswith('embeddings') or node.name.startswith('phase_train'):
                    print(node.name)
                    whitelist_names.append(node.name)

            # Replace all the variables in the graph with constants of the same values
            output_graph_def = graph_util.convert_variables_to_constants(
                sess, gd, output_node_names.split(","),
                variable_names_whitelist=whitelist_names)

        # Serialize and dump the output graph to the filesystem
        with tf.gfile.GFile(args.output_file, 'wb') as f:
            f.write(output_graph_def.SerializeToString())
        print("%d ops in the final graph." % len(output_graph_def.node))
  def _GetGraphDef(self, use_trt, max_batch_size, model_dir):
    """Get the frozen mnist GraphDef.

    Args:
      use_trt: whether use TF-TRT to convert the graph.
      max_batch_size: the max batch size to apply during TF-TRT conversion.
      model_dir: the model directory to load the checkpoints.

    Returns:
      The frozen mnist GraphDef.
    """
    graph = ops.Graph()
    with self.session(graph=graph) as sess:
      with graph.device('/GPU:0'):
        x = array_ops.placeholder(
            shape=(None, 28, 28, 1), dtype=dtypes.float32, name=INPUT_NODE_NAME)
        self._BuildGraph(x)
      # Load weights
      mnist_saver = saver.Saver()
      checkpoint_file = latest_checkpoint(model_dir)
      mnist_saver.restore(sess, checkpoint_file)
      # Freeze
      graph_def = graph_util.convert_variables_to_constants(
          sess, sess.graph_def, output_node_names=[OUTPUT_NODE_NAME])
    # Convert with TF-TRT
    if use_trt:
      logging.info('Number of nodes before TF-TRT conversion: %d',
                   len(graph_def.node))
      converter = trt_convert.TrtGraphConverter(
          input_graph_def=graph_def,
          nodes_blacklist=[OUTPUT_NODE_NAME],
          max_batch_size=max_batch_size,
          precision_mode='INT8',
          # There is a 2GB GPU memory limit for each test, so we set
          # max_workspace_size_bytes to 256MB to leave enough room for TF
          # runtime to allocate GPU memory.
          max_workspace_size_bytes=1 << 28,
          minimum_segment_size=2,
          use_calibration=False,
          use_function_backup=False)
      graph_def = converter.convert()
      logging.info('Number of nodes after TF-TRT conversion: %d',
                   len(graph_def.node))
      num_engines = len(
          [1 for n in graph_def.node if str(n.op) == 'TRTEngineOp'])
      self.assertEqual(1, num_engines)
    return graph_def
def _freeze_graph_with_def_protos(
    input_graph_def,
    output_node_names,
    input_saver_def,
    input_checkpoint):
  """Converts all variables in a graph and checkpoint into constants."""

  with _ops.Graph().as_default():
    _ = _importer.import_graph_def(input_graph_def, name='')

    with _session.Session() as sess:
      saver = _saver_lib.Saver(saver_def=input_saver_def)
      saver.restore(sess, input_checkpoint)
      output_graph_def = _graph_util.convert_variables_to_constants(
          sess, input_graph_def, output_node_names)

  return output_graph_def
def freeze_mobilenet(meta_file, img_size=224, factor=1.0, num_classes=1001):

  tf.reset_default_graph()

  inp = tf.placeholder(tf.float32,
                      shape=(None, img_size, img_size, 3),
                      name="input")

  is_training=False
  weight_decay = 0.0
  arg_scope = mobilenet_v1.mobilenet_v1_arg_scope(weight_decay=weight_decay)
  with slim.arg_scope(arg_scope):
    logits, _ = mobilenet_v1.mobilenet_v1(inp,
                                          num_classes=num_classes,
                                          is_training=is_training,
                                          depth_multiplier=factor)

  predictions = tf.contrib.layers.softmax(logits)
  output = tf.identity(predictions, name='output')

  ckpt_file = meta_file.replace('.meta', '')
  output_graph_fn = ckpt_file.replace('.ckpt', '.pb')
  output_node_names = "output"

  rest_var = slim.get_variables_to_restore()

  with tf.Session() as sess:
    graph = tf.get_default_graph()
    input_graph_def = graph.as_graph_def()

    saver = tf.train.Saver(rest_var)
    saver.restore(sess, ckpt_file)

    # We use a built-in TF helper to export variables to constant
    output_graph_def = graph_util.convert_variables_to_constants(
        sess, # The session is used to retrieve the weights
        input_graph_def, # The graph_def is used to retrieve the nodes
        # The output node names are used to select the useful nodes
        output_node_names.split(",")
    )

    # Finally we serialize and dump the output graph to the filesystem
    with tf.gfile.GFile(output_graph_fn, "wb") as f:
        f.write(output_graph_def.SerializeToString())
    print("{} ops in the final graph.".format(len(output_graph_def.node)))
 def _GetGraphDef(self):
   """Get the graph def for testing."""
   g, var, _, _ = self._GetGraph()
   with self.session(graph=g, config=self._GetConfigProto()) as sess:
     sess.run(var.initializer)
     graph_def = graph_util.convert_variables_to_constants(
         sess, g.as_graph_def(add_shapes=True), ["output"])
   node_name_to_op = {node.name: node.op for node in graph_def.node}
   self.assertEqual({
       "v1": "Const",
       "v1/read": "Identity",
       "input": "Placeholder",
       "add": "Add",
       "mul": "Mul",
       "add_1": "Add",
       "output": "Identity"
   }, node_name_to_op)
   return graph_def
Exemple #30
0
def freeze_graph(model_folder):
    # We retrieve our checkpoint fullpath
    checkpoint = tf.train.get_checkpoint_state(model_folder)
    input_checkpoint = checkpoint.model_checkpoint_path

    # We precise the file fullname of our freezed graph
    absolute_model_folder = "/".join(input_checkpoint.split('/')[:-1])
    output_graph = absolute_model_folder + "/frozen_model.pb"

    # Before exporting our graph, we need to precise what is our output node
    # this variables is plural, because you can have multiple output nodes
    #freeze之前必须明确哪个是输出结点,也就是我们要得到推论结果的结点
    #输出结点可以看我们模型的定义
    #只有定义了输出结点,freeze才会把得到输出结点所必要的结点都保存下来,或者哪些结点可以丢弃
    #所以,output_node_names必须根据不同的网络进行修改
    output_node_names = "Accuracy/predictions"

    # We clear the devices, to allow TensorFlow to control on the loading where it wants operations to be calculated
    clear_devices = True

    # We import the meta graph and retrive a Saver
    saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices)

    # We retrieve the protobuf graph definition
    graph = tf.get_default_graph()
    input_graph_def = graph.as_graph_def()

    #We start a session and restore the graph weights
    #这边已经将训练好的参数加载进来,也即最后保存的模型是有图,并且图里面已经有参数了,所以才叫做是frozen
    #相当于将参数已经固化在了图当中
    with tf.Session() as sess:
        saver.restore(sess, input_checkpoint)

        # We use a built-in TF helper to export variables to constant
        output_graph_def = graph_util.convert_variables_to_constants(
            sess,
            input_graph_def,
            output_node_names.split(",") # We split on comma for convenience
        )

        # Finally we serialize and dump the output graph to the filesystem
        with tf.gfile.GFile(output_graph, "wb") as f:
            f.write(output_graph_def.SerializeToString())
        print("%d ops in the final graph." % len(output_graph_def.node))
Exemple #31
0
def optimize_class_model(args, num_labels, logger=None):
    """
    加载中文分类模型
    :param args:
    :param num_labels:
    :param logger:
    :return:
    """
    if not logger:
        logger = set_logger(
            colored('CLASSIFICATION_MODEL, Lodding...', 'cyan'), args.verbose)
    try:
        # 如果PB文件已经存在则,返回PB文件的路径,否则将模型转化为PB文件,并且返回存储PB文件的路径
        if args.model_pb_dir is None:
            # 获取当前的运行路径
            tmp_file = os.path.join(os.getcwd(), 'predict_optimizer')
            if not os.path.exists(tmp_file):
                os.mkdir(tmp_file)
        else:
            tmp_file = args.model_pb_dir
        pb_file = os.path.join(tmp_file, 'classification_model.pb')
        if os.path.exists(pb_file):
            print('pb_file exits', pb_file)
            return pb_file
        import tensorflow as tf

        graph = tf.Graph()
        with graph.as_default():
            with tf.Session() as sess:
                input_ids = tf.placeholder(tf.int32, (None, args.max_seq_len),
                                           'input_ids')
                input_mask = tf.placeholder(tf.int32, (None, args.max_seq_len),
                                            'input_mask')

                bert_config = modeling.BertConfig.from_json_file(
                    os.path.join(args.bert_model_dir, 'bert_config.json'))
                from bert_base.train.models import create_classification_model
                loss, per_example_loss, logits, probabilities = create_classification_model(
                    bert_config=bert_config,
                    is_training=False,
                    input_ids=input_ids,
                    input_mask=input_mask,
                    segment_ids=None,
                    labels=None,
                    num_labels=num_labels)
                # pred_ids = tf.argmax(probabilities, axis=-1, output_type=tf.int32, name='pred_ids')
                # pred_ids = tf.identity(pred_ids, 'pred_ids')
                probabilities = tf.identity(probabilities, 'pred_prob')
                saver = tf.train.Saver()

            with tf.Session() as sess:
                sess.run(tf.global_variables_initializer())
                saver.restore(sess, tf.train.latest_checkpoint(args.model_dir))
                logger.info('freeze...')
                from tensorflow.python.framework import graph_util
                tmp_g = graph_util.convert_variables_to_constants(
                    sess, graph.as_graph_def(), ['pred_prob'])
                logger.info('predict cut finished !!!')
        # 存储二进制模型到文件中
        logger.info('write graph to a tmp file: %s' % pb_file)
        with tf.gfile.GFile(pb_file, 'wb') as f:
            f.write(tmp_g.SerializeToString())
        return pb_file
    except Exception as e:
        logger.error('fail to optimize the graph! %s' % e, exc_info=True)
Exemple #32
0
    print('CNN Finished')
    #print('classify = ', classify)
    #layer0.__setstate__(loaded_objects[0])
    #layer1.__setstate__(loaded_objects[1])
    #layer2.__setstate__(loaded_objects[2])
    #layer3.__setstate__(loaded_objects[3])
    #layer4.__setstate__(loaded_objects[4])
    pb_file_path = './model/livecount.pb'
    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)
        writer = tf.summary.FileWriter("./model/", sess.graph)
        #saver = tf.train.Saver()
        #saver.save(sess, MODEL_SAVE_DIR)
        constant_graph = graph_util.convert_variables_to_constants(
            sess, sess.graph.as_graph_def(), ["output"])
        with tf.gfile.FastGFile(pb_file_path, mode='wb') as f:
            f.write(constant_graph.SerializeToString())
    sess.close()

    def classify(index, frames):

        #MODEL_SAVE_DIR ='./model/'
        with tf.Session() as sess:
            init = tf.global_variables_initializer()
            sess.run(init)
            #saver = tf.train.Saver()
            #saver.save(sess, MODEL_SAVE_DIR)
            print(test_set_x[0].shape)
            print(test_set_x[index * batch_size:(index + 1) *
                             batch_size].shape)
Exemple #33
0
def save_pb():
    minimal_graph = convert_variables_to_constants(sess, sess.graph_def,["output_node"])
    tf.train.write_graph(minimal_graph, '.','uncompressed_Alex.pb', as_text=False)
    os.system('gzip -c uncompressed_Alex.pb > uncompressed_Alex.pb.gz')
Exemple #34
0
def main(_):
    # Setup the directory we'll write summaries to for TensorBoard
    if tf.gfile.Exists(FLAGS.summaries_dir):
        tf.gfile.DeleteRecursively(FLAGS.summaries_dir)
    tf.gfile.MakeDirs(FLAGS.summaries_dir)

    # Set up the pre-trained graph.
    maybe_download_and_extract()
    graph, bottleneck_tensor, jpeg_data_tensor, resized_image_tensor = (
        create_inception_graph())

    # Look at the folder structure, and create lists of all the images.
    image_lists = create_image_lists(FLAGS.image_dir, FLAGS.testing_percentage,
                                     FLAGS.validation_percentage)
    class_count = len(list(image_lists.keys()))
    if class_count == 0:
        print('No valid folders of images found at ' + FLAGS.image_dir)
        return -1
    if class_count == 1:
        print('Only one valid folder of images found at ' + FLAGS.image_dir +
              ' - multiple classes are needed for classification.')
        return -1

    # See if the command-line flags mean we're applying any distortions.
    do_distort_images = should_distort_images(FLAGS.flip_left_right,
                                              FLAGS.random_crop,
                                              FLAGS.random_scale,
                                              FLAGS.random_brightness)
    sess = tf.Session()

    if do_distort_images:
        # We will be applying distortions, so setup the operations we'll need.
        distorted_jpeg_data_tensor, distorted_image_tensor = add_input_distortions(
            FLAGS.flip_left_right, FLAGS.random_crop, FLAGS.random_scale,
            FLAGS.random_brightness)
    else:
        # We'll make sure we've calculated the 'bottleneck' image summaries and
        # cached them on disk.
        cache_bottlenecks(sess, image_lists, FLAGS.image_dir,
                          FLAGS.bottleneck_dir, jpeg_data_tensor,
                          bottleneck_tensor)

    # Add the new layer that we'll be training.
    (train_step, cross_entropy, bottleneck_input, ground_truth_input,
     final_tensor) = add_final_training_ops(len(list(image_lists.keys())),
                                            FLAGS.final_tensor_name,
                                            bottleneck_tensor)

    # Create the operations we need to evaluate the accuracy of our new layer.
    evaluation_step, prediction = add_evaluation_step(final_tensor,
                                                      ground_truth_input)

    # Merge all the summaries and write them out to /tmp/retrain_logs (by
    # default)
    merged = tf.summary.merge_all()
    train_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/train',
                                         sess.graph)
    validation_writer = tf.summary.FileWriter(FLAGS.summaries_dir +
                                              '/validation')

    # Set up all our weights to their initial default values.
    init = tf.global_variables_initializer()
    sess.run(init)

    # Run the training for as many cycles as requested on the command line.
    for i in range(FLAGS.how_many_training_steps):
        # Get a batch of input bottleneck values, either calculated fresh every time
        # with distortions applied, or from the cache stored on disk.
        if do_distort_images:
            train_bottlenecks, train_ground_truth = get_random_distorted_bottlenecks(
                sess, image_lists, FLAGS.train_batch_size, 'training',
                FLAGS.image_dir, distorted_jpeg_data_tensor,
                distorted_image_tensor, resized_image_tensor,
                bottleneck_tensor)
        else:
            train_bottlenecks, train_ground_truth, _ = get_random_cached_bottlenecks(
                sess, image_lists, FLAGS.train_batch_size, 'training',
                FLAGS.bottleneck_dir, FLAGS.image_dir, jpeg_data_tensor,
                bottleneck_tensor)
        # Feed the bottlenecks and ground truth into the graph, and run a training
        # step. Capture training summaries for TensorBoard with the `merged`
        # op.
        train_summary, _ = sess.run(
            [merged, train_step],
            feed_dict={
                bottleneck_input: train_bottlenecks,
                ground_truth_input: train_ground_truth
            })
        train_writer.add_summary(train_summary, i)

        # Every so often, print out how well the graph is training.
        is_last_step = (i + 1 == FLAGS.how_many_training_steps)
        if (i % FLAGS.eval_step_interval) == 0 or is_last_step:
            train_accuracy, cross_entropy_value = sess.run(
                [evaluation_step, cross_entropy],
                feed_dict={
                    bottleneck_input: train_bottlenecks,
                    ground_truth_input: train_ground_truth
                })
            print('%s: Step %d: Train accuracy = %.1f%%' %
                  (datetime.now(), i, train_accuracy * 100))
            print('%s: Step %d: Cross entropy = %f' %
                  (datetime.now(), i, cross_entropy_value))
            validation_bottlenecks, validation_ground_truth, _ = (
                get_random_cached_bottlenecks(
                    sess, image_lists, FLAGS.validation_batch_size,
                    'validation', FLAGS.bottleneck_dir, FLAGS.image_dir,
                    jpeg_data_tensor, bottleneck_tensor))
            # Run a validation step and capture training summaries for TensorBoard
            # with the `merged` op.
            validation_summary, validation_accuracy = sess.run(
                [merged, evaluation_step],
                feed_dict={
                    bottleneck_input: validation_bottlenecks,
                    ground_truth_input: validation_ground_truth
                })
            validation_writer.add_summary(validation_summary, i)
            print('%s: Step %d: Validation accuracy = %.1f%% (N=%d)' %
                  (datetime.now(), i, validation_accuracy * 100,
                   len(validation_bottlenecks)))

    # We've completed all our training, so run a final test evaluation on
    # some new images we haven't used before.
    test_bottlenecks, test_ground_truth, test_filenames = (
        get_random_cached_bottlenecks(sess, image_lists, FLAGS.test_batch_size,
                                      'testing', FLAGS.bottleneck_dir,
                                      FLAGS.image_dir, jpeg_data_tensor,
                                      bottleneck_tensor))
    test_accuracy, predictions = sess.run([evaluation_step, prediction],
                                          feed_dict={
                                              bottleneck_input:
                                              test_bottlenecks,
                                              ground_truth_input:
                                              test_ground_truth
                                          })
    print('Final test accuracy = %.1f%% (N=%d)' %
          (test_accuracy * 100, len(test_bottlenecks)))

    if FLAGS.print_misclassified_test_images:
        print('=== MISCLASSIFIED TEST IMAGES ===')
        for i, test_filename in enumerate(test_filenames):
            if predictions[i] != test_ground_truth[i].argmax():
                print(
                    '%70s  %s' %
                    (test_filename, list(image_lists.keys())[predictions[i]]))

    # Write out the trained graph and labels with the weights stored as
    # constants.
    output_graph_def = graph_util.convert_variables_to_constants(
        sess, graph.as_graph_def(), [FLAGS.final_tensor_name])
    with gfile.FastGFile(FLAGS.output_graph, 'wb') as f:
        f.write(output_graph_def.SerializeToString())
    with gfile.FastGFile(FLAGS.output_labels, 'w') as f:
        f.write('\n'.join(list(image_lists.keys())) + '\n')
                         output_fld,
                         f,
                         as_text=True)
    print('saved the graph definition in ascii format at: ',
          str(Path(output_fld) / f))

# convert variables to constants and save

# In[ ]:

from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io

if args.quantize:
    from tensorflow.tools.graph_transforms import TransformGraph

    transforms = ["quantize_weights", "quantize_nodes"]
    transformed_graph_def = TransformGraph(sess.graph.as_graph_def(), [],
                                           pred_node_names, transforms)
    constant_graph = graph_util.convert_variables_to_constants(
        sess, transformed_graph_def, pred_node_names)
else:
    constant_graph = graph_util.convert_variables_to_constants(
        sess, sess.graph.as_graph_def(), pred_node_names)
graph_io.write_graph(constant_graph,
                     output_fld,
                     args.output_model_file,
                     as_text=False)
print('saved the freezed graph (ready for inference) at: ',
      str(Path(output_fld) / args.output_model_file))
graph = ksess.graph
kgraph = graph.as_graph_def()
print(kgraph)

import os
num_output = 1
prefix = "output"
pred = [None] * num_output
outputName = [None] * num_output
for i in range(num_output):
    outputName[i] = prefix + str(i)
    pred[i] = tf.identity(model.get_output_at(i), name=outputName[i])
print('output name: ', outputName)

# convert variables in the model graph to constants
constant_graph = graph_util.convert_variables_to_constants(
    ksess, ksess.graph.as_graph_def(), outputName)

# save the model in .pb and .txt
output_dir = "./"
output_graph_name = args.out_prefix + ".pb"
output_text_name = args.out_prefix + ".txt"
graph_io.write_graph(constant_graph,
                     output_dir,
                     output_graph_name,
                     as_text=False)
graph_io.write_graph(constant_graph,
                     output_dir,
                     output_text_name,
                     as_text=True)
print('saved graph .pb at: {0}\nsaved graph .txt at: {1}'.format(
    os.path.join(output_dir, output_graph_name),
Exemple #37
0
def backward():
    yolo = YOLO(config.class_num,
                config.anchors,
                width=config.width,
                height=config.height)
    data = Data(config.train_file,
                config.class_num,
                config.batch_size,
                config.anchors,
                config.data_augment,
                width=config.width,
                height=config.height,
                data_debug=config.data_debug)

    inputs = tf.compat.v1.placeholder(dtype=tf.float32,
                                      shape=[config.batch_size, None, None, 3])
    y1_true = tf.compat.v1.placeholder(
        dtype=tf.float32,
        shape=[config.batch_size, None, None, 3, 4 + 1 + config.class_num])
    y2_true = tf.compat.v1.placeholder(
        dtype=tf.float32,
        shape=[config.batch_size, None, None, 3, 4 + 1 + config.class_num])
    y3_true = tf.compat.v1.placeholder(
        dtype=tf.float32,
        shape=[config.batch_size, None, None, 3, 4 + 1 + config.class_num])

    feature_y1, feature_y2, feature_y3 = yolo.forward(
        inputs, weight_decay=config.weight_decay, isTrain=True)

    global_step = tf.Variable(0, trainable=False)

    # 损失 yolov4
    loss = yolo.get_loss_v4(feature_y1, feature_y2, feature_y3, y1_true,
                            y2_true, y3_true, config.cls_normalizer,
                            config.ignore_thresh, config.prob_thresh,
                            config.score_thresh)
    l2_loss = tf.compat.v1.losses.get_regularization_loss()

    epoch = compute_curr_epoch(global_step, config.batch_size,
                               len(data.imgs_path))
    lr = config_lr(config.lr_type, config.lr_init, epoch)
    optimizer = config_optimizer(config.optimizer_type, lr, config.momentum)

    update_ops = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(update_ops):
        gvs = optimizer.compute_gradients(loss + l2_loss)
        clip_grad_var = [
            gv if gv[0] is None else [tf.clip_by_norm(gv[0], 100.), gv[1]]
            for gv in gvs
        ]
        train_step = optimizer.apply_gradients(clip_grad_var,
                                               global_step=global_step)

    # 初始化
    init = tf.compat.v1.global_variables_initializer()

    saver = tf.compat.v1.train.Saver()
    with tf.compat.v1.Session() as sess:
        sess.run(init)
        step = 0

        ckpt = tf.compat.v1.train.get_checkpoint_state(config.model_path)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
            step = eval(step)
            Log.add_log("message:存在 ckpt 模型, global_step=" + str(step))
        else:
            Log.add_log("message:不存在 ckpt 模型")

        # 一共迭代这么多次
        total_steps = np.ceil(config.total_epoch * len(data.imgs_path) /
                              config.batch_size)
        while step < total_steps:
            start = time.perf_counter()
            batch_img, y1, y2, y3 = next(data)
            _, loss_, step, lr_ = sess.run([train_step, loss, global_step, lr],
                                           feed_dict={
                                               inputs: batch_img,
                                               y1_true: y1,
                                               y2_true: y2,
                                               y3_true: y3
                                           })
            end = time.perf_counter()
            print(
                "step: %6d, loss: %.5g\t, w: %3d, h: %3d, lr:%.5g\t, time: %5f s"
                % (step, loss_, data.width, data.height, lr_, end - start))

            if (loss_ > 1e3) and (step > 1e3):
                Log.add_log("error:loss exception, loss_value = " + str(loss_))
                ''' break the process or lower learning rate '''
                raise ValueError("error:loss exception, loss_value = " +
                                 str(loss_) +
                                 ", please lower your learning rate")
                # lr = tf.math.maximum(tf.math.divide(lr, 10), config.lr_lower)

            if step % 5 == 2:
                Log.add_loss(str(step) + "\t" + str(loss_))

            if (step + 1) % config.save_step == 0:
                # save ckpt model
                if config.save_ckpt_model:
                    Log.add_log("message: save ckpt model, step=" + str(step) +
                                ", lr=" + str(lr_))
                    saver.save(sess,
                               path.join(config.model_path, config.model_name),
                               global_step=step)
                if config.save_pb_model:
                    Log.add_log("message: save pb model, step=" + str(step))
                    pb_model_name = path.join(
                        config.model_path,
                        config.model_name) + '-' + str(step) + ".pb"
                    constant_graph = graph_util.convert_variables_to_constants(
                        sess, sess.graph_def, [
                            'yolo/Conv_1/BiasAdd', 'yolo/Conv_9/BiasAdd',
                            'yolo/Conv_17/BiasAdd'
                        ])
                    # save  PB model
                    with tf.gfile.FastGFile(pb_model_name, mode='wb') as f:
                        f.write(constant_graph.SerializeToString())

        # save ckpt model
        if config.save_ckpt_model:
            Log.add_log("message:save final ckpt model, step=" + str(step))
            saver.save(sess,
                       path.join(config.model_path, config.model_name),
                       global_step=step)

        # save pb model
        if config.save_pb_model:
            Log.add_log("message: save final pb model, step=" + str(step))
            pb_model_name = path.join(
                config.model_path, config.model_name) + '-' + str(step) + ".pb"
            constant_graph = graph_util.convert_variables_to_constants(
                sess, sess.graph_def, [
                    'yolo/Conv_1/BiasAdd', 'yolo/Conv_9/BiasAdd',
                    'yolo/Conv_17/BiasAdd'
                ])
            # save  PB model
            with tf.gfile.FastGFile(pb_model_name, mode='wb') as f:
                f.write(constant_graph.SerializeToString())
    return 0
Exemple #38
0
def train():
    input_placeholder = tf.placeholder(
        tf.float32,
        shape=[None, input_height, input_width, channals],
        name="inputs")
    labels_placeholder = tf.placeholder(tf.int32, shape=[None], name="labels")
    is_training_placeholder = tf.placeholder_with_default(False,
                                                          shape=(),
                                                          name="is_training")
    keep_prob_placeholder = tf.placeholder_with_default(1.0,
                                                        shape=(),
                                                        name="keep_prob")

    classModel = SqueezeNet_Digit.SqueezeNet_Digit(
        is_training=is_training_placeholder, num_classes=len(label_dict))
    preprocessed_inputs = classModel.preprocess(input_placeholder)
    logits = classModel.inference(preprocessed_inputs)

    softmax_output, classes = classModel.postprocess(logits)
    softmax_output_ = tf.identity(softmax_output, name="softmax_output")
    classes_ = tf.identity(classes, name="classes")

    loss = classModel.loss(logits, labels_placeholder)
    accuarcy = tf.reduce_mean(
        tf.cast(tf.equal(classes, labels_placeholder), tf.float32))

    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)

    global_step = tf.Variable(0, trainable=False)
    learning_rate = tf.train.exponential_decay(0.05, global_step, 100, 0.9)
    optimizer = tf.train.MomentumOptimizer(learning_rate, 0.9)

    #大坑1!!!!
    #注意要使用依赖,保证每次训练之前要更新batch norm的均值和方差,不然后面会导致训练时精度高,但是测试时精度极低
    with tf.control_dependencies([tf.group(*update_ops)]):
        train_step = optimizer.minimize(loss, global_step)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        # 不仅保存可训练参数,也要保存batch norm的均值和方差,这两个是不可训练的,所以需要我们手动保存
        var_list = tf.trainable_variables()
        if global_step is not None:
            var_list.append(global_step)
        g_list = tf.global_variables()  # 从全局变量中获得batch norm的缩放和偏差
        bn_moving_vars = [g for g in g_list if 'moving_mean' in g.name]
        bn_moving_vars += [g for g in g_list if 'moving_variance' in g.name]
        var_list += bn_moving_vars
        ckpt_saver = tf.train.Saver(var_list=var_list, max_to_keep=5)

        total_iters_num = 0
        for epoch_num in range(total_epoch_num):
            train_images_all, train_labels_all = get_images_path(train_dir)
            test_images_all, test_labels_all = get_images_path(test_dir)
            train_images_num = len(train_images_all)
            cur_epoch_iters_num = train_images_num // batch_size
            for iters_num in range(cur_epoch_iters_num):
                indices = range(train_images_num)[iters_num *
                                                  batch_size:(iters_num + 1) *
                                                  batch_size]
                #batch_image原始是uint8类型,但是可以传给tf.float32的placeholder
                train_batch_image, train_batch_labels = next_batch(
                    False, indices, train_images_all, train_labels_all)
                test_batch_image, test_batch_labels = next_batch(
                    True, None, test_images_all, test_labels_all)

                if train_batch_image is None or test_batch_image is None:
                    continue
                train_dict = {
                    input_placeholder: train_batch_image,
                    labels_placeholder: train_batch_labels,
                    is_training_placeholder: True,
                    keep_prob_placeholder: 0.5
                }
                test_dict = {
                    input_placeholder: test_batch_image,
                    labels_placeholder: test_batch_labels,
                    is_training_placeholder: True,
                    keep_prob_placeholder: 1.0
                }

                sess.run(train_step, feed_dict=train_dict)

                _loss, _accuary, _learning_rate = sess.run(
                    [loss, accuarcy, learning_rate], feed_dict=train_dict)
                _test_accuary = sess.run([accuarcy], feed_dict=test_dict)

                one_moving_meaning_show = "No mean or variance"
                if len(bn_moving_vars) > 0:
                    one_moving_meaning = sess.graph.get_tensor_by_name(
                        bn_moving_vars[0].name)
                    one_moving_meaning_show = np.mean(
                        one_moving_meaning.eval())
                # 大坑2!!!!
                #除了使用依赖更新batch norm的均值和方差,还要等待均值和方差warm up到稳定的程度才能算ok
                #这里可以使用某个均值张量的均值来查看是否已经稳定(即均值不会一直增长,而是在某个固定值附近震荡)
                #同时为了加速warm up,可以设定batch norm的decay=0.95(默认0.999)
                print("one_moving_meaning:", one_moving_meaning_show)

                total_iters_num += 1
                if total_iters_num % snapshot == 0:
                    #保存PB
                    constant_graph = graph_util.convert_variables_to_constants(
                        sess, sess.graph_def, ["softmax_output"])
                    with tf.gfile.FastGFile(pb_path + model_name + "-" +
                                            str(total_iters_num) + ".pb",
                                            mode="wb") as fw:
                        fw.write(constant_graph.SerializeToString())
                    #保存CKPT
                    ckpt_saver.save(sess,
                                    ckpt_path + model_name + ".ckpt",
                                    global_step=total_iters_num)
                show_text="epoch:{},epoch-iters:{},total-iters:{},loss:{},accuarcy:{},lr:{},val:{}".format\
                             (epoch_num,iters_num+1,total_iters_num,_loss,_accuary,_learning_rate,_test_accuary)
                print(show_text)
Exemple #39
0
        saver2.restore(sess, options.ckpt)
    for i in range(10000):
        batch_xs, batch_ys = datasets.next_batch(100)
        sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
        if (i % 100 == 0):
            train_accuracy = accuracy.eval(feed_dict={
                x: batch_xs,
                y_: batch_ys
            })
            print("step %d, training accuracy %g" % (i, train_accuracy))
            saver.save(sess, checkpoint_dir + '/model.ckpt', global_step=i + 1)
    return x, y, y_


sess = tf.InteractiveSession()

try:
    x, y, y_ = nets[options.model]()
except KeyError:
    x, y, y_ = loadpb(options.model)

train(x, y, y_)

if (options.freeze):
    print('export frozen graph %s.pb', options.model)
    from tensorflow.python.framework import graph_util
    constant_graph = graph_util.convert_variables_to_constants(
        sess, sess.graph_def, ['y'])
    with tf.gfile.FastGFile('./%s.pb' % (options.model), mode='wb') as f:
        f.write(constant_graph.SerializeToString())
def keras_to_opencv(
    model_path
):  # Made by Christian, works for OpenCV 4.1. For OpenCV < 4, frozen model seems to work
    from tensorflow.python.framework import graph_util
    from tensorflow.python.framework import graph_io
    from tensorflow.python.tools.optimize_for_inference_lib import optimize_for_inference
    """
    Prepares Keras dnn model to OpenCV model inference
    :param keras_weights: Path to Keras .h5 file with weights values generated from model.save_weights() 
    :param keras_structure: Path to Keras json file with net structure. Generated from model.to_json()
    :param output: Output path of Tensorflow optimized protobuf file that can be loaded with 
    cv2.dnn.readNetFromTensorflow() 
    :return: None  
    """
    # Sets the learning phase to a fixed value.
    K.set_learning_phase(0)
    net_model = load_model(model_path)
    output_folder = str(Path(model_path).parent)

    # # load keras model
    # with open(keras_structure, 'r') as json_file:
    #     loaded_model_json = json_file.read()  # read json file
    #     net_model = K.models.model_from_json(loaded_model_json)  # create model from json
    #     net_model.load_weights(keras_weights)  # load weights to model

    # Test if successful
    # assert net_model is not None

    # Find input and output node names
    input_nodes_names = [node.name[:-2] for node in net_model.inputs]
    output_nodes_names = [node.name[:-2] for node in net_model.outputs]

    # Freeze graph and convert variables to constants
    sess = K.get_session()
    saver = tf.train.Saver(tf.global_variables())
    constant_graph = graph_util.convert_variables_to_constants(
        sess, sess.graph.as_graph_def(), output_nodes_names)
    # save frozen graph
    frozen_graph_name = "frozen_graph.pb"
    graph_io.write_graph(constant_graph,
                         output_folder,
                         frozen_graph_name,
                         as_text=False)
    print('Saved frozen graph at: ',
          os.path.join(output_folder, frozen_graph_name))

    # Optimize frozen graph for model inference
    # load frozen graph
    input_graph_def = tf.GraphDef()
    with tf.gfile.FastGFile(os.path.join(output_folder, frozen_graph_name),
                            "rb") as f:
        data = f.read()
        input_graph_def.ParseFromString(data)

    # Optimize graph
    output_graph_def = optimize_for_inference(input_graph_def,
                                              input_nodes_names,
                                              output_nodes_names,
                                              tf.float32.as_datatype_enum)

    # Save inference optimized protobuf file
    # save graph to output file
    optimized_graph_name = "opencv_optimized.pb"
    f = tf.gfile.FastGFile(os.path.join(output_folder, optimized_graph_name),
                           "w")
    f.write(output_graph_def.SerializeToString())
    print('Saved optimized graph (ready for inference) at: ',
          os.path.join(output_folder, optimized_graph_name))
Exemple #41
0
 def save_pb(self, sess, path):
     constant_graph = graph_util.convert_variables_to_constants(
         sess, sess.graph_def, ["output/predictions"])
     with tf.gfile.FastGFile(path, mode='wb') as f:
         f.write(constant_graph.SerializeToString())
         print("Saved!")
Exemple #42
0
def freeze_graph_with_def_protos(input_graph_def,
                                 input_saver_def,
                                 input_checkpoint,
                                 output_node_names,
                                 restore_op_name,
                                 filename_tensor_name,
                                 clear_devices,
                                 initializer_nodes,
                                 variable_names_blacklist=''):
    """Converts all variables in a graph and checkpoint into constants."""
    del restore_op_name, filename_tensor_name  # Unused by updated loading code.

    # 'input_checkpoint' may be a prefix if we're using Saver V2 format
    if not saver_lib.checkpoint_exists(input_checkpoint):
        raise ValueError('Input checkpoint "' + input_checkpoint +
                         '" does not exist!')

    if not output_node_names:
        raise ValueError(
            'You must supply the name of a node to --output_node_names.')

    # Remove all the explicit device specifications for this node. This helps to
    # make the graph more portable.
    if clear_devices:
        for node in input_graph_def.node:
            node.device = ''

    with tf.Graph().as_default():
        tf.import_graph_def(input_graph_def, name='')
        config = tf.ConfigProto(graph_options=tf.GraphOptions())
        with session.Session(config=config) as sess:
            if input_saver_def:
                saver = saver_lib.Saver(saver_def=input_saver_def)
                saver.restore(sess, input_checkpoint)
            else:
                var_list = {}
                reader = pywrap_tensorflow.NewCheckpointReader(
                    input_checkpoint)
                var_to_shape_map = reader.get_variable_to_shape_map()
                for key in var_to_shape_map:
                    try:
                        tensor = sess.graph.get_tensor_by_name(key + ':0')
                    except KeyError:
                        # This tensor doesn't exist in the graph (for example it's
                        # 'global_step' or a similar housekeeping element) so skip it.
                        continue
                    var_list[key] = tensor
                saver = saver_lib.Saver(var_list=var_list)
                saver.restore(sess, input_checkpoint)
                if initializer_nodes:
                    sess.run(initializer_nodes)

            variable_names_blacklist = (variable_names_blacklist.split(',')
                                        if variable_names_blacklist else None)
            output_graph_def = graph_util.convert_variables_to_constants(
                sess,
                input_graph_def,
                output_node_names.split(','),
                variable_names_blacklist=variable_names_blacklist)

    return output_graph_def
Exemple #43
0
allClasses = to_categorical(kanjiTargets)
yTrain = allClasses[permutation[0:trainingSetStart]]
yTest = allClasses[permutation[trainingSetStart:numberOfKanji]]

model = Sequential()

model.add(Conv2D(64, kernel_size=3, activation='relu',
                 input_shape=(32, 32, 1)))
model.add(Conv2D(32, kernel_size=3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(counter, activation='softmax'))

model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
# model.compile(optimizer=Adam(lr=0.0001), loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])

model.fit(xTrain, yTrain, validation_data=(xTest, yTest), epochs=10)

# predictFirst = model.predict(xTest[:1])
# np.argmax(predictFirst)

output_node_names = 'dense_1/Softmax'
output_graph_def = graph_util.convert_variables_to_constants(
    K.get_session(),
    tf.get_default_graph().as_graph_def(), output_node_names.split(","))
model_file = "./saved_model_2.pb"
with tf.gfile.GFile(model_file, "wb") as f:
    f.write(output_graph_def.SerializeToString())
Exemple #44
0
def generatePB(pb_dest="model_mnist_bnn.pb"):
    gd = sess.graph.as_graph_def()
    gd2 = graph_util.convert_variables_to_constants(sess, gd, ['output'])
    with gfile.FastGFile(pb_dest, 'wb') as f:
        f.write(gd2.SerializeToString())
    print('pb saved')
		x_collection, y_collection  = shuffle_batch(trX, trY, batch_size)

		for x,y in zip(x_collection, y_collection)[:-1]:
			sess.run(model.optimizer, feed_dict={model.x: x, model.labels: y, model.dropout: dropout, model.is_train: True})
	
		#if step % 5 == 0:
		feed = {model.x: x, model.labels: y, model.dropout: dropout, model.is_train: True}
		loss, acc = sess.run([model.loss, model.acc], feed_dict=feed)

		feed = {model.x: test_x[:1000], model.labels: test_y[:1000], model.dropout: 0, model.is_train: False}
		val_acc = sess.run(model.acc, feed_dict=feed)
		print "Epoch %d/%d - loss: %f - acc: %f\tval_acc: %f" % (step+1, epochs, loss, acc, val_acc)
		
		if step % 100 == 20:	
			checkpoint_filepath='log/step-%d.ckpt' % step
			saver.save(sess,checkpoint_filepath)
			print 'checkpoint saved!'


		if step % 100 == 99 and False: 
			output_graph_def = graph_util.convert_variables_to_constants(sess, sess.graph_def, 
											output_node_names=["x", "y", 'dropout', 'logit','is_train','version'])
			with tf.gfile.FastGFile('./load_pb/%s_%d_TP%f_FP%f.pb' %(prefix,step,TP_mean_acc,FP_mean_acc), mode='wb') as f:
				f.write(output_graph_def.SerializeToString())

			print '\nmodel protobuf saved!\n'
			



Exemple #46
0
def train_network(graph, batch_size, num_epochs, pb_file_path):
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        epoch_delta = 2
        for epoch_index in range(num_epochs):
            for i in range(12):
                sess.run(
                    [graph['optimize']],
                    feed_dict={
                        graph['x']: np.reshape(x_train[i], (1, 224, 224, 3)),
                        graph['y']: ([[1, 0]] if y_train[i] == 0 else [[0, 1]])
                    })
            if epoch_index % epoch_delta == 0:
                total_batches_in_train_set = 0
                total_correct_times_in_train_set = 0
                total_cost_in_train_set = 0.
                for i in range(12):
                    return_correct_times_in_batch = sess.run(
                        graph['correct_times_in_batch'],
                        feed_dict={
                            graph['x']: np.reshape(x_train[i],
                                                   (1, 224, 224, 3)),
                            graph['y']:
                            ([[1, 0]] if y_train[i] == 0 else [[0, 1]])
                        })
                    mean_cost_in_batch = sess.run(
                        graph['cost'],
                        feed_dict={
                            graph['x']: np.reshape(x_train[i],
                                                   (1, 224, 224, 3)),
                            graph['y']:
                            ([[1, 0]] if y_train[i] == 0 else [[0, 1]])
                        })
                    total_batches_in_train_set += 1
                    total_correct_times_in_train_set += return_correct_times_in_batch
                    total_cost_in_train_set += (mean_cost_in_batch *
                                                batch_size)

                total_batches_in_test_set = 0
                total_correct_times_in_test_set = 0
                total_cost_in_test_set = 0.
                for i in range(3):
                    return_correct_times_in_batch = sess.run(
                        graph['correct_times_in_batch'],
                        feed_dict={
                            graph['x']: np.reshape(x_val[i], (1, 224, 224, 3)),
                            graph['y']:
                            ([[1, 0]] if y_val[i] == 0 else [[0, 1]])
                        })
                    mean_cost_in_batch = sess.run(
                        graph['cost'],
                        feed_dict={
                            graph['x']: np.reshape(x_val[i], (1, 224, 224, 3)),
                            graph['y']:
                            ([[1, 0]] if y_val[i] == 0 else [[0, 1]])
                        })
                    total_batches_in_test_set += 1
                    total_correct_times_in_test_set += return_correct_times_in_batch
                    total_cost_in_test_set += (mean_cost_in_batch * batch_size)

                acy_on_test = total_correct_times_in_test_set / float(
                    total_batches_in_test_set * batch_size)
                acy_on_train = total_correct_times_in_train_set / float(
                    total_batches_in_train_set * batch_size)
                print(
                    'Epoch - {:2d}, acy_on_test:{:6.2f}%({}/{}),loss_on_test:{:6.2f}, acy_on_train:{:6.2f}%({}/{}),loss_on_train:{:6.2f}'
                    .format(epoch_index, acy_on_test * 100.0,
                            total_correct_times_in_test_set,
                            total_batches_in_test_set * batch_size,
                            total_cost_in_test_set, acy_on_train * 100.0,
                            total_correct_times_in_train_set,
                            total_batches_in_train_set * batch_size,
                            total_cost_in_train_set))
            constant_graph = graph_util.convert_variables_to_constants(
                sess, sess.graph_def, ["output"])
            with tf.gfile.FastGFile(pb_file_path, mode='wb') as f:
                f.write(constant_graph.SerializeToString())
def save_graph_to_file(sess, output_file, final_tensor_name):
    output_graph_def = graph_util.convert_variables_to_constants(
        sess, sess.graph_def, [final_tensor_name])
    with gfile.FastGFile(output_file, "wb") as f:
        f.write(output_graph_def.SerializeToString())
import keras
from keras.models import load_model

from tensorflow.python.framework import graph_util

import tensorflow as tf
sess = tf.Session()

import keras.backend as K
K.set_learning_phase(0)   # avoid output keras_learning_phase placeholder :)
K.set_session(sess)


model = load_model("mnist_cnn.h5")
#print(model.input, model.output)

input_tensor = model.input.name.split(":")[0]
output_tensor = model.output.name.split(":")[0]

output_graph_def = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), [output_tensor])
tf.train.write_graph(output_graph_def, '../../model-zoo/tf/mnist', 'keras-tf-mnist.pb', False)

print "\nInput Tensor: ", input_tensor
print "Output Tensor:", output_tensor
Exemple #49
0
def create_inference_graph(input_graph_def,
                           outputs,
                           max_batch_size=1,
                           max_workspace_size_bytes=2 << 20,
                           precision_mode=TrtPrecisionMode.FP32,
                           minimum_segment_size=3,
                           is_dynamic_op=False,
                           maximum_cached_engines=1,
                           cached_engine_batches=None,
                           use_calibration=True,
                           input_saved_model_dir=None,
                           input_saved_model_tags=None,
                           output_saved_model_dir=None,
                           session_config=None):
    """Python wrapper for the TRT transformation.

  Args:
    input_graph_def: a GraphDef object containing a model to be transformed. If
      set to None, the graph will be read from the SavedModel loaded from
      input_saved_model_dir.
    outputs: list of tensors or node names for the model outputs. Only used when
      input_graph_def is not None.
    max_batch_size: max size for the input batch.
    max_workspace_size_bytes: the maximum GPU temporary memory which the TRT
      engine can use at execution time. This corresponds to the 'workspaceSize'
      parameter of nvinfer1::IBuilder::setMaxWorkspaceSize().
    precision_mode: one of TrtPrecisionMode.supported_precision_modes().
    minimum_segment_size: the minimum number of nodes required for a subgraph to
      be replaced by TRTEngineOp.
    is_dynamic_op: whether to generate dynamic TRT ops which will build the TRT
      network and engine at run time.
    maximum_cached_engines: max number of cached TRT engines in dynamic TRT ops.
      If the number of cached engines is already at max but none of them can
      serve the input, the TRTEngineOp will fall back to run the TF function
      based on which the TRTEngineOp is created.
    cached_engine_batches: a list of batch sizes used to create cached
      engines, only used when is_dynamic_op is True. The length of the list
      should be <= maximum_cached_engines, and the dynamic TRT op will
      use this list to determine the batch sizes of the cached engines, instead
      of making the decision on the fly. This is useful when we know the most
      common batch size(s) the application is going to generate.
    use_calibration: this argument is ignored if precision_mode is not INT8. If
      set to True, a calibration graph will be created to calibrate the missing
      ranges. The calibration graph must be converted to an inference graph
      using calib_graph_to_infer_graph() after running calibration. if set to
      False, quantization nodes will be expected for every tensor in the graph
      (exlcuding those which will be fused). If a range is missing, an error
      will occur. Please note that accuracy may be negatively affected if there
      is a mismatch between which tensors TRT quantizes and which tensors were
      trained with fake quantization.
    input_saved_model_dir: the directory to load the SavedModel which contains
      the input graph to transforms. Used only when input_graph_def is None.
    input_saved_model_tags: list of tags to load the SavedModel.
    output_saved_model_dir: if not None, construct a SavedModel using the
      returned GraphDef and save it to the specified directory. This option only
      works when the input graph is loaded from a SavedModel, i.e. when
      input_saved_model_dir is specified and input_graph_def is None.
    session_config: the ConfigProto used to create a Session. It's also used as
      a template to create a TRT-enabled ConfigProto for conversion. If not
      specified, a default ConfigProto will be used.

  Returns:
    A GraphDef transformed from input_graph_def (or the SavedModel graph def
    loaded from input_saved_model_dir, if input_graph_def is not present), where
    all TRT compatible subgraphs are replaced with TRTEngineOps, and a TF
    function is added for each of the subgraphs.

    If is_dynamic_op is True, each TRTEngineOp will contain a serialized
    subgraph GraphDef, which will be converted to a TRT engine at execution time
    and the TRT engine will be cached for future usage. A new TRT engine will be
    created each time when none of the cached engines match the input shapes. If
    it fails to execute the TRT engine or the number of cached engines reaches
    maximum_cached_engines, the op will fall back to call the corresponding TF
    function.

    If is_dynamic_op is False, each TRTEngineOp will contain a serialized TRT
    engine created from the corresponding subgraph. No more engines will be
    created on the fly, and the op will fall back to call the corresponding TF
    function when it fails to execute the engine.

  Raises:
    ValueError: if the combination of the parameters is invalid.
    RuntimeError: if the TensorRT library version is incompatible.
  """
    compiled_version = get_linked_tensorrt_version()
    loaded_version = get_loaded_tensorrt_version()
    version_mismatch = False
    if loaded_version[0] < compiled_version[0]:
        tf_logging.error(
            "TensorRT version mismatch. Tensorflow was compiled against " +
            "TensorRT %s but library loaded from environment is TensorRT %s" %
            (".".join([str(x) for x in compiled_version]),
             ".".join([str(x) for x in loaded_version])) +
            ". Please make sure that correct version of TensorRT " +
            "is available in the system and added to ldconfig or LD_LIBRARY_PATH"
        )
        raise RuntimeError("Incompatible TensorRT library version")
    for i in zip(loaded_version, compiled_version):
        if i[0] != i[1]:
            tf_logging.warn("TensorRT mismatch. Compiled against version " +
                            "%s, but loaded %s. Things may not work" %
                            (".".join([str(x) for x in compiled_version]),
                             ".".join([str(x) for x in loaded_version])))
            version_mismatch = True
            break
    if not version_mismatch:
        tf_logging.info("Running against TensorRT version %s" %
                        ".".join([str(x) for x in loaded_version]))

    if session_config is None:
        session_config = config_pb2.ConfigProto()

    if input_saved_model_tags is None:
        input_saved_model_tags = [tag_constants.SERVING]
    saved_model_loader = None
    grappler_meta_graph_def = None

    if input_graph_def is None:
        # Read from SavedModel and freeze the graph if necessary.
        if input_saved_model_dir is None:
            raise ValueError(
                "input_graph_def and input_saved_model_dir cannot be "
                "both None")
        with ops.Graph().as_default():
            with session.Session(config=session_config) as sess:
                saved_model_loader = loader_impl.SavedModelLoader(
                    input_saved_model_dir)
                input_meta_graph_def = saved_model_loader.load(
                    sess, input_saved_model_tags)
                output_node_names = set()

                def _gather_names(tensor_info):
                    """Get the node names from a TensorInfo."""
                    return set([
                        tensor_info[key].name.split(":")[0]
                        for key in tensor_info
                    ])

                # Get input and outputs from all SignatureDef.
                for key in input_meta_graph_def.signature_def:
                    signature_def = input_meta_graph_def.signature_def[key]
                    output_node_names.update(
                        _gather_names(signature_def.inputs))
                    output_node_names.update(
                        _gather_names(signature_def.outputs))

                # Freeze the variables in the SavedModel graph and copy the frozen
                # graph over.
                frozen_graph_def = graph_util.convert_variables_to_constants(
                    sess, sess.graph.as_graph_def(add_shapes=True),
                    list(output_node_names))
                grappler_meta_graph_def = meta_graph_pb2.MetaGraphDef()
                grappler_meta_graph_def.graph_def.CopyFrom(frozen_graph_def)

                # Copy the collections that are not variables.
                for key in input_meta_graph_def.collection_def:
                    # TODO(laigd): currently we use the collection key to filter out
                    # collections that depend on variable ops, but this may miss some
                    # other user-defined collections. A better way would be to use
                    # CollectionDef::NodeList for the filtering.
                    if key not in [
                            "variables", "local_variables", "model_variables",
                            "trainable_variables", "train_op",
                            "table_initializer"
                    ]:
                        grappler_meta_graph_def.collection_def[key].CopyFrom(
                            input_meta_graph_def.collection_def[key])

                # Copy other information.
                grappler_meta_graph_def.meta_info_def.CopyFrom(
                    input_meta_graph_def.meta_info_def)
                for key in input_meta_graph_def.signature_def:
                    grappler_meta_graph_def.signature_def[key].CopyFrom(
                        input_meta_graph_def.signature_def[key])
                # TODO(laigd): maybe add back AssetFileDef.
    else:
        if output_saved_model_dir is not None:
            raise ValueError("output_saved_model_dir cannot be set when "
                             "input_graph_def is set")
        # Create MetaGraphDef from input graph.
        graph = ops.Graph()
        with graph.as_default():
            importer.import_graph_def(input_graph_def, name="")
        grappler_meta_graph_def = saver.export_meta_graph(
            graph_def=graph.as_graph_def(add_shapes=True), graph=graph)
        if outputs:
            output_collection = meta_graph_pb2.CollectionDef()
            output_list = output_collection.node_list.value
            for i in outputs:
                if isinstance(i, ops.Tensor):
                    output_list.append(_to_bytes(i.name))
                else:
                    output_list.append(_to_bytes(i))
            # TODO(laigd): use another key as the outputs are really not train_op.
            grappler_meta_graph_def.collection_def["train_op"].CopyFrom(
                output_collection)

    # Create TRT-enabled ConfigProto.
    session_config_with_trt = config_pb2.ConfigProto()
    session_config_with_trt.CopyFrom(session_config)
    rewriter_config = None
    if (session_config_with_trt.HasField("graph_options") and
            session_config_with_trt.graph_options.HasField("rewrite_options")):
        rewriter_config = session_config_with_trt.graph_options.rewrite_options
    rewriter_config_with_trt = get_tensorrt_rewriter_config(
        rewriter_config, max_batch_size, max_workspace_size_bytes,
        precision_mode, minimum_segment_size, is_dynamic_op,
        maximum_cached_engines, cached_engine_batches, use_calibration)
    session_config_with_trt.graph_options.rewrite_options.CopyFrom(
        rewriter_config_with_trt)

    # Run Grappler.
    transformed_graph_def = tf_optimizer.OptimizeGraph(session_config_with_trt,
                                                       grappler_meta_graph_def,
                                                       graph_id=b"tf_graph")

    # Optionally write the transformed graphdef as SavedModel.
    if output_saved_model_dir is not None:
        saved_model_builder = builder.SavedModelBuilder(output_saved_model_dir)
        with ops.Graph().as_default():
            importer.import_graph_def(transformed_graph_def, name="")
            # We don't use TRT here.
            with session.Session(config=session_config) as sess:
                saved_model_builder.add_meta_graph_and_variables(
                    sess,
                    input_saved_model_tags,
                    signature_def_map=grappler_meta_graph_def.signature_def)
        # Ignore other meta graphs from the input SavedModel.
        saved_model_builder.save()

    return transformed_graph_def
"""
import tensorflow as tf
from tensorflow.python.framework.graph_util import convert_variables_to_constants
import os

a = tf.Variable([[1], [2]], dtype=tf.float32, name='a')
b = tf.Variable(3, dtype=tf.float32, name='b')

output = tf.add(a, b, name='out')  # Tensor must have a name

graph_dir = "./graph_dir"
if not os.path.exists(graph_dir):
    os.makedirs(graph_dir)

# Save graph file
with tf.Session() as sess:
    tf.global_variables_initializer().run()

    # Convert Variable to constant, "out" is the name of the tensor
    graph = convert_variables_to_constants(sess, sess.graph_def, ["out"])
    tf.train.write_graph(graph, graph_dir, 'graph.pb', as_text=False)

# Restore graph file
with tf.Session() as sess:
    # with open(os.path.join(graph_dir,'graph.pb'), 'rb') as f:
    with tf.gfile.FastGFile(os.path.join(graph_dir, 'graph.pb'), 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        sess.graph.as_default()
        output = tf.import_graph_def(graph_def, return_elements=['out:0'])
        print(sess.run(output))
Exemple #51
0
    print("Inputs:", input_nodes_names)
    print("Outputs:", output_nodes_names)
    print("")

    # get sesssion
    sess = keras.backend.get_session()

    output_folder = create_output_folder(ARGS.output_dir)

    saver = tf.train.Saver(tf.global_variables())

    if ARGS.ascii:
        write_graph_def_in_ascii(sess, output_folder)

    # freeze graph: trasnform variable placeholders into constants
    constant_graph = graph_util.convert_variables_to_constants(
        sess, sess.graph.as_graph_def(), output_nodes_names)
    # save frozen graph
    frozen_graph_name = "frozen_" + ARGS.output_name
    graph_io.write_graph(constant_graph,
                         output_folder,
                         frozen_graph_name,
                         as_text=False)
    print('Saved frozen graph at: ', osp.join(output_folder,
                                              frozen_graph_name))

    # Optimize for inference

    # load frozen graph
    input_graph_def = tf.GraphDef()
    with tf.gfile.Open(os.path.join(output_folder, frozen_graph_name),
                       "rb") as f:
Exemple #52
0
    def train(self, output_model_path):
        settings = self.settings
        image_dir = self.scaffold_path + '/images'
        bottleneck_dir = self.scaffold_path + '/bottlenecks'

        sess = tf.Session()

        final_tensor_name = 'retrained_layer'
        (train_step, cross_entropy, bottleneck_input, ground_truth_input,
         final_tensor) = add_final_training_ops(len(self.image_lists.keys()),
                                                final_tensor_name,
                                                self.bottleneck_tensor,
                                                settings['learning_rate'])

        # Set up all our weights to their initial default values.
        init = tf.variables_initializer(tf.global_variables())
        sess.run(init)

        # Create the operations we need to evaluate the accuracy of our new layer.
        evaluation_step = add_evaluation_step(final_tensor, ground_truth_input)
        # Run the training for as many cycles as requested on the command line.
        for i in range(settings['num_steps']):
            # Get a catch of input bottleneck values, either calculated fresh every time
            # with distortions applied, or from the cache stored on disk.
            if self.do_distort_images:
                train_bottlenecks, train_ground_truth = get_random_distorted_bottlenecks(
                    sess, self.image_lists, settings['train_batch_size'],
                    'training', image_dir, self.distorted_jpeg_data_tensor,
                    self.distorted_image_tensor, self.resized_image_tensor,
                    self.bottleneck_tensor)
            else:
                train_bottlenecks, train_ground_truth = get_random_cached_bottlenecks(
                    sess, self.image_lists, settings['train_batch_size'],
                    'training', bottleneck_dir, image_dir,
                    self.jpeg_data_tensor, self.bottleneck_tensor)
            # Feed the bottlenecks and ground truth into the graph, and run a training
            # step.
            sess.run(train_step,
                     feed_dict={
                         bottleneck_input: train_bottlenecks,
                         ground_truth_input: train_ground_truth
                     })
            # Every so often, print out how well the graph is training.
            is_last_step = (i + 1 == settings['num_steps'])
            if (i % settings['eval_step_interval']) == 0 or is_last_step:
                train_accuracy, cross_entropy_value = sess.run(
                    [evaluation_step, cross_entropy],
                    feed_dict={
                        bottleneck_input: train_bottlenecks,
                        ground_truth_input: train_ground_truth
                    })
                logger.debug('%s: Step %d: Train accuracy = %.1f%%' %
                             (datetime.now(), i, train_accuracy * 100))
                logger.debug('%s: Step %d: Cross entropy = %f' %
                             (datetime.now(), i, cross_entropy_value))

            validation_bottlenecks, validation_ground_truth = (
                get_random_cached_bottlenecks(
                    sess, self.image_lists, settings['validation_batch_size'],
                    'validation', bottleneck_dir, image_dir,
                    self.jpeg_data_tensor, self.bottleneck_tensor))
            validation_accuracy = sess.run(evaluation_step,
                                           feed_dict={
                                               bottleneck_input:
                                               validation_bottlenecks,
                                               ground_truth_input:
                                               validation_ground_truth
                                           })
            logger.debug('%s: Step %d: Validation accuracy = %.1f%%' %
                         (datetime.now(), i, validation_accuracy * 100))

        # We've completed all our training, so run a final test evaluation on
        # some new images we haven't used before.
        test_bottlenecks, test_ground_truth = get_random_cached_bottlenecks(
            sess, self.image_lists, settings['test_batch_size'], 'testing',
            bottleneck_dir, image_dir, self.jpeg_data_tensor,
            self.bottleneck_tensor)
        test_accuracy = sess.run(evaluation_step,
                                 feed_dict={
                                     bottleneck_input: test_bottlenecks,
                                     ground_truth_input: test_ground_truth
                                 })
        logger.info('Model trained, final test accuracy = %.1f%%' %
                    (test_accuracy * 100))

        benchmark_info = {
            'validation_accuracy': float(validation_accuracy),
            'train_accuracy': float(train_accuracy),
            'test_accuracy': float(test_accuracy),
            'cross_entropy_value': float(cross_entropy_value)
        }

        create_empty_model(output_model_path)
        transfer_model_meta(self.scaffold_path, output_model_path)
        output_graph_path = output_model_path + '/state/model.pb'

        # Write out the trained graph and labels with the weights stored as constants.
        output_graph_def = graph_util.convert_variables_to_constants(
            sess, sess.graph.as_graph_def(), [final_tensor_name])
        with gfile.FastGFile(output_graph_path, 'wb') as f:
            f.write(output_graph_def.SerializeToString())

        # Persist labels with softmax IDs
        with open(output_model_path + '/labels.json', 'w') as f:
            json.dump({'labels': self.labels.values()}, f)

        # Cleanup
        tf.reset_default_graph()
        sess.close()

        return benchmark_info
Exemple #53
0
def freeze_graph_with_def_protos(input_graph_def,
                                 input_saver_def,
                                 input_checkpoint,
                                 output_node_names,
                                 restore_op_name,
                                 filename_tensor_name,
                                 output_graph,
                                 clear_devices,
                                 initializer_nodes,
                                 variable_names_whitelist="",
                                 variable_names_blacklist="",
                                 input_meta_graph_def=None,
                                 input_saved_model_dir=None,
                                 saved_model_tags=None,
                                 checkpoint_version=saver_pb2.SaverDef.V2):
    """Converts all variables in a graph and checkpoint into constants.

  Args:
    input_graph_def: A `GraphDef`.
    input_saver_def: A `SaverDef` (optional).
    input_checkpoint: The prefix of a V1 or V2 checkpoint, with V2 taking
      priority.  Typically the result of `Saver.save()` or that of
      `tf.train.latest_checkpoint()`, regardless of sharded/non-sharded or
      V1/V2.
    output_node_names: The name(s) of the output nodes, comma separated.
    restore_op_name: Unused.
    filename_tensor_name: Unused.
    output_graph: String where to write the frozen `GraphDef`.
    clear_devices: A Bool whether to remove device specifications.
    initializer_nodes: Comma separated string of initializer nodes to run before
                       freezing.
    variable_names_whitelist: The set of variable names to convert (optional, by
                              default, all variables are converted).
    variable_names_blacklist: The set of variable names to omit converting
                              to constants (optional).
    input_meta_graph_def: A `MetaGraphDef` (optional),
    input_saved_model_dir: Path to the dir with TensorFlow 'SavedModel' file
                           and variables (optional).
    saved_model_tags: Group of comma separated tag(s) of the MetaGraphDef to
                      load, in string format (optional).
    checkpoint_version: Tensorflow variable file format (saver_pb2.SaverDef.V1
                        or saver_pb2.SaverDef.V2)

  Returns:
    Location of the output_graph_def.
  """
    del restore_op_name, filename_tensor_name  # Unused by updated loading code.

    # 'input_checkpoint' may be a prefix if we're using Saver V2 format
    if (not input_saved_model_dir
            and not checkpoint_management.checkpoint_exists(input_checkpoint)):
        raise ValueError("Input checkpoint '" + input_checkpoint +
                         "' doesn't exist!")

    if not output_node_names:
        raise ValueError(
            "You need to supply the name of a node to --output_node_names.")

    # Remove all the explicit device specifications for this node. This helps to
    # make the graph more portable.
    if clear_devices:
        if input_meta_graph_def:
            for node in input_meta_graph_def.graph_def.node:
                node.device = ""
        elif input_graph_def:
            for node in input_graph_def.node:
                node.device = ""

    if input_graph_def:
        _ = importer.import_graph_def(input_graph_def, name="")
    with session.Session() as sess:
        if input_saver_def:
            saver = saver_lib.Saver(saver_def=input_saver_def,
                                    write_version=checkpoint_version)
            saver.restore(sess, input_checkpoint)
        elif input_meta_graph_def:
            restorer = saver_lib.import_meta_graph(input_meta_graph_def,
                                                   clear_devices=True)
            restorer.restore(sess, input_checkpoint)
            if initializer_nodes:
                sess.run(initializer_nodes.replace(" ", "").split(","))
        elif input_saved_model_dir:
            if saved_model_tags is None:
                saved_model_tags = []
            loader.load(sess, saved_model_tags, input_saved_model_dir)
        else:
            var_list = {}
            reader = py_checkpoint_reader.NewCheckpointReader(input_checkpoint)
            var_to_shape_map = reader.get_variable_to_shape_map()

            # List of all partition variables. Because the condition is heuristic
            # based, the list could include false positives.
            all_partition_variable_names = [
                tensor.name.split(":")[0]
                for op in sess.graph.get_operations()
                for tensor in op.values()
                if re.search(r"/part_\d+/", tensor.name)
            ]
            has_partition_var = False

            for key in var_to_shape_map:
                try:
                    tensor = sess.graph.get_tensor_by_name(key + ":0")
                    if any(key in name
                           for name in all_partition_variable_names):
                        has_partition_var = True
                except KeyError:
                    # This tensor doesn't exist in the graph (for example it's
                    # 'global_step' or a similar housekeeping element) so skip it.
                    continue
                var_list[key] = tensor

            try:
                saver = saver_lib.Saver(var_list=var_list,
                                        write_version=checkpoint_version)
            except TypeError as e:
                # `var_list` is required to be a map of variable names to Variable
                # tensors. Partition variables are Identity tensors that cannot be
                # handled by Saver.
                if has_partition_var:
                    raise ValueError(
                        "Models containing partition variables cannot be converted "
                        "from checkpoint files. Please pass in a SavedModel using "
                        "the flag --input_saved_model_dir.")
                # Models that have been frozen previously do not contain Variables.
                elif _has_no_variables(sess):
                    raise ValueError(
                        "No variables were found in this model. It is likely the model "
                        "was frozen previously. You cannot freeze a graph twice."
                    )
                    return 0
                else:
                    raise e

            saver.restore(sess, input_checkpoint)
            if initializer_nodes:
                sess.run(initializer_nodes.replace(" ", "").split(","))

        variable_names_whitelist = (variable_names_whitelist.replace(
            " ", "").split(",") if variable_names_whitelist else None)
        variable_names_blacklist = (variable_names_blacklist.replace(
            " ", "").split(",") if variable_names_blacklist else None)

        if input_meta_graph_def:
            output_graph_def = graph_util.convert_variables_to_constants(
                sess,
                input_meta_graph_def.graph_def,
                output_node_names.replace(" ", "").split(","),
                variable_names_whitelist=variable_names_whitelist,
                variable_names_blacklist=variable_names_blacklist)
        else:
            output_graph_def = graph_util.convert_variables_to_constants(
                sess,
                input_graph_def,
                output_node_names.replace(" ", "").split(","),
                variable_names_whitelist=variable_names_whitelist,
                variable_names_blacklist=variable_names_blacklist)

    # Write GraphDef to file if output path has been given.
    if output_graph:
        with gfile.GFile(output_graph, "wb") as f:
            f.write(output_graph_def.SerializeToString())

    return output_graph_def
def save_graph_to_file(sess, graph, graph_file_name, final_tensor_name):
    output_graph_def = graph_util.convert_variables_to_constants(
        sess, graph.as_graph_def(), [final_tensor_name])
    with gfile.FastGFile(graph_file_name, 'wb') as f:
        f.write(output_graph_def.SerializeToString())
    return
parser.add_argument('--meta',
                    required=True,
                    type=str,
                    help='input model checkpoint meta data file (.meta)')
parser.add_argument('--prefix',
                    required=True,
                    type=str,
                    help='input model data prefix')
FLAGS, unparsed = parser.parse_known_args()

output_node_names = "y_pred"
#saver = tf.train.import_meta_graph('model.ckpt-74928.meta', clear_devices=True)
saver = tf.train.import_meta_graph(FLAGS.meta, clear_devices=True)

graph = tf.get_default_graph()
input_graph_def = graph.as_graph_def()
sess = tf.Session()
#saver.restore(sess, "./model.ckpt-74928")
saver.restore(sess, FLAGS.prefix)
output_graph_def = graph_util.convert_variables_to_constants(
    sess,  # The session is used to retrieve the weights
    input_graph_def,  # The graph_def is used to retrieve the nodes 
    output_node_names.split(
        ",")  # The output node names are used to select the usefull nodes
)
output_graph = "estate_model.pb"
with tf.gfile.GFile(output_graph, "wb") as f:
    f.write(output_graph_def.SerializeToString())

sess.close()
import tensorflow as tf
from tensorflow.python.framework import graph_util

v1 = tf.Variable(tf.constant(1.0, shape=[1]), name="v1")
v2 = tf.Variable(tf.constant(2.0, shape=[1]), name="v2")
result = v1 + v2

init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    # 导出当前计算图的GraphDef部分, 只需要这一部分就可以完成从
    # 输入层到输出层的计算过程.
    graph_def = tf.get_default_graph().as_graph_def()

    # 将图中的变量及其取值转化为常量, 同时将图中不必要的节点去掉.
    # 在之前的demo中, 一些初始化的操作也会被保存,.
    # 在下面一行代码中, 最后一个参数['add']给除了需要保存的节点的名称,
    # 注意这里给出的是计算节点的名称, 所以没有add后面的0
    output_graph_def = graph_util.convert_variables_to_constants(
        sess, graph_def, ['add'])

    # 将导出的模型存入文件
    with tf.gfile.GFile("/home/ma/tfmodels/combined_model.pb", "wb") as f:
        f.write(output_graph_def.SerializeToString())
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)
    #Path(output_model).parent.mkdir(parents=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)

    # TODO(amirabdi): Support networks with multiple inputs
    orig_output_node_names = [node.op.name for node in model.outputs]
    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))
Exemple #58
0
    def train(self, train_data, test_data, test_label, GPU_ratio=0.2, epochs=50, batch_size=16, fine_tune=False,
              save_ckpt=True):

        #             self.GPU_ratio = GPU_ratio
        #             self.epochs = epochs
        #             self.batch_size = batch_size

        # 計算total batch
        total_batches = train_data.shape[0] // batch_size
        if train_data.shape[0] % batch_size:
            total_batches += 1

        # 設定GPU參數
        config = tf.ConfigProto(log_device_placement=True,
                                allow_soft_placement=True,  # 允許當找不到設備時自動轉換成有支援的設備
                                )
        config.gpu_options.per_process_gpu_memory_fraction = GPU_ratio

        with tf.Session(config=config) as sess:

            if fine_tune is True:  # 使用已經訓練好的權重繼續訓練
                files = [file.path for file in os.scandir(self.save_path) if file.is_file()]

                if not files:  # 沒有任何之前的權重

                    sess.run(tf.global_variables_initializer())

                    print('no previous model param can be used')

                else:

                    self.saver.restore(sess, tf.train.latest_checkpoint(self.save_path))

                    print('use previous model param')

            else:
                sess.run(tf.global_variables_initializer())
                print('no previous model param can be used')

            for epoch in range(epochs):

                for index in range(total_batches):

                    num_start = index * batch_size
                    num_end = num_start + batch_size

                    if num_end >= train_data.shape[0] and batch_size > 1:
                        num_end = train_data.shape[0] - 1

                    sess.run(self.optimizer, feed_dict={self.input_x: train_data[num_start:num_end]})

                # compute mean loss after a epoch
                train_loss = []
                for index in range(train_data.shape[0]):
                    single_loss = sess.run(self.loss, feed_dict={self.input_x: train_data[index:index + 1]})

                    train_loss.append(single_loss)

                train_loss = np.array(train_loss)
                train_loss = np.mean(train_loss)

                test_loss = []
                acc = 0
                for index in range(test_data.shape[0]):

                    single_loss = sess.run(self.loss, feed_dict={self.input_x: test_data[index:index + 1]})

                    test_loss.append(single_loss)

                    if single_loss > train_loss:

                        if test_label[index] == 0 or test_label[index] == 1:
                            acc += 1

                    elif single_loss >= 0:

                        if test_label[index] == 2:
                            acc += 1

                acc /= test_data.shape[0]
                test_loss = np.array(test_loss)
                test_loss = np.mean(test_loss)
                msg = "Epoch {}\ntrain set loss = {}".format(epoch, train_loss)
                print(msg)
                #self.UDP_send(msg,self.send_address)

                msg = "test set loss = {}, accuracy = {}".format(test_loss, acc)
                print(msg)
                #self.UDP_send(msg, self.send_address)

                # 紀錄資料:本次epoch ckpt檔
                if save_ckpt is True:
                    model_save_path = self.saver.save(sess, self.out_dir_prefix, global_step=epoch)

                    print('Save model checkpoint to ', model_save_path)

                graph = tf.get_default_graph().as_graph_def()
                output_graph_def = graph_util.convert_variables_to_constants(sess, graph,['output'])  # graph也可以直接填入sess.graph_def

                # 'model_saver/'為置放的資料夾,'combined_model.pb'為檔名
                with tf.gfile.GFile("model_saver/pb_test_model.pb", "wb") as f:

                    f.write(output_graph_def.SerializeToString())
Exemple #59
0
def create_mtcnn(sess, model_path):
    if not model_path:
        model_path,_ = os.path.split(os.path.realpath(__file__))

    with tf.variable_scope('pnet'):
        data_pnet = tf.placeholder(tf.float32, (None,None,None,3), 'input')
        pnet = PNet({'data':data_pnet})
        pnet.load(os.path.join(model_path, 'det1.npy'), sess)
            #jjia enable TRT
        print("Pnet TensorRT enabled")
        frozen_pnet_graph = convert_variables_to_constants(sess, sess.graph_def, ['pnet/conv4-2/BiasAdd','pnet/prob1'])
        trt_pnet_graph = trt.create_inference_graph(input_graph_def=frozen_pnet_graph,
        outputs=['pnet/conv4-2/BiasAdd:0','pnet/prob1:0'],
        max_batch_size = 1, 
        max_workspace_size_bytes=500000000, # 1GB mem assgined to TRT
        precision_mode="FP16",  # Precision "FP32","FP16" or "INT8"                                        
        minimum_segment_size=1
        )
        
    with tf.variable_scope('rnet'):
        data_rnet = tf.placeholder(tf.float32, (None,24,24,3), 'input')
        rnet = RNet({'data':data_rnet})
        rnet.load(os.path.join(model_path, 'det2.npy'), sess)
        #jjia enable TRT
        print("Rnet TensorRT enabled")
        frozen_rnet_graph = convert_variables_to_constants(sess, sess.graph_def, ['rnet/conv5-2/conv5-2','rnet/prob1'])
        trt_rnet_graph = trt.create_inference_graph(input_graph_def=frozen_rnet_graph,
        outputs=['rnet/conv5-2/conv5-2:0','rnet/prob1:0'],
        max_batch_size = 128, 
        max_workspace_size_bytes=500000000, # 500Mb mem assgined to TRT
        precision_mode="FP16",  # Precision "FP32","FP16" or "INT8"                                        
        minimum_segment_size=1
        )
        
    with tf.variable_scope('onet'):
        data_onet = tf.placeholder(tf.float32, (None,48,48,3), 'input')
        onet = ONet({'data':data_onet})
        onet.load(os.path.join(model_path, 'det3.npy'), sess)
        #jjia enable TRT
        print("Onet TensorRT enabled")
        frozen_onet_graph = convert_variables_to_constants(sess, sess.graph_def, ['onet/conv6-2/conv6-2', 'onet/conv6-3/conv6-3', 'onet/prob1'])
        trt_onet_graph = trt.create_inference_graph(input_graph_def=frozen_onet_graph,
        outputs=['onet/conv6-2/conv6-2:0', 'onet/conv6-3/conv6-3:0', 'onet/prob1:0'],
        max_batch_size = 128, 
        max_workspace_size_bytes=500000000, # 1GB mem assgined to TRT
        precision_mode="FP16",  # Precision "FP32","FP16" or "INT8"                                        
        minimum_segment_size=1
        )

    sess.close()
    #tf.reset_default_graph()
    with tf.Graph().as_default():
        sess = tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True), log_device_placement=False))        
        with sess.as_default():
            #for node in frozen_pnet_graph.node:
            #    print("[NODE] ",  node.name, node.op)
            tf.import_graph_def(trt_pnet_graph,input_map=None, name='') # import frozen_pnet_graph to disable trt
            tf.import_graph_def(trt_rnet_graph,input_map=None, name='') # import frozen_rnet_graph to disable trt
            tf.import_graph_def(trt_onet_graph,input_map=None, name='') # import frozen_onet_graph to disable trt
            pnet_fun = lambda img : sess.run(('pnet/conv4-2/BiasAdd:0', 'pnet/prob1:0'), feed_dict={'pnet/input:0':img})
            rnet_fun = lambda img : sess.run(('rnet/conv5-2/conv5-2:0', 'rnet/prob1:0'), feed_dict={'rnet/input:0':img})
            onet_fun = lambda img : sess.run(('onet/conv6-2/conv6-2:0', 'onet/conv6-3/conv6-3:0', 'onet/prob1:0'), feed_dict={'onet/input:0':img})

    return pnet_fun, rnet_fun, onet_fun
Exemple #60
0
def save_pb(checkpoint, output_file):
    """"""
    model_config = config.get('model_config')
    with tf.Graph().as_default():
        height = 384
        if config.get('height'):
            height = config.get('height')
        inputs = tf.placeholder(dtype=tf.float32,
                                shape=[None, height, 128, 3],
                                name='graph_input')
        network_name = model_config['name']
        del model_config['name']
        model = select_network(network_name)(**model_config)
        outputs = model.forward(inputs)
        if network_name in ['source']:
            dest_node = outputs[0]
            features = tf.nn.l2_normalize(tf.concat(dest_node, axis=-1),
                                          axis=-1,
                                          name='output_features')
            print('features: ', features.get_shape())
            out_names = [features.op.name]
        elif network_name in ['cacenet']:
            features = tf.nn.l2_normalize(outputs[1][0],
                                          axis=-1,
                                          name='output_features')
            out_names = [features.op.name]
        elif network_name in ['mgn']:
            dest_node = []
            global_softmax_branches, local_softmax_branches, _, _ = outputs
            for o in global_softmax_branches:
                dest_node.append(tf.nn.l2_normalize(o, axis=-1))
            for o in local_softmax_branches:
                o = tf.nn.l2_normalize(tf.concat(o, axis=-1), axis=-1)
                dest_node.append(o)
            features = tf.nn.l2_normalize(tf.concat(dest_node, axis=-1),
                                          axis=-1,
                                          name='output_features')
            print('features: ', features.get_shape())
            out_names = [features.op.name]
        # load_vars = tf.global_variables()
        load_vars = {}
        for v in tf.global_variables():
            v_name = v.op.name
            if 'features' in v_name:
                v_name = 'tower_0/' + v_name
            # if 'non_local_graph/x1/kernel' in v_name:
            #     continue
            load_vars[v_name] = v
        saver = tf.train.Saver(load_vars)

        with tf.Session() as sess:
            print('load checkpoint from %s' % checkpoint)
            sess.run(tf.global_variables_initializer())
            saver.restore(sess, checkpoint)

            cur_graphdef = sess.graph.as_graph_def()

            output_graphdef = graph_util.convert_variables_to_constants(
                sess, cur_graphdef, out_names)

            with tf.gfile.GFile(output_file, 'wb') as gf:
                gf.write(output_graphdef.SerializeToString())