首页 > 解决方案 > 如何使用python模型模型来提取特征?

问题描述

我训练了一个进行对象分类的 python 模型。然后,我想用这个模型作为特征提取器,但我不知道层的全名。代码是

base_model = load_model('models/deepfake-detection-model.h5')
base_model.summary()

for layer in base_model.layers:
    print(layer.name)


#extract = Model(model.inputs, layer_n) # Dense(128,...)

data= cv2.imread('dataset/real/eudeqjhdfd_4.png')

#features = extract.predict(data)
for l in base_model.layers:
    print (l.output_shape)

layer_n=inception_resnet_v2
model = Model(inputs=base_model.input, outputs=base_model.get_layer(layer_n).output)

我使用模型摘要函数来获取模型的图层名称,但输出是

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
inception_resnet_v2 (Model)  (None, None, None, 1536)  54336736  
_________________________________________________________________
global_average_pooling2d (Gl (None, 1536)              0         
_________________________________________________________________
dense (Dense)                (None, 2)                 3074      
=================================================================
Total params: 54,339,810
Trainable params: 54,279,266
Non-trainable params: 60,544
_________________________________________________________________

inception_resnet_v2
global_average_pooling2d
denseb

(None, None, None, 1536)
(None, 1536)
(None, 2)

Traceback (most recent call last):
 File "D:\Hamdy\Project of Gradition\code\python\hh.py", line 26, in <module>
   layer_n=inception_resnet_v2
NameError: name 'inception_resnet_v2' is not defined 

谁能帮助我告诉我如何知道模型层的名称并将其用作特征提取技术?

标签: pythonmachine-learningmodelclassificationlayer

解决方案


推荐阅读