Ejemplo n.º 1
0
def main(unused_argv):
    batchsize_video = 1

    dir_path = os.path.dirname(os.path.realpath(__file__))
    train_folder = os.path.join(dir_path, "train/")
    test_folder = os.path.join(dir_path, "test/")

    train_target = os.path.join(dir_path, 'train_target.csv')
    my_solution_file = os.path.join(dir_path, 'solution.csv')

    tf_record_dir = os.path.join(dir_path, 'tf_records')
    os.makedirs(tf_record_dir, exist_ok=True)

    tf_record_train = os.path.join(tf_record_dir, 'train' + '.tfrecords')
    tf_record_test = os.path.join(tf_record_dir, 'test' + '.tfrecords')

    if not os.path.exists(tf_record_train):
        x_train = get_videos_from_folder(train_folder)
        y_train = get_target_from_csv(train_target)
        save_tf_record(x_train, tf_record_train, y=y_train)

    if not os.path.exists(tf_record_test):
        x_test = get_videos_from_folder(test_folder)
        save_tf_record(x_test, tf_record_test)

    # Create the Estimator
    classifier = tf.estimator.Estimator(model_fn=cnn_model_fn,
                                        model_dir="\\tmp\\model")

    # Set up logging for predictions
    # Log the values in the "Softmax" tensor with label "probabilities"
    tensors_to_log = {"probabilities": "softmax_tensor"}
    logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log,
                                              every_n_iter=50)

    print('{}: Train'.format(datetime.now().strftime("%H:%M:%S")))
    # Train the model
    classifier.train(input_fn=lambda: input_fn_from_dataset(
        tf_record_train, batch_size=batchsize_video),
                     max_steps=1,
                     hooks=[logging_hook])

    print('{}: Evaluate'.format(datetime.now().strftime("%H:%M:%S")))
    # Evaluate the model and print results
    #eval_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": eval_data}, y=eval_labels, num_epochs=1, shuffle=False)
    eval_results = classifier.evaluate(input_fn=lambda: input_fn_from_dataset(
        tf_record_test, batch_size=batchsize_video))
    print(eval_results)

    print('{}: Predict'.format(datetime.now().strftime("%H:%M:%S")))
    pred = classifier.predict(
        input_fn=lambda: input_fn_from_dataset(tf_record_test,
                                               batch_size=batchsize_video,
                                               num_epochs=1,
                                               shuffle=False))

    print('{}: Save solution to {}'.format(datetime.now().strftime("%H:%M:%S"),
                                           my_solution_file))
    solution = prob_positive_class_from_prediction(pred)
    save_solution(my_solution_file, solution)
Ejemplo n.º 2
0
def compute_success_rate():
    files = [
        os.path.join('.', 'Maps', f) for f in os.listdir('Maps')
        if f[-3:] == 'mat' and f[-12:-4] != 'solution'
    ]
    print('Files Found: {}'.format(files))
    success_count = {}
    for window_ratio in [1, 2, 3, 4, 5]:
        success_count[window_ratio] = 10
        for f in files:
            cost_matrix = load_cost_matrix(f)
            num_nodes = cost_matrix.shape[0]

            score_vector = np.ones(num_nodes)
            task_windows = setup_task_windows(score_vector, window_ratio)

            max_cost = get_starting_cost(cost_matrix, task_windows)
            try:
                plan, visit_times, profit, cost, solver_stats = get_solution(
                    score_vector, cost_matrix, task_windows, max_cost)
            except ValueError:
                print('Failed with cost {}'.format(max_cost))
                success_count[window_ratio] -= 1
                continue

            wait_times = compute_wait_times(plan, cost_matrix, visit_times)

            visit_order_waits, visit_times_waits = get_arrive_depart_pairs(
                plan, visit_times, wait_times, cost)
            save_solution(f,
                          visit_order_waits,
                          visit_times_waits,
                          solver_type='tw')

            g, tour, verified_cost = build_graph(plan, score_vector,
                                                 cost_matrix)

            print(f)
            msg = 'The maximum profit tour found is \n'
            for idx, k in enumerate(tour):
                msg += str(k)
                if idx < len(tour) - 1:
                    msg += ' -> '
                else:
                    msg += ' -> 0'
            print(msg)

            msg = 'Profit: {:.2f}, cost: {:.2f}, verification cost: {:.2f}'
            print(msg.format(profit, cost, verified_cost))
            print(solver_stats.solve_time)
            print(solver_stats.setup_time)
            msg = 'Time taken: {} seconds'
            time = solver_stats.solve_time + solver_stats.setup_time
            print(msg.format(time))
        success_count[window_ratio] /= 10.0

    print('Success probabilities across constraint ratios')
    print(success_count)
    return success_count
