LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
helper.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 #ifndef LBANN_TRANSFORMS_VISION_UNIT_TEST_HELPER
27 #define LBANN_TRANSFORMS_VISION_UNIT_TEST_HELPER
28 
29 #include <El.hpp>
30 
31 inline void
32 apply_elementwise(El::Matrix<uint8_t>& mat,
33  El::Int height,
34  El::Int width,
35  El::Int channels,
36  std::function<void(uint8_t&, El::Int, El::Int, El::Int)> f)
37 {
38  uint8_t* buf = mat.Buffer();
39  for (El::Int channel = 0; channel < channels; ++channel) {
40  for (El::Int col = 0; col < width; ++col) {
41  for (El::Int row = 0; row < height; ++row) {
42  f(buf[channels * (col + row * width) + channel], row, col, channel);
43  }
44  }
45  }
46 }
47 
48 inline void identity(El::Matrix<uint8_t>& mat,
49  El::Int height,
50  El::Int width,
51  El::Int channels = 1)
52 {
53  mat.Resize(height * width * channels, 1);
55  height,
56  width,
57  channels,
58  [](uint8_t& x, El::Int row, El::Int col, El::Int) {
59  x = (row == col) ? 1 : 0;
60  });
61 }
62 
63 inline void zeros(El::Matrix<uint8_t>& mat,
64  El::Int height,
65  El::Int width,
66  El::Int channels = 1)
67 {
68  mat.Resize(height * width * channels, 1);
69  uint8_t* buf = mat.Buffer();
70  for (El::Int i = 0; i < height * width * channels; ++i) {
71  buf[i] = 0;
72  }
73 }
74 
75 inline void ones(El::Matrix<uint8_t>& mat,
76  El::Int height,
77  El::Int width,
78  El::Int channels = 1)
79 {
80  mat.Resize(height * width * channels, 1);
81  uint8_t* buf = mat.Buffer();
82  for (El::Int i = 0; i < height * width * channels; ++i) {
83  buf[i] = 1;
84  }
85 }
86 
87 inline void print(const El::Matrix<uint8_t>& mat,
88  El::Int height,
89  El::Int width,
90  El::Int channels = 1)
91 {
92  const uint8_t* buf = mat.LockedBuffer();
93  for (El::Int channel = 0; channel < channels; ++channel) {
94  for (El::Int col = 0; col < width; ++col) {
95  for (El::Int row = 0; row < height; ++row) {
96  std::cout << ((int)buf[channels * (col + row * width) + channel])
97  << " ";
98  }
99  std::cout << std::endl;
100  }
101  std::cout << "--" << std::endl;
102  }
103 }
104 
105 #endif // LBANN_TRANSFORMS_VISION_UNIT_TEST_HELPER
void ones(El::Matrix< uint8_t > &mat, El::Int height, El::Int width, El::Int channels=1)
Definition: helper.hpp:75
void identity(El::Matrix< uint8_t > &mat, El::Int height, El::Int width, El::Int channels=1)
Definition: helper.hpp:48
void print(const El::Matrix< uint8_t > &mat, El::Int height, El::Int width, El::Int channels=1)
Definition: helper.hpp:87
void apply_elementwise(El::Matrix< uint8_t > &mat, El::Int height, El::Int width, El::Int channels, std::function< void(uint8_t &, El::Int, El::Int, El::Int)> f)
Definition: helper.hpp:32
void zeros(El::Matrix< uint8_t > &mat, El::Int height, El::Int width, El::Int channels=1)
Definition: helper.hpp:63