Skip to content

Commit 084cd30

Browse files
some fixes in stage1
1 parent 9ffe1cc commit 084cd30

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/mtcnn/stage1.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Point } from '../Point';
44
import { BoundingBox } from './BoundingBox';
55
import { CELL_SIZE, CELL_STRIDE } from './config';
66
import { nms } from './nms';
7+
import { normalize } from './normalize';
78
import { PNet } from './PNet';
89
import { PNetParams } from './types';
910

@@ -12,13 +13,12 @@ function rescaleAndNormalize(x: tf.Tensor4D, scale: number): tf.Tensor4D {
1213

1314
const [height, width] = x.shape.slice(1)
1415
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)
1617

1718
return (tf.transpose(normalized, [0, 2, 1, 3]) as tf.Tensor4D)
1819
})
1920
}
2021

21-
2222
function extractBoundingBoxes(
2323
scoresTensor: tf.Tensor2D,
2424
regionsTensor: tf.Tensor3D,
@@ -38,10 +38,10 @@ function extractBoundingBoxes(
3838

3939
const boundingBoxes = indices.map(idx => {
4040
const cell = new BoundingBox(
41-
Math.round((idx.x * CELL_STRIDE + 1) / scale),
4241
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)
4545
)
4646

4747
const score = scoresTensor.get(idx.y, idx.x)
@@ -63,14 +63,22 @@ function extractBoundingBoxes(
6363
return boundingBoxes
6464
}
6565

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+
) {
6772

68-
const boxesForScale = scales.map((scale) => {
73+
const boxesForScale = scales.map((scale, i) => {
6974

7075
const { scoresTensor, regionsTensor } = tf.tidy(() => {
7176
const resized = rescaleAndNormalize(imgTensor, scale)
77+
78+
7279
const { prob, regions } = PNet(resized, params)
7380

81+
7482
const scores = tf.unstack(prob, 3)[1]
7583
const [sh, sw] = scores.shape.slice(1)
7684
const [rh, rw] = regions.shape.slice(1)
@@ -120,9 +128,9 @@ export function stage1(imgTensor: tf.Tensor4D, scales: number[], scoreThreshold:
120128
.map(({ cell, region, score }) => ({
121129
box: new BoundingBox(
122130
cell.left + (region.left * cell.width),
123-
cell.right + (region.right * cell.width),
124131
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)
126134
).toSquare().round(),
127135
score
128136
}))

0 commit comments

Comments
 (0)