LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
load_model.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 // load_model .hpp .cpp - Callbacks to load pretrained model(s)
28 
29 #ifndef LBANN_CALLBACKS_CALLBACK_LOAD_MODEL_HPP_INCLUDED
30 #define LBANN_CALLBACKS_CALLBACK_LOAD_MODEL_HPP_INCLUDED
31 
32 #include <utility>
33 
35 
36 #include <google/protobuf/message.h>
37 
38 // Forward-declare protobuf classes
39 namespace lbann_data {
40 class Model;
41 }
42 
43 namespace lbann {
44 namespace callback {
45 
49 class load_model : public callback_base
50 {
51 public:
56  load_model(std::vector<std::string> dirs, std::string extension = "prototext")
57  : callback_base(),
58  m_dirs(std::move(dirs)),
59  m_extension(std::move(extension)),
60  m_loaded(false)
61  {}
62  load_model(const load_model&) = default;
63  load_model& operator=(const load_model&) = default;
64  load_model* copy() const override { return new load_model(*this); }
65 
66  inline void add_dir(const std::string& dir) { m_dirs.emplace_back(dir); }
67 
68  void on_train_begin(model* m) override;
69 
70  void on_test_begin(model* m) override;
71 
72  std::string name() const override { return "load model"; }
73 
75 
78  template <class Archive>
79  void serialize(Archive& ar);
80 
82 
83 private:
84  friend class lbann::model;
85  friend class cereal::access;
86  load_model() = default;
87 
88 private:
90  void write_specific_proto(lbann_data::Callback& proto) const final;
91 
92  std::vector<std::string> m_dirs; // director(ies) to load pretrained model(s)
94  std::string m_extension; // file extension
95 
97  bool m_loaded;
98 };
99 
100 // Builder function
101 std::unique_ptr<callback_base>
102 build_load_model_callback_from_pbuf(const google::protobuf::Message&,
103  std::shared_ptr<lbann_summary> const&);
104 
105 } // namespace callback
106 } // namespace lbann
107 
108 #endif // LBANN_CALLBACKS_CALLBACK_LOAD_MODEL_HPP_INCLUDED
std::unique_ptr< callback_base > build_load_model_callback_from_pbuf(const google::protobuf::Message &, std::shared_ptr< lbann_summary > const &)
std::string name() const override
Return this callback&#39;s name.
Definition: load_model.hpp:72
void serialize(std::ostream &os, google::protobuf::Message const &msg)
Serialize the protobuf message to a stream.
std::string m_extension
Disables the normal behavior of saving when training is complete.
Definition: load_model.hpp:94
void add_dir(const std::string &dir)
Definition: load_model.hpp:66
bool m_loaded
Flag to indicate if the model has already been loaded.
Definition: load_model.hpp:97
Base class for callbacks during training/testing.
Definition: callback.hpp:76
std::vector< std::string > m_dirs
Definition: load_model.hpp:92
Abstract base class for neural network models.
Definition: model.hpp:83
load_model * copy() const override
Definition: load_model.hpp:64
load_model(std::vector< std::string > dirs, std::string extension="prototext")
Definition: load_model.hpp:56