-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtils.py
More file actions
67 lines (57 loc) · 2.27 KB
/
Utils.py
File metadata and controls
67 lines (57 loc) · 2.27 KB
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
"""
will contan some important utility functions for the project
"""
import tensorflow as tf
from pathlib import Path
def plot_and_save_model_structure(model,
save_path="resources/",
save_as=f"model"):
"""
Plot and save the model structure
"""
model_name = save_as if not model.name else f'{save_as}-{model.name}'
save_path = Path(save_path)
save_path.mkdir(parents=True, exist_ok=True)
save_path = save_path / f"{model_name }.png"
save_as = str(save_path)
print(f"[INFO] Saving model structure to {save_as}")
tf.keras.utils.plot_model(model,
to_file=save_as,
show_shapes=False,
show_dtype=False,
show_layer_names=True,
rankdir="TB",
expand_nested=False,
dpi=96,
layer_range=None,
show_layer_activations=False,
show_trainable=False,
)
print(f"[INFO] Model structure saved to {save_as}")
def save_model(model: tf.keras.Model,
target_dir: str,
model_name: str):
"""Saves a tf model to a target directory.
Args:
model: A target tf model to save.
target_dir: A directory for saving the model to.
model_name: A filename for the saved model. Should include
either ".pth" or ".pt" as the file extension.
Example usage:
save_model(model=model,
target_dir="models",
model_name=".pth")
"""
# Create target directory
target_dir_path = Path(target_dir)
target_dir_path.mkdir(parents=True,exist_ok=True)
# Create model save path
assert model_name.endswith(".h5") or model_name.endswith(".h5"), "model_name should end with '.h5' "
model_save_path = str(target_dir_path / model_name)
# Save the model state_dict()
print(f"[INFO] Saving model to: {model_save_path}")
try:
model.save(model_save_path)
print(f"[INFO] Model saved to: {model_save_path}")
except Exception as e:
print(f"[ERROR] Error while saving model to {model_save_path}")