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
26
void
DeserializedModel::deserialize
(
const
Listing
& listing) {
27
InvalidArgument::check
(listing.
has
(
"ops"
),
"The listing has no 'ops'"
);
28
InvalidArgument::check
(listing.
has
(
"connections"
),
"The listing has no 'connections'"
);
29
30
const
auto
& theMap =
AbstractOperation::Deserializer::getDeserializersMap
();
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)
50
addConnection
(
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
60
DeserializedModel::DeserializedModel
(
Context
& context,
const
Listing
& listing):
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
76
DeserializedModel::~DeserializedModel
() {
77
for
(
auto
op
:
ownedOps
) {
78
ops
.erase(
std::remove
(
ops
.begin(),
ops
.end(),
op
),
ops
.end());
79
delete
op
;
80
}
81
}
Beatmup::Context
Basic class: task and memory management, any kind of static data.
Definition:
context.h:59
Beatmup::GL::ProgramBank::context
Context & context
Definition:
program_bank.h:44
Beatmup::InvalidArgument
Definition:
exception.h:70
Beatmup::InvalidArgument::check
static void check(const bool condition, const std::string &message)
Definition:
exception.h:75
Beatmup::Listing
Parser of simple YAML-like listings.
Definition:
listing.h:40
Beatmup::Listing::has
bool has(const std::string &key) const
Returns true if a specific chapter is present in the listing.
Definition:
listing.h:132
Beatmup::NNets::AbstractOperation::Deserializer::getDeserializersMap
static std::map< std::string, Deserializer * > & getDeserializersMap()
Definition:
operation.cpp:83
Beatmup::NNets::DeserializedModel::DeserializedModel
DeserializedModel(Context &context, const Listing &listing)
Definition:
deserialized_model.cpp:60
Beatmup::NNets::DeserializedModel::~DeserializedModel
~DeserializedModel()
Definition:
deserialized_model.cpp:76
Beatmup::NNets::DeserializedModel::deserialize
void deserialize(const Listing &listing)
Definition:
deserialized_model.cpp:26
Beatmup::NNets::DeserializedModel::ownedOps
std::vector< AbstractOperation * > ownedOps
initially deserialized operations to be destroyed with the model
Definition:
deserialized_model.h:91
Beatmup::NNets::Model
Neural net model.
Definition:
model.h:92
Beatmup::NNets::Model::ops
std::vector< AbstractOperation * > ops
model operations
Definition:
model.h:122
Beatmup::NNets::Model::addConnection
void addConnection(AbstractOperation &source, AbstractOperation &dest, int output=0, int input=0, int shuffle=0)
Definition:
model.cpp:91
Beatmup::NNets::Model::append
void append(AbstractOperation *newOp, bool connect=false)
Adds a new operation to the model.
Definition:
model.cpp:47
deserialized_model.h
Beatmup
Definition:
basic_types.h:22
std::to_string
std::string to_string(Beatmup::NNets::ActivationFunction function)
remove
seq remove(start, end)
op
jlong jint op
Definition:
wrapper_imag.cpp:744
str
JNIEnv jobject jstring str
Definition:
wrapper_nnets.cpp:81
core
nnets
deserialized_model.cpp
Generated on Tue Nov 21 2023 13:54:27 for Beatmup by
1.9.1