Using rstan in posit.cloud AKA RStudio Cloud - c++

I am trying to use rstan on an RStudio Cloud session.
the install.packages("rstan") worked without error, reading the library also.
I'm following a toy example
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
x=rnorm(1000,5,1)
my_data=list(N=1000,x~x)
stancode = 'data{ int N; real x[N];} parameters{real mu;real sigma;} model{ x ~ normal(mu,sigma);}'
fit = stan(model_code = stancode,data=my_data)
it returns this error
Error in compileCode(f, code, language = language, verbose = verbose) :
/cloud/lib/x86_64-pc-linux-gnu-library/4.2/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:55:30: warning: ignoring attributes on template argument ‘Eigen::internal::packet_traits<double>::type’ {aka ‘__vector(2) double’} [-Wignored-attributes] 55 | >::type PacketReturnType; | ^~~~~~~~~~~~~~~~g++: fatal error: Killed signal terminated program cc1pluscompilation terminated.make: *** [/opt/R/4.2.2/lib/R/etc/Makeconf:178: file189644bbcfc.o] Error 1
Error in sink(type = "output") : invalid connection``
My session info :
R version 4.2.2 (2022-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.5 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3
LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3
locale:
[1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8 LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rstan_2.21.8 ggplot2_3.4.1 StanHeaders_2.21.0-7
loaded via a namespace (and not attached):
[1] Rcpp_1.0.10 pillar_1.8.1 compiler_4.2.2 prettyunits_1.1.1 tools_4.2.2 digest_0.6.31 pkgbuild_1.4.0
[8] evaluate_0.20 lifecycle_1.0.3 tibble_3.1.8 gtable_0.3.1 pkgconfig_2.0.3 rlang_1.0.6 cli_3.6.0
[15] rstudioapi_0.14 yaml_2.3.7 parallel_4.2.2 xfun_0.37 loo_2.5.1 fastmap_1.1.0 gridExtra_2.3
[22] withr_2.5.0 dplyr_1.1.0 knitr_1.42 generics_0.1.3 vctrs_0.5.2 stats4_4.2.2 grid_4.2.2
[29] tidyselect_1.2.0 glue_1.6.2 inline_0.3.19 R6_2.5.1 processx_3.8.0 fansi_1.0.4 rmarkdown_2.20
[36] callr_3.7.3 magrittr_2.0.3 htmltools_0.5.4 codetools_0.2-18 matrixStats_0.63.0 scales_1.2.1 ps_1.7.2
[43] colorspace_2.1-0 utf8_1.2.3 RcppParallel_5.1.6 munsell_0.5.0 crayon_1.5.2

Related

How to export PyTorch model to file (Python) and load it (libtorch C++) using TorchScript?

I am struggling with the (de)serialization of PyTorch data. I would like to save my model to a PT(H) file after training it with PyTorch (using GPU). Next I would like to load that serialized model in C++ context (using libtorch). Currently I am just experimenting with basic export/import functionality to get the hang of it.
The code is provided below. I am getting the following error:
Error loading model
Unrecognized data format
Exception raised from load at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\torch\csrc\jit\serialization\import.cpp:449 (most recent call first):
00007FFBB1FFDA2200007FFBB1FFD9C0 c10.dll!c10::Error::Error [<unknown file> # <unknown line number>]
00007FFBB1FFD43E00007FFBB1FFD3F0 c10.dll!c10::detail::torchCheckFail [<unknown file> # <unknown line number>]
00007FFB4B87B54700007FFB4B87B4E0 torch_cpu.dll!torch::jit::load [<unknown file> # <unknown line number>]
00007FFB4B87B42A00007FFB4B87B380 torch_cpu.dll!torch::jit::load [<unknown file> # <unknown line number>]
00007FF6089A737A00007FF6089A7210 pytroch_load_model.exe!main [c:\users\USER\projects\cmake dx cuda pytorch\cmake_integration_examples\pytorch\src\pytroch_load_model.cpp # 19]
00007FF6089D8A9400007FF6089D8A60 pytroch_load_model.exe!invoke_main [d:\agent\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl # 79]
00007FF6089D893E00007FF6089D8810 pytroch_load_model.exe!__scrt_common_main_seh [d:\agent\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl # 288]
00007FF6089D87FE00007FF6089D87F0 pytroch_load_model.exe!__scrt_common_main [d:\agent\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl # 331]
00007FF6089D8B2900007FF6089D8B20 pytroch_load_model.exe!mainCRTStartup [d:\agent\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp # 17]
00007FFBDF8C703400007FFBDF8C7020 KERNEL32.DLL!BaseThreadInitThunk [<unknown file> # <unknown line number>]
00007FFBDFBA265100007FFBDFBA2630 ntdll.dll!RtlUserThreadStart [<unknown file> # <unknown line number>]
Here is the code:
Python (PyTorch):
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
class TestModel(nn.Module):
def __init__(self):
super(TestModel, self).__init__()
self.x = 2
def forward(self):
return self.x
test_net = torch.jit.script(Net())
test_module = torch.jit.script(TestModel())
torch.jit.save(test_net, 'test_net.pt')
torch.jit.save(test_module, 'test_module.pt')
C++ (libtorch)
#include <torch/script.h>
#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 {
std::cout << "Trying to load model..." << std::endl;
// Deserialize the ScriptModule from a file using torch::jit::load().
module = torch::jit::load(argv[1]);
}
catch (const c10::Error& e) {
std::cerr << "Loading failed" << std::endl;
std::cerr << e.what() << std::endl;
return -1;
}
std::cout << "Loading successful" << std::endl;
}
I am using the shared distribution of libtorch 1.12.1. I have tried with both the GPU and CPU version (release, not debug builds) on Windows 10. The TestModel is even taken straight from the Torch JIT documentation...
CMakeLists.txt
cmake_minimum_required (VERSION 3.12 FATAL_ERROR)
project(pytroch
DESCRIPTION "CMake example for PyTorch (libtorch C++) integration"
LANGUAGES CXX
)
set(CMAKE_CXX_STANDARD 14)
set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src")
set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/deps/libtorch/1.12.1/release/cpu/share/cmake/Torch")
find_package(Torch REQUIRED)
if(TORCH_FOUND)
message(STATUS "Found Torch")
else()
message(CRITICAL_ERROR "Unable to find Torch")
endif(TORCH_FOUND)
add_executable(pytroch_load_model
"${SRC_DIR}/pytroch_load_model.cpp"
)
target_include_directories(pytroch_load_model PUBLIC ${TORCH_INCLUDE_DIRS})
target_link_libraries(pytroch_load_model PRIVATE ${TORCH_LIBRARIES})
message("${TORCH_LIBRARIES}")
file(GLOB LIBTORCH_DLLS
"${CMAKE_SOURCE_DIR}/deps/libtorch/1.12.1/release/cpu/lib/*.dll"
)
file(COPY
${LIBTORCH_DLLS}
DESTINATION "${CMAKE_BINARY_DIR}/bin/"
)
The CMakeLists.txt above is part of a larger project. I am posting it here to demonstrate how I am linking against the libraries required to run my code.
Since the PT file has mostly non-readable characters inside (after all it is serialized) I cannot really check what is going on in there. I can see though that Net as well as cpu are present as words (one can only partially read such a file).
I have created an issue on PyTorch GitHub page. It appears that on cannot combine a release build of the libtorch library with a debug build of the software that links against it.
The issue is gone once I switch to a release build. I will check with the debug build at some point but currently the code I have that uses libtorch is very tiny so no need for extensive debugging.
I see two problems with this:
The developer is forced to use the huge (especially the CUDA version) debug build of libtorch
The developer may not want to use a debug build especially if they don't want to debug libtorch itself.

ROS2 publish mesh marker

I'm trying to display a mesh resource in Rviz. It must be pretty straightforward as was in ROS1, but I'm not sure why it is not working.
Here is a sample code I tried:
// meshpub.cpp
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/color_rgba.hpp>
#include <geometry_msgs/msg/pose.hpp>
#include <visualization_msgs/msg/marker.hpp>
#include <ament_index_cpp/get_package_share_directory.hpp>
using namespace std;
int main(int argc, char ** argv)
{
(void) argc;
(void) argv;
rclcpp::init(argc, argv);
rclcpp::Node node("shape_pubilsher");
string base_frame = "base_link";
string topic = "/marker";
// Getting the model file path:
auto package_share_directory = ament_index_cpp::get_package_share_directory("package");
auto file_name = package_share_directory.append("/meshes/model.obj");
auto qos = rclcpp::QoS(1000);
auto publisher = node.create_publisher<visualization_msgs::msg::Marker>(topic, qos);
RCLCPP_INFO(node.get_logger(), "Waiting for Rviz to load...");
while(node.get_node_graph_interface()->count_subscribers(topic) == 0) {
rclcpp::sleep_for(200ms);
}
// Creating the marker and initialising its fields
geometry_msgs::msg::Pose pose;
pose.position.x = 0;
pose.position.y = 0;
pose.position.z = 0;
pose.orientation.x = 0;
pose.orientation.y = 0;
pose.orientation.z = 0;
pose.orientation.w = 0;
std_msgs::msg::ColorRGBA colour;
colour.a = 1;
colour.r = 1;
colour.g = 0;
colour.b = 0;
visualization_msgs::msg::Marker marker;
marker.header.frame_id = base_frame;
marker.header.stamp = node.now();
marker.action = visualization_msgs::msg::Marker::ADD;
marker.type = visualization_msgs::msg::Marker::MESH_RESOURCE;
marker.pose = pose;
marker.id = 0;
marker.mesh_resource = file_name;
marker.scale.x = 2;
marker.scale.y = 2;
marker.scale.z = 2;
marker.color = colour;
RCLCPP_INFO(node.get_logger(), "Attempting to publish mesh");
publisher->publish(marker);
while(rclcpp::ok());
return 0;
}
I have installed the meshes folder which contains 3D model files onto the package share directory. I have tried different model file formats like .obj, .fbx, .blend and .dae (all were supported in ROS1 Rviz), but Rviz refuses to display it, and all I get is this heartwarming congratulation message:
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [rviz2-1]: process started with pid [4733]
[INFO] [meshpub-2]: process started with pid [4735]
[meshpub-2] [INFO] [1664444431.191595500] [meshpub]: Waiting for Rviz to load...
[rviz2-1] QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-amirint'
[rviz2-1] [INFO] [1664444431.789824400] [rviz2]: Stereo is NOT SUPPORTED
[rviz2-1] [INFO] [1664444431.790017200] [rviz2]: OpenGl version: 3.1 (GLSL 1.4)
[rviz2-1] [INFO] [1664444431.863061600] [rviz2]: Stereo is NOT SUPPORTED
[meshpub-2] [INFO] [1664444433.394043100] [meshpub]: Attempting to publish mesh
[rviz2-1] [ERROR] [1664444434.023035400] [rviz2]: Could not load resource [/mnt/e/avnv/ros2_visually/install/package/share/package/meshes/model.obj]: Unable to open file "/mnt/e/avnv/ros2_visually/install/package/share/package/meshes/model.obj".
Although /mnt/e/avnv/ros2_visually/install/package/share/package/meshes/model.obj is the correct path of the resource, the error message does not specifically tell whether this is a no such file or directory or a file format not supported sort of thing. It only says Unable to open file ....
I'm using Ubuntu 20.04 on wsl2 with ROS_DISTRO=galactic.
File paths have to be in the form file:///path/to/file or package://path/to/file. I mistakenly assumed
ament_index_cpp::get_package_share_directory("package") will by default return the path in the mentioned form.
So just manually appending a file:// to the returned path string fixed the issue.

OpenCV DNN fails reading an ONNX network

I'm trying to load a simple four-layer convolutional neural network from an ONNX file in C++ with OpenCV. The ONNX file was created from a TensorFlow model using the tf2onnx library in Python. I saved the model with the following piece of code.
(onnx_model_proto, storage) = tf2onnx.convert.from_keras(model, opset=8)
with open(os.path.join("models", 'upscaleModelData.onnx'), "wb") as f:
f.write(onnx_model_proto.SerializeToString())
When reading via cv::dnn::Net net = cv::dnn::readNetFromONNX("model.onnx"); in C++, I get the following error.
[ INFO:0] global ####\master_winpack-build-win64-vc15\opencv\modules\dnn\src\onnx\onnx_importer.cpp (429) cv::dnn::dnn4_v20210608::ONNXImporter::populateNet DNN/ONNX: loading ONNX v4 model produced by 'tf2onnx':1.10.0. Number of nodes = 13, inputs = 1, outputs = 1
OpenCV(4.5.3) Error: Unspecified error (Can't create layer "model/tf.nn.depth_to_space/DepthToSpace:0" of type "DepthToSpace") in cv::dnn::dnn4_v20210608::LayerData::getLayerInstance, file ####\opencv\modules\dnn\src\dnn.cpp, line 621
[ERROR:0] global ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp (2127) cv::dnn::dnn4_v20210608::ONNXImporter::handleNode DNN/ONNX: ERROR during processing node with 1 inputs and 1 outputs: [DepthToSpace]:(model/tf.nn.depth_to_space/DepthToSpace:0)
[ INFO:0] global ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp (2131) cv::dnn::dnn4_v20210608::ONNXImporter::handleNode Input[0] = 'model/conv2d_3/Relu:0'
[ INFO:0] global ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp (2135) cv::dnn::dnn4_v20210608::ONNXImporter::handleNode Output[0] = 'model/tf.nn.depth_to_space/DepthToSpace:0'
OpenCV(4.5.3) Error: Unspecified error (> Node [DepthToSpace]:(model/tf.nn.depth_to_space/DepthToSpace:0) parse error: OpenCV(4.5.3) ####\opencv\modules\dnn\src\dnn.cpp:621: error: (-2:Unspecified error) Can't create layer "model/tf.nn.depth_to_space/DepthToSpace:0" of type "DepthToSpace" in function 'cv::dnn::dnn4_v20210608::LayerData::getLayerInstance'
> ) in cv::dnn::dnn4_v20210608::ONNXImporter::handleNode, file ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp, line 2146
OpenCV(4.5.3) ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp:2146: error: (-2:Unspecified error) in function 'cv::dnn::dnn4_v20210608::ONNXImporter::handleNode'
> Node [DepthToSpace]:(model/tf.nn.depth_to_space/DepthToSpace:0) parse error: OpenCV(4.5.3) ####\opencv\modules\dnn\src\dnn.cpp:621: error: (-2:Unspecified error) Can't create layer "model/tf.nn.depth_to_space/DepthToSpace:0" of type "DepthToSpace" in function 'cv::dnn::dnn4_v20210608::LayerData::getLayerInstance'
I'm using OpenCV on Windows and Visual C++.
This might be a similar issue to this: EMGU - EDSR : Can't create layer DepthToSpace.
I tried reading the same network from TensorFlow PB and H5 file, but I get a similar result.
For reading a PB file, created with cv::dnn::readNetFromTensorflow("model.pb") I get:
OpenCV(4.5.3) Error: Unspecified error (FAILED: fs.is_open(). Can't open "model.pb") in cv::dnn::ReadProtoFromBinaryFile, file ####\opencv\modules\dnn\src\caffe\caffe_io.cpp, line 1133
OpenCV(4.5.3) ####\opencv\modules\dnn\src\caffe\caffe_io.cpp:1133: error: (-2:Unspecified error) FAILED: fs.is_open(). Can't open "model.pb" in function 'cv::dnn::ReadProtoFromBinaryFile'
For reading a H5 file created with cv::dnn::readNetFromTensorflow("upscaleModelData.h5"); I get:
OpenCV(4.5.3) Error: Unspecified error (Cannot determine an origin framework of files: model.h5) in cv::dnn::dnn4_v20210608::readNet, file ####\opencv\modules\dnn\src\dnn.cpp, line 5461
OpenCV(4.5.3) ####\opencv\modules\dnn\src\dnn.cpp:5461: error: (-2:Unspecified error) Cannot determine an origin framework of files: model.h5 in function 'cv::dnn::dnn4_v20210608::readNet'
Does this mean I should make modifications to the model's layers so that it can be read by OpenCV? Is this a compatibility issue? Any feedback, workarounds or alternative approaches (e.g. TensorFlow C++ API solutions) are welcome.
Edit 1: Implementing a layer-type workaround
I reimplemented the depth_to_space layer manually in Python, using the following piece of code, based on these links: onnx-tensorflow, depth_to_space, keras-subpixel-conv.
x_shape = tf.shape(x)
n, h, w, c = x_shape[0], x_shape[1], x_shape[2], x_shape[3]
y = tf.reshape(x, (n, h, w, bs, bs, c // (bs ** 2)))
y = tf.transpose(y, (0, 1, 3, 2, 4, 5))
outputs = tf.reshape(y, (n, h * bs, w * bs, c // (bs ** 2)))
Now, when I run the same C++ code, I get the following error.
[ INFO:0] global ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp (429) cv::dnn::dnn4_v20210608::ONNXImporter::populateNet DNN/ONNX: loading ONNX v4 model produced by 'tf2onnx':1.10.0. Number of nodes = 38, inputs = 14, outputs = 1
OpenCV(4.5.3) Error: Assertion failed (indexMat.total() == 1) in cv::dnn::dnn4_v20210608::ONNXImporter::handleNode, file ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp, line 1842
[ERROR:0] global ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp (2127) cv::dnn::dnn4_v20210608::ONNXImporter::handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 1 outputs: [Gather]:(model/tf.compat.v1.shape/Shape:0)
[ INFO:0] global ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp (2131) cv::dnn::dnn4_v20210608::ONNXImporter::handleNode Input[0] = 'Shape__72:0'
[ INFO:0] global ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp (2131) cv::dnn::dnn4_v20210608::ONNXImporter::handleNode Input[1] = 'Const__76'
[ INFO:0] global ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp (2135) cv::dnn::dnn4_v20210608::ONNXImporter::handleNode Output[0] = 'model/tf.compat.v1.shape/Shape:0'
OpenCV(4.5.3) Error: Unspecified error (> Node [Gather]:(model/tf.compat.v1.shape/Shape:0) parse error: OpenCV(4.5.3) ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp:1842: error: (-215:Assertion failed) indexMat.total() == 1 in function 'cv::dnn::dnn4_v20210608::ONNXImporter::handleNode'
> ) in cv::dnn::dnn4_v20210608::ONNXImporter::handleNode, file ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp, line 2146
OpenCV(4.5.3) ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp:2146: error: (-2:Unspecified error) in function 'cv::dnn::dnn4_v20210608::ONNXImporter::handleNode'
> Node [Gather]:(model/tf.compat.v1.shape/Shape:0) parse error: OpenCV(4.5.3) ####\opencv\modules\dnn\src\onnx\onnx_importer.cpp:1842: error: (-215:Assertion failed) indexMat.total() == 1 in function 'cv::dnn::dnn4_v20210608::ONNXImporter::handleNode'

Torch Vision C++ interface error "Unknown builtin op: torchvision::nms"

I'm trying to run a script in jit(using script) in torchscript for FasterRCNN.
I installed CUDA 10.1, compatible cudnn, LibTorch (C++) 1.7.1 and Torch Vision 0.8.2
I followed the instructions in both torchscript and vision and I have the following:
--- CMakeLists.txt ---
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(load_and_run_model_proj)
list(APPEND CMAKE_PREFIX_PATH "/home/fstrati/libtorch_shared_cuda_10.1/libtorch")
list(APPEND CMAKE_PREFIX_PATH "/opt/vision_0.8.2")
find_package(Torch REQUIRED)
find_package(TorchVision REQUIRED)
add_executable(load_and_run_model src/load_and_run_model.cpp)
# target_link_libraries(load_and_run_model "${TORCH_LIBRARIES}")
target_link_libraries(load_and_run_model PUBLIC TorchVision::TorchVision)
set_property(TARGET load_and_run_model PROPERTY CXX_STANDARD 14)
--- CMakeLists.txt ---
and
--- src/load_and_run_model.cpp ---
#include <torch/script.h> // One-stop header.
#include <torchvision/vision.h>
#include <torchvision/nms.h>
#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 << e.what() << std::endl;
std::cerr << "error loading the model\n";
return -1;
}
std::cout << "ok\n";
return 0;
}
--- src/load_and_run_model.cpp ---
I compile & link fine.
When I try to run it with traced script TorchCcript for fasterRCNN creates with jit.script
I get the following error:
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 "C:\Users\andre\anaconda3\envs\pytorch\lib\site-packages\torchvision\ops\boxes.py", line 42
"""
_assert_has_ops()
return torch.ops.torchvision.nms(boxes, scores, iou_threshold)
~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
Serialized File "code/__torch__/torchvision/ops/boxes.py", line 93
_42 = __torch__.torchvision.extension._assert_has_ops
_43 = _42()
_44 = ops.torchvision.nms(boxes, scores, iou_threshold)
~~~~~~~~~~~~~~~~~~~ <--- HERE
return _44
'nms' is being compiled since it was called from 'batched_nms'
File "C:\Users\andre\anaconda3\envs\pytorch\lib\site-packages\torchvision\ops\boxes.py", line 88
offsets = idxs.to(boxes) * (max_coordinate + torch.tensor(1).to(boxes))
boxes_for_nms = boxes + offsets[:, None]
keep = nms(boxes_for_nms, scores, iou_threshold)
~~~ <--- HERE
return keep
Serialized File "code/__torch__/torchvision/ops/boxes.py", line 50
_18 = torch.slice(offsets, 0, 0, 9223372036854775807, 1)
boxes_for_nms = torch.add(boxes, torch.unsqueeze(_18, 1), alpha=1)
keep = __torch__.torchvision.ops.boxes.nms(boxes_for_nms, scores, iou_threshold, )
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
_11 = keep
return _11
'batched_nms' is being compiled since it was called from 'RegionProposalNetwork.filter_proposals'
Serialized File "code/__torch__/torchvision/models/detection/rpn.py", line 64
_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 "C:\Users\andre\anaconda3\envs\pytorch\lib\site-packages\torchvision\models\detection\rpn.py", line 344
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 37
proposals = (self.box_coder).decode(torch.detach(pred_bbox_deltas0), anchors, )
proposals0 = torch.view(proposals, [num_images, -1, 4])
_8 = (self).filter_proposals(proposals0, objectness0, images.image_sizes, num_anchors_per_level, )
~~~~~~~~~~~~~~~~~~~~~ <--- HERE
boxes, scores, = _8
losses = annotate(Dict[str, Tensor], {})
Any ideas or suggestion on how to cure this error: seems like the operator nms is not registered.
By the way the master branch of torchvision with cuda is not compiling so I report the error
for the tag v0.8.2 of torch vision.
After researching this issue I came about
this commit in master that is not in
v0.8.2 of torch vision:
https://github.com/pytorch/vision/pull/2798/commits/fb893e7ba390d1b668efb4b84b3376cf634bd043
Applying the commit to v0.8.2 solved the problem:
now operators are correctly registered in torch script

How to use pretty debugging printers to see Eigen objects in QtCreator?

I am trying to see the contents of an Eigen vector in the Locals and Expressions window of the QtCreator:
I see that it is a vector with 10 components:
but when I click it open I get:
I tried to use this script to do the trick. I saved it along with an empty __init__.py file into folder ~/Scripts/Eigen and created the .gdbinit file:
python
import sys
sys.path.insert(0, '/home/martin/Scripts/Eigen')
from printers import register_eigen_printers
register_eigen_printers (None)
end
I restarted QtCreator and nothing changed. I have checked the "read .gdbinit at startup" option in QtCreator settings and still nothing.
What is worrying me is that I know that in the past the viewing of Eigen objects worked without me meddling with some scripts.
Please, what am I doing wrong?
I am using:
gcc version 4.8.1
Ubuntu 13.10
gdb 7.6.1-ubuntu
Qt Creator 3.0.0
Based on Qt 5.2.0
and I am compiling using these flags:
g++ -c -pipe -gdwarf-4 -fvar-tracking-assignments -g
-Wall -W -fPIE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG
EDIT:
as #ggael suggested, I tried to run the gdb directly:
(gdb) run
Starting program: /home/martin/Projects/TestGrounds/test
Traceback (most recent call last):
File "/usr/lib/debug/usr/lib/i386-linux-gnu/libstdc++.so.6.0.18-gdb.py", line 59, in <module>
from libstdcxx.v6.printers import register_libstdcxx_printers
ImportError: No module named 'libstdcxx'
Traceback (most recent call last):
File "/usr/lib/debug/usr/lib/i386-linux-gnu/libstdc++.so.6.0.18-gdb.py", line 59, in <module>
from libstdcxx.v6.printers import register_libstdcxx_printers
ImportError: No module named 'libstdcxx'
And then:
(gdb) print vec
Python Exception <class 'TypeError'> 'map' object is not subscriptable:
Python Exception <class 'TypeError'> 'map' object is not subscriptable:
$1 = {<Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >> = {<Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >> = {<Eigen::DenseBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >> = {<Eigen::internal::special_scalar_op_base<Eigen::Matrix<double, -1, 1, 0, -1, 1>, double, double, false>> = {<Eigen::DenseCoeffsBase<Eigen::Matrix<double, -1, 1, 0, -1, 1>, 3>> = {<Eigen::DenseCoeffsBase<Eigen::Matrix<double, -1, 1, 0, -1, 1>, 1>> = {<Eigen::DenseCoeffsBase<Eigen::Matrix<double, -1, 1, 0, -1, 1>, 0>> = {<Eigen::EigenBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >> = {<No data fields>}, <No data fields>}, <No data fields>}, <No data fields>}, <No data fields>}, <No data fields>}, <No data fields>},
m_storage = {m_data = 0x804e020, m_rows = 10}}, <No data fields>}
Please, could you explain what does this mean? What is libstdcxx?
This is a bug in /usr/lib/debug/usr/lib/i386-linux-gnu/libstdc++.so.6.0.18-gdb.py. Make sure you have the lastest version of the gcc4.8 packages, it might be that this issue is already fixed in unbuntu (it is fixed in debian). See this bug entry. In the last ressort you can patch this file so that it searches in the right location.
Works for me as expected. The output looks like
vec (10 x 1), ColumnMajor Eigen::VectorXd
[0] 3.1400000000000001 double
[1] 3.1400000000000001 double
[2] 3.1400000000000001 double
[3] 3.1400000000000001 double
[4] 3.1400000000000001 double
[5] 3.1400000000000001 double
[6] 3.1400000000000001 double
[7] 3.1400000000000001 double
[8] 3.1400000000000001 double
[9] 3.1400000000000001 double
Make sure to use Qt Creator's own pretty-printing system by removing(!) the checkmark in Tools/Options/Debugger/GDB/Load system pretty printers. You also don't need the code you put into your .gdbinit in this case.
The problem was probably in the debugger itself. I tried out many combinations of
load \ do not load .gdbinit
load \ do not load system pretty printers
use \ don't use code model
-gdwarf-x compiler flags
and none of this really mattered. Then I tried a fresh install of Ubuntu 12.04 with old GDB 7.4 and a brand new QtCreator 3.0.1 and it worked! I got the same results for
gcc 4.6, gcc 4.7 and gcc 4.8.1.
Then I tried the newest GDB 7.7 on Ubuntu 13.10 and again it worked, while GDB 7.5, GDB 7.6 did not work.
Also a strange thing, std::shared_ptr is correctly viewed only in QtCreator 3.0.1.
std::multimap is not pretty printed in any configuration.
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <memory>
#include <eigen3/Eigen/Dense>
int main(int argc, char *argv[])
{
std::vector<int> vec(4, 3);
std::map<int, std::string> map = {{1,"one"},{2,"two"},{3,"three"}};
std::multimap<int, std::string> multi = {{1,"one"},{2,"two"},{3,"three"}};
Eigen::VectorXd vector = Eigen::VectorXd::Constant(3, 3.14);
std::shared_ptr<Eigen::VectorXd> pointer(new Eigen::VectorXd(vector));
std::cout << vector << std::endl;
return 0;
}