LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
summarize_images.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 // summarize_images .hpp .cpp - Callback hooks to dump
27 // results of image testing to event files
29 
30 #ifndef LBANN_CALLBACKS_SUMMARIZE_IMAGES_HPP_INCLUDED
31 #define LBANN_CALLBACKS_SUMMARIZE_IMAGES_HPP_INCLUDED
32 
34 
35 #include <google/protobuf/message.h>
36 #include <lbann/base.hpp>
37 #include <string>
38 #include <vector>
39 
40 // Forward-declare protobuf classes
41 namespace lbann_data {
42 class Callback_CallbackSummarizeImages;
43 }
44 
45 namespace lbann {
46 namespace callback {
47 
53 {
54 
55 public:
56  virtual std::vector<std::pair<size_t, El::Int>>
57  get_image_indices(model const&) const = 0;
58  virtual std::string get_tag(std::string const& layer_name,
59  El::Int index,
60  El::Int epoch) const = 0;
61  virtual void write_strategy_proto(
62  lbann_data::Callback_CallbackSummarizeImages& msg) const = 0;
63  virtual ~image_output_strategy() = default;
64 
65 }; // class image_output_strategy
66 
72 {
73 public:
74  enum class MatchType
75  {
76  NOMATCH = 0,
77  MATCH = 1,
78  ALL = 2
79  }; // enum class MatchType
80 
86  categorical_accuracy_strategy(std::string const& cat_accuracy_layer_name,
87  MatchType match_type = MatchType::NOMATCH,
88  size_t num_images = 10)
89  : m_cat_accuracy_layer_name(cat_accuracy_layer_name),
90  m_match_type(match_type),
91  m_num_images(num_images)
92  {}
93 
97  std::vector<std::pair<size_t, El::Int>>
98  get_image_indices(model const& m) const final;
99 
101  std::string get_tag(std::string const& layer_name,
102  El::Int index,
103  El::Int epoch) const final;
104 
105 private:
107  void write_strategy_proto(
108  lbann_data::Callback_CallbackSummarizeImages& msg) const final;
109 
113  bool meets_criteria(const DataType& match) const noexcept;
114 
116  std::string const m_cat_accuracy_layer_name;
117 
120 
122  size_t m_num_images;
123 
124 }; // class categorical_accuracy_strategy : image_output_strategy
125 
126 std::unique_ptr<image_output_strategy>
127 build_categorical_accuracy_strategy_from_pbuf(google::protobuf::Message const&);
128 
134 {
135 
136 public:
141  autoencoder_strategy(std::string const& input_layer_name,
142  size_t num_images = 10)
143  : m_input_layer_name{input_layer_name}, m_num_images{num_images}
144  {}
145 
149  std::vector<std::pair<size_t, El::Int>>
150  get_image_indices(model const& m) const final;
151 
153  std::string get_tag(std::string const& layer_name,
154  El::Int index,
155  El::Int epoch) const final;
156 
158  void write_strategy_proto(
159  lbann_data::Callback_CallbackSummarizeImages& msg) const final;
160 
161 private:
163  std::string m_input_layer_name;
164 
166  size_t m_num_images;
167 
169  mutable std::unordered_set<El::Int> m_tracked_images;
170 
172  mutable std::unordered_map<model const*, std::vector<size_t>>
174 
175 }; // class Autoencoder : image_output_strategy
176 
177 std::unique_ptr<image_output_strategy>
178 build_track_sample_ids_strategy_from_pbuf(google::protobuf::Message const&);
179 
184 {
185 
186 public:
194  summarize_images(std::shared_ptr<lbann_summary> const& summarizer,
195  std::unique_ptr<image_output_strategy> strategy,
196  std::string const& img_source_layer_name,
197  uint64_t interval = 1,
198  std::string const& img_format = ".jpg");
199 
201  callback_base* copy() const override;
202 
204  std::string name() const override { return "summarize_images"; }
205 
207  void on_batch_evaluate_end(model* m) override;
208 
210  void write_specific_proto(lbann_data::Callback& proto) const final;
211 
212 private:
214  void dump_images_to_summary(model const& m) const;
215 
216 private:
217  /* @brief lbann_summary object */
218  std::shared_ptr<lbann_summary> m_summarizer;
219 
220  /* @brief image_output_strategy object */
221  std::unique_ptr<image_output_strategy> m_strategy;
222 
223  /* @brief Names of layers */
225 
226  /* @brief Interval for dumping images */
228 
230  std::string m_img_format;
231 
232 }; // class summarize_images
233 
238 Layer const& get_layer_by_name(model const& m, std::string const& layer_name);
239 
240 std::unique_ptr<callback_base> build_summarize_images_callback_from_pbuf(
241  const google::protobuf::Message&,
242  const std::shared_ptr<lbann_summary>& summarizer);
243 
244 } // namespace callback
245 } // namespace lbann
246 
247 #endif // LBANN_CALLBACKS_SUMMARIZE_IMAGES_HPP_INCLUDED
std::unique_ptr< image_output_strategy > build_track_sample_ids_strategy_from_pbuf(google::protobuf::Message const &)
std::unique_ptr< image_output_strategy > build_categorical_accuracy_strategy_from_pbuf(google::protobuf::Message const &)
autoencoder_strategy(std::string const &input_layer_name, size_t num_images=10)
Constructor.
size_t m_num_images
Number of images to be tracked.
std::string const m_cat_accuracy_layer_name
Name of categorical accuracy layer.
std::string m_img_format
Image file format. Valid options: .jpg, .png, .pgm.
Neural network tensor operation.
Definition: layer.hpp:285
Layer const & get_layer_by_name(model const &m, std::string const &layer_name)
Get a layer from model based on name.
std::shared_ptr< lbann_summary > m_summarizer
std::unordered_map< model const *, std::vector< size_t > > m_shuffled_indices
A map from models to shuffled indices.
Base class for callbacks during training/testing.
Definition: callback.hpp:76
categorical_accuracy_strategy(std::string const &cat_accuracy_layer_name, MatchType match_type=MatchType::NOMATCH, size_t num_images=10)
summarize_images Constructor.
MatchType m_match_type
Criterion to dump images.
Abstract base class for neural network models.
Definition: model.hpp:83
size_t m_num_images
Number of images to be dumped per epoch.
Subclass of image_output_strategy to dump autoencoder images.
std::string m_input_layer_name
Name of input layer.
Callback to dump images to event files based on strategy.
std::unordered_set< El::Int > m_tracked_images
Sample indices of images to track.
std::string name() const override
Return name of callback.
std::unique_ptr< image_output_strategy > m_strategy
Interface for strategies for determining which images to output to the summarizer.
std::unique_ptr< callback_base > build_summarize_images_callback_from_pbuf(const google::protobuf::Message &, const std::shared_ptr< lbann_summary > &summarizer)