allInOneTrainingPlots.py
Go to the documentation of this file.
1 import os, sys
2 import re
3 import pandas as pd
4 import argparse
5 import matplotlib.pyplot as plt
6 import numpy as np
7 from ROOT import *
8 
9 #gROOT.Macro( os.path.expanduser( '$ROOTSYS/macros/rootlogon.C') )
10 
11 parser = argparse.ArgumentParser()
12 parser.add_argument('--input', type=str, required=True)
13 parser.add_argument('--output', type=str, required=True)
14 args = parser.parse_args()
15 log1=args.input
16 
17 def extractTrainLoss(logPath):
18  lossPattern = r"Iteration (?P<iter_num>\d+) .*, loss = (?P<loss_val>[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?)"
19 
20  iterations = []
21  losses = []
22 
23  for pair in re.findall(lossPattern, open(logPath, 'r').read()):
24  iterations.append(int(pair[0]))
25  losses.append(float(pair[1]))
26 
27  return pd.DataFrame({"iteration":iterations, "loss":losses})
28 
29 df = extractTrainLoss(log1)
30 
31 print df.head()
32 
33 df.plot("iteration", "loss")
34 
35 def extractTestLoss(logPath):
36  iterationPattern = r"Iteration (?P<iter_num>\d+), Testing net"
37  iterations = [int(r) for r
38  in re.findall(iterationPattern,
39  open(logPath, 'r').read())]
40 
41  testPattern = r"Test net output #0: loss3/loss3 = (?P<loss_val>[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?)"
42  tests = [float(r[0]) for r
43  in re.findall(testPattern, open(logPath, 'r').read())]
44 
45  accPattern = r" Test net output #1: loss3/top-1 = (?P<loss_val>[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?)"
46  accs = [float(r[0]) for r
47  in re.findall(accPattern, open(logPath, 'r').read())]
48  print len(iterations), len(tests), len(accs)
49 
50  return pd.DataFrame({"iteration":iterations, "loss":tests, "accuracy":accs})
51 
52 TGaxis.SetMaxDigits(8)
53 
54 tdf = extractTestLoss(log1)
55 
56 print tdf.head()
57 
58 ax = df.plot("iteration", "loss", label="Train Loss")
59 tdf.plot("iteration", "loss", ax=ax, label="Test Loss" )
60 
61 df["rollavg"] = pd.rolling_window(df.loss, window=200, win_type="boxcar")
62 
63 ax = df.plot("iteration", "rollavg", label="Train Loss")
64 tdf.plot("iteration", "loss", ax=ax, label="Test Loss" )
65 
66 tdf.plot("iteration", "accuracy", label="Test Accuracy" )
67 
68 tdf["rollacc"] = pd.rolling_window(tdf.accuracy, window=10, win_type="boxcar")
69 ax = tdf.plot("iteration", "rollacc", label="Test Accuracy" )
70 
71 trainLoss = TGraph()
72 trainLoss.SetName("trainLoss")
73 
74 testLoss = TGraph()
75 testLoss.SetName("testLoss")
76 
77 testAccuracy = TGraph()
78 testAccuracy.SetName("testAccuracy")
79 
80 trainLoss.SetLineColor(kGreen+2);
81 testLoss.SetLineColor(kBlue);
82 testAccuracy.SetLineColor(kRed);
83 
84 trainLoss.SetLineWidth(2);
85 testLoss.SetLineWidth(2);
86 testAccuracy.SetLineWidth(2);
87 
88 iPoint = 0
89 for num, row in tdf.iterrows():
90  testLoss.SetPoint(iPoint, row.iteration, row.loss)
91  testAccuracy.SetPoint(iPoint, row.iteration, row.accuracy)
92  iPoint += 1
93 
94 iPoint = 0
95 for num, row in df.iterrows():
96  if not np.isnan(row.rollavg):
97  trainLoss.SetPoint(iPoint, row.iteration, row.rollavg)
98  iPoint += 1
99 
100 c1 = TCanvas("c1","gerrors2")
101 pad = TPad("pad","",0,0,1,1)
102 pad.SetFillColor(kWhite)
103 pad.SetTickx(False)
104 pad.SetTicky(False)
105 pad.Draw()
106 pad.cd()
107 
108 hr = c1.DrawFrame(0,2.0,2000000,4.0)
109 hr.SetYTitle("Loss")
110 hr.SetXTitle("Number of Training Iterations")
111 hr.GetXaxis().CenterTitle()
112 hr.GetYaxis().SetTitleSize(.04)
113 hr.GetYaxis().CenterTitle()
114 hr.GetXaxis().SetTitleOffset(1.3)
115 hr.GetYaxis().SetTitleOffset(1.2)
116 hr.GetXaxis().SetNoExponent(kFALSE)
117 hr.GetXaxis().SetNdivisions(5)
118 hr.GetXaxis().SetLabelOffset(.02)
119 hr.SetFillColor(kWhite)
120 
121 pad.GetFrame().SetFillColor(kWhite)
122 pad.GetFrame().SetBorderSize(12)
123 
124 testLoss.Draw("l")
125 trainLoss.Draw("l same")
126 c1.cd()
127 
128 overlay = TPad("overlay","",0,0,1,1)
129 overlay.SetFillStyle(4000)
130 overlay.SetFillColor(kWhite)
131 overlay.SetFrameFillStyle(4000)
132 overlay.SetTickx(False)
133 overlay.SetTicky(False)
134 overlay.Draw()
135 overlay.cd()
136 
137 xmin = pad.GetUxmin()
138 ymin = 0.1
139 xmax = pad.GetUxmax()
140 ymax = 0.4
141 hframe = overlay.DrawFrame(xmin,ymin,xmax,ymax)
142 hframe.SetFillColor(kWhite)
143 hframe.GetXaxis().SetLabelOffset(99)
144 hframe.GetYaxis().SetLabelOffset(99)
145 hframe.GetXaxis().SetTickLength(0)
146 hframe.GetYaxis().SetTickLength(0)
147 
148 testAccuracy.Draw("l")
149 
150 mylegend = TLegend(0.63,0.67,0.86,0.89)
151 mylegend.AddEntry(trainLoss,"Training Loss","L")
152 mylegend.AddEntry(testLoss,"Test Loss","L")
153 mylegend.AddEntry(testAccuracy,"Test Accuracy","L")
154 mylegend.SetLineWidth(0)
155 mylegend.SetBorderSize(0)
156 mylegend.SetFillStyle(0)
157 mylegend.Draw()
158 
159 axis = TGaxis(xmax,ymin,xmax, ymax,ymin,ymax,510,"+L")
160 axis.SetLineColor(kBlack)
161 axis.SetTitle("Accuracy")
162 axis.SetTitleFont(42)
163 axis.SetTitleOffset(1.1)
164 axis.SetTitleColor(kBlack)
165 axis.SetLabelFont(42)
166 axis.CenterTitle()
167 axis.SetLabelColor(kBlack)
168 axis.SetLabelSize(.035)
169 axis.Draw()
170 
171 c1.cd()
172 c1.Modified()
173 c1.Update()
174 
175 outName = "learning_" + str(args.output) + ".png"
176 
177 #c1.cd()
178 c1.Print(outName)
179 
180 #raw_input("Press Enter to Exit")
bin1_2sigma SetFillColor(3)
legend SetBorderSize(0)
procfile open("FD_BRL_v0.txt")