<filename>plotter.py<gh_stars>0
import os
import argparse
import numpy as np
import matplotlib.pyplot as plt
import glob

import settings

def plot_train(path):
    '''plot training plot for a single training trial'''
    train_logs = np.loadtxt(path, dtype=float)
    plt.plot(train_logs)
    plt.show()


def plot_averages_stds(models=['cnn'], dataset='mnist', epoch = 5, n_stds=4):
    '''plot averages and standard deviation of multiple trials with same epoch'''

    # set color for each model
    colors_list = ['#FF1111', '#FF2222', '#FF3333']

    # plot logs for each model based on provided model list
    for i, model in enumerate(models):
        search_string = model + '_' + dataset + '_' + str(epoch) + 'e'
        train_path = 'assets/saved_logs/*_' + search_string + '/train_logs.txt'
        test_path = 'assets/saved_logs/*_' + search_string + '/test_logs.txt'
        all_train_logs = []
        for fname in glob.glob(train_path):
            train_logs = np.loadtxt(fname, dtype=float)
            all_train_logs.append(train_logs)
        all_test_logs = []
        for fname in glob.glob(test_path):
            test_logs = np.loadtxt(fname, dtype=float)
            all_test_logs.append(test_logs)
        
        x = np.arange(1,epoch+1)
        averages_train = np.array(all_train_logs).mean(axis=0)
        vars_train = np.array(all_train_logs).var(axis=0)
        stds_train = vars_train ** 0.5
        averages_test = np.array(all_test_logs).mean(axis=0)
        vars_test = np.array(all_test_logs).var(axis=0)
        stds_test = vars_test ** 0.5
        fig, (ax1, ax2) = plt.subplots(1, 2, figsize =(15,5))
        ax1.plot(x, averages_train, c=colors_list[i], label=model)
        ax2.plot(x, averages_test, c=colors_list[i], label=model)
        for k in np.linspace(0, n_stds, 4):
            ax1.fill_between(x, averages_train-k*stds_train, averages_train+k*stds_train, alpha = .3,
                            edgecolor=None, facecolor=colors_list[i], linewidth=0)
            ax2.fill_between(x, averages_test-k*stds_test, averages_test+k*stds_test, alpha = .3,
                            edgecolor=None, facecolor=colors_list[i], linewidth=0)

    ax1.set_title(f"Training loss on {dataset}")
    ax2.set_title(f"Test accuracy on {dataset}")
    ax1.legend()
    ax2.legend()
    ax1.set_xticks(x)
    ax2.set_xticks(x)
    # save plot and show if display is available
    if not os.path.isdir('assets/saved_plots'):
        os.makedirs('asset/saved_plots')
    fig.savefig(f'assets/saved_plots/{dataset}_{epoch}e.png')
    plt.show()

if __name__ == "__main__":
    # parsing input arguments
    parser = argparse.ArgumentParser(description='Plotting with multiple models')
    parser.add_argument('--dataset', default='mnist',
                        help='Choose dataset mnist/cifar10')
    parser.add_argument('--models', default=['cnn'],
                        help='Provide list of models cnn/mcd/ensemble')
    parser.add_argument('--epochs', type=int, default=settings.EPOCHS,
                        help='Total number of epochs for training')
    args = parser.parse_args()
    
    # execute plot
    plot_averages_stds(models=args.models, dataset=args.dataset, epoch = args.epochs, n_stds=4)


<filename>plotter.py<gh_stars>0
import os
import argparse
import numpy as np
import matplotlib.pyplot as plt
import glob

import settings

def plot_train(path):
    '''plot training plot for a single training trial'''
    train_logs = np.loadtxt(path, dtype=float)
    plt.plot(train_logs)
    plt.show()


def plot_averages_stds(models=['cnn'], dataset='mnist', epoch = 5, n_stds=4):
    '''plot averages and standard deviation of multiple trials with same epoch'''

    # set color for each model
    colors_list = ['#FF1111', '#FF2222', '#FF3333']

    # plot logs for each model based on provided model list
    for i, model in enumerate(models):
        search_string = model + '_' + dataset + '_' + str(epoch) + 'e'
        train_path = 'assets/saved_logs/*_' + search_string + '/train_logs.txt'
        test_path = 'assets/saved_logs/*_' + search_string + '/test_logs.txt'
        all_train_logs = []
        for fname in glob.glob(train_path):
            train_logs = np.loadtxt(fname, dtype=float)
            all_train_logs.append(train_logs)
        all_test_logs = []
        for fname in glob.glob(test_path):
            test_logs = np.loadtxt(fname, dtype=float)
            all_test_logs.append(test_logs)
        
        x = np.arange(1,epoch+1)
        averages_train = np.array(all_train_logs).mean(axis=0)
        vars_train = np.array(all_train_logs).var(axis=0)
        stds_train = vars_train ** 0.5
        averages_test = np.array(all_test_logs).mean(axis=0)
        vars_test = np.array(all_test_logs).var(axis=0)
        stds_test = vars_test ** 0.5
        fig, (ax1, ax2) = plt.subplots(1, 2, figsize =(15,5))
        ax1.plot(x, averages_train, c=colors_list[i], label=model)
        ax2.plot(x, averages_test, c=colors_list[i], label=model)
        for k in np.linspace(0, n_stds, 4):
            ax1.fill_between(x, averages_train-k*stds_train, averages_train+k*stds_train, alpha = .3,
                            edgecolor=None, facecolor=colors_list[i], linewidth=0)
            ax2.fill_between(x, averages_test-k*stds_test, averages_test+k*stds_test, alpha = .3,
                            edgecolor=None, facecolor=colors_list[i], linewidth=0)

    ax1.set_title(f"Training loss on {dataset}")
    ax2.set_title(f"Test accuracy on {dataset}")
    ax1.legend()
    ax2.legend()
    ax1.set_xticks(x)
    ax2.set_xticks(x)
    # save plot and show if display is available
    if not os.path.isdir('assets/saved_plots'):
        os.makedirs('asset/saved_plots')
    fig.savefig(f'assets/saved_plots/{dataset}_{epoch}e.png')
    plt.show()

if __name__ == "__main__":
    # parsing input arguments
    parser = argparse.ArgumentParser(description='Plotting with multiple models')
    parser.add_argument('--dataset', default='mnist',
                        help='Choose dataset mnist/cifar10')
    parser.add_argument('--models', default=['cnn'],
                        help='Provide list of models cnn/mcd/ensemble')
    parser.add_argument('--epochs', type=int, default=settings.EPOCHS,
                        help='Total number of epochs for training')
    args = parser.parse_args()
    
    # execute plot
    plot_averages_stds(models=args.models, dataset=args.dataset, epoch = args.epochs, n_stds=4)


plot training plot for a single training trial plot averages and standard deviation of multiple trials with same epoch # set color for each model # plot logs for each model based on provided model list # save plot and show if display is available # parsing input arguments # execute plot