LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
data_reader_numpy_npz.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.
25 //
26 // lbann_data_reader_numpy_npz .hpp .cpp - generic_data_reader class for numpy
27 // .npz dataset
29 
30 #ifndef LBANN_DATA_READER_NUMPY_NPZ_HPP
31 #define LBANN_DATA_READER_NUMPY_NPZ_HPP
32 
33 #include "data_reader.hpp"
34 #include "data_reader_numpy.hpp"
35 #include <cnpy.h>
36 
37 namespace lbann {
45 {
46 public:
47  numpy_npz_reader(const bool shuffle);
48  // These need to be explicit because of some issue with the cnpy copy
49  // constructor/assignment operator not linking correctly otherwise.
52  ~numpy_npz_reader() override {}
53 
54  numpy_npz_reader* copy() const override
55  {
56  return new numpy_npz_reader(*this);
57  }
58 
59  std::string get_type() const override { return "numpy_npz_reader"; }
60 
63 
64  void load() override;
65 
66  int get_num_labels() const override { return m_num_labels; }
67  int get_num_responses() const override
68  {
70  }
71  int get_linearized_data_size() const override { return m_num_features; }
72  int get_linearized_label_size() const override { return m_num_labels; }
73  int get_linearized_response_size() const override
74  {
76  }
77  const std::vector<El::Int> get_data_dims() const override
78  {
79  std::vector<El::Int> dims(m_data.shape.begin() + 1, m_data.shape.end());
80  return dims;
81  }
82 
83 protected:
84  bool fetch_datum(CPUMat& X, int data_id, int mb_idx) override;
85  bool fetch_label(CPUMat& Y, int data_id, int mb_idx) override;
86  bool fetch_response(CPUMat& Y, int data_id, int mb_idx) override;
87 
89  int m_num_samples = 0;
91  int m_num_features = 0;
93  int m_num_labels = 0;
101  cnpy::NpyArray m_data, m_labels, m_responses;
102 
103  // A constant to be multiplied when data is converted
104  // from int16 to DataType.
105  DataType m_scaling_factor_int16 = 1.0;
106 
107 private:
108  // Keys to retrieve data, labels, responses from a given .npz file.
109  static const std::string NPZ_KEY_DATA, NPZ_KEY_LABELS, NPZ_KEY_RESPONSES;
110 };
111 
112 } // namespace lbann
113 
114 #endif // LBANN_DATA_READER_NUMPY_NPZ_HPP
void load() override
int get_linearized_label_size() const override
Get the linearized size (i.e. number of elements) in a label.
const std::vector< El::Int > get_data_dims() const override
Get the dimensions of the data.
numpy_npz_reader(const bool shuffle)
int get_linearized_response_size() const override
Get the linearized size (i.e. number of elements) in a response.
int get_num_responses() const override
Return the number of responses in this dataset.
static const std::string NPZ_KEY_LABELS
numpy_npz_reader & operator=(const numpy_npz_reader &)
bool fetch_label(CPUMat &Y, int data_id, int mb_idx) override
int m_num_labels
Number of label classes.
std::string get_type() const override
bool fetch_response(CPUMat &Y, int data_id, int mb_idx) override
bool fetch_datum(CPUMat &X, int data_id, int mb_idx) override
int m_num_features
Number of features in each sample.
El::Matrix< DataType, El::Device::CPU > CPUMat
Definition: base.hpp:116
int m_num_response_features
Number of features in each response.
int get_linearized_data_size() const override
Get the linearized size (i.e. number of elements) in a sample.
numpy_npz_reader * copy() const override
static const std::string NPZ_KEY_DATA
static const std::string NPZ_KEY_RESPONSES
void set_scaling_factor_int16(DataType s)
Set a scaling factor for int16 data.
int get_num_labels() const override
Return the number of labels (classes) in this dataset.
int m_num_samples
Number of samples.