类型错误:get_config()缺少1个必需的位置参数:保存Keras模型时为“self”

u0sqgete  于 2023-01-05  发布在  其他
关注(0)|答案(1)|浏览(163)

我尝试使用Tensorflow=2.11.0保存VGG16模型以进行迁移学习,但一直收到以下错误消息:
第一个月
下面是我用于获取、修改和保存基本VGG16模型的代码

class BaseModelPreparation:
    def __init__(self,config:BaseModelPreparationConfig) -> None:
        self.config = config

    #get the base model
    def get_base_model(self):
        self.model = tf.keras.applications.vgg16.VGG16(
            #they are part of parameters
            input_shape=self.config.params_image_size,
            weights=self.config.params_weights,
            include_top=self.config.params_include_top
            )
        """
                include_top means whether to include the ANN part of the model or not
        """

        base_model_path = self.config.base_model_path
        self.save_model(path=base_model_path,model = self.model)

    @staticmethod
    def _prepare_full_model(model,classes,freeze_all,freeze_till,learning_rate):
        """
        model: the model which we are using
        classes: output classes(Cat,Dog)
        freeze_all : Freezes weights of all the layers in CNN part.
        freeze_till: freeze weights uptill the layer given in the CNN part.
        learning_rate
        """

        #CNN part
        if (freeze_all):
            for layer in model.layers:
                model.trainable = False
        elif (freeze_till is not None) and (freeze_till > 0):
            for layer in model.layers[:-freeze_till]:
                model.trainable = False

        # Ann part
    
        flattern_in = tf.keras.layers.Flatten()(model.output) #passing the model output though a flattern layer method
        prediction = tf.keras.layers.Dense(
            units=classes,
            activation="softmax"
        )(flattern_in)  #passing the flattern output though a dense layer
        #the above approch is known as fuctional approch

        full_model = tf.keras.models.Model(
            inputs = model.input,
            outputs = prediction
        )

        full_model.compile(
            optimizer=tf.keras.optimizers.SGD(learning_rate=learning_rate),
            loss=tf.keras.losses.CategoricalCrossentropy,
            metrics=["accuracy"]
        )

        full_model.summary()
        return full_model

    #update the base model
    def update_base_model(self):
        self.full_model = self._prepare_full_model(
            model = self.model,
            classes = self.config.params_classes,
            freeze_all = True,
            freeze_till = None,
            learning_rate = self.config.params_learning_rate
        )

        self.save_model(path = self.config.updated_base_model_path,model = self.full_model)

    @staticmethod
    def save_model(path:Path , model :tf.keras.Model):
        model.save(path)

调用类

base_model_prepare = BaseModelPreparation(config)
base_model_prepare.get_base_model()
base_model_prepare.update_base_model()

已检查path变量是否为有效文件路径,并且model对象是否为tf.keras.Model类的有效示例。
我不知道是什么原因导致这个错误或如何修复它。任何帮助将不胜感激。

brc7rcf0

brc7rcf01#

错误的是损失是一个函数,所以它应该是

loss=tf.keras.losses.CategoricalCrossentropy(),

相关问题