jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def get_train_ops(agent_net, global_step, opt, sess_train_dir, targets_tensor, total_loss):
    grads_and_vars = opt.compute_gradients(total_loss)
    visualize_model(agent_net.input, agent_net.out, targets_tensor)
    visualize_gradients(grads_and_vars)
    summary_op = tf.summary.merge_all()
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(update_ops):
        train_op = opt.apply_gradients(grads_and_vars, global_step)
    init_fn = agent_net.get_tf_init_fn(tf.initialize_all_variables())
    saver = tf.train.Saver()
    sv = tf.train.Supervisor(is_chief=True,
                             logdir=sess_train_dir,
                             summary_op=None,  # Automatic summaries don't work with placeholders.
                             saver=saver,
                             global_step=global_step,
                             save_summaries_secs=30,
                             save_model_secs=60,
                             init_op=None,
                             init_fn=init_fn)
    return summary_op, sv, train_op
//  [python]
def get_train_ops(agent_net, global_step, opt, sess_train_dir, targets_tensor, total_loss):
    grads_and_vars = opt.compute_gradients(total_loss)
    visualize_model(agent_net.input, agent_net.out, targets_tensor)
    visualize_gradients(grads_and_vars)
    summary_op = tf.summary.merge_all()
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(update_ops):
        train_op = opt.apply_gradients(grads_and_vars, global_step)
    init_fn = agent_net.get_tf_init_fn(tf.global_variables_initializer())
    saver = tf.train.Saver()
    sv = tf.train.Supervisor(is_chief=True,
                             logdir=sess_train_dir,
                             summary_op=None,  # Automatic summaries don't work with placeholders.
                             saver=saver,
                             global_step=global_step,
                             save_summaries_secs=30,
                             save_model_secs=60,
                             init_op=None,
                             init_fn=init_fn)
    return summary_op, sv, train_op
//  [python]
def test_predict():
    # below is a function test; if you use this for text classifiction, you need to tranform sentence to indices of vocabulary first. then feed data to the graph.
    num_classes = 9+2 #additional two classes:one is for _GO, another is for _END
    learning_rate = 0.001
    batch_size = 1
    decay_steps = 1000
    decay_rate = 0.9
    sequence_length = 6 #5
    vocab_size = 300
    is_training = False #True
    dropout_keep_prob = 1  # 0.5 #num_sentences
    l2_lambda=0.0001
    d_model=512 #512
    d_k=64
    d_v=64
    h=8
    num_layer=1#6
    embed_size = d_model
    model = Transformer(num_classes, learning_rate, batch_size, decay_steps, decay_rate, sequence_length,
                                    vocab_size, embed_size,d_model,d_k,d_v,h,num_layer,is_training,l2_lambda=l2_lambda)
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        ckpt_dir = 'checkpoint_transformer/sequence_reverse/'
        saver.restore(sess, tf.train.latest_checkpoint(ckpt_dir))
        print("=================restored.")
        for i in range(1500):
            label_list=get_unique_labels()
            input_x = np.array([label_list+[9]],dtype=np.int32)
            label_list_original=copy.deepcopy(label_list)
            label_list.reverse()
            input_y_label=np.array([np.max([label_list[0],label_list[1]])],dtype=np.int32)

            predict, W_projection_value = sess.run([ model.predictions, model.W_projection], #model.loss_val,--->loss, model.train_op
                                feed_dict={model.input_x:input_x,
                                           model.dropout_keep_prob: dropout_keep_prob})
            print(i, "label_list_original as input x:",label_list_original, "prediction:", predict,";label:",input_y_label)
//  [python]
def test_predict():
    # below is a function test; if you use this for text classifiction, you need to tranform sentence to indices of vocabulary first. then feed data to the graph.
    num_classes = 9+2 #additional two classes:one is for _GO, another is for _END
    learning_rate = 0.001
    batch_size = 1
    decay_steps = 1000
    decay_rate = 0.9
    sequence_length = 6 #5
    vocab_size = 300
    is_training = False #True
    dropout_keep_prob = 1  # 0.5 #num_sentences
    l2_lambda=0.0001
    d_model=512 #512
    d_k=64
    d_v=64
    h=8
    num_layer=1#6
    embed_size = d_model
    model = Transformer(num_classes, learning_rate, batch_size, decay_steps, decay_rate, sequence_length,
                                    vocab_size, embed_size,d_model,d_k,d_v,h,num_layer,is_training,l2_lambda=l2_lambda)
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        ckpt_dir = 'checkpoint_transformer/sequence_reverse/'
        saver.restore(sess, tf.train.latest_checkpoint(ckpt_dir))
        print("=================restored.")
        for i in range(1500):
            label_list=get_unique_labels()
            input_x = np.array([label_list+[9]],dtype=np.int32)
            label_list_original=copy.deepcopy(label_list)
            label_list.reverse()
            input_y_label=np.array([np.max([label_list[0],label_list[1]])],dtype=np.int32)

            predict, W_projection_value = sess.run([ model.predictions, model.W_projection], #model.loss_val,--->loss, model.train_op
                                feed_dict={model.input_x:input_x,
                                           model.dropout_keep_prob: dropout_keep_prob})
            print(i, "label_list_original as input x:",label_list_original, "prediction:", predict,";label:",input_y_label)
