jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def _init_pos_model(self, session):
        """Create POS Tagger model and initialize with random or load parameters in session."""
        # initilize config
        config_dict = load_config(self.model_config_path)
        config = get_config(config_dict, self.name)
        config.batch_size = 1
        config.num_steps = 1 # iterator one token per time
        model_var_scope = get_model_var_scope(self.var_scope, self.name)
        print ("NOTICE: Input POS Model Var Scope Name '%s'" % model_var_scope)
        # Check if self.model already exist
        if self.model is None:
            with tf.variable_scope(model_var_scope, tf.AUTO_REUSE):
                self.model = pos_model.POSTagger(is_training=False, config=config) # save object after is_training
        # Load Specific .data* ckpt file
        if len(glob.glob(self.ckpt_path + '.data*')) > 0: # file exist with pattern: 'pos.ckpt.data*'
            print("NOTICE: Loading model parameters from %s" % self.ckpt_path)
            all_vars = tf.global_variables()
            model_vars = [k for k in all_vars if model_var_scope in k.name.split("/")]
            tf.train.Saver(model_vars).restore(session, self.ckpt_path)
        else:
            print("NOTICE: Model not found, Try to run method: deepnlp.download(module='pos', name='%s')" % self.name)
            print("NOTICE: Created with fresh parameters.")
            session.run(tf.global_variables_initializer())
//  [python]
def _init_ner_model(self, session):
        """Create ner Tagger model and initialize or load parameters in session."""
        # initilize config
        config_dict = load_config(self.model_config_path)
        config = get_config(config_dict, self.name)
        if config is None:
            print ("WARNING: Input model name %s has no configuration..." % self.name)
        config.batch_size = 1
        config.num_steps = 1 # iterator one token per time
        model_var_scope = get_model_var_scope(self.var_scope, self.name)
        print ("NOTICE: Input NER Model Var Scope Name '%s'" % model_var_scope)
        # Check if self.model already exist
        if self.model is None:
            with tf.variable_scope(model_var_scope, reuse = tf.AUTO_REUSE):
                self.model = ner_model.NERTagger(is_training=True, config=config) # save object after is_training
        #else:   # Model Graph Def already exist
        #    print ("DEBUG: Model Def already exists")
        # update model parameters
        if len(glob.glob(self.ckpt_path + '.data*')) > 0: # file exist with pattern: 'ner.ckpt.data*'
            print("NOTICE: Loading model parameters from %s" % self.ckpt_path)
            all_vars = tf.global_variables()
            model_vars = [k for k in all_vars if model_var_scope in k.name.split("/")]   # e.g. ner_var_scope_zh
            tf.train.Saver(model_vars).restore(session, self.ckpt_path)
        else:
            print("NOTICE: Model not found, Try to run method: deepnlp.download(module='ner', name='%s')" % self.name)
            print("NOTICE: Created with fresh parameters.")
            session.run(tf.global_variables_initializer())
//  [python]
def initialize_uninitialized(sess = None):
    """
    Initialize unitialized variables, doesn't affect those already initialized
    :param sess: in which session to initialize stuff. Defaults to tf.get_default_session()
    """
    sess = sess or tf.get_default_session()
    global_vars          = tf.global_variables()
    is_not_initialized   = sess.run([tf.is_variable_initialized(var) for var in global_vars])
    not_initialized_vars = [v for (v, f) in zip(global_vars, is_not_initialized) if not f]

    if len(not_initialized_vars):
        sess.run(tf.variables_initializer(not_initialized_vars))
//  [python]
def restore(self, data_path, var_list=None):
        if data_path.endswith('.npy'):
            self.load_npy(data_path, self.sess)
        else:
            loader = tf.train.Saver(var_list=tf.all_variables())
            loader.restore(self.sess, data_path)
        
        print('Restore from {}'.format(data_path))
//  [python]
def restore(self, data_path, var_list=None):
        if data_path.endswith('.npy'):
            self.load_npy(data_path, self.sess)
        else:
            loader = tf.train.Saver(var_list=tf.global_variables())
            loader.restore(self.sess, data_path)
        
        print('Restore from {}'.format(data_path))
//  [python]
def operate(self):
        self.loss = tf.reduce_mean(tf.square(tf.reshape(self.pred, [-1]) - tf.reshape(self.Y, [-1])))
        self.optim = tf.train.AdamOptimizer(self.config.learning_rate).minimize(self.loss)
        self.saver = tf.train.Saver(tf.all_variables())
//  [python]
def operate(self):
        self.loss = tf.reduce_mean(tf.square(tf.reshape(self.pred, [-1]) - tf.reshape(self.Y, [-1])))
        self.optim = tf.train.AdamOptimizer(self.config.learning_rate).minimize(self.loss)
        self.saver = tf.train.Saver(tf.global_variables())
//  [python]
def get_variables(x, collection=None):
  """Get parent TensorFlow variables of input.

  Args:
    x: RandomVariable or tf.Tensor.
      Query node to find parents of.
    collection: list of tf.Variable.
      The collection of variables to check with respect to; defaults to
      all variables in the graph.

  Returns:
    list of tf.Variable.
    TensorFlow variables that x depends on.

  #### Examples

  
  a = tf.Variable(0.0)
  b = tf.Variable(0.0)
  c = Normal(a * b, 1.0)
  assert set(ed.get_variables(c)) == set([a, b])
  
  """
  if collection is None:
    collection = tf.global_variables()

  node_dict = {node.name: node for node in collection}

  # Traverse the graph. Add each node to the set if it's in the collection.
  output = set()
  visited = set()
  nodes = {x}
  while nodes:
    node = nodes.pop()

    if node in visited:
      continue
    visited.add(node)

    if isinstance(node, RandomVariable):
      node = node.value()

    candidate_node = node_dict.get(node.name, None)
    if candidate_node is not None and candidate_node != x:
      output.add(candidate_node)

    nodes.update(node.op.inputs)

  return list(output)
