예제 #1
0
def train():

    # Set size for use with tf.image.resize_images with align_corners=True.
    # For example,
    #   [1 4 7] =>   [1 2 3 4 5 6 7]    (length 3*(3-1)+1)
    # instead of
    # [1 4 7] => [1 1 2 3 4 5 6 7 7]  (length 3*3)
    final_score_sz = 255

    # build the computational graph of Siamese fully-convolutional network
    siamNet = siam.Siamese(8)
    # get tensors that will be used during training
    image, z_crops, x_crops, templates_z, scores, loss, train_step, distance_to_gt, summary = siamNet.build_tracking_graph_train(
        final_score_sz)

    # read tfrecodfile holding all the training data
    data_reader = read_training_dataset.myReader(700, 700, 3)
    batched_data = data_reader.read_tfrecord(os.path.join(
        "tfrecords", "training_dataset"),
                                             num_epochs=50,
                                             batch_size=8)

    # run trainer
    trainer(final_score_sz, batched_data, image, templates_z, scores, loss,
            train_step, distance_to_gt, z_crops, x_crops, siamNet, summary)
예제 #2
0
#data_reader = read_training_dataset.myReader(700, 700, 3)
#batched_data = data_reader.read_tfrecord(os.path.join("tfrecords", "training_dataset"), num_epochs = 50, batch_size = 8)    



#下面开始调试fcsiamese源代码
#hp, evaluation, run, env, design = parse_arguments()
## Set size for use with tf.image.resize_images with align_corners=True.
## For example,
##   [1 4 7] =>   [1 2 3 4 5 6 7]    (length 3*(3-1)+1)
## instead of
## [1 4 7] => [1 1 2 3 4 5 6 7 7]  (length 3*3)
#final_score_sz = hp.response_up * (design.score_sz - 1) + 1
#    
# build the computational graph of Siamese fully-convolutional network
siamNet = siam.Siamese(batch_size = 8)
# get tensors that will be used during training
siamNet.build_tracking_graph_train()
#image, z_crops, x_crops, templates_z, scores, loss, train_step, distance_to_gt, summary= siamNet.build_tracking_graph_train(final_score_sz, design, env, hp)
# 
## read tfrecodfile holding all the training data
#data_reader = src.read_training_dataset.myReader(design.resize_width, design.resize_height, design.channel)
#batched_data = data_reader.read_tfrecord(os.path.join(env.tfrecord_path, env.tfrecord_filename), num_epochs = design.num_epochs, batch_size = design.batch_size)
#    
## run trainer
#trainer(hp, run, design, final_score_sz, batched_data, image, templates_z, scores, loss, train_step, distance_to_gt,  z_crops, x_crops, siamNet, summary)
#
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
import tflearn

import siamese
import dataset

data = dataset.Dataset()
x1, x2, y = data.random("C:/Users/Мой Господин/PycharmProjects/practice_task1/data_odometry_gray/dataset/sequences/00/image_0",
                        "C:/Users/Мой Господин/PycharmProjects/practice_task1/00.txt", 100)

