@@ -18,10 +18,13 @@ function assignBoxesToAnchors(groundTruthBoxes, reshapedImgDims) {
18
18
const numCells = getNumCells ( inputSize )
19
19
20
20
return groundTruthBoxes . map ( box => {
21
- const { left : x , top : y , width, height } = box . rescale ( reshapedImgDims )
21
+ const { left, top, width, height } = box . rescale ( reshapedImgDims )
22
22
23
- const row = Math . round ( ( y / inputSize ) * numCells )
24
- const col = Math . round ( ( x / inputSize ) * numCells )
23
+ const ctX = left + ( width / 2 )
24
+ const ctY = top + ( height / 2 )
25
+
26
+ const col = Math . floor ( ( ctX / inputSize ) * numCells )
27
+ const row = Math . floor ( ( ctY / inputSize ) * numCells )
25
28
26
29
const anchorsByIou = getAnchors ( ) . map ( ( anchor , idx ) => ( {
27
30
idx,
@@ -92,11 +95,13 @@ function computeBoxAdjustments(groundTruthBoxes, reshapedImgDims) {
92
95
const centerX = ( left + right ) / 2
93
96
const centerY = ( top + bottom ) / 2
94
97
95
- const dCenterX = centerX - ( col * CELL_SIZE + ( CELL_SIZE / 2 ) )
96
- const dCenterY = centerY - ( row * CELL_SIZE + ( CELL_SIZE / 2 ) )
98
+ //const dCenterX = centerX - (col * CELL_SIZE + (CELL_SIZE / 2))
99
+ //const dCenterY = centerY - (row * CELL_SIZE + (CELL_SIZE / 2))
100
+ const dCenterX = centerX - ( col * CELL_SIZE )
101
+ const dCenterY = centerY - ( row * CELL_SIZE )
97
102
98
- const dx = inverseSigmoid ( dCenterX / inputSize )
99
- const dy = inverseSigmoid ( dCenterY / inputSize )
103
+ const dx = inverseSigmoid ( dCenterX / CELL_SIZE )
104
+ const dy = inverseSigmoid ( dCenterY / CELL_SIZE )
100
105
const dw = Math . log ( ( width / CELL_SIZE ) / getAnchors ( ) [ anchor ] . x )
101
106
const dh = Math . log ( ( height / CELL_SIZE ) / getAnchors ( ) [ anchor ] . y )
102
107
@@ -134,13 +139,14 @@ function computeIous(predBoxes, groundTruthBoxes, reshapedImgDims) {
134
139
135
140
const iou = faceapi . iou (
136
141
box . rescale ( reshapedImgDims ) ,
137
- predBox . box
142
+ predBox . box . rescale ( reshapedImgDims )
138
143
)
139
144
140
145
if ( window . debug ) {
141
- console . log ( 'ground thruth box:' , box . rescale ( reshapedImgDims ) )
142
- console . log ( 'predicted box:' , predBox . box )
143
- console . log ( iou )
146
+ console . log ( 'ground thruth box:' , box . rescale ( reshapedImgDims ) . toRect ( ) )
147
+ console . log ( 'predicted box:' , predBox . box . rescale ( reshapedImgDims ) . toRect ( ) )
148
+ console . log ( 'predicted score:' , predBox . score )
149
+ console . log ( 'iou:' , iou )
144
150
}
145
151
146
152
const anchorOffset = anchor * 5
@@ -164,31 +170,6 @@ function computeObjectLoss(outTensor, groundTruthBoxes, reshapedImgDims, padding
164
170
{ paddings }
165
171
)
166
172
167
- if ( window . debug ) {
168
- console . log ( predBoxes )
169
- console . log ( predBoxes . filter ( b => b . score > 0.1 ) )
170
- }
171
-
172
- // debug
173
-
174
- const numCells = getNumCells ( Math . max ( reshapedImgDims . width , reshapedImgDims . height ) )
175
- if ( predBoxes . length !== ( numCells * numCells * getAnchors ( ) . length ) ) {
176
- console . log ( predBoxes . length )
177
- throw new Error ( 'predBoxes.length !== (numCells * numCells * 25)' )
178
- }
179
-
180
- const isInvalid = num => ! num && num !== 0
181
-
182
-
183
- predBoxes . forEach ( ( { row, col, anchor } ) => {
184
- if ( [ row , col , anchor ] . some ( isInvalid ) ) {
185
- console . log ( row , col , anchor )
186
- throw new Error ( 'row, col, anchor invalid' )
187
- }
188
- } )
189
-
190
- // debug
191
-
192
173
const ious = computeIous (
193
174
predBoxes ,
194
175
groundTruthBoxes ,
@@ -208,7 +189,6 @@ function computeCoordLoss(groundTruthBoxes, outTensor, reshapedImgDims, mask, pa
208
189
reshapedImgDims
209
190
)
210
191
211
- // debug
212
192
if ( window . debug ) {
213
193
const indToPos = [ ]
214
194
const numCells = outTensor . shape [ 1 ]
@@ -220,40 +200,39 @@ function computeCoordLoss(groundTruthBoxes, outTensor, reshapedImgDims, mask, pa
220
200
}
221
201
}
222
202
223
- const m = Array . from ( mask . dataSync ( ) )
224
- const ind = m . map ( ( val , ind ) => ( { val, ind } ) ) . filter ( v => v . val !== 0 ) . map ( v => v . ind )
203
+ const indices = Array . from ( mask . dataSync ( ) ) . map ( ( val , ind ) => ( { val, ind } ) ) . filter ( v => v . val !== 0 ) . map ( v => v . ind )
225
204
const gt = Array . from ( boxAdjustments . dataSync ( ) )
226
205
const out = Array . from ( outTensor . dataSync ( ) )
227
206
228
- const comp = ind . map ( i => (
207
+ const comp = indices . map ( i => (
229
208
{
230
209
pos : indToPos [ i ] ,
231
210
gt : gt [ i ] ,
232
211
out : out [ i ]
233
212
}
234
213
) )
235
- console . log ( comp )
236
214
console . log ( comp . map ( c => `gt: ${ c . gt } , out: ${ c . out } ` ) )
237
215
238
- const printBbox = ( which ) => {
239
- const { col , row , anchor } = comp [ 0 ] . pos
240
- console . log ( col , row , anchor )
216
+ const getBbox = ( which ) => {
217
+ const { row , col , anchor } = comp [ 0 ] . pos
218
+
241
219
const ctX = ( ( col + faceapi . sigmoid ( comp [ 0 ] [ which ] ) ) / numCells ) * paddings . x
242
220
const ctY = ( ( row + faceapi . sigmoid ( comp [ 1 ] [ which ] ) ) / numCells ) * paddings . y
243
221
const width = ( ( Math . exp ( comp [ 2 ] [ which ] ) * getAnchors ( ) [ anchor ] . x ) / numCells ) * paddings . x
244
222
const height = ( ( Math . exp ( comp [ 3 ] [ which ] ) * getAnchors ( ) [ anchor ] . y ) / numCells ) * paddings . y
245
223
246
224
const x = ( ctX - ( width / 2 ) )
247
225
const y = ( ctY - ( height / 2 ) )
248
- console . log ( which , x * reshapedImgDims . width , y * reshapedImgDims . height , width * reshapedImgDims . width , height * reshapedImgDims . height )
249
- }
250
-
251
226
252
- printBbox ( 'out' )
253
- printBbox ( 'gt' )
227
+ return new faceapi . BoundingBox ( x , y , x + width , y + height )
228
+ }
254
229
230
+ const outRect = getBbox ( 'out' ) . rescale ( reshapedImgDims ) . toRect ( )
231
+ const gtRect = getBbox ( 'gt' ) . rescale ( reshapedImgDims ) . toRect ( )
232
+ console . log ( 'out' , outRect )
233
+ console . log ( 'gtRect' , gtRect )
255
234
}
256
- // debug
235
+
257
236
258
237
const lossTensor = tf . sub ( boxAdjustments , outTensor )
259
238
0 commit comments