-
Notifications
You must be signed in to change notification settings - Fork 30
/
plot.py
122 lines (106 loc) · 4.89 KB
/
plot.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
# This file is a part of StarNet code.
# https://github.com/nekitmm/starnet
#
# StarNet is a neural network that can remove stars from images leaving only background.
#
# Throughout the code all input and output images are 8 bits per channel tif images.
# This code in original form will not read any images other than these (like jpeg, etc), but you can change that if you like.
#
# Copyright (c) 2018 Nikita Misiura
# http://www.astrobin.com/users/nekitmm/
#
# This code is distributed on an "AS IS" BASIS WITHOUT WARRANTIES OF ANY KIND, express or implied.
# Please review LICENSE file before use.
import numpy as np
import matplotlib.pyplot as plt
LOGS_DIR = './logs/' # Output directory.
# list of nice colours
# thanks to this tutorial:
# http://www.randalolson.com/2014/06/28/how-to-make-beautiful-data-visualizations-in-python-with-matplotlib/
tableau20 = np.array([[ 31, 119, 180],
[174, 199, 232],
[255, 127, 14],
[255, 187, 120],
[ 44, 160, 44],
[152, 223, 138],
[214, 39, 40],
[255, 152, 150],
[148, 103, 189],
[197, 176, 213],
[140, 86, 75],
[196, 156, 148],
[227, 119, 194],
[247, 182, 210],
[127, 127, 127],
[199, 199, 199],
[188, 189, 34],
[219, 219, 141],
[ 23, 190, 207],
[158, 218, 229]], dtype = np.float)
tableau20 /= 255
def plot():
createPlot(file_name = "perceptual_losses",
plot_name = "Perceptual losses",
data_labels = ['P 1', 'P 2', 'P 3', 'P 4', 'P 5', 'P 6', 'P 7', 'P 8'],
X_label = "Epoch",
data = np.loadtxt(LOGS_DIR + '/perceptual_losses.txt', skiprows = 1, delimiter = ' '))
createPlot(file_name = "L1_loss",
plot_name = "L1 loss",
data_labels = [''],
X_label = "Epoch",
data = np.loadtxt(LOGS_DIR + '/L1_loss.txt', skiprows = 1, delimiter = ' '))
createPlot(file_name = "accuracy",
plot_name = "Accuracy, %",
data_labels = [''],
X_label = "Epoch",
data = np.loadtxt(LOGS_DIR + '/accuracy.txt', skiprows = 1, delimiter = ' '))
createPlot(file_name = "total_loss",
plot_name = "Total loss",
data_labels = [''],
X_label = "Epoch",
data = np.loadtxt(LOGS_DIR + '/total_loss.txt', skiprows = 1, delimiter = ' '))
createPlot(file_name = "adversarial_losses",
plot_name = "Adversarial losses",
data_labels = ['Discriminative', 'Generative'],
X_label = "Epoch",
data = np.loadtxt(LOGS_DIR + '/adversarial_losses.txt', skiprows = 1, delimiter = ' '))
def createPlot(file_name, plot_name, data_labels, X_label, data):
# plot size with 16:9 ratio
plt.figure(figsize = (16, 9))
# start plot
ax = plt.subplot(111)
# remove top and right box boundaries
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
# define boundaries for plot
MAX_X = int(data[-1, 0]) + 2
MAX_Y = int(np.max(data[:, 1:]) * 12) / 10
plt.ylim(0, MAX_Y)
plt.xlim(0, MAX_X)
# ticks and lines
yaxis_range = np.arange(0, MAX_Y, MAX_Y / 10)
xaxis_range = np.arange(0, MAX_Y, MAX_Y / 10)
plt.yticks(yaxis_range, [str('%.2f' % x) for x in yaxis_range], fontsize = 20)
plt.xticks(fontsize = 20)
for y in yaxis_range:
plt.plot(range(0, MAX_X + 1), [y] * len(range(0, MAX_X + 1)), "--", lw = 0.5, color = "black", alpha = 0.3)
plt.tick_params(axis = "both", which = "both", bottom = "on", top = "off",
labelbottom = "on", left = "on", right = "off", labelleft = "on")
# list of positions of data labels
ly_pos = np.arange(0.25, 1, 0.06) * MAX_Y
# selected list of colours
c_ind = [0, 4, 16, 6, 3, 10, 8, 14]
# X axis label
plt.text(MAX_X + 0.25, 0, X_label, fontsize = 20, color = "black")
#plot the data
for i in range(1, data.shape[1]):
# select colour
color = c_ind[i - 1]
# plot
plt.plot(data[:, 0], data[:, i], lw = 1.5, color = tableau20[color])
# add label for this data
plt.text(MAX_X + 0.5, ly_pos[i - 1], data_labels[i - 1], fontsize = 20, color = tableau20[color])
# add caption
plt.text(MAX_X / 2, MAX_Y * 1.05, plot_name, fontsize = 25, ha = "center")
plt.savefig(LOGS_DIR + file_name + ".png", bbox_inches = "tight")
plt.close()