5 – PyTorch C API V2

Now, let’s see how we actually load our Script Module into C++. So for this, there is a requirement that we need LibTorch, so this is the C++ API for PyTorch and we’ll also want to use CMake for building our actual application. So, here we’re just going to make a very simple C++ application that only loads in our module and then if that’s successful, it’s just going to print out okay. So here, we are including LibTorch and then we are creating our main application function, and this is going to take some arguments from the command line. So, where we’re going to use this, is that we’re going to call our application from the command line and pass in the path to our Script Module, which we exported before. Here, this line is where we actually load in our Script Module. So, we’re going to create a shared pointer for our Script Module and then we use torch jit load, to actually load in our module itself. So, argv here is the path to our exported Script Module. Then, if our module isn’t null, so if we actually did load in this thing, then we’re going to print out okay to the standard output. It’s like I was mentioning before, we are using CMake to build our application, so this is an example of CMakeLists.txt that’s going to configure how we build it. Specifically, it’s going to be looking for the LibTorch package, so find package torch. We get LibTorch, you can just download it from the download page here on the PyTorch website. So, if you download it and unzip it, then you’ll get this folder with the structure, looks like this. So, when we’re building our application, we just need to point CMake to this library and then it will automatically have all the headers, libraries, and everything it needs, and it will compile our application for us. Then to build our application, we want to have our application directory look like this. So, we have this top-level directory example app, and then include our CMakeLists.txt which is up here, just copy this, and then our actual C++ file. We’ll create a build directory then move into that directory, so then we will call CMake and we’ll point it to our LibTorch package, so wherever that is, and then the dot dot here will point it to our actual C++ files in the directory one above from here. Type make and this should build our application. So, if everything went well, now we just need to call our application and then pass in our saved Script Module and then if it loads in, everything’s good, then it’ll return an okay for us. So, that was just a really simple scripting, we weren’t really doing anything with the model itself, we were just loading it in and making sure that worked. So, we can add just a few lines to our existing application, where we’re actually going to pass them data through our model. Here, we’re just creating a vector called inputs and then we’re going to create a torch Tensor of ones with the size 1, 3, 224 by 224, and going to insert this into our vector inputs. So, we have our model loaded into module here and we just call the forward method on this passing our inputs, and then we convert it to a Tensor and we’re going to set this to this variable output. Now, we can look at the values and output using output.slice and so, we’re going to look at the first dimension. We’re going to start at zero and then end at the fifth element. Here’s a nice little tip, if you want to learn more about the PyTorch C++ API, then check out the docs here. So, pytorch.org/cppdocs. So, now if we again build and compile this application then run it, you’ll see that we actually get an output from our model. This is a pretty simple example of how you use the C++ API and Script modules. But using the same process, you should be able to start deploying your own models to C++ environments. So, the general workflow for this will be building and defining your model in Python with PyTorch, training it there, and then once it’s all trained, you can convert it to a Script Module either with tracing or the annotations, then serialize it with the save method, and then from there you can use the C++ API to load it into a C++ application and go from there. Cheers.

%d 블로거가 이것을 좋아합니다: