Python之tqdm

Python之tqdm

Python之tqdm主要作用是用于显示进度,使用较为简单:

  • 创建进度条;
  • 关闭进度条;

注意比较有意思的一点是关于tqdm来源问题:
Tqdm在阿拉伯语表示进步,在西班牙语中表示我非常爱你。是一个快速,可扩展的Python进度条,可以在Python长循环中添加一个进度提示信息,用户只需要封装任意的迭代器tqdm(iterator)即可完成进度条。相比ProgressBar来说Tqdm的开销非常低,同时Tqdm可以在任何环境中不需要任何依赖运行

def calculate_weigths_labels(dataset, dataloader, num_classes):
    # Create an instance from the data loader
    z = np.zeros((num_classes,))
    # Initialize tqdm
    tqdm_batch = tqdm(dataloader)
    print('Calculating classes weights')
    for sample in tqdm_batch:
        y = sample['label']
        y = y.detach().cpu().numpy()
        mask = (y >= 0) & (y < num_classes)
        labels = y[mask].astype(np.uint8)
        count_l = np.bincount(labels, minlength=num_classes)
        z += count_l
    tqdm_batch.close()
    total_frequency = np.sum(z)
    class_weights = []
    for frequency in z:
        class_weight = 1 / (np.log(1.02 + (frequency / total_frequency)))
        class_weights.append(class_weight)
    ret = np.array(class_weights)
    classes_weights_path = os.path.join(Path.db_root_dir(dataset), dataset+'_classes_weights.npy')
    np.save(classes_weights_path, ret)

    return ret

参考文献:

  • torch中文网:https://ptorch.com/news/170.html

你可能感兴趣的:(dataset)