Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
keras-team
GitHub Repository: keras-team/keras-io
Path: blob/master/guides/keras_hub/upload.py
8371 views
1
"""
2
Title: Uploading Models with KerasHub
3
Author: [Samaneh Saadat](https://github.com/SamanehSaadat/), [Matthew Watson](https://github.com/mattdangerw/)
4
Date created: 2024/04/29
5
Last modified: 2024/04/29
6
Description: An introduction on how to upload a fine-tuned KerasHub model to model hubs.
7
Accelerator: GPU
8
"""
9
10
"""
11
# Introduction
12
13
Fine-tuning a machine learning model can yield impressive results for specific tasks.
14
Uploading your fine-tuned model to a model hub allows you to share it with the broader community.
15
By sharing your models, you'll enhance accessibility for other researchers and developers,
16
making your contributions an integral part of the machine learning landscape.
17
This can also streamline the integration of your model into real-world applications.
18
19
This guide walks you through how to upload your fine-tuned models to popular model hubs such as
20
[Kaggle Models](https://www.kaggle.com/models) and [Hugging Face Hub](https://huggingface.co/models).
21
"""
22
23
"""
24
# Setup
25
26
Let's start by installing and importing all the libraries we need. We use KerasHub for this guide.
27
"""
28
29
"""shell
30
pip install -q --upgrade keras-hub huggingface-hub kagglehub
31
"""
32
33
import os
34
35
os.environ["KERAS_BACKEND"] = "jax"
36
37
import keras_hub
38
39
"""
40
# Data
41
42
We can use the IMDB reviews dataset for this guide. Let's load the dataset from `tensorflow_dataset`.
43
"""
44
45
import tensorflow_datasets as tfds
46
47
imdb_train, imdb_test = tfds.load(
48
"imdb_reviews",
49
split=["train", "test"],
50
as_supervised=True,
51
batch_size=4,
52
)
53
54
"""
55
We only use a small subset of the training samples to make the guide run faster.
56
However, if you need a higher quality model, consider using a larger number of training samples.
57
"""
58
59
imdb_train = imdb_train.take(100)
60
61
"""
62
# Task Upload
63
64
A `keras_hub.models.Task`, wraps a `keras_hub.models.Backbone` and a `keras_hub.models.Preprocessor` to create
65
a model that can be directly used for training, fine-tuning, and prediction for a given text problem.
66
In this section, we explain how to create a `Task`, fine-tune and upload it to a model hub.
67
"""
68
69
"""
70
## Load Model
71
72
If you want to build a Causal LM based on a base model, simply call `keras_hub.models.CausalLM.from_preset`
73
and pass a built-in preset identifier.
74
"""
75
76
causal_lm = keras_hub.models.CausalLM.from_preset("gpt2_base_en")
77
78
79
"""
80
## Fine-tune Model
81
82
After loading the model, you can call `.fit()` on the model to fine-tune it.
83
Here, we fine-tune the model on the IMDB reviews which makes the model movie domain-specific.
84
"""
85
86
# Drop labels and keep the review text only for the Causal LM.
87
imdb_train_reviews = imdb_train.map(lambda x, y: x)
88
89
# Fine-tune the Causal LM.
90
causal_lm.fit(imdb_train_reviews)
91
92
"""
93
## Save the Model Locally
94
95
To upload a model, you need to first save the model locally using `save_to_preset`.
96
"""
97
98
preset_dir = "./gpt2_imdb"
99
causal_lm.save_to_preset(preset_dir)
100
101
"""
102
Let's see the saved files.
103
"""
104
105
os.listdir(preset_dir)
106
107
"""
108
### Load a Locally Saved Model
109
110
A model that is saved to a local preset can be loaded using `from_preset`.
111
What you save in, is what you get back out.
112
"""
113
114
causal_lm = keras_hub.models.CausalLM.from_preset(preset_dir)
115
116
"""
117
You can also load the `keras_hub.models.Backbone` and `keras_hub.models.Tokenizer` objects from this preset directory.
118
Note that these objects are equivalent to `causal_lm.backbone` and `causal_lm.preprocessor.tokenizer` above.
119
"""
120
121
backbone = keras_hub.models.Backbone.from_preset(preset_dir)
122
tokenizer = keras_hub.models.Tokenizer.from_preset(preset_dir)
123
124
"""
125
## Upload the Model to a Model Hub
126
127
After saving a preset to a directory, this directory can be uploaded to a model hub such as Kaggle or Hugging Face directly from the KerasHub library.
128
To upload the model to Kaggle, the URI must start with `kaggle://` and to upload to Hugging Face, it should start with `hf://`.
129
"""
130
"""
131
### Upload to Kaggle
132
"""
133
134
"""
135
To upload a model to Kaggle, first, we need to authenticate with Kaggle.
136
This can in one of the following ways:
137
1. Set environment variables `KAGGLE_USERNAME` and `KAGGLE_KEY`.
138
2. Provide a local `~/.kaggle/kaggle.json`.
139
3. Call `kagglehub.login()`.
140
141
Let's make sure we are logged in before continuing.
142
"""
143
144
import kagglehub
145
146
if "KAGGLE_USERNAME" not in os.environ or "KAGGLE_KEY" not in os.environ:
147
kagglehub.login()
148
149
150
"""
151
152
To upload a model we can use `keras_hub.upload_preset(uri, preset_dir)` API where `uri` has the format of
153
`kaggle://<KAGGLE_USERNAME>/<MODEL>/Keras/<VARIATION>` for uploading to Kaggle and `preset_dir` is the directory that the model is saved in.
154
155
Running the following uploads the model that is saved in `preset_dir` to Kaggle:
156
"""
157
kaggle_username = kagglehub.whoami()["username"]
158
kaggle_uri = f"kaggle://{kaggle_username}/gpt2/keras/gpt2_imdb"
159
keras_hub.upload_preset(kaggle_uri, preset_dir)
160
161
"""
162
### Upload to Hugging Face
163
"""
164
165
"""
166
To upload a model to Hugging Face, first, we need to authenticate with Hugging Face.
167
This can in one of the following ways:
168
1. Set environment variables `HF_USERNAME` and `HF_TOKEN`.
169
2. Call `huggingface_hub.notebook_login()`.
170
171
Let's make sure we are logged in before coninuing.
172
"""
173
174
import huggingface_hub
175
176
if "HF_USERNAME" not in os.environ or "HF_TOKEN" not in os.environ:
177
huggingface_hub.notebook_login()
178
179
"""
180
181
`keras_hub.upload_preset(uri, preset_dir)` can be used to upload a model to Hugging Face if `uri` has the format of
182
`kaggle://<HF_USERNAME>/<MODEL>`.
183
184
Running the following uploads the model that is saved in `preset_dir` to Hugging Face:
185
"""
186
187
hf_username = huggingface_hub.whoami()["name"]
188
hf_uri = f"hf://{hf_username}/gpt2_imdb"
189
keras_hub.upload_preset(hf_uri, preset_dir)
190
191
192
"""
193
## Load a User Uploaded Model
194
195
After verifying that the model is uploaded to Kaggle, we can load the model by calling `from_preset`.
196
197
```python
198
causal_lm = keras_hub.models.CausalLM.from_preset(
199
f"kaggle://{kaggle_username}/gpt2/keras/gpt2_imdb"
200
)
201
```
202
203
We can also load the model uploaded to Hugging Face by calling `from_preset`.
204
205
```python
206
causal_lm = keras_hub.models.CausalLM.from_preset(f"hf://{hf_username}/gpt2_imdb")
207
```
208
"""
209
210
211
"""
212
# Classifier Upload
213
214
Uploading a classifier model is similar to Causal LM upload.
215
To upload the fine-tuned model, first, the model should be saved to a local directory using `save_to_preset`
216
API and then it can be uploaded via `keras_hub.upload_preset`.
217
"""
218
219
# Load the base model.
220
classifier = keras_hub.models.Classifier.from_preset(
221
"bert_tiny_en_uncased", num_classes=2
222
)
223
224
# Fine-tune the classifier.
225
classifier.fit(imdb_train)
226
227
# Save the model to a local preset directory.
228
preset_dir = "./bert_tiny_imdb"
229
classifier.save_to_preset(preset_dir)
230
231
# Upload to Kaggle.
232
keras_hub.upload_preset(
233
f"kaggle://{kaggle_username}/bert/keras/bert_tiny_imdb", preset_dir
234
)
235
236
"""
237
After verifying that the model is uploaded to Kaggle, we can load the model by calling `from_preset`.
238
239
```python
240
classifier = keras_hub.models.Classifier.from_preset(
241
f"kaggle://{kaggle_username}/bert/keras/bert_tiny_imdb"
242
)
243
```
244
"""
245
246