Skip to content

Commit 0320da0

Browse files
log individual loss types and average losses
1 parent 3afe611 commit 0320da0

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

tools/train/tinyYolov2/overfit.html

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,26 @@
1717
<script>
1818
tf = faceapi.tf
1919

20-
const weightsUrl = '/tmp/test.weights'
21-
const fromEpoch = 800
20+
const weightsUrl = '/tmp/initial_tiny_yolov2_glorot_normal.weights'
21+
const fromEpoch = 0
2222

2323

24-
window.debug = true
25-
window.logTrainSteps = true
24+
window.debug = false
25+
window.logTrainSteps = false
2626

2727

2828
// hyper parameters
2929
window.objectScale = 5
3030
window.noObjectScale = 1
3131
window.coordScale = 1
3232

33-
window.saveEveryNthIteration = 50
33+
window.saveEveryNthIteration = 1
3434
window.trainSteps = 4000
3535
//window.optimizer = tf.train.sgd(0.001)
3636
window.optimizer = tf.train.adam(0.001, 0.9, 0.999, 1e-8)
3737

38-
const numTrainSamples = 1
38+
// all samples
39+
const numTrainSamples = Infinity
3940

4041
async function loadNetWeights(uri) {
4142
return new Float32Array(await (await fetch(uri)).arrayBuffer())
@@ -50,13 +51,18 @@
5051
window.net = new faceapi.TinyYolov2(true)
5152
window.net.load(weights)
5253
window.net.variable()
53-
window.detectionFilenames = (await fetchDetectionFilenames()).slice(1, numTrainSamples + 1)
54+
window.detectionFilenames = (await fetchDetectionFilenames()).slice(0, numTrainSamples)
5455
window.lossMap = {}
5556

5657
console.log('ready')
5758
}
5859

59-
const trainSizes = [608]
60+
const trainSizes = [320]
61+
62+
function logLossChange(lossType) {
63+
const { currentLoss, prevLoss, detectionFilenames } = window
64+
log(`${lossType} : ${faceapi.round(currentLoss[lossType])} (avg: ${faceapi.round(currentLoss[lossType] / detectionFilenames.length)}) (delta: ${currentLoss[lossType] - prevLoss[lossType]})`)
65+
}
6066

6167
async function train(batchSize = 1) {
6268
for (let i = fromEpoch; i < trainSteps; i++) {
@@ -81,21 +87,30 @@
8187
log()
8288
log('step %s done (%s ms)', i, ts)
8389

84-
const currentLoss = Object.keys(lossMap).map(k => lossMap[k]).reduce((sum, l) => sum + l)
90+
window.prevLoss = window.currentLoss
91+
window.currentLoss = Object.keys(lossMap)
92+
.map(filename => lossMap[filename])
93+
.reduce((accumulatedLosses, losses) =>
94+
Object.keys(losses)
95+
.map(key => ({
96+
[key]: (accumulatedLosses[key] || 0) + losses[key]
97+
}))
98+
.reduce((map, curr) => ({ ...map, ...curr }), {}),
99+
{}
100+
)
101+
85102
if (window.prevLoss) {
86-
log('prevLoss:', window.prevLoss)
87-
log('currentLoss:', currentLoss)
88-
log('loss change:', currentLoss - window.prevLoss)
103+
logLossChange('noObjectLoss')
104+
logLossChange('objectLoss')
105+
logLossChange('coordLoss')
106+
logLossChange('totalLoss')
89107
}
90108
log()
91109
log('--------------------')
92110
log()
93111

94-
window.prevLoss = currentLoss
95-
96-
97112
if (((i + 1) % saveEveryNthIteration) === 0) {
98-
saveWeights(window.net, 'adam_511_' + i + '.weights')
113+
saveWeights(window.net, 'adam_511_' + (i + 1) + '.weights')
99114
}
100115
}
101116
}

tools/train/tinyYolov2/train.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,26 @@ async function trainStep(batchCreators, inputSize) {
2525
batchInput.getRelativePaddings(batchIdx)
2626
)
2727

28-
29-
const total = totalLoss.dataSync()[0]
28+
const losses = {
29+
totalLoss: totalLoss.dataSync()[0],
30+
noObjectLoss: noObjectLoss.dataSync()[0],
31+
objectLoss: objectLoss.dataSync()[0],
32+
coordLoss: coordLoss.dataSync()[0]
33+
}
3034

3135
if (window.logTrainSteps) {
3236
log(`ground truth boxes: ${groundTruthBoxes[batchIdx].length}`)
33-
log(`noObjectLoss[${dataIdx}]: ${noObjectLoss.dataSync()}`)
34-
log(`objectLoss[${dataIdx}]: ${objectLoss.dataSync()}`)
35-
log(`coordLoss[${dataIdx}]: ${coordLoss.dataSync()}`)
36-
log(`totalLoss[${dataIdx}]: ${total}`)
37+
log(`noObjectLoss[${dataIdx}]: ${losses.noObjectLoss}`)
38+
log(`objectLoss[${dataIdx}]: ${losses.objectLoss}`)
39+
log(`coordLoss[${dataIdx}]: ${losses.coordLoss}`)
40+
log(`totalLoss[${dataIdx}]: ${losses.totalLoss}`)
3741

3842
if (window.lossMap[filenames]) {
39-
log(`loss change: ${total - window.lossMap[filenames]}`)
43+
log(`loss change: ${losses.totalLoss - window.lossMap[filenames].totalLoss}`)
4044
}
4145
}
4246

43-
window.lossMap[filenames] = total
47+
window.lossMap[filenames] = losses
4448

4549
return totalLoss
4650
}, true)

tools/train/tinyYolov2/verify.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138

139139
async function run() {
140140
$('#imgByNr').keydown(onKeyDown)
141-
const weights = await loadNetWeights('/tmp/test2.weights')
141+
const weights = await loadNetWeights('/tmp/test_n100_320_114.weights')
142142
window.net = new faceapi.TinyYolov2(true)
143143
await window.net.load(weights)
144144

0 commit comments

Comments
 (0)