//  [python]
def get_variables(x, collection=None):
  """Get parent TensorFlow variables of input.

  Args:
    x: RandomVariable or tf.Tensor.
      Query node to find parents of.
    collection: list of tf.Variable.
      The collection of variables to check with respect to; defaults to
      all variables in the graph.

  Returns:
    list of tf.Variable.
    TensorFlow variables that x depends on.

  #### Examples

  ```python
  a = tf.Variable(0.0)
  b = tf.Variable(0.0)
  c = Normal(a * b, 1.0)
  assert set(ed.get_variables(c)) == set([a, b])
  ```
  """
  if collection is None:
    collection = tf.global_variables()

  node_dict = {node.name: node for node in collection}

  # Traverse the graph. Add each node to the set if it's in the collection.
  output = set()
  visited = set()
  nodes = {x}
  while nodes:
    node = nodes.pop()

    if node in visited:
      continue
    visited.add(node)

    if isinstance(node, RandomVariable):
      node = node.value()

    candidate_node = node_dict.get(node.name, None)
    if candidate_node is not None and candidate_node != x:
      output.add(candidate_node)

    nodes.update(node.op.inputs)

  return list(output)
//  [python]
def t_optimistic_restore(session, save_file):
    var_names = [var for var in tf.global_variables()
            if "InceptionV3" in var.name.split(':')[0]]
    saver = tf.train.Saver(var_names)
    saver.restore(session, save_file)
//  [python]
def __init__(self, vot=False, dis=False):
        self.vot = vot
        self.dis = dis

        config = tf.ConfigProto(allow_soft_placement=True)
        # config.gpu_options.per_process_gpu_memory_fraction = 0.5
        config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=config)

        self.V_image_op = tf.placeholder(tf.float32, (None, 128, 128, 3), name='V_input_image')
        self.V_feat_op = _image_to_feat(self.V_image_op, is_training=False, reuse=False)
        variables = tf.global_variables()  # list
        # Initialize all variables first
        self.sess.run(tf.variables_initializer(variables, name='init'))  # tf.variables_initializer
        restorer = tf.train.Saver(variables)
        restorer.restore(self.sess, pretrained_model)

        config_file = PROJECT_PATH + 'RPN/ssd_mobilenet_tracking.config'
        checkpoint_dir = PROJECT_PATH + 'RPN/dump'

        model_config, train_config, input_config, eval_config \
            = get_configs_from_pipeline_file(config_file)

        model_scope = 'model'
        model = build_man_model(model_config=model_config, is_training=False)

        self.initConstantOp = tf.placeholder(tf.float32, [1, 1, 1, 512])

        self.initFeatOp, self.initInputOp \
            = build_init_graph(model, model_scope, reuse=None)

        self.pre_box_tensor, self.scores_tensor, self.input_cur_image \
            = build_box_predictor(model, model_scope, self.initConstantOp, reuse=None)

        variables_to_restore = tf.global_variables()
        restore_model(self.sess, model_scope, checkpoint_dir, variables_to_restore, V_NET)

        with tf.keras.utils.CustomObjectScope({
            'relu6': tf.keras.layers.LeakyReLU(0),
            'DepthwiseConv2D': tf.keras.layers.DepthwiseConv2D}):
            self.branch_z = tf.keras.models.load_model(
                PROJECT_PATH + 'Skim/branch_z_3.h5',
                custom_objects={"tf": tf})
            self.branch_search = tf.keras.models.load_model(
                PROJECT_PATH + 'Skim/branch_search_3.h5',
                custom_objects={"tf": tf})
//  [python]
def _countModelParameters(self):
    """Counts the number of parameters in the model at top level scope."""
    counter = {}
    for v in tf.global_variables():
      name = v.op.name.split("/")[0]
      num_params = v.get_shape().num_elements()
      if not num_params:
        self.fail("Could not infer num_elements from Variable %s" % v.op.name)
      counter[name] = counter.get(name, 0) + num_params
    return counter
//  [python]
def restore_from_classification_checkpoint_fn(
      self,
      first_stage_feature_extractor_scope,
      second_stage_feature_extractor_scope):
    """Returns a map of variables to load from a foreign checkpoint.

    Note that this overrides the default implementation in
    faster_rcnn_meta_arch.FasterRCNNFeatureExtractor which does not work for
    InceptionResnetV2 checkpoints.

    TODO(jonathanhuang,rathodv): revisit whether it's possible to force the
    `Repeat` namescope as created in `_extract_box_classifier_features` to
    start counting at 2 (e.g. `Repeat_2`) so that the default restore_fn can
    be used.

    Args:
      first_stage_feature_extractor_scope: A scope name for the first stage
        feature extractor.
      second_stage_feature_extractor_scope: A scope name for the second stage
        feature extractor.

    Returns:
      A dict mapping variable names (to load from a checkpoint) to variables in
      the model graph.
    """

    variables_to_restore = {}
    for variable in tf.global_variables():
      if variable.op.name.startswith(
          first_stage_feature_extractor_scope):
        var_name = variable.op.name.replace(
            first_stage_feature_extractor_scope + '/', '')
        variables_to_restore[var_name] = variable
      if variable.op.name.startswith(
          second_stage_feature_extractor_scope):
        var_name = variable.op.name.replace(
            second_stage_feature_extractor_scope
            + '/InceptionResnetV2/Repeat', 'InceptionResnetV2/Repeat_2')
        var_name = var_name.replace(
            second_stage_feature_extractor_scope + '/', '')
        variables_to_restore[var_name] = variable
    return variables_to_restore