network = siamese.Siamese()
regression = tflearn.regression(network.loss(), optimizer='sgd', metric='accuracy', learning_rate=0.002)
model = tflearn.DNN(regression)
model.fit([x1, x2], y, batch_size=2, show_metric=True)
예제 #4
0
def get_debiasing_projection(num_classifiers: int,
                             input_dim: int,
                             is_autoregressive: bool,
                             X_train: np.ndarray,
                             X_dev: np.ndarray,
                             dropout_rate=0,
                             device="cpu") -> np.ndarray:
    """
    :param classifier_class: the sklearn classifier class (SVM/Perceptron etc.)
    :param cls_params: a dictionary, containing the params for the sklearn classifier
    :param num_classifiers: number of iterations (equivalent to number of dimensions to remove)
    :param input_dim: size of input vectors
    :param is_autoregressive: whether to train the ith classiifer on the data projected to the nullsapces of w1,...,wi-1
    :param min_accuracy: above this threshold, ignore the learned classifier
    :param X_train: ndarray, training vectors
    :param Y_train: ndarray, training labels (protected attributes)
    :param X_dev: ndarray, eval vectors
    :param Y_dev: ndarray, eval labels (protected attributes)
    :param by_class: if true, at each iteration sample one main-task label, and extract the protected attribute only from vectors from this class
    :param T_train_main: ndarray, main-task train labels
    :param Y_dev_main: ndarray, main-task eval labels
    :param dropout_rate: float, default: 0 (note: not recommended to be used with autoregressive=True)
    :return: P, the debiasing projection; rowspace_projections, the list of all rowspace projection; Ws, the list of all calssifiers.
    """
    if dropout_rate > 0 and is_autoregressive:
        warnings.warn(
            "Dropout is not recommended with autoregressive training, as it violates the propety w_i.dot(w_(i+1)) = 0 that is necessary for a mathematically valid projection."
        )

    I = np.eye(input_dim)

    X_train_cp = X_train.copy()
    X_dev_cp = X_dev.copy()
    rowspace_projections = []
    Ws = []

    pbar = tqdm(range(num_classifiers))
    for i in pbar:
        print("======================================")
        clf = siamese.Siamese(X_train_cp,
                              X_dev_cp,
                              100,
                              batch_size=1000,
                              dropout_rate=dropout_rate,
                              device=device).to(device)

        acc = clf.train_network(20)
        pbar.set_description("iteration: {}, accuracy: {}".format(i, acc))

        W = clf.get_weights()

        Ws.append(W)
        P_rowspace_wi = get_rowspace_projection(
            W)  # projection to W's rowspace
        rowspace_projections.append(P_rowspace_wi)

        if is_autoregressive:
            """
            to ensure numerical stability, explicitly project to the intersection of the nullspaces found so far (instaed of doing X = P_iX,
            which is problematic when w_i is not exactly orthogonal to w_i-1,...,w1, due to e.g inexact argmin calculation).
            """
            # use the intersection-projection formula of Ben-Israel 2013 http://benisrael.net/BEN-ISRAEL-NOV-30-13.pdf:
            # N(w1)∩ N(w2) ∩ ... ∩ N(wn) = N(P_R(w1) + P_R(w2) + ... + P_R(wn))

            Q = np.sum(rowspace_projections, axis=0)
            P = I - get_rowspace_projection(Q)

            # project

            X_train_cp = (P.dot(X_train.T)).T
            X_dev_cp = (P.dot(X_dev.T)).T
    """
    calculae final projection matrix P=PnPn-1....P2P1
    since w_i.dot(w_i-1) = 0, P2P1 = I - P1 - P2 (proof in the paper); this is more stable.
    by induction, PnPn-1....P2P1 = I - (P1+..+PN). We will use instead Ben-Israel's formula to increase stability,
    i.e., we explicitly project to intersection of all nullspaces (not very critical)
    """

    Q = np.sum(rowspace_projections, axis=0)
    P = I - get_rowspace_projection(Q)

    return P, rowspace_projections, Ws
