-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
91 lines (75 loc) · 2.67 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
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
import torch
import matplotlib.pyplot as plt # for plotting informations on graph and images using tensors
import json
def read_config_file(config_file):
""" read json config file and return dictionnary """
import json
with open('config.json') as f:
d = json.load(f)
return d
def display_img(img,label, train):
""" Our dataset consists of images in form of Tensors, this function uses
imshow() method of matplotlib python library to display images"""
print(f"Label : {train.classes[label]}")
#permute method reshapes the image from (3,256,256) to (256,256,3).
plt.imshow(img.permute(1,2,0))
plt.show()
def show_batch(dl):
"""Plot images grid of single batch"""
for images, labels in dl:
fig,ax = plt.subplots(figsize = (30,30))
ax.set_xticks([])
ax.set_yticks([])
ax.imshow(make_grid(images,nrow=8).permute(1,2,0))
break
# for moving data into GPU (if available)
def get_default_device():
"""Pick GPU if available, else CPU"""
if torch.cuda.is_available:
print('torch.cuda is_available')
return torch.device("cuda")
else:
return torch.device("cpu")
# for moving data to device (CPU or GPU)
def to_device(data, device):
"""Move tensor(s) to chosen device"""
if isinstance(data, (list,tuple)):
return [to_device(x, device) for x in data]
return data.to(device, non_blocking=True)
# for loading in the device (GPU if available else CPU)
class DeviceDataLoader():
"""Wrap a dataloader to move data to a device"""
def __init__(self, dl, device):
self.dl = dl
self.device = device
def __iter__(self):
"""Yield a batch of data after moving it to device"""
for b in self.dl:
yield to_device(b, self.device)
def __len__(self):
"""Number of batches"""
return len(self.dl)
def plot_accuracies(history):
accuracies = [x['val_accuracy'] for x in history]
plt.plot(accuracies, '-x')
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.title('Accuracy vs. No. of epochs')
plt.show()
def plot_losses(history):
train_losses = [x.get('train_loss') for x in history]
val_losses = [x['val_loss'].detach().cpu() for x in history]
plt.plot(train_losses, '-bx')
plt.plot(val_losses, '-rx')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.legend(['Training', 'Validation'])
plt.title('Loss vs. No. of epochs')
plt.show()
def plot_lrs(history):
lrs = np.concatenate([x.get('lrs', []) for x in history])
plt.plot(lrs)
plt.xlabel('Batch no.')
plt.ylabel('Learning rate')
plt.title('Learning Rate vs. Batch no.')
plt.show()