def make_weights_for_balanced_classes(images, nclasses):
    """Computing class weights for balanced sampling.
    From: https://discuss.pytorch.org/t/balanced-sampling-between-classes-with-torchvision-dataloader/2703/3

    Parameters
    ----------
    images : list
        List with image and respective label.
    nclasses : int
        Number of classes.

    Returns
    -------
    list
        List with weights per class.
    """
    count = [0] * nclasses
    for item in images:
        count[item[1]] += 1
    weight_per_class = [0.0] * nclasses
    N = float(sum(count))
    for i in range(nclasses):
        weight_per_class[i] = N / (float(count[i]) + 0.0001)
    weight = [0] * len(images)
    for idx, val in enumerate(images):
        weight[idx] = weight_per_class[val[1]]
    return weight