22.02.2024 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

dropout. You’re already familiar with all of this, except for the element in the

middle.

Adaptive Pooling

The element in the middle, AdaptiveAvgPool2d (F.adaptive_avg_pool2d() in its

functional form), is a special kind of pooling: Instead of requiring the kernel size

(and stride), it requires the desired output size. In other words, whatever the

image size it gets as input, it will return a tensor with the desired size.

"What’s so special about this?"

It gives you the freedom to use images of different sizes as inputs! We’ve seen that

convolutions and traditional max pooling, in general, shrink the image’s dimensions.

But the classifier part of the model expects an input of a determined size. That

means that the input image has to be of a determined size such that, at the end of

the shrinkage process, it matches what the classifier part is expecting. The

adaptive pooling, however, guarantees the output size, so it can easily match the

classifier expectations regarding input size.

Let’s say we have two dummy tensors representing feature maps produced by the

featurizer part of the model. The feature maps have different dimensions (32x32

and 12x12). Applying adaptive pooling to both of them ensures that both outputs

have the same dimensions (6x6):

result1 = F.adaptive_avg_pool2d(

torch.randn(16, 32, 32), output_size=(6, 6)

)

result2 = F.adaptive_avg_pool2d(

torch.randn(16, 12, 12), output_size=(6, 6)

)

result1.shape, result2.shape

Output

(torch.Size([16, 6, 6]), torch.Size([16, 6, 6]))

Cool, right? We have the architecture already, but our model is untrained (it still

has random weights). Let’s fix that by…

Transfer Learning in Practice | 507

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

Saved successfully!

Ooh no, something went wrong!