Tensorflow Keras 模型子类化 — call 函数

huangapple go评论67阅读模式
英文:

Tensorflow Keras Model subclassing -- call function

问题

我正在使用Tensorflow进行自监督学习的实验。我正在运行的示例代码可以在Keras示例网站上找到。这是链接到NNCLR示例。可以在这里找到下载代码的Github链接。虽然我可以运行示例没有问题,但当我尝试使用model.save()保存预训练模型或微调模型时,出现了问题。
我得到的错误消息如下:

ValueError: 无法保存模型<__main__.NNCLR object at 0x7f6bc0f39550>因为输入形状不可用或模型的前向传递没有定义要定义前向传递请覆盖`Model.call()`。要指定输入形状直接调用`build(input_shape)`,或者使用`Model()`、`Model.fit()``Model.predict()`在实际数据上调用模型如果有自定义的训练步骤请确保通过`Model.__call__`在训练步骤中调用前向传递`model(inputs)`,而不是`model.call()`。

我不确定如何覆盖`Model.call()`方法感谢帮助


  [1]: https://keras.io/examples/vision/nnclr/
  [2]: https://github.com/keras-team/keras-io/blob/master/examples/vision/nnclr.py
英文:

I am experimenting with self supervised learning using tensorflow. The example code I'm running can be found in the Keras examples website. This is the link to the NNCLR example. The Github link to download the code can be found here. While I have no issues running the examples, I am running into issues when I try to save the pretrained or the finetuned model using model.save().
The error I'm getting is this:

 f&quot;Model {model} cannot be saved either because the input shape is not &quot;
 ValueError: Model &lt;__main__.NNCLR object at 0x7f6bc0f39550&gt; cannot be saved either 
 because the input shape is not available or because the forward pass of the model is
 not defined. To define a forward pass, please override `Model.call()`. 
 To specify an input shape, either call `build(input_shape)` directly, or call the model on actual data using `Model()`, `Model.fit()`, or `Model.predict()`. 
 If you have a custom training step, please make sure to invoke the forward pass in train step through  
`Model.__call__`, i.e. `model(inputs)`, as opposed to `model.call()`.

I am unsure how to override the Model.call() method. Appreciate some help.

答案1

得分: 1

以下是翻译好的部分:

一种在这种情况下实现模型保存的方法是重写 keras.Model 类中的 save(或 save_weights)方法。在您的情况下,首先在 NNCLR 类中初始化微调模型,然后重写其 save 方法。顺便提一下,通过这种方式,您还可以使用 ModelCheckpoint API。

如下所述,在 NNCLR 模型类中定义微调模型,并重写其 save 方法。

class NNCLR(keras.Model):
    def __init__(...):
        super().__init__()
        ...
        self.finetuning_model = keras.Sequential(
            [
                layers.Input(shape=input_shape),
                self.classification_augmenter,
                self.encoder,
                layers.Dense(10),
            ],
            name="finetuning_model",
        )
    
    ...

    def save(
        self, filepath, overwrite=True, include_optimizer=True, 
        save_format=None, signatures=None, options=None
    ):
        self.finetuning_model.save(
            filepath=filepath,
            overwrite=overwrite,
            save_format=save_format,
            options=options,
            include_optimizer=include_optimizer,
            signatures=signatures
        )

model = NNCLR(...)
model.compile
model.fit

接下来,您可以执行以下操作:

model.save('finetune_model') # 以SavedModel格式保存
finetune_model = tf.keras.models.load_model('finetune_model', compile=False)

# NNCLR 代码示例:评估部分
"A popular way to evaluate a SSL method in computer vision or 
for that fact any other pre-training method as such is to learn 
a linear classifier on the frozen features of the trained backbone 
model and evaluate the classifier on unseen images."

for layer in finetune_model.layers:
    if not isinstance(layer, layers.Dense):
        layer.trainable = False

finetune_model.summary() # OK
finetune_model.compile(
    optimizer=keras.optimizers.Adam(),
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=[keras.metrics.SparseCategoricalAccuracy(name="acc")],
)
finetune_model.fit

请注意,以上内容是提供的翻译,可能与原文有细微差异,但保留了代码的结构和逻辑。

英文:

One way to achieve model saving in such cases is to override the save (or save_weights) method in the keras.Model class. In your case, first initialize the finetune model in the NNCLR class. And next, override the save method for it. FYI, in this way, you may also able to use ModelCheckpoint API.


As said, define the finetune model in the NNCLR model class and override the save method for it.

class NNCLR(keras.Model):
    def __init__(...):
        super().__init__()
        ...
        self.finetuning_model = keras.Sequential(
            [
                layers.Input(shape=input_shape),
                self.classification_augmenter,
                self.encoder,
                layers.Dense(10),
            ],
            name=&quot;finetuning_model&quot;,
        )
    
    ...

    def save(
        self, filepath, overwrite=True, include_optimizer=True, 
        save_format=None, signatures=None, options=None
    ):
        self.finetuning_model.save(
            filepath=filepath,
            overwrite=overwrite,
            save_format=save_format,
            options=options,
            include_optimizer=include_optimizer,
            signatures=signatures
        )

model = NNCLR(...)
model.compile
model.fit

Next, you can do

model.save(&#39;finetune_model&#39;) # SavedModel format
finetune_model = tf.keras.models.load_model(&#39;finetune_model&#39;, compile=False)

&#39;&#39;&#39;
NNCLR code example: Evaluate sections
&quot;A popular way to evaluate a SSL method in computer vision or 
for that fact any other pre-training method as such is to learn 
a linear classifier on the frozen features of the trained backbone 
model and evaluate the classifier on unseen images.&quot;
&#39;&#39;&#39;
for layer in finetune_model.layers:
    if not isinstance(layer, layers.Dense):
        layer.trainable = False

finetune_model.summary() # OK
finetune_model.compile(
    optimizer=keras.optimizers.Adam(),
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=[keras.metrics.SparseCategoricalAccuracy(name=&quot;acc&quot;)],
)
finetune_model.fit

huangapple
  • 本文由 发表于 2023年2月10日 09:44:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定