ATLAS Offline Software
Loading...
Searching...
No Matches
AthExTriton/src/EvaluateUtils.cxx
Go to the documentation of this file.
1// Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3#include "EvaluateUtils.h"
4
5#include <arpa/inet.h>
6
7#include <algorithm>
8#include <cstdint>
9#include <format>
10#include <fstream>
11#include <numeric>
12
13namespace EvaluateUtils {
14//*******************************************************************
15// for reading MNIST images
16std::vector<std::vector<std::vector<float>>> read_mnist_pixel_notFlat(
17 const std::string& full_path) {
18 std::ifstream file(full_path.c_str(), std::ios::binary);
19 std::int32_t magic_number = 0;
20 std::int32_t number_of_images = 0;
21 std::int32_t n_rows = 0;
22 std::int32_t n_cols = 0;
23
24 file.read(reinterpret_cast<char*>(&magic_number), sizeof(magic_number));
25 // This file format stores numbers in big-endian (network) ordering
26 magic_number = ntohl(magic_number);
27
28 // The "magic number" encodes the data type and number of dimensions.
29 // This therefore has to be 0x0803 to be a set of images with unsigned byte
30 // pixels
31 const std::int32_t required_magic = 0x0803;
32 if (magic_number != required_magic) {
33 throw std::runtime_error(
34 std::format("The MNIST input file at {} has the magic number {:#06x}! "
35 "Expected {:#06x}.",
36 full_path, magic_number, required_magic));
37 }
38
39 // The next three fields are the number of images, and the number of rows and
40 // columns per image
41 file.read(reinterpret_cast<char*>(&number_of_images),
42 sizeof(number_of_images));
43 number_of_images = ntohl(number_of_images);
44 file.read(reinterpret_cast<char*>(&n_rows), sizeof(n_rows));
45 n_rows = ntohl(n_rows);
46 if (n_rows < 1) {
47 throw std::runtime_error(
48 std::format("The MNIST input file at {} has images with {} rows",
49 full_path, n_rows));
50 }
51 file.read(reinterpret_cast<char*>(&n_cols), sizeof(n_cols));
52 n_cols = ntohl(n_cols);
53 if (n_cols < 1) {
54 throw std::runtime_error(
55 std::format("The MNIST input file at {} has images with {} columns",
56 full_path, n_cols));
57 }
58
59 // Now we can make the vector
60 std::vector<std::vector<std::vector<float>>> input_tensor_values;
61 input_tensor_values.resize(
62 number_of_images,
63 std::vector<std::vector<float>>(n_rows, std::vector<float>(n_cols)));
64 for (int i = 0; i < number_of_images; ++i) {
65 for (int r = 0; r < n_rows; ++r) {
66 for (int c = 0; c < n_cols; ++c) {
67 std::uint8_t temp = 0;
68 file.read((char*)&temp, sizeof(temp));
69 input_tensor_values[i][r][c] =
70 float(temp) / std::numeric_limits<std::uint8_t>::max();
71 }
72 }
73 }
74 return input_tensor_values;
75}
76
77std::vector<float> flattenNestedVectors(
78 const std::vector<std::vector<float>>& nestedVector) {
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}
91} // namespace EvaluateUtils
int r
Definition globals.cxx:22
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)
TFile * file