From febe44e7019f7ee47cba56f32ed4eb62f891e927 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Tue, 29 Mar 2022 14:24:35 +0300 Subject: [PATCH 01/15] swapped task_name with tags list --- src/huggingface_hub/keras_mixin.py | 24 +++++++++++++----------- tests/test_keras_integration.py | 2 +- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index 09c22635e3..f8f15d1d7f 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -100,7 +100,7 @@ def _create_model_card( model, repo_dir: Path, plot_model: Optional[bool] = True, - task_name: Optional[str] = None, + tags: Optional[list] = None, ): """ Creates a model card for the repository. @@ -110,8 +110,10 @@ def _create_model_card( _plot_network(model, repo_dir) readme_path = f"{repo_dir}/README.md" model_card = "---\n" - if task_name is not None: - model_card += f"tags:\n- {task_name}\n" + if tags is not None: + model_card += "tags:\n" + for tag in tags: + model_card += f"- {tag}\n" model_card += "library_name: keras\n---\n" model_card += "\n## Model description\n\nMore information needed\n" model_card += "\n## Intended uses & limitations\n\nMore information needed\n" @@ -149,7 +151,7 @@ def save_pretrained_keras( config: Optional[Dict[str, Any]] = None, include_optimizer: Optional[bool] = False, plot_model: Optional[bool] = True, - task_name: Optional[str] = None, + tags: Optional[list] = None, **model_save_kwargs, ): """Saves a Keras model to save_directory in SavedModel format. Use this if you're using the Functional or Sequential APIs. @@ -162,8 +164,8 @@ def save_pretrained_keras( Configuration object to be saved alongside the model weights. include_optimizer(:obj:`bool`, `optional`): Whether or not to include optimizer in serialization. - task_name (:obj:`str`, `optional`): - Name of the task the model was trained on. See the available tasks at https://github.com/huggingface/huggingface_hub/blob/main/js/src/lib/interfaces/Types.ts. + tags (:obj:`list`, `optional`): + List of tags that are related to model. See example tags at https://github.com/huggingface/hub-docs/blame/main/modelcard.md. plot_model (:obj:`bool`): Setting this to `True` will plot the model and put it in the model card. Requires graphviz and pydot to be installed. model_save_kwargs(:obj:`dict`, `optional`): @@ -191,7 +193,7 @@ def save_pretrained_keras( with open(path, "w") as f: json.dump(config, f) - _create_model_card(model, save_directory, plot_model, task_name) + _create_model_card(model, save_directory, plot_model, tags) tf.keras.models.save_model( model, save_directory, include_optimizer=include_optimizer, **model_save_kwargs ) @@ -215,7 +217,7 @@ def push_to_hub_keras( git_email: Optional[str] = None, config: Optional[dict] = None, include_optimizer: Optional[bool] = False, - task_name: Optional[str] = None, + tags: Optional[list] = None, plot_model: Optional[bool] = True, **model_save_kwargs, ): @@ -258,8 +260,8 @@ def push_to_hub_keras( Configuration object to be saved alongside the model weights. include_optimizer (:obj:`bool`, `optional`): Whether or not to include optimizer during serialization. - task_name (:obj:`str`, `optional`): - Name of the task the model was trained on. See the available tasks at https://github.com/huggingface/huggingface_hub/blob/main/js/src/lib/interfaces/Types.ts. + tags (:obj:`dict`, `optional`): + List of tags that are related to model. See example tags at https://github.com/huggingface/hub-docs/blame/main/modelcard.md. plot_model (:obj:`bool`): Setting this to `True` will plot the model and put it in the model card. Requires graphviz and pydot to be installed. model_save_kwargs(:obj:`dict`, `optional`): @@ -317,7 +319,7 @@ def push_to_hub_keras( config=config, include_optimizer=include_optimizer, plot_model=plot_model, - task_name=task_name, + tags=tags, **model_save_kwargs, ) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index 3b4aec780a..80e67a1c34 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -300,7 +300,7 @@ def test_abs_path_from_pretrained(self): f"{WORKING_REPO_DIR}/{REPO_NAME}", config={"num": 10, "act": "gelu_fast"}, plot_model=True, - task_name=None, + tags=None, ) new_model = from_pretrained_keras(f"{WORKING_REPO_DIR}/{REPO_NAME}") From 2886373388fd2c504583828948f8e816ff92016a Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Tue, 29 Mar 2022 16:55:44 +0300 Subject: [PATCH 02/15] added metadata creation with pyyaml --- src/huggingface_hub/keras_mixin.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index f8f15d1d7f..6b5c9803e3 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -4,6 +4,7 @@ from shutil import copytree, rmtree from typing import Any, Dict, Optional, Union +import yaml from huggingface_hub import ModelHubMixin from huggingface_hub.file_download import ( is_graphviz_available, @@ -109,12 +110,13 @@ def _create_model_card( if plot_model and is_graphviz_available() and is_pydot_available(): _plot_network(model, repo_dir) readme_path = f"{repo_dir}/README.md" + metadata = {} + metadata["tags"] = tags + metadata["library_name"] = "keras" + doc = yaml.dump(metadata, default_flow_style=False) model_card = "---\n" - if tags is not None: - model_card += "tags:\n" - for tag in tags: - model_card += f"- {tag}\n" - model_card += "library_name: keras\n---\n" + model_card += doc + model_card += "---\n" model_card += "\n## Model description\n\nMore information needed\n" model_card += "\n## Intended uses & limitations\n\nMore information needed\n" model_card += "\n## Training and evaluation data\n\nMore information needed\n" From f81fed565fca6929f416b631b88b0edcccd809c7 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Tue, 29 Mar 2022 17:03:00 +0300 Subject: [PATCH 03/15] minor nit --- src/huggingface_hub/keras_mixin.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index 6b5c9803e3..8a9c573fb9 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -113,9 +113,8 @@ def _create_model_card( metadata = {} metadata["tags"] = tags metadata["library_name"] = "keras" - doc = yaml.dump(metadata, default_flow_style=False) model_card = "---\n" - model_card += doc + model_card += yaml.dump(metadata, default_flow_style=False) model_card += "---\n" model_card += "\n## Model description\n\nMore information needed\n" model_card += "\n## Intended uses & limitations\n\nMore information needed\n" From a426a9d8fd1f90d7d461066676b32f68ab2c6a38 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Thu, 31 Mar 2022 14:24:08 +0300 Subject: [PATCH 04/15] added task_name back, made tags work with strings --- src/huggingface_hub/keras_mixin.py | 36 +++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index 8a9c573fb9..54f0281af5 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -1,5 +1,6 @@ import json import os +import warnings from pathlib import Path from shutil import copytree, rmtree from typing import Any, Dict, Optional, Union @@ -101,7 +102,8 @@ def _create_model_card( model, repo_dir: Path, plot_model: Optional[bool] = True, - tags: Optional[list] = None, + tags: Optional[Union[list, str]] = None, + task_name: Optional[str] = None, ): """ Creates a model card for the repository. @@ -111,7 +113,12 @@ def _create_model_card( _plot_network(model, repo_dir) readme_path = f"{repo_dir}/README.md" metadata = {} - metadata["tags"] = tags + if isinstance(tags, list): + metadata["tags"] = tags + elif isinstance(tags, str): + metadata["tags"] = [tags] + if task_name is not None: + metadata["tags"].append(task_name) metadata["library_name"] = "keras" model_card = "---\n" model_card += yaml.dump(metadata, default_flow_style=False) @@ -152,7 +159,8 @@ def save_pretrained_keras( config: Optional[Dict[str, Any]] = None, include_optimizer: Optional[bool] = False, plot_model: Optional[bool] = True, - tags: Optional[list] = None, + tags: Optional[Union[list, str]] = None, + task_name: Optional[str] = None, **model_save_kwargs, ): """Saves a Keras model to save_directory in SavedModel format. Use this if you're using the Functional or Sequential APIs. @@ -165,8 +173,10 @@ def save_pretrained_keras( Configuration object to be saved alongside the model weights. include_optimizer(:obj:`bool`, `optional`): Whether or not to include optimizer in serialization. - tags (:obj:`list`, `optional`): - List of tags that are related to model. See example tags at https://github.com/huggingface/hub-docs/blame/main/modelcard.md. + tags (:obj:`dict`, `optional`): + List of tags that are related to model or string of a single tag. See example tags at https://github.com/huggingface/hub-docs/blame/main/modelcard.md. + task_name (:obj:`str`, `optional`): + Name of the task the model was trained on. See the available tasks at https://github.com/huggingface/huggingface_hub/blob/main/js/src/lib/interfaces/Types.ts. plot_model (:obj:`bool`): Setting this to `True` will plot the model and put it in the model card. Requires graphviz and pydot to be installed. model_save_kwargs(:obj:`dict`, `optional`): @@ -184,6 +194,12 @@ def save_pretrained_keras( os.makedirs(save_directory, exist_ok=True) + if task_name: + warnings.warn( + "`task_name` input argument is deprecated and " + "will be removed in v0.7. Pass `tags` instead.", + FutureWarning, + ) # saving config if config: if not isinstance(config, dict): @@ -194,7 +210,7 @@ def save_pretrained_keras( with open(path, "w") as f: json.dump(config, f) - _create_model_card(model, save_directory, plot_model, tags) + _create_model_card(model, save_directory, plot_model, tags, task_name) tf.keras.models.save_model( model, save_directory, include_optimizer=include_optimizer, **model_save_kwargs ) @@ -218,7 +234,8 @@ def push_to_hub_keras( git_email: Optional[str] = None, config: Optional[dict] = None, include_optimizer: Optional[bool] = False, - tags: Optional[list] = None, + tags: Optional[Union[list, str]] = None, + task_name: Optional[str] = None, plot_model: Optional[bool] = True, **model_save_kwargs, ): @@ -262,7 +279,9 @@ def push_to_hub_keras( include_optimizer (:obj:`bool`, `optional`): Whether or not to include optimizer during serialization. tags (:obj:`dict`, `optional`): - List of tags that are related to model. See example tags at https://github.com/huggingface/hub-docs/blame/main/modelcard.md. + List of tags that are related to model or string of a single tag. See example tags at https://github.com/huggingface/hub-docs/blame/main/modelcard.md. + task_name (:obj:`str`, `optional`): + Name of the task the model was trained on. See the available tasks at https://github.com/huggingface/huggingface_hub/blob/main/js/src/lib/interfaces/Types.ts. plot_model (:obj:`bool`): Setting this to `True` will plot the model and put it in the model card. Requires graphviz and pydot to be installed. model_save_kwargs(:obj:`dict`, `optional`): @@ -321,6 +340,7 @@ def push_to_hub_keras( include_optimizer=include_optimizer, plot_model=plot_model, tags=tags, + task_name=task_name, **model_save_kwargs, ) From 6f15fad350a085d101023aa392eade6ba873afc7 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Thu, 31 Mar 2022 14:59:32 +0300 Subject: [PATCH 05/15] fixed docstring for style --- src/huggingface_hub/keras_mixin.py | 138 ++++++++++++++++------------- 1 file changed, 78 insertions(+), 60 deletions(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index 54f0281af5..f3e90d34c5 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -163,24 +163,32 @@ def save_pretrained_keras( task_name: Optional[str] = None, **model_save_kwargs, ): - """Saves a Keras model to save_directory in SavedModel format. Use this if you're using the Functional or Sequential APIs. - - model: - The Keras model you'd like to save. The model must be compiled and built. - save_directory (:obj:`str`): - Specify directory in which you want to save the Keras model. - config (:obj:`dict`, `optional`): - Configuration object to be saved alongside the model weights. - include_optimizer(:obj:`bool`, `optional`): - Whether or not to include optimizer in serialization. - tags (:obj:`dict`, `optional`): - List of tags that are related to model or string of a single tag. See example tags at https://github.com/huggingface/hub-docs/blame/main/modelcard.md. - task_name (:obj:`str`, `optional`): - Name of the task the model was trained on. See the available tasks at https://github.com/huggingface/huggingface_hub/blob/main/js/src/lib/interfaces/Types.ts. - plot_model (:obj:`bool`): - Setting this to `True` will plot the model and put it in the model card. Requires graphviz and pydot to be installed. - model_save_kwargs(:obj:`dict`, `optional`): - model_save_kwargs will be passed to tf.keras.models.save_model(). + """ + Saves a Keras model to save_directory in SavedModel format. Use this if + you're using the Functional or Sequential APIs. + Args: + model (`Keras.Model`): + The [Keras + model](https://www.tensorflow.org/api_docs/python/tf/keras/Model) + you'd like to save. The model must be compiled and built. + save_directory (`str`): + Specify directory in which you want to save the Keras model. + config (`dict`, *optional*): + Configuration object to be saved alongside the model weights. + include_optimizer(`bool`, *optional*, defaults to `False`): + Whether or not to include optimizer in serialization. + tags (`dict`, *optional*): + List of tags that are related to model or string of a single tag. See example tags + [here](https://github.com/huggingface/hub-docs/blame/main/modelcard.md). + task_name (`str`, *optional*): + Name of the task the model was trained on. Available tasks + [here](https://github.com/huggingface/hub-docs/blob/main/js/src/lib/interfaces/Types.ts). + plot_model (`bool`, *optional*, defaults to `True`): + Setting this to `True` will plot the model and put it in the model + card. Requires graphviz and pydot to be installed. + model_save_kwargs(`dict`, *optional*): + model_save_kwargs will be passed to + [`tf.keras.models.save_model()`](https://www.tensorflow.org/api_docs/python/tf/keras/models/save_model). """ if is_tf_available(): import tensorflow as tf @@ -240,52 +248,62 @@ def push_to_hub_keras( **model_save_kwargs, ): """ - Upload model checkpoint or tokenizer files to the 🤗 Model Hub while synchronizing a local clone of the repo in - :obj:`repo_path_or_name`. - + Upload model checkpoint or tokenizer files to the Hub while synchronizing a + local clone of the repo in `repo_path_or_name`. Parameters: - model: - The Keras model you'd like to push to the hub. The model must be compiled and built. - repo_path_or_name (:obj:`str`, `optional`): - Can either be a repository name for your model or tokenizer in the Hub or a path to a local folder (in - which case the repository will have the name of that local folder). If not specified, will default to - the name given by :obj:`repo_url` and a local directory with that name will be created. - repo_url (:obj:`str`, `optional`): - Specify this in case you want to push to an existing repository in the hub. If unspecified, a new - repository will be created in your namespace (unless you specify an :obj:`organization`) with - :obj:`repo_name`. - log_dir (:obj:`str`, `optional`): - TensorBoard logging directory to be pushed. The Hub automatically hosts - and displays a TensorBoard instance if log files are included in the repository. - commit_message (:obj:`str`, `optional`): - Message to commit while pushing. Will default to :obj:`"add model"`. - organization (:obj:`str`, `optional`): - Organization in which you want to push your model or tokenizer (you must be a member of this - organization). - private (:obj:`bool`, `optional`): - Whether or not the repository created should be private. - api_endpoint (:obj:`str`, `optional`): + model (`Keras.Model`): + The [Keras + model](`https://www.tensorflow.org/api_docs/python/tf/keras/Model`) + you'd like to push to the Hub. The model must be compiled and built. + repo_path_or_name (`str`, *optional*): + Can either be a repository name for your model or tokenizer in the + Hub or a path to a local folder (in which case the repository will + have the name of that local folder). If not specified, will default + to the name given by `repo_url` and a local directory with that name + will be created. + repo_url (`str`, *optional*): + Specify this in case you want to push to an existing repository in + the Hub. If unspecified, a new repository will be created in your + namespace (unless you specify an `organization`) with `repo_name`. + log_dir (`str`, *optional*): + TensorBoard logging directory to be pushed. The Hub automatically + hosts and displays a TensorBoard instance if log files are included + in the repository. + commit_message (`str`, *optional*, defaults to "Add message"): + Message to commit while pushing. + organization (`str`, *optional*): + Organization in which you want to push your model or tokenizer (you + must be a member of this organization). + private (`bool`, *optional*): + Whether the repository created should be private. + api_endpoint (`str`, *optional*): The API endpoint to use when pushing the model to the hub. - use_auth_token (:obj:`bool` or :obj:`str`, `optional`): - The token to use as HTTP bearer authorization for remote files. If :obj:`True`, will use the token - generated when running :obj:`transformers-cli login` (stored in :obj:`~/.huggingface`). Will default to - :obj:`True`. - git_user (``str``, `optional`): - will override the ``git config user.name`` for committing and pushing files to the hub. - git_email (``str``, `optional`): - will override the ``git config user.email`` for committing and pushing files to the hub. - config (:obj:`dict`, `optional`): + use_auth_token (`bool` or `str`, *optional*, defaults to `True`): + The token to use as HTTP bearer authorization for remote files. If + `True`, will use the token generated when running `transformers-cli + login` (stored in `~/.huggingface`). Will default to `True`. + git_user (`str`, *optional*): + will override the `git config user.name` for committing and pushing + files to the Hub. + git_email (`str`, *optional*): + will override the `git config user.email` for committing and pushing + files to the Hub. + config (`dict`, *optional*): Configuration object to be saved alongside the model weights. - include_optimizer (:obj:`bool`, `optional`): + include_optimizer (`bool`, *optional*, defaults to `False`): Whether or not to include optimizer during serialization. - tags (:obj:`dict`, `optional`): - List of tags that are related to model or string of a single tag. See example tags at https://github.com/huggingface/hub-docs/blame/main/modelcard.md. - task_name (:obj:`str`, `optional`): - Name of the task the model was trained on. See the available tasks at https://github.com/huggingface/huggingface_hub/blob/main/js/src/lib/interfaces/Types.ts. - plot_model (:obj:`bool`): - Setting this to `True` will plot the model and put it in the model card. Requires graphviz and pydot to be installed. - model_save_kwargs(:obj:`dict`, `optional`): - model_save_kwargs will be passed to tf.keras.models.save_model(). + tags (`dict`, *optional*): + List of tags that are related to model or string of a single tag. See example tags + [here](https://github.com/huggingface/hub-docs/blame/main/modelcard.md). + task_name (`str`, *optional*): + Name of the task the model was trained on. Available tasks + [here](https://github.com/huggingface/huggingface_hub/blob/main/js/src/lib/interfaces/Types.ts). + plot_model (`bool`, *optional*, defaults to `True`): + Setting this to `True` will plot the model and put it in the model + card. Requires graphviz and pydot to be installed. + model_save_kwargs(`dict`, *optional*): + model_save_kwargs will be passed to + [`tf.keras.models.save_model()`](https://www.tensorflow.org/api_docs/python/tf/keras/models/save_model). Returns: The url of the commit of your model in the given repository. From 27e867ae8d822971256e46879ef4473b7fbedc74 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Thu, 31 Mar 2022 16:18:07 +0300 Subject: [PATCH 06/15] nits and added test --- src/huggingface_hub/keras_mixin.py | 31 ++++++++++++++++-------------- tests/test_keras_integration.py | 12 ++++++++++++ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index d72f7bf389..461c9faaf5 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -102,8 +102,7 @@ def _create_model_card( model, repo_dir: Path, plot_model: Optional[bool] = True, - tags: Optional[Union[list, str]] = None, - task_name: Optional[str] = None, + metadata: Optional[dict] = None, ): """ Creates a model card for the repository. @@ -112,13 +111,6 @@ def _create_model_card( if plot_model and is_graphviz_available() and is_pydot_available(): _plot_network(model, repo_dir) readme_path = f"{repo_dir}/README.md" - metadata = {} - if isinstance(tags, list): - metadata["tags"] = tags - elif isinstance(tags, str): - metadata["tags"] = [tags] - if task_name is not None: - metadata["tags"].append(task_name) metadata["library_name"] = "keras" model_card = "---\n" model_card += yaml.dump(metadata, default_flow_style=False) @@ -166,6 +158,7 @@ def save_pretrained_keras( """ Saves a Keras model to save_directory in SavedModel format. Use this if you're using the Functional or Sequential APIs. + Args: model (`Keras.Model`): The [Keras @@ -174,9 +167,12 @@ def save_pretrained_keras( save_directory (`str`): Specify directory in which you want to save the Keras model. config (`dict`, *optional*): - Configuration object to be saved alongside the model weights. + Configuration object to be saved alongside the model weights. include_optimizer(`bool`, *optional*, defaults to `False`): Whether or not to include optimizer in serialization. + plot_model (`bool`, *optional*, defaults to `True`): + Setting this to `True` will plot the model and put it in the model + card. Requires graphviz and pydot to be installed. tags (`dict`, *optional*): List of tags that are related to model or string of a single tag. See example tags [here](https://github.com/huggingface/hub-docs/blame/main/modelcard.md). @@ -184,9 +180,6 @@ def save_pretrained_keras( Name of the task the model was trained on. Available tasks [here](https://github.com/huggingface/hub-docs/blob/main/js/src/lib/interfaces/Types.ts). This is deprecated in favor of `tags` and will be removed in v0.7. - plot_model (`bool`, *optional*, defaults to `True`): - Setting this to `True` will plot the model and put it in the model - card. Requires graphviz and pydot to be installed. model_save_kwargs(`dict`, *optional*): model_save_kwargs will be passed to [`tf.keras.models.save_model()`](https://www.tensorflow.org/api_docs/python/tf/keras/models/save_model). @@ -219,7 +212,17 @@ def save_pretrained_keras( with open(path, "w") as f: json.dump(config, f) - _create_model_card(model, save_directory, plot_model, tags, task_name) + metadata = {} + if isinstance(tags, list): + metadata["tags"] = tags + elif isinstance(tags, str): + metadata["tags"] = [tags] + if task_name is not None and "tags" in metadata: + metadata["tags"].append(task_name) + elif task_name is not None and metadata == {}: + metadata["tags"] = [task_name] + + _create_model_card(model, save_directory, plot_model, metadata) tf.keras.models.save_model( model, save_directory, include_optimizer=include_optimizer, **model_save_kwargs ) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index 80e67a1c34..89afcb61d4 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -272,6 +272,18 @@ def test_from_pretrained_weights(self): .item() ) + def test_save_pretrained_task_name_deprecation(self): + REPO_NAME = repo_name("FROM_PRETRAINED") + model = self.model_init() + model.build((None, 2)) + + with pytest.warns( + FutureWarning, match="`task_name` input argument is deprecated*" + ): + save_pretrained_keras( + model, f"{WORKING_REPO_DIR}/{REPO_NAME}", task_name="test" + ) + def test_rel_path_from_pretrained(self): model = self.model_init() model.build((None, 2)) From 51bb1e1a4ba91eeae57e92cf6a541644bc524cee Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Thu, 31 Mar 2022 16:20:01 +0300 Subject: [PATCH 07/15] nit - fixed repo name of the test --- tests/test_keras_integration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index 89afcb61d4..c38e8b5fa0 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -273,7 +273,7 @@ def test_from_pretrained_weights(self): ) def test_save_pretrained_task_name_deprecation(self): - REPO_NAME = repo_name("FROM_PRETRAINED") + REPO_NAME = repo_name("save") model = self.model_init() model.build((None, 2)) From 90c08a5f3abc1dc4c24a4b84873440315a7e0d85 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Thu, 31 Mar 2022 16:20:57 +0300 Subject: [PATCH 08/15] nit - make style --- src/huggingface_hub/keras_mixin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index 461c9faaf5..7dac46896c 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -158,7 +158,7 @@ def save_pretrained_keras( """ Saves a Keras model to save_directory in SavedModel format. Use this if you're using the Functional or Sequential APIs. - + Args: model (`Keras.Model`): The [Keras From d1fba63339a29d6b42c41561c6f3144b0771d981 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Thu, 14 Apr 2022 15:12:56 +0300 Subject: [PATCH 09/15] added test and warning in case task_name is passed --- src/huggingface_hub/keras_mixin.py | 30 +++++++++--------------------- tests/test_keras_integration.py | 9 +++++++-- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index 7dac46896c..7a78502e55 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -152,7 +152,6 @@ def save_pretrained_keras( include_optimizer: Optional[bool] = False, plot_model: Optional[bool] = True, tags: Optional[Union[list, str]] = None, - task_name: Optional[str] = None, **model_save_kwargs, ): """ @@ -176,10 +175,6 @@ def save_pretrained_keras( tags (`dict`, *optional*): List of tags that are related to model or string of a single tag. See example tags [here](https://github.com/huggingface/hub-docs/blame/main/modelcard.md). - task_name (`str`, *optional*): - Name of the task the model was trained on. Available tasks - [here](https://github.com/huggingface/hub-docs/blob/main/js/src/lib/interfaces/Types.ts). - This is deprecated in favor of `tags` and will be removed in v0.7. model_save_kwargs(`dict`, *optional*): model_save_kwargs will be passed to [`tf.keras.models.save_model()`](https://www.tensorflow.org/api_docs/python/tf/keras/models/save_model). @@ -196,12 +191,12 @@ def save_pretrained_keras( os.makedirs(save_directory, exist_ok=True) - if task_name: + if "task_name" in model_save_kwargs: warnings.warn( - "`task_name` input argument is deprecated and " - "will be removed in v0.7. Pass `tags` instead.", + "`task_name` input argument is removed. Pass `tags` instead.", FutureWarning, ) + task_name = model_save_kwargs.pop("task_name") # saving config if config: if not isinstance(config, dict): @@ -217,10 +212,8 @@ def save_pretrained_keras( metadata["tags"] = tags elif isinstance(tags, str): metadata["tags"] = [tags] - if task_name is not None and "tags" in metadata: - metadata["tags"].append(task_name) - elif task_name is not None and metadata == {}: - metadata["tags"] = [task_name] + if "task_name" in locals(): + metadata["task_name"] = task_name _create_model_card(model, save_directory, plot_model, metadata) tf.keras.models.save_model( @@ -301,9 +294,8 @@ def push_to_hub_keras( git_email: Optional[str] = None, config: Optional[dict] = None, include_optimizer: Optional[bool] = False, - tags: Optional[Union[list, str]] = None, - task_name: Optional[str] = None, plot_model: Optional[bool] = True, + tags: Optional[Union[list, str]] = None, **model_save_kwargs, ): """ @@ -352,15 +344,12 @@ def push_to_hub_keras( Configuration object to be saved alongside the model weights. include_optimizer (`bool`, *optional*, defaults to `False`): Whether or not to include optimizer during serialization. - tags (`dict`, *optional*): - List of tags that are related to model or string of a single tag. See example tags - [here](https://github.com/huggingface/hub-docs/blame/main/modelcard.md). - task_name (`str`, *optional*): - Name of the task the model was trained on. Available tasks - [here](https://github.com/huggingface/huggingface_hub/blob/main/js/src/lib/interfaces/Types.ts). plot_model (`bool`, *optional*, defaults to `True`): Setting this to `True` will plot the model and put it in the model card. Requires graphviz and pydot to be installed. + tags (`dict`, *optional*): + List of tags that are related to model or string of a single tag. See example tags + [here](https://github.com/huggingface/hub-docs/blame/main/modelcard.md). model_save_kwargs(`dict`, *optional*): model_save_kwargs will be passed to [`tf.keras.models.save_model()`](https://www.tensorflow.org/api_docs/python/tf/keras/models/save_model). @@ -418,7 +407,6 @@ def push_to_hub_keras( include_optimizer=include_optimizer, plot_model=plot_model, tags=tags, - task_name=task_name, **model_save_kwargs, ) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index c38e8b5fa0..e9b18a5039 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -278,10 +278,15 @@ def test_save_pretrained_task_name_deprecation(self): model.build((None, 2)) with pytest.warns( - FutureWarning, match="`task_name` input argument is deprecated*" + FutureWarning, + match="`task_name` input argument is removed. Pass `tags` instead.", ): save_pretrained_keras( - model, f"{WORKING_REPO_DIR}/{REPO_NAME}", task_name="test" + model, + f"{WORKING_REPO_DIR}/{REPO_NAME}", + tags=["test"], + task_name="test", + save_traces=True, ) def test_rel_path_from_pretrained(self): From 620260862915e1e9e89874a33a354f345db017e6 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Thu, 14 Apr 2022 16:28:50 +0300 Subject: [PATCH 10/15] minor fix --- src/huggingface_hub/keras_mixin.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index 7a78502e55..4e932009de 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -213,7 +213,10 @@ def save_pretrained_keras( elif isinstance(tags, str): metadata["tags"] = [tags] if "task_name" in locals(): - metadata["task_name"] = task_name + if "tags" in metadata: + metadata["tags"].append(task_name) + else: + metadata["tags"] = [task_name] _create_model_card(model, save_directory, plot_model, metadata) tf.keras.models.save_model( From ba4d44b3ce40f3ecf7d6b640fe9f5ec23103f604 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Wed, 20 Apr 2022 14:35:07 +0300 Subject: [PATCH 11/15] changed type in docstring of tags, and position for backwards compatibility --- src/huggingface_hub/keras_mixin.py | 14 +++++++------- tests/test_keras_integration.py | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index 4e932009de..f3445270d3 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -150,8 +150,8 @@ def save_pretrained_keras( save_directory: str, config: Optional[Dict[str, Any]] = None, include_optimizer: Optional[bool] = False, - plot_model: Optional[bool] = True, tags: Optional[Union[list, str]] = None, + plot_model: Optional[bool] = True, **model_save_kwargs, ): """ @@ -172,7 +172,7 @@ def save_pretrained_keras( plot_model (`bool`, *optional*, defaults to `True`): Setting this to `True` will plot the model and put it in the model card. Requires graphviz and pydot to be installed. - tags (`dict`, *optional*): + tags (`list`, *optional*): List of tags that are related to model or string of a single tag. See example tags [here](https://github.com/huggingface/hub-docs/blame/main/modelcard.md). model_save_kwargs(`dict`, *optional*): @@ -297,8 +297,8 @@ def push_to_hub_keras( git_email: Optional[str] = None, config: Optional[dict] = None, include_optimizer: Optional[bool] = False, - plot_model: Optional[bool] = True, tags: Optional[Union[list, str]] = None, + plot_model: Optional[bool] = True, **model_save_kwargs, ): """ @@ -347,12 +347,12 @@ def push_to_hub_keras( Configuration object to be saved alongside the model weights. include_optimizer (`bool`, *optional*, defaults to `False`): Whether or not to include optimizer during serialization. - plot_model (`bool`, *optional*, defaults to `True`): - Setting this to `True` will plot the model and put it in the model - card. Requires graphviz and pydot to be installed. tags (`dict`, *optional*): List of tags that are related to model or string of a single tag. See example tags [here](https://github.com/huggingface/hub-docs/blame/main/modelcard.md). + plot_model (`bool`, *optional*, defaults to `True`): + Setting this to `True` will plot the model and put it in the model + card. Requires graphviz and pydot to be installed. model_save_kwargs(`dict`, *optional*): model_save_kwargs will be passed to [`tf.keras.models.save_model()`](https://www.tensorflow.org/api_docs/python/tf/keras/models/save_model). @@ -408,8 +408,8 @@ def push_to_hub_keras( repo_path_or_name, config=config, include_optimizer=include_optimizer, - plot_model=plot_model, tags=tags, + plot_model=plot_model, **model_save_kwargs, ) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index e9b18a5039..2e1084c5ec 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -316,8 +316,8 @@ def test_abs_path_from_pretrained(self): model, f"{WORKING_REPO_DIR}/{REPO_NAME}", config={"num": 10, "act": "gelu_fast"}, - plot_model=True, tags=None, + plot_model=True, ) new_model = from_pretrained_keras(f"{WORKING_REPO_DIR}/{REPO_NAME}") From 92372ea6181ea1e098aa1d78980c1dc4548cd24b Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Wed, 20 Apr 2022 18:52:58 +0300 Subject: [PATCH 12/15] added unadded changes in my previous commit, docstrings --- src/huggingface_hub/keras_mixin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index f3445270d3..a009eed8a0 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -172,7 +172,7 @@ def save_pretrained_keras( plot_model (`bool`, *optional*, defaults to `True`): Setting this to `True` will plot the model and put it in the model card. Requires graphviz and pydot to be installed. - tags (`list`, *optional*): + tags (Union[`str`,`list`], *optional*): List of tags that are related to model or string of a single tag. See example tags [here](https://github.com/huggingface/hub-docs/blame/main/modelcard.md). model_save_kwargs(`dict`, *optional*): From 765950bdf3406bad5956ba09aa67baf207e561fa Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Wed, 20 Apr 2022 19:45:44 +0300 Subject: [PATCH 13/15] changed assignment of task_name and additional nits --- src/huggingface_hub/keras_mixin.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index a009eed8a0..a1d922b2f3 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -169,12 +169,12 @@ def save_pretrained_keras( Configuration object to be saved alongside the model weights. include_optimizer(`bool`, *optional*, defaults to `False`): Whether or not to include optimizer in serialization. - plot_model (`bool`, *optional*, defaults to `True`): - Setting this to `True` will plot the model and put it in the model - card. Requires graphviz and pydot to be installed. tags (Union[`str`,`list`], *optional*): List of tags that are related to model or string of a single tag. See example tags [here](https://github.com/huggingface/hub-docs/blame/main/modelcard.md). + plot_model (`bool`, *optional*, defaults to `True`): + Setting this to `True` will plot the model and put it in the model + card. Requires graphviz and pydot to be installed. model_save_kwargs(`dict`, *optional*): model_save_kwargs will be passed to [`tf.keras.models.save_model()`](https://www.tensorflow.org/api_docs/python/tf/keras/models/save_model). @@ -191,12 +191,6 @@ def save_pretrained_keras( os.makedirs(save_directory, exist_ok=True) - if "task_name" in model_save_kwargs: - warnings.warn( - "`task_name` input argument is removed. Pass `tags` instead.", - FutureWarning, - ) - task_name = model_save_kwargs.pop("task_name") # saving config if config: if not isinstance(config, dict): @@ -212,7 +206,13 @@ def save_pretrained_keras( metadata["tags"] = tags elif isinstance(tags, str): metadata["tags"] = [tags] - if "task_name" in locals(): + + task_name = model_save_kwargs.pop("task_name", None) + if task_name is not None: + warnings.warn( + "`task_name` input argument is removed. Pass `tags` instead.", + FutureWarning, + ) if "tags" in metadata: metadata["tags"].append(task_name) else: @@ -347,7 +347,7 @@ def push_to_hub_keras( Configuration object to be saved alongside the model weights. include_optimizer (`bool`, *optional*, defaults to `False`): Whether or not to include optimizer during serialization. - tags (`dict`, *optional*): + tags (Union[`list`, `str`], *optional*): List of tags that are related to model or string of a single tag. See example tags [here](https://github.com/huggingface/hub-docs/blame/main/modelcard.md). plot_model (`bool`, *optional*, defaults to `True`): From 36bf2a5a74831842109910c9b365afede5f0e2d2 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Thu, 21 Apr 2022 16:35:27 +0300 Subject: [PATCH 14/15] fixed save_pretrained_keras argument order for compatibility --- src/huggingface_hub/keras_mixin.py | 8 ++++---- tests/test_keras_integration.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index a1d922b2f3..b327ac146b 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -150,8 +150,8 @@ def save_pretrained_keras( save_directory: str, config: Optional[Dict[str, Any]] = None, include_optimizer: Optional[bool] = False, - tags: Optional[Union[list, str]] = None, plot_model: Optional[bool] = True, + tags: Optional[Union[list, str]] = None, **model_save_kwargs, ): """ @@ -169,12 +169,12 @@ def save_pretrained_keras( Configuration object to be saved alongside the model weights. include_optimizer(`bool`, *optional*, defaults to `False`): Whether or not to include optimizer in serialization. - tags (Union[`str`,`list`], *optional*): - List of tags that are related to model or string of a single tag. See example tags - [here](https://github.com/huggingface/hub-docs/blame/main/modelcard.md). plot_model (`bool`, *optional*, defaults to `True`): Setting this to `True` will plot the model and put it in the model card. Requires graphviz and pydot to be installed. + tags (Union[`str`,`list`], *optional*): + List of tags that are related to model or string of a single tag. See example tags + [here](https://github.com/huggingface/hub-docs/blame/main/modelcard.md). model_save_kwargs(`dict`, *optional*): model_save_kwargs will be passed to [`tf.keras.models.save_model()`](https://www.tensorflow.org/api_docs/python/tf/keras/models/save_model). diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index 2e1084c5ec..e9b18a5039 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -316,8 +316,8 @@ def test_abs_path_from_pretrained(self): model, f"{WORKING_REPO_DIR}/{REPO_NAME}", config={"num": 10, "act": "gelu_fast"}, - tags=None, plot_model=True, + tags=None, ) new_model = from_pretrained_keras(f"{WORKING_REPO_DIR}/{REPO_NAME}") From 9387c055601687f5c41faa94c86dc60cf1629506 Mon Sep 17 00:00:00 2001 From: Merve Noyan Date: Fri, 22 Apr 2022 15:49:45 +0300 Subject: [PATCH 15/15] Update src/huggingface_hub/keras_mixin.py Co-authored-by: Omar Sanseviero --- src/huggingface_hub/keras_mixin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index b327ac146b..6f95fc5e33 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -210,7 +210,7 @@ def save_pretrained_keras( task_name = model_save_kwargs.pop("task_name", None) if task_name is not None: warnings.warn( - "`task_name` input argument is removed. Pass `tags` instead.", + "`task_name` input argument is deprecated. Pass `tags` instead.", FutureWarning, ) if "tags" in metadata: