Beatmup
deserialized_model.cpp
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "deserialized_model.h"
20 #include <sstream>
21 
22 using namespace Beatmup;
23 using namespace NNets;
24 
25 
27  InvalidArgument::check(listing.has("ops"), "The listing has no 'ops'");
28  InvalidArgument::check(listing.has("connections"), "The listing has no 'connections'");
29 
31 
32  // process ops
33  const auto& ops = listing["ops"];
34  for (const auto& op : ops) {
35  // find a deserializer
36  const std::string type = op["_type"];
37  auto john = theMap.find(type);
38  if (john == theMap.end())
39  throw InvalidArgument("Cannot find deserializer of type " + type + " declared in operation starting at line " + std::to_string(op.getLineNumber()));
40 
41  // create op
42  auto newOp = john->second->deserialize(context, op);
43  ownedOps.push_back(newOp);
44  append(newOp, false);
45  }
46 
47  // process connections
48  const auto conns = listing["connections"];
49  for (const auto& conn : conns)
51  conn["from"],
52  conn["to"],
53  conn.get<int>("from_output", 0),
54  conn.get<int>("to_input", 0),
55  conn.get<int>("shuffle", 0)
56  );
57 }
58 
59 
61  Model(context)
62 {
63  deserialize(listing);
64 }
65 
66 
67 DeserializedModel::DeserializedModel(Context& context, const std::string& str):
68  Model(context)
69 {
70  std::istringstream strstr(str);
71  Listing listing(strstr);
72  deserialize(listing);
73 }
74 
75 
77  for (auto op : ownedOps) {
78  ops.erase(std::remove(ops.begin(), ops.end(), op), ops.end());
79  delete op;
80  }
81 }
Basic class: task and memory management, any kind of static data.
Definition: context.h:59
static void check(const bool condition, const std::string &message)
Definition: exception.h:75
Parser of simple YAML-like listings.
Definition: listing.h:40
bool has(const std::string &key) const
Returns true if a specific chapter is present in the listing.
Definition: listing.h:132
static std::map< std::string, Deserializer * > & getDeserializersMap()
Definition: operation.cpp:83
DeserializedModel(Context &context, const Listing &listing)
void deserialize(const Listing &listing)
std::vector< AbstractOperation * > ownedOps
initially deserialized operations to be destroyed with the model
Neural net model.
Definition: model.h:92
std::vector< AbstractOperation * > ops
model operations
Definition: model.h:122
void addConnection(AbstractOperation &source, AbstractOperation &dest, int output=0, int input=0, int shuffle=0)
Definition: model.cpp:91
void append(AbstractOperation *newOp, bool connect=false)
Adds a new operation to the model.
Definition: model.cpp:47
std::string to_string(Beatmup::NNets::ActivationFunction function)
seq remove(start, end)
jlong jint op
JNIEnv jobject jstring str