Strange feature map; 83 % zeros

MikeTensor: <p>Does somebody know why over 87 % of the output tensor of my feature map is 0?</p>
<p>It happens with all image sizes (224x224, 512x512, 1242x375).<br>
The output tensor of my feature extractor is: [1, 214016] (flatten) – at 1242x375 px images.<br>
But only 26721 values are non-zeros.</p>
<p>I am using vgg16 and I have removed the classifier- and the avgpool-layers.<br>
Any ideas?</p>
<pre><code class="lang-auto">import torchvision.models as models
import torch.nn as nn
device = "cuda"

#Model
model = models.vgg16(weights='IMAGENET1K_V1')
model.classifier = nn.Sequential(*list(model.classifier.children())[:-7])
model.avgpool = nn.Sequential(*list(model.avgpool.children())[:-1])

img_model = img_tensor.unsqueeze(0)
image_device = img_model.to(device)
img_feature = model.to(device)(image_device)
</code></pre>

MikeTensor: <p>This is how the model looks like:</p>
<p>VGG(<br>
(features): Sequential(<br>
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(1): ReLU(inplace=True)<br>
(2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(3): ReLU(inplace=True)<br>
(4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(6): ReLU(inplace=True)<br>
(7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(8): ReLU(inplace=True)<br>
(9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(11): ReLU(inplace=True)<br>
(12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(13): ReLU(inplace=True)<br>
(14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(15): ReLU(inplace=True)<br>
(16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(18): ReLU(inplace=True)<br>
(19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(20): ReLU(inplace=True)<br>
(21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(22): ReLU(inplace=True)<br>
(23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(25): ReLU(inplace=True)<br>
(26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(27): ReLU(inplace=True)<br>
(28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(29): ReLU(inplace=True)<br>
(30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
)<br>
(avgpool): Sequential()<br>
(classifier): Sequential()<br>
)</p>

Jamie_Donnelly: <p>Check the outputs of the layer prior to the final ReLU layer. If most of the values are 0 in the final layers, it’s probably because the values in the previous layer are negative.</p>
<p>If you’re using torchvision models from <code>torchvision.models</code> without pre-trained weights and getting lots of small negative values, I would recommend re-initialising the weights in the network before training.</p>

J_Johnson: <p>That might not be a bad thing. How many non-zero values do you need in order to classify something? 17% non-zeros of 214,016 possible values is over 36k unique features to classify from.</p>
<p>If you had 1,000 classes, you really only need log_2(1,000) possible binary values, which is ≈10.</p>
<p>Granted, in classification problems where each feature set may not set all of your classes orthogonal to all other classes, more possible features are ideal.</p>
<p>You can think of each of those features as describing something contained in the picture. I.e. has feathers, has wheels, has white, has red, etc. Then the classifier matrix(your final layer) just multiplies by a trainable value and adds up each feature into each class probability. I.e. Has a beak, is green, has feathers, shaped like a bird, might classify strongly as a parrot.</p>