From 5778d2065dbc550b6dc979b894255912a8927518 Mon Sep 17 00:00:00 2001 From: Parichay Kapoor Date: Thu, 23 Jul 2020 14:31:07 +0900 Subject: [PATCH] [C-API] C-API updated with tizen ACR Updating C-API with tizen ACR Also add corresponding changes in the package Signed-off-by: Parichay Kapoor --- Applications/Classification/jni/main_func.cpp | 8 +- Applications/Tizen_CAPI/capi_func.c | 13 +- Applications/mnist/jni/main.cpp | 8 +- api/capi/doc/nntrainer_doc.h | 2 +- api/capi/include/nntrainer.h | 215 ++++++++++++++++---------- api/nntrainer-api-common.h | 113 ++++++++++++-- nntrainer/src/databuffer_func.cpp | 2 +- test/include/nntrainer_test_util.h | 8 +- test/input_gen/genInput.py | 6 +- test/nntrainer_test_util.cpp | 8 +- 10 files changed, 271 insertions(+), 112 deletions(-) diff --git a/Applications/Classification/jni/main_func.cpp b/Applications/Classification/jni/main_func.cpp index f6a84f3..bdf5097 100644 --- a/Applications/Classification/jni/main_func.cpp +++ b/Applications/Classification/jni/main_func.cpp @@ -144,9 +144,11 @@ bool getData(std::ifstream &F, std::vector &outVec, * @param[out] outVec * @param[out] outLabel * @param[out] last if the data is finished + * @param[in] user_data private data for the callback * @retval status for handling error */ -int getMiniBatch_train(float **outVec, float **outLabel, bool *last) { +int getMiniBatch_train(float **outVec, float **outLabel, bool *last, + void *user_data) { std::vector memI; std::vector memJ; unsigned int count = 0; @@ -203,9 +205,11 @@ int getMiniBatch_train(float **outVec, float **outLabel, bool *last) { * @param[out] outVec * @param[out] outLabel * @param[out] last if the data is finished + * @param[in] user_data private data for the callback * @retval status for handling error */ -int getMiniBatch_val(float **outVec, float **outLabel, bool *last) { +int getMiniBatch_val(float **outVec, float **outLabel, bool *last, + void *user_data) { std::vector memI; std::vector memJ; diff --git a/Applications/Tizen_CAPI/capi_func.c b/Applications/Tizen_CAPI/capi_func.c index ca489b9..3b7ac93 100644 --- a/Applications/Tizen_CAPI/capi_func.c +++ b/Applications/Tizen_CAPI/capi_func.c @@ -34,8 +34,9 @@ static bool alloc_val = false; unsigned int seed; -int gen_data_train(float **outVec, float **outLabel, bool *last); -int gen_data_val(float **outVec, float **outLabel, bool *last); +int gen_data_train(float **outVec, float **outLabel, bool *last, + void *user_data); +int gen_data_val(float **outVec, float **outLabel, bool *last, void *user_data); bool file_exists(const char *filename); bool file_exists(const char *filename) { @@ -131,9 +132,11 @@ static bool get_data(const char *file_name, float *outVec, float *outLabel, * @param[out] outVec * @param[out] outLabel * @param[out] status for error handling + * @param[in] user_data private data for the callback * @retval true/false */ -int gen_data_train(float **outVec, float **outLabel, bool *last) { +int gen_data_train(float **outVec, float **outLabel, bool *last, + void *user_data) { int memI[mini_batch]; long file_size; unsigned int count = 0; @@ -213,9 +216,11 @@ int gen_data_train(float **outVec, float **outLabel, bool *last) { * @param[out] outVec * @param[out] outLabel * @param[out] last if the data is finished + * @param[in] user_data private data for the callback * @retval status for handling error */ -int gen_data_val(float **outVec, float **outLabel, bool *last) { +int gen_data_val(float **outVec, float **outLabel, bool *last, + void *user_data) { int memI[mini_batch]; unsigned int i, j; diff --git a/Applications/mnist/jni/main.cpp b/Applications/mnist/jni/main.cpp index 4e21773..f0394b5 100644 --- a/Applications/mnist/jni/main.cpp +++ b/Applications/mnist/jni/main.cpp @@ -115,9 +115,11 @@ bool getData(std::ifstream &F, std::vector &outVec, * @param[out] outVec * @param[out] outLabel * @param[out] last if the data is finished + * @param[in] user_data private data for the callback * @retval status for handling error */ -int getMiniBatch_train(float **outVec, float **outLabel, bool *last) { +int getMiniBatch_train(float **outVec, float **outLabel, bool *last, + void *user_data) { std::vector memI; std::vector memJ; unsigned int count = 0; @@ -160,9 +162,11 @@ int getMiniBatch_train(float **outVec, float **outLabel, bool *last) { * @param[out] outVec * @param[out] outLabel * @param[out] last if the data is finished + * @param[in] user_data private data for the callback * @retval status for handling error */ -int getMiniBatch_val(float **outVec, float **outLabel, bool *last) { +int getMiniBatch_val(float **outVec, float **outLabel, bool *last, + void *user_data) { std::vector memI; std::vector memJ; diff --git a/api/capi/doc/nntrainer_doc.h b/api/capi/doc/nntrainer_doc.h index 298bc85..9fa783b 100644 --- a/api/capi/doc/nntrainer_doc.h +++ b/api/capi/doc/nntrainer_doc.h @@ -22,7 +22,7 @@ * @section CAPI_ML_NNTRAINER_TRAIN_HEADER Required Header * \#include \n * @section CAPI_ML_NNTRAINER_TRAIN_OVERVIEW Overview - * The NNTrainer function provides interfaces to create and train Machine + * The NNTrainer API provides interfaces to create and train Machine * Learning models on the device locally. * * This function allows the following operations with NNTrainer: diff --git a/api/capi/include/nntrainer.h b/api/capi/include/nntrainer.h index 06cbe01..77f8d01 100644 --- a/api/capi/include/nntrainer.h +++ b/api/capi/include/nntrainer.h @@ -15,7 +15,7 @@ * @file nntrainer.h * @date 08 July 2020 * @brief NNTrainer C-API Header. - * This allows to construct, control and train a neural network model in + * This allows to construct, control, and train a neural network model in * Tizen devices with nntrainer. * @see https://github.com/nnstreamer/nntrainer * @author Jijoong Moon @@ -23,7 +23,6 @@ * @bug No known bugs except for NYI items * * @note This API is not stable and still experimental. - * @todo Make this API thread safe. */ #ifndef __TIZEN_MACHINELEARNING_NNTRAINER_H__ @@ -89,140 +88,151 @@ typedef enum { /** * @brief Constructs the neural network model. - * @details Use this function to create Neural Network Model. + * @details Use this function to create neural network model. * @since_tizen 6.x - * @param[out] model The NNTrainer Model handler from the given description. + * @remarks If the function succeeds, @a model must be released using + * ml_train_model_destroy(). + * @param[out] model The NNTrainer model handle from the given description. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. * @retval #ML_ERROR_CANNOT_ASSIGN_ADDRESS Cannot assign object. */ int ml_train_model_construct(ml_train_model_h *model); /** - * @brief Construct the neural network model with the given configuration file. + * @brief Constructs the neural network model with the given configuration file. * @details Use this function to create neural network model with the given * configuration file. * @since_tizen 6.x + * @remarks If the function succeeds, @a model must be released using + * ml_train_model_destroy(). * @param[in] model_conf The nntrainer model configuration file. - * @param[out] model The NNTrainer model handler from the given description. + * @param[out] model The NNTrainer model handle from the given description. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. */ int ml_train_model_construct_with_conf(const char *model_conf, ml_train_model_h *model); /** - * @brief Compile and finalize the neural network model with the given loss. + * @brief Compiles and finalizes the neural network model with the given loss. * @details Use this function to initialize neural network model. Various - * hyper parameter before compile the model can be set. Once compiled, + * hyperparameter before compile the model can be set. Once compiled, * any modification to the properties of model or layers/dataset/optimizer in * the model will be restricted. Further, addition of layers or changing the * optimizer/dataset of the model will not be permitted. * @since_tizen 6.x - * @param[in] type The NNTrainer loss type. - * @param[in] ... hyper parmeter for compiling the model - * @param[in] model The NNTrainer model handler. + * @param[in] model The NNTrainer model handle. + * @param[in] ... hyperparmeters for compiling the model * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. */ int ml_train_model_compile(ml_train_model_h model, ...); /** - * @brief Train the neural network model. + * @brief Trains the neural network model. * @details Use this function to train the compiled neural network model with * the passed training hyperparameters. This function will return once the - * training along with requested validation and testing is completed. + * training, along with requested validation and testing, is completed. * @since_tizen 6.x - * @param[in] model The NNTrainer model handler. - * @param[in] ... Hyperparmeter for train model. + * @param[in] model The NNTrainer model handle. + * @param[in] ... Hyperparmeters for train model. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. */ int ml_train_model_run(ml_train_model_h model, ...); /** * @brief Destructs the neural network model. - * @details Use this function to destroy Neural Network Model. + * @details Use this function to destroy neural network model. * @since_tizen 6.x - * @param[in] model The NNTrainer model handler from the given description. + * @param[in] model The NNTrainer model handle from the given description. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. */ int ml_train_model_destroy(ml_train_model_h model); /** - * @brief Get the summary of the neural network model. + * @brief Gets the summary of the neural network model. * @details Use this function to get the summary of the neural network model. * @since_tizen 6.x * @remarks If the function succeeds, @a summary should be released using * free(). - * @param[in] model The NNTrainer model handler to get summary. + * @param[in] model The NNTrainer model handle to get summary. * @param[in] verbosity Verbose level of the summary * @param[out] summary The summary of the current model. Avoid logic to parse * and exploit @a summary if possible. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. */ int ml_train_model_get_summary(ml_train_model_h model, ml_train_summary_type_e verbosity, char **summary); /** - * @brief Add layer in neural network model. + * @brief Adds layer in neural network model. * @details Use this function to add a layer to the model. The layer is added to * the end of the existing layers in the model. This transfers the - * ownership of the layer to the network. No need to delete the layer once it + * ownership of the layer to the network. No need to destroy the layer once it * is added to a model. * @since_tizen 6.x - * @param[in] model The NNTrainer model handler. - * @param[in] layer The NNTrainer layer handler. + * @param[in] model The NNTrainer model handle. + * @param[in] layer The NNTrainer layer handle. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. */ int ml_train_model_add_layer(ml_train_model_h model, ml_train_layer_h layer); /** - * @brief Set the optimizer for the neural network model. - * @details Use this function to set Neural Network Optimizer. Unsets the - * previous optimizer if any. This transfers the ownership of the optimizer to - * the network. No need to destroy the optimizer if it is to a model. + * @brief Sets the optimizer for the neural network model. + * @details Use this function to set neural network optimizer. This transfers + * the ownership of the optimizer to the network. No need to destroy the + * optimizer if it is to a model. * @since_tizen 6.x - * @param[in] model The NNTrainer model handler. - * @param[in] dataset The NNTrainer dataset handler. + * @remarks Unsets the previously set optimizer, if any. The previously set + * optimizer must be freed using ml_train_optimizer_destroy(). + * @param[in] model The NNTrainer model handle. + * @param[in] optimizer The NNTrainer optimizer handle. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. */ int ml_train_model_set_optimizer(ml_train_model_h model, ml_train_optimizer_h optimizer); /** - * @brief Set the dataset (data provider) for the neural network model. + * @brief Sets the dataset (data provider) for the neural network model. * @details Use this function to set dataset for running the model. The dataset - * will provide training, validation and test data for the model. Unsets the - * previous dataset if any. This transfers the ownership of the dataset to - * the network. No need to delete the dataset once it is set to a model. + * will provide training, validation and test data for the model. This transfers + * the ownership of the dataset to the network. No need to destroy the dataset + * once it is set to a model. * @since_tizen 6.x - * @param[in] model The NNTrainer model handler. - * @param[in] dataset The NNTrainer dataset handler. + * @remarks Unsets the previously set dataset, if any. The previously set + * dataset must be freed using ml_train_dataset_destroy(). + * @param[in] model The NNTrainer model handle. + * @param[in] dataset The NNTrainer dataset handle. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. */ int ml_train_model_set_dataset(ml_train_model_h model, ml_train_dataset_h dataset); /** - * @brief Create a neural network layer. - * @details Use this function to create Neural Network Layer. + * @brief Creates a neural network layer. + * @details Use this function to create neural network layer. * @since_tizen 6.x - * @param[out] layer The NNTrainer layer handler from the given description. + * @remarks If the function succeeds, @a layer must be released using + * ml_train_layer_destroy(), if not added to a model. If added to a model, @a + * layer is available until the model is released. + * @param[out] layer The NNTrainer layer handle from the given description. * @param[in] type The NNTrainer layer type * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. @@ -232,60 +242,94 @@ int ml_train_model_set_dataset(ml_train_model_h model, int ml_train_layer_create(ml_train_layer_h *layer, ml_train_layer_type_e type); /** - * @brief destroy the neural network layer. - * @details Use this function to destroy Neural Network Layer. Fails if layer is + * @brief Frees the neural network layer. + * @details Use this function to destroy neural network layer. Fails if layer is * owned by a model. * @since_tizen 6.x - * @param[in] layer The NNTrainer layer handler. + * @param[in] layer The NNTrainer layer handle. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. */ int ml_train_layer_destroy(ml_train_layer_h layer); /** - * @brief Set the neural network layer Property. - * @details Use this function to set Neural Network Layer Property. + * @brief Sets the neural network layer Property. + * @details Use this function to set neural network layer Property. * @since_tizen 6.x - * @param[in] layer The NNTrainer layer handler. + * @param[in] layer The NNTrainer layer handle. * @param[in] ... Property values with NULL for termination. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. + * + * Here is an example of the usage of this function: + * @code + * int status; + * ml_train_layer_h handle; + * + * status = ml_train_layer_create(&handle, ML_TRAIN_LAYER_TYPE_FC); + * if (status != ML_ERROR_NONE) { + * // Handle error case + * return status; + * } + * + * // Many of these hyperparmeters are optional + * status = ml_train_layer_set_property(handle, "input_shape=32:1:1:6270", + * "unit=10", "bias_init_zero=true", "activation=sigmoid", + * "weight_decay=l2_norm", "weight_ini=he_uniform", NULL); + * if (status != ML_ERROR_NONE) { + * // Handle error case + * ml_train_layer_destroy(handle); + * return status; + * } + * + * status = ml_train_layer_destroy(handle); + * if (status != ML_ERROR_NONE) { + * // Handle error case + * return status; + * } + * @endcode */ int ml_train_layer_set_property(ml_train_layer_h layer, ...); /** - * @brief Create a neural network optimizer. - * @details Use this function to create Neural Network optimizer. + * @brief Creates a neural network optimizer. + * @details Use this function to create neural network optimizer. If not set to + * a model, @a optimizer should be released using ml_train_optimizer_destroy(). + * If set to a model, @a optimizer is available until model is released. * @since_tizen 6.x - * @param[out] optimizer The NNTrainer optimizer handler. + * @remarks If the function succeeds, @a optimizer must be released using + * ml_train_optimizer_destroy(), if not set to a model. If set to a model, @a + * optimizer is available until the model is released. + * @param[out] optimizer The NNTrainer optimizer handle. * @param[in] type The NNTrainer optimizer type. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. + * @retval #ML_ERROR_CANNOT_ASSIGN_ADDRESS Cannot assign object. */ int ml_train_optimizer_create(ml_train_optimizer_h *optimizer, ml_train_optimizer_type_e type); /** - * @brief destroy the neural network optimizer. - * @details Use this function to destroy Neural Netowrk Optimizer. Fails if + * @brief Frees the neural network optimizer. + * @details Use this function to destroy neural network optimizer. Fails if * optimizer is owned by a model. * @since_tizen 6.x - * @param[in] optimizer The NNTrainer optimizer handler. + * @param[in] optimizer The NNTrainer optimizer handle. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. */ int ml_train_optimizer_destroy(ml_train_optimizer_h optimizer); /** - * @brief Set the neural network optimizer property. - * @details Use this function to set Neural Network optimizer Property. + * @brief Sets the neural network optimizer property. + * @details Use this function to set neural network optimizer property. * @since_tizen 6.x - * @param[in] optimizer The NNTrainer optimizer handler. - * @param[in] ... Property values with NULL at the end. + * @param[in] optimizer The NNTrainer optimizer handle. + * @param[in] ... Property values with NULL for termination. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. @@ -293,19 +337,26 @@ int ml_train_optimizer_destroy(ml_train_optimizer_h optimizer); int ml_train_optimizer_set_property(ml_train_optimizer_h optimizer, ...); /** - * @brief Create a dataset with generators to feed to a neural network. - * @details Use this function to create a Neural Network Dataset using + * @brief Creates a dataset with generators to feed to a neural network. + * @details Use this function to create a neural network dataset using * generators. The generators will provide data representing a single input * batch. When setting this dataset to a model, the data generated by the * generators should match the input and the label shape for the model. * @since_tizen 6.x - * @param[out] dataset The NNTrainer Dataset handler from the given description. + * @remarks If the function succeeds, @a dataset must be released using + * ml_train_dataset_destroy(), if not set to a model. If set to a model, @a + * dataset is available until the model is released. + * @param[out] dataset The NNTrainer dataset handle from the given description. + * If not set to a model, @a dataset should be released using + * ml_train_dataset_destroy(). If set to a model, @a dataset is available until + * model is released. * @param[in] train_cb The dataset generator for training. * @param[in] valid_cb The dataset generator for validating. Can be null. * @param[in] test_cb The dataset generator for testing. Can be null. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. + * @retval #ML_ERROR_CANNOT_ASSIGN_ADDRESS Cannot assign object. */ int ml_train_dataset_create_with_generator(ml_train_dataset_h *dataset, ml_train_datagen_cb train_cb, @@ -313,17 +364,21 @@ int ml_train_dataset_create_with_generator(ml_train_dataset_h *dataset, ml_train_datagen_cb test_cb); /** - * @brief Create a dataset with files to feed to a neural network. - * @details Use this function to create a Neural Network Dataset using + * @brief Creates a dataset with files to feed to a neural network. + * @details Use this function to create a neural network dataset using * files. * @since_tizen 6.x - * @param[out] dataset The NNTrainer Dataset handler from the given description. - * @param[in] train_fle The dataset file for training. + * @param[out] dataset The NNTrainer dataset handle from the given description. + * If not set to a model, @a dataset should be released using + * ml_train_dataset_destroy(). If set to a model, @a dataset is available until + * model is released. + * @param[in] train_file The dataset file for training. * @param[in] valid_file The dataset file for validating. Can be null. * @param[in] test_file The dataset file for testing. Can be null. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. + * @retval #ML_ERROR_CANNOT_ASSIGN_ADDRESS Cannot assign object. */ int ml_train_dataset_create_with_file(ml_train_dataset_h *dataset, const char *train_file, @@ -331,22 +386,22 @@ int ml_train_dataset_create_with_file(ml_train_dataset_h *dataset, const char *test_file); /** - * @brief Destroy the neural network dataset. + * @brief Frees the neural network dataset. * @details Use this function to destroy dataset. Fails if dataset is owned by a * model. * @since_tizen 6.x - * @param[in] dataset The NNTrainer dataset handler. + * @param[in] dataset The NNTrainer dataset handle. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. */ int ml_train_dataset_destroy(ml_train_dataset_h dataset); /** - * @brief Set the neural network dataset Property. - * @details Use this function to set dataset Property. + * @brief Sets the neural network dataset property. + * @details Use this function to set dataset property. * @since_tizen 6.x - * @param[in] dataset The NNTrainer dataset handler. + * @param[in] dataset The NNTrainer dataset handle. * @param[in] ... Property values with NULL for termination. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. diff --git a/api/nntrainer-api-common.h b/api/nntrainer-api-common.h index 0fa0394..9b864bb 100644 --- a/api/nntrainer-api-common.h +++ b/api/nntrainer-api-common.h @@ -8,33 +8,116 @@ * @see https://github.com/nnstreamer/nntrainer * @author Parichay Kapoor * @bug No known bugs except for NYI items + * + * @note This API is not stable and still experimental. */ -#ifndef __NNTRAINER_API_COMMON_H__ -#define __NNTRAINER_API_COMMON_H__ +#ifndef __TIZEN_MACHINELEARNING_NNTRAINER_API_COMMON_H__ +#define __TIZEN_MACHINELEARNING_NNTRAINER_API_COMMON_H__ /** * @brief Dataset generator callback function for train/valid/test data. - * @details The Containers passed will already be allocated with sufficient - * space to contain the data by the caller. This function should return a batch - * of input and label of data in the passed containers. The callback should fill - * the data row-wise in the containers obliging to the input shape set for the - * model. The order of the inputs in case of multiple input layers will be + * + * @details The user of the API must provide this callback function to supply + * data to the model and register the callback with + * ml_train_dataset_create_with_generator(). The model will call this callback + * whenever it needs more data. This function should provide a single element of + * input and label data in the passed containers. The containers passed by the + * caller will already be allocated with sufficient space to contain the data. + * This function callback should fill the data row-wise in the containers + * provided. The containers represent array of memory to hold inputs for the + * model. If the model contains two inputs, then input[0] will hold the first + * input, and input[1] will hold the second input. The same applies for labels + * as well. The number of inputs and labels, and the size of each input and + * label should match with the shape of each input and label set in the model. + * The order of the inputs/labels, in case of multiple of inputs/labels, will be * determined based on the sequence of addition of the input layers to the * model. - * @note This function can be called multiple times in parallel. - * @param[out] input Container to hold all the input data. - * @param[out] label Container to hold corresponding label data. + * @since_tizen 6.x + * @note This function can be called multiple times in parallel when total + * number of samples are set as a property for this dataset. In this case, last + * is only used for verification purposes. If total number of samples for the + * dataset is unknown, this function will be called in sequence. + * @param[out] input Container to hold all the input data. Should not be freed + * by the user. + * @param[out] label Container to hold corresponding label data. Should not be + * freed by the user. * @param[out] last Container to notify if data is finished. Set true if no more - * data to provide, else set false. + * data to provide, else set false. Should not be freed by the user. + * @param[in] user_data User application's private data. * @return @c 0 on success. Otherwise a negative error value. * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER Invalid Parameter. + * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter. + * + * A sample implementation of this function is below: + * Note : input data information available from outside this function. + * num_samples : total number of data samples in the dataset. + * count : number of samples already given. + * num_inputs : number of inputs. + * num_labels : number of labels. + * input_length[num_inputs] : length of the input. With (batch, c, h, w) as + * input shape, the length will be c * h * w. + * label_length[num_labels] : length of the label. With (batch, l) as label + * shape, then length will be l. + * @code + * // function signature : + * // int rand_dataset_generator (float **input, float **label, bool *last, + * // void *user_data). + * + * // This sample fills inputs and labels with random data. + * srand(0); + * if (count > num_samples) { + * *last = true; + * // handle preparation for start of next epoch + * return ML_ERROR_NONE; + * } + * + * // Fill input data + * for (int idx = 0; idx < num_inputs; ++ idx) { + * for (int len = 0; len < input_length[idx]; ++ len) { + * input[idx][len] = rand(); + * } + * } + * + * // Fill label data + * for (int idx = 0; idx < num_inputs; ++ idx) { + * for (int len = 0; len < label_length[idx]; ++ len) { + * label[idx][len] = rand(); + * } + * } + * + * // Update the helper variables + * count += 1; + * + * return ML_ERROR_NONE; + * @endcode + * + * + * Below is an example of the usage of this sample: + * @code + * int status; + * ml_train_dataset_h handle; + * + * status = ml_train_dataset_create_with_generator(&handle, + * rand_dataset_generator, NULL, NULL); + * if (status != ML_ERROR_NONE) { + * // handle error case. + * return status; + * } + * + * // Destroy the handle if not added to a model. + * status = ml_train_layer_destroy(handle); + * if (status != ML_ERROR_NONE) { + * // handle error case + * return status; + * } + * @endcode */ -typedef int (*ml_train_datagen_cb)(float **input, float **label, bool *last); +typedef int (*ml_train_datagen_cb)(float **input, float **label, bool *last, + void *user_data); /** - * @brief Enumeration for the neural network summary verbosity of NNTrainer + * @brief Enumeration for the neural network summary verbosity of NNTrainer. * @since_tizen 6.x */ typedef enum { @@ -45,4 +128,4 @@ typedef enum { information */ } ml_train_summary_type_e; -#endif +#endif /* __TIZEN_MACHINELEARNING_NNTRAINER_API_COMMON_H__ */ diff --git a/nntrainer/src/databuffer_func.cpp b/nntrainer/src/databuffer_func.cpp index 01aeb6c..df7cec1 100644 --- a/nntrainer/src/databuffer_func.cpp +++ b/nntrainer/src/databuffer_func.cpp @@ -204,7 +204,7 @@ void DataBufferFromCallback::updateData(BufferType type) { testReadyFlag = DATA_NOT_READY; if (buf_size - (*cur_size) > 0) { /** @todo Update to support multiple inputs later */ - status = callback(vec_arr, veclabel_arr, &endflag); + status = callback(vec_arr, veclabel_arr, &endflag, NULL); if (status == ML_ERROR_NONE && !endflag) { for (unsigned int i = 0; i < input_dim.batch(); ++i) { diff --git a/test/include/nntrainer_test_util.h b/test/include/nntrainer_test_util.h index 125cbe1..1628a91 100644 --- a/test/include/nntrainer_test_util.h +++ b/test/include/nntrainer_test_util.h @@ -370,18 +370,22 @@ void replaceString(const std::string &from, const std::string &to, * @param[out] outVec * @param[out] outLabel * @param[out] last if the data is finished + * @param[in] user_data private data for the callback * @retval status for handling error */ -int getMiniBatch_train(float **outVec, float **outLabel, bool *last); +int getMiniBatch_train(float **outVec, float **outLabel, bool *last, + void *user_data); /** * @brief get data which size is mini batch for val * @param[out] outVec * @param[out] outLabel * @param[out] last if the data is finished + * @param[in] user_data private data for the callback * @retval status for handling error */ -int getMiniBatch_val(float **outVec, float **outLabel, bool *last); +int getMiniBatch_val(float **outVec, float **outLabel, bool *last, + void *user_data); #endif /* __cplusplus */ #endif /* __NNTRAINER_TEST_UTIL_H__ */ diff --git a/test/input_gen/genInput.py b/test/input_gen/genInput.py index dab9ecb..8f39f93 100755 --- a/test/input_gen/genInput.py +++ b/test/input_gen/genInput.py @@ -167,7 +167,7 @@ def fc_tf_simplified_backward(x, kernel, label, bias, activation, opt): optimizer = tf.keras.optimizers.Adam(learning_rate = 1,beta_1=0.9, beta_2=0.999, epsilon=1.0e-7) else: raise 'unknown optimizer' - + tf_grad = tf.gradients(fc_out, all_variables) train_op = optimizer.apply_gradients(list(zip(tf_grad[1:], trainable_variables))) @@ -193,7 +193,7 @@ def fc_tf_simplified_backward(x, kernel, label, bias, activation, opt): print(tf_outs[0]) print(tf_outs[2][0]) print(tf_outs[2][1]) - print("-------------------") + print("-------------------") return tf_outs @@ -384,7 +384,7 @@ def gen_test_case_fc(input_shape, kernel_shape, base_name): bias = gen_input(base_name + "_FCKernel.in", kernel_shape[-1:], savefile=False) with open(base_name+"_FCKernel.in", 'ab') as outfile: np.array(bias, dtype=np.float32).tofile(outfile) - + golden_fc_simplified = fc_tf_simplified_backward(input_data, kernel, label, bias, activation=None, opt='adam') save(base_name + "_goldenFCGradientAdam.out", golden_fc_simplified[1][0]) save(base_name + "_goldenFCUpdatedWeightAdam.out", golden_fc_simplified[2][0]) diff --git a/test/nntrainer_test_util.cpp b/test/nntrainer_test_util.cpp index fd57a34..163ff96 100644 --- a/test/nntrainer_test_util.cpp +++ b/test/nntrainer_test_util.cpp @@ -113,9 +113,11 @@ static bool getData(std::ifstream &F, std::vector &outVec, * @param[out] outVec * @param[out] outLabel * @param[out] last if the data is finished + * @param[in] user_data private data for the callback * @retval status for handling error */ -int getMiniBatch_train(float **outVec, float **outLabel, bool *last) { +int getMiniBatch_train(float **outVec, float **outLabel, bool *last, + void *user_data) { std::vector memI; std::vector memJ; unsigned int count = 0; @@ -187,9 +189,11 @@ int getMiniBatch_train(float **outVec, float **outLabel, bool *last) { * @param[out] outVec * @param[out] outLabel * @param[out] last if the data is finished + * @param[in] user_data private data for the callback * @retval status for handling error */ -int getMiniBatch_val(float **outVec, float **outLabel, bool *last) { +int getMiniBatch_val(float **outVec, float **outLabel, bool *last, + void *user_data) { std::vector memI; std::vector memJ; -- 2.7.4