Exemplo n.º 1
0
import numpy as np
import os

from datagenerator import ImageDataGenerator
from datetime import datetime
from tensorflow.contrib.data import Iterator
from tensorflow.contrib.data import Dataset
from helper import get_dataset

import siamese

# prepare data and tf.session
sess = tf.InteractiveSession()

# setup siamese network
siamese_model = siamese.siamese_network()
train_step = tf.train.AdamOptimizer(0.001).minimize(siamese_model.loss)
saver = tf.train.Saver()
#tf.initialize_all_variables().run()
# if you just want to load a previously trainmodel?

# model_ckpt = 'model.ckpt'
# if os.path.isfile(model_ckpt):
#     input_var = None
#     while input_var not in ['yes', 'no']:
#         input_var = raw_input("We found model.ckpt file. Do you want to load it [yes/no]?")
#     if input_var == 'yes':
#         new = False

############################ INPUT PIPELINE ############################
train_file = 'word_train_2_classes.txt'
Exemplo n.º 2
0
def main():
    ### Parameters
    percent_train = 0.7
    feature_length = 10
    capacity = 2000
    batch_size = 128
    num_threads = 8

    #Currently due to network using all convolutions, only works correctly with 224 x 224 images which is origional alexnet input.
    height = 224
    width = 224
    channels = 3

    margin = 1.0
    learning_rate=1E-5
    dropout_keep_prob = 0.5

    epochs = 1000
    batches_per_epoch = 100


    # Record Directory
    time_string = strftime("%Y-%m-%d_%H-%M-%S", gmtime())
    store_directory = os.path.join('records',time_string)

    os.mkdir(store_directory)

    #Get data split into training and testing data.
    train_data, test_data = get_split_data(percent_train=percent_train)

    #Save the split information for future use.
    with open(os.path.join(store_directory, "train_data_names_labels.obj"), 'wb') as train_data_names_labels_file:
        pickle.dump(train_data, train_data_names_labels_file)

    with open(os.path.join(store_directory, "test_data_names_labels.obj"), 'wb') as test_data_names_labels:
        pickle.dump(test_data, test_data_names_labels)

    train_image_names, train_image_labels= zip(*train_data)
    test_image_names, test_image_labels= zip(*test_data)

    batch_images1, batch_images2, batch_labels, _, _ = input_data_label(train_image_names, train_image_labels, capacity, height, width, batch_size, num_threads)
    test_batch_images1, test_batch_images2, test_batch_labels, _, _ = input_data_label(test_image_names, test_image_labels, capacity, height, width, batch_size, num_threads)

    with siamese_network(height, width, channels, learning_rate=learning_rate, dropout_keep_prob=dropout_keep_prob, contrast_loss_margin=margin, feature_length=feature_length) as network:
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(sess=network.sess, coord=coord)

        model_dir = os.path.join(store_directory, "model")
        if not os.path.isdir(model_dir):
            os.mkdir(model_dir)

        with open(os.path.join(store_directory, 'results.csv'), 'w', newline='\n') as csvfile:
            spamwriter = csv.writer(csvfile)
            spamwriter.writerow(['iteration', 'average_train_loss', 'average_train_acc', 'average_test_loss', 'average_test_acc', 'best_training_acc',
                 'best_training_threshold', 'best_testing_acc', 'best_testing_threshold'])

            for e in tqdm(range(epochs), desc="Training progress"):
                t = tqdm(range(batches_per_epoch), desc="Epoch %i" % e, mininterval=0.5)

                #Training Step
                all_dist = []
                all_labels = []
                total_loss = 0
                for batch_counter in t:
                    images1, images2, labels = network.sess.run([batch_images1, batch_images2, batch_labels])
                    opt, l, dist = network.train(images1, images2, labels)

                    all_dist.extend(dist)
                    all_labels.extend(labels)

                    total_loss += l

                average_train_loss = total_loss/batches_per_epoch
                print("average_train_loss:", average_train_loss)

                training_acc, best_training_acc, best_training_threshold = find_optimal_threshold(all_dist, all_labels, 0, 2.0, 0.01)
                print('best_training_acc:', best_training_acc, ' best_training_threshold:',best_training_threshold)

                # Save session
                network.save(os.path.join(model_dir,'model'), global_step=e)

                total_test_loss = 0

                #Test Evaluation
                all_dist = []
                all_labels = []
                test_epochs = 10
                for test_batch in tqdm(range(test_epochs)):
                    images1, images2, labels = network.sess.run([test_batch_images1, test_batch_images2, test_batch_labels])
                    l, sq_dist, feat1, feat2 = network.test(images1,images2, labels=labels)
                    dist = np.sqrt(sq_dist)
                    all_dist.extend(dist)
                    all_labels.extend(labels)

                    total_test_loss += l

                average_test_loss = total_test_loss/test_epochs
                testing_acc, best_testing_acc, best_testing_threshold = find_optimal_threshold(all_dist, all_labels,
                                                                                                  0, 2.0, 0.01)

                print('best_testing_acc:', best_testing_acc, ' best_testing_threshold:',best_testing_threshold)
                spamwriter.writerow([e, average_train_loss, average_test_loss, best_training_acc, best_training_threshold, best_testing_acc, best_testing_threshold])
                csvfile.flush()

            print('Finished training!')

        coord.request_stop()




    print("hi")
from util import get_test_results

from data_generator_for_siamese import ImageDataGenerator
from datetime import datetime
from tensorflow.contrib.data import Iterator
from tensorflow.contrib.data import Dataset
from helper import get_dataset

import siamese

# prepare data and tf.session
sess = tf.InteractiveSession()
num_classes = 999
batch_size = 32
# setup siamese network
siamese_model = siamese.siamese_network(batch_size)
train_step = tf.train.AdamOptimizer(0.001).minimize(siamese_model.loss)
saver = tf.train.Saver()
# tf.initialize_all_variables().run()
# if you just want to load a previously trainmodel?

# model_ckpt = 'model.ckpt'
# if os.path.isfile(model_ckpt):
#     input_var = None
#     while input_var not in ['yes', 'no']:
#         input_var = raw_input("We found model.ckpt file. Do you want to load it [yes/no]?")
#     if input_var == 'yes':
#         new = False

############################ INPUT PIPELINE ############################
train_file = 'word_train.txt'
Exemplo n.º 4
0
def main():
    store_directory = os.path.join('records', 'best')
    model_dir = os.path.join(store_directory, 'model')

    height = 224
    width = 224
    channels = 3
    learning_rate = 0
    dropout_keep_prob = 1.0
    margin = 1
    feature_length = 10
    capacity = 2000
    batch_size = 100
    num_threads = 8

    with siamese_network(height,
                         width,
                         channels,
                         learning_rate=learning_rate,
                         dropout_keep_prob=dropout_keep_prob,
                         contrast_loss_margin=margin,
                         feature_length=feature_length) as network:
        network.load(tf.train.latest_checkpoint(os.path.join(model_dir)))

        # Save the split information for future use.
        with open(os.path.join(store_directory, "train_data_names_labels.obj"),
                  'rb') as train_data_names_labels_file:
            train_data = pickle.load(train_data_names_labels_file)

        with open(os.path.join(store_directory, "test_data_names_labels.obj"),
                  'rb') as test_data_names_labels:
            test_data = pickle.load(test_data_names_labels)

        train_image_names, train_image_labels = zip(*train_data)
        test_image_names, test_image_labels = zip(*test_data)

        train_batch_images1, train_batch_images2, batch_labels, train_batch_label1, train_batch_label2 = input_data_label(
            train_image_names,
            train_image_labels,
            capacity,
            height,
            width,
            batch_size,
            num_threads,
            shuffle=False)
        test_batch_images1, test_batch_images2, test_batch_labels, test_batch_label1, test_batch_label2 = input_data_label(
            test_image_names,
            test_image_labels,
            capacity,
            height,
            width,
            batch_size,
            num_threads,
            shuffle=False)
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(sess=network.sess, coord=coord)

        train_feats = []
        train_labels = []
        for i in tqdm(range(len(train_image_names) // batch_size // 2)):
            images1, images2, labels, label1, label2 = network.sess.run([
                train_batch_images1, train_batch_images2, batch_labels,
                train_batch_label1, train_batch_label2
            ])
            feat1 = network.inference(images1)
            feat2 = network.inference(images2)
            train_feats.extend(feat1.tolist())
            train_feats.extend(feat2.tolist())
            train_labels.extend(label1.tolist())
            train_labels.extend(label2.tolist())
        train_feats = np.array(train_feats)

        all_dist = []
        all_labels = []
        test_feats = []
        test_labels = []
        for i in tqdm(range(len(test_image_labels) // batch_size // 2)):
            images1, images2, labels, label1, label2 = network.sess.run([
                test_batch_images1, test_batch_images2, test_batch_labels,
                test_batch_label1, test_batch_label2
            ])
            l, sq_dist, feat1, feat2 = network.test(images1,
                                                    images2,
                                                    labels=labels)
            test_feats.extend(feat1)
            test_feats.extend(feat2)
            test_labels.extend(label1)
            test_labels.extend(label2)

            dist = np.sqrt(sq_dist)
            all_dist.extend(dist)
            all_labels.extend(labels)

        test_feats = np.array(test_feats)

        accs = [
            np.mean((all_dist > threshold) == all_labels)
            for threshold in np.arange(0, 2.0, 0.01)
        ]
        print(accs)

        pca = PCA(n_components=2)
        pca_train_feat = pca.fit_transform(train_feats)
        pca_test_feat = pca.transform(test_feats)

        plot(pca_train_feat, train_labels)
        plot(pca_test_feat, test_labels)