Search code examples
kerasdeep-learningconv-neural-networkfeature-extractionresnet

How to extract features from a layer of the pretrained ResNet model Keras


I trained a model with Resnet3D and I want to extract the neurons of a layer. I plan to use them with the SVM classifier. How can I extract these weights and put them to the numpy array?

Load the weights by keras

model = Resnet3DBuilder.build_resnet_18((128, 96, 96, 3), nClass[0])
model.load_weights('drive/app/models/3d_resnet_modelq.hdf5')

extract a layer

dns = model.layers[-1].output

now what should i do?


Solution

  • If you just want to visualise the features, in pure Keras you can define a Model with the desired layer as output:

    from keras.models import Model
    
    model_cut = Model(inputs=model.inputs, output=model.layers[-1].output)
    features = model_cut.predict(x)  # Assuming you have your images in x
    

    Note that in order for this to work, model must have been compiled at least once.