<p>Your methodology is correct : the problem is that the first layers of a big model like ResNet50 or VGG16 are really big. Connecting a fully connected (dense) layer to a big output (like 112x112x64) leads to a very heavy weights.</p>
<p>There is some strategy against that problem described in the paper <a href="https://arxiv.org/abs/1610.01644" rel="nofollow noreferrer">Understanding intermediate layers using linear classifier probes</a></p>
<blockquote>
<p><strong>Practical concern : Dimension reduction on features</strong></p>
<p>Another practical problem can arise when certain layers of a neural network have an exceedingly large quantity of features. The first few layers of Inception v3,  for example,  have a few million features when we multiply height, width and channels.  This leads to parameters for a single probe taking upwards of a few gigabytes of storage, which is disproportionately large when we consider that the entire set of model parameters takes less space than that.In those cases, we have three possible suggestions for trimming down the space of features on which we fit the probes.</p>
<ul>
<li><strong>Use only a random subset of the features (but always the same ones).</strong>  This is used on the Inception v3 model in Section 4.2.</li>
<li>Project the features to a lower-dimensional space.  Learn this mapping.  This is probably a worse idea than it sounds because the projection matrix itself can take a lot of storage (even more than the probe parameters).</li>
<li>When dealing with features in the form of images (height, width, channels), <strong>we can perform2D pooling along the (height, width) of each channel. This reduces the number of features to the number of channels.</strong> This is used on the ResNet-50 model in Section 4.1</li>
</ul>
</blockquote>
Tensorflow - Getting Probes of Pretrained models
<p>I need to get the probes of a pretained model in TensorFlow (dataset imagenet), that is for each block of a VGG16, or ResNet50 or any other pertained model in TensorFlow, I want to have a prediction of the class <code>y_hat</code>, so an array of zeros but for the predicted class which will be 1.</p>
<p>I have written the following code to get the outputs from each block (found in another StackOverflow question):</p>
<pre><code>IMG_SHAPE = (224, 224, 3)
model = tf.keras.applications.VGG16(input_shape = IMG_SHAPE,
                                    include_top=False,
                                    weights='imagenet')

# Download the weights file and then:

model = tf.keras.applications.VGG16(input_shape = IMG_SHAPE,
                                    include_top=False,
                                    weights=None)
pretrain_model_path = &quot;/content/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5&quot;
model.load_weights(pretrain_model_path)

# Get blocks output:

probe1 = model.get_layer('block1_pool').output
probe2 = model.get_layer('block2_pool').output
probe3 = model.get_layer('block3_pool').output
probe4 = model.get_layer('block4_pool').output
probe5 = model.get_layer('block5_pool').output

probe_1 = tf.keras.models.Model(inputs=model.input, outputs=probe1)
probe_2 = tf.keras.models.Model(inputs=model.input, outputs=probe2)
probe_3 = tf.keras.models.Model(inputs=model.input, outputs=probe3)
probe_4 = tf.keras.models.Model(inputs=model.input, outputs=probe4)
probe_5 = tf.keras.models.Model(inputs=model.input, outputs=probe5)
</code></pre>
<p>Now, I'm stuck, because I am trying to flat and dense the output of the block to get the prediction using softmax, but it gives me thousands of errors:</p>
<pre><code>inputs = model.input
lay = model.get_layer('block1_pool').output
x = tf.keras.layers.Flatten()(lay)
outputs = tf.keras.layers.Dense(1000, activation='softmax')(x)
model_1 = tf.keras.Model(inputs=inputs, outputs=outputs)

---------------------------------------------------------------------------
ResourceExhaustedError                    Traceback (most recent call last)
&lt;ipython-input-88-22a2004a95b6&gt; in &lt;module&gt;()
      2 lay = model.get_layer('block1_pool').output
      3 x = tf.keras.layers.Flatten()(lay)
----&gt; 4 outputs = tf.keras.layers.Dense(1000, activation='softmax')(x)
      5 model_1 = tf.keras.Model(inputs=inputs, outputs=outputs)

21 frames
/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)

ResourceExhaustedError: OOM when allocating tensor with shape[802816,1000] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc [Op:RandomUniform]

</code></pre>
<p>But nothing seems to work. Does anybody know how I can get 'final' predictions from each block?</p>