Ejemplo n.º 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()
Ejemplo n.º 2
0
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
Ejemplo n.º 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()
Ejemplo n.º 4
0
def evaluate_x_test(arg, layer_idx):
    grouped_data, true_labels, network_labels = get_batch_data(arg, network)
    if layer_idx == 'all':
        lower_idx = 0
        upper_idx = last_layer_index + 1
    else:
        lower_idx = layer_idx
        upper_idx = layer_idx + 1
    accuracies = []
    for i in range(lower_idx, upper_idx):
        tropical_test_labels = get_tropical_test_labels(arg, network, grouped_data, i, epoch_number)
        if tropical_test_labels is not None:
            tropical_accuracy = sum(tropical_test_labels == true_labels) / len(true_labels)
            tropical_network_agreement = sum(tropical_test_labels == network_labels) / len(network_labels)
            logger.info(
                'Agreement of the tropical function and the neural network on the test set: ' + str(
                    tropical_network_agreement))
            logger.info('Agreement of the tropical function and the true labels on the test set: ' + str(
                tropical_accuracy))
            accuracies.append(np.array([str(tropical_network_agreement), str(tropical_accuracy)]))
    accuracies = np.array(accuracies)
    save_dir = get_saving_directory(arg)
    np.savetxt(os.path.join(save_dir, "accuracies.csv"), accuracies, delimiter=",", fmt='%s')
Ejemplo n.º 5
0
def compute_agreement(array1, array2):
    return np.sum(array1 == array2) / array1.size


arg = parse_arguments()
for i in range(5):
    arg.network_number = str(i)
    network = load_network(arg, None)
    x_test, y_test = load_data(arg, arg.data_type)
    network_labels = np.argmax(network.predict(x_test), axis=1)
    print(arg.data_type.capitalize() + ' accuracy, network number ' + str(i) +
          ':')
    print(np.sum(network_labels == y_test) / y_test.size)

save_dir = get_saving_directory(arg)
network = load_network(arg, None)
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)
Ejemplo n.º 6
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