-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogger.go
153 lines (140 loc) · 3.63 KB
/
logger.go
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
152
153
package callback
import (
"errors"
"fmt"
"github.com/codingbeard/cblog"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
var (
LoggerVerbose = "verbose"
LoggerTotalBatches = "totalBatches"
LoggerPrefetched = "prefetched"
)
type Logger struct {
FileLogger *cblog.Logger
Progress bool
ProgressLogDir string
modeStarts map[Mode]int64
modeStartsLock *sync.Mutex
lastPrint int64
}
func (l *Logger) Init() error {
if l.FileLogger == nil {
return errors.New("no FileLogger set on logger callback")
}
if l.Progress && l.ProgressLogDir == "" {
return errors.New("progress is enabled but no ProgressLogDir set on logger callback")
}
l.modeStartsLock = &sync.Mutex{}
l.modeStarts = make(map[Mode]int64)
return nil
}
func (l *Logger) Call(event Event, mode Mode, epoch int, batch int, logs []Log) ([]Action, error) {
l.modeStartsLock.Lock()
defer l.modeStartsLock.Unlock()
if event == EventStart {
if mode == ModeVal {
if l.modeStarts[ModeTrain] == 0 {
l.modeStarts[ModeTrain] = time.Now().Unix()
}
} else if mode == ModeTest {
if l.modeStarts[ModeTrain] == 0 {
l.modeStarts[ModeTrain] = time.Now().Unix()
}
if l.modeStarts[ModeVal] == 0 {
l.modeStarts[ModeVal] = time.Now().Unix()
}
}
l.modeStarts[mode] = time.Now().Unix()
l.lastPrint = 0
return []Action{ActionNop}, nil
}
totalBatches := 0
var metricString []string
var metricValues []interface{}
prefetched := -1
verbose := false
for _, log := range logs {
if log.Name == LoggerTotalBatches {
totalBatches = int(log.Value)
} else if log.Name == LoggerPrefetched {
prefetched = int(log.Value)
} else if log.Name == LoggerVerbose {
if log.Value != -1 {
verbose = true
}
} else {
metricString = append(metricString, log.Name+": %."+strconv.Itoa(log.Precision)+"f")
metricValues = append(metricValues, log.Value)
}
}
if event == EventSave {
if verbose {
l.FileLogger.InfoF("training", "Saved")
}
return []Action{ActionNop}, nil
}
previousElapsed := int64(0)
elapsedInMode := time.Now().Unix() - l.modeStarts[mode]
if mode == ModeVal {
previousElapsed = time.Now().Unix() - l.modeStarts[ModeTrain] - elapsedInMode
} else if mode == ModeTest {
previousElapsed = time.Now().Unix() - l.modeStarts[ModeTrain] + time.Now().Unix() - l.modeStarts[ModeVal] - elapsedInMode
}
logType := strings.Title(string(mode))
if event == EventEnd {
logType = "End"
}
if event == EventEnd && (mode == ModeVal || mode == ModeTest) {
if verbose {
fmt.Print("\r")
}
l.FileLogger.InfoF("training", fmt.Sprintf(
"%s %d %d/%d (%ds/%ds) %s ",
logType,
epoch,
batch,
totalBatches,
previousElapsed+elapsedInMode,
previousElapsed+int64((float32(elapsedInMode)/float32(batch))*float32(totalBatches)),
fmt.Sprintf(
strings.Join(metricString, " "),
metricValues...,
),
))
} else if verbose {
now := time.Now().Unix()
if now > l.lastPrint {
l.lastPrint = now
log := fmt.Sprintf(
"\r%s : logger.go : %s %d %d/%d (%ds/%ds) %s | Prefetched %d",
time.Now().Format("2006-01-02 15:04:05.000"),
logType,
epoch,
batch,
totalBatches,
previousElapsed+elapsedInMode,
previousElapsed+int64((float32(elapsedInMode)/float32(batch))*float32(totalBatches)),
fmt.Sprintf(
strings.Join(metricString, " "),
metricValues...,
),
prefetched,
)
fmt.Print(log)
if l.Progress {
e := ioutil.WriteFile(filepath.Join(l.ProgressLogDir, "progress.log"), []byte(log[1:]), os.ModePerm)
if e != nil {
l.FileLogger.ErrorF("error", e.Error())
}
}
}
}
return []Action{ActionNop}, nil
}