-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
54 lines (45 loc) · 1.59 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
import matplotlib.pyplot as plt
import os
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor
plt.style.use("ggplot")
def get_data(batch_size=64):
# CIFAR10 training dataset.
dataset_train = datasets.CIFAR10(
root="data",
train=True,
download=True,
transform=ToTensor(),
)
# CIFAR10 validation dataset.
dataset_valid = datasets.CIFAR10(
root="data",
train=False,
download=True,
transform=ToTensor(),
)
# Create data loaders.
train_loader = DataLoader(dataset_train, batch_size=batch_size, shuffle=True)
valid_loader = DataLoader(dataset_valid, batch_size=batch_size, shuffle=False)
return train_loader, valid_loader
def save_plots(train_acc, valid_acc, train_loss, valid_loss, name=None):
"""
Function to save the loss and accuracy plots to disk.
"""
# Accuracy plots.
plt.figure(figsize=(10, 7))
plt.plot(train_acc, color="tab:blue", linestyle="-", label="train accuracy")
plt.plot(valid_acc, color="tab:red", linestyle="-", label="validataion accuracy")
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.legend()
plt.savefig(os.path.join("outputs", name + "_accuracy.png"))
# Loss plots.
plt.figure(figsize=(10, 7))
plt.plot(train_loss, color="tab:blue", linestyle="-", label="train loss")
plt.plot(valid_loss, color="tab:red", linestyle="-", label="validataion loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legend()
plt.savefig(os.path.join("outputs", name + "_loss.png"))