The first task is to remove the final layers of the pretrained model to transform it into a feature extractor. As usual, Keras makes this operation quite easy. For Sequential models, the list of layers is accessible through the model.layers attribute. This structure has a pop() method, which removes the last layer of the model. Therefore, if we know the number of final layers we need to remove to transform a network into a specific feature extractor (for instance, two layers for a standard ResNet model), this can be done as follows:
for i in range(num_layers_to_remove):
model.layers.pop()
In pure TensorFlow, editing an operational graph supporting a model is neither simple nor recommended. However, we have to keep in mind that unused graph operations are not executed at runtime. So, still having the old layers present in the compiled graph will not affect the computational performance of the new model, as long as they are not called anymore. Therefore, instead...