Skip to content

Commit 9789474

Browse files
authored
Merge branch 'master' into packed_accessor
2 parents 1f6b4a4 + f9d5be7 commit 9789474

14 files changed

+563
-171
lines changed

_static/img/torchscript_to_cpp.png

133 KB
Loading
65.2 KB
Loading

advanced_source/cpp_export.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Loading a PyTorch Model in C++
2-
==============================
1+
3. Loading a TorchScript Model in C++
2+
=====================================
33

44
**This tutorial was updated to work with PyTorch 1.2**
55

@@ -94,8 +94,8 @@ vanilla Pytorch model::
9494

9595
Because the ``forward`` method of this module uses control flow that is
9696
dependent on the input, it is not suitable for tracing. Instead, we can convert
97-
it to a ``ScriptModule``.
98-
In order to convert the module to the ``ScriptModule``, one needs to
97+
it to a ``ScriptModule``.
98+
In order to convert the module to the ``ScriptModule``, one needs to
9999
compile the module with ``torch.jit.script`` as follows::
100100

101101
class MyModule(torch.nn.Module):
@@ -133,7 +133,7 @@ on the module and pass it a filename::
133133

134134
traced_script_module.save("traced_resnet_model.pt")
135135

136-
This will produce a ``traced_resnet_model.pt`` file in your working directory.
136+
This will produce a ``traced_resnet_model.pt`` file in your working directory.
137137
If you also would like to serialize ``my_module``, call ``my_module.save("my_module_model.pt")``
138138
We have now officially left the realm of Python and are ready to cross over to the sphere
139139
of C++.
@@ -168,7 +168,7 @@ do:
168168
return -1;
169169
}
170170
171-
171+
172172
torch::jit::script::Module module;
173173
try {
174174
// Deserialize the ScriptModule from a file using torch::jit::load().

advanced_source/cpp_frontend.rst

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -769,24 +769,24 @@ modules in the ``forward()`` method of a module we define ourselves:
769769
.. code-block:: cpp
770770
771771
struct GeneratorImpl : nn::Module {
772-
GeneratorImpl()
773-
: conv1(nn::Conv2dOptions(kNoiseSize, 512, 4)
772+
GeneratorImpl(int kNoiseSize)
773+
: conv1(nn::Conv2dOptions(kNoiseSize, 256, 4)
774774
.with_bias(false)
775775
.transposed(true)),
776-
batch_norm1(512),
777-
conv2(nn::Conv2dOptions(512, 256, 4)
776+
batch_norm1(256),
777+
conv2(nn::Conv2dOptions(256, 128, 3)
778778
.stride(2)
779779
.padding(1)
780780
.with_bias(false)
781781
.transposed(true)),
782-
batch_norm2(256),
783-
conv3(nn::Conv2dOptions(256, 128, 4)
782+
batch_norm2(128),
783+
conv3(nn::Conv2dOptions(128, 64, 4)
784784
.stride(2)
785785
.padding(1)
786786
.with_bias(false)
787787
.transposed(true)),
788-
batch_norm3(128),
789-
conv4(nn::Conv2dOptions(128, 64, 4)
788+
batch_norm3(64),
789+
conv4(nn::Conv2dOptions(64, 1, 4)
790790
.stride(2)
791791
.padding(1)
792792
.with_bias(false)
@@ -796,19 +796,28 @@ modules in the ``forward()`` method of a module we define ourselves:
796796
.stride(2)
797797
.padding(1)
798798
.with_bias(false)
799-
.transposed(true)) {}
800-
801-
torch::Tensor forward(torch::Tensor x) {
802-
x = torch::relu(batch_norm1(conv1(x)));
803-
x = torch::relu(batch_norm2(conv2(x)));
804-
x = torch::relu(batch_norm3(conv3(x)));
805-
x = torch::relu(batch_norm4(conv4(x)));
806-
x = torch::tanh(conv5(x));
807-
return x;
808-
}
809-
810-
nn::Conv2d conv1, conv2, conv3, conv4, conv5;
811-
nn::BatchNorm batch_norm1, batch_norm2, batch_norm3, batch_norm4;
799+
.transposed(true))
800+
{
801+
// register_module() is needed if we want to use the parameters() method later on
802+
register_module("conv1", conv1);
803+
register_module("conv2", conv2);
804+
register_module("conv3", conv3);
805+
register_module("conv4", conv4);
806+
register_module("batch_norm1", batch_norm1);
807+
register_module("batch_norm2", batch_norm1);
808+
register_module("batch_norm3", batch_norm1);
809+
}
810+
811+
torch::Tensor forward(torch::Tensor x) {
812+
x = torch::relu(batch_norm1(conv1(x)));
813+
x = torch::relu(batch_norm2(conv2(x)));
814+
x = torch::relu(batch_norm3(conv3(x)));
815+
x = torch::tanh(conv4(x));
816+
return x;
817+
}
818+
819+
nn::Conv2d conv1, conv2, conv3, conv4;
820+
nn::BatchNorm batch_norm1, batch_norm2, batch_norm3;
812821
};
813822
TORCH_MODULE(Generator);
814823

advanced_source/super_resolution_with_onnxruntime.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Exporting a Model from PyTorch to ONNX and Running it using ONNX Runtime
2+
4. (optional) Exporting a Model from PyTorch to ONNX and Running it using ONNX Runtime
33
========================================================================
44
55
In this tutorial, we describe how to convert a model defined
@@ -37,14 +37,14 @@
3737
# and is widely used in image processing or video editing. For this
3838
# tutorial, we will use a small super-resolution model.
3939
#
40-
# First, let's create a SuperResolution model in PyTorch.
40+
# First, let's create a SuperResolution model in PyTorch.
4141
# This model uses the efficient sub-pixel convolution layer described in
4242
# `"Real-Time Single Image and Video Super-Resolution Using an Efficient
4343
# Sub-Pixel Convolutional Neural Network" - Shi et al <https://arxiv.org/abs/1609.05158>`__
4444
# for increasing the resolution of an image by an upscale factor.
4545
# The model expects the Y component of the YCbCr of an image as an input, and
46-
# outputs the upscaled Y component in super resolution.
47-
#
46+
# outputs the upscaled Y component in super resolution.
47+
#
4848
# `The
4949
# model <https://github.com/pytorch/examples/blob/master/super_resolution/model.py>`__
5050
# comes directly from PyTorch's examples without modification:
@@ -94,7 +94,7 @@ def _initialize_weights(self):
9494
# It is important to call ``torch_model.eval()`` or ``torch_model.train(False)``
9595
# before exporting the model, to turn the model to inference mode.
9696
# This is required since operators like dropout or batchnorm behave
97-
# differently in inference and training mode.
97+
# differently in inference and training mode.
9898
#
9999

100100
# Load pretrained model weights
@@ -113,7 +113,7 @@ def _initialize_weights(self):
113113

114114
######################################################################
115115
# Exporting a model in PyTorch works via tracing or scripting. This
116-
# tutorial will use as an example a model exported by tracing.
116+
# tutorial will use as an example a model exported by tracing.
117117
# To export a model, we call the ``torch.onnx.export()`` function.
118118
# This will execute the model, recording a trace of what operators
119119
# are used to compute the outputs.
@@ -124,10 +124,10 @@ def _initialize_weights(self):
124124
# all the input's dimensions, unless specified as a dynamic axes.
125125
# In this example we export the model with an input of batch_size 1,
126126
# but then specify the first dimension as dynamic in the ``dynamic_axes``
127-
# parameter in ``torch.onnx.export()``.
127+
# parameter in ``torch.onnx.export()``.
128128
# The exported model will thus accept inputs of size [batch_size, 1, 224, 224]
129-
# where batch_size can be variable.
130-
#
129+
# where batch_size can be variable.
130+
#
131131
# To learn more details about PyTorch's export interface, check out the
132132
# `torch.onnx documentation <https://pytorch.org/docs/master/onnx.html>`__.
133133
#
@@ -238,7 +238,7 @@ def to_numpy(tensor):
238238
# Then we split the image into its Y, Cb, and Cr components.
239239
# These components represent a greyscale image (Y), and
240240
# the blue-difference (Cb) and red-difference (Cr) chroma components.
241-
# The Y component being more sensitive to the human eye, we are
241+
# The Y component being more sensitive to the human eye, we are
242242
# interested in this component which we will be transforming.
243243
# After extracting the Y component, we convert it to a tensor which
244244
# will be the input of our model.
@@ -278,7 +278,7 @@ def to_numpy(tensor):
278278
# The post-processing steps have been adopted from PyTorch
279279
# implementation of super-resolution model
280280
# `here <https://github.com/pytorch/examples/blob/master/super_resolution/super_resolve.py>`__.
281-
#
281+
#
282282

283283
img_out_y = Image.fromarray(np.uint8((img_out_y[0] * 255.0).clip(0, 255)[0]), mode='L')
284284

@@ -301,12 +301,12 @@ def to_numpy(tensor):
301301
#
302302
# ONNX Runtime being a cross platform engine, you can run it across
303303
# multiple platforms and on both CPUs and GPUs.
304-
#
304+
#
305305
# ONNX Runtime can also be deployed to the cloud for model inferencing
306306
# using Azure Machine Learning Services. More information `here <https://docs.microsoft.com/en-us/azure/machine-learning/service/concept-onnx>`__.
307307
#
308308
# More information about ONNX Runtime's performance `here <https://github.com/microsoft/onnxruntime#high-performance>`__.
309309
#
310-
#
310+
#
311311
# For more information about ONNX Runtime `here <https://github.com/microsoft/onnxruntime>`__.
312312
#

0 commit comments

Comments
 (0)