Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
1 vote
0 answers
332 views

How I can load model and inference using Libtorch (C++)?

I trained simple model for MNIST using it https://github.com/pytorch/examples/blob/main/cpp/mnist/mnist.cpp I added code for saving model like below string model_path = "model.pt"; torch::...
John's user avatar
  • 25
0 votes
0 answers
347 views

Getting started with Libtorch in C++

having some trouble getting a basic nn Module working with libtorch. I’m trying to copy the example found here Using the PyTorch C++ Frontend — PyTorch Tutorials 2.3.0+cu121 documentation #include <...
brando f's user avatar
  • 421
1 vote
1 answer
381 views

How to see details behind CPU-only Libtorch Matrix-Matrix multiplication routines?

I have downloaded the libtorch CPU-only version from the website and unzipped it. Inside my .cpp application which uses libtorch, I write (I am using intel-mkl for other parts of the application, and ...
velenos14's user avatar
  • 566
0 votes
1 answer
322 views

The shape of the Tensor output from the "forward_t" method in "tch-rs" does not match the size of the input tensor in "cross_entropy_for_logits"

let loss = net.forward_t(&batch_images, true).cross_entropy_for_logits(&batch_lbls); The batch_images here refers to 128 grayscale images of size 512x512, which are converted into tensors and ...
A lie Z's user avatar
0 votes
0 answers
62 views

How to install torchtext in cmake project on windows 10?

How to install torchtext in cmake project on windows 10? Error: CMake Error at X:\Programming\C++\Tests\TestPyTorch1\CMakeLists.txt:11 (find_package): By not providing "FindTorchText.cmake"...
Romalik Normalik's user avatar
2 votes
0 answers
55 views

libtorch Conv1D doesn't operate over signal length dimension

I have a torch model, that simply contains a conv1d (intended to be used to implement a STFT fourier transform). The model works fine in torch, and traced using torch.jit.load in Python. When I ...
bjp's user avatar
  • 21
0 votes
1 answer
636 views

I can't convert the pytorch maskrcnn_resnet50_fpn model to the scripted module

I just want to convert standatd torchvision model to libtorch format loaded_model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=False) # loaded_model.cpu() loaded_model.eval() ...
Andrey Taranov's user avatar
0 votes
1 answer
3k views

LibTorch: There's no way to put a model on the CUDA device that will process a tensor already on the CUDA device

I cannot put, and keep the model on the CUDA device. I cannot send a tensor that is already on CUDA through a model without getting the "found at least two devices, cpu and cuda" error. Did ...
Ant's user avatar
  • 1,143
0 votes
1 answer
666 views

How can I expand a tensor in Libtorch? (The C++ version of PyTorch)

How can I use LibTorch to expand a tensor of the shape 42, 358 into a shape of 10, 42, 358? I know how to do this in PyTorch, (AKA Torch). torch.ones(42, 358).expand(10, -1, -1).shape returns torch....
Ant's user avatar
  • 1,143
1 vote
1 answer
832 views

How to set the input to a LSTM network in C++

I'm new to libtorch and I need to load a LSTM network in C++. Before that, I have already tested with the following Python script and it is working well: actuator_net_file = "resources/...
Song's user avatar
  • 21
1 vote
0 answers
625 views

How to load the MNIST dataset from the datasets in torch C++

I'm new to torch and c++ i want to create a basic simple network that does hand digit recognition this is my Net struct. struct Net:torch::nn::Module { Net() { fc1 = register_module("...
crispengari's user avatar
  • 9,281
1 vote
0 answers
111 views

Convert a tensor with type caffe2::TypeMeta to std::vector

I have an tensor with type caffe2::TypeMeta, and I need to get its value as a regular std::vector object. I know how to convert a Torch::Tensor object with a regular type to a std::vector object, but ...
Afshin Oroojlooy's user avatar
1 vote
1 answer
6k views

I'm having issues uninstalling pytorch

