forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added accuracy calculation
Showing
6 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include <deepworks/tensor.hpp> | ||
|
||
namespace deepworks { | ||
namespace metric { | ||
|
||
float accuracy(const Tensor& y_pred, const Tensor& y_true); | ||
|
||
float sparse_accuracy(const Tensor& y_pred, const Tensor& y_true); | ||
|
||
} // namespace metric | ||
} // namespace deepworks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <numeric> | ||
#include <algorithm> | ||
|
||
#include <Eigen/Core> | ||
#include <deepworks/metrics.hpp> | ||
#include "util/assert.hpp" | ||
|
||
|
||
using ConstMatrix = Eigen::Map<const Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>; | ||
using ConstVector = Eigen::Map<const Eigen::Matrix<float, 1, Eigen::Dynamic, Eigen::RowMajor>>; | ||
|
||
float deepworks::metric::accuracy(const deepworks::Tensor& y_pred, | ||
const deepworks::Tensor& y_true) { | ||
const auto& shape = y_pred.shape(); | ||
DeepWorks_Assert(shape.size() == 2); | ||
int rows = shape[0]; | ||
int cols = shape[1]; | ||
DeepWorks_Assert(y_true.total() == rows); | ||
|
||
ConstMatrix y_pred_mat(y_pred.data(), rows, cols); | ||
ConstVector y_true_vec(y_true.data(), rows); | ||
|
||
float acc = 0; | ||
ConstMatrix::Index pred_col; | ||
// FIXME: Calculate without loop | ||
for (int i = 0; i < rows; i++) { | ||
y_pred_mat.row(i).maxCoeff(&pred_col); | ||
auto label = y_true_vec[i]; | ||
acc += pred_col == label; | ||
} | ||
return acc / rows; | ||
} | ||
|
||
float deepworks::metric::sparse_accuracy(const deepworks::Tensor& y_pred, | ||
const deepworks::Tensor& y_true) { | ||
const auto& shape = y_pred.shape(); | ||
DeepWorks_Assert(y_true.shape() == shape); | ||
DeepWorks_Assert(shape.size() == 2); | ||
int rows = shape[0]; | ||
int cols = shape[1]; | ||
|
||
ConstMatrix y_pred_mat(y_pred.data(), rows, cols); | ||
ConstMatrix y_true_mat(y_true.data(), rows, cols); | ||
float acc = 0; | ||
ConstMatrix::Index pred_col, label_col; | ||
// FIXME: Calculate without loop | ||
for (int i = 0; i < rows; i++) { | ||
y_pred_mat.row(i).maxCoeff(&pred_col); | ||
y_true_mat.row(i).maxCoeff(&label_col); | ||
acc += pred_col == label_col; | ||
} | ||
return acc / rows; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <deepworks/tensor.hpp> | ||
#include <deepworks/metrics.hpp> | ||
|
||
namespace dw = deepworks; | ||
|
||
TEST(TestMetrics, accuracy) { | ||
std::vector<float> labels = {0, 1, 0, 0}; | ||
std::vector<float> predict = { | ||
0.56, 0.44, | ||
0.03, 0.97, | ||
0.57, 0.43, | ||
0.18, 0.82, | ||
}; | ||
|
||
dw::Tensor y_pred(dw::Shape{4, 2}); | ||
dw::Tensor y_true(dw::Shape{4}); | ||
|
||
std::copy(labels.begin(), labels.end(), y_true.data()); | ||
std::copy(predict.begin(), predict.end(), y_pred.data()); | ||
|
||
float expected = 0.75; | ||
float acc = dw::metric::accuracy(y_pred, y_true); | ||
EXPECT_FLOAT_EQ(expected, acc); | ||
} | ||
|
||
TEST(TestMetrics, sparse_accuracy) { | ||
std::vector<float> labels = { | ||
1, 0, | ||
0, 1, | ||
0, 1, | ||
1, 0, | ||
}; | ||
std::vector<float> predict = { | ||
0.56, 0.44, | ||
0.03, 0.97, | ||
0.57, 0.43, | ||
0.18, 0.82, | ||
}; | ||
|
||
dw::Tensor y_pred(dw::Shape{4, 2}); | ||
dw::Tensor y_true(dw::Shape{4, 2}); | ||
|
||
std::copy(labels.begin(), labels.end(), y_true.data()); | ||
std::copy(predict.begin(), predict.end(), y_pred.data()); | ||
|
||
float expected = 0.5; | ||
float acc = dw::metric::sparse_accuracy(y_pred, y_true); | ||
EXPECT_FLOAT_EQ(expected, acc); | ||
} |