Search code examples
pythonkerasdeep-learningconv-neural-network

how to get the extracted feature vector from transfer learning models python?


I am trying to implement a classification model with ResNet50. I know, CNN or transfer learning models extract the features themselves. How can I get the extracted feature vector from the image dataset in python?

Code Snippet of model training:

base_model = ResNet152V2(
    input_shape = (height, width, 3),
    include_top=False,
    weights='imagenet'
)
from tensorflow.keras.layers import MaxPool2D, BatchNormalization, GlobalAveragePooling2D

top_model = base_model.output
top_model = GlobalAveragePooling2D()(top_model)


top_model = Dense(1072, activation='relu')(top_model)
top_model = Dropout(0.6)(top_model)  
top_model = Dense(256, activation='relu')(top_model)
top_model = Dropout(0.6)(top_model)


prediction = Dense(len(categories), activation='softmax')(top_model)
model = Model(inputs=base_model.input, outputs=prediction)

for layer in model.layers:
  layer_trainable=False
from tensorflow.keras.optimizers import Adam
model.compile(
  optimizer = Adam(learning_rate=0.00001),
  loss='categorical_crossentropy',
  metrics=['accuracy']
)
import keras
from keras.callbacks import EarlyStopping
es_callback = keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)


datagen = ImageDataGenerator(rotation_range= 30,
                             width_shift_range=0.2,
                             height_shift_range=0.2, 
                             shear_range=0.2,
                             zoom_range=0.3,
                             horizontal_flip=True
                             )

resnet152v2 = model.fit(datagen.flow(X_train, y_train),
                        validation_data = (X_val, y_val),
                        epochs = 100,
                        callbacks=[es_callback]
                        )

I followed a solution from here.

Code snippet of extracting features from the trained model:

extract = Model(model.inputs, model.layers[-3].output) #top_model = Dropout(0.6)(top_model)
features = extract.predict(X_test) 

The shape of X_test is (120, 224, 224, 3)


Solution

  • Once you have trained your model, you can save it (or just directly use it in the same file) then cut off the top layer.

    model = load_model("xxx.h5")   #Or use model directly
    
    #Use functional model
    from tensorflow.keras.models import Model
    featuresModel = Model(inputs=model.input,outputs=model.layers[-3].output)   #Get rids of dropout too
    
    #Inference directly
    featureVector = featuresModel.predict(yourBatchData, batch_size=yourBatchLength)
    

    If inferenced in batches, you would need to separate the output results to see the result for each input in the batch.