//  [python]
def test_training():
    # below is a function test; if you use this for text classifiction, you need to tranform sentence to indices of vocabulary first. then feed data to the graph.
    num_classes = 9+2 #additional two classes:one is for _GO, another is for _END
    learning_rate = 0.0001 #/10.0
    batch_size = 1
    decay_steps = 1000
    decay_rate = 0.9
    sequence_length = 6 #5 TODO
    vocab_size = 300
    is_training = True #True
    dropout_keep_prob = 0.9  # 0.5 #num_sentences
    #decoder_sent_length=6
    l2_lambda=0.0001#0.0001
    d_model=512 #512
    d_k=64
    d_v=64
    h=8
    num_layer=1
    embed_size = d_model

    model = Transformer(num_classes, learning_rate, batch_size, decay_steps, decay_rate, sequence_length,
                        vocab_size, embed_size,d_model,d_k,d_v,h,num_layer,is_training,l2_lambda=l2_lambda)
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        ckpt_dir = 'checkpoint_transformer/sequence_reverse/'
        if os.path.exists(ckpt_dir+"checkpoint"):
            saver.restore(sess, tf.train.latest_checkpoint(ckpt_dir))
        for i in range(15000):
            label_list=get_unique_labels()
            input_x = np.array([label_list+[9]],dtype=np.int32)
            label_list_original=copy.deepcopy(label_list)
            label_list.reverse()
            input_y_label=np.array([np.max([label_list[0],label_list[1]])],dtype=np.int32)

            loss, acc, predict, W_projection_value, _ = sess.run([model.loss_val, model.accuracy, model.predictions, model.W_projection, model.train_op],
                                                     feed_dict={model.input_x:input_x, model.input_y_label: input_y_label,
                                                                model.dropout_keep_prob: dropout_keep_prob}) #model.dropout_keep_prob: dropout_keep_prob
            print(i,"loss:", loss, "acc:", acc, "label_list_original as input x:",label_list_original,";input_y_label:", input_y_label, "prediction:", predict)
            if i%1500==0:
                save_path = ckpt_dir + "model.ckpt"
                saver.save(sess, save_path, global_step=i)
//  [python]
def test_training():
    # below is a function test; if you use this for text classifiction, you need to tranform sentence to indices of vocabulary first. then feed data to the graph.
    num_classes = 9+2 #additional two classes:one is for _GO, another is for _END
    learning_rate = 0.0001 #/10.0
    batch_size = 1
    decay_steps = 1000
    decay_rate = 0.9
    sequence_length = 6 #5 TODO
    vocab_size = 300
    is_training = True #True
    dropout_keep_prob = 0.9  # 0.5 #num_sentences
    #decoder_sent_length=6
    l2_lambda=0.0001#0.0001
    d_model=512 #512
    d_k=64
    d_v=64
    h=8
    num_layer=1
    embed_size = d_model

    model = Transformer(num_classes, learning_rate, batch_size, decay_steps, decay_rate, sequence_length,
                        vocab_size, embed_size,d_model,d_k,d_v,h,num_layer,is_training,l2_lambda=l2_lambda)
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        ckpt_dir = 'checkpoint_transformer/sequence_reverse/'
        if os.path.exists(ckpt_dir+"checkpoint"):
            saver.restore(sess, tf.train.latest_checkpoint(ckpt_dir))
        for i in range(15000):
            label_list=get_unique_labels()
            input_x = np.array([label_list+[9]],dtype=np.int32)
            label_list_original=copy.deepcopy(label_list)
            label_list.reverse()
            input_y_label=np.array([np.max([label_list[0],label_list[1]])],dtype=np.int32)

            loss, acc, predict, W_projection_value, _ = sess.run([model.loss_val, model.accuracy, model.predictions, model.W_projection, model.train_op],
                                                     feed_dict={model.input_x:input_x, model.input_y_label: input_y_label,
                                                                model.dropout_keep_prob: dropout_keep_prob}) #model.dropout_keep_prob: dropout_keep_prob
            print(i,"loss:", loss, "acc:", acc, "label_list_original as input x:",label_list_original,";input_y_label:", input_y_label, "prediction:", predict)
            if i%1500==0:
                save_path = ckpt_dir + "model.ckpt"
                saver.save(sess, save_path, global_step=i)
//  [python]
pred_label,harmnization = deploy_seg.inference(com_placeholder, masks_placeholder)

saver = tf.train.Saver()

gpu_options = tf.GPUOptions(allow_growth=True)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) 
sess.run((tf.initialize_all_variables(), tf.local_variables_initializer()))
//  [python]
pred_label,harmnization = deploy_seg.inference(com_placeholder, masks_placeholder)

saver = tf.train.Saver()

gpu_options = tf.GPUOptions(allow_growth=True)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) 
sess.run((tf.global_variables_initializer(), tf.local_variables_initializer()))
//  [python]
def testBatchNorm(self):
    with tf.Graph().as_default():
      # 4 images with resolution 2x1 and 3 channels.
      x1 = tf.constant([[[5, 7, 2]], [[5, 8, 8]]], dtype=tf.float32)
      x2 = tf.constant([[[1, 2, 0]], [[4, 0, 4]]], dtype=tf.float32)
      x3 = tf.constant([[[6, 2, 6]], [[5, 0, 5]]], dtype=tf.float32)
      x4 = tf.constant([[[2, 4, 2]], [[6, 4, 1]]], dtype=tf.float32)
      x = tf.stack([x1, x2, x3, x4])
      self.assertAllEqual(x.shape.as_list(), [4, 2, 1, 3])

      core_bn = tf.layers.batch_normalization(x, training=True)
      contrib_bn = tf.contrib.layers.batch_norm(x, is_training=True)
      custom_bn = arch_ops.batch_norm(x, is_training=True)
      with self.session() as sess:
        sess.run(tf.initialize_all_variables())
        core_bn, contrib_bn, custom_bn = sess.run(
            [core_bn, contrib_bn, custom_bn])
        tf.logging.info("core_bn: %s", core_bn[0])
        tf.logging.info("contrib_bn: %s", contrib_bn[0])
        tf.logging.info("custom_bn: %s", custom_bn[0])
        self.assertAllClose(core_bn, contrib_bn)
        self.assertAllClose(custom_bn, contrib_bn)
        expected_values = np.asarray(
            [[[[0.4375205, 1.30336881, -0.58830315]],
              [[0.4375205, 1.66291881, 1.76490951]]],
             [[[-1.89592218, -0.49438119, -1.37270737]],
              [[-0.14584017, -1.21348119, 0.19610107]]],
             [[[1.02088118, -0.49438119, 0.98050523]],
              [[0.4375205, -1.21348119, 0.58830321]]],
             [[[-1.31256151, 0.22471881, -0.58830315]],
              [[1.02088118, 0.22471881, -0.98050523]]]],
            dtype=np.float32)
        self.assertAllClose(custom_bn, expected_values)
