Skip to content

Commit 2230107

Browse files
make padToSquare padding order append first when size is uneven + fixed issue when dimensions differ by 1 only
1 parent d68e848 commit 2230107

File tree

7 files changed

+113
-30
lines changed

7 files changed

+113
-30
lines changed

build/padToSquare.js

Lines changed: 15 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/padToSquare.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/face-api.js

Lines changed: 15 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/face-api.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/face-api.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/padToSquare.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,28 @@ export function padToSquare(
2121
}
2222

2323
const dimDiff = Math.abs(height - width)
24-
const paddingAmount = Math.floor(dimDiff * (isCenterImage ? 0.5 : 1))
24+
const paddingAmount = Math.round(dimDiff * (isCenterImage ? 0.5 : 1))
2525
const paddingAxis = height > width ? 2 : 1
2626

27-
const getPaddingTensorShape = (isRoundUp: boolean = false): number[] => {
27+
const createPaddingTensor = (paddingAmount: number): tf.Tensor => {
2828
const paddingTensorShape = imgTensor.shape.slice()
29-
paddingTensorShape[paddingAxis] = paddingAmount + (isRoundUp ? 1 : 0)
30-
return paddingTensorShape
29+
paddingTensorShape[paddingAxis] = paddingAmount
30+
return tf.fill(paddingTensorShape, 0)
3131
}
3232

33-
const tensorsToStack = (isCenterImage ? [tf.fill(getPaddingTensorShape(!isEven(dimDiff)), 0)] : [])
34-
.concat([imgTensor, tf.fill(getPaddingTensorShape(), 0)]) as tf.Tensor4D[]
33+
const paddingTensorAppend = createPaddingTensor(paddingAmount)
34+
const remainingPaddingAmount = dimDiff - paddingTensorAppend.shape[paddingAxis]
35+
36+
const paddingTensorPrepend = isCenterImage && remainingPaddingAmount
37+
? createPaddingTensor(remainingPaddingAmount)
38+
: null
39+
40+
const tensorsToStack = [
41+
paddingTensorPrepend,
42+
imgTensor,
43+
paddingTensorAppend
44+
]
45+
.filter(t => t !== null) as tf.Tensor4D[]
3546
return tf.concat(tensorsToStack, paddingAxis)
3647
})
3748
}

