Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
Another advantage of these shortcuts is that they provide ashorter path for the gradients to travel back to the initial layers,thus preventing the vanishing gradients problem.Residual BlocksWe’re finally ready to tackle the main component of the ResNet model (the topperformer of ILSVRC-2015), the residual block.Figure 7.10 - Residual blockThe residual block isn’t so different from our own DummyResidual model, except forthe fact that the residual block has two consecutive weight layers and a ReLUactivation at the end. Moreover, it may have more than two consecutive weightlayers, and the weight layers do not necessarily need to be linear.For image classification, it makes much more sense to use convolutional instead oflinear layers, right? Right! And why not throw some batch normalization layers inthe mix? Sure! The residual block looks like this now:Residual Connections | 551
class ResidualBlock(nn.Module):def __init__(self, in_channels, out_channels, stride=1):super(ResidualBlock, self).__init__()self.conv1 = nn.Conv2d(in_channels, out_channels,kernel_size=3, padding=1, stride=stride,bias=False)self.bn1 = nn.BatchNorm2d(out_channels)self.relu = nn.ReLU(inplace=True)self.conv2 = nn.Conv2d(out_channels, out_channels,kernel_size=3, padding=1,bias=False)self.bn2 = nn.BatchNorm2d(out_channels)self.downsample = Noneif out_channels != in_channels:self.downsample = nn.Conv2d(in_channels, out_channels,kernel_size=1, stride=stride)def forward(self, x):identity = x# First "weight layer" + activationout = self.conv1(x)out = self.bn1(out)out = self.relu(out)# Second "weight layer"out = self.conv2(out)out = self.bn2(out)# What is that?!if self.downsample is not None:identity = self.downsample(identity)# Adding inputs before activationout = out + identityout = self.relu(out)return out552 | Chapter 7: Transfer Learning
- 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 550 and 551: Figure 7.4 - 1x1 convolutionThe inp
- 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 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
- Page 600 and 601: torch.manual_seed(42)parm = nn.Para
- Page 602 and 603: (and only if) the norm exceeds the
- Page 604 and 605: if callable(self.clipping): 1self.c
- Page 606 and 607: Moreover, let’s use a ten times h
- Page 608 and 609: Clipping with HooksFirst, we reset
- Page 610 and 611: • visualizing the difference betw
- Page 612 and 613: Chapter 8SequencesSpoilersIn this c
- Page 614 and 615: Before shuffling, the pixels were o
- Page 616 and 617: And then let’s visualize the firs
- Page 618 and 619: sequence so far, and a data point f
- Page 620 and 621: Considering this, the not "unrolled
- Page 622 and 623: linear_input = nn.Linear(n_features
- Page 624 and 625: Outputtensor([[0.3924, 0.8146]], gr
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super(ResidualBlock, self).__init__()
self.conv1 = nn.Conv2d(
in_channels, out_channels,
kernel_size=3, padding=1, stride=stride,
bias=False
)
self.bn1 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(
out_channels, out_channels,
kernel_size=3, padding=1,
bias=False
)
self.bn2 = nn.BatchNorm2d(out_channels)
self.downsample = None
if out_channels != in_channels:
self.downsample = nn.Conv2d(
in_channels, out_channels,
kernel_size=1, stride=stride
)
def forward(self, x):
identity = x
# First "weight layer" + activation
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
# Second "weight layer"
out = self.conv2(out)
out = self.bn2(out)
# What is that?!
if self.downsample is not None:
identity = self.downsample(identity)
# Adding inputs before activation
out = out + identity
out = self.relu(out)
return out
552 | Chapter 7: Transfer Learning