Keras VGGFace 提取特征
Keras VGGFace extracting features
我正在尝试使用 TensorFlow 和 Keras 从 VGGFace 模型的卷积层中提取特征。
这是我的代码:
# Layer Features
layer_name = 'conv1_2' # Edit this line
vgg_model = VGGFace() # Pooling: None, avg or max
out = vgg_model.get_layer(layer_name).output
vgg_model_new = Model(vgg_model.input, out)
def main():
img = image.load_img('myimage.jpg', target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = utils.preprocess_input(x, version=1)
preds = vgg_model_new.predict(x)
print('Predicted:', utils.decode_predictions(preds))
exit(0)
但是,在 print('Predicted:', utils.decode_predictions(preds))
行出现以下错误:
Message=decode_predictions
expects a batch of predictions (i.e. a
2D array of shape (samples, 2622)) for V1 or (samples, 8631) for
V2.Found array with shape: (1, 224, 224, 64)
我只想提取特征,此时不需要对图像进行分类。此代码基于 https://github.com/rcmalli/keras-vggface
您不应该在那里使用 utils.decode_predictions(preds)
,因为它仅用于分类。你可以在这里看到函数的定义 https://github.com/rcmalli/keras-vggface/blob/master/keras_vggface/utils.py#L66
如果要打印特征,请使用print('Predicted:',preds)
我正在尝试使用 TensorFlow 和 Keras 从 VGGFace 模型的卷积层中提取特征。
这是我的代码:
# Layer Features
layer_name = 'conv1_2' # Edit this line
vgg_model = VGGFace() # Pooling: None, avg or max
out = vgg_model.get_layer(layer_name).output
vgg_model_new = Model(vgg_model.input, out)
def main():
img = image.load_img('myimage.jpg', target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = utils.preprocess_input(x, version=1)
preds = vgg_model_new.predict(x)
print('Predicted:', utils.decode_predictions(preds))
exit(0)
但是,在 print('Predicted:', utils.decode_predictions(preds))
行出现以下错误:
Message=
decode_predictions
expects a batch of predictions (i.e. a 2D array of shape (samples, 2622)) for V1 or (samples, 8631) for V2.Found array with shape: (1, 224, 224, 64)
我只想提取特征,此时不需要对图像进行分类。此代码基于 https://github.com/rcmalli/keras-vggface
您不应该在那里使用 utils.decode_predictions(preds)
,因为它仅用于分类。你可以在这里看到函数的定义 https://github.com/rcmalli/keras-vggface/blob/master/keras_vggface/utils.py#L66
如果要打印特征,请使用print('Predicted:',preds)