Example #1
0
def plot_ECEs_and_temperatures(arg):
    save_dir = get_saving_directory(arg)
    array_name = get_file_name(arg, '.npy')
    array_directory = os.path.join(save_dir, 'Arrays')
    path = os.path.join(array_directory, 'Temperatures', array_name)
    if os.path.isfile(path):
        plot_dir = create_directory(save_dir, 'Plots')
        plot_name = get_file_name(arg, '.png')
        # Temperatures
        temperatures = np.load(path)
        plt.plot(temperatures)
        plt.title('Temperatures')
        temperature_dir = create_directory(plot_dir, 'Temperatures')
        file_name = os.path.join(temperature_dir, plot_name)
        plt.savefig(file_name)
        plt.close()
        # ECEs
        before_scaling_path = os.path.join(array_directory, 'ECEs_before_scaling', array_name)
        ECEs_before_scaling = np.load(before_scaling_path)
        after_scaling_path = os.path.join(array_directory, 'ECEs_after_scaling', array_name)
        ECEs_after_scaling = np.load(after_scaling_path)
        plt.plot(ECEs_before_scaling, color='blue', label='before scaling')
        plt.plot(ECEs_after_scaling, color='orange', label='after scaling')
        plt.title('ECEs')
        ECE_dir = create_directory(plot_dir, 'ECEs')
        file_name = os.path.join(ECE_dir, plot_name)
        plt.savefig(file_name)
        plt.close()
def get_tropical_function_directory(arg, folder_name, data_type, epoch_number):
    save_dir = get_saving_directory(arg)
    func_dir = create_directory(save_dir, data_type.capitalize())
    if arg.epochs == 'all' or arg.epochs == 'special':
        func_dir = create_directory(func_dir, 'all_epochs', 'epoch_' + str(epoch_number))
    if arg.extract_all_dimensions:
        all_dimensions_string = '_all_dimensions'
    else:
        all_dimensions_string = ''
    func_dir = create_directory(func_dir, folder_name + all_dimensions_string)
    return func_dir
Example #3
0
def plot_loss_and_accuracy(history, arg):
    # plot loss
    fig, axs = plt.subplots(2, 1, constrained_layout=True)
    l1, = axs[0].plot(history['loss'], color='blue', label='train')
    l2, = axs[0].plot(history['val_loss'], color='orange', label='test')
    axs[0].set_title('Cross Entropy Loss', fontsize=10)
    axs[0].legend((l1, l2), ('train', 'test'), loc='upper right')
    fig.suptitle(os.path.join('Activation: ' + arg.activation_function, 'OH Factor: ' + str(arg.overheating_factor),
                              '\n', 'Maximum Temperature: ' + str(arg.maximum_temperature),
                              'Reset Temperature: ' + str(arg.reset_temperature)), fontsize=12, fontweight='bold')
    # plot accuracy
    l1, = axs[1].plot(history['accuracy'], color='blue', label='train')
    l2, = axs[1].plot(history['val_accuracy'], color='orange', label='test')
    axs[1].set_title('Classification Accuracy', fontsize=10)
    axs[1].legend((l1, l2), ('train', 'test'), loc='lower right')
    # Saving
    save_dir = get_saving_directory(arg)
    accuracy_and_loss_directory = create_directory(save_dir, 'Plots', 'Accuracy_and_Loss')
    plot_name = get_file_name(arg, '.png')
    file_name = os.path.join(accuracy_and_loss_directory, plot_name)
    plt.savefig(file_name)
    plt.close()
Example #4
0
x_train, y_train, x_test, y_test = load_data(arg.data_set)
training_labels = np.argmax(y_train, axis=1)
test_labels = np.argmax(y_test, axis=1)
no_labels = y_train.shape[1]

x_train_adv = generate_adversarial_data(network,
                                        x_train,
                                        method='momentum_iterative')
x_test_adv = generate_adversarial_data(network,
                                       x_test,
                                       method='momentum_iterative')
train_accuracy = compute_accuracy(network, x_train, training_labels)
test_accuracy = compute_accuracy(network, x_test, test_labels)
train_adv_accuracy = compute_accuracy(network, x_train_adv, training_labels)
test_adv_accuracy = compute_accuracy(network, x_test_adv, test_labels)

last_layer_index = len(network.layers) - 2
training_directory = create_directory(save_dir, 'Training')
function_path = get_function_path(arg, last_layer_index, training_directory)

x_train_adv_tropical = prepare_data_for_tropical_function(x_train_adv)
out = [None] * no_labels
for j in range(no_labels):
    pos_terms_j = np.load(function_path + 'pos_label_' + str(j) + '.npy')
    out[j] = np.max(np.dot(pos_terms_j, x_train_adv_tropical), axis=0)
tropical_adv_labels = np.argmax(out, axis=0)
tropical_adv_accuracy = np.sum(
    tropical_adv_labels == training_labels) / y_train.shape[0]
equal_after_adv = compute_agreement()
a = 5
Example #5
0
    seconds = round(seconds)
    hours = seconds // 3600
    seconds = seconds % 3600
    minutes = seconds // 60
    seconds = seconds % 60
    return str(hours) + ' hours, ' + str(minutes) + ' minutes, ' + str(
        seconds) + ' seconds.'