//  [python]
def eval_dataset(ds, greedy, num_samples, save_path, max_length, seed=123):
    np.random.seed(seed) # reproducibility
    tf.set_random_seed(seed)
  
    config.is_training = False
    config.greedy = greedy
    config.batch_size = num_samples ##### #####
    config.max_length = max_length ##### #####
    config.temperature = 1.2 ##### #####

    tf.reset_default_graph()
    actor = Actor() # Build graph

    variables_to_save = [v for v in tf.global_variables() if 'Adam' not in v.name] # Save & restore all the variables.
    saver = tf.train.Saver(var_list=variables_to_save, keep_checkpoint_every_n_hours=1.0) 

    with tf.Session() as sess:  # start session
        sess.run(tf.global_variables_initializer()) # Run initialize op

        # save_path = "save/"+dir_
        saver.restore(sess, save_path+"/actor.ckpt") # Restore variables from disk.

        predictions_length, predictions_length_w2opt = [], []
        results, results_w2opt = [], []
    #     for i in tqdm(range(1000)): # test instance
    #         seed_ = 1+i
    #         input_batch = dataset.test_batch(1, actor.max_length, actor.dimension, seed=seed_, shuffle=False)
        for i, coords in enumerate(tqdm(ds, mininterval=30)):
            start = time.time()
            input_batch = [preprocess_instance(coords)]  # Add batch dimension
            feed = {actor.input_: input_batch} # Get feed dict
            tour, reward = sess.run([actor.tour, actor.reward], feed_dict=feed) # sample tours
            j = np.argmin(reward) # find best solution
            best_permutation = tour[j][:-1]
            predictions_length.append(reward[j])
            
#             print('reward (before 2 opt)',reward[j])
#             dataset.visualize_2D_trip(input_batch[0][best_permutation])
#             dataset.visualize_sampling(tour)
            
            results.append((calc_tsp_length(coords, best_permutation), best_permutation, time.time() - start))
            

            opt_tour, opt_length = dataset.loop2opt(input_batch[0][best_permutation])
            predictions_length_w2opt.append(opt_length)
            
#             print('reward (with 2 opt)', opt_length)
#             dataset.visualize_2D_trip(opt_tour)
            
            # Find permutation corresponding to opt_tour
            opt_tour_ind = np.linalg.norm(opt_tour[:, None, :] - input_batch[0][None, :, :], axis=-1).argmin(-1)
            
            results_w2opt.append((calc_tsp_length(coords, opt_tour_ind), opt_tour_ind, time.time() - start))
            

        predictions_length = np.asarray(predictions_length) # average tour length
        predictions_length_w2opt = np.asarray(predictions_length_w2opt)
        print("Testing COMPLETED ! Mean length1:",np.mean(predictions_length), "Mean length2:",np.mean(predictions_length_w2opt))
        
        return results, results_w2opt
//  [python]
def variables(self):
        # for v in tf.trainable_variables():
        #     print(v)
        return tf.global_variables()