예제 #5
0
def feature_extraction():
    """
    This method restores a TensorFlow checkpoint file (.ckpt) and rebuilds inference
    model with restored parameters. From then on you can basically use that model in
    any way you want, for instance, feature extraction, finetuning or as a submodule
    of a larger architecture. However, this method should extract features from a
    specified layer and store them in data files such as '.h5', '.npy'/'.npz'
    depending on your preference. You will use those files later in the assignment.

    Args:
        [optional]
    Returns:
        None
    """

    ########################
    # PUT YOUR CODE HERE  #
    ########################
    #model = convnet.ConvNet(n_classes=10)

    #x = tf.placeholder(tf.float32, [None, 32, 32, 3])
    #y = tf.placeholder(tf.float32, [None, 10])

    #logits = model.inference(x)

    #accuracy = model.accuracy(logits, y)

    #init = tf.initialize_all_variables()

    #saver = tf.train.Saver()

    with tf.Session() as sess:
  	#saver.restore(sess, "checkpoints/convnet")

    	cifar10 = cifar10_utils.get_cifar10('cifar10/cifar-10-batches-py')
    	x_test, y_test = cifar10.test.images, cifar10.test.labels

    	#acc, fc2_out, fc1_out, flatten = sess.run([accuracy, model.fc2_out, model.fc1_out, model.flatten], feed_dict={x: x_test, y: y_test})
    	#print('Accuracy: ' + str(acc))
    
    
    	tsne = manifold.TSNE(n_components=2, random_state=0)
    	#fc2_tsne = tsne.fit_transform(np.squeeze(fc2_out))
	#fc1_tsne = tsne.fit_transform(np.squeeze(fc1_out))
	#flatten_tsne = tsne.fit_transform(np.squeeze(flatten))
    	
	#fc2_tsne = np.load('fc2_tsne')
	#fc1_tsne = np.load('fc1_tsne')
	#flatten_tsne = np.load('flatten_tsne')
	#labels = np.argmax(y_test, axis=1)
	
    	#plt.figure(figsize=(25, 20))  #in inches

        #x = fc2_tsne[:,0]/np.linalg.norm(fc2_tsne[:,0])
	#y = fc2_tsne[:,1]/np.linalg.norm(fc2_tsne[:,1])
        #plt.scatter(x, y, c=labels)
	#plt.colorbar()
    	#plt.savefig('fc2_tsne_norm.png')

	#plt.figure(figsize=(25, 20))

        #x = fc1_tsne[:,0]/np.linalg.norm(fc1_tsne[:,0])
        #y = fc1_tsne[:,1]/np.linalg.norm(fc1_tsne[:,1])
        #plt.scatter(x, y, c=labels)
	#plt.colorbar()
        #plt.savefig('fc1_tsne_norm.png')
	
	#plt.figure(figsize=(25, 20))  #in inches

        #x = flatten_tsne[:,0]/np.linalg.norm(flatten_tsne[:,0])
        #y = flatten_tsne[:,1]/np.linalg.norm(flatten_tsne[:,1])
        #plt.scatter(x, y, c=labels)
	#plt.colorbar()
        #plt.savefig('flatten_tsne_norm.png')

	#fc2_tsne.dump('fc2_tsne')
	#fc1_tsne.dump('fc1_tsne')
	#flatten_tsne.dump('flatten_tsne')
        #print('fc1 scores: ')
	#_classify(fc1_tsne, labels)
	#print('fc2 scores: ')
	#_classify(fc2_tsne, labels)
	#print('flatten layer scores: ')
	#_classify(flatten_tsne, labels)


    model = siamese.Siamese()
    x1 = tf.placeholder(tf.float32, [None, 32, 32, 3])

    model.inference(x1, reuse=False)

    with tf.Session() as siamese_sess:
        init = tf.initialize_all_variables()

        saver = tf.train.Saver()
        saver.restore(siamese_sess, "checkpoints/siamese")


        dataset = cifar10_utils.get_cifar10('cifar10/cifar-10-batches-py')
        x_test, y_test = cifar10.test.images, cifar10.test.labels

        
        l2_out = siamese_sess.run(model.l2_out, feed_dict={x1: x_test})

        siamese_tsne = tsne.fit_transform(np.squeeze(l2_out))
        plt.figure(figsize=(25, 20))  #in inches
        labels = np.argmax(y_test, axis=1)

        x = siamese_tsne[:,0]/np.linalg.norm(siamese_tsne[:,0])
        y = siamese_tsne[:,1]/np.linalg.norm(siamese_tsne[:,1])
        plt.scatter(x, y, c=labels)
        plt.colorbar()
        plt.savefig('siamese_l2out.png')

        siamese_tsne.dump('siamese_tsne')
        print('siamese L2 scores: ')
        _classify(siamese_tsne, labels)
