【发布时间】:2020-06-02 01:17:51
【问题描述】:
我正在尝试使用具有 vggface 权重的 keras 和 resnet50 模型构建人脸验证系统。我试图实现这一点的方法是通过以下步骤:
- 给定两张图片,我首先使用 mtcnn 作为嵌入找出人脸
- 然后我计算两个向量嵌入之间的余弦距离。距离从0开始到1.....(这里要注意 相同的两张脸的距离越小)
使用 resnet50 的预训练模型,我得到了相当不错的结果。但是由于该模型主要是根据欧洲数据进行训练的,并且我希望在印度次大陆上进行人脸验证,因此我不能依赖它。我想在我自己的数据集上训练它们。我有 10000 个类,每个类包含 2 个图像。通过图像增强,我可以从这两个图像中为每个类创建 10-15 个图像。
这是我用于训练的示例代码
base_model = VGGFace(model='resnet50',include_top=False,input_shape=(224, 224, 3))
base_model.layers.pop()
base_model.summary()
for layer in base_model.layers:
layer.trainable = False
y=base_model.input
x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.
x=Dense(1024,activation='relu')(x) #dense layer 2
x=Dense(512,activation='relu')(x) #dense layer 3
preds=Dense(8322,activation='softmax')(x) #final layer with softmax activation
model=Model(inputs=base_model.input,outputs=preds)
model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])
model.summary()
train_datagen=ImageDataGenerator(preprocessing_function=preprocess_input) #included in our dependencies
train_generator=train_datagen.flow_from_directory('/Users/imac/Desktop/Fayed/Facematching/testenv/facenet/Dataset/train', # this is where you specify the path to the main data folder
target_size=(224,224),
color_mode='rgb',
batch_size=32,
class_mode='categorical',
shuffle=True)
step_size_train=train_generator.n/train_generator.batch_size
model.fit_generator(generator=train_generator,
steps_per_epoch=step_size_train,
epochs=10)
model.save('directory')
就代码而言,我的理解是我禁用最后一层,然后添加 4 层训练它们并将它们存储在一个字典中。
然后我使用
加载模型model=load_model('directory of my saved model')
model.summary()
yhat = model.predict(samples)
我预测两个图像的嵌入,然后计算余弦距离。但问题是,我训练有素的模型会使预测变得更糟。对于同一个人的两个图像,预训练模型给出的距离为 0.3,而我训练的模型显示距离为 1.0。尽管在训练过程中损失函数随着每个时期而减少并且准确度在增加,但这并没有反映在我的预测输出上。我想增加预训练模型的预测结果。
如何使用我自己的数据实现这一目标?
注意:我在机器学习方面相对较新,对模型层了解不多
【问题讨论】:
标签: python tensorflow machine-learning keras deep-learning