-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathtest_keras_integration.py
342 lines (264 loc) · 12.2 KB
/
test_keras_integration.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import json
import os
import unittest
from pathlib import Path
import pytest
from huggingface_hub import HfApi, hf_hub_download, snapshot_download
from huggingface_hub.keras_mixin import (
KerasModelHubMixin,
from_pretrained_keras,
push_to_hub_keras,
save_pretrained_keras,
)
from huggingface_hub.utils import is_graphviz_available, is_pydot_available, is_tf_available, logging
from .testing_constants import ENDPOINT_STAGING, TOKEN, USER
from .testing_utils import repo_name
logger = logging.get_logger(__name__)
if is_tf_available():
import tensorflow as tf
def require_tf(test_case):
"""
Decorator marking a test that requires TensorFlow, graphviz and pydot.
These tests are skipped when TensorFlow, graphviz and pydot are installed.
"""
if not is_tf_available() or not is_pydot_available() or not is_graphviz_available():
return unittest.skip("test requires Tensorflow, graphviz and pydot.")(test_case)
else:
return test_case
if is_tf_available():
# Define dummy mixin model...
class DummyModel(tf.keras.Model, KerasModelHubMixin):
def __init__(self, **kwargs):
super().__init__()
self.l1 = tf.keras.layers.Dense(2, activation="relu")
dummy_batch_size = input_dim = 2
self.dummy_inputs = tf.ones([dummy_batch_size, input_dim])
def call(self, x):
return self.l1(x)
else:
DummyModel = None
@require_tf
@pytest.mark.usefixtures("fx_cache_dir")
class CommonKerasTest(unittest.TestCase):
cache_dir: Path
@classmethod
def setUpClass(cls):
"""
Share this valid token in all tests below.
"""
cls._api = HfApi(endpoint=ENDPOINT_STAGING, token=TOKEN)
class HubMixinTestKeras(CommonKerasTest):
def test_save_pretrained(self):
model = DummyModel()
model(model.dummy_inputs)
model.save_pretrained(self.cache_dir)
files = os.listdir(self.cache_dir)
self.assertTrue("saved_model.pb" in files)
self.assertTrue("keras_metadata.pb" in files)
self.assertTrue("README.md" in files)
self.assertTrue("model.png" in files)
self.assertEqual(len(files), 7)
model.save_pretrained(self.cache_dir, config={"num": 12, "act": "gelu"})
files = os.listdir(self.cache_dir)
self.assertTrue("config.json" in files)
self.assertTrue("saved_model.pb" in files)
self.assertEqual(len(files), 8)
def test_keras_from_pretrained_weights(self):
model = DummyModel()
model(model.dummy_inputs)
model.save_pretrained(self.cache_dir)
new_model = DummyModel.from_pretrained(self.cache_dir)
# Check the reloaded model's weights match the original model's weights
self.assertTrue(tf.reduce_all(tf.equal(new_model.weights[0], model.weights[0])))
# Check a new model's weights are not the same as the reloaded model's weights
another_model = DummyModel()
another_model(tf.ones([2, 2]))
self.assertFalse(tf.reduce_all(tf.equal(new_model.weights[0], another_model.weights[0])).numpy().item())
def test_abs_path_from_pretrained(self):
model = DummyModel()
model(model.dummy_inputs)
model.save_pretrained(self.cache_dir, config={"num": 10, "act": "gelu_fast"})
model = DummyModel.from_pretrained(self.cache_dir)
self.assertTrue(model.config == {"num": 10, "act": "gelu_fast"})
def test_push_to_hub_keras_mixin_via_http_basic(self):
repo_id = f"{USER}/{repo_name()}"
model = DummyModel()
model(model.dummy_inputs)
model.push_to_hub(repo_id=repo_id, token=TOKEN, config={"num": 7, "act": "gelu_fast"})
# Test model id exists
assert self._api.model_info(repo_id).id == repo_id
# Test config has been pushed to hub
config_path = hf_hub_download(
repo_id=repo_id, filename="config.json", use_auth_token=TOKEN, cache_dir=self.cache_dir
)
with open(config_path) as f:
assert json.load(f) == {"num": 7, "act": "gelu_fast"}
# Delete tmp file and repo
self._api.delete_repo(repo_id=repo_id)
@require_tf
class HubKerasSequentialTest(CommonKerasTest):
def model_init(self):
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(2, activation="relu"))
model.compile(optimizer="adam", loss="mse")
return model
def model_fit(self, model):
x = tf.constant([[0.44, 0.90], [0.65, 0.39]])
y = tf.constant([[1, 1], [0, 0]])
model.fit(x, y)
return model
def test_save_pretrained(self):
model = self.model_init()
with pytest.raises(ValueError, match="Model should be built*"):
save_pretrained_keras(model, save_directory=self.cache_dir)
model.build((None, 2))
save_pretrained_keras(model, save_directory=self.cache_dir)
files = os.listdir(self.cache_dir)
self.assertIn("saved_model.pb", files)
self.assertIn("keras_metadata.pb", files)
self.assertIn("model.png", files)
self.assertIn("README.md", files)
self.assertEqual(len(files), 7)
loaded_model = from_pretrained_keras(self.cache_dir)
self.assertIsNone(loaded_model.optimizer)
def test_save_pretrained_model_card_fit(self):
model = self.model_init()
model = self.model_fit(model)
save_pretrained_keras(model, save_directory=self.cache_dir)
files = os.listdir(self.cache_dir)
history = json.loads((self.cache_dir / "history.json").read_text())
self.assertIn("saved_model.pb", files)
self.assertIn("keras_metadata.pb", files)
self.assertIn("model.png", files)
self.assertIn("README.md", files)
self.assertIn("history.json", files)
self.assertEqual(history, model.history.history)
self.assertEqual(len(files), 8)
def test_save_model_card_history_removal(self):
model = self.model_init()
model = self.model_fit(model)
history_path = self.cache_dir / "history.json"
history_path.write_text("Keras FTW")
with pytest.warns(UserWarning, match="`history.json` file already exists, *"):
save_pretrained_keras(model, save_directory=self.cache_dir)
# assert that it's not the same as old history file and it's overridden
self.assertNotEqual("Keras FTW", history_path.read_text())
# Check the history is saved as a json in the repository.
files = os.listdir(self.cache_dir)
self.assertIn("history.json", files)
# Check that there is no "Training Metrics" section in the model card.
# This was done in an older version.
self.assertNotIn("Training Metrics", (self.cache_dir / "README.md").read_text())
def test_save_pretrained_optimizer_state(self):
model = self.model_init()
model.build((None, 2))
save_pretrained_keras(model, self.cache_dir, include_optimizer=True)
loaded_model = from_pretrained_keras(self.cache_dir)
self.assertIsNotNone(loaded_model.optimizer)
def test_from_pretrained_weights(self):
model = self.model_init()
model.build((None, 2))
save_pretrained_keras(model, self.cache_dir)
new_model = from_pretrained_keras(self.cache_dir)
# Check a new model's weights are not the same as the reloaded model's weights
another_model = DummyModel()
another_model(tf.ones([2, 2]))
self.assertFalse(tf.reduce_all(tf.equal(new_model.weights[0], another_model.weights[0])).numpy().item())
def test_save_pretrained_task_name_deprecation(self):
model = self.model_init()
model.build((None, 2))
with pytest.warns(
FutureWarning,
match="`task_name` input argument is deprecated. Pass `tags` instead.",
):
save_pretrained_keras(model, self.cache_dir, tags=["test"], task_name="test", save_traces=True)
def test_abs_path_from_pretrained(self):
model = self.model_init()
model.build((None, 2))
save_pretrained_keras(
model, self.cache_dir, config={"num": 10, "act": "gelu_fast"}, plot_model=True, tags=None
)
new_model = from_pretrained_keras(self.cache_dir)
self.assertTrue(tf.reduce_all(tf.equal(new_model.weights[0], model.weights[0])))
self.assertTrue(new_model.config == {"num": 10, "act": "gelu_fast"})
def test_push_to_hub_keras_sequential_via_http_basic(self):
repo_id = f"{USER}/{repo_name()}"
model = self.model_init()
model = self.model_fit(model)
push_to_hub_keras(model, repo_id=repo_id, token=TOKEN, api_endpoint=ENDPOINT_STAGING)
assert self._api.model_info(repo_id).id == repo_id
repo_files = self._api.list_repo_files(repo_id)
assert "README.md" in repo_files
assert "model.png" in repo_files
self._api.delete_repo(repo_id=repo_id)
def test_push_to_hub_keras_sequential_via_http_plot_false(self):
repo_id = f"{USER}/{repo_name()}"
model = self.model_init()
model = self.model_fit(model)
push_to_hub_keras(model, repo_id=repo_id, token=TOKEN, api_endpoint=ENDPOINT_STAGING, plot_model=False)
repo_files = self._api.list_repo_files(repo_id)
self.assertNotIn("model.png", repo_files)
self._api.delete_repo(repo_id=repo_id)
def test_push_to_hub_keras_via_http_override_tensorboard(self):
"""Test log directory is overwritten when pushing a keras model a 2nd time."""
repo_id = f"{USER}/{repo_name()}"
log_dir = self.cache_dir / "tb_log_dir"
log_dir.mkdir(parents=True, exist_ok=True)
(log_dir / "tensorboard.txt").write_text("Keras FTW")
model = self.model_init()
model.build((None, 2))
push_to_hub_keras(model, repo_id=repo_id, log_dir=log_dir, api_endpoint=ENDPOINT_STAGING, token=TOKEN)
log_dir2 = self.cache_dir / "tb_log_dir2"
log_dir2.mkdir(parents=True, exist_ok=True)
(log_dir2 / "override.txt").write_text("Keras FTW")
push_to_hub_keras(model, repo_id=repo_id, log_dir=log_dir2, api_endpoint=ENDPOINT_STAGING, token=TOKEN)
files = self._api.list_repo_files(repo_id)
self.assertIn("logs/override.txt", files)
self.assertNotIn("logs/tensorboard.txt", files)
self._api.delete_repo(repo_id=repo_id)
def test_push_to_hub_keras_via_http_with_model_kwargs(self):
repo_id = f"{USER}/{repo_name()}"
model = self.model_init()
model = self.model_fit(model)
push_to_hub_keras(
model,
repo_id=repo_id,
api_endpoint=ENDPOINT_STAGING,
token=TOKEN,
include_optimizer=True,
save_traces=False,
)
assert self._api.model_info(repo_id).id == repo_id
snapshot_path = snapshot_download(repo_id=repo_id, cache_dir=self.cache_dir)
from_pretrained_keras(snapshot_path)
self._api.delete_repo(repo_id)
@require_tf
class HubKerasFunctionalTest(CommonKerasTest):
def model_init(self):
inputs = tf.keras.layers.Input(shape=(2,))
outputs = tf.keras.layers.Dense(2, activation="relu")(inputs)
model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer="adam", loss="mse")
return model
def model_fit(self, model):
x = tf.constant([[0.44, 0.90], [0.65, 0.39]])
y = tf.constant([[1, 1], [0, 0]])
model.fit(x, y)
return model
def test_save_pretrained(self):
model = self.model_init()
model.build((None, 2))
self.assertTrue(model.built)
save_pretrained_keras(model, self.cache_dir)
files = os.listdir(self.cache_dir)
self.assertIn("saved_model.pb", files)
self.assertIn("keras_metadata.pb", files)
self.assertEqual(len(files), 7)
def test_save_pretrained_fit(self):
model = self.model_init()
model = self.model_fit(model)
save_pretrained_keras(model, self.cache_dir)
files = os.listdir(self.cache_dir)
self.assertIn("saved_model.pb", files)
self.assertIn("keras_metadata.pb", files)
self.assertEqual(len(files), 8)