@@ -4,6 +4,7 @@ import { Point } from '../Point';
4
4
import { BoundingBox } from './BoundingBox' ;
5
5
import { CELL_SIZE , CELL_STRIDE } from './config' ;
6
6
import { nms } from './nms' ;
7
+ import { normalize } from './normalize' ;
7
8
import { PNet } from './PNet' ;
8
9
import { PNetParams } from './types' ;
9
10
@@ -12,13 +13,12 @@ function rescaleAndNormalize(x: tf.Tensor4D, scale: number): tf.Tensor4D {
12
13
13
14
const [ height , width ] = x . shape . slice ( 1 )
14
15
const resized = tf . image . resizeBilinear ( x , [ Math . floor ( height * scale ) , Math . floor ( width * scale ) ] )
15
- const normalized = tf . mul ( tf . sub ( resized , tf . scalar ( 127.5 ) ) , tf . scalar ( 0.0078125 ) )
16
+ const normalized = normalize ( resized )
16
17
17
18
return ( tf . transpose ( normalized , [ 0 , 2 , 1 , 3 ] ) as tf . Tensor4D )
18
19
} )
19
20
}
20
21
21
-
22
22
function extractBoundingBoxes (
23
23
scoresTensor : tf . Tensor2D ,
24
24
regionsTensor : tf . Tensor3D ,
@@ -38,10 +38,10 @@ function extractBoundingBoxes(
38
38
39
39
const boundingBoxes = indices . map ( idx => {
40
40
const cell = new BoundingBox (
41
- Math . round ( ( idx . x * CELL_STRIDE + 1 ) / scale ) ,
42
41
Math . round ( ( idx . y * CELL_STRIDE + 1 ) / scale ) ,
43
- Math . round ( ( idx . x * CELL_STRIDE + CELL_SIZE ) / scale ) ,
44
- Math . round ( ( idx . y * CELL_STRIDE + CELL_SIZE ) / scale )
42
+ Math . round ( ( idx . x * CELL_STRIDE + 1 ) / scale ) ,
43
+ Math . round ( ( idx . y * CELL_STRIDE + CELL_SIZE ) / scale ) ,
44
+ Math . round ( ( idx . x * CELL_STRIDE + CELL_SIZE ) / scale )
45
45
)
46
46
47
47
const score = scoresTensor . get ( idx . y , idx . x )
@@ -63,14 +63,22 @@ function extractBoundingBoxes(
63
63
return boundingBoxes
64
64
}
65
65
66
- export function stage1 ( imgTensor : tf . Tensor4D , scales : number [ ] , scoreThreshold : number , params : PNetParams ) {
66
+ export function stage1 (
67
+ imgTensor : tf . Tensor4D ,
68
+ scales : number [ ] ,
69
+ scoreThreshold : number ,
70
+ params : PNetParams
71
+ ) {
67
72
68
- const boxesForScale = scales . map ( ( scale ) => {
73
+ const boxesForScale = scales . map ( ( scale , i ) => {
69
74
70
75
const { scoresTensor, regionsTensor } = tf . tidy ( ( ) => {
71
76
const resized = rescaleAndNormalize ( imgTensor , scale )
77
+
78
+
72
79
const { prob, regions } = PNet ( resized , params )
73
80
81
+
74
82
const scores = tf . unstack ( prob , 3 ) [ 1 ]
75
83
const [ sh , sw ] = scores . shape . slice ( 1 )
76
84
const [ rh , rw ] = regions . shape . slice ( 1 )
@@ -120,9 +128,9 @@ export function stage1(imgTensor: tf.Tensor4D, scales: number[], scoreThreshold:
120
128
. map ( ( { cell, region, score } ) => ( {
121
129
box : new BoundingBox (
122
130
cell . left + ( region . left * cell . width ) ,
123
- cell . right + ( region . right * cell . width ) ,
124
131
cell . top + ( region . top * cell . height ) ,
125
- cell . bottom + ( region . bottom * cell . height ) ,
132
+ cell . right + ( region . right * cell . width ) ,
133
+ cell . bottom + ( region . bottom * cell . height )
126
134
) . toSquare ( ) . round ( ) ,
127
135
score
128
136
} ) )
0 commit comments