|
1 | 1 | Loading a PyTorch Model in C++ |
2 | 2 | ============================== |
3 | 3 |
|
| 4 | +**This tutorial was updated to work with PyTorch 1.2** |
| 5 | + |
4 | 6 | As its name suggests, the primary interface to PyTorch is the Python |
5 | 7 | programming language. While Python is a suitable and preferred language for |
6 | 8 | many scenarios requiring dynamism and ease of iteration, there are equally many |
@@ -129,10 +131,11 @@ earlier in the tracing example. To perform this serialization, simply call |
129 | 131 | `save <https://pytorch.org/docs/master/jit.html#torch.jit.ScriptModule.save>`_ |
130 | 132 | on the module and pass it a filename:: |
131 | 133 |
|
132 | | - traced_script_module.save("model.pt") |
| 134 | + traced_script_module.save("traced_resnet_model.pt") |
133 | 135 |
|
134 | | -This will produce a ``model.pt`` file in your working directory. We have now |
135 | | -officially left the realm of Python and are ready to cross over to the sphere |
| 136 | +This will produce a ``traced_resnet_model.pt`` file in your working directory. |
| 137 | +If you also would like to serialize ``my_module``, call ``my_module.save("my_module_model.pt")`` |
| 138 | +We have now officially left the realm of Python and are ready to cross over to the sphere |
136 | 139 | of C++. |
137 | 140 |
|
138 | 141 | Step 3: Loading Your Script Module in C++ |
@@ -285,13 +288,14 @@ distribution. If all goes well, it will look something like this: |
285 | 288 | [100%] Linking CXX executable example-app |
286 | 289 | [100%] Built target example-app |
287 | 290 |
|
288 | | -If we supply the path to the serialized ``ResNet18`` model we created earlier |
| 291 | +If we supply the path to the traced ``ResNet18`` model ``traced_resnet_model.pt`` we created earlier |
289 | 292 | to the resulting ``example-app`` binary, we should be rewarded with a friendly |
290 | | -"ok": |
| 293 | +"ok". Please note, if try to run this example with ``my_module_model.pt`` you will get an error saying that |
| 294 | +your input is of an incompatible shape. ``my_module_model.pt`` expects 1D instead of 4D. |
291 | 295 |
|
292 | 296 | .. code-block:: sh |
293 | 297 |
|
294 | | - root@4b5a67132e81:/example-app/build# ./example-app model.pt |
| 298 | + root@4b5a67132e81:/example-app/build# ./example-app <path_to_model>/traced_resnet_model.pt |
295 | 299 | ok |
296 | 300 |
|
297 | 301 | Step 4: Executing the Script Module in C++ |
@@ -338,7 +342,7 @@ application and running it with the same serialized model: |
338 | 342 | [ 50%] Building CXX object CMakeFiles/example-app.dir/example-app.cpp.o |
339 | 343 | [100%] Linking CXX executable example-app |
340 | 344 | [100%] Built target example-app |
341 | | - root@4b5a67132e81:/example-app/build# ./example-app model.pt |
| 345 | + root@4b5a67132e81:/example-app/build# ./example-app traced_resnet_model.pt |
342 | 346 | -0.2698 -0.0381 0.4023 -0.3010 -0.0448 |
343 | 347 | [ Variable[CPUFloatType]{1,5} ] |
344 | 348 |
|
|
0 commit comments