-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
65 lines (51 loc) · 1.8 KB
/
utils.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
import torch
from pathlib import Path
import matplotlib.pyplot as plt
def save_model(model: torch.nn.Module,
target_dir: str,
model_name: str):
"""Saves a PyTorch model to a target directory.
Args:
model: A target PyTorch 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_0,
target_dir="models",
model_name="05_going_modular_tingvgg_model.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(".pth") or model_name.endswith(".pt"), "model_name should end with '.pt' or '.pth'"
model_save_path = target_dir_path / model_name
# Save the model state_dict()
print(f"[INFO] Saving model to: {model_save_path}")
torch.save(obj=model.state_dict(),
f=model_save_path)
def set_seeds(seed:int=42):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
def plot_loss_curves(results):
acc = results['train_acc']
test_acc = results['test_acc']
loss = results['train_loss']
test_loss = results['test_loss']
epochs = range(len(loss))
plt.figure(figsize=(15, 7))
plt.subplot(1, 2, 1)
plt.plot(epochs, loss, label='train loss')
plt.plot(epochs, test_loss, label='test loss')
plt.title('Loss')
plt.xlabel('Epochs')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(epochs, acc, label='train accuracy')
plt.plot(epochs, test_acc, label='test accuracy')
plt.title('Accuracy')
plt.xlabel('Epochs')
plt.legend()
plt.show()