-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRecorder.py
executable file
·151 lines (119 loc) · 5.4 KB
/
Recorder.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os
import numpy as np
import torchvision.utils as vutils
from tensorboardX import SummaryWriter
from IPython import display
from matplotlib import pyplot as plt
import torch
from datetime import date
'''
TensorBoard Data will be stored in './runs' path
'''
class Recorder:
def __init__(self, model_name, data_name):
self.data_name = data_name
self.comment = '{}_{}'.format(model_name, data_name)
self.data_subdir = data_name
self.image_subdir = os.path.join(self.data_subdir, 'images')
self.check_point_store = os.path.join(self.data_subdir, 'CheckPointStore')
# Recorder.make_dir(self.data_subdir)
# Recorder.make_dir(self.image_subdir)
# Recorder.make_dir(self.check_point_store)
# TensorBoard
# self.writer = SummaryWriter('{}_log'.format(model_name.lower()))
# today = date.today()
# d1 = today.strftime("%d/%m/%Y")
# print("d1 =", d1)
if os.path.exists('runs/{}'.format(model_name)):
model_name = model_name + '(0)'
while os.path.exists('runs/{}'.format(model_name)):
tmp = list(model_name)
num = int(tmp[-2])
num += 1
tmp[-2] = str(num)
model_name = ''.join(tmp)
# self.writer = SummaryWriter()
self.writer = SummaryWriter('runs/{}'.format(model_name))
def record(self, loss, epoch, n_batch, num_batches, loss_name='loss'):
# var_class = torch.autograd.variable.Variable
if isinstance(loss, torch.autograd.Variable):
loss = loss.data.cpu().numpy()
recoder_step = Recorder.recoder_step(epoch, n_batch, num_batches)
self.writer.add_scalar(loss_name, loss, recoder_step)
def log_images(self, images, num_images, epoch, n_batch, num_batches,
format='NCHW', normalize=True, title=None, nrows=8, range=(0, 1)):
"""
input images are expected in format (NCHW)
"""
if type(images) == np.ndarray:
if len(images.shape) == 3:
images = torch.from_numpy(images).permute(2, 0, 1)
else:
images = torch.from_numpy(images)
if format == 'NHWC':
images = images.transpose(1, 3)
recoder_step = Recorder.recoder_step(epoch, n_batch, num_batches)
# print(recoder_step)
img_name = '{}/images: *{}*'.format(self.comment, title)
# nrows = int(np.sqrt(num_images))
# print("{} / {}".format(num_images, nrows))
# Make horizontal grid from image tensor
horizontal_grid = vutils.make_grid(images, nrow=nrows, normalize=normalize, scale_each=True, range=range)
# Make vertical grid from image tensor
grid = vutils.make_grid(images, nrow=nrows, normalize=True, scale_each=True)
# Add horizontal images t o tensorboard
self.writer.add_image(img_name, horizontal_grid, recoder_step)
# Save plots // Save images to folder using IPython
# self.save_torch_images(horizontal_grid, grid, epoch, n_batch)
def save_torch_images(self, horizontal_grid, grid, epoch, n_batch, plot_horizontal=True, axis=False):
# Plot and save horizontal
fig = plt.figure(figsize=(16, 16))
plt.imshow(np.moveaxis(horizontal_grid.numpy(), 0, -1))
if not axis:
plt.axis('off')
if plot_horizontal:
display.display(plt.gcf())
self.save_images(fig, epoch, n_batch, comment='horizontal')
plt.close()
# Save squared
fig = plt.figure()
plt.imshow(np.moveaxis(grid.numpy(), 0, -1))
if not axis:
plt.axis('off')
self.save_images(fig, epoch, n_batch)
plt.close()
def save_images(self, fig, epoch, n_batch, comment=''):
fig.savefig(os.path.join(self.image_subdir,
'{}_epoch_{}_batch_{}.png'.format(comment, epoch, n_batch)))
@staticmethod
def display_status(epoch, num_epochs, n_batch, num_batches, loss):
if isinstance(loss, torch.Tensor):
loss_value = loss.data.cpu().numpy()
else:
loss_value = loss
if isinstance(loss_value, tuple):
print('Epoch: [{}/{}], Batch Num: [{}/{}]'.format(
epoch, num_epochs, n_batch, num_batches))
print('Generator={}, Discriminator={}, Encoder={}, Entropy={}'.format(loss_value[0],
loss_value[1],
loss_value[2],
loss_value[3]))
else:
print('Epoch: [{}/{}], Batch Num: [{}/{}], Loss:{:.4f}'.format(
epoch, num_epochs, n_batch, num_batches, loss_value))
def save_models(self, model, epoch, name=None):
out_dir = self.check_point_store
torch.save(model.state_dict(),
'{}/{}_epoch_{}'.format(out_dir, name, epoch))
print('>> epoch_{} saving in {}'.format(epoch, out_dir))
def close(self):
self.writer.close()
# Private Functionality
@staticmethod
def recoder_step(epoch, n_batch, num_batches):
return n_batch
# return epoch * num_batches #+ n_batch
@staticmethod
def make_dir(directory):
if not os.path.exists(directory):
os.mkdir(directory)