//  [python]
def testBatchNorm(self):
    with tf.Graph().as_default():
      # 4 images with resolution 2x1 and 3 channels.
      x1 = tf.constant([[[5, 7, 2]], [[5, 8, 8]]], dtype=tf.float32)
      x2 = tf.constant([[[1, 2, 0]], [[4, 0, 4]]], dtype=tf.float32)
      x3 = tf.constant([[[6, 2, 6]], [[5, 0, 5]]], dtype=tf.float32)
      x4 = tf.constant([[[2, 4, 2]], [[6, 4, 1]]], dtype=tf.float32)
      x = tf.stack([x1, x2, x3, x4])
      self.assertAllEqual(x.shape.as_list(), [4, 2, 1, 3])

      core_bn = tf.layers.batch_normalization(x, training=True)
      contrib_bn = tf.contrib.layers.batch_norm(x, is_training=True)
      custom_bn = arch_ops.batch_norm(x, is_training=True)
      with self.session() as sess:
        sess.run(tf.global_variables_initializer())
        core_bn, contrib_bn, custom_bn = sess.run(
            [core_bn, contrib_bn, custom_bn])
        tf.logging.info("core_bn: %s", core_bn[0])
        tf.logging.info("contrib_bn: %s", contrib_bn[0])
        tf.logging.info("custom_bn: %s", custom_bn[0])
        self.assertAllClose(core_bn, contrib_bn)
        self.assertAllClose(custom_bn, contrib_bn)
        expected_values = np.asarray(
            [[[[0.4375205, 1.30336881, -0.58830315]],
              [[0.4375205, 1.66291881, 1.76490951]]],
             [[[-1.89592218, -0.49438119, -1.37270737]],
              [[-0.14584017, -1.21348119, 0.19610107]]],
             [[[1.02088118, -0.49438119, 0.98050523]],
              [[0.4375205, -1.21348119, 0.58830321]]],
             [[[-1.31256151, 0.22471881, -0.58830315]],
              [[1.02088118, 0.22471881, -0.98050523]]]],
            dtype=np.float32)
        self.assertAllClose(custom_bn, expected_values)
//  [python]
def __init__(self, run_config: RunConfig, model_config: Dict):
        self.config = CMLConfig(**model_config)
        super().__init__(run_config, self.config)

        self.num_users, self.num_items = self.dataset.num_users, self.dataset.num_items
        self.user_pos_train = self.dataset.train_data.to_user_dict()

        self._build_model()
        tf_config = tf.ConfigProto()  # allow_soft_placement=False, log_device_placement=True
        tf_config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=tf_config)
        self.sess.run(tf.global_variables_initializer())
//  [python]
def _initialize_session(self, sess):
        """Initialize a new session to run the operations.

        Args:
            sess (tf.Session): Tensorflow session.

        """

        # Initialize the the variables that we introduced (like queues etc.)
        sess.run(tf.initialize_all_variables())
        sess.run(tf.local_variables_initializer())

        # Restore the weights from Inception
        # (do not call a global/local variable initializer after this call)
        saver = tf.train.Saver()
        saver.restore(sess, self.checkpoint_file)
//  [python]
def _initialize_session(self, sess):
        """Initialize a new session to run the operations.

        Args:
            sess (tf.Session): Tensorflow session.

        """

        # Initialize the the variables that we introduced (like queues etc.)
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())

        # Restore the weights from Inception
        # (do not call a global/local variable initializer after this call)
        saver = tf.train.Saver()
        saver.restore(sess, self.checkpoint_file)
//  [python]
def train_spn(spn, train_im, train_lab=None, num_epochs=50, batch_size=100, sess=tf.Session()):

    input_ph = tf.placeholder(tf.float32, [batch_size, train_im.shape[1]])
    label_ph = tf.placeholder(tf.int32, [batch_size])
    marginalized = tf.zeros_like(input_ph)
    spn_output = spn.forward(input_ph, marginalized)
    if train_lab is not None:
        disc_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=label_ph, logits=spn_output))
        label_idx = tf.stack([tf.range(batch_size), label_ph], axis=1)
        gen_loss = tf.reduce_mean(-1 * tf.gather_nd(spn_output, label_idx))
    very_gen_loss = -1 * tf.reduce_mean(tf.reduce_logsumexp(spn_output, axis=1))
    loss = disc_loss
    optimizer = tf.train.AdamOptimizer()
    train_op = optimizer.minimize(loss)
    batches_per_epoch = train_im.shape[0] // batch_size

    # sess.run(tf.variables_initializer(optimizer.variables()))
    sess.run(tf.initialize_all_variables())

    for i in range(num_epochs):
        num_correct = 0
        for j in range(batches_per_epoch):
            im_batch = train_im[j * batch_size : (j + 1) * batch_size, :]
            label_batch = train_lab[j * batch_size : (j + 1) * batch_size]

            _, cur_output, cur_loss = sess.run(
                [train_op, spn_output, loss], feed_dict={input_ph: im_batch, label_ph: label_batch}
            )

            max_idx = np.argmax(cur_output, axis=1)

            num_correct_batch = np.sum(max_idx == label_batch)
            num_correct += num_correct_batch

        acc = num_correct / (batch_size * batches_per_epoch)
        print(i, acc, cur_loss)
