1
1
const batchIdx = 0
2
2
3
- function minimize ( groundTruthBoxes , batchInput , inputSize , batch ) {
3
+ function minimize ( groundTruthBoxes , batchInput , inputSize , batch , { reshapedImgDims , paddings } ) {
4
4
const filename = batch . filenames [ batchIdx ]
5
5
const { dataIdx } = batch
6
6
@@ -16,8 +16,8 @@ function minimize(groundTruthBoxes, batchInput, inputSize, batch) {
16
16
} = computeLoss (
17
17
outTensor ,
18
18
groundTruthBoxes ,
19
- batchInput . getReshapedInputDimensions ( batchIdx ) ,
20
- batchInput . getRelativePaddings ( batchIdx )
19
+ reshapedImgDims ,
20
+ paddings
21
21
)
22
22
23
23
const losses = {
@@ -47,6 +47,35 @@ function minimize(groundTruthBoxes, batchInput, inputSize, batch) {
47
47
} , true )
48
48
}
49
49
50
+ function imageToSquare ( img ) {
51
+ const scale = 608 / Math . max ( img . height , img . width )
52
+ const width = scale * img . width
53
+ const height = scale * img . height
54
+
55
+ const canvas1 = faceapi . createCanvasFromMedia ( img )
56
+ const targetCanvas = faceapi . createCanvas ( { width : 608 , height : 608 } )
57
+ targetCanvas . getContext ( '2d' ) . putImageData ( canvas1 . getContext ( '2d' ) . getImageData ( 0 , 0 , width , height ) , 0 , 0 )
58
+ return targetCanvas
59
+ }
60
+
61
+ function getPaddingsAndReshapedSize ( img , inputSize ) {
62
+ const [ h , w ] = [ img . height , img . width ]
63
+ const maxDim = Math . max ( h , w )
64
+
65
+ const f = inputSize / maxDim
66
+ const reshapedImgDims = {
67
+ height : Math . floor ( h * f ) ,
68
+ width : Math . floor ( w * f )
69
+ }
70
+
71
+ const paddings = new faceapi . Point (
72
+ maxDim / img . width ,
73
+ maxDim / img . height
74
+ )
75
+
76
+ return { paddings, reshapedImgDims }
77
+ }
78
+
50
79
async function trainStep ( batchCreators , inputSizes , rescaleEveryNthBatch , onBatchProcessed = ( ) => { } ) {
51
80
52
81
async function step ( currentBatchCreators ) {
@@ -61,7 +90,11 @@ async function trainStep(batchCreators, inputSizes, rescaleEveryNthBatch, onBatc
61
90
const batch = await batchCreator ( )
62
91
const { imgs, groundTruthBoxes, filenames, dataIdx } = batch
63
92
64
- const batchInput = await faceapi . toNetInput ( imgs )
93
+ const img = imgs [ 0 ]
94
+ const { reshapedImgDims, paddings } = getPaddingsAndReshapedSize ( img , inputSize )
95
+ const squareImg = imageToSquare ( img )
96
+
97
+ const batchInput = await faceapi . toNetInput ( squareImg )
65
98
66
99
const [ imgHeight , imgWidth ] = batchInput . inputs [ batchIdx ] . shape
67
100
@@ -90,7 +123,8 @@ async function trainStep(batchCreators, inputSizes, rescaleEveryNthBatch, onBatc
90
123
}
91
124
92
125
let ts = Date . now ( )
93
- const loss = minimize ( filteredGroundTruthBoxes , batchInput , inputSize , batch )
126
+ const loss = minimize ( filteredGroundTruthBoxes , batchInput , inputSize , batch , { reshapedImgDims, paddings } )
127
+
94
128
ts = Date . now ( ) - ts
95
129
if ( window . logTrainSteps ) {
96
130
log ( `trainStep time for dataIdx ${ dataIdx } (${ inputSize } ): ${ ts } ms` )
0 commit comments