Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NotImplementedError: Layer ArcFace was created by passing non-serializable argument values in __init__() #33

Open
HoaiAn0906 opened this issue Feb 13, 2023 · 0 comments

Comments

@HoaiAn0906
Copy link

NotImplementedError:
Layer ArcFace was created by passing
non-serializable argument values in __init__(),
and therefore the layer must override get_config() in
order to be serializable. Please implement get_config().

Example:

class CustomLayer(keras.layers.Layer):
def init(self, arg1, arg2, **kwargs):
super().init(**kwargs)
self.arg1 = arg1
self.arg2 = arg2

def get_config(self):
    config = super().get_config()
    config.update({
        "arg1": self.arg1,
        "arg2": self.arg2,
    })
    return config

This is my code:
batch_size=32
train_datagen_names = keras.preprocessing.image.ImageDataGenerator(preprocessing_function=preprocess_input)
train_generator_names = train_datagen_names.flow_from_directory('/content/drive/MyDrive/train_dir',target_size=(224,224),color_mode='rgb',batch_size=32,class_mode='categorical',shuffle=True)
NO_CLASSES = len(train_generator_names.class_indices.values())

generate data

class train_Generator_xandy(object):
def init(self):
datagen = ImageDataGenerator(preprocessing_function=preprocess_input)

    train_generator = datagen.flow_from_directory(
        '/content/drive/MyDrive/train_dir',
        target_size=(224, 224),
        color_mode='rgb',
        batch_size=32,
        class_mode='categorical',
        shuffle=True
    )

    self.gene = train_generator

def __iter__(self):
    return self

def __next__(self):
    X, Y = self.gene.next()
    return [X, Y], Y

class val_Generator_xandy(object):
def init(self):
datagen = ImageDataGenerator(preprocessing_function=preprocess_input)

    validation_generator = datagen.flow_from_directory(
        '/content/drive/MyDrive/validation_dir',
        target_size=(224, 224),
        color_mode='rgb',
        batch_size=32,
        class_mode='categorical',
        shuffle=True
    )

    self.gene = validation_generator

def __iter__(self):
    return self

def __next__(self):
    X, Y = self.gene.next()
    return [X, Y], Y

base_model = VGG16(include_top=False, weights='imagenet', input_shape=(224, 224, 3))
base_model.summary()

x = base_model.output
y = Input(shape=(NO_CLASSES, ))
x = BatchNormalization()(x)
x = Dropout(0.5)(x)
x = Flatten()(x)
x = Dense(128, kernel_initializer='he_normal',kernel_regularizer=keras.regularizers.l2(1e-4))(x)
#x = Dense(128, activation='relu')(x)
#predictions = Dense(num_classes, activation='softmax')(x)
predictions = ArcFace(NO_CLASSES,regularizer=keras.regularizers.l2(1e-4))([x, y])

model = Model(inputs=[base_model.input, y], outputs=predictions)

for layer in base_model.layers:
layer.trainable = False

model.summary()

opt = keras.optimizers.legacy.SGD(learning_rate=0.1, momentum=0.9, decay=5e-4, nesterov=True)

model.compile(loss='categorical_crossentropy',optimizer=opt,metrics=['accuracy'])

train_generator = train_Generator_xandy()
validation_generator = val_Generator_xandy()

model.fit(train_generator,batch_size = 1,verbose = 1,epochs = 20)

model.fit(
train_generator,
steps_per_epoch=batch_size,
epochs=10,
validation_data=validation_generator,
validation_steps=batch_size
)

creates a HDF5 file

model.save('transfer_learning_trained' + '_face_cnn_model.h5')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant