Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add precision classification metric #2293

Merged
merged 31 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c73438f
Implement confusion matrix and precision, first draft
Aug 21, 2024
63f4a1d
Implement confusion matrix
Sep 9, 2024
b9d71b6
format :D
Sep 9, 2024
eac29aa
add agg type to cm, reformat debug representation add testing.
Sep 20, 2024
59db68b
formating and tiny refactor
Sep 21, 2024
4261bd8
add ClassificationMetric trait, rename variables and types, move test…
Sep 21, 2024
5431a2f
change unwrap to expect
Sep 21, 2024
fd2e585
update book
Sep 21, 2024
56965e8
remove unused code
Sep 22, 2024
419438a
changes to make reusing code easier
Sep 22, 2024
dfac847
format :D
Sep 22, 2024
ea4b29c
change to static data tests
Sep 24, 2024
e23aa7b
remove classification metric trait, add auxiliary code for classific…
Oct 14, 2024
60a246b
move classification objects to classification.rs, use rstest, remove …
Oct 21, 2024
c145531
review docstring, add top_k for multiclass tasks.
Oct 23, 2024
0c984c4
move class averaging and metric computation to metric implementation,…
Oct 25, 2024
b0a2939
change struct and var names
Oct 25, 2024
f18e321
Merge branch 'main' into add-to-metrics
Oct 26, 2024
386802c
rename params, enforce nonzero for top_k param, optimize one_hot for …
Oct 30, 2024
b525527
add adaptor por classification input, correct one hot function
Nov 1, 2024
ff7611a
define default for ClassReduction, derive new for Precision metric wi…
Nov 8, 2024
4cbcff2
Merge branch 'main' into add-to-metrics
Nov 8, 2024
eeab0d3
expose PrecisionMetric, change metric initialization
Nov 8, 2024
aea207f
check one_hot input tensor has more than 1 classes and correct it's i…
Nov 16, 2024
410f273
Merge branch 'main' into add-to-metrics
Nov 16, 2024
746fa9d
implement adaptor for MultilabelClassificationOutput and Classificati…
Nov 16, 2024
7428b86
change with_top_k to take usize
Nov 18, 2024
58e1902
Merge branch 'main' into add-to-metrics
Nov 18, 2024
d598f00
Add precision config for binary, multiclass and multilabel
laggui Nov 18, 2024
1542ee9
Fix dummy_classification_input
laggui Nov 18, 2024
03ebe1d
make PrecisionMetric public
Nov 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement confusion matrix and precision, first draft
  • Loading branch information
Tiago Sanona committed Sep 21, 2024
commit c73438fc370fa4576324c72f2ab3cada52e1225c
38 changes: 38 additions & 0 deletions crates/burn-train/src/metric/confusion_matrix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use burn_core::prelude::{Backend, Bool, Tensor};

#[derive(Clone, Debug)]
pub struct ConfusionMatrix<B: Backend> {
pub true_positive: Tensor<B, 2, Bool>,
pub false_positive: Tensor<B, 2, Bool>,
pub true_negative: Tensor<B, 2, Bool>,
pub false_negative: Tensor<B, 2, Bool>,
}

impl<B: Backend> ConfusionMatrix<B> {
pub fn from(predictions: Tensor<B, 2, Bool>, targets: Tensor<B, 2, Bool>) -> Self {
let stats_tensor = predictions.int() + targets.int() * 2;
Self {
true_positive: stats_tensor.clone().equal_elem(3),
false_negative: stats_tensor.clone().equal_elem(2),
false_positive: stats_tensor.clone().equal_elem(1),
true_negative: stats_tensor.equal_elem(0)
}
}

pub fn positive(self) -> Tensor<B, 2, Bool> {
(self.true_positive.int() + self.false_negative.int()).bool()
}

pub fn negative(self) -> Tensor<B, 2, Bool> {
self.positive().bool_not()
}

pub fn predicted_positive(self) -> Tensor<B, 2, Bool> {
(self.true_positive.int() + self.false_positive.int()).bool()
}

pub fn predicted_negative(self) -> Tensor<B, 2, Bool> {
self.predicted_positive().bool_not()
}

}
4 changes: 4 additions & 0 deletions crates/burn-train/src/metric/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ pub use top_k_acc::*;
pub(crate) mod processor;
/// Module responsible to save and exposes data collected during training.
pub mod store;

#[cfg(feature = "metrics")]
mod precision;
mod confusion_matrix;
166 changes: 166 additions & 0 deletions crates/burn-train/src/metric/precision.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
use core::marker::PhantomData;
use std::ops::Deref;
use super::state::{FormatOptions, NumericMetricState};
use super::{MetricEntry, MetricMetadata};
use crate::metric::{Metric, Numeric, confusion_matrix::ConfusionMatrix};
use burn_core::tensor::backend::Backend;
use burn_core::tensor::{Bool, ElementConversion, Tensor};

