Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
StepByStep Methoddef visualize_filters(self, layer_name, **kwargs):try:# Gets the layer object from the modellayer = self.modelfor name in layer_name.split('.'):layer = getattr(layer, name)# We are only looking at filters for 2D convolutionsif isinstance(layer, nn.Conv2d):# Takes the weight informationweights = layer.weight.data.cpu().numpy()# weights -> (channels_out (filter), channels_in, H, W)n_filters, n_channels, _, _ = weights.shape# Builds a figuresize = (2 * n_channels + 2, 2 * n_filters)fig, axes = plt.subplots(n_filters, n_channels,figsize=size)axes = np.atleast_2d(axes)axes = axes.reshape(n_filters, n_channels)# For each channel_out (filter)for i in range(n_filters):StepByStep._visualize_tensors(axes[i, :],1weights[i],2layer_name=f'Filter #{i}',title='Channel')for ax in axes.flat:ax.label_outer()fig.tight_layout()return figexcept AttributeError:returnsetattr(StepByStep, 'visualize_filters', visualize_filters)1 The i-th row of subplots corresponds to a particular filter; each row has as manycolumns as there are input channels.Visualizing Filters and More! | 393
2 The i-th element of the weights corresponds to the i-th filter, which learneddifferent weights to convolve each of the input channels.OK, let’s see what the filter looks like:fig = sbs_cnn1.visualize_filters('conv1', cmap='gray')Figure 5.19 - Our model’s only filterIs this a filter one could come up with to try distinguishing between the differentclasses we have? Maybe, but it is not easy to grasp, just by looking at this filter, whatit is effectively accomplishing.To really understand the effect this filter has on each image, we need to visualizethe intermediate values produced by our model, namely, the output of each andevery layer!"How can we visualize the output of each layer? Do we have tomodify our StepByStep class to capture those?"It is much easier than that: We can use hooks!HooksA hook is simply a way to force a model to execute a function either after itsforward pass or after its backward pass. Hence, there are forward hooks andbackward hooks. We’re using only forward hooks here, but the idea is the same forboth.First, we create a function that is going to be, guess what, hooked to the forwardpass. Let’s illustrate the process with a dummy model:394 | Chapter 5: Convolutions
- Page 368 and 369: the more dimensions, the more separ
- Page 370 and 371: import randomimport numpy as npfrom
- Page 372 and 373: identity = np.array([[[[0, 0, 0],[0
- Page 374 and 375: Figure 5.4 - Striding the image, on
- Page 376 and 377: Output-----------------------------
- Page 378 and 379: Outputtensor([[[[9., 5., 0., 7.],[0
- 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 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 430 and 431: return figsetattr(StepByStep, 'visu
- 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
StepByStep Method
def visualize_filters(self, layer_name, **kwargs):
try:
# Gets the layer object from the model
layer = self.model
for name in layer_name.split('.'):
layer = getattr(layer, name)
# We are only looking at filters for 2D convolutions
if isinstance(layer, nn.Conv2d):
# Takes the weight information
weights = layer.weight.data.cpu().numpy()
# weights -> (channels_out (filter), channels_in, H, W)
n_filters, n_channels, _, _ = weights.shape
# Builds a figure
size = (2 * n_channels + 2, 2 * n_filters)
fig, axes = plt.subplots(n_filters, n_channels,
figsize=size)
axes = np.atleast_2d(axes)
axes = axes.reshape(n_filters, n_channels)
# For each channel_out (filter)
for i in range(n_filters):
StepByStep._visualize_tensors(
axes[i, :],
1
weights[i],
2
layer_name=f'Filter #{i}',
title='Channel'
)
for ax in axes.flat:
ax.label_outer()
fig.tight_layout()
return fig
except AttributeError:
return
setattr(StepByStep, 'visualize_filters', visualize_filters)
1 The i-th row of subplots corresponds to a particular filter; each row has as many
columns as there are input channels.
Visualizing Filters and More! | 393