Git Product home page Git Product logo

Comments (5)

MaigoAkisame avatar MaigoAkisame commented on August 18, 2024

Do you have an example test case where the two scripts yield different results?

from cmu-thesis.

lijuncheng16 avatar lijuncheng16 commented on August 18, 2024

pred_truth.tar.gz

from cmu-thesis.

lijuncheng16 avatar lijuncheng16 commented on August 18, 2024

Thank you Yun for your quick reply! Included are 2 pickle files of pred + truth file of (20123,527) shape

from cmu-thesis.

lijuncheng16 avatar lijuncheng16 commented on August 18, 2024

Your eval script's AUC and dprime is about 50% of the sklearn one below:

import numpy as np
from scipy import stats
from sklearn import metrics
import torch

def d_prime(auc):
    standard_normal = stats.norm()
    d_prime = standard_normal.ppf(auc) * np.sqrt(2.0)
    return d_prime

def calculate_stats(output, target):
    """Calculate statistics including mAP, AUC, etc.
    Args:
      output: 2d array, (samples_num, classes_num)
      target: 2d array, (samples_num, classes_num)
    Returns:
      stats: list of statistic of each class.
    """

    classes_num = target.shape[-1]
    stats = []

    # Accuracy, only used for single-label classification such as esc-50, not for multiple label one such as AudioSet
    acc = metrics.accuracy_score(np.argmax(target, 1), np.argmax(output, 1))

    # Class-wise statistics
    for k in range(classes_num):

        # Average precision
        avg_precision = metrics.average_precision_score(
            target[:, k], output[:, k], average=None)

        # AUC
        auc = metrics.roc_auc_score(target[:, k], output[:, k], average=None)

        # Precisions, recalls
        (precisions, recalls, thresholds) = metrics.precision_recall_curve(
            target[:, k], output[:, k])

        # FPR, TPR
        (fpr, tpr, thresholds) = metrics.roc_curve(target[:, k], output[:, k])

        save_every_steps = 1000     # Sample statistics to reduce size
        dict = {'precisions': precisions[0::save_every_steps],
                'recalls': recalls[0::save_every_steps],
                'AP': avg_precision,
                'fpr': fpr[0::save_every_steps],
                'fnr': 1. - tpr[0::save_every_steps],
                'auc': auc,
                # note acc is not class-wise, this is just to keep consistent with other metrics
                'acc': acc
                }
        stats.append(dict)

    return stats

from cmu-thesis.

MaigoAkisame avatar MaigoAkisame commented on August 18, 2024

I've found the cause: the pred array you passed in has the float16 dtype, and my function roc isn't robust against it.
Below is what happened in detail:

def roc(pred, truth):  # here, pred is float16 and truth is bool
    data = numpy.array(sorted(zip(pred, truth), reverse = True))  # here, data is float16
    pred, truth = data[:,0], data[:,1]  # here, both pred and truth becomes float16

    # now we're computing the cumsum on a float16 array.
    # float16 doesn't have enough precision to represent 2049:
    # when it adds 1 to 2048, the result is still 2048.
    # therefore the cumsum is capped at 2048, and messed up everything from here.
    TP = truth.cumsum()
    FP = (1 - truth).cumsum()

    mask = numpy.concatenate([numpy.diff(pred) < 0, numpy.array([True])])
    TP = numpy.concatenate([numpy.array([0]), TP[mask]])
    FP = numpy.concatenate([numpy.array([0]), FP[mask]])
    return TP, FP

I've modified the second line of the function to convert truth to bool, and this fixes the problem.

from cmu-thesis.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.