Extraction and modification of intermediate layer outputs from pre-trained models and layer by layer inference

Hi everyone, I would like to know if it is possible to obtain and modify the outputs of several layers (features extraction), modify them and use them as new inputs for the following layers, continuing then with the inference.

The purpose is to inject faults into some features and check the behaviour of several pre-trained models analyzing their accuracy to some datasets like ImageNet .

It occurs to me a possible solution is to make some kind of layer by layer inference if it is possible, but I am not sure if this is the proper way. Any help would be appreciated.

Thanks!

Hi @Izan_C_G, To make inference using a single layer you can extract the layer from the model using

model=keras.applications.ResNet50(
    include_top=True,
    weights="imagenet",
    input_tensor=None,
    input_shape=None,
    pooling=None,
    classes=1000,
    classifier_activation="softmax",
)

layer=model.layers[12]

and you can pass the input to the layer to make inference

data = tf.random.normal(shape=(1, 224, 224,3))
layer(data)

Thank You.