Beatmup
deserialized_model.h
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 #pragma once
20 
21 // list all operations here to initialize deserializers
22 #include "conv2d.h"
23 #include "pooling2d.h"
24 #include "dense.h"
25 #include "image_sampler.h"
26 #include "softmax.h"
27 
28 #include "model.h"
29 #include "../utils/listing.h"
30 
31 
32 /** \page NNetsModelSerialization NNets model serialization format
33 
34  In %Beatmup::NNets, neural networks can be serialized into and reconstructed from a YAML-like piece of text.
35 
36  The text representation is generated by `beatmup_keras` Python module providing a way to convert a TensorFlow/Keras model into a Beatmup model. Also, any instance of Beatmup::Model can be serialized in the text form by using Beatmup::NNets::Model::serializeToString().
37 
38  Beatmup::NNets::DeserializedModel is suggested to be used to construct a model from its text representation and run the inference.
39 
40  The text representation of a model contains sections \c ops and \c connections listing operations and connections between them. It does not contain model data (e.g., convolution filters and biases), but only the model structure; Beatmup::ChunkCollection and its subclasses are suggested to be used to store the model data.
41 
42  Every operation and connection block is filled with its corresponding parameters values.
43  The keywords mostly correspond to the operations constructors arguments put in snake case (see \ref NNetsOpsSerialization and \ref NNetsConnectionsSerialization for the detailed descriptions).
44  The values are numeric or in plain text.
45 
46  Example:
47  \code{yaml}
48  ops:
49  - _name: convolution
50  _type: conv2d
51  activation: brelu6
52  input_channels: 3
53  kernel_size: 3
54  output_channels: 16
55  use_bias: true
56  - _name: pool
57  _type: pooling2d
58  operator: max
59  size: 2
60  - _name: fc
61  _type: dense
62  output_dims: 4
63  use_bias: true
64  - _name: final
65  _type: softmax
66  connections:
67  - from: convolution
68  to: pool
69  - from: pool
70  to: fc
71  - from: fc
72  to: softmax
73  \endcode
74 
75  Detailed reference:
76 
77  - \subpage NNetsOpsSerialization
78  - \subpage NNetsConnectionsSerialization
79  - \subpage NNetsActivationFunctionsSerialization
80 */
81 
82 namespace Beatmup {
83  namespace NNets {
84  /**
85  Model reconstructed from a serialized representation.
86  The representation format is the one rendered with Model::serialize(): a YAML-like listing containing "ops" and "connections" sections
87  describing the model operations in execution order and connections between them respectively (see \ref NNetsModelSerialization).
88  */
89  class DeserializedModel : public Model {
90  public:
91  class OperationDeserializer;
92  private:
93  std::vector<AbstractOperation*> ownedOps; //!< initially deserialized operations to be destroyed with the model
94 
95  void deserialize(const Listing& listing);
96 
97  public:
98  DeserializedModel(Context& context, const Listing& listing);
99 
100  /**
101  Constructs a model from its serialized representation.
102  The expected representation format is the one rendered with Model::serialize().
103  \param[in] context A context instance the model resources are bound to
104  \param[in] str A string containing the model representation
105  */
106  DeserializedModel(Context& context, const std::string& str);
108  };
109  }
110 }
Basic class: task and memory management, any kind of static data.
Definition: context.h:59
Parser of simple YAML-like listings.
Definition: listing.h:40
Model reconstructed from a serialized representation.
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
JNIEnv jobject jstring str