Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
Figure 7.4 - 1x1 convolutionThe input is an RGB image, and there are two filters; each filter has three 1x1kernels, one for each channel of the input. What are these filters actually doing?Let’s check it out!Figure 7.5 - 1x1 convolutionMaybe it is even more clear if it is presented as a formula:Equation 7.1 - Filter arithmeticA filter using a 1x1 convolution corresponds to a weightedaverage of the input channels.In other words, a 1x1 convolution is a linear combination of theinput channels, computed pixel by pixel.There is another way to get a linear combination of the inputs: a linear layer, also1x1 Convolutions | 525
referred to as a fully connected layer. Performing a 1x1 convolution is akin toapplying a linear layer to each individual pixel over its channels.This is the reason why a 1x1 convolution is said to be equivalentto a fully connected (linear) layer.In the example above, each of the two filters produces a different linearcombination of the RGB channels. Does this ring any bells? In Chapter 6, we sawthat grayscale images can be computed using a linear combination of the red,green, and blue channels of colored images. So, we can convert an image tograyscale using a 1x1 convolution!scissors = Image.open('rps/scissors/scissors01-001.png')image = ToTensor()(scissors)[:3, :, :].view(1, 3, 300, 300)weights = torch.tensor([0.2126, 0.7152, 0.0722]).view(1, 3, 1, 1)convolved = F.conv2d(input=image, weight=weights)converted = ToPILImage()(convolved[0])grayscale = scissors.convert('L')Figure 7.6 - Convolution vs conversionSee? They are the same … or are they? If you have a really sharp eye, maybe you areable to notice a subtle difference between the two shades of gray. It doesn’t haveanything to do with the use of convolutions, though—it turns out, PIL uses slightlydifferent weights for converting RGB into grayscale.526 | Chapter 7: Transfer Learning
- Page 500 and 501: Equation 6.16 - Looking aheadOnce N
- Page 502 and 503: Figure 6.22 - Path taken by each SG
- Page 504 and 505: for epoch in range(4):# training lo
- Page 506 and 507: course) up to a given number of epo
- Page 508 and 509: Next, we create a protected method
- Page 510 and 511: Mini-Batch SchedulersThese schedule
- Page 512 and 513: Schedulers in StepByStep — Part I
- Page 514 and 515: Scheduler PathsBefore trying out a
- Page 516 and 517: After applying each scheduler to SG
- Page 518 and 519: Data Preparation1 # Loads temporary
- Page 520 and 521: Figure 6.31 - LossesEvaluationprint
- Page 522 and 523: [96] http://www.samkass.com/theorie
- Page 524 and 525: ImportsFor the sake of organization
- Page 526 and 527: ILSVRC-2012The 2012 edition [111] o
- Page 528 and 529: remained unchanged.ResNet (MSRA Tea
- Page 530 and 531: Transfer Learning in PracticeIn Cha
- Page 532 and 533: dropout. You’re already familiar
- Page 534 and 535: OutputDownloading: "https://downloa
- Page 536 and 537: Replacing the "Top" of the Model1 a
- Page 538 and 539: Model Size Classifier Layer(s) Repl
- Page 540 and 541: Model TrainingWe have everything se
- Page 542 and 543: "Removing" the Top Layer1 alex.clas
- Page 544 and 545: torch.save(train_preproc.tensors, '
- Page 546 and 547: Outputtensor([[109, 124],[124, 124]
- Page 548 and 549: Model Configuration1 optimizer_mode
- Page 552 and 553: The weights used by PIL are 0.299 f
- Page 554 and 555: • reduce the number of output cha
- Page 556 and 557: The constructor method defines the
- Page 558 and 559: Does it sound familiar? That’s wh
- Page 560 and 561: and w to represent these parameters
- Page 562 and 563: A mini-batch of size 64 is small en
- Page 564 and 565: normed1 = batch_normalizer(batch1[0
- Page 566 and 567: OutputOrderedDict([('running_mean',
- Page 568 and 569: OutputOrderedDict([('running_mean',
- Page 570 and 571: batch_normalizer = nn.BatchNorm2d(n
- Page 572 and 573: torch.manual_seed(23)dummy_points =
- Page 574 and 575: np.concatenate([dummy_points[:5].nu
- Page 576 and 577: Another advantage of these shortcut
- Page 578 and 579: It should be pretty clear, except f
- Page 580 and 581: Data Preparation1 # ImageNet statis
- Page 582 and 583: Data Preparation — Preprocessing1
- Page 584 and 585: • freezing the layers of the mode
- Page 586 and 587: Extra ChapterVanishing and Explodin
- Page 588 and 589: discussing it, let me illustrate it
- Page 590 and 591: Model Configuration (2)1 loss_fn =
- Page 592 and 593: weights. If done properly, the init
- Page 594 and 595: just did), or, if you are training
- Page 596 and 597: Figure E.3 - The effect of batch no
- Page 598 and 599: Model Configuration1 torch.manual_s
referred to as a fully connected layer. Performing a 1x1 convolution is akin to
applying a linear layer to each individual pixel over its channels.
This is the reason why a 1x1 convolution is said to be equivalent
to a fully connected (linear) layer.
In the example above, each of the two filters produces a different linear
combination of the RGB channels. Does this ring any bells? In Chapter 6, we saw
that grayscale images can be computed using a linear combination of the red,
green, and blue channels of colored images. So, we can convert an image to
grayscale using a 1x1 convolution!
scissors = Image.open('rps/scissors/scissors01-001.png')
image = ToTensor()(scissors)[:3, :, :].view(1, 3, 300, 300)
weights = torch.tensor([0.2126, 0.7152, 0.0722]).view(1, 3, 1, 1)
convolved = F.conv2d(input=image, weight=weights)
converted = ToPILImage()(convolved[0])
grayscale = scissors.convert('L')
Figure 7.6 - Convolution vs conversion
See? They are the same … or are they? If you have a really sharp eye, maybe you are
able to notice a subtle difference between the two shades of gray. It doesn’t have
anything to do with the use of convolutions, though—it turns out, PIL uses slightly
different weights for converting RGB into grayscale.
526 | Chapter 7: Transfer Learning