MCPcopy Create free account
hub / github.com/alibaba/MNN / main

Function main

tools/cpp/testTrain.cpp:65–235  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

63}
64
65int main(int argc, const char* argv[]) {
66 // check given & expect
67 if (argc < 3) {
68 return 0;
69 }
70 const char* jsonPath = argv[1];
71 const char* dirPath = argv[2];
72 rapidjson::Document document;
73 {
74 std::ifstream fileNames(jsonPath);
75 std::ostringstream output;
76 output << fileNames.rdbuf();
77 auto outputStr = output.str();
78 document.Parse(outputStr.c_str());
79 if (document.HasParseError()) {
80 MNN_ERROR("Invalid json\n");
81 return 0;
82 }
83 }
84 auto picObj = document.GetObject();
85 float learnRate;
86 if (document.HasMember("LearningRate")) {
87 learnRate = document["LearningRate"].GetFloat();
88 }
89 auto modelPath = std::string(dirPath) + "/" + picObj["Model"].GetString();
90 auto lossName = picObj["Loss"].GetString();
91 auto inputName = picObj["Input"].GetString();
92 auto targetName = picObj["Target"].GetString();
93 auto dataArray = picObj["Data"].GetArray();
94 auto lR = picObj["LR"].GetString();
95 auto decay = picObj["Decay"].GetFloat();
96
97 // create net
98 auto type = MNN_FORWARD_CPU;
99 MNN::BackendConfig::PrecisionMode precision = MNN::BackendConfig::Precision_Low;
100 std::shared_ptr<MNN::Interpreter> net =
101 std::shared_ptr<MNN::Interpreter>(MNN::Interpreter::createFromFile(modelPath.c_str()));
102
103 // create session
104 MNN::ScheduleConfig config;
105 config.type = type;
106 config.saveTensors.emplace_back(lossName);
107 MNN::BackendConfig backendConfig;
108 backendConfig.precision = precision;
109 config.backendConfig = &backendConfig;
110 auto session = net->createSession(config);
111 if (nullptr == net->getSessionInput(session, inputName) || nullptr == net->getSessionInput(session, targetName) || nullptr == net->getSessionInput(session, lR) || nullptr == net->getSessionOutput(session, lossName)) {
112 MNN_ERROR("Invalid model for train\n");
113 return 0;
114 }
115 static bool gDebug = false;
116 bool onlyInfer = false;
117 auto lossTensor = net->getSessionOutput(session, lossName);
118 std::vector<float> loss;
119 MNN::TensorCallBack beforeCallBack = [&](const std::vector<MNN::Tensor*>& ntensors, const std::string& opName) {
120 return true;
121 };
122 MNN::TensorCallBack callBack = [&](const std::vector<MNN::Tensor*>& ntensors, const std::string& opName) {

Callers

nothing calls this directly

Calls 15

HasMemberMethod · 0.80
getSessionInputMethod · 0.80
getSessionOutputMethod · 0.80
getDimensionTypeMethod · 0.80
copyToHostTensorMethod · 0.80
copyFromHostTensorMethod · 0.80
updateSessionToModelMethod · 0.80
getModelBufferMethod · 0.80
stringFunction · 0.50
loadFunction · 0.50
strMethod · 0.45

Tested by

no test coverage detected