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.

Let’s see what the Inception module looks like in code:

class Inception(nn.Module):

def __init__(self, in_channels):

super(Inception, self).__init__()

# in_channels@HxW -> 2@HxW

self.branch1x1_1 = nn.Conv2d(in_channels,2,kernel_size=1)

# in_channels@HxW -> 2@HxW -> 3@HxW

self.branch5x5_1 = nn.Conv2d(in_channels,2,kernel_size=1) 1

self.branch5x5_2 = nn.Conv2d(2, 3, kernel_size=5, padding=2)

# in_channels@HxW -> 2@HxW -> 3@HxW

self.branch3x3_1 = nn.Conv2d(in_channels,2,kernel_size=1) 1

self.branch3x3_2 = nn.Conv2d(2, 3, kernel_size=3, padding=1)

# in_channels@HxW -> in_channels@HxW -> 1@HxW

self.branch_pool_1 = nn.AvgPool2d(

kernel_size=3, stride=1, padding=1

)

self.branch_pool_2 = nn.Conv2d(

1

in_channels, 2, kernel_size=1

)

def forward(self, x):

# Produces 2 channels

branch1x1 = self.branch1x1_1(x)

# Produces 3 channels

branch5x5 = self.branch5x5_1(x)

branch5x5 = self.branch5x5_2(branch5x5)

# Produces 3 channels

branch3x3 = self.branch3x3_1(x)

branch3x3 = self.branch3x3_2(branch3x3)

# Produces 2 channels

branch_pool = self.branch_pool_1(x)

branch_pool = self.branch_pool_2(branch_pool)

# Concatenates all channels together (10)

outputs = torch.cat([branch1x1, branch5x5,

branch3x3, branch_pool], 1)

return outputs

1

1

1

2

1 Dimension reduction with 1x1 convolution

2 Stacking / concatenating channels

530 | Chapter 7: Transfer Learning

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

Saved successfully!

Ooh no, something went wrong!