//  [python]
def train_spn(spn, train_im, train_lab=None, num_epochs=50, batch_size=100, sess=tf.Session()):

    input_ph = tf.placeholder(tf.float32, [batch_size, train_im.shape[1]])
    label_ph = tf.placeholder(tf.int32, [batch_size])
    marginalized = tf.zeros_like(input_ph)
    spn_output = spn.forward(input_ph, marginalized)
    if train_lab is not None:
        disc_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=label_ph, logits=spn_output))
        label_idx = tf.stack([tf.range(batch_size), label_ph], axis=1)
        gen_loss = tf.reduce_mean(-1 * tf.gather_nd(spn_output, label_idx))
    very_gen_loss = -1 * tf.reduce_mean(tf.reduce_logsumexp(spn_output, axis=1))
    loss = disc_loss
    optimizer = tf.train.AdamOptimizer()
    train_op = optimizer.minimize(loss)
    batches_per_epoch = train_im.shape[0] // batch_size

    # sess.run(tf.variables_initializer(optimizer.variables()))
    sess.run(tf.global_variables_initializer())

    for i in range(num_epochs):
        num_correct = 0
        for j in range(batches_per_epoch):
            im_batch = train_im[j * batch_size : (j + 1) * batch_size, :]
            label_batch = train_lab[j * batch_size : (j + 1) * batch_size]

            _, cur_output, cur_loss = sess.run(
                [train_op, spn_output, loss], feed_dict={input_ph: im_batch, label_ph: label_batch}
            )

            max_idx = np.argmax(cur_output, axis=1)

            num_correct_batch = np.sum(max_idx == label_batch)
            num_correct += num_correct_batch

        acc = num_correct / (batch_size * batches_per_epoch)
        print(i, acc, cur_loss)
//  [python]
def __init__(
            self,
            n_actions,
            n_features,
            learning_rate=0.005,
            reward_decay=0.9,
            e_greedy=0.9,
            replace_target_iter=500,
            memory_size=10000,
            batch_size=32,
            e_greedy_increment=None,
            output_graph=False,
            prioritized=True,
            sess=None,
    ):
        self.n_actions = n_actions
        self.n_features = n_features
        self.lr = learning_rate
        self.gamma = reward_decay
        self.epsilon_max = e_greedy
        self.replace_target_iter = replace_target_iter
        self.memory_size = memory_size
        self.batch_size = batch_size
        self.epsilon_increment = e_greedy_increment
        self.epsilon = 0 if e_greedy_increment is not None else self.epsilon_max

        self.prioritized = prioritized    # decide to use double q or not

        self.learn_step_counter = 0

        self._build_net()
        t_params = tf.get_collection('target_net_params')
        e_params = tf.get_collection('eval_net_params')
        self.replace_target_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)]

        if self.prioritized:
            self.memory = Memory(capacity=memory_size)
        else:
            self.memory = np.zeros((self.memory_size, n_features*2+2))

        if sess is None:
            self.sess = tf.Session()
            self.sess.run(tf.global_variables_initializer())
        else:
            self.sess = sess

        if output_graph:
            tf.summary.FileWriter("logs/", self.sess.graph)

        self.cost_his = []
//  [python]
t0 = datetime.now()
LL = []
W1_val = None
W2_val = None
init = tf.initialize_all_variables()
//  [python]
t0 = datetime.now()
LL = []
W1_val = None
W2_val = None
init = tf.global_variables_initializer()
//  [python]
def __init__(self, cache_path):
        super(USE, self).__init__()
        os.environ['TFHUB_CACHE_DIR'] = cache_path
        module_url = "https://tfhub.dev/google/universal-sentence-encoder-large/3"
        self.embed = hub.Module(module_url)
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=config)
        self.build_graph()
        self.sess.run([tf.global_variables_initializer(), tf.tables_initializer()])
