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 23 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
1 change: 1 addition & 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
12 changes: 8 additions & 4 deletions crates/burn-tensor/src/tensor/api/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ where
/// ```
pub fn one_hot(self, num_classes: usize) -> Tensor<B, 2, Int> {
check!(TensorCheck::one_hot_tensor(self.clone(), num_classes));
let [num_samples] = self.dims();
let indices = self.unsqueeze();
let values = indices.ones_like();
Tensor::zeros([num_samples, num_samples], &indices.device()).scatter(1, indices, values)
if num_classes == 1 {
self.unsqueeze_dim(1)
} else {
laggui marked this conversation as resolved.
Show resolved Hide resolved
let [num_samples] = self.dims();
let indices = self.unsqueeze_dim(1);
let values = indices.ones_like();
Tensor::zeros([num_samples, num_classes], &indices.device()).scatter(1, indices, values)
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/burn-train/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ ratatui = { workspace = true, optional = true, features = ["all-widgets", "cross
# Utilities
derive-new = { workspace = true }
serde = { workspace = true, features = ["std", "derive"] }
rstest.workspace = true



[dev-dependencies]
burn-ndarray = { path = "../burn-ndarray", version = "0.16.0" }
Expand Down
11 changes: 11 additions & 0 deletions crates/burn-train/src/learner/classification.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::metric::classification::ClassificationInput;
use crate::metric::{AccuracyInput, Adaptor, HammingScoreInput, LossInput};
use burn_core::tensor::backend::Backend;
use burn_core::tensor::{Int, Tensor};
Expand Down Expand Up @@ -27,6 +28,16 @@ impl<B: Backend> Adaptor<LossInput<B>> for ClassificationOutput<B> {
}
}

impl<B: Backend> Adaptor<ClassificationInput<B>> for ClassificationOutput<B> {
fn adapt(&self) -> ClassificationInput<B> {
let [_, num_classes] = self.output.dims();
ClassificationInput::new(
self.output.clone(),
self.targets.clone().one_hot(num_classes).bool(),
)
}
}

/// Multi-label classification output adapted for multiple metrics.
#[derive(new)]
pub struct MultiLabelClassificationOutput<B: Backend> {
Expand Down
76 changes: 76 additions & 0 deletions crates/burn-train/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,79 @@ pub use learner::*;

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

#[cfg(test)]
pub(crate) mod tests {
use crate::{metric::classification::ClassificationInput, TestBackend};
use burn_core::prelude::Tensor;
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 (targets, predictions) = match classification_type {
ClassificationType::Binary => {
(
Tensor::from_data([[0], [1], [0], [0], [1]], &Default::default()),
Tensor::from_data(
[[0.3], [0.2], [0.7], [0.1], [0.55]],
//[[0], [0], [1], [0], [1]] with threshold=0.5
&Default::default(),
),
)
}
ClassificationType::Multiclass => {
(
Tensor::from_data(
[[0, 1, 0], [1, 0, 0], [0, 0, 1], [0, 0, 1], [1, 0, 0]],
&Default::default(),
),
Tensor::from_data(
[
[0.2, 0.8, 0.0],
[0.3, 0.6, 0.1],
[0.7, 0.25, 0.05],
[0.1, 0.15, 0.8],
[0.9, 0.03, 0.07],
],
//[[0, 1, 0], [0, 1, 0], [1, 0, 0], [0, 0, 1], [1, 0, 0]] with top_k=1
//[[1, 1, 0], [1, 1, 0], [1, 1, 0], [0, 1, 1], [1, 0, 1]] with top_k=2
&Default::default(),
),
)
}
ClassificationType::Multilabel => {
(
Tensor::from_data(
[[1, 1, 0], [1, 0, 1], [1, 1, 1], [0, 0, 1], [1, 0, 0]],
&Default::default(),
),
Tensor::from_data(
[
[0.1, 0.7, 0.6],
[0.3, 0.9, 0.05],
[0.8, 0.9, 0.4],
[0.7, 0.5, 0.9],
[1.0, 0.3, 0.2],
],
//[[0, 1, 1], [0, 1, 0], [1, 1, 0], [1, 0, 1], [1, 0, 0]] with threshold=0.5
&Default::default(),
),
)
}
};
ClassificationInput::new(predictions, targets)
}
}
26 changes: 26 additions & 0 deletions crates/burn-train/src/metric/classification.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use burn_core::prelude::{Backend, Bool, Tensor};

/// Input for classification tasks.
#[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>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With your changes in #2413 we should be able to accept targets that are not one-hot encoded (e.g., [1, 0, 2, 2, 0] instead of [[0, 1, 0], [1, 0, 0], [0, 0, 1], [0, 0, 1], [1, 0, 0]]) and have it configurable for the metric.

But this can be done in a follow-up PR 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, I've been looking at the existing code in burn-trai/src/learner/classification and I think it would be easier to just use the adaptor to convert between bin/multiclass/multilable outputs to the general one hot encoded metrics, non?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah my comment was not meant to say that this is where is should be handled specifically 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah of course, I just wanted to get your thoughts on what I had thought to do. Now we have the ClassificationOutput and MultiLabelClassificationOutput adapted for ClassificationInput. It works but I'm still not super happy about it since then the user is able to use, for example BinaryPrecisionMetric with a MultiLabelClassificationOutput. My idea for the future would be to have separated Inputs and Outputs for each of the classification types such that this would not be possible and would complain at compile time. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand where you're coming from! But I don't see a straightforward way to do this.. we would have to have a different implementation for the binary, multiclass and multilabel precision metrics because the input is an associated type for the Metric trait.

For now I think it's fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I couldn't think of a way around it but maybe it'll come to me while working on other metrics. 🤞

}

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

/// Class Averaging types for Classification metrics.
#[derive(Copy, Clone, Default)]
pub enum ClassReduction {
///Computes the statistics over all classes before averaging
#[default]
Micro,
///Computes the statistics independently for each class before averaging
Macro,
}
Loading