ATLAS Offline Software
Loading...
Searching...
No Matches
EvaluateUtils Namespace Reference

Functions

std::vector< std::vector< std::vector< float > > > read_mnist_pixel_notFlat (const std::string &full_path)
std::vector< float > flattenNestedVectors (const std::vector< std::vector< float > > &nestedVector)

Function Documentation

◆ flattenNestedVectors()

std::vector< float > EvaluateUtils::flattenNestedVectors ( const std::vector< std::vector< float > > & nestedVector)

Definition at line 77 of file AthExTriton/src/EvaluateUtils.cxx.

78 {
79 const std::size_t total_size = std::transform_reduce(
80 nestedVector.cbegin(), nestedVector.cend(), 0, std::plus<>{},
81 [](const std::vector<float>& c) { return c.size(); });
82
83 std::vector<float> result{};
84 result.reserve(total_size);
85
86 for (const auto& v : nestedVector) {
87 std::ranges::copy(v, std::back_inserter(result));
88 }
89 return result;
90}

◆ read_mnist_pixel_notFlat()

std::vector< std::vector< std::vector< float > > > EvaluateUtils::read_mnist_pixel_notFlat ( const std::string & full_path)

Definition at line 14 of file AthExOnnxRuntime/src/EvaluateUtils.cxx.

15 {
16 std::ifstream file(full_path.c_str(), std::ios::binary);
17 std::int32_t magic_number = 0;
18 std::int32_t number_of_images = 0;
19 std::int32_t n_rows = 0;
20 std::int32_t n_cols = 0;
21
22 file.read(reinterpret_cast<char*>(&magic_number), sizeof(magic_number));
23 // This file format stores numbers in big-endian (network) ordering
24 magic_number = ntohl(magic_number);
25
26 // The "magic number" encodes the data type and number of dimensions.
27 // This therefore has to be 0x0803 to be a set of images with unsigned byte
28 // pixels
29 const std::int32_t required_magic = 0x0803;
30 if (magic_number != required_magic) {
31 throw std::runtime_error(
32 std::format("The MNIST input file at {} has the magic number {:#06x}! "
33 "Expected {:#06x}.",
34 full_path, magic_number, required_magic));
35 }
36
37 // The next three fields are the number of images, and the number of rows and
38 // columns per image
39 file.read(reinterpret_cast<char*>(&number_of_images),
40 sizeof(number_of_images));
41 number_of_images = ntohl(number_of_images);
42 file.read(reinterpret_cast<char*>(&n_rows), sizeof(n_rows));
43 n_rows = ntohl(n_rows);
44 if (n_rows < 1) {
45 throw std::runtime_error(
46 std::format("The MNIST input file at {} has images with {} rows",
47 full_path, n_rows));
48 }
49 file.read(reinterpret_cast<char*>(&n_cols), sizeof(n_cols));
50 n_cols = ntohl(n_cols);
51 if (n_cols < 1) {
52 throw std::runtime_error(
53 std::format("The MNIST input file at {} has images with {} columns",
54 full_path, n_cols));
55 }
56
57 // Now we can make the vector
58 std::vector<std::vector<std::vector<float>>> input_tensor_values;
59 input_tensor_values.resize(
60 number_of_images,
61 std::vector<std::vector<float>>(n_rows, std::vector<float>(n_cols)));
62 for (int i = 0; i < number_of_images; ++i) {
63 for (int r = 0; r < n_rows; ++r) {
64 for (int c = 0; c < n_cols; ++c) {
65 std::uint8_t temp = 0;
66 file.read((char*)&temp, sizeof(temp));
67 input_tensor_values[i][r][c] =
68 float(temp) / std::numeric_limits<std::uint8_t>::max();
69 }
70 }
71 }
72 return input_tensor_values;
73}
int r
Definition globals.cxx:22
TFile * file