//  [python]
def main(_):
    prepare_dirs_and_logger(config)

    if not config.task.lower().startswith('tsp'):
        raise Exception("[!] Task should starts with TSP")

    if config.max_enc_length is None:
        config.max_enc_length = config.max_data_length
    if config.max_dec_length is None:
        config.max_dec_length = config.max_data_length

    rng = np.random.RandomState(config.random_seed)
    tf.set_random_seed(config.random_seed)

    model = Model(config)

    batch_size = config.batch_size

    train_enc_seq, train_target_seq, train_enc_seq_length, train_target_seq_length = gen_data('data/tsp10.txt')

    eval_enc_seq,eval_target_seq,eval_enc_seq_length,eval_target_seq_length = train_enc_seq[-batch_size:], \
                                                                              train_target_seq[-batch_size:], \
                                                                              train_enc_seq_length[-batch_size:], \
                                                                              train_target_seq_length[-batch_size:]

    train_enc_seq, train_target_seq, train_enc_seq_length, train_target_seq_length= train_enc_seq[: -batch_size], \
                                                                                  train_target_seq[:-batch_size], \
                                                                                  train_enc_seq_length[:-batch_size], \
                                                                                  train_target_seq_length[:-batch_size]

    test_enc_seq, test_target_seq, test_enc_seq_length, test_target_seq_length = gen_data('data/tsp10_test.txt')

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for step in range(min(config.max_step,len(train_enc_seq)//batch_size)):
            train_batch={
                'enc_seq': train_enc_seq[step * batch_size:(step + 1) * batch_size],
                'enc_seq_length': train_enc_seq_length[step * batch_size:(step + 1) * batch_size],
                'target_seq': train_target_seq[step * batch_size:(step + 1) * batch_size],
                'target_seq_length': train_target_seq_length[step * batch_size:(step + 1) * batch_size]
            }
            loss = model.train(sess,train_batch)
            print(str(step) + " train loss : " + str(loss))

            if step > 0 and step % config.eval_step == 0:
                eval_batch = {
                    'enc_seq': eval_enc_seq,
                    'enc_seq_length': eval_enc_seq_length,
                    'target_seq': eval_target_seq,
                    'target_seq_length': eval_target_seq_length
                }
                eval_loss = model.eval(sess,eval_batch)
                print(str(step) + " eval loss : " + str(eval_loss))
//  [python]
def test_single_predictor_model_training(self):
    model_proto = model_pb2.Model()
    text_format.Merge(SINGLE_PREDICTOR_MODEL_TEXT_PROTO, model_proto)
    model_object = model_builder.build(model_proto, True)
    test_groundtruth_text_list = [
      tf.constant(b'hello', dtype=tf.string),
      tf.constant(b'world', dtype=tf.string)]
    model_object.provide_groundtruth({'groundtruth_text': test_groundtruth_text_list})
    test_input_image = tf.random_uniform(
      shape=[2, 32, 100, 3], minval=0, maxval=255,
      dtype=tf.float32, seed=1)
    prediction_dict = model_object.predict(model_object.preprocess(test_input_image))
    loss = model_object.loss(prediction_dict)
    with self.test_session() as sess:
      sess.run([
        tf.initialize_all_variables(),
        tf.tables_initializer()])
      outputs = sess.run({'loss': loss})
      print(outputs['loss'])
//  [python]
def test_single_predictor_model_training(self):
    model_proto = model_pb2.Model()
    text_format.Merge(SINGLE_PREDICTOR_MODEL_TEXT_PROTO, model_proto)
    model_object = model_builder.build(model_proto, True)
    test_groundtruth_text_list = [
      tf.constant(b'hello', dtype=tf.string),
      tf.constant(b'world', dtype=tf.string)]
    model_object.provide_groundtruth({'groundtruth_text': test_groundtruth_text_list})
    test_input_image = tf.random_uniform(
      shape=[2, 32, 100, 3], minval=0, maxval=255,
      dtype=tf.float32, seed=1)
    prediction_dict = model_object.predict(model_object.preprocess(test_input_image))
    loss = model_object.loss(prediction_dict)
    with self.test_session() as sess:
      sess.run([
        tf.global_variables_initializer(),
        tf.tables_initializer()])
      outputs = sess.run({'loss': loss})
      print(outputs['loss'])
//  [python]
def test_single_predictor_model_inference(self):
    model_proto = model_pb2.Model()
    text_format.Merge(SINGLE_PREDICTOR_MODEL_TEXT_PROTO, model_proto)
    model_object = model_builder.build(model_proto, False)
    test_groundtruth_text_list = [
      tf.constant(b'hello', dtype=tf.string),
      tf.constant(b'world', dtype=tf.string)]
    model_object.provide_groundtruth({'groundtruth_text': test_groundtruth_text_list})
    test_input_image = tf.random_uniform(
      shape=[2, 32, 100, 3], minval=0, maxval=255,
      dtype=tf.float32, seed=1)
    prediction_dict = model_object.predict(model_object.preprocess(test_input_image))
    recognition_dict = model_object.postprocess(prediction_dict)
    with self.test_session() as sess:
      sess.run([
        tf.initialize_all_variables(),
        tf.tables_initializer()])
      outputs = sess.run(recognition_dict)
      print(outputs)
//  [python]
def test_single_predictor_model_inference(self):
    model_proto = model_pb2.Model()
    text_format.Merge(SINGLE_PREDICTOR_MODEL_TEXT_PROTO, model_proto)
    model_object = model_builder.build(model_proto, False)
    test_groundtruth_text_list = [
      tf.constant(b'hello', dtype=tf.string),
      tf.constant(b'world', dtype=tf.string)]
    model_object.provide_groundtruth({'groundtruth_text': test_groundtruth_text_list})
    test_input_image = tf.random_uniform(
      shape=[2, 32, 100, 3], minval=0, maxval=255,
      dtype=tf.float32, seed=1)
    prediction_dict = model_object.predict(model_object.preprocess(test_input_image))
    recognition_dict = model_object.postprocess(prediction_dict)
    with self.test_session() as sess:
      sess.run([
        tf.global_variables_initializer(),
        tf.tables_initializer()])
      outputs = sess.run(recognition_dict)
      print(outputs)
//  [python]
def train():
    setup_logging()

    cfg = load_config()
    dataset = create_dataset(cfg)

    batch_spec = get_batch_spec(cfg)
    batch, enqueue_op, placeholders = setup_preloading(batch_spec)

    losses = pose_net(cfg).train(batch)
    total_loss = losses['total_loss']

    for k, t in losses.items():
        tf.summary.scalar(k, t)
    merged_summaries = tf.summary.merge_all()

    variables_to_restore = slim.get_variables_to_restore(include=["resnet_v1"])
    restorer = tf.train.Saver(variables_to_restore)
    saver = tf.train.Saver(max_to_keep=5)

    sess = tf.Session()

    coord, thread = start_preloading(sess, enqueue_op, dataset, placeholders)

    train_writer = tf.summary.FileWriter(cfg.log_dir, sess.graph)

    learning_rate, train_op = get_optimizer(total_loss, cfg)

    sess.run(tf.initialize_all_variables())
    sess.run(tf.local_variables_initializer())

    # Restore variables from disk.
    restorer.restore(sess, cfg.init_weights)

    max_iter = int(cfg.multi_step[-1][1])

    display_iters = cfg.display_iters
    cum_loss = 0.0
    lr_gen = LearningRate(cfg)

    for it in range(max_iter+1):
        current_lr = lr_gen.get_lr(it)
        [_, loss_val, summary] = sess.run([train_op, total_loss, merged_summaries],
                                          feed_dict={learning_rate: current_lr})
        cum_loss += loss_val
        train_writer.add_summary(summary, it)

        if it % display_iters == 0:
            average_loss = cum_loss / display_iters
            cum_loss = 0.0
            logging.info("iteration: {} loss: {} lr: {}"
                         .format(it, "{0:.4f}".format(average_loss), current_lr))

        # Save snapshot
        if (it % cfg.save_iters == 0 and it != 0) or it == max_iter:
            model_name = cfg.snapshot_prefix
            saver.save(sess, model_name, global_step=it)

    sess.close()
    coord.request_stop()
    coord.join([thread])
//  [python]
def train():
    setup_logging()

    cfg = load_config()
    dataset = create_dataset(cfg)

    batch_spec = get_batch_spec(cfg)
    batch, enqueue_op, placeholders = setup_preloading(batch_spec)

    losses = pose_net(cfg).train(batch)
    total_loss = losses['total_loss']

    for k, t in losses.items():
        tf.summary.scalar(k, t)
    merged_summaries = tf.summary.merge_all()

    variables_to_restore = slim.get_variables_to_restore(include=["resnet_v1"])
    restorer = tf.train.Saver(variables_to_restore)
    saver = tf.train.Saver(max_to_keep=5)

    sess = tf.Session()

    coord, thread = start_preloading(sess, enqueue_op, dataset, placeholders)

    train_writer = tf.summary.FileWriter(cfg.log_dir, sess.graph)

    learning_rate, train_op = get_optimizer(total_loss, cfg)

    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())

    # Restore variables from disk.
    restorer.restore(sess, cfg.init_weights)

    max_iter = int(cfg.multi_step[-1][1])

    display_iters = cfg.display_iters
    cum_loss = 0.0
    lr_gen = LearningRate(cfg)

    for it in range(max_iter+1):
        current_lr = lr_gen.get_lr(it)
        [_, loss_val, summary] = sess.run([train_op, total_loss, merged_summaries],
                                          feed_dict={learning_rate: current_lr})
        cum_loss += loss_val
        train_writer.add_summary(summary, it)

        if it % display_iters == 0:
            average_loss = cum_loss / display_iters
            cum_loss = 0.0
            logging.info("iteration: {} loss: {} lr: {}"
                         .format(it, "{0:.4f}".format(average_loss), current_lr))

        # Save snapshot
        if (it % cfg.save_iters == 0 and it != 0) or it == max_iter:
            model_name = cfg.snapshot_prefix
            saver.save(sess, model_name, global_step=it)

    sess.close()
    coord.request_stop()
    coord.join([thread])
//  [python]
def test_multi_predictors_model_training(self):
    model_proto = model_pb2.Model()
    text_format.Merge(MULTIPLE_PREDICTOR_MODEL_TEXT_PROTO, model_proto)
    model_object = model_builder.build(model_proto, True)
    test_groundtruth_text_list = [
      tf.constant(b'hello', dtype=tf.string),
      tf.constant(b'world', dtype=tf.string)]
    model_object.provide_groundtruth({'groundtruth_text': test_groundtruth_text_list})
    test_input_image = tf.random_uniform(
      shape=[2, 32, 100, 3], minval=0, maxval=255,
      dtype=tf.float32, seed=1)
    prediction_dict = model_object.predict(model_object.preprocess(test_input_image))
    loss = model_object.loss(prediction_dict)
    with self.test_session() as sess:
      sess.run([
        tf.initialize_all_variables(),
        tf.tables_initializer()])
      outputs = sess.run({'loss': loss})
      print(outputs['loss'])
//  [python]
def test_multi_predictors_model_training(self):
    model_proto = model_pb2.Model()
    text_format.Merge(MULTIPLE_PREDICTOR_MODEL_TEXT_PROTO, model_proto)
    model_object = model_builder.build(model_proto, True)
    test_groundtruth_text_list = [
      tf.constant(b'hello', dtype=tf.string),
      tf.constant(b'world', dtype=tf.string)]
    model_object.provide_groundtruth({'groundtruth_text': test_groundtruth_text_list})
    test_input_image = tf.random_uniform(
      shape=[2, 32, 100, 3], minval=0, maxval=255,
      dtype=tf.float32, seed=1)
    prediction_dict = model_object.predict(model_object.preprocess(test_input_image))
    loss = model_object.loss(prediction_dict)
    with self.test_session() as sess:
      sess.run([
        tf.global_variables_initializer(),
        tf.tables_initializer()])
      outputs = sess.run({'loss': loss})
      print(outputs['loss'])
//  [python]
def test_multi_predictor_model_inference(self):
    model_proto = model_pb2.Model()
    text_format.Merge(MULTIPLE_PREDICTOR_MODEL_TEXT_PROTO, model_proto)
    model_object = model_builder.build(model_proto, False)
    test_groundtruth_text_list = [
      tf.constant(b'hello', dtype=tf.string),
      tf.constant(b'world', dtype=tf.string)]
    model_object.provide_groundtruth({'groundtruth_text': test_groundtruth_text_list})
    test_input_image = tf.random_uniform(
      shape=[2, 32, 100, 3], minval=0, maxval=255,
      dtype=tf.float32, seed=1)
    prediction_dict = model_object.predict(model_object.preprocess(test_input_image))
    recognition_dict = model_object.postprocess(prediction_dict)
    with self.test_session() as sess:
      sess.run([
        tf.initialize_all_variables(),
        tf.tables_initializer()])
      outputs = sess.run(recognition_dict)
      print(outputs)
//  [python]
def test_multi_predictor_model_inference(self):
    model_proto = model_pb2.Model()
    text_format.Merge(MULTIPLE_PREDICTOR_MODEL_TEXT_PROTO, model_proto)
    model_object = model_builder.build(model_proto, False)
    test_groundtruth_text_list = [
      tf.constant(b'hello', dtype=tf.string),
      tf.constant(b'world', dtype=tf.string)]
    model_object.provide_groundtruth({'groundtruth_text': test_groundtruth_text_list})
    test_input_image = tf.random_uniform(
      shape=[2, 32, 100, 3], minval=0, maxval=255,
      dtype=tf.float32, seed=1)
    prediction_dict = model_object.predict(model_object.preprocess(test_input_image))
    recognition_dict = model_object.postprocess(prediction_dict)
    with self.test_session() as sess:
      sess.run([
        tf.global_variables_initializer(),
        tf.tables_initializer()])
      outputs = sess.run(recognition_dict)
      print(outputs)
//  [python]
def test_stn_multi_predictors_model_training(self):
    model_proto = model_pb2.Model()
    text_format.Merge(STN_MULTIPLE_PREDICTOR_MODEL_TEXT_PROTO, model_proto)
    model_object = model_builder.build(model_proto, True)
    test_groundtruth_text_list = [
      tf.constant(b'hello', dtype=tf.string),
      tf.constant(b'world', dtype=tf.string)]
    model_object.provide_groundtruth({'groundtruth_text': test_groundtruth_text_list})
    test_input_image = tf.random_uniform(
      shape=[2, 32, 100, 3], minval=0, maxval=255,
      dtype=tf.float32, seed=1)
    prediction_dict = model_object.predict(model_object.preprocess(test_input_image))
    loss = model_object.loss(prediction_dict)
    with self.test_session() as sess:
      sess.run([
        tf.initialize_all_variables(),
        tf.tables_initializer()])
      outputs = sess.run({'loss': loss})
      print(outputs['loss'])
//  [python]
def test_stn_multi_predictors_model_training(self):
    model_proto = model_pb2.Model()
    text_format.Merge(STN_MULTIPLE_PREDICTOR_MODEL_TEXT_PROTO, model_proto)
    model_object = model_builder.build(model_proto, True)
    test_groundtruth_text_list = [
      tf.constant(b'hello', dtype=tf.string),
      tf.constant(b'world', dtype=tf.string)]
    model_object.provide_groundtruth({'groundtruth_text': test_groundtruth_text_list})
    test_input_image = tf.random_uniform(
      shape=[2, 32, 100, 3], minval=0, maxval=255,
      dtype=tf.float32, seed=1)
    prediction_dict = model_object.predict(model_object.preprocess(test_input_image))
    loss = model_object.loss(prediction_dict)
    with self.test_session() as sess:
      sess.run([
        tf.global_variables_initializer(),
        tf.tables_initializer()])
      outputs = sess.run({'loss': loss})
      print(outputs['loss'])
//  [python]
def _tf_session():
  with tf.Session() as sess:
    sess.run([tf.initialize_all_variables(), tf.local_variables_initializer()])

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord, start=True)

    yield sess

    coord.request_stop()
    coord.join(threads)
