tf.keras.callbacks.SidecarEvaluatorModelExport
Stay organized with collections
Save and categorize content based on your preferences.
Callback to save the best Keras model.
Inherits From: ModelCheckpoint
, Callback
tf.keras.callbacks.SidecarEvaluatorModelExport(
export_filepath, checkpoint_filepath, **kwargs
)
It expands the functionality of the existing ModelCheckpoint callback to
enable exporting the best models after evaluation with validation dataset.
When using the SidecarEvaluatorModelExport
callback in conjunction with
keras.utils.SidecarEvaluator
, users should provide the filepath
, which
is the path for this callback to export model or save weights to, and
ckpt_filepath
, which is where the checkpoint is available to extract
the epoch number from. The callback will then export the model that the
evaluator deems as the best (among the checkpoints saved by the training
counterpart) to the specified filepath
. This callback is intended to be
used by SidecarEvaluator only.
Example:
model.compile(loss=..., optimizer=...,
metrics=['accuracy'])
sidecar_evaluator = keras.utils.SidecarEvaluator(
model=model,
data=dataset,
checkpoint_dir=checkpoint_dir,
max_evaluations=1,
callbacks=[
SidecarEvaluatorModelExport(
export_filepath=os.path.join(checkpoint_dir,
'best_model_eval',
'best-model-{epoch:04d}'),
checkpoint_filepath=os.path.join(checkpoint_dir,
'ckpt-{epoch:04d}'),
save_freq="eval",
save_weights_only=True,
monitor="loss",
mode="min",
verbose=1,
),
],
)
sidecar_evaluator.start()
# Model weights are saved if evaluator deems it's the best seen so far.
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2"><h2 class="add-link">Args</h2></th></tr>
<tr>
<td>
`export_filepath`<a id="export_filepath"></a>
</td>
<td>
Path where best models should be saved by this
`SidecarEvaluatorModelExport` callback. Epoch formatting options, such
as `os.path.join(best_model_dir, 'best-model-{epoch:04d}')`, can be
used to allow saved model to preserve epoch information in the file
name. SidecarEvaluatorModelExport will use the "training epoch" at
which the checkpoint was saved by training to fill the epoch
placeholder in the path.
</td>
</tr><tr>
<td>
`checkpoint_filepath`<a id="checkpoint_filepath"></a>
</td>
<td>
Path where checkpoints were saved by training. This
should be the same as what is provided to `filepath` argument of
`ModelCheckpoint` on the training side, such as
`os.path.join(checkpoint_dir, 'ckpt-{epoch:04d}')`.
</td>
</tr>
</table>
## Methods
<h3 id="set_model"><code>set_model</code></h3>
<a target="_blank" class="external" href="https://github.com/keras-team/keras/tree/v2.14.0/keras/callbacks.py#L694-L695">View source</a>
<pre class="devsite-click-to-copy prettyprint lang-py tfo-signature-link">
<code>set_model(
model
)
</code></pre>
<h3 id="set_params"><code>set_params</code></h3>
<a target="_blank" class="external" href="https://github.com/keras-team/keras/tree/v2.14.0/keras/callbacks.py#L691-L692">View source</a>
<pre class="devsite-click-to-copy prettyprint lang-py tfo-signature-link">
<code>set_params(
params
)
</code></pre>
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[],[],null,["# tf.keras.callbacks.SidecarEvaluatorModelExport\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v2.14.0/keras/utils/sidecar_evaluator.py#L341-L432) |\n\nCallback to save the best Keras model.\n\nInherits From: [`ModelCheckpoint`](../../../tf/keras/callbacks/ModelCheckpoint), [`Callback`](../../../tf/keras/callbacks/Callback) \n\n tf.keras.callbacks.SidecarEvaluatorModelExport(\n export_filepath, checkpoint_filepath, **kwargs\n )\n\nIt expands the functionality of the existing ModelCheckpoint callback to\nenable exporting the best models after evaluation with validation dataset.\n\nWhen using the `SidecarEvaluatorModelExport` callback in conjunction with\n[`keras.utils.SidecarEvaluator`](../../../tf/keras/utils/SidecarEvaluator), users should provide the `filepath`, which\nis the path for this callback to export model or save weights to, and\n`ckpt_filepath`, which is where the checkpoint is available to extract\nthe epoch number from. The callback will then export the model that the\nevaluator deems as the best (among the checkpoints saved by the training\ncounterpart) to the specified `filepath`. This callback is intended to be\nused by SidecarEvaluator only.\n\n#### Example:\n\n model.compile(loss=..., optimizer=...,\n metrics=['accuracy'])\n sidecar_evaluator = keras.utils.SidecarEvaluator(\n model=model,\n data=dataset,\n checkpoint_dir=checkpoint_dir,\n max_evaluations=1,\n callbacks=[\n SidecarEvaluatorModelExport(\n export_filepath=os.path.join(checkpoint_dir,\n 'best_model_eval',\n 'best-model-{epoch:04d}'),\n checkpoint_filepath=os.path.join(checkpoint_dir,\n 'ckpt-{epoch:04d}'),\n save_freq=\"eval\",\n save_weights_only=True,\n monitor=\"loss\",\n mode=\"min\",\n verbose=1,\n ),\n ],\n )\n sidecar_evaluator.start()\n # Model weights are saved if evaluator deems it's the best seen so far.\n\n \u003c!-- Tabular view --\u003e\n \u003ctable class=\"responsive fixed orange\"\u003e\n \u003ccolgroup\u003e\u003ccol width=\"214px\"\u003e\u003ccol\u003e\u003c/colgroup\u003e\n \u003ctr\u003e\u003cth colspan=\"2\"\u003e\u003ch2 class=\"add-link\"\u003eArgs\u003c/h2\u003e\u003c/th\u003e\u003c/tr\u003e\n\n \u003ctr\u003e\n \u003ctd\u003e\n `export_filepath`\u003ca id=\"export_filepath\"\u003e\u003c/a\u003e\n \u003c/td\u003e\n \u003ctd\u003e\n Path where best models should be saved by this\n `SidecarEvaluatorModelExport` callback. Epoch formatting options, such\n as `os.path.join(best_model_dir, 'best-model-{epoch:04d}')`, can be\n used to allow saved model to preserve epoch information in the file\n name. SidecarEvaluatorModelExport will use the \"training epoch\" at\n which the checkpoint was saved by training to fill the epoch\n placeholder in the path.\n \u003c/td\u003e\n \u003c/tr\u003e\u003ctr\u003e\n \u003ctd\u003e\n `checkpoint_filepath`\u003ca id=\"checkpoint_filepath\"\u003e\u003c/a\u003e\n \u003c/td\u003e\n \u003ctd\u003e\n Path where checkpoints were saved by training. This\n should be the same as what is provided to `filepath` argument of\n `ModelCheckpoint` on the training side, such as\n `os.path.join(checkpoint_dir, 'ckpt-{epoch:04d}')`.\n \u003c/td\u003e\n \u003c/tr\u003e\n \u003c/table\u003e\n\n\n\n ## Methods\n\n \u003ch3 id=\"set_model\"\u003e\u003ccode\u003eset_model\u003c/code\u003e\u003c/h3\u003e\n\n \u003ca target=\"_blank\" class=\"external\" href=\"https://github.com/keras-team/keras/tree/v2.14.0/keras/callbacks.py#L694-L695\"\u003eView source\u003c/a\u003e\n\n \u003cpre class=\"devsite-click-to-copy prettyprint lang-py tfo-signature-link\"\u003e\n \u003ccode\u003eset_model(\n model\n )\n \u003c/code\u003e\u003c/pre\u003e\n\n\n\n\n \u003ch3 id=\"set_params\"\u003e\u003ccode\u003eset_params\u003c/code\u003e\u003c/h3\u003e\n\n \u003ca target=\"_blank\" class=\"external\" href=\"https://github.com/keras-team/keras/tree/v2.14.0/keras/callbacks.py#L691-L692\"\u003eView source\u003c/a\u003e\n\n \u003cpre class=\"devsite-click-to-copy prettyprint lang-py tfo-signature-link\"\u003e\n \u003ccode\u003eset_params(\n params\n )\n \u003c/code\u003e\u003c/pre\u003e"]]