Question
How do I convert a std::vector<double>
into a std::vector <torch::jit::IValue>
which can be input to a TorchScript model?
Background
I trained a model in Python and followed this guide to use the model in C++: https://pytorch.org/tutorials/advanced/cpp_export.html#step-4-executing-the-script-module-in-c
Please note that I have almost no experience with C++
Working code with dummy Tensor
Everything works fine when I make a dummy Tensor directly in C++:
#include <torch/script.h> // One-stop header.
#include <iostream>
#include <memory>
using namespace std::chrono;
int main(int argc, const char* argv[]) {
std::cout << "ok\n";
if (argc != 2) {
std::cerr << "usage: example-app <path-to-exported-script-module>\n";
return -1;
}
torch::jit::script::Module module;
try {
// Deserialize the ScriptModule from a file using torch::jit::load().
module = torch::jit::load(argv[1]);
}
catch (const c10::Error& e) {
std::cerr << "error loading the model\n";
return -1;
}
std::cout << "ok\n";
// Create a vector of inputs.
int sample_rate = 16000;
int input = sample_rate * 100;
std::vector<torch::jit::IValue> inputs;
inputs.push_back(torch::ones({1, input}));
// Execute the model and turn its output into a tensor.
at::Tensor output = module.forward(inputs).toTensor().sigmoid();
std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n';
Attempting the same with vector double
In our real application the audio data is of the format std::vector<double>
, however. This cannot be changed.
From my Googling, what I would likely need is the function torch::from_blob()
, so I tried to replace:
// Create a vector of inputs.
int sample_rate = 16000;
int input = sample_rate * 100;
std::vector<torch::jit::IValue> inputs;
inputs.push_back(torch::ones({1, input}));
with:
std::vector <torch::jit::IValue> inputs;
// dummy double
std::vector<double> wavData = std::vector<double>(5);
auto options = torch::TensorOptions().dtype(torch::kFloat32);
inputs.push_back(torch::from_blob(wavData, {1, input}, options));
Problem
The code I found only used these 3 arguments. But this throws the error:
path/example-app.cpp: In function ‘int main(int, const char**)’:
path/example-app.cpp:47:71: error: no matching function for call to ‘from_blob(std::vector<double, std::allocator<double> >&, <brace-enclosed initializer list>, c10::TensorOptions&)’ inputs.push_back(torch::from_blob(wavData, {1, input}, options));
It seems I also need to supply a const Deleter& deleter
, but I have no clue what how I’m supposed to create that.
Unfortunately the docs page of from_blob() is broken and therefore also not of much help.
Any insights are appreciated!