[英]Writing Keras Model Class
I want to rewrite the code below as a class:我想将下面的代码重写为一个类:
input = Input(shape=(28, 28, 1))
label = Input(shape=(10,))
x = Conv2D(32, kernel_size=(3, 3), activation='relu')(input)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Conv2D(64, kernel_size=(3, 3), activation='relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = BatchNormalization()(x)
x = Dropout(0.5)(x)
x = Flatten()(x)
x = Dense(512, kernel_initializer='he_normal')(x)
x = BatchNormalization()(x)
output = ArcFace(num_classes=10)([x, label])
model = Model([input, label], output)
model.compile(loss='categorical_crossentropy',
optimizer=Adam(),
metrics=['accuracy'])
this is what I have:这就是我所拥有的:
class ArcFace_Model():
def __init__(self, input_shape, num_classes):
self.input_shape = (input_shape,)
self.num_classes = num_classes
self.label_shape = (num_classes,)
def build(self):
#create model
model = Sequential()
#add model layers
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=self.input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(512, kernel_initializer='he_normal'))
model.add(BatchNormalization())
model.add(ArcFace(num_classes=self.num_classes))
# loss and optimizer
optimizer=Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False)
model.compile(loss=categorical_crossentropy,
optimizer=optimizer,
metrics=['accuracy'])
return model
but I have a problem my input_shape is 128 but the code input is (28,28,1).但我有一个问题,我的 input_shape 是 128,但代码输入是 (28,28,1)。 I am doing this because I want to use ArcFace in my model I found a class layer on github .我这样做是因为我想在我的模型中使用 ArcFace 我在github上找到了一个类层。
Is there a way to fix this?有没有办法来解决这个问题?
You can build keras based arcface model within deepface.您可以在 deepface 中构建基于 keras 的 arcface 模型。 I mentioned below supported face recognition models.我在下面提到了支持的人脸识别模型。
#!pip install deepface
from deepface import DeepFace
models = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace', 'DeepID', 'ArcFace']
model = DeepFace.build_model(models[5])
print(model.summary())
You can apply face recognition with arcface as well.您也可以使用 arcface 应用人脸识别。 In this way, you don't have to build the model manually.这样,您不必手动构建模型。 It also handles pre-processing stages of a face recognition pipeline including detection and alignment in the background.它还处理人脸识别管道的预处理阶段,包括后台的检测和对齐。
obj = DeepFace.verify('img1.jpg', 'img2.jpg', model_name = 'ArcFace')
print(obj)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.