22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

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

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

The constructor method defines the seven elements used by four branches (you

can identify each of them in Figure 7.7). In this example, I’ve configured all 1x1

convolutions to produce two channels each, but it is not required that they all have

the same number of output channels. The same applies to both 3x3 and 5x5

convolution branches: Although I’ve configured them both to produce the same

number of channels (three) each, this is not a requirement.

It is required, though, that all branches produce outputs with

matching height and width. This means that the padding must be

adjusted according to the kernel size in order to output the

correct dimensions.

The forward method feeds the input x to each of the four branches, and then it uses

torch.cat() to concatenate the resulting channels along the corresponding

dimension (according to PyTorch’s NCHW shape convention). This concatenation

would fail if the height and width of the outputs did not match across the different

branches.

What if we run our example image (scissors, in the color version) through the

Inception module?

inception = Inception(in_channels=3)

output = inception(image)

output.shape

Output

torch.Size([1, 10, 300, 300])

There we go: The output has the expected ten channels.

As you can see, the idea behind the Inception module is actually quite simple. Later

versions had slightly different architectures (like switching the 5x5 convolution by

two 3x3 convolutions in a row), but the overall structure remains. There is one

more thing, though.

If you check Inception’s code in PyTorch, you’ll find that it does not use nn.Conv2d

directly, but rather something called BasicConv2d (reproduced below):

Inception Modules | 531

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

Saved successfully!

Ooh no, something went wrong!