简体   繁体   English

Dlib特征数组作为CNN和预测的输入

[英]Dlib feature arrays as input for CNN and prediction

I am attempting to create a face recognition application by using CNN and dlib feature extractor. 我正在尝试使用CNN和dlib特征提取器来创建人脸识别应用程序。 What I want to do is to extract the features from a bunch of photos of the same person, then send the arrays to my CNN which will produce a 2 class classifier for that person. 我想做的是从同一个人的一堆照片中提取特征,然后将数组发送到我的CNN,这将为该个人生成2类分类器。

How can I change it to accept dlib feature arrays, how would the predict method look like and how should the data be formatted? 如何更改它以接受dlib特征数组,预测方法的外观如何以及数据应如何格式化?

As of now, my network is configured to take images as inputs but I am unsure how to change it to work with feature arrays. 到目前为止,我的网络已配置为将图像作为输入,但是我不确定如何更改它以与要素阵列配合使用。

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

train_datagen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='binary', shuffle=True)

print(train_generator.class_indices)

validation_generator = test_datagen.flow_from_directory(validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='binary', shuffle=True)

print(validation_generator.class_indices)

model.fit_generator(train_generator, shuffle=True, steps_per_epoch=train_samples // batch_size, epochs=epochs, callbacks=[tensorboard], validation_data=validation_generator, validation_steps=validation_samples // batch_size)

model.save('Models/model.h5')

The way I want this to work is to use a program that extracts the features of each face in each photo into a file that my CNN can use to create the yes/no classifier file that later can be used for predictions. 我希望这种方法的工作方式是使用一个程序,将每张照片中每张脸的特征提取到一个文件中,我的CNN可以使用该程序创建是/否分类器文件,该文件以后可以用于预测。

This is a first try that surely needs more engineering. 这是肯定需要更多工程设计的首次尝试。 You can consider the first convolutional layers of a CNN as "feature extraction" layers, the last fully connected layers as "classification" layers. 您可以将CNN的第一个卷积层视为“特征提取”层,将最后完全连接的层视为“分类”层。

import tensorflow as tf
import tensorflow.keras.layers as ll

i1 = ll.Input(input_shape1) #the images
x = ll.Conv2D(32, (3, 3),activation='relu')(i1)
x = ll.MaxPooling2D(pool_size=(2, 2))(x)
x = ll.Conv2D(32, (3, 3),activation='relu')(x)
x = ll.MaxPooling2D(pool_size=(2, 2))(x)
x = ll.Conv2D(64, (3, 3),activation='relu')(x)
x = ll.MaxPooling2D(pool_size=(2, 2))(x)

i2 = ll.Input(input_shape2) #the feature manually extracted

y = ll.Concatenate([x,i2])
y = ll.Flatten()(y)
y = ll.Dense(64,activation='relu')(y)
y = ll.Dropout(0.5)(y)
y = ll.Dense(1, activation='sigmoid')(y)

model = tf.keras.models.Model(inputs = [i1,i2], outputs = y)

Then compile and fit as usual, but you will need a generator to serve [i1,i2] and replace the ImageDataGenerator . 然后像往常一样进行编译和拟合,但是您将需要一个生成器来服务[i1,i2]并替换ImageDataGenerator If you want to use only the features and not the image then the architecture will be simpler: forget the convolutional part and just try a dense net. 如果您只想使用功能而不是图像,那么体系结构会更简单:忘记卷积部分,然后尝试使用密集网络。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM