Example #1
0
def main():
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    DATASET_DIRECTORY = '../data_part1'

    X, y, X_hidden = dataset_manip.load_dataset(DATASET_DIRECTORY)
    num_classes = len(set(y))

    print('X.shape = ' + str(X.shape))
    print('X_hidden.shape = ' + str(X_hidden.shape))

    ens = Ensemble(input_shape=(77, 71, 1),
                   num_classes=10,
                   num_models=11,
                   batch_size=512,
                   path='./ensemble_files',
                   load=False)
    ens.train(X=X, y=y, epochs_per_model=300, split_rate=0.9)
    print(ens.measure_accuracy(X, y))

    return
    X_train, X_validation, y_train, y_validation = dataset_manip.split_dataset(
        X, y, rate=0.5)

    model = Model(image_shape=X.shape[1:],
                  num_classes=num_classes,
                  model_path='./model_files/model',
                  batch_size=512,
                  first_run=True)  # 1250

    model.train(X_train, y_train, X_validation, y_validation, 500)
    model.train_unsupervised(X_hidden, X_validation, y_validation, 200)

    print('Final Accuracy: {}'.format(
        model.measure_accuracy(X_validation, y_validation)))
Example #2
0
	def train(self, X, y, epochs_per_model, split_rate):
		num_samples = X.shape[0]
		
		for i in range(self.num_models):
			print('Training model {}'.format(i))
			permutation = np.random.permutation(num_samples)
			X = X[permutation]
			y = y[permutation]
			X_train, X_validation, y_train, y_validation = dataset_manip.split_dataset(X, y, rate = split_rate)
			self.models[i].train(X_train, y_train, X_validation, y_validation, epochs_per_model)
Example #3
0
def main():
	os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
	DATASET_DIRECTORY = '/home/felipe/tcv3/data_part1'
	
	X, y, X_hidden = dataset_manip.load_dataset(DATASET_DIRECTORY)
	num_classes = len(set(y))
	
	print('X.shape = ' + str(X.shape))
	print('X_hidden.shape = ' + str(X_hidden.shape))
	
	X_train, X_validation, y_train, y_validation = dataset_manip.split_dataset(X, y, rate = 0.8)
	model = Model(image_shape = X.shape[1 : ], num_classes = num_classes, model_path = './model_files/model', batch_size = 1250, first_run = True)
	model.train(X_train, y_train, X_validation, y_validation, 200)
	print(model.measure_accuracy(X_validation, y_validation))
Example #4
0
def main():
	os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
	DATASET_DIRECTORY = '../data_part1'
	
	X, y, X_hidden = dataset_manip.load_dataset(DATASET_DIRECTORY)
	
	X = resize_image_set(X, (64, 64, 1))
	X_hidden = resize_image_set(X_hidden, (64, 64, 1))
	
	print('X.shape = ' + str(X.shape))
	print('X_hidden.shape = ' + str(X_hidden.shape))
	
	X_train, X_validation, y_train, y_validation = dataset_manip.split_dataset(X, y, rate = 0.9)
	
	model = autoencoder.Autoencoder(input_shape = X.shape[1 : ], model_path = './model_files/autoencoder_model', batch_size = 8, first_run = True)
	print(model.measure_loss(X_validation))
	model.train(X_train, X_validation, num_epochs = 100)
	print(model.measure_loss(X_validation))
def main():
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    DATASET_DIRECTORY = '/home/felipe/tcv3/data_part1'
    TRAIN_RATE = 0.8
    NUM_EPOCHS = 2000
    BATCH_SIZE = 500
    MODEL_PATH = ''

    X, y, X_hidden = dataset_manip.load_dataset(DATASET_DIRECTORY)
    num_classes = len(set(y))

    X_train, X_validation, y_train, y_validation = dataset_manip.split_dataset(
        X, y, rate=TRAIN_RATE)

    graph = define_model({
        'num_inputs': X.shape[1],
        'num_outputs': num_classes,
        'learning_rate': 1e-3,
        'decay_steps': 300,
        'decay_rate': 0.95,
        'staircase': True,
        'activation_function': tf.nn.softmax
    })

    best_acc, predictions = train_model(graph, X_train, X_validation, y_train,
                                        y_validation, NUM_EPOCHS, BATCH_SIZE,
                                        0.5, X_hidden)

    _, test_images = dataset_manip.load_paths(DATASET_DIRECTORY)

    result = list(zip(test_images, predictions[0]))

    result.sort(key=lambda x: int(os.path.splitext(os.path.basename(x[0]))[0]))
    print('Number of predictions: {}'.format(len(result)))

    with open('logistic_regression.txt', 'w') as f:
        for image_name, prediction in result:
            f.write('{} {}\n'.format(os.path.basename(image_name), prediction))
