-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_loss.py
132 lines (113 loc) · 5.34 KB
/
graph_loss.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
# -*- coding: utf-8 -*-
# Author: Antoine DELPLACE
# Last update: 18/11/2019
"""
Post-processing program to visualize loss functions and the processing time during training of DCGAN and SRResGAN
Parameters
----------
directory_name : name of the folder containing the log of the training program called "output.txt"
(contains regex like (\d+.\d+) - Epoch (\d+), Batch (\d+): d_loss=(-?\d+.\d+(e(\+|-)\d+)?|nan), g_loss=(-?\d+.\d+(e(\+|-)\d+)?|nan))
Return
----------
Plot and save the two graphs "learning_curve.pdf" and "processing_time.pdf" in the folder directory_name
"""
import numpy as np
import matplotlib.pyplot as plt
import os
import sys
import re
if __name__ == "__main__":
# Reading parameters
if len(sys.argv) < 2:
print("Need a directory_name")
sys.exit(1)
directory_name = sys.argv[1]
print(directory_name)
# Reading output text file
with open(os.path.join(directory_name, "output.txt")) as f:
file_lines = f.readlines()
file_lines = [x.strip() for x in file_lines]
# Initialization
i=0
while " - Epoch 0, Batch 0: " not in file_lines[i]:
i += 1
tab_time_val = []
tab_epoch_val = []
tab_batch_val = []
tab_d_loss_val = []
tab_g_loss_val = []
tab_time_test = []
tab_epoch_test = []
tab_batch_test = []
tab_d_loss_test = []
tab_g_loss_test = []
# Extracting data
while i < len(file_lines):
if " - Testing: " in file_lines[i]:
regex_group = re.search("(\d+.\d+) - Epoch (\d+), Batch (\d+) - Testing: d_loss=(-?\d+.\d+(e(\+|-)\d+)?|nan), g_loss=(-?\d+.\d+(e(\+|-)\d+)?|nan)", file_lines[i])
#print(i, regex_group.group(1), regex_group.group(2), regex_group.group(3), regex_group.group(4), regex_group.group(7))
tab_time_test.append(regex_group.group(1))
tab_epoch_test.append(regex_group.group(2))
tab_batch_test.append(regex_group.group(3))
tab_d_loss_test.append(regex_group.group(4))
tab_g_loss_test.append(regex_group.group(7))
else:
regex_group = re.search("(\d+.\d+) - Epoch (\d+), Batch (\d+): d_loss=(-?\d+.\d+(e(\+|-)\d+)?|nan), g_loss=(-?\d+.\d+(e(\+|-)\d+)?|nan)", file_lines[i])
#print(i, regex_group.group(1), regex_group.group(2), regex_group.group(3), regex_group.group(4), regex_group.group(7))
tab_time_val.append(regex_group.group(1))
tab_epoch_val.append(regex_group.group(2))
tab_batch_val.append(regex_group.group(3))
tab_d_loss_val.append(regex_group.group(4))
tab_g_loss_val.append(regex_group.group(7))
i += 1
tab_time_val = np.array(tab_time_val, dtype=float)
tab_epoch_val = np.array(tab_epoch_val, dtype=int)
tab_batch_val = np.array(tab_batch_val, dtype=int)
tab_d_loss_val = np.array(tab_d_loss_val, dtype=float)
tab_g_loss_val = np.array(tab_g_loss_val, dtype=float)
tab_time_val_difference = np.diff(tab_time_val)
tab_batch_glob_val = tab_batch_val+tab_epoch_val*(np.max(tab_batch_val)+1)
tab_batch_glob_val_difference = np.diff(tab_batch_glob_val)
tab_time_test = np.array(tab_time_test, dtype=float)
tab_epoch_test = np.array(tab_epoch_test, dtype=int)
tab_batch_test = np.array(tab_batch_test, dtype=int)
tab_d_loss_test = np.array(tab_d_loss_test, dtype=float)
tab_g_loss_test = np.array(tab_g_loss_test, dtype=float)
tab_time_test_difference = np.diff(tab_time_test)
tab_batch_glob_test = tab_batch_test+tab_epoch_test*(np.max(tab_batch_val)+1)
tab_batch_glob_test_difference = np.diff(tab_batch_glob_test)
# Plotting learning curve
plt.figure(figsize=(8, 8))
plt.title("Learning curve")
plt.plot(tab_d_loss_val)
plt.plot(tab_g_loss_val)
plt.plot(tab_batch_glob_test, tab_d_loss_test, '.')
plt.plot(tab_batch_glob_test, tab_g_loss_test, '.')
for i in range(0, len(tab_batch_val)):
if tab_batch_val[i] == 0:
plt.axvline(x=tab_batch_glob_val[i], linestyle="--", color="gray")
plt.xlabel("Batchs")
plt.ylabel("Loss")
if np.nanmin(tab_d_loss_test) < 0 or np.nanmin(tab_g_loss_test) < 0 or np.nanmin(tab_d_loss_val) < 0 or np.nanmin(tab_g_loss_val) < 0:
plt.yscale('symlog')
else:
plt.yscale('log')
plt.legend(["Discriminator validation loss", "Generator validation loss", "Discriminator test loss", "Generator test loss", "Epochs"])
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
plt.savefig(os.path.join(directory_name, "learning_curve.pdf"), format="pdf")
plt.show()
# Plotting processing time graph
plt.figure(figsize=(8, 8))
plt.title("Average processing time")
plt.plot(np.cumsum(tab_batch_glob_val_difference), np.divide(tab_time_val_difference, tab_batch_glob_val_difference))
plt.plot(np.cumsum(tab_batch_glob_test_difference), np.divide(tab_time_test_difference, tab_batch_glob_test_difference), '.')
for i in range(0, len(tab_batch_val)):
if tab_batch_val[i] == 0:
plt.axvline(x=tab_batch_glob_val[i], linestyle="--", color="gray")
plt.xlabel("Batchs")
plt.ylabel("Time (sec/input)")
plt.yscale('log')
plt.legend(["Validation processing time", "Test processing time", "Epochs"])
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
plt.savefig(os.path.join(directory_name, "processing_time.pdf"), format="pdf")
plt.show()