diff --git a/blockchain_integration/pi_network/dapi/ai/ml/tensorflow/model.cpp b/blockchain_integration/pi_network/dapi/ai/ml/tensorflow/model.cpp new file mode 100644 index 000000000..63cab5f33 --- /dev/null +++ b/blockchain_integration/pi_network/dapi/ai/ml/tensorflow/model.cpp @@ -0,0 +1,52 @@ +#include "model.h" +#include +#include +#include + +Model::Model(const std::string& model_path) : model_path_(model_path) {} + +Model::~Model() { + if (session_) { + delete session_; + } +} + +void Model::load() { + // Load the model from a file + tensorflow::NewSession(tensorflow::SessionOptions(), &session_); + tensorflow::LoadSavedModel(session_, {tensorflow::kSavedModelTagServe}, model_path_, &graph_def_); + tensorflow::GraphDef graph_def; + TF_CHECK_OK(tensorflow::NewSession(tensorflow::SessionOptions(), &session_)); + TF_CHECK_OK(session_->Create(graph_def)); + TF_CHECK_OK(session_->Run({}, {}, {"input"}, &inputs_, &outputs_)); +} + +std::vector Model::run(const std::vector& input) { + // Create a tensor for the input data + tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, input.size()})); + auto input_tensor_mapped = input_tensor.tensor(); + for (int i = 0; i < input.size(); i++) { + input_tensor_mapped(0, i) = input[i]; + } + + // Run the model on the input data + std::vector outputs; + TF_CHECK_OK(session_->Run({{inputs_[0], input_tensor}}, {"output"}, {}, &outputs)); + + // Extract the output data + std::vector output; + auto output_tensor_mapped = outputs[0].tensor(); + for (int i = 0; i < output_tensor_mapped.dimension(1); i++) { + output.push_back(output_tensor_mapped(0, i)); + } + + return output; +} + +std::vector Model::get_input_shape() const { + return {1, inputs_[0].shape().dim_size(1)}; +} + +std::vector Model::get_output_shape() const { + return {1, outputs_[0].shape().dim_size(1)}; +}