start = time.time()
data_dimension = 16
no_data_points = 10**7
epsilon_power = 3
epsilon = 10**(-epsilon_power)

save_dir = create_directory(os.getcwd(), 'Probability_of_Points')
ending = '_epsilon_' + str(epsilon_power) + '.npy'
min_path = os.path.join(save_dir, 'min' + ending)
max_path = os.path.join(save_dir, 'max' + ending)
mean_path = os.path.join(save_dir, 'mean' + ending)
median_path = os.path.join(save_dir, 'median' + ending)
std_path = os.path.join(save_dir, 'std' + ending)

min = load_or_initialise(min_path)
max = load_or_initialise(max_path)
mean = load_or_initialise(mean_path)
median = load_or_initialise(median_path)
std = load_or_initialise(std_path)

previously_completed_runs = len(min)
Example #6
0
import numpy as np
import scipy.io
import os
from Utilities.Saver import create_directory

directory = '/home/martint/Documents/MATLAB/tropex/Data/Exp11/DEBUGGING/10'
mat_directory = os.path.join(directory, 'chosen_mat')
np_directory = os.path.join(directory, 'chosen_arrays')
create_directory(np_directory)
for file_name in os.listdir(mat_directory):
    if file_name.split('.')[-1] == 'mat':
        old_path = os.path.join(mat_directory, file_name)
        new_path = os.path.join(np_directory, file_name)
        mat = scipy.io.loadmat(old_path)
        correlations = mat['chosen_correlations']
        np.save(new_path, correlations)
Example #7
0
def get_logger(arg):
    save_dir = get_saving_directory(arg)
    transformation_path = create_directory(save_dir,
                                           arg.data_type.capitalize())
    file_name = sys.argv[0].split('/')[-1].split('.')[0]
    if file_name == 'TropEx' or file_name == 'TropEx2' or file_name == 'TropEx_WIP':
        logger = logging.getLogger('TropEx_log')
        logger_path = os.path.join(transformation_path, 'TropEx.log')
    elif file_name == 'Evaluation':
        logger = logging.getLogger(arg.data_type.capitalize() + '_log')
        logger_path = os.path.join(transformation_path, arg.data_type + '.log')
    elif arg.mode == 'exp4_variation':
        logger = logging.getLogger('exp4_variation_log')
        logger_path = os.path.join(transformation_path,
                                   'exp4_variation' + '.log')
    elif arg.mode == 'exp6_add_shifted_functions':
        logger = logging.getLogger('exp6_add_shifted_functions_log')
        logger_path = os.path.join(transformation_path,
                                   'exp6_add_shifted_functions' + '.log')
    elif arg.mode == 'exp9_compute_coefficient_statistics':
        logger = logging.getLogger('exp9_compute_coefficient_statistics_log')
        logger_path = os.path.join(
            transformation_path,
            'exp9_compute_coefficient_statistics' + '.log')
    elif arg.mode == 'exp10_slide_extracted_function_over_image':
        logger = logging.getLogger(
            'exp10_slide_extracted_function_over_image_log')
        logger_path = os.path.join(
            transformation_path,
            'exp10_slide_extracted_function_over_image' + '.log')
    elif arg.mode == 'exp11_compare_linear_functions':
        logger = logging.getLogger('exp11_compare_linear_functions_log')
        logger_path = os.path.join(transformation_path,
                                   'exp11_compare_linear_functions' + '.log')
    elif arg.mode == 'exp12_compare_activation_patterns':
        logger = logging.getLogger('exp12_compare_activation_patterns_log')
        logger_path = os.path.join(
            transformation_path, 'exp12_compare_activation_patterns' + '.log')
    elif arg.mode == 'save_linear_coefficients_to_mat':
        logger = logging.getLogger('save_linear_coefficients_to_mat_log')
        logger_path = os.path.join(transformation_path,
                                   'save_linear_coefficients_to_mat' + '.log')
    elif arg.mode == 'exp14_interpolation':
        logger = logging.getLogger('interpolation_log')
        logger_path = os.path.join(transformation_path,
                                   'interpolation' + '.log')
    elif arg.mode == 'save_to_mat':
        logger = logging.getLogger('save_to_mat_log')
        logger_path = os.path.join(transformation_path, 'save_to_mat' + '.log')
    elif arg.mode == 'compute_network_accuracies':
        logger = logging.getLogger('compute_network_accuracies_log')
        logger_path = os.path.join(transformation_path,
                                   'compute_network_accuracies' + '.log')
    else:
        logger = None
        logger_path = ''
    logger.setLevel(logging.INFO)

    file_handler = logging.FileHandler(logger_path, mode='w')
    file_handler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)
    logger.info("================================================")
    logger.info("Data Type: " + arg.data_type.capitalize())
    logger.info("Network Type: " + arg.network_type_coarse)
    logger.info("Network Name: " + arg.network_type_fine)
    logger.info("Lower Index of Data Points: {}".format(arg.data_points_lower))
    logger.info("Upper Index of Data Points: {}".format(arg.data_points_upper))
    return logger