jloverich
(jloverich)
September 17, 2021, 4:23pm
#1
I am able to write a tensor to a file in c++
somefunc(at::Tensor& image) {
torch::save(image, "original_image.pt");
and then when I load into python and look at it’s contents it see
cpp_tensor=torch.load("original_image.pt")
cpp_tensor
I get
RecursiveScriptModule(original_name=Module)
Am I able to access the tensor from the “RecursiveScriptModule”? When I do the same thing with pytorch it just returns the original tensor. How do access the original tensor?
ptrblck
September 19, 2021, 3:30am
#2
This should work to save a tensor in libtorch
and load it in Python:
#include <torch/torch.h>
#include <iostream>
int main() {
auto tensor = torch::ones({3, 3});
auto bytes = torch::pickle_save(tensor);
std::ofstream fout("lala.pt", std::ios::out | std::ios::binary);
fout.write(bytes.data(), bytes.size());
fout.close();
}
>>> import torch
>>> x = torch.load('lala.pt')
>>> x
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])