-
Notifications
You must be signed in to change notification settings - Fork 652
/
utils.py
56 lines (44 loc) · 1.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
import numpy as np
from matplotlib import pyplot as plt
import csv
import math
import pandas
def plot_log(filename, show=True):
data = pandas.read_csv(filename)
fig = plt.figure(figsize=(4,6))
fig.subplots_adjust(top=0.95, bottom=0.05, right=0.95)
fig.add_subplot(211)
for key in data.keys():
if key.find('loss') >= 0 and not key.find('val') >= 0: # training loss
plt.plot(data['epoch'].values, data[key].values, label=key)
plt.legend()
plt.title('Training loss')
fig.add_subplot(212)
for key in data.keys():
if key.find('acc') >= 0: # acc
plt.plot(data['epoch'].values, data[key].values, label=key)
plt.legend()
plt.title('Training and validation accuracy')
# fig.savefig('result/log.png')
if show:
plt.show()
def combine_images(generated_images, height=None, width=None):
num = generated_images.shape[0]
if width is None and height is None:
width = int(math.sqrt(num))
height = int(math.ceil(float(num)/width))
elif width is not None and height is None: # height not given
height = int(math.ceil(float(num)/width))
elif height is not None and width is None: # width not given
width = int(math.ceil(float(num)/height))
shape = generated_images.shape[1:3]
image = np.zeros((height*shape[0], width*shape[1]),
dtype=generated_images.dtype)
for index, img in enumerate(generated_images):
i = int(index/width)
j = index % width
image[i*shape[0]:(i+1)*shape[0], j*shape[1]:(j+1)*shape[1]] = \
img[:, :, 0]
return image
if __name__=="__main__":
plot_log('result/log.csv')