Tutorials for PyTorch C++
alantess / learntorch Goto Github PK
View Code? Open in Web Editor NEWTutorials for PyTorch C++
License: MIT License
Tutorials for PyTorch C++
License: MIT License
Hi, Thanks for the amazing tutorials.
I am solving 1D pde and tried to call that model in C++. I have done this successfully but the values i am predicting in Pytorch and in C++ are different.
#include <torch/script.h>
#include
#include
int main(int argc, const char* argv[])
{
if (argc != 2) {
std::cerr << "usage: main \n";
return -1;
}
try {
// Deserialize the ScriptModule from a file using torch::jit::load().
torch::jit::script::Module module;
module = torch::jit::load("/home/sunny/neural/diffusion.pth");
// Create a vector of inputs.
double x;
std::cout<<"Please enter the value of x ";
std::cin>>x;
double t;
std::cout<<"Please enter the value of t ";
std::cin>>t;
std::vector<torch::jit::IValue> inputs;
inputs.push_back(torch::tensor({{x,t}}));
// Execute the model and turn its output into a tensor.
torch::Tensor output = module.forward((inputs)).toTensor();
std::cout << output << std::endl;
}
catch (const c10::Error& e) {
std::cerr << "error loading the model\n";
return -1;
}
}
I used the same method you mentioned to save the model. Can you tell me what i am doing wrong?
Hello,
did you try as well to load a FasterRCNN model in C++ ? a simple code for models.py
would be:
import torch
import torchvision
from torchvision import models
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
# An instance of your model.
#model = torchvision.models.resnet18()
model = models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
script_model = torch.jit.script(model)
script_model.save("model.pt")
This does save the model.pt
. But upon loading the model in C++,
#include <torch/script.h> // One-stop header.
#include <iostream>
#include <memory>
int main(int argc, const char* argv[]) {
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";
}
There is an error which says:
./example-app ../model.pt
terminate called after throwing an instance of 'torch::jit::ErrorReport'
what():
Unknown builtin op: torchvision::nms.
Could not find any similar ops to torchvision::nms. This op may not exist or may not be currently supported in TorchScript.
:
File "/home/ubuntu/Documents/python-virtual-environments/env/lib/python3.8/site-packages/torchvision/ops/boxes.py", line 35
"""
_assert_has_ops()
return torch.ops.torchvision.nms(boxes, scores, iou_threshold)
~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
Serialized File "code/__torch__/torchvision/ops/boxes.py", line 148
_61 = __torch__.torchvision.extension._assert_has_ops
_62 = _61()
_63 = ops.torchvision.nms(boxes, scores, iou_threshold)
~~~~~~~~~~~~~~~~~~~ <--- HERE
return _63
'nms' is being compiled since it was called from '_batched_nms_vanilla'
File "/home/ubuntu/Documents/python-virtual-environments/env/lib/python3.8/site-packages/torchvision/ops/boxes.py", line 102
for class_id in torch.unique(idxs):
curr_indices = torch.where(idxs == class_id)[0]
curr_keep_indices = nms(boxes[curr_indices], scores[curr_indices], iou_threshold)
~~~ <--- HERE
keep_mask[curr_indices[curr_keep_indices]] = True
keep_indices = torch.where(keep_mask)[0]
Serialized File "code/__torch__/torchvision/ops/boxes.py", line 77
_28 = torch.index(boxes, _27)
_29 = annotate(List[Optional[Tensor]], [curr_indices])
curr_keep_indices = __torch__.torchvision.ops.boxes.nms(_28, torch.index(scores, _29), iou_threshold, )
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
_30 = annotate(List[Optional[Tensor]], [curr_keep_indices])
_31 = torch.index(curr_indices, _30)
'_batched_nms_vanilla' is being compiled since it was called from 'batched_nms'
Serialized File "code/__torch__/torchvision/ops/boxes.py", line 35
idxs: Tensor,
iou_threshold: float) -> Tensor:
_9 = __torch__.torchvision.ops.boxes._batched_nms_vanilla
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
_10 = __torch__.torchvision.ops.boxes._batched_nms_coordinate_trick
if torch.gt(torch.numel(boxes), 4000):
'batched_nms' is being compiled since it was called from 'RegionProposalNetwork.filter_proposals'
Serialized File "code/__torch__/torchvision/models/detection/rpn.py", line 72
_11 = __torch__.torchvision.ops.boxes.clip_boxes_to_image
_12 = __torch__.torchvision.ops.boxes.remove_small_boxes
_13 = __torch__.torchvision.ops.boxes.batched_nms
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
num_images = (torch.size(proposals))[0]
device = ops.prim.device(proposals)
'RegionProposalNetwork.filter_proposals' is being compiled since it was called from 'RegionProposalNetwork.forward'
File "/home/ubuntu/Documents/python-virtual-environments/env/lib/python3.8/site-packages/torchvision/models/detection/rpn.py", line 356
proposals = self.box_coder.decode(pred_bbox_deltas.detach(), anchors)
proposals = proposals.view(num_images, -1, 4)
boxes, scores = self.filter_proposals(proposals, objectness, images.image_sizes, num_anchors_per_level)
~~~~~~~~~~~~~~~~~~~~~ <--- HERE
losses = {}
Serialized File "code/__torch__/torchvision/models/detection/rpn.py", line 43
proposals0 = torch.view(proposals, [num_images, -1, 4])
image_sizes = images.image_sizes
_8 = (self).filter_proposals(proposals0, objectness0, image_sizes, num_anchors_per_level, )
~~~~~~~~~~~~~~~~~~~~~ <--- HERE
boxes, scores, = _8
losses = annotate(Dict[str, Tensor], {})
Aborted (core dumped)
There was a similar error reported here:
pytorch/pytorch#17054
One of the solution was to use the Nightly build. I tried that as well but no difference.
Could you please help me out on this ?
I'm making a vector with full of my tensor images and pass to my model but it doesn't work. I did use your codes. Where is the problem?
Code: https://stackoverflow.com/questions/70947854/can't-feed-my-model-with-my-image-data-tensor please
@alantess
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.