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 13 commits
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
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion burn-book/src/building-blocks/metric.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ When working with the learner, you have the option to record metrics that will b
throughout the training process. We currently offer a restricted range of metrics.

| Metric | Description |
| ---------------- | ------------------------------------------------------- |
|------------------|---------------------------------------------------------|
| Accuracy | Calculate the accuracy in percentage |
| TopKAccuracy | Calculate the top-k accuracy in percentage |
| Precision | Calculate precision in percentage |
| Loss | Output the loss used for the backward pass |
| CPU Temperature | Fetch the temperature of CPUs |
| CPU Usage | Fetch the CPU utilization |
Expand Down
4 changes: 4 additions & 0 deletions crates/burn-train/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ crossterm = { workspace = true, optional = true }
# Utilities
derive-new = { workspace = true }
serde = { workspace = true, features = ["std", "derive"] }
strum = { workspace = true }
yare = "3.0.0"
laggui marked this conversation as resolved.
Show resolved Hide resolved


[dev-dependencies]
burn-ndarray = { path = "../burn-ndarray", version = "0.15.0" }
approx = "0.5.1"
laggui marked this conversation as resolved.
Show resolved Hide resolved

[package.metadata.docs.rs]
features = ["doc"]
74 changes: 74 additions & 0 deletions crates/burn-train/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,77 @@ pub use learner::*;

#[cfg(test)]
pub(crate) type TestBackend = burn_ndarray::NdArray<f32>;

#[cfg(test)]
pub(crate) type TestDevice = burn_ndarray::NdArrayDevice;

#[cfg(test)]
pub(crate) mod tests {
use crate::{metric::ClassificationInput, TestBackend, TestDevice};
use burn_core::{
prelude::{Bool, Tensor},
tensor::Distribution,
};
use std::default::Default;

/// Probability of tp before adding errors
pub const THRESHOLD: f64 = 0.5;

#[derive(Debug)]
pub enum ClassificationType {
Binary,
Multiclass,
Multilabel,
}

/// Sample x Class shaped matrix for use in
/// classification metrics testing
pub fn dummy_classification_input(
classification_type: &ClassificationType,
) -> ClassificationInput<TestBackend> {
let (real_targets, prediction_targets) = match classification_type {
ClassificationType::Binary => {
let real_targets = Tensor::<TestBackend, 2, Bool>::from_data(
[[0], [1], [0], [0], [1]],
&TestDevice::default(),
);

let prediction_targets = Tensor::<TestBackend, 2>::from_data(
[[0], [0], [1], [0], [1]],
&TestDevice::default(),
);
(real_targets, prediction_targets)
}
ClassificationType::Multiclass => {
let real_targets = Tensor::<TestBackend, 2, Bool>::from_data(
[[0, 1, 0], [1, 0, 0], [0, 0, 1], [0, 0, 1], [1, 0, 0]],
&TestDevice::default(),
);

let prediction_targets = Tensor::<TestBackend, 2>::from_data(
[[0, 1, 0], [0, 1, 0], [1, 0, 0], [0, 0, 1], [1, 0, 0]],
&TestDevice::default(),
);
(real_targets, prediction_targets)
}
ClassificationType::Multilabel => {
let real_targets = Tensor::<TestBackend, 2, Bool>::from_data(
[[1, 1, 0], [1, 0, 1], [1, 1, 1], [0, 0, 1], [1, 0, 0]],
&TestDevice::default(),
);

let prediction_targets = Tensor::<TestBackend, 2>::from_data(
[[0, 1, 1], [0, 1, 0], [1, 1, 0], [1, 0, 1], [1, 0, 0]],
&TestDevice::default(),
);
(real_targets, prediction_targets)
}
};
let predictions = prediction_targets
.random_like(Distribution::Uniform(0.0, THRESHOLD - 0.1))
.sub(prediction_targets.clone())
.abs();

ClassificationInput::new(predictions, real_targets)
}
}
74 changes: 74 additions & 0 deletions crates/burn-train/src/metric/base.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use burn_core::prelude::{Backend, Bool, Tensor};
use burn_core::tensor::cast::ToElement;
use burn_core::{data::dataloader::Progress, LearningRate};
use strum::EnumIter;

/// Metric metadata that can be used when computing metrics.
pub struct MetricMetadata {
Expand Down Expand Up @@ -56,6 +59,77 @@ pub trait Metric: Send + Sync {
fn clear(&mut self);
}

/// The [classification metric](ClassificationMetric) input type.
#[derive(new, Debug, Clone)]
pub struct ClassificationInput<B: Backend> {
/// Sample x Class Non thresholded normalized predictions.
pub predictions: Tensor<B, 2>,
/// Sample x Class one-hot encoded target.
pub targets: Tensor<B, 2, Bool>,
}

impl<B: Backend> From<ClassificationInput<B>> for (Tensor<B, 2>, Tensor<B, 2, Bool>) {
fn from(val: ClassificationInput<B>) -> Self {
(val.predictions, val.targets)
}
}

/// Class Averaging types for Classification metrics.
#[derive(EnumIter, Copy, Clone, Debug)]
laggui marked this conversation as resolved.
Show resolved Hide resolved
pub enum ClassAverageType {
///Computes the statistics over all classes before averaging
Micro,
///Computes the statistics independently for each class before averaging
Macro,
}

impl ClassAverageType {
laggui marked this conversation as resolved.
Show resolved Hide resolved
/// sum over samples
pub fn aggregate_sum<B: Backend>(self, sample_class_mask: Tensor<B, 2, Bool>) -> Tensor<B, 1> {
use ClassAverageType::*;
match self {
Macro => sample_class_mask.float().sum_dim(0).squeeze(0),
Micro => sample_class_mask.float().sum(),
}
}

/// average over samples
pub fn aggregate_mean<B: Backend>(self, sample_class_mask: Tensor<B, 2, Bool>) -> Tensor<B, 1> {
use ClassAverageType::*;
match self {
Macro => sample_class_mask.float().mean_dim(0).squeeze(0),
Micro => sample_class_mask.float().mean(),
}
}

///convert to averaged metric, returns tensor
pub fn to_averaged_tensor<B: Backend>(
self,
mut aggregated_metric: Tensor<B, 1>,
) -> Tensor<B, 1> {
use ClassAverageType::*;
match self {
Macro => {
if aggregated_metric.contains_nan().any().into_scalar() {
let nan_mask = aggregated_metric.is_nan();
aggregated_metric = aggregated_metric
.clone()
.select(0, nan_mask.bool_not().argwhere().squeeze(1))
}
aggregated_metric.mean()
}
Micro => aggregated_metric,
}
}

///convert to averaged metric, returns float
pub fn to_averaged_metric<B: Backend>(self, aggregated_metric: Tensor<B, 1>) -> f64 {
self.to_averaged_tensor(aggregated_metric)
.into_scalar()
.to_f64()
}
}

/// Adaptor are used to transform types so that they can be used by metrics.
///
/// This should be implemented by a model's output type for all [metric inputs](Metric::Input) that are
Expand Down
Loading