2021-12-07 10:57:55 +08:00
|
|
|
// Copyright (c) OpenMMLab. All rights reserved.
|
|
|
|
|
2022-06-01 14:10:43 +08:00
|
|
|
#include "inference.h"
|
2021-12-07 10:57:55 +08:00
|
|
|
|
|
|
|
#include "archive/json_archive.h"
|
2022-06-01 14:10:43 +08:00
|
|
|
#include "core/model.h"
|
2021-12-07 10:57:55 +08:00
|
|
|
|
|
|
|
namespace mmdeploy::graph {
|
|
|
|
|
2022-06-01 14:10:43 +08:00
|
|
|
Result<unique_ptr<Inference>> InferenceParser::Parse(const Value& config) {
|
|
|
|
try {
|
|
|
|
auto& model_config = config["params"]["model"];
|
|
|
|
Model model;
|
|
|
|
if (model_config.is_any<Model>()) {
|
|
|
|
model = model_config.get<Model>();
|
|
|
|
} else {
|
|
|
|
model = Model(model_config.get<string>());
|
|
|
|
}
|
|
|
|
OUTCOME_TRY(auto pipeline_json, model.ReadFile("pipeline.json"));
|
|
|
|
auto json = nlohmann::json::parse(pipeline_json);
|
|
|
|
|
|
|
|
auto context = config.value("context", Value(ValueType::kObject));
|
|
|
|
context["model"] = std::move(model);
|
|
|
|
|
|
|
|
auto pipeline_config = from_json<Value>(json);
|
|
|
|
pipeline_config["context"] = context;
|
|
|
|
|
|
|
|
auto inference = std::make_unique<Inference>();
|
|
|
|
OUTCOME_TRY(NodeParser::Parse(config, *inference));
|
|
|
|
OUTCOME_TRY(inference->pipeline_, PipelineParser{}.Parse(pipeline_config));
|
|
|
|
|
|
|
|
return std::move(inference);
|
|
|
|
} catch (const Exception& e) {
|
|
|
|
MMDEPLOY_ERROR("exception: {}", e.what());
|
|
|
|
return failure(e.code());
|
2021-12-07 10:57:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 14:10:43 +08:00
|
|
|
class InferenceCreator : public Creator<Node> {
|
2021-12-07 10:57:55 +08:00
|
|
|
public:
|
|
|
|
const char* GetName() const override { return "Inference"; }
|
|
|
|
int GetVersion() const override { return 0; }
|
2021-12-16 13:51:22 +08:00
|
|
|
std::unique_ptr<Node> Create(const Value& value) override {
|
2022-06-01 14:10:43 +08:00
|
|
|
return InferenceParser::Parse(value).value();
|
2021-12-16 13:51:22 +08:00
|
|
|
}
|
2021-12-07 10:57:55 +08:00
|
|
|
};
|
|
|
|
|
2022-06-01 14:10:43 +08:00
|
|
|
REGISTER_MODULE(Node, InferenceCreator);
|
2021-12-07 10:57:55 +08:00
|
|
|
|
|
|
|
} // namespace mmdeploy::graph
|