Skip to content

Commit 793b647

Browse files
authored
Add hello world example for C++ torchvision (#2003)
* Add hello world example for C++ torchvision * README updates and mention the hello_world example
1 parent 91b4459 commit 793b647

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

README.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ Once installed, the library can be accessed in cmake (after properly configuring
8282
find_package(TorchVision REQUIRED)
8383
target_link_libraries(my-target PUBLIC TorchVision::TorchVision)
8484
85-
The ``TorchVision`` package will also automatically look for the ``Torch`` and ``pybind11`` packages and add them as dependencies to ``my-target``,
86-
so make sure that they are also available to cmake via the ``CMAKE_PREFIX_PATH``.
85+
The ``TorchVision`` package will also automatically look for the ``Torch`` package and add it as a dependency to ``my-target``,
86+
so make sure that it is also available to cmake via the ``CMAKE_PREFIX_PATH``.
87+
88+
For an example setup, take a look at ``examples/cpp/hello_world``.
8789

8890
Documentation
8991
=============
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(hello-world)
3+
4+
# The first thing do is to tell cmake to find the TorchVision library.
5+
# The package pulls in all the necessary torch libraries,
6+
# so there is no need to also add `find_package(Torch)` here.
7+
find_package(TorchVision REQUIRED)
8+
9+
add_executable(hello-world main.cpp)
10+
11+
# We now need to link the TorchVision library to our executable.
12+
# We can do that by using the TorchVision::TorchVision target,
13+
# which also adds all the necessary torch dependencies.
14+
target_link_libraries(hello-world TorchVision::TorchVision)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Hello World!
2+
============
3+
4+
This is a minimal example of getting TorchVision to work in C++ with CMake.
5+
6+
7+
In order to successfully compile this example, make sure you have both ``LibTorch`` and
8+
``TorchVision`` installed.
9+
Once both dependencies are sorted, we can start the CMake fun:
10+
11+
1) Create a ``build`` directory inside the current one.
12+
2) from within the ``build`` directory, run the following commands:
13+
- | ``cmake -DCMAKE_PREFIX_PATH="<PATH_TO_LIBTORCH>;<PATH_TO_TORCHVISION>" ..``
14+
| where ``<PATH_TO_LIBTORCH>`` and ``<PATH_TO_TORCHVISION>`` are the paths to the libtorch and torchvision installations.
15+
- ``cmake --build .``
16+
17+
| That's it!
18+
| You should now have a ``hello-world`` executable in your ``build`` folder.
19+
Running it will output a (fairly long) tensor of random values to your terminal.

examples/cpp/hello_world/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
3+
#include <torchvision/models/resnet.h>
4+
5+
int main()
6+
{
7+
auto model = vision::models::ResNet18();
8+
model->eval();
9+
10+
// Create a random input tensor and run it through the model.
11+
auto in = torch::rand({1, 3, 10, 10});
12+
auto out = model->forward(in);
13+
14+
std::cout << out;
15+
}

0 commit comments

Comments
 (0)