Hi, I would like to know how to load a file included in traced .pt model that was added as an extra file in C++. In this case I save the model with PyTorch and try to load it with C++.
This could be an example (from documentation):
class MyModule(torch.nn.Module):
def forward(self, x):
return x + 10
m = torch.jit.script(MyModule())
# Save with extra files
extra_files = torch._C.ExtraFilesMap()
extra_files['module.meta'] = 'metadata'
torch.jit.save(m, 'scriptmodule.pt', _extra_files=extra_files)
module.meta could be a text file and I would like to read its content when loading model in C++ like this:
#include <torch/torch.h>
#include <torch/script.h>
int main()
{
torch::jit::script::Module module;
try
{
std::string modulePath = "scriptmodule.pt";
torch::jit::ExtraFilesMap extraFilesMap_;
std::pair<std::string, std::string> moduleMeta("module.meta", "");
extraFilesMap_.insert(moduleMeta);
module = torch::jit::load(modelPath, torch::kCPU, extraFilesMap_);
module.eval();
for (auto const& fileMap : extraFilesMap_)
{
std::cout << fileMap.first << std::endl;
std::cout << fileMap.second << std::endl;
}
//How could open and read the content of the file "module.meta"
}
catch (const std::exception& e)
{
const std::string errorMsg = e.what();
std::cerr << " Error: " << errorMsg << std::endl;
}
return 0;
}
From here, extraFilesMap_ when doing iteration will print fileMap.first = module.meta and fileMap.second = metadata
How could I read that extra file content?
Thanks in advance