예제 #6
0
def train_siamese():
    """
    Performs training and evaluation of Siamese model.

    First define your graph using class Siamese and its methods. Then define
    necessary operations such as trainer (train_step in this case), savers
    and summarizers. Finally, initialize your model within a tf.Session and
    do the training.

    ---------------------------
    How to evaluate your model:
    ---------------------------
    On train set, it is fine to monitor loss over minibatches. On the other
    hand, in order to evaluate on test set you will need to create a fixed
    validation set using the data sampling function you implement for siamese
    architecture. What you need to do is to iterate over all minibatches in
    the validation set and calculate the average loss over all minibatches.

    ---------------------------------
    How often to evaluate your model:
    ---------------------------------
    - on training set every print_freq iterations
    - on test set every eval_freq iterations

    ------------------------
    Additional requirements:
    ------------------------
    Also you are supposed to take snapshots of your model state (i.e. graph,
    weights and etc.) every checkpoint_freq iterations. For this, you should
    study TensorFlow's tf.train.Saver class. For more information, please
    checkout:
    [https://www.tensorflow.org/versions/r0.11/how_tos/variables/index.html]
    """

    # Set the random seeds for reproducibility. DO NOT CHANGE.
    tf.set_random_seed(42)
    np.random.seed(42)

    ########################
    # PUT YOUR CODE HERE  #
    ########################
    n_tuples = 500
    _size = FLAGS.batch_size
    f_same = 0.2
    
    cifar10 = cifar10_siamese_utils.get_cifar10('cifar10/cifar-10-batches-py')
    
    dataset = cifar10_siamese_utils.create_dataset(source="Test", num_tuples=n_tuples, batch_size=_size, fraction_same=f_same)
    

    model = siamese.Siamese()
    x1 = tf.placeholder(tf.float32, [None, 32, 32, 3])
    x2 = tf.placeholder(tf.float32, [None, 32, 32, 3])
    
    y = tf.placeholder(tf.float32, [None, 1])
    
    channel1_out = model.inference(x1, reuse=False)
        
    channel2_out = model.inference(x2, reuse=True)
    
    loss = model.loss(channel1_out, channel2_out, y, margin=1)
  
    step = train_step(loss)
    
    init = tf.initialize_all_variables()
  
    saver = tf.train.Saver()
    sess = tf.Session()
    sess.run(init)
  
    merged = tf.merge_all_summaries()

    train_writer = tf.train.SummaryWriter(FLAGS.log_dir + '/train',
                                      sess.graph)
    test_writer = tf.train.SummaryWriter(FLAGS.log_dir + '/test')

    test_loss = 0. 
    for i in range(FLAGS.max_steps):
      batch_x1, batch_x2, batch_labels = cifar10.train.next_batch(FLAGS.batch_size)
      summary, _, l = sess.run([merged, step, loss], feed_dict={x1: batch_x1, x2: batch_x2, y:batch_labels})
      if i % FLAGS.print_freq == 0:
          print('iteration: ' + str(i)+ ' Train Loss: ' + str(l))
          train_writer.add_summary(summary, i)
      

      if i % FLAGS.eval_freq == 0:
          test_loss = 0.          
          for _tuple in dataset:
              (x1_test, x2_test, y_test) = _tuple
              summary, l = sess.run([merged, loss], feed_dict={x1: x1_test, x2: x2_test, y: y_test })
              test_loss += l
          test_loss /= n_tuples
          print('iteration: ' + str(i)+ ' Test Loss: ' + str(test_loss))
          test_writer.add_summary(summary, i)

#      if i % FLAGS.checkpoint_freq == 0:
#          save_path = saver.save(sess, "checkpoints/siamese{:d}".format(i))

    save_path = saver.save(sess, "checkpoints/siamese")