//  [python]
def _tf_session():
  with tf.Session() as sess:
    sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord, start=True)

    yield sess

    coord.request_stop()
    coord.join(threads)
//  [python]
def test_stn_multi_predictor_model_inference(self):
    model_proto = model_pb2.Model()
    text_format.Merge(STN_MULTIPLE_PREDICTOR_MODEL_TEXT_PROTO, model_proto)
    model_object = model_builder.build(model_proto, False)
    test_groundtruth_text_list = [
      tf.constant(b'hello', dtype=tf.string),
      tf.constant(b'world', dtype=tf.string)]
    model_object.provide_groundtruth({'groundtruth_text': test_groundtruth_text_list})
    test_input_image = tf.random_uniform(
      shape=[2, 32, 100, 3], minval=0, maxval=255,
      dtype=tf.float32, seed=1)
    prediction_dict = model_object.predict(model_object.preprocess(test_input_image))
    recognition_dict = model_object.postprocess(prediction_dict)
    with self.test_session() as sess:
      sess.run([
        tf.initialize_all_variables(),
        tf.tables_initializer()])
      outputs = sess.run(recognition_dict)
      print(outputs)
//  [python]
def test_stn_multi_predictor_model_inference(self):
    model_proto = model_pb2.Model()
    text_format.Merge(STN_MULTIPLE_PREDICTOR_MODEL_TEXT_PROTO, model_proto)
    model_object = model_builder.build(model_proto, False)
    test_groundtruth_text_list = [
      tf.constant(b'hello', dtype=tf.string),
      tf.constant(b'world', dtype=tf.string)]
    model_object.provide_groundtruth({'groundtruth_text': test_groundtruth_text_list})
    test_input_image = tf.random_uniform(
      shape=[2, 32, 100, 3], minval=0, maxval=255,
      dtype=tf.float32, seed=1)
    prediction_dict = model_object.predict(model_object.preprocess(test_input_image))
    recognition_dict = model_object.postprocess(prediction_dict)
    with self.test_session() as sess:
      sess.run([
        tf.global_variables_initializer(),
        tf.tables_initializer()])
      outputs = sess.run(recognition_dict)
      print(outputs)
