From f631867b6a599d98eb7092bcd356a81b6ae78e70 Mon Sep 17 00:00:00 2001 From: Jihoon Lee Date: Wed, 11 Nov 2020 11:59:20 +0900 Subject: [PATCH] [Model] Apply appcontext Apply appcontext to NeuralNetwork. From this patch `chdir()` hack is not needed :) **Self evaluation:** 1. Build test: [X]Passed [ ]Failed [ ]Skipped 2. Run test: [X]Passed [ ]Failed [ ]Skipped Signed-off-by: Jihoon Lee --- .../TransferLearning/Draw_Classification/jni/Android.mk | 3 ++- .../TransferLearning/Draw_Classification/jni/main.cpp | 8 +++----- jni/Android.mk | 3 ++- nntrainer/models/model_loader.cpp | 13 +++++++++---- nntrainer/models/neuralnet.cpp | 4 ++++ nntrainer/models/neuralnet.h | 15 +++++++++++++-- 6 files changed, 33 insertions(+), 13 deletions(-) diff --git a/Applications/TransferLearning/Draw_Classification/jni/Android.mk b/Applications/TransferLearning/Draw_Classification/jni/Android.mk index cb961a5..939d63e 100644 --- a/Applications/TransferLearning/Draw_Classification/jni/Android.mk +++ b/Applications/TransferLearning/Draw_Classification/jni/Android.mk @@ -62,7 +62,8 @@ LOCAL_LDLIBS := -llog LOCAL_SRC_FILES := main.cpp -LOCAL_SHARED_LIBRARIES := capi-nntrainer app_utils +# @todo add api for context +LOCAL_SHARED_LIBRARIES := nntrainer capi-nntrainer app_utils LOCAL_C_INCLUDES += $(TFLITE_INCLUDES) $(NNTRAINER_INCLUDES) $(APP_UTILS_INCLUDES) diff --git a/Applications/TransferLearning/Draw_Classification/jni/main.cpp b/Applications/TransferLearning/Draw_Classification/jni/main.cpp index 9ab44e4..5931f29 100644 --- a/Applications/TransferLearning/Draw_Classification/jni/main.cpp +++ b/Applications/TransferLearning/Draw_Classification/jni/main.cpp @@ -44,6 +44,7 @@ #endif #include "bitmap_helpers.h" +#include #include /** Number of dimensions for the input data */ @@ -420,11 +421,8 @@ int main(int argc, char *argv[]) { /** location of resources ( ../../res/ ) */ std::string data_path = args[1]; - /// @todo #716 and discard this - if (chdir(data_path.c_str()) < 0) { - std::cout << "changing directory to data path failed\n"; - return 1; - } + /// @todo add capi version of this + nntrainer::AppContext::Global().setWorkingDirectory(data_path); srand(time(NULL)); diff --git a/jni/Android.mk b/jni/Android.mk index 7727540..618816c 100644 --- a/jni/Android.mk +++ b/jni/Android.mk @@ -82,7 +82,8 @@ NNTRAINER_SRCS := $(NNTRAINER_ROOT)/nntrainer/models/neuralnet.cpp \ $(NNTRAINER_ROOT)/nntrainer/optimizers/sgd.cpp \ $(NNTRAINER_ROOT)/nntrainer/optimizers/optimizer_factory.cpp \ $(NNTRAINER_ROOT)/nntrainer/utils/util_func.cpp \ - $(NNTRAINER_ROOT)/nntrainer/utils/parse_util.cpp + $(NNTRAINER_ROOT)/nntrainer/utils/parse_util.cpp \ + $(NNTRAINER_ROOT)/nntrainer/app_context.cpp # Add tflite backbone building ifeq ($(ENABLE_TFLITE_BACKBONE),1) diff --git a/nntrainer/models/model_loader.cpp b/nntrainer/models/model_loader.cpp index 611cd23..7ccb230 100644 --- a/nntrainer/models/model_loader.cpp +++ b/nntrainer/models/model_loader.cpp @@ -61,7 +61,9 @@ int ModelLoader::loadModelConfigIni(dictionary *ini, NeuralNetwork &model) { model.epochs = iniparser_getint(ini, "Model:Epochs", model.epochs); model.loss_type = (LossType)parseType( iniparser_getstring(ini, "Model:Loss", unknown), TOKEN_LOSS); - model.save_path = iniparser_getstring(ini, "Model:Save_path", "./model.bin"); + const std::string &save_path = + iniparser_getstring(ini, "Model:Save_path", "./model.bin"); + model.setSavePath(save_path); model.batch_size = iniparser_getint(ini, "Model:Batch_Size", model.batch_size); @@ -384,11 +386,14 @@ int ModelLoader::loadFromIni(std::string ini_file, NeuralNetwork &model, * @note The order of backbones in the ini file defines the order on the * backbones in the model graph */ - const char *backbone = + const char *backbone_path = iniparser_getstring(ini, (sec_name + ":Backbone").c_str(), unknown); - if (backbone == unknown) { + + const std::string &backbone = + model.app_context.getWorkingPath(backbone_path); + if (backbone_path == unknown) { status = loadLayerConfigIni(ini, layer, sec_name); - } else if (fileIni(backbone)) { + } else if (fileIni(backbone_path)) { status = loadBackboneConfigIni(ini, backbone, model, sec_name); NN_INI_RETURN_STATUS(); continue; diff --git a/nntrainer/models/neuralnet.cpp b/nntrainer/models/neuralnet.cpp index 39fde2b..c19773b 100644 --- a/nntrainer/models/neuralnet.cpp +++ b/nntrainer/models/neuralnet.cpp @@ -911,4 +911,8 @@ void NeuralNetwork::print(std::ostream &out, unsigned int flags, /// @todo Add status to check neuralnet has been run. #290 } +void NeuralNetwork::setSavePath(const std::string &path) { + save_path = app_context.getWorkingPath(path); +} + } /* namespace nntrainer */ diff --git a/nntrainer/models/neuralnet.h b/nntrainer/models/neuralnet.h index cb16e46..a0392a1 100644 --- a/nntrainer/models/neuralnet.h +++ b/nntrainer/models/neuralnet.h @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -77,7 +78,7 @@ public: /** * @brief Constructor of NeuralNetwork Class */ - NeuralNetwork() : + NeuralNetwork(AppContext app_context_ = AppContext(AppContext::Global())) : batch_size(1), epochs(1), epoch_idx(0), @@ -90,7 +91,8 @@ public: continue_train(false), initialized(false), def_name_count(0), - loadedFromConfig(false) {} + loadedFromConfig(false), + app_context(app_context_) {} /** * @brief Destructor of NeuralNetwork Class @@ -382,6 +384,8 @@ private: RunStats training; /** training statistics of the model */ RunStats testing; /** testing statistics of the model */ + AppContext app_context; /** Configurations bound to current app */ + /** * @brief print function for neuralnet * @param[in] out outstream @@ -498,6 +502,13 @@ private: * @param[in] flags verbosity from ml_train_summary_type_e */ void printMetrics(std::ostream &out, unsigned int flags = 0); + + /** + * @brief Set the Save Path + * + * @param path path to set as a save path + */ + void setSavePath(const std::string &path); }; } /* namespace nntrainer */ -- 2.7.4