#[derive(Clone)]
enum MetricAverage {
Micro,
Macro,
Weighted(Box<[f64]>)
}

/// The precision metric.
pub struct PrecisionMetric<B: Backend> {
state: NumericMetricState,
threshold: f32,
average: MetricAverage,
_b: PhantomData<B>,
}

/// The [precision metric](PrecisionMetric) input type.
#[derive(new)]
pub struct PrecisionInput<B: Backend> {
predictions: Tensor<B, 2>,
targets: Tensor<B, 2, Bool>,
}

impl<B: Backend> PrecisionMetric<B> {
/// Creates the metric.
pub fn new() -> Self {
Self::default()
}

/// Sets the threshold.
pub fn with_threshold(mut self, threshold: f32) -> Self {
self.threshold = threshold;
self
}

/// Sets average type.
pub fn with_average(mut self, average: MetricAverage) -> Self {
self.average = average;
self
}

}

impl<B: Backend> Default for PrecisionMetric<B> {
/// Creates a new metric instance with default values.
fn default() -> Self {
Self {
state: NumericMetricState::default(),
threshold: 0.5,
average: MetricAverage::Micro,
_b: PhantomData,
}
}
}

impl<B: Backend> Metric for PrecisionMetric<B> {
const NAME: &'static str = "Precision";

type Input = PrecisionInput<B>;

fn update(&mut self, input: &PrecisionInput<B>, _metadata: &MetricMetadata) -> MetricEntry {
let [batch_size, n_classes] = input.predictions.dims();

let targets = input.targets.clone().to_device(&B::Device::default());
let predictions = input
.predictions
.clone()
.to_device(&B::Device::default())
.greater_elem(self.threshold);

let stats = ConfusionMatrix::from(predictions, targets);

let precision: f64 = match self.average.clone() {
MetricAverage::Macro => {
(stats.clone().true_positive.int().sum_dim(0) / stats.predicted_positive().int().sum_dim(0)).sum().into_scalar().elem::<f64>() / n_classes as f64
},
MetricAverage::Micro => {
stats.clone().true_positive.int().sum().into_scalar().elem::<f64>() / stats.predicted_positive().int().sum().into_scalar().elem::<f64>()
},
MetricAverage::Weighted(weights) => {
(stats.clone().true_positive.float().sum_dim(0) * Tensor::from_floats(weights.deref(), &B::Device::default())).sum().into_scalar().elem::<f64>()
}
};

self.state.update(
100.0 * precision,
batch_size,
FormatOptions::new(Self::NAME).unit("%").precision(2),
)
}

fn clear(&mut self) {
self.state.reset()
}
}

impl<B: Backend> Numeric for PrecisionMetric<B> {
fn value(&self) -> f64 {
self.state.value()
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::TestBackend;

#[test]
fn test_precision_without_padding() {
let device = Default::default();
let mut metric = PrecisionMetric::<TestBackend>::new();
let input = PrecisionInput::new(
Tensor::from_data(
[
[0.0, 0.2, 0.8], // 2
[1.0, 0.0, 0.0], // 0
[0.3, 0.6, 0.2], // 1
[0.1, 0.7, 0.2], // 1
],
&device,
),
Tensor::from_data(
[
[0, 0, 1],
[0, 0, 0],
[0, 1, 0],
[0, 1, 0],
],
&device),
);

let _entry = metric.update(&input, &MetricMetadata::fake());
assert_eq!(75.0, metric.value());
}

/*#[test]
laggui marked this conversation as resolved.
Show resolved Hide resolved
fn test_precision_with_padding() {
let device = Default::default();
let mut metric = PrecisionMetric::<TestBackend>::new().with_pad_token(3);
let input = PrecisionInput::n(
Tensor::from_data(
[
[0.0, 0.2, 0.8, 0.0], // 2
[1.0, 2.0, 0.5, 0.0], // 1
[0.4, 0.1, 0.2, 0.0], // 0
[0.6, 0.7, 0.2, 0.0], // 1
[0.0, 0.1, 0.2, 5.0], // Predicted padding should not count
[0.0, 0.1, 0.2, 0.0], // Error on padding should not count
[0.6, 0.0, 0.2, 0.0], // Error on padding should not count
],
&device,
),
Tensor::from_data([2, 2, 1, 1, 3, 3, 3], &device),
);

let _entry = metric.update(&input, &MetricMetadata::fake());
assert_eq!(todo!(), metric.value());
}*/
}