简体   繁体   中英

creating a progress bar for a validation test Python

The following code is used to find p-values for a significant test on Cifar-10 database. Because we need a min of 1000 permutations, it is a very slow process, and I want to inlude a progress bar to show how time for each permutation. I was thinking of using the tqdm library and sleep function, but am stuck on where to go from here. any help would be greatly appreciated!

'''

def validate_significance(val_loader, model, criterion, args):

model.eval()



vec_acc1 = []
vec_acc1_chance = []
vec_acc5 = []
vec_acc5_chance = []

for ss in range(0, args.num_permutations):


    val_loader = get_rand_sample_loader(val_loader.dataset, args)

    acc1_over_one_permutaion = 0
    acc1_chance_over_one_permutation = 0
    acc5_over_one_permutaion = 0
    acc5_chance_over_one_permutation = 0
    cnt = 0
    with torch.no_grad():
        for i, (images, target) in enumerate(val_loader):

            if args.gpu is not None:
                images = images.cuda(args.gpu, non_blocking=True)
            if torch.cuda.is_available():
                target = target.cuda(args.gpu, non_blocking=True)

            # compute output
            output = model(images)

            # measure accuracy on true labels
            acc1, acc5 = accuracy(output, target, topk=(1, 5))
            acc1_over_one_permutaion += acc1.item()
            acc5_over_one_permutaion += acc5.item()

            # now, measure accuracy on permuted labels
            target = target[torch.randperm(len(target))]  # scrambling the labels
            acc1_perm, acc5_perm = accuracy(output, target, topk=(1, 5))
            acc1_chance_over_one_permutation += acc1_perm.item()
            acc5_chance_over_one_permutation += acc5_perm.item()
            cnt += 1

    vec_acc1.append(acc1_over_one_permutaion / cnt)
    vec_acc1_chance.append(acc1_chance_over_one_permutation / cnt)
    vec_acc5.append(acc5_over_one_permutaion / cnt)
    vec_acc5_chance.append(acc5_chance_over_one_permutation / cnt)

p_acc1 = stats.ttest_ind(vec_acc1, vec_acc1_chance, equal_var=False)
p_acc5 = stats.ttest_ind(vec_acc5, vec_acc5_chance, equal_var=False)

return p_acc1, p_acc5

'''

from tqdm import tqdm

def validate_significance(val_loader, model, criterion, args):
    model.eval()

    vec_acc1 = []
    vec_acc1_chance = []
    vec_acc5 = []
    vec_acc5_chance = []

    for ss in tqdm(range(0, args.num_permutations)):
        ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM