-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
44 lines (34 loc) · 1.02 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
import numpy as np
import matplotlib.pyplot as plt
import json
import os
import config
def imshow(img):
img = img / 2 + 0.5
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
def save_history(history, path, name_file):
os.makedirs(path, exist_ok=True)
with open(os.path.join(path, name_file), "w+") as f:
json.dump(history, f, indent=2)
def visualize_images(images, epoch=0, batch=0, save=True):
images = images.clamp(0, 1)
fig, ax = plt.subplots(1, 10, figsize=(10, 2))
for i in range(10):
img = images[i].detach().cpu().numpy()
img = np.transpose(img, (1, 2, 0))
ax[i].imshow(img, cmap='gray')
ax[i].axis('off')
if save:
plt.savefig(f"{config.RESULT_DIR}/e_{epoch}_b_{batch}.png")
def plot_history(history):
plt.figure()
for key in history:
plt.plot(history[key], label=key)
plt.grid()
plt.title('Training History')
plt.xlabel('Iteration')
plt.ylabel('Loss')
plt.legend()
plt.show()