20.03.2021 Views

Deep-Learning-with-PyTorch

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

468 CHAPTER 15 Deploying to production

You can download a C++ library of PyTorch from the PyTorch page. But given that

we already have PyTorch installed, 10 we might as well use that; it comes with all we

need for C++. We need to know where our PyTorch installation lives, so open Python

and check torch.__file__, which may say /usr/local/lib/python3.7/dist-packages/

torch/__init__.py. This means the CMake files we need are in /usr/local/lib/

python3.7/dist-packages/torch/share/cmake/.

While using CMake seems like overkill for a single source file project, linking to

PyTorch is a bit complex; so we just use the following as a boilerplate CMake file. 11

Listing 15.12

CMakeLists.txt

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

project(cyclegan-jit)

We need Torch.

find_package(Torch REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(cyclegan-jit cyclegan_jit.cpp)

target_link_libraries(cyclegan-jit pthread jpeg X11)

target_link_libraries(cyclegan-jit "${TORCH_LIBRARIES}")

set_property(TARGET cyclegan-jit PROPERTY CXX_STANDARD 14)

Project name. Replace it with your

own here and on the other lines.

We want to compile

an executable named

cyclegan-jit from the

cyclegan_jit.cpp

source file.

Links to the bits required

for CImg. CImg itself is allinclude,

so it does not

appear here.

It is best to make a build directory as a subdirectory of where the source code resides

and then in it run CMake as 12 CMAKE_PREFIX_PATH=/usr/local/lib/python3.7/

dist-packages/torch/share/cmake/ cmake .. and finally make. This will build the

cyclegan-jit program, which we can then run as follows:

./cyclegan-jit ../traced_zebra_model.pt

../../data/p1ch2/horse.jpg /tmp/z.jpg

We just ran our PyTorch model without Python. Awesome! If you want to ship your

application, you likely want to copy the libraries from /usr/local/lib/python3.7/distpackages/torch/lib

into where your executable is, so that they will always be found.

15.4.2 C++ from the start: The C++ API

The C++ modular API is intended to feel a lot like the Python one. To get a taste, we

will translate the CycleGAN generator into a model natively defined in C++, but without

the JIT. We do, however, need the pretrained weights, so we’ll save a traced version

of the model (and here it is important to trace not a function but the model).

10 We hope you have not been slacking off about trying out things you read.

11 The code directory has a bit longer version to work around Windows issues.

12 You might have to replace the path with where your PyTorch or LibTorch installation is located. Note that

the C++ library can be more picky than the Python one in terms of compatibility: If you are using a CUDAenabled

library, you need to have the matching CUDA headers installed. If you get cryptic error messages

about “Caffe2 using CUDA,” you need to install a CPU-only version of the library, but CMake found a CUDAenabled

one.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!