/*
    Beatmup image and signal processing library
    Copyright (C) 2020, lnstadrum

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

// list all operations here to initialize deserializers
#include "conv2d.h"
#include "pooling2d.h"
#include "dense.h"
#include "image_sampler.h"
#include "softmax.h"

#include "model.h"
#include "../utils/listing.h"


/** \page NNetsModelSerialization  NNets model serialization format

    In %Beatmup::NNets, neural networks can be serialized into and reconstructed from a YAML-like piece of text.

    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().

    Beatmup::NNets::DeserializedModel is suggested to be used to construct a model from its text representation and run the inference.

    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.

    Every operation and connection block is filled with its corresponding parameters values.
    The keywords mostly correspond to the operations constructors arguments put in snake case (see \ref NNetsOpsSerialization and \ref NNetsConnectionsSerialization for the detailed descriptions).
    The values are numeric or in plain text.

    Example:
    \code{yaml}
    ops:
    - _name: convolution
        _type: conv2d
        activation: brelu6
        input_channels: 3
        kernel_size: 3
        output_channels: 16
        use_bias: true
    - _name: pool
        _type: pooling2d
        operator: max
        size: 2
    - _name: fc
        _type: dense
        output_dims: 4
        use_bias: true
    - _name: final
        _type: softmax
    connections:
    - from: convolution
        to: pool
    - from: pool
        to: fc
    - from: fc
        to: softmax
    \endcode

    Detailed reference:

    - \subpage NNetsOpsSerialization
    - \subpage NNetsConnectionsSerialization
    - \subpage NNetsActivationFunctionsSerialization
*/

namespace Beatmup {
    namespace NNets {
        /**
            Model reconstructed from a serialized representation.
            The representation format is the one rendered with Model::serialize(): a YAML-like listing containing "ops" and "connections" sections
            describing the model operations in execution order and connections between them respectively (see \ref NNetsModelSerialization).
        */
        class DeserializedModel : public Model {
        public:
            class OperationDeserializer;
        private:
            std::vector<AbstractOperation*> ownedOps;       //!< initially deserialized operations to be destroyed with the model

            void deserialize(const Listing& listing);

        public:
            DeserializedModel(Context& context, const Listing& listing);

            /**
                Constructs a model from its serialized representation.
                The expected representation format is the one rendered with Model::serialize().
                \param[in] context      A context instance the model resources are bound to
                \param[in] str          A string containing the model representation
            */
            DeserializedModel(Context& context, const std::string& str);
            ~DeserializedModel();
        };
    }
}