有没有办法使用c++中的onnxruntime将神经网络的权重写入文件?

问题描述 投票:0回答:0

我正在编写一个程序来使用 onxxruntime 执行推理。我在下面添加了我的工作示例。我看到推理后的输出与我的预期输出不符,所以我想验证加载的权重。有没有办法使用 C++ 中的 onnxruntime 将神经网络模型的权重写入文件?

#include<iostream>
#include<fstream>
#include </usr/local/include/onnxruntime/onnxruntime/core/session/onnxruntime_cxx_api.h>

std::vector<float> readBinaryFile(const std::string& filename) {

    std::ifstream file(filename, std::ios::binary | std::ios::ate);

    if (!file.is_open()) {
        std::cerr << "Error opening file: " << filename << std::endl;
        return std::vector<float>();
    }

    std::streamsize size = file.tellg();
    file.seekg(0, std::ios::beg);

    // Read the binary data into a vector of bytes
    std::vector<char> buffer(size);
    if (!file.read(buffer.data(), size)) {
        std::cerr << "Error reading file: " << filename << std::endl;
        return std::vector<float>();
    }

    file.close();

    const float* dataArray = reinterpret_cast<const float*>(buffer.data());

    // Calculate the number of elements in the array
    size_t numElements = size / sizeof(float);
    std::cout<<"Number of elements  "<< numElements<<std::endl;
    // Copy the data to a vector of floats
    std::vector<float> result(dataArray, dataArray + numElements);

    return result;
}

int main() {




    std::vector<float> input_data; 
    std::string fileName = "data.bin";
    input_data = readBinaryFile(fileName);

    // Load the ONNX model
    Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "ONNX_Runtime_Logs");
    Ort::SessionOptions session_options;
    Ort::Session session(env, "model.onnx", session_options);

    // model takes 1 input and gives 1 output
    // Get model input and output names
    Ort::AllocatorWithDefaultOptions allocator;
    std::vector<char*> input_name;
    std::vector< char*> output_names;
    input_name.push_back( session.GetInputName(0, allocator));
    output_names.push_back( session.GetOutputName(0,allocator));

    
    //How to print the weights in the graph here?


    // Prepare input data (replace this with your actual input data)
  // Replace with appropriate data and size
    std::vector<int64_t> input_shape; // Replace with appropriate shape

    Ort::MemoryInfo      mem_info = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator,
                                                          OrtMemType::OrtMemTypeDefault);
    std::vector<int64_t>  inputDims = {1,1,256,256,2};

    std::vector<Ort::Value> input;
     input.push_back(Ort::Value::CreateTensor<float>(
                mem_info,
                input_data.data(),
                256*256*2*1*1, inputDims.data(), inputDims.size()));

    // Create a vector to hold output tensor
    std::vector<Ort::Value> output_tensors;
    std::vector<Ort::Value> output;
    // Perform inference
     output_tensors = session.Run(Ort::RunOptions{nullptr}, input_name.data(), input.data(),
                              input_name.size(), output_names.data(),output_names.size()); 

    // Retrieve output data (assuming the output is a single tensor)
    if (!output_tensors.empty()) {
        const float* output_data = output_tensors[0].GetTensorMutableData<float>();

    } else {
        std::cout<<"Exited"<<std::endl;
        return 1;
    }

    return 0;
}

c++ inference onnxruntime
© www.soinside.com 2019 - 2024. All rights reserved.