//  [python]
def test(args, data):
    adj_info = data[0]
    latest_per_user_by_time = data[1]
    user_id_map = data[2]
    item_id_map = data[3]
    train_df = data[4]
    valid_df = data[5]
    test_df = data[6]
    
    args.num_items = len(item_id_map) + 1
    args.num_users = len(user_id_map)
    args.batch_size = 1
    placeholders = construct_placeholders(args)
    
    minibatch = MinibatchIterator(adj_info,
                latest_per_user_by_time,
                [train_df, valid_df, test_df],
                placeholders,
                batch_size=args.batch_size,
                max_degree=args.max_degree,
                num_nodes=len(user_id_map),
                max_length=args.max_length,
                samples_1_2=[args.samples_1, args.samples_2],
                training=False)
    
    dgrec = DGRec(args, minibatch.sizes, placeholders)
    
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    saver = tf.train.Saver(tf.all_variables(), max_to_keep=3)
    ckpt = tf.train.get_checkpoint_state(args.ckpt_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print('Restore model from {}!'.format(args.ckpt_dir))
    else:
        print('Failed to restore model from {}'.format(args.ckpt_dir))
        sys.exit(0)
    ret = evaluate(sess, dgrec, minibatch, "test")
    print("Test results(batch_size=1):",
          "\tloss=", "{:.5f}".format(ret[0]),
          "\trecall@20=", "{:.5f}".format(ret[1]),
          "\tndcg=", "{:.5f}".format(ret[2]),
          )

    recall = ret[-3]
    ndcg = ret[-2]
    x_strs = ret[-1]
    with open('metric_dist.txt','w') as f:
        for idx in range(len(ret[-1])):
            f.write(x_strs[idx] + '\t' + str(recall[idx]) + '\t' + str(ndcg[idx]) + '\n')
//  [python]
def test(args, data):
    adj_info = data[0]
    latest_per_user_by_time = data[1]
    user_id_map = data[2]
    item_id_map = data[3]
    train_df = data[4]
    valid_df = data[5]
    test_df = data[6]
    
    args.num_items = len(item_id_map) + 1
    args.num_users = len(user_id_map)
    args.batch_size = 1
    placeholders = construct_placeholders(args)
    
    minibatch = MinibatchIterator(adj_info,
                latest_per_user_by_time,
                [train_df, valid_df, test_df],
                placeholders,
                batch_size=args.batch_size,
                max_degree=args.max_degree,
                num_nodes=len(user_id_map),
                max_length=args.max_length,
                samples_1_2=[args.samples_1, args.samples_2],
                training=False)
    
    dgrec = DGRec(args, minibatch.sizes, placeholders)
    
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    saver = tf.train.Saver(tf.global_variables(), max_to_keep=3)
    ckpt = tf.train.get_checkpoint_state(args.ckpt_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print('Restore model from {}!'.format(args.ckpt_dir))
    else:
        print('Failed to restore model from {}'.format(args.ckpt_dir))
        sys.exit(0)
    ret = evaluate(sess, dgrec, minibatch, "test")
    print("Test results(batch_size=1):",
          "\tloss=", "{:.5f}".format(ret[0]),
          "\trecall@20=", "{:.5f}".format(ret[1]),
          "\tndcg=", "{:.5f}".format(ret[2]),
          )

    recall = ret[-3]
    ndcg = ret[-2]
    x_strs = ret[-1]
    with open('metric_dist.txt','w') as f:
        for idx in range(len(ret[-1])):
            f.write(x_strs[idx] + '\t' + str(recall[idx]) + '\t' + str(ndcg[idx]) + '\n')
//  [python]
def restore_into_scope(model_path, scope_name, sess):
    global_vars = tf.global_variables()
    tensors_to_load = [v for v in global_vars if v.name.startswith(scope_name + "/")]

    load_dict = {}
    for j in range(0, np.size(tensors_to_load)):
        tensor_name = tensors_to_load[j].name
        tensor_name = tensor_name[0:-2]  # remove ':0'
        tensor_name = tensor_name.replace(scope_name + "/", "")  # remove scope
        load_dict.update({tensor_name: tensors_to_load[j]})
    loader = tf.train.Saver(var_list=load_dict)
    loader.restore(sess, model_path)
    log_string(
        "Model restored from: {0} into scope: {1}.".format(model_path, scope_name)
    )
//  [python]
def restore_into_scope(model_path, scope_name, sess):
    # restored_vars = get_tensors_in_checkpoint_file(file_name=MODEL_PATH)
    # tensors_to_load = build_tensors_in_checkpoint_file(restored_vars, scope=scope_name)

    global_vars = tf.global_variables()
    tensors_to_load = [v for v in global_vars if v.name.startswith(scope_name + "/")]

    load_dict = {}
    for j in range(0, np.size(tensors_to_load)):
        tensor_name = tensors_to_load[j].name
        tensor_name = tensor_name[0:-2]  # remove ':0'
        tensor_name = tensor_name.replace(scope_name + "/", "")  # remove scope
        load_dict.update({tensor_name: tensors_to_load[j]})
    loader = tf.train.Saver(var_list=load_dict)
    loader.restore(sess, model_path)
    log_string(
        "Model restored from: {0} into scope: {1}.".format(model_path, scope_name)
    )
//  [python]
def restore_map(self, from_detection_checkpoint=True):
    """Returns a map of variables to load from a foreign checkpoint.

    See parent class for details.

    Args:
      from_detection_checkpoint: whether to restore from a full detection
        checkpoint (with compatible variable names) or to restore from a
        classification checkpoint for initialization prior to training.

    Returns:
      A dict mapping variable names (to load from a checkpoint) to variables in
      the model graph.
    """
    variables_to_restore = {}
    for variable in tf.global_variables():
      if variable.op.name.startswith(self._extract_features_scope):
        var_name = variable.op.name
        if not from_detection_checkpoint:
          var_name = (re.split('^' + self._extract_features_scope + '/',
                               var_name)[-1])
        variables_to_restore[var_name] = variable
    return variables_to_restore
//  [python]
def restore_map(self, from_detection_checkpoint=True):
    """Returns a map of variables to load from a foreign checkpoint.

    See parent class for details.

    Args:
      from_detection_checkpoint: whether to restore from a full detection
        checkpoint (with compatible variable names) or to restore from a
        classification checkpoint for initialization prior to training.

    Returns:
      A dict mapping variable names (to load from a checkpoint) to variables in
      the model graph.
    """
    if not from_detection_checkpoint:
      return self._feature_extractor.restore_from_classification_checkpoint_fn(
          self.first_stage_feature_extractor_scope,
          self.second_stage_feature_extractor_scope)

    variables_to_restore = tf.global_variables()
    variables_to_restore.append(slim.get_or_create_global_step())
    # Only load feature extractor variables to be consistent with loading from
    # a classification checkpoint.
    feature_extractor_variables = tf.contrib.framework.filter_variables(
        variables_to_restore,
        include_patterns=[self.first_stage_feature_extractor_scope,
                          self.second_stage_feature_extractor_scope])
    return {var.op.name: var for var in feature_extractor_variables}
//  [python]
def testNoBatchNormScaleByDefault(self):
    height, width = 299, 299
    num_classes = 1000
    inputs = tf.placeholder(tf.float32, (1, height, width, 3))
    with slim.arg_scope(inception.inception_v3_arg_scope()):
      inception.inception_v3(inputs, num_classes, is_training=False)

    self.assertEqual(tf.global_variables('.*/BatchNorm/gamma:0$'), [])
//  [python]
def save_ckpt(mode_name='model.ckpt', save_dir='checkpoint', var_list=None, global_step=None, printable=False):
    """Save parameters into `ckpt` file.

    Parameters
    ------------
    mode_name : str
        The name of the model, default is ``model.ckpt``.
    save_dir : str
        The path / file directory to the `ckpt`, default is ``checkpoint``.
    var_list : list of tensor
        The parameters / variables (tensor) to be saved. If empty, save all global variables (default).
    global_step : int or None
        Step number.
    printable : boolean
        Whether to print all parameters information.

    See Also
    --------
    load_ckpt

    """

    if var_list is None:
        if sess is None:
            # FIXME: not sure whether global variables can be accessed in eager mode
            raise ValueError(
                "If var_list is None, sess must be specified. "
                "In eager mode, can not access global variables easily. "
            )
        var_list = []

    ckpt_file = os.path.join(save_dir, mode_name)
    if var_list == []:
        var_list = tf.global_variables()

    logging.info("[*] save %s n_weights: %d" % (ckpt_file, len(var_list)))

    if printable:
        for idx, v in enumerate(var_list):
            logging.info("  param {:3}: {:15}   {}".format(idx, v.name, str(v.get_shape())))

    if sess:
        # graph mode
        saver = tf.train.Saver(var_list)
        saver.save(sess, ckpt_file, global_step=global_step)
    else:
        # eager mode
        # saver = tfes.Saver(var_list)
        # saver.save(ckpt_file, global_step=global_step)
        # TODO: tf2.0 not stable, cannot import tensorflow.contrib.eager.python.saver
        pass
//  [python]
def restore_session(session,
                    path,
                    replace_to=None,
                    replace_from=None,
                    verbose=False,
                    use_metagraph=True,
                    only_features=False):
    """
    Call restore on tf.train.Saver on a specific path to store all the
    variables of the current tensorflow session to a file for later restoring.

    Arguments:
        session : tf.Session
        path : str, place containing the session data to restore
        verbose : bool, print status messages.
        use_metagraph : bool, restore by re-creating saved metagraph.

    Returns:
        bool : success or failure of the restoration
    """
    makedirs(path, exist_ok=True)
    if not path.endswith("/"):
        path = path + "/"
    checkpoint = tf.train.get_checkpoint_state(path)
    if verbose:
        print("Looking for saved session under %r" % (path,), flush=True)
    if checkpoint is None or checkpoint.model_checkpoint_path is None:
        if verbose:
            print("No saved session found", flush=True)
        return False
    fname = basename(checkpoint.model_checkpoint_path)
    if verbose:
        print("Restoring saved session from %r" % (join(path, fname),), flush=True)

    if use_metagraph:
        param_saver = tf.train.import_meta_graph(join(path, fname + ".meta"),
            clear_devices=True)
        missing_vars = []
    else:
        if only_features:
            to_restore = {}
            whitelist = ["embedding", "/RNN/", "/RNNParams", "CharacterConvolution", "HighwayLayer"]
            for var in tf.all_variables():
                if any(keyword in var.name for keyword in whitelist):
                    to_restore[var.name[:-2]] = var
            param_saver = tf.train.Saver(to_restore)
        else:
            if replace_to is not None and replace_from is not None:
                to_restore = {}
                for var in tf.all_variables():
                    var_name = var.name[:var.name.rfind(":")]
                    old_name = var_name.replace(replace_to, replace_from)
                    to_restore[old_name] = var
                param_saver = tf.train.Saver(to_restore)
                missing_vars = []
            else:
                reader = tf.train.NewCheckpointReader(join(path, fname))
                saved_shapes = reader.get_variable_to_shape_map()
                found_vars = [var for var in tf.all_variables()
                              if var.name.split(':')[0] in saved_shapes]
                missing_vars = [var for var in tf.all_variables()
                                if var.name.split(':')[0] not in saved_shapes]
                param_saver = tf.train.Saver(found_vars)
    param_saver.restore(session, join(path, fname))
    session.run([var.initializer for var in missing_vars])
    return True
//  [python]
def restore_session(session,
                    path,
                    replace_to=None,
                    replace_from=None,
                    verbose=False,
                    use_metagraph=True,
                    only_features=False):
    """
    Call restore on tf.train.Saver on a specific path to store all the
    variables of the current tensorflow session to a file for later restoring.

    Arguments:
        session : tf.Session
        path : str, place containing the session data to restore
        verbose : bool, print status messages.
        use_metagraph : bool, restore by re-creating saved metagraph.

    Returns:
        bool : success or failure of the restoration
    """
    makedirs(path, exist_ok=True)
    if not path.endswith("/"):
        path = path + "/"
    checkpoint = tf.train.get_checkpoint_state(path)
    if verbose:
        print("Looking for saved session under %r" % (path,), flush=True)
    if checkpoint is None or checkpoint.model_checkpoint_path is None:
        if verbose:
            print("No saved session found", flush=True)
        return False
    fname = basename(checkpoint.model_checkpoint_path)
    if verbose:
        print("Restoring saved session from %r" % (join(path, fname),), flush=True)

    if use_metagraph:
        param_saver = tf.train.import_meta_graph(join(path, fname + ".meta"),
            clear_devices=True)
        missing_vars = []
    else:
        if only_features:
            to_restore = {}
            whitelist = ["embedding", "/RNN/", "/RNNParams", "CharacterConvolution", "HighwayLayer"]
            for var in tf.global_variables():
                if any(keyword in var.name for keyword in whitelist):
                    to_restore[var.name[:-2]] = var
            param_saver = tf.train.Saver(to_restore)
        else:
            if replace_to is not None and replace_from is not None:
                to_restore = {}
                for var in tf.global_variables():
                    var_name = var.name[:var.name.rfind(":")]
                    old_name = var_name.replace(replace_to, replace_from)
                    to_restore[old_name] = var
                param_saver = tf.train.Saver(to_restore)
                missing_vars = []
            else:
                reader = tf.train.NewCheckpointReader(join(path, fname))
                saved_shapes = reader.get_variable_to_shape_map()
                found_vars = [var for var in tf.global_variables()
                              if var.name.split(':')[0] in saved_shapes]
                missing_vars = [var for var in tf.global_variables()
                                if var.name.split(':')[0] not in saved_shapes]
                param_saver = tf.train.Saver(found_vars)
    param_saver.restore(session, join(path, fname))
    session.run([var.initializer for var in missing_vars])
    return True
//  [python]
def sync_all_params():
    """Sync all tf variables across MPI processes."""
    return sync_params(tf.compat.v1.global_variables())
//  [python]
def sync_all_params():
    """Sync all tf variables across MPI processes."""
    return sync_params(tf.global_variables())
//  [python]
def _initialize_variables():
    """Utility to initialize uninitialized variables on the fly.
    """
    variables = tf.global_variables()
    uninitialized_variables = []
    for v in variables:
        if not hasattr(v, '_keras_initialized') or not v._keras_initialized:
            uninitialized_variables.append(v)
            v._keras_initialized = True
    if uninitialized_variables:
        sess = get_session()
        sess.run(tf.variables_initializer(uninitialized_variables))
//  [python]
def testBatchNormScale(self):
    height, width = 299, 299
    num_classes = 1000
    inputs = tf.placeholder(tf.float32, (1, height, width, 3))
    with slim.arg_scope(
        inception.inception_v3_arg_scope(batch_norm_scale=True)):
      inception.inception_v3(inputs, num_classes, is_training=False)

    gamma_names = set(
        v.op.name for v in tf.compat.v1.global_variables('.*/BatchNorm/gamma:0$'))
    self.assertGreater(len(gamma_names), 0)
    for v in tf.compat.v1.global_variables('.*/BatchNorm/moving_mean:0$'):
      self.assertIn(v.op.name[:-len('moving_mean')] + 'gamma', gamma_names)
//  [python]
def testBatchNormScale(self):
    height, width = 299, 299
    num_classes = 1000
    inputs = tf.placeholder(tf.float32, (1, height, width, 3))
    with slim.arg_scope(
        inception.inception_v3_arg_scope(batch_norm_scale=True)):
      inception.inception_v3(inputs, num_classes, is_training=False)

    gamma_names = set(
        v.op.name for v in tf.global_variables('.*/BatchNorm/gamma:0$'))
    self.assertGreater(len(gamma_names), 0)
    for v in tf.global_variables('.*/BatchNorm/moving_mean:0$'):
      self.assertIn(v.op.name[:-len('moving_mean')] + 'gamma', gamma_names)
//  [python]
def get_global_variable_by_name(name):
	"""Returns the global variable of given name.

	name : the name of the global variable
	"""
	return [v for v in tf.global_variables() if v.name == name][0]
//  [python]
def get_ema_vars():
  """Get all exponential moving average (ema) variables."""
  ema_vars = tf.trainable_variables() + tf.get_collection('moving_vars')
  for v in tf.global_variables():
    # We maintain mva for batch norm moving mean and variance as well.
    if 'moving_mean' in v.name or 'moving_variance' in v.name:
      ema_vars.append(v)
  return list(set(ema_vars))
//  [python]
def save_all_variables(fn):
    util.save_pkl({var.name: var.eval() for var in tf.compat.v1.global_variables()}, fn)
//  [python]
def save_all_variables(fn):
    util.save_pkl({var.name: var.eval() for var in tf.global_variables()}, fn)
//  [python]
def init(model_path, sess):
    def get_variables_in_checkpoint_file(file_name):
        reader = tf.pywrap_tensorflow.NewCheckpointReader(file_name)
        # reader.get_tensor()
        var_to_shape_map = reader.get_variable_to_shape_map()
        return var_to_shape_map, reader

    var_keep_dic, reader = get_variables_in_checkpoint_file(model_path)
    my_var_list = tf.all_variables()
    sess.run(tf.variables_initializer(my_var_list, name='init'))
    variables_to_restore = []
    my_dict = {}
    for v in my_var_list:
        name = v.name.split(':')[0]
        my_dict[name] = 0
        if not var_keep_dic.has_key(name):
            print('He does not have', name)
        else:
            if v.shape != var_keep_dic[name]:
                print('Does not match shape: ', v.shape, var_keep_dic[name])
                continue
            variables_to_restore.append(v)
    for name in var_keep_dic:
        if not my_dict.has_key(name):
            print('I do not have ', name)
    restorer = tf.train.Saver(variables_to_restore)
    restorer.restore(sess, model_path)
    print('Initialized')
//  [python]
def init(model_path, sess):
    def get_variables_in_checkpoint_file(file_name):
        reader = tf.pywrap_tensorflow.NewCheckpointReader(file_name)
        # reader.get_tensor()
        var_to_shape_map = reader.get_variable_to_shape_map()
        return var_to_shape_map, reader

    var_keep_dic, reader = get_variables_in_checkpoint_file(model_path)
    my_var_list = tf.global_variables()
    sess.run(tf.variables_initializer(my_var_list, name='init'))
    variables_to_restore = []
    my_dict = {}
    for v in my_var_list:
        name = v.name.split(':')[0]
        my_dict[name] = 0
        if not var_keep_dic.has_key(name):
            print('He does not have', name)
        else:
            if v.shape != var_keep_dic[name]:
                print('Does not match shape: ', v.shape, var_keep_dic[name])
                continue
            variables_to_restore.append(v)
    for name in var_keep_dic:
        if not my_dict.has_key(name):
            print('I do not have ', name)
    restorer = tf.train.Saver(variables_to_restore)
    restorer.restore(sess, model_path)
    print('Initialized')
//  [python]
def run_train():
  """Train CAPTCHA for a number of steps."""

  with tf.Graph().as_default():
    images, labels = captcha.inputs(train=True, batch_size=FLAGS.batch_size)

    logits = captcha.inference(images, keep_prob=0.5)

    loss = captcha.loss(logits, labels)

    train_op = captcha.training(loss)

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

    init_op = tf.group(tf.initialize_all_variables(),
                       tf.local_variables_initializer())

    sess = tf.Session()

    sess.run(init_op)

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    try:
      step = 0
      while not coord.should_stop():
        start_time = time.time()
        _, loss_value = sess.run([train_op, loss])
        duration = time.time() - start_time
        if step % 10 == 0:
          print('>> Step %d run_train: loss = %.2f (%.3f sec)' % (step, loss_value,
                                                     duration))
        if step % 100 == 0:
          print('>> %s Saving in %s' % (datetime.now(), FLAGS.checkpoint))
          saver.save(sess, FLAGS.checkpoint, global_step=step)
        step += 1
    except Exception as e:
      print('>> %s Saving in %s' % (datetime.now(), FLAGS.checkpoint))
      saver.save(sess, FLAGS.checkpoint, global_step=step)
      coord.request_stop(e)
    finally:
      coord.request_stop()
    coord.join(threads)
    sess.close()
//  [python]
def run_train():
  """Train CAPTCHA for a number of steps."""

  with tf.Graph().as_default():
    images, labels = captcha.inputs(train=True, batch_size=FLAGS.batch_size)

    logits = captcha.inference(images, keep_prob=0.5)

    loss = captcha.loss(logits, labels)

    train_op = captcha.training(loss)

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

    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())

    sess = tf.Session()

    sess.run(init_op)

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    try:
      step = 0
      while not coord.should_stop():
        start_time = time.time()
        _, loss_value = sess.run([train_op, loss])
        duration = time.time() - start_time
        if step % 10 == 0:
          print('>> Step %d run_train: loss = %.2f (%.3f sec)' % (step, loss_value,
                                                     duration))
        if step % 100 == 0:
          print('>> %s Saving in %s' % (datetime.now(), FLAGS.checkpoint))
          saver.save(sess, FLAGS.checkpoint, global_step=step)
        step += 1
    except Exception as e:
      print('>> %s Saving in %s' % (datetime.now(), FLAGS.checkpoint))
      saver.save(sess, FLAGS.checkpoint, global_step=step)
      coord.request_stop(e)
    finally:
      coord.request_stop()
    coord.join(threads)
    sess.close()
//  [python]
def main():
	args = parse_args()
	image_path = args.image_path
	lm_path = args.lm_path
	# lm_path = os.path.join(args.image_path,'lm5p') # detected landmarks for training images should be saved in <image_path>/lm5p subfolder
	
	# create save path for aligned images and extracted coefficients
	save_path = args.save_path	
	if not os.path.exists(os.path.join(save_path,'img')):
		os.makedirs(os.path.join(save_path,'img'))
	if not os.path.exists(os.path.join(save_path,'coeff')):
		os.makedirs(os.path.join(save_path,'coeff'))

	# Load BFM09 face model
	if not os.path.isfile('./renderer/BFM face model/BFM_model_front_gan.mat'):
		transferBFM09()

	# Load standard landmarks for alignment
	lm3D = load_lm3d()


	# Build reconstruction model
	with tf.Graph().as_default() as graph:

		images = tf.placeholder(name = 'input_imgs', shape = [None,224,224,3], dtype = tf.float32)
		Face3D = face_decoder.Face3D() # analytic 3D face formation process
		coeff = R_Net(images,is_training=False) # 3D face reconstruction network

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

			var_list = tf.trainable_variables()
			g_list = tf.global_variables()

			# Add batch normalization params into trainable variables 
			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

			# Create saver to save and restore weights
			resnet_vars = [v for v in var_list if 'resnet_v1_50' in v.name]
			res_fc = [v for v in var_list if 'fc-id' in v.name or 'fc-ex' in v.name or 'fc-tex' in v.name or 'fc-angles' in v.name or 'fc-gamma' in v.name or 'fc-XY' in v.name or 'fc-Z' in v.name or 'fc-f' in v.name]
			resnet_vars += res_fc

			saver = tf.train.Saver(var_list = var_list,max_to_keep = 100)
			saver.restore(sess,R_net_weights)

			for file in os.listdir(os.path.join(image_path)):
				if file.endswith('png'):
					print(file)

					# load images and landmarks
					image = Image.open(os.path.join(image_path,file))
					if not os.path.isfile(os.path.join(lm_path,file.replace('png','txt'))):
						continue
					lm = np.loadtxt(os.path.join(lm_path,file.replace('png','txt')))
					lm = np.reshape(lm,[5,2])

					# align image for 3d face reconstruction
					align_img,_,_ = Preprocess(image,lm,lm3D) # 512*512*3 RGB image
					align_img = np.array(align_img)

					align_img_ = align_img[:,:,::-1] #RGBtoBGR
					align_img_ = cv2.resize(align_img_,(224,224)) # input image to reconstruction network should be 224*224
					align_img_ = np.expand_dims(align_img_,0)
					coef = sess.run(coeff,feed_dict = {images: align_img_})

					# align image for GAN training
					# eliminate translation and rescale face size to proper scale
					rescale_img = crop_n_rescale_face_region(align_img,coef) # 256*256*3 RGB image
					coef = np.squeeze(coef,0)

					# save aligned images and extracted coefficients
					cv2.imwrite(os.path.join(save_path,'img',file),rescale_img[:,:,::-1])
					savemat(os.path.join(save_path,'coeff',file.replace('.png','.mat')),{'coeff':coef})
//  [python]
def clusterPlanes(options):
    tf.reset_default_graph()
    
    options.batchSize = 1
    min_after_dequeue = 1000

    reader = RecordReaderAll()
    if options.dataset == 'SUNCG':
        filename_queue = tf.train.string_input_producer(['/mnt/vision/PlaneNet/planes_SUNCG_val.tfrecords'], num_epochs=10000)
    elif options.dataset == 'NYU_RGBD':
        filename_queue = tf.train.string_input_producer(['/mnt/vision/PlaneNet/planes_nyu_rgbd_val.tfrecords'], num_epochs=1)
        options.deepSupervision = 0
        options.predictLocal = 0
    elif options.dataset == 'matterport':
        filename_queue = tf.train.string_input_producer(['/mnt/vision/PlaneNet/planes_matterport_val.tfrecords'], num_epochs=1)
    else:
        filename_queue = tf.train.string_input_producer(['/mnt/vision/PlaneNet/planes_scannet_val.tfrecords'], num_epochs=1)
        pass
    
    img_inp, global_gt_dict, local_gt_dict = reader.getBatch(filename_queue, numOutputPlanes=options.numOutputPlanes, batchSize=options.batchSize, min_after_dequeue=min_after_dequeue, getLocal=True, random=False)
        

    
    training_flag = tf.constant(False, tf.bool)

    options.gpu_id = 0
    global_pred_dict, local_pred_dict, deep_pred_dicts = build_graph(img_inp, img_inp, training_flag, options)

    var_to_restore = tf.all_variables()


    config=tf.ConfigProto()
    config.gpu_options.allow_growth=True
    config.allow_soft_placement=True
    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())

    pred_dict = {}
    with tf.Session(config=config) as sess:
        sess.run(init_op)
        #var_to_restore = [v for v in var_to_restore if 'res4b22_relu_non_plane' not in v.name]
        loader = tf.train.Saver(var_to_restore)
        loader.restore(sess, "%s/checkpoint.ckpt"%(options.checkpoint_dir))
        #loader.restore(sess, options.fineTuningCheckpoint)
        
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        
        
        try:
            predPlanes = []
            for index in xrange(options.numImages):
                if index % 10 == 0:
                    print(('image', index))
                    pass
                t0=time.time()

                img, global_gt, global_pred = sess.run([img_inp, global_gt_dict, global_pred_dict])

                predPlanes.append(global_pred['plane'][0])
                continue
            predPlanes = np.array(predPlanes)
            print(predPlanes.shape)
            predPlanes = np.mean(predPlanes, axis=0)
            np.save('dump/plane.npy', predPlanes)
            pass
        except tf.errors.OutOfRangeError:
            print('Done training -- epoch limit reached')
        finally:
            # When done, ask the threads to stop.
            coord.request_stop()
            pass
        
        # Wait for threads to finish.
        coord.join(threads)
        sess.close()
        pass
    return pred_dict