So a while back I installed pytorch using pip and as a compute platform "CPU Only". Now I want to use torch on GPU but no matter how I try to install CUDA as a compute platform, it still ...
azhariamir01's user avatar
3 votes
1 answer
601 views

What is the most efficient way of converting a std::vector<std::tuple<>> to a torch::Tensor?

I have a vector of tuples that I need to convert into a torch::Tensor. What I have come up with so far, is the vanilla approach which is as follows : std::vector<torch::Tensor> anchors; std::...
Hossein's user avatar
  • 25.9k
1 vote
2 answers
691 views

Getting C1001 Internal compiler error when building pytorch on windows

I'm trying to build Pytorch on windows using visual studio, but it seems it faces some internal compiler error which I have not been able to figure out its cause. out of 46 targets, 35 gets built ...
Hossein's user avatar
  • 25.9k
0 votes
1 answer
2k views

Undefined reference error despite of having the library specified in CMake (issues linking with libtorch (C++11 ABI?)

I'm trying to create a test executable out of a library I made. Let's name them lib1 and lib2. lib1 gets built along with its test just fine. the lib2 is also built without any issues. However, ...
Hossein's user avatar
  • 25.9k
6 votes
1 answer
3k views

Get value from c10::Dict<c10::IValue, c10::IValue> in Pytorch C++

I'm using a TorchScript Model on Pytorch C++ Frontend. The model in Python returns an output dict as Dict[str, List[torch.Tensor]]. When I use it in C++, it returns a c10::Dict<c10::IValue, c10::...
lamhoangtung's user avatar
2 votes
1 answer
413 views

Trying to build a torchscript extension results in INVALID TYPE: Only int64_t and bool are supported as an integral argument type error

I'm following the torch_script_custom_classes in order to expose my C++ dll in Python. The bulk of the code that I have at the moment is as follows: #include <torch/script.h> #include <string&...
Hossein's user avatar
  • 25.9k
1 vote
1 answer
277 views

How to remove the multiplier from the libtorch output and display the final result?

When I try to display/print some tensors to the screen, I face something like the following where instead of getting the final result, it seems libtorch displays the tensor with a multiplier (i.e. 0....
Hossein's user avatar
  • 25.9k
6 votes
1 answer
9k views

How to properly convert a cv::Mat into a torch::Tensor with perfect match of values?

I am trying to run inference on a jit traced model in C++ and currently the output I get in Python is different than the output I get in C++. Initially I thought this be caused by the jit model itself,...
Hossein's user avatar
  • 25.9k
5 votes
1 answer
2k views

How to convert a list of tensors into a torch::Tensor?

I'm trying to convert the following Python code into its equivalent libtorch: tfm = np.float32([[A[0, 0], A[1, 0], A[2, 0]], [A[0, 1], A[1, 1], A[2, 1]] ]) In ...
Hossein's user avatar
  • 25.9k
0 votes
1 answer
2k views

How to compare a torch::tensor shape against some other shapes?

I'm trying to compare a torch::Tensor's sizes agains something else, but it seems I'm doing it wrong. I tried: auto t = torch::ones({ 3,3 }).sizes(); std::cout << c10::IntArrayRef{ 3,3 } ...
Hossein's user avatar
  • 25.9k
1 vote
2 answers
567 views

What's the equivalent of numpy.spacing(1) in libtorch?

It seems pytorch does have/expose the finfo link, but I can't find it in libtorch. Is it even made available in libtorch yet or not? Using the torch.finfo I could easily do: esp = torch.finfo(torch....
Hossein's user avatar
  • 25.9k
1 vote
1 answer
913 views

How to Convert a torch::Tensor into a cv::InputArray?

I'm trying to use the cv::getAffineTransform(), and I'm stuck on how to convert the tensors into the corrosponding cv::InputArray. I have tried these to face access violation : torch::Tensor src = ...
Hossein's user avatar
  • 25.9k
2 votes
1 answer
781 views

What's the equivalent of np.delete in libtorch?

It seems we don't have an np.delete equivalent in libtorch yet, so how can we emulate its behavior? For example I'm trying to rewrite the following bit of code in libtorch: ids = np.delete( ids, np....
Hossein's user avatar
  • 25.9k
15 votes
2 answers
23k views

What's the best way of checking whether a torch::Tensor is empty?

I'm currently using the has_storage() method to check whether a tensor is empty or not, but I wonder if there is anything better other than this! and whether there are any implications involved in ...
Hossein's user avatar
  • 25.9k
0 votes
1 answer
1k views

How to stack a tensor of shape (n, k) with tensors of shape (k) in libtorch?

torch::stack accepts a c10::TensorList and works perfectly fine when tensors of the same shape is given. However, when you try to send the output of a previously torch::stacked Tensor, it fails and ...
Hossein's user avatar
  • 25.9k
7 votes
2 answers
6k views

How is it possible to convert a std::vector<std::vector<double>> to a torch::Tensor?

I have a std::vector<std::vector<double>> where I want to conver it into a torch::Tensor in libtorch. However it seems, the torch::tensor(), or torch::from_blob(), can't be used for this ...
Hossein's user avatar
  • 25.9k
2 votes
1 answer
2k views

Libtorch on Raspberry can't load pt file but working on ubuntu

I'm trying to build a C++ program with libtorch on a Raspberry PI. The program is working on Ubuntu, but I've got the following error at build on Raspberry : error: use of deleted function ‘void ...
totok's user avatar
  • 1,490
1 vote
1 answer
1k views

How to solve GTest and LibTorch linkage conflict

This question follows my precedent one. I'm writing a C++ program with OpenCV, Torch and NumCpp. The program compiles and works fine for now, but I need to write unit tests. I've followed google's ...
totok's user avatar
  • 1,490
0 votes
2 answers
3k views

Linkage problem when building program with libtorch on Raspberry PI

I'm writing a C++ program using libtorch and OpenCV. Here is the output of my CMakeLists.txt, with the libraries versions : -- Pytorch status : -- libraries: torch;torch_library;/usr/lib/libc10.so ...
totok's user avatar
  • 1,490
1 vote
0 answers
150 views

LuaJIT cannot load libtorch.dll

Following the guide here as best I could (C drive not X, 64 bit), I have reached the stage of testing Torch with require('torch') torch.test() Upon trying to require the torch module I am given this ...
TSSans's user avatar
  • 23
0 votes
1 answer
1k views

why is torch::nonzero() very slow?

I executed the code below.But nonzero() function is very slow (take 0.1 sec). what is wrong my code? torch::Tensor a = torch::randn({30000, 80}); torch::Tensor b = a.select(1, 4) > 0.2; const ...
H.Sakamoto's user avatar
1 vote
2 answers
2k views

Set neural network initial weight values in C++ torch

I was looking for an API to set initial weight values in libtorch. In the python version, (i.e. pytorch) one can easily use torch.nn.functional.weight.data.fill_(xx) and torch.nn.functional.bias.data....
Afshin Oroojlooy's user avatar
3 votes
2 answers
1k views

Torch C++: API to check NAN

I am using libtorch C++. In python version we can easily check the value of a tensor by calling its numpy value, and in numpy we have np.isnan(). I was wondering if there is a built in function in ...
Afshin Oroojlooy's user avatar
11 votes
1 answer
17k views

Torch C++: Getting the value of a int tensor by using *.data<int>()

In the C++ version of Libtorch, I found that I can get the value of a float tensor by *tensor_name[0].data<float>(), in which instead of 0 I can use any other valid index. But, when I have ...
Afshin Oroojlooy's user avatar
0 votes
1 answer
891 views

LibTorch with CMake via Eclipse in Windows:Terminated exit value 390

I used cmake4eclipse to build torch C++ version 1.0 stable in Windows 10. Basically, I have the following CMakeLists.txt to build the mnist example: cmake_minimum_required(VERSION 3.1 FATAL_ERROR) ...
Afshin Oroojlooy's user avatar