//  [python]
def __init__(self, X, y, X_val, y_val,
		x_size, y_size, h_size, type_in, sess,
		alpha=0.1, loss_type='lube', censor_R=False, 
		soften=100., lambda_in=10., sigma_in=0.5, 
		particle_n=40, v_prev=0.721,v_global=1.193, v_local=1.193, v_max=2.0,
		n_epoch=100, activation='relu', **kwargs):
		"""
		run particle swarm optimisation (pso) training scheme
		we'll use tensorflow stuff in order to mimic as close
		as possible the gradient trained versions
		some inputs are those required to initialise NN
		others are for the PSO scheme
		it will run off of the default loss of NN
		"""

		# initialise
		particles = []
		name = 0
		for i in range(0,particle_n):
			print('creating particle:',name)
			particles.append(TfNetwork(x_size, y_size, h_size, type_in, 
				alpha, loss_type, censor_R, 
				soften, lambda_in, sigma_in, bias_rand=True, activation=activation))
			name += 1

		sess.run(tf.global_variables_initializer())

		# initialisation loop
		best_global_loss = 1000000.
		best_global_params = np.array(1) # dummy array to start
		best_global_name = 0
		name = 0
		for p in particles:

			# sets best position, best params, velocity
			p.pso_init(X, y, sess, v_max, name)
			print('initalising particle:', name, '\tinitial loss:', p.best_loss)
			name += 1

			# initialise global best
			if p.best_loss < best_global_loss:
				best_global_loss = p.best_loss
				best_global_params = p.best_params
				best_global_name = p.name
				best_NN = p

		print('\ncurr best_global_name:',best_global_name)
		print('curr best_global_loss:',best_global_loss)	

		# main loop
		loss_log = []
		for epoch in range(0,n_epoch):
			string_in = '\n-- curr epoch: ' + str(epoch)
			print_time(string_in)
			for p in particles:
				# sets new values
				p.pso_update(X, y, sess, v_max, v_prev, 
					v_local, v_global, best_global_params)

				# print info
				print('\nep ',epoch, ' part ', p.name, '\ttrn loss ',round(p.curr_loss,5), end='\t')
				for i in range(0,len(p.metric)):
					print(p.metric_name[i], 
						sess.run(p.metric[i], feed_dict={p.X: X_val, p.y_true: y_val,
						p.W[0]: p.curr_params[0],
						p.W[1]: p.curr_params[1],
						# p.W[2]: p.curr_params[2],
						p.b[0]: p.curr_params[2],
						p.b[1]: p.curr_params[3]}),
						# p.b[2]: p.curr_params[5] }),
						end='\t')

				# update global best
				if p.best_loss < best_global_loss:
					best_global_loss = p.best_loss
					best_global_params = p.best_params
					best_global_name = p.name
					best_NN = p

				loss_log.append((epoch, p.name, p.curr_loss, best_global_loss))

			print('\ncurr best_global_name:',best_global_name)
			print('curr best_global_loss:',best_global_loss)

		print('best_global_params:',best_global_params)

		best_NN.set_params(best_global_params, sess)

		self.particle_n = particle_n
		self.loss_log = np.array(loss_log)
		self.best_NN = best_NN # return best NN
		self.loss_type = loss_type
		self.alpha = alpha
		self.v_prev = v_prev
		self.v_global = v_global
		self.v_local = v_local
		self.v_max = v_max
		self.best_global_loss = best_global_loss