//  [python]
def clusterPlanes(options):
    tf.reset_default_graph()
    
    options.batchSize = 1
    min_after_dequeue = 1000

    reader = RecordReaderAll()
    if options.dataset == 'SUNCG':
        filename_queue = tf.train.string_input_producer(['/mnt/vision/PlaneNet/planes_SUNCG_val.tfrecords'], num_epochs=10000)
    elif options.dataset == 'NYU_RGBD':
        filename_queue = tf.train.string_input_producer(['/mnt/vision/PlaneNet/planes_nyu_rgbd_val.tfrecords'], num_epochs=1)
        options.deepSupervision = 0
        options.predictLocal = 0
    elif options.dataset == 'matterport':
        filename_queue = tf.train.string_input_producer(['/mnt/vision/PlaneNet/planes_matterport_val.tfrecords'], num_epochs=1)
    else:
        filename_queue = tf.train.string_input_producer(['/mnt/vision/PlaneNet/planes_scannet_val.tfrecords'], num_epochs=1)
        pass
    
    img_inp, global_gt_dict, local_gt_dict = reader.getBatch(filename_queue, numOutputPlanes=options.numOutputPlanes, batchSize=options.batchSize, min_after_dequeue=min_after_dequeue, getLocal=True, random=False)
        

    
    training_flag = tf.constant(False, tf.bool)

    options.gpu_id = 0
    global_pred_dict, local_pred_dict, deep_pred_dicts = build_graph(img_inp, img_inp, training_flag, options)

    var_to_restore = tf.global_variables()


    config=tf.ConfigProto()
    config.gpu_options.allow_growth=True
    config.allow_soft_placement=True
    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())

    pred_dict = {}
    with tf.Session(config=config) as sess:
        sess.run(init_op)
        #var_to_restore = [v for v in var_to_restore if 'res4b22_relu_non_plane' not in v.name]
        loader = tf.train.Saver(var_to_restore)
        loader.restore(sess, "%s/checkpoint.ckpt"%(options.checkpoint_dir))
        #loader.restore(sess, options.fineTuningCheckpoint)
        
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        
        
        try:
            predPlanes = []
            for index in xrange(options.numImages):
                if index % 10 == 0:
                    print(('image', index))
                    pass
                t0=time.time()

                img, global_gt, global_pred = sess.run([img_inp, global_gt_dict, global_pred_dict])

                predPlanes.append(global_pred['plane'][0])
                continue
            predPlanes = np.array(predPlanes)
            print(predPlanes.shape)
            predPlanes = np.mean(predPlanes, axis=0)
            np.save('dump/plane.npy', predPlanes)
            pass
        except tf.errors.OutOfRangeError:
            print('Done training -- epoch limit reached')
        finally:
            # When done, ask the threads to stop.
            coord.request_stop()
            pass
        
        # Wait for threads to finish.
        coord.join(threads)
        sess.close()
        pass
    return pred_dict
//  [python]
def initialize(self, sess):
    # Initial file lists are empty
    np_paths = []
    ss_paths = []
    # Fresh train directly from ImageNet weights
    print('Loading initial model weights from {:s}'.format(self.pretrained_model))
    variables = tf.global_variables()
    # Initialize all variables first
    sess.run(tf.variables_initializer(variables, name='init'))
    var_keep_dic = self.get_variables_in_checkpoint_file(self.pretrained_model)
    # Get the variables to restore, ignoring the variables to fix
    variables_to_restore = self.net.get_variables_to_restore(variables, var_keep_dic)

    restorer = tf.train.Saver(variables_to_restore)
    restorer.restore(sess, self.pretrained_model)
    print('Loaded.')
    # Need to fix the variables before loading, so that the RGB weights are changed to BGR
    # For VGG16 it also changes the convolutional weights fc6 and fc7 to
    # fully connected weights
    self.net.fix_variables(sess, self.pretrained_model)
    print('Fixed.')
    last_snapshot_iter = 0
    rate = cfg.TRAIN.LEARNING_RATE
    stepsizes = list(cfg.TRAIN.STEPSIZE)

    return rate, last_snapshot_iter, stepsizes, np_paths, ss_paths