Ejemplo n.º 3
0
def compute_solve_times(files):
    problem_sizes = [3, 4, 5, 6, 7, 8, 9, 10]
    small_map_solve_times = {p: [] for p in problem_sizes}
    large_map_solve_times = {p: [] for p in problem_sizes}
    for n in problem_sizes:
        for f in files:
            cost_matrix = load_cost_matrix(f)
            # cost_matrix = cost_matrix[0:n, 0:n]
            # print('----- Edge Costs -----')
            # print(cost_matrix)
            num_nodes = cost_matrix.shape[0]

            score_vector = np.ones(num_nodes)
            task_windows = setup_task_windows(score_vector)

            max_cost = get_starting_cost(cost_matrix, task_windows)
            try:
                plan, visit_times, profit, cost, solver_stats = get_solution(
                    score_vector, cost_matrix, task_windows, max_cost)
            except ValueError:
                print('Failed with cost {}'.format(max_cost))
                continue

            wait_times = compute_wait_times(plan, cost_matrix, visit_times)

            visit_order_waits, visit_times_waits = get_arrive_depart_pairs(
                plan, visit_times, wait_times, cost)
            save_solution(f,
                          visit_order_waits,
                          visit_times_waits,
                          solver_type='tw')

            g, tour, verified_cost = build_graph(plan, score_vector,
                                                 cost_matrix)

            print(f)
            msg = 'The maximum profit tour found is \n'
            for idx, k in enumerate(tour):
                msg += str(k)
                if idx < len(tour) - 1:
                    msg += ' -> '
                else:
                    msg += ' -> 0'
            print(msg)

            msg = 'Profit: {:.2f}, cost: {:.2f}, verification cost: {:.2f}'
            print(msg.format(profit, cost, verified_cost))
            msg = 'Time taken: {} seconds'
            time = solver_stats.solve_time + solver_stats.setup_time
            print(msg.format(time))
            # display_results(g, plan, task_windows, visit_times, wait_times, cost)
            if f[-12:-7] == '20x20':
                small_map_solve_times[n].append(time)
            elif f[-12:-7] == '50x50':
                large_map_solve_times[n].append(time)

    return (small_map_solve_times, large_map_solve_times)
Ejemplo n.º 4
0
from get_data import get_videos_from_folder, get_target_from_csv
import os
import numpy as np
from utils import save_solution

dir_path = os.path.dirname(os.path.realpath(__file__))
train_folder = os.path.join(dir_path, "train/")
test_folder = os.path.join(dir_path, "test/")

train_target = os.path.join(dir_path, 'train_target.csv')
my_solution_file = os.path.join(dir_path, 'solution.csv')

x_train = get_videos_from_folder(train_folder)
y_train = get_target_from_csv(train_target)
x_test = get_videos_from_folder(test_folder)

dummy_solution = 0.1 * np.ones(len(x_test))
save_solution(my_solution_file, dummy_solution)
Ejemplo n.º 5
0
        else:
            solution[a1 //
                     2][a1 %
                        2], solution[a2 //
                                     2][a2 %
                                        2] = solution[a2 //
                                                      2][a2 %
                                                         2], solution[a1 //
                                                                      2][a1 %
                                                                         2]
            no_progress_round += 1

    return solution


if __name__ == "__main__":
    n, m, _, preferences = utils.get_preferences("data/sample_2.txt")

    assert n % 2 == 0

    solution = solve_2_roommates_greedy(n, m, preferences)
    score = utils.compute_score(preferences, solution)
    print(f"Found a solution with a greedy algorithm of score {score}\n")
    utils.save_solution("output/solution_" + str(score) + ".txt", solution)

    solution = solve_2_roommates_conv(n, m, preferences)
    score = utils.compute_score(preferences, solution)

    print(f"Found a solution with a score of {score}")

    utils.save_solution("output/solution_" + str(score) + ".txt", solution)
