rcmalli / keras-vggface

VGGFace implementation with Keras Framework
MIT License
928 stars 417 forks source link

How to extract feature in fc7 for a test image? #6

Closed PapaMadeleine2022 closed 6 years ago

PapaMadeleine2022 commented 7 years ago

sorry to bother you. I am a fresh hand for face recognition and keras. I want to use your Feature Extractioncode to get the face feature in FC7. But I find the most elements of the FC7 feature are zero. Is this right?
below is my code:

from keras.engine import  Model
from keras.layers import Input
from keras_vggface import VGGFace
import numpy as np
from keras.preprocessing import image

image_input = Input(shape=(224, 224, 3))
# for theano uncomment
# image_input = Input(shape=(3,224, 224))

# Convolution Features
#vgg_model_conv = VGGFace(include_top=False, pooling='avg') # pooling: None, avg or max
#vgg_model_conv = VGGFace(include_top=False) # pooling: None, avg or max

# FC7 Features
vgg_model = VGGFace() # pooling: None, avg or max
out = vgg_model.get_layer('fc7').output
#vgg_model_fc7 = Model(image_input, out)
vgg_model_fc7 = Model(vgg_model.input, out)

# Change the image path with yours.
img = image.load_img('images/chip_2.png', target_size=(224, 224))
print type(img)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
# TF order aka 'channel-last'
x = x[:, :, :, ::-1]
# TH order aka 'channel-first'
# x = x[:, ::-1, :, :]
# Zero-center by mean pixel
x[:, 0, :, :] -= 93.5940
x[:, 1, :, :] -= 104.7624
x[:, 2, :, :] -= 129.1863

vgg_model_fc7_preds = vgg_model_fc7.predict(x)
print vgg_model_fc7_preds[0]
rcmalli commented 7 years ago

The reason of this situation caused by the implementation of the fully connected (Dense) layers. Normally, Dense layers are defined as follows:

x = Dense(4096, activation='relu', name='fc6')(x)
x = Dense(4096, activation='relu', name='fc7')(x)

These definitions make the output of each layer also calculated by 'Relu' operation which sets the negative values to zero. I have updated the source code and separated the activation functions as layers. This solution is not perfect but it should allow us to obtain Dense layer outputs easily. You may update the library and use your code without any change.

Note: Any solution for getting the output of certain Tensor variable from the model is still welcomed.

oxydron commented 7 years ago

@rcmalli I think that your solution is the best solution.

PapaMadeleine2022 commented 7 years ago

@rcmalli thank you for your explanation.