Forward hook method for visualising

username: brunoj
cooked: <p>TypeError: Invalid shape (14, 14, 480) for image data got this error<br>
my code is this</p>
<p>from google.colab.patches import cv2_imshow</p>
<p>!pip install efficientnet_pytorch</p>
<p>import cv2</p>
<p>import numpy as np</p>
<h1>
<a name="load-and-resize-the-image-1" class="anchor" href="#load-and-resize-the-image-1"></a>Load and resize the image</h1>
<p>image_path = ‘/content/cat.jpg’</p>
<p>image = cv2.imread(image_path)</p>
<p>image = cv2.resize(image, (224,224)) # Resize to match EfficientNet input size</p>
<h1>
<a name="convert-image-to-float32-and-normalize-2" class="anchor" href="#convert-image-to-float32-and-normalize-2"></a>Convert image to float32 and normalize</h1>
<p>image = image.astype(np.float32) / 255.0</p>
<h1>
<a name="expand-image-dimensions-to-match-model-input-shape-3" class="anchor" href="#expand-image-dimensions-to-match-model-input-shape-3"></a>Expand image dimensions to match model input shape</h1>
<p>image = np.expand_dims(image, axis=0)</p>
<p>import matplotlib.pyplot as plt</p>
<p>import torch</p>
<p>from efficientnet_pytorch import EfficientNet</p>
<h1>
<a name="load-your-pretrained-efficientnet-model-4" class="anchor" href="#load-your-pretrained-efficientnet-model-4"></a>Load your pretrained EfficientNet model</h1>
<p>model = EfficientNet.from_pretrained(‘efficientnet-b0’)</p>
<h1>
<a name="print-the-model-architecture-to-see-the-available-modules-and-their-names-5" class="anchor" href="#print-the-model-architecture-to-see-the-available-modules-and-their-names-5"></a>Print the model architecture to see the available modules and their names</h1>
<p>print(model)</p>
<h1>
<a name="alternatively-you-can-inspect-the-models-_modules-dictionary-6" class="anchor" href="#alternatively-you-can-inspect-the-models-_modules-dictionary-6"></a>Alternatively, you can inspect the model’s _modules dictionary</h1>
<p>print(model._modules)</p>
<p>import matplotlib.pyplot as plt</p>
<p>import torch</p>
<p>from efficientnet_pytorch import EfficientNet</p>
<h1>
<a name="load-your-pretrained-efficientnet-model-7" class="anchor" href="#load-your-pretrained-efficientnet-model-7"></a>Load your pretrained EfficientNet model</h1>
<p>model = EfficientNet.from_pretrained(‘efficientnet-b0’)</p>
<h1>
<a name="define-the-forward-hook-function-to-capture-intermediate-layer-outputs-8" class="anchor" href="#define-the-forward-hook-function-to-capture-intermediate-layer-outputs-8"></a>Define the forward hook function to capture intermediate layer outputs</h1>
<p>intermediate_outputs = []</p>
<p>def hook(module, input, output):</p>
<p>intermediate_outputs.append(output)</p>
<h1>
<a name="register-the-forward-hook-on-the-conv6-layer-9" class="anchor" href="#register-the-forward-hook-on-the-conv6-layer-9"></a>Register the forward hook on the conv6 layer</h1>
<p>model._blocks[6]._depthwise_conv.register_forward_hook(hook)</p>
<h1>
<a name="pass-the-image-through-the-model-to-trigger-the-forward-hook-and-capture-the-intermediate-layer-output-10" class="anchor" href="#pass-the-image-through-the-model-to-trigger-the-forward-hook-and-capture-the-intermediate-layer-output-10"></a>Pass the image through the model to trigger the forward hook and capture the intermediate layer output</h1>
<p>model(image)</p>
<h1>
<a name="visualize-the-captured-intermediate-layer-output-as-a-feature-map-11" class="anchor" href="#visualize-the-captured-intermediate-layer-output-as-a-feature-map-11"></a>Visualize the captured intermediate layer output as a feature map</h1>
<p>feature_map = intermediate_outputs[0][0].detach().numpy()</p>
<h1>
<a name="normalize-the-feature-map-for-visualization-12" class="anchor" href="#normalize-the-feature-map-for-visualization-12"></a>Normalize the feature map for visualization</h1>
<p>feature_map -= feature_map.mean()</p>
<p>feature_map /= feature_map.std()</p>
<p>feature_map *= 64</p>
<p>feature_map += 128</p>
<p>feature_map = np.clip(feature_map, 0, 255).astype(‘uint8’)</p>
<h1>
<a name="display-the-intermediate-layer-output-13" class="anchor" href="#display-the-intermediate-layer-output-13"></a>Display the intermediate layer output</h1>
<p>plt.figure()</p>
<p>plt.title(‘Conv6 Layer Output’)</p>
<p>plt.imshow(feature_map.transpose(1, 2, 0))</p>
<p>plt.axis(‘off’)</p>
<p>plt.show()</p>

username: ptrblck
cooked: <p>The intermediate output in the shape <code>[14, 14, 480]</code> (after permutation) is not a valid image format since it has <code>480</code> channels as the error message explains.<br>
If you want to visualize each channel separately, use a loop and index the channel dimension.</p>