//  [python]
def initialize(self, sess):
        self.sess = sess
        self.sess.run(tf.initialize_all_variables())
        self.actor_optimizer.sync()
        self.critic_optimizer.sync()
        self.sess.run(self.target_init_updates)
//  [python]
def initialize(self, sess):
        self.sess = sess
        self.sess.run(tf.global_variables_initializer())
        self.actor_optimizer.sync()
        self.critic_optimizer.sync()
        self.sess.run(self.target_init_updates)
//  [python]
def main(args):
    input_images = tf.placeholder(tf.float32, shape=[1, 480, 360, 4])

    with tf.variable_scope("Gen"):
        gen = UNet(4,4)
        output = tf.sigmoid(gen(input_images))

    global_step = tf.get_variable('global_step', initializer=0, trainable=False)

    init = tf.initialize_all_variables()
    sess = tf.Session()
    sess.run(init)

    saver = tf.train.Saver()
    if args.checkpoint is not None and os.path.exists(os.path.join(args.logdir, 'checkpoint')):
        if args.checkpoint == -1:#latest checkpoint
            saver.restore(sess, tf.train.latest_checkpoint(args.logdir))
        else:#Specified checkpoint
            saver.restore(sess, os.path.join(args.logdir, model_name+".ckpt-"+str(args.checkpoint)))
        logging.info('Model restored to step ' + str(global_step.eval(sess)))


    images, targets = [], []

    input_filename = args.input
    image = load_image(input_filename)
    print(image.shape)
    trimap = generate_trimap(args.object)

    image = np.array(image)
    trimap = np.array(trimap)[..., np.newaxis]
    print(image.shape)
    print(trimap.shape)
    image = np.concatenate((image, trimap), axis = 2).astype(np.float32) / 255

    result = sess.run(output, feed_dict={
            input_images: np.asarray([image]),
            })

    print(result.shape)
    image = Image.fromarray((result[0,:,:,:]*255).astype(np.uint8))
    image.save(args.output)
//  [python]
def main(args):
    input_images = tf.placeholder(tf.float32, shape=[1, 480, 360, 4])

    with tf.variable_scope("Gen"):
        gen = UNet(4,4)
        output = tf.sigmoid(gen(input_images))

    global_step = tf.get_variable('global_step', initializer=0, trainable=False)

    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)

    saver = tf.train.Saver()
    if args.checkpoint is not None and os.path.exists(os.path.join(args.logdir, 'checkpoint')):
        if args.checkpoint == -1:#latest checkpoint
            saver.restore(sess, tf.train.latest_checkpoint(args.logdir))
        else:#Specified checkpoint
            saver.restore(sess, os.path.join(args.logdir, model_name+".ckpt-"+str(args.checkpoint)))
        logging.info('Model restored to step ' + str(global_step.eval(sess)))


    images, targets = [], []

    input_filename = args.input
    image = load_image(input_filename)
    print(image.shape)
    trimap = generate_trimap(args.object)

    image = np.array(image)
    trimap = np.array(trimap)[..., np.newaxis]
    print(image.shape)
    print(trimap.shape)
    image = np.concatenate((image, trimap), axis = 2).astype(np.float32) / 255

    result = sess.run(output, feed_dict={
            input_images: np.asarray([image]),
            })

    print(result.shape)
    image = Image.fromarray((result[0,:,:,:]*255).astype(np.uint8))
    image.save(args.output)
//  [python]
def process_checkpoint(input_ckpt, output_ckpt_path, dense_layer):
    """
    This function loads a RN50 checkpoint with Dense layer as the final layer 
    and transforms the final dense layer into a 1x1 convolution layer. The weights
    of the dense layer are reshaped into weights of 1x1 conv layer.
    Args:
        input_ckpt: Path to the input RN50 ckpt which has dense layer as classification layer.
    Returns:
        None. New checkpoint with 1x1 conv layer as classification layer is generated.
    """
    with tf.Session() as sess:
        # Load all the variables
        all_vars = tf.train.list_variables(input_ckpt)
        # Capture the dense layer weights and reshape them to a 4D tensor which would be 
        # the weights of a 1x1 convolution layer. This code replaces the dense (FC) layer
        # to a 1x1 conv layer. 
        dense_layer_value=0.
        new_var_list=[]
        for var in all_vars:
            curr_var = tf.train.load_variable(input_ckpt, var[0])
            if var[0]==dense_layer:
                dense_layer_value = curr_var
            else:
                new_var_list.append(tf.Variable(curr_var, name=var[0]))
 
        dense_layer_shape = [1, 1, 2048, 1001]
        new_var_value = np.reshape(dense_layer_value, dense_layer_shape)
        new_var = tf.Variable(new_var_value, name=dense_layer)
        new_var_list.append(new_var)
        
        sess.run(tf.initialize_all_variables())
        tf.train.Saver(var_list=new_var_list).save(sess, output_ckpt_path, write_meta_graph=False, write_state=False)
        print ("Rewriting checkpoint completed")