Ejemplo n.º 6
0
def get_time_data():
    np.random.seed(1)

    files = [
        os.path.join('.', 'Maps', f) for f in os.listdir('Maps')
        if f[-3:] == 'mat' and f[-12:-4] != 'solution'
    ]
    maps20x20 = [f for f in files if '20x20' in f]
    maps50x50 = [f for f in files if '20x20' in f]
    big_maps = [f for f in files if '100_POI' in f]

    runtimes = {}
    for n in [4, 6, 8, 10, 12, 14]:
        print(n)
        runtimes[n] = []
        for f in big_maps:
            cost_matrix = load_cost_matrix(f)
            # high diagonal costs cause numerical errors
            # use constraints to prevent travel to self
            # diagonal cost must be > 0 for this to work
            # but should be low
            np.fill_diagonal(cost_matrix, 1)
            cost_matrix = cost_matrix[0:n, 0:n]
            # print('----- Edge Costs -----')
            # print(cost_matrix)
            num_nodes = cost_matrix.shape[0]

            score_vector = np.ones(num_nodes)
            task_windows = setup_task_windows(score_vector)
            # print('----- Task Windows -----')
            # print(np.around(task_windows, 2).astype('float'))
            max_cost = get_starting_cost(cost_matrix, task_windows)

            plan, visit_times, profit, cost, solve_time = get_solution(
                score_vector, cost_matrix, task_windows, max_cost)
            runtimes[n].append(solve_time)

            wait_times = compute_wait_times(plan, cost_matrix, visit_times)

            visit_order_waits, visit_times_waits = get_arrive_depart_pairs(
                plan, visit_times, wait_times, cost)
            save_solution(f,
                          visit_order_waits,
                          visit_times_waits,
                          solver_type='tw')

            print('----- Visited -----')
            print(np.sum(plan, axis=1))
            print('----- Plan -----')
            print(np.around(plan, 2).astype('int32'))
            # print('----- Edge Costs -----')
            # print(cost_matrix)
            print('----- Wait Times -----')
            print(np.around(wait_times, 2))

            g, tour, verified_cost = build_graph(plan, score_vector,
                                                 cost_matrix)

            print(f)
            msg = 'The maximum profit tour found is \n'
            for idx, k in enumerate(tour):
                msg += str(k)
                if idx < len(tour) - 1:
                    msg += ' -> '
                else:
                    msg += ' -> 0'
            print(msg)

            msg = 'Profit: {:.2f}, cost: {:.2f}, verification cost: {:.2f}'
            print(msg.format(profit, cost, verified_cost))
            msg = 'Time taken: {} seconds'
            print(msg.format(solve_time))
            # display_results(g, plan, task_windows, visit_times, wait_times, cost)

    with open('results_tw.txt', 'w') as f:
        f.write(str(runtimes))
Ejemplo n.º 7
0
            score_vector = np.ones(num_nodes)
            task_windows = setup_task_windows(score_vector)

            try:
                plan, visit_times, profit, cost, solve_time = get_solution(
                    score_vector, cost_matrix, task_windows, budget)
            except ValueError:
                print('No solution for {}'.format(f))
                continue

            wait_times = compute_wait_times(plan, cost_matrix, visit_times)

            visit_order_waits, visit_times_waits = get_arrive_depart_pairs(
                plan, visit_times, wait_times, cost)
            save_solution(f,
                          visit_order_waits,
                          visit_times_waits,
                          solver_type='tw')

            # print('----- Visited -----')
            # print(np.sum(plan, axis=1))
            # print('----- Plan -----')
            # print(np.around(plan, 2).astype('int32'))
            # print('----- Edge Costs -----')
            # print(cost_matrix)
            # print('----- Wait Times -----')
            # print(np.around(wait_times, 2))

            g, tour, verified_cost = build_graph(plan, score_vector,
                                                 cost_matrix)

            print(f)
Ejemplo n.º 8
0
model.add(Dense(2, activation='softmax'))

#model.summary()

#compile model using accuracy to measure model performance
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

#train the model
model.load_weights('conv.model')

'''
for i in range(0, len(x_test)):
    for j in range(0, len(x_test[i])):
        image = x_test[i][j]
        result = model.precit(image)
        print(image.shape)
'''
answers = []
for i in range(0, len(x_test)):
    class_1_prob_sum = 0
    print(i / len(x_test))
    for j in range(0, len(x_test[i])):
        image = x_test[i][j]
        image = np.expand_dims(image, 2)
        image = np.expand_dims(image, 0)
        class_1_prob_sum += model.predict(image)[0][1]
    class_1_prob_sum = class_1_prob_sum / len(x_test[i]) 
    answers.append(class_1_prob_sum)

save_solution(my_solution_file, answers)
Ejemplo n.º 9
0
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='lower right')
plt.savefig("accuracy.png")
plt.show()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper right')
plt.savefig("loss.png")
plt.show()

y_predict_train = model.predict(x=X_train_reshape,
                                batch_size=batch_size,
                                verbose=0,
                                steps=None)
roc_auc_train = roc_auc_score(y_train, y_predict_train[:, 1])

# make prediction
y_predict_video = model.predict(x=X_test_reshape,
                                batch_size=batch_size,
                                verbose=0,
                                steps=None)
y_predict = y_predict_video[:, 1]

### own code ###

save_solution(my_solution_file, y_predict)