Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
return figsetattr(StepByStep, 'visualize_outputs', visualize_outputs)Then, let’s use the method above to plot the feature maps for the layers in thefeaturizer part of our model:featurizer_layers = ['conv1', 'relu1', 'maxp1', 'flatten']with plt.style.context('seaborn-white'):fig = sbs_cnn1.visualize_outputs(featurizer_layers)Figure 5.20 - Feature maps (featurizer)Figure 5.21 - Mini-batch of images (reproduced here for an easier comparison)Looks cool, right? Even though I’ve plotted the images in the first four rows withthe same size, they have different dimensions, as indicated by the row labels on theleft. The shade of gray is also computed per row: The maximum (white) andminimum (black) values were computed across the ten images produced by a givenlayer; otherwise, some rows would be too dark (the ranges vary a lot from one layerto the next).What can we learn from these images? First, convolving the learned filter with theinput image produces some interesting results:Visualizing Filters and More! | 405
• For diagonals tilted to the left (images #0, #1, #2, and #7), the filter seems tosuppress the diagonal completely.• For parallel lines (only verticals in the example above, images #3 to #6, and #8),the filter produces a striped pattern, brighter to the left of the original line,darker to its right.• For diagonals tilted to the right (only image #9), the filter produces a thickerline with multiple shades.Then, the ReLU activation function removes the negative values. Unfortunately,after this operation, images #6 and #8 (parallel vertical lines) had all linessuppressed and seem indistinguishable from images #0, #1, #2, and #7 (diagonalstilted to the left).Next, max pooling reduces the dimensions of the images, and they get flattened torepresent sixteen features.Now, look at the flattened features. That’s what the classifier will look at to try tosplit the images into three different classes. For a relatively simple problem likethis, we can pretty much see the patterns there. Let’s see what the classifier layerscan make of it.Visualizing Classifier LayersThe second part of our model, which is aptly called a classifier, has the typicalstructure: a hidden layer (FC1), an activation function, and an output layer (FC2).Let’s visualize the outputs of each and every one of the layers that were capturedby our hook function for the same ten images:classifier_layers = ['fc1', 'relu2', 'fc2']with plt.style.context('seaborn-white'):fig = sbs_cnn1.visualize_outputs(classifier_layers, y=labels_batch, yhat=predicted)406 | Chapter 5: Convolutions
- Page 380 and 381: OutputParameter containing:tensor([
- Page 382 and 383: Moreover, notice that if we were to
- Page 384 and 385: In code, as usual, PyTorch gives us
- Page 386 and 387: Outputtensor([[[[5., 5., 0., 8., 7.
- Page 388 and 389: edge = np.array([[[[0, 1, 0],[1, -4
- Page 390 and 391: A pooling kernel of two-by-two resu
- Page 392 and 393: Outputtensor([[22., 23., 11., 24.,
- Page 394 and 395: Figure 5.15 - LeNet-5 architectureS
- Page 396 and 397: • second block: produces 16-chann
- Page 398 and 399: Transformed Dataset1 class Transfor
- Page 400 and 401: LossNew problem, new loss. Since we
- Page 402 and 403: Outputtensor([4.0000, 1.0000, 0.500
- Page 404 and 405: The loss only considers the predict
- Page 406 and 407: Outputtensor([[-1.5229, -0.3146, -2
- Page 408 and 409: IMPORTANT: I can’t stress this en
- Page 410 and 411: figures at the beginning of this ch
- Page 412 and 413: The three units in the output layer
- Page 414 and 415: StepByStep Method@staticmethoddef _
- Page 416 and 417: The meow() method is totally indepe
- Page 418 and 419: StepByStep Methoddef visualize_filt
- Page 420 and 421: dummy_model = nn.Linear(1, 1)dummy_
- Page 422 and 423: dummy_listOutput[(Linear(in_feature
- Page 424 and 425: Output{Conv2d(1, 1, kernel_size=(3,
- Page 426 and 427: will be the externally defined vari
- Page 428 and 429: Removing Hookssbs_cnn1.remove_hooks
- Page 432 and 433: Figure 5.22 - Feature maps (classif
- Page 434 and 435: classification: The predicted class
- Page 436 and 437: convolutional layers to our model a
- Page 438 and 439: Capturing Outputsfeaturizer_layers
- Page 440 and 441: the filters learned by the model pr
- Page 442 and 443: given chapter are imported at its v
- Page 444 and 445: Data PreparationThe data preparatio
- Page 446 and 447: model anyway. We’ll use it to com
- Page 448 and 449: StepByStep Method@staticmethoddef m
- Page 450 and 451: "What’s wrong with the colors?"Th
- Page 452 and 453: three_channel_filter = np.array([[[
- Page 454 and 455: Fancier Model (Constructor)class CN
- Page 456 and 457: Fancier Model (Classifier)def class
- Page 458 and 459: torch.manual_seed(44)dropping_model
- Page 460 and 461: Outputtensor([0.1000, 0.2000, 0.300
- Page 462 and 463: Figure 6.8 - Output distribution fo
- Page 464 and 465: Adaptive moment estimation (Adam) u
- Page 466 and 467: torch.manual_seed(13)# Model Config
- Page 468 and 469: Outputtorch.Size([5, 3, 3, 3])Its s
- Page 470 and 471: Choosing a learning rate that works
- Page 472 and 473: Higher-Order Learning Rate Function
- Page 474 and 475: Perfect! Now let’s build the actu
- Page 476 and 477: ax.set_xlabel('Learning Rate')ax.se
- Page 478 and 479: LRFinderThe function we’ve implem
return fig
setattr(StepByStep, 'visualize_outputs', visualize_outputs)
Then, let’s use the method above to plot the feature maps for the layers in the
featurizer part of our model:
featurizer_layers = ['conv1', 'relu1', 'maxp1', 'flatten']
with plt.style.context('seaborn-white'):
fig = sbs_cnn1.visualize_outputs(featurizer_layers)
Figure 5.20 - Feature maps (featurizer)
Figure 5.21 - Mini-batch of images (reproduced here for an easier comparison)
Looks cool, right? Even though I’ve plotted the images in the first four rows with
the same size, they have different dimensions, as indicated by the row labels on the
left. The shade of gray is also computed per row: The maximum (white) and
minimum (black) values were computed across the ten images produced by a given
layer; otherwise, some rows would be too dark (the ranges vary a lot from one layer
to the next).
What can we learn from these images? First, convolving the learned filter with the
input image produces some interesting results:
Visualizing Filters and More! | 405