test/tests/padToSquare.test.ts

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('padToSquare', () => {
77

88
describe('even size', () => {
99

10-
it('is padded to square', () => tf.tidy(() => {
10+
it('is padded to square by 2 columns', () => tf.tidy(() => {
1111
const imgTensor = tf.tensor4d(Array(24).fill(1), [1, 4, 2, 3])
1212
const result = padToSquare(imgTensor)
1313

@@ -21,7 +21,7 @@ describe('padToSquare', () => {
2121
expect(paddedCols[3].dataSync()).toEqual(zeros(12))
2222
}))
2323

24-
it('is padded to square and centered', () => tf.tidy(() => {
24+
it('is padded to square by 2 columns and centered', () => tf.tidy(() => {
2525
const imgTensor = tf.tensor4d(Array(24).fill(1), [1, 4, 2, 3])
2626
const result = padToSquare(imgTensor, true)
2727

@@ -35,11 +35,39 @@ describe('padToSquare', () => {
3535
expect(paddedCols[3].dataSync()).toEqual(zeros(12))
3636
}))
3737

38+
it('is padded to square by 1 column', () => tf.tidy(() => {
39+
const imgTensor = tf.tensor4d(Array(36).fill(1), [1, 4, 3, 3])
40+
const result = padToSquare(imgTensor)
41+
42+
expect(result.shape).toEqual([1, 4, 4, 3])
43+
44+
const paddedCols = tf.unstack(result, 2)
45+
expect(paddedCols.length).toEqual(4)
46+
expect(paddedCols[0].dataSync()).toEqual(ones(12))
47+
expect(paddedCols[1].dataSync()).toEqual(ones(12))
48+
expect(paddedCols[2].dataSync()).toEqual(ones(12))
49+
expect(paddedCols[3].dataSync()).toEqual(zeros(12))
50+
}))
51+
52+
it('is padded to square by 1 column and centered', () => tf.tidy(() => {
53+
const imgTensor = tf.tensor4d(Array(36).fill(1), [1, 4, 3, 3])
54+
const result = padToSquare(imgTensor, true)
55+
56+
expect(result.shape).toEqual([1, 4, 4, 3])
57+
58+
const paddedCols = tf.unstack(result, 2)
59+
expect(paddedCols.length).toEqual(4)
60+
expect(paddedCols[0].dataSync()).toEqual(ones(12))
61+
expect(paddedCols[1].dataSync()).toEqual(ones(12))
62+
expect(paddedCols[2].dataSync()).toEqual(ones(12))
63+
expect(paddedCols[3].dataSync()).toEqual(zeros(12))
64+
}))
65+
3866
})
3967

4068
describe('uneven size', () => {
4169

42-
it('is padded to square', () => tf.tidy(() => {
70+
it('is padded to square by 3 columns', () => tf.tidy(() => {
4371
const imgTensor = tf.tensor4d(Array(30).fill(1), [1, 5, 2, 3])
4472
const result = padToSquare(imgTensor)
4573

@@ -54,17 +82,46 @@ describe('padToSquare', () => {
5482
expect(paddedCols[4].dataSync()).toEqual(zeros(15))
5583
}))
5684

57-
it('is padded to square and centered', () => tf.tidy(() => {
85+
it('is padded to square by 3 columns and centered', () => tf.tidy(() => {
5886
const imgTensor = tf.tensor4d(Array(30).fill(1), [1, 5, 2, 3])
5987
const result = padToSquare(imgTensor, true)
6088

6189
expect(result.shape).toEqual([1, 5, 5, 3])
62-
const data = result.dataSync()
6390

6491
const paddedCols = tf.unstack(result, 2)
6592
expect(paddedCols.length).toEqual(5)
6693
expect(paddedCols[0].dataSync()).toEqual(zeros(15))
67-
expect(paddedCols[1].dataSync()).toEqual(zeros(15))
94+
expect(paddedCols[1].dataSync()).toEqual(ones(15))
95+
expect(paddedCols[2].dataSync()).toEqual(ones(15))
96+
expect(paddedCols[3].dataSync()).toEqual(zeros(15))
97+
expect(paddedCols[4].dataSync()).toEqual(zeros(15))
98+
}))
99+
100+
it('is padded to square by 1 column', () => tf.tidy(() => {
101+
const imgTensor = tf.tensor4d(Array(60).fill(1), [1, 5, 4, 3])
102+
const result = padToSquare(imgTensor)
103+
104+
expect(result.shape).toEqual([1, 5, 5, 3])
105+
106+
const paddedCols = tf.unstack(result, 2)
107+
expect(paddedCols.length).toEqual(5)
108+
expect(paddedCols[0].dataSync()).toEqual(ones(15))
109+
expect(paddedCols[1].dataSync()).toEqual(ones(15))
110+
expect(paddedCols[2].dataSync()).toEqual(ones(15))
111+
expect(paddedCols[3].dataSync()).toEqual(ones(15))
112+
expect(paddedCols[4].dataSync()).toEqual(zeros(15))
113+
}))
114+
115+
it('is padded to square by 1 column and centered', () => tf.tidy(() => {
116+
const imgTensor = tf.tensor4d(Array(60).fill(1), [1, 5, 4, 3])
117+
const result = padToSquare(imgTensor, true)
118+
119+
expect(result.shape).toEqual([1, 5, 5, 3])
120+
121+
const paddedCols = tf.unstack(result, 2)
122+
expect(paddedCols.length).toEqual(5)
123+
expect(paddedCols[0].dataSync()).toEqual(ones(15))
124+
expect(paddedCols[1].dataSync()).toEqual(ones(15))
68125
expect(paddedCols[2].dataSync()).toEqual(ones(15))
69126
expect(paddedCols[3].dataSync()).toEqual(ones(15))
70127
expect(paddedCols[4].dataSync()).toEqual(zeros(15))

0 commit comments

Comments
 (0)