Example #6
0
def main():
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    DATASET_DIRECTORY = '/home/felipe/tcv3/data_part1'
    TRAIN_RATE = 0.8
    NUM_EPOCHS = 2000
    BATCH_SIZE = 500
    HIDDEN_LAYER_SIZE = 400

    X, y, X_hidden = dataset_manip.load_dataset(DATASET_DIRECTORY)
    num_classes = len(set(y))

    X_train, X_validation, y_train, y_validation = dataset_manip.split_dataset(
        X, y, rate=TRAIN_RATE)

    #hl_sizes = []
    #learning_rates = []

    model = OneHiddenLayer(X.shape[1], num_classes, HIDDEN_LAYER_SIZE, 5e0,
                           200, 0.88)
    #model = OneHiddenLayer(X.shape[1], num_classes, HIDDEN_LAYER_SIZE, 4e0, 300, 0.85)
    #model.train(X_train, y_train, X_validation, y_validation, BATCH_SIZE, NUM_EPOCHS)

    best_acc, predictions = model.train(X_train, y_train, X_validation,
                                        y_validation, BATCH_SIZE, NUM_EPOCHS,
                                        X_hidden)
    print('Best: {}'.format(best_acc))

    _, test_images = dataset_manip.load_paths(DATASET_DIRECTORY)

    result = list(zip(test_images, predictions[0]))

    result.sort(key=lambda x: int(os.path.splitext(os.path.basename(x[0]))[0]))
    print('Number of predictions: {}'.format(len(result)))

    with open('multilayer_perceptron.txt', 'w') as f:
        for image_name, prediction in result:
            f.write('{} {}\n'.format(os.path.basename(image_name), prediction))
Example #7
0
def main():
    DATASET_DIRECTORY = '/home/felipe/tcv3/data_part1'
    TRAIN_RATE = 0.8
    NUM_EPOCHS = 1000
    BATCH_SIZE = 500
    MODEL_PATH = ''

    X, y, X_hidden = dataset_manip.load_dataset(DATASET_DIRECTORY)
    num_classes = len(set(y))

    X_train, X_validation, y_train, y_validation = dataset_manip.split_dataset(
        X, y, rate=TRAIN_RATE)

    graph = tf.Graph()
    with graph.as_default():
        global_step = tf.Variable(0, trainable=False)

        X_tf = tf.placeholder(tf.float32, shape=(None, X.shape[1]))
        y_tf = tf.placeholder(tf.int64, shape=(None, ))
        y_one_hot = tf.one_hot(y_tf, num_classes)
        learning_rate = tf.train.exponential_decay(1e-3,
                                                   global_step,
                                                   250,
                                                   0.93,
                                                   staircase=True)

        out = tf.layers.dense(X_tf, num_classes, activation=tf.nn.softmax)
        loss = tf.reduce_mean(tf.reduce_sum((y_one_hot - out)**2))

        train_op = tf.train.GradientDescentOptimizer(
            learning_rate=learning_rate).minimize(loss,
                                                  global_step=global_step)
        result = tf.argmax(out, 1)
        accuracy = tf.reduce_mean(tf.cast(tf.equal(result, y_tf), tf.float32))

        saver = tf.train.Saver()

    def train(X_train, y_train):
        best_acc = 0

        with tf.Session(graph=graph) as session:
            session.run(tf.global_variables_initializer())

            index = 0
            X_remainder = np.empty(shape=(0, X_train.shape[1]))
            y_remainder = np.empty(shape=(0, ))
            num_samples = X_train.shape[0]

            for epoch in range(1, NUM_EPOCHS + 1):
                print('Epoch {}'.format(epoch))

                while index < num_samples:
                    session.run(
                        [train_op],
                        feed_dict={
                            X_tf: X_train[index:index + BATCH_SIZE],
                            y_tf: y_train[index:index + BATCH_SIZE]
                        })

                    if index + BATCH_SIZE > num_samples:
                        X_remainder = np.copy(X_train[index:])
                        y_remainder = np.copy(y_train[index:])

                    index += BATCH_SIZE

                index = (index % num_samples)

                p = np.random.permutation(num_samples)
                X_train = X_train[p]
                y_train = y_train[p]

                if (X_remainder.shape[0] > 0):
                    X_remainder = np.concatenate(
                        (X_remainder, X_train[:index]), axis=0)
                    y_remainder = np.concatenate(
                        (y_remainder, y_train[:index]), axis=0)
                    session.run([train_op],
                                feed_dict={
                                    X_tf: X_remainder,
                                    y_tf: y_remainder
                                })

                    assert X_remainder.shape[0] == BATCH_SIZE
                    assert y_remainder.shape[0] == BATCH_SIZE

                print(session.run(learning_rate))
                val_loss, val_accuracy = session.run([loss, accuracy],
                                                     feed_dict={
                                                         X_tf: X_validation,
                                                         y_tf: y_validation
                                                     })
                if (val_accuracy > best_acc):
                    #saver.save(session, './my-model')
                    best_acc = val_accuracy

                print(
                    'Validation Loss: {:.3f}\tValidation Accuracy: {:.3f}\t Best Acc: {:.3f}'
                    .format(val_loss, val_accuracy, best_acc))

            print(best_acc)

    train(X_train, y_train)