-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathhub_mixin.py
155 lines (124 loc) · 4.7 KB
/
hub_mixin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import torch
import json
from pathlib import Path
from typing import Optional, Union
from functools import wraps
from huggingface_hub import (
PyTorchModelHubMixin,
ModelCard,
ModelCardData,
hf_hub_download,
)
MODEL_CARD = """
---
{{ card_data }}
---
# {{ model_name }} Model Card
Table of Contents:
- [Load trained model](#load-trained-model)
- [Model init parameters](#model-init-parameters)
- [Model metrics](#model-metrics)
- [Dataset](#dataset)
## Load trained model
```python
import segmentation_models_pytorch as smp
model = smp.from_pretrained("<save-directory-or-this-repo>")
```
## Model init parameters
```python
model_init_params = {{ model_parameters }}
```
## Model metrics
{{ metrics | default("[More Information Needed]", true) }}
## Dataset
Dataset name: {{ dataset | default("[More Information Needed]", true) }}
## More Information
- Library: {{ repo_url | default("[More Information Needed]", true) }}
- Docs: {{ docs_url | default("[More Information Needed]", true) }}
This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin)
"""
def _format_parameters(parameters: dict):
params = {k: v for k, v in parameters.items() if not k.startswith("_")}
params = [
f'"{k}": {v}' if not isinstance(v, str) else f'"{k}": "{v}"'
for k, v in params.items()
]
params = ",\n".join([f" {param}" for param in params])
params = "{\n" + f"{params}" + "\n}"
return params
class SMPHubMixin(PyTorchModelHubMixin):
def generate_model_card(self, *args, **kwargs) -> ModelCard:
model_parameters_json = _format_parameters(self.config)
metrics = kwargs.get("metrics", None)
dataset = kwargs.get("dataset", None)
if metrics is not None:
metrics = json.dumps(metrics, indent=4)
metrics = f"```json\n{metrics}\n```"
tags = self._hub_mixin_info.model_card_data.get("tags", []) or []
tags.extend(["segmentation-models-pytorch", "semantic-segmentation", "pytorch"])
model_card_data = ModelCardData(
languages=["python"],
library_name="segmentation-models-pytorch",
license="mit",
tags=tags,
pipeline_tag="image-segmentation",
)
model_card = ModelCard.from_template(
card_data=model_card_data,
template_str=MODEL_CARD,
repo_url="https://github.com/qubvel/segmentation_models.pytorch",
docs_url="https://smp.readthedocs.io/en/latest/",
model_parameters=model_parameters_json,
model_name=self.__class__.__name__,
metrics=metrics,
dataset=dataset,
)
return model_card
@wraps(PyTorchModelHubMixin.save_pretrained)
def save_pretrained(
self, save_directory: Union[str, Path], *args, **kwargs
) -> Optional[str]:
model_card_kwargs = kwargs.pop("model_card_kwargs", {})
if "dataset" in kwargs:
model_card_kwargs["dataset"] = kwargs.pop("dataset")
if "metrics" in kwargs:
model_card_kwargs["metrics"] = kwargs.pop("metrics")
kwargs["model_card_kwargs"] = model_card_kwargs
# set additional attribute to be able to deserialize the model
self.config["_model_class"] = self.__class__.__name__
try:
# call the original save_pretrained
result = super().save_pretrained(save_directory, *args, **kwargs)
finally:
self.config.pop("_model_class", None)
return result
@property
@torch.jit.unused
def config(self) -> dict:
return self._hub_mixin_config
@wraps(PyTorchModelHubMixin.from_pretrained)
def from_pretrained(
pretrained_model_name_or_path: str, *args, strict: bool = True, **kwargs
):
config_path = Path(pretrained_model_name_or_path) / "config.json"
if not config_path.exists():
config_path = hf_hub_download(
pretrained_model_name_or_path,
filename="config.json",
revision=kwargs.get("revision", None),
)
with open(config_path, "r") as f:
config = json.load(f)
model_class_name = config.pop("_model_class")
import segmentation_models_pytorch as smp
model_class = getattr(smp, model_class_name)
return model_class.from_pretrained(
pretrained_model_name_or_path, *args, **kwargs, strict=strict
)
def supports_config_loading(func):
"""Decorator to filter special config kwargs"""
@wraps(func)
def wrapper(self, *args, **kwargs):
kwargs = {k: v for k, v in kwargs.items() if not k.startswith("_")}
return func(self, *args, **kwargs)
return wrapper