Commit cc63d787 by vincent

init karma + jasmine

parent be40997b
module.exports = function(config) {
config.set({
frameworks: ['jasmine', 'karma-typescript'],
files: [
'src/**/*.ts',
'test/**/*.ts'
],
preprocessors: {
'**/*.ts': ['karma-typescript']
},
karmaTypescriptConfig: {
tsconfig: 'tsconfig.test.json'
},
browsers: ['Chrome']
})
}
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -7,7 +7,8 @@
"scripts": {
"rollup-min": "rollup -c rollup.config.js --environment minify:true",
"rollup": "rollup -c rollup.config.js",
"build": "npm run rollup && npm run rollup-min && tsc"
"build": "npm run rollup && npm run rollup-min && tsc",
"test": "karma start"
},
"keywords": [
"face",
......@@ -22,7 +23,13 @@
"@tensorflow/tfjs-core": "^0.11.0"
},
"devDependencies": {
"@types/jasmine": "^2.8.8",
"@types/node": "^10.1.1",
"jasmine-core": "^3.1.0",
"karma": "^2.0.3",
"karma-chrome-launcher": "^2.2.0",
"karma-jasmine": "^1.1.2",
"karma-typescript": "^3.0.12",
"rollup": "^0.59.1",
"rollup-plugin-commonjs": "^9.1.3",
"rollup-plugin-node-resolve": "^3.3.0",
......
import * as tf from '@tensorflow/tfjs-core';
import { isEven } from './utils';
/**
* Pads the smaller dimension of an image tensor with zeros, such that width === height.
*
......@@ -18,13 +20,18 @@ export function padToSquare(
return imgTensor
}
const paddingAmount = Math.floor(Math.abs(height - width) * (isCenterImage ? 0.5 : 1))
const dimDiff = Math.abs(height - width)
const paddingAmount = Math.floor(dimDiff * (isCenterImage ? 0.5 : 1))
const paddingAxis = height > width ? 2 : 1
const paddingTensorShape = imgTensor.shape.slice() as [number, number, number, number]
paddingTensorShape[paddingAxis] = paddingAmount
const tensorsToStack = (isCenterImage ? [tf.fill(paddingTensorShape, 0)] : [])
.concat([imgTensor, tf.fill(paddingTensorShape, 0)]) as tf.Tensor4D[]
const getPaddingTensorShape = (isRoundUp: boolean = false): number[] => {
const paddingTensorShape = imgTensor.shape.slice()
paddingTensorShape[paddingAxis] = paddingAmount + (isRoundUp ? 1 : 0)
return paddingTensorShape
}
const tensorsToStack = (isCenterImage ? [tf.fill(getPaddingTensorShape(!isEven(dimDiff)), 0)] : [])
.concat([imgTensor, tf.fill(getPaddingTensorShape(), 0)]) as tf.Tensor4D[]
return tf.concat(tensorsToStack, paddingAxis)
})
}
\ No newline at end of file
......@@ -9,6 +9,10 @@ export function isFloat(num: number) {
return num % 1 !== 0
}
export function isEven(num: number) {
return num % 2 === 0
}
export function round(num: number) {
return Math.floor(num * 100) / 100
}
......
import * as tf from '@tensorflow/tfjs-core';
import { padToSquare } from '../src/padToSquare';
import { ones, zeros } from './utils';
describe('padToSquare', () => {
describe('even size', () => {
it('is padded to square', () => tf.tidy(() => {
const imgTensor = tf.tensor4d(Array(24).fill(1), [1, 4, 2, 3])
const result = padToSquare(imgTensor)
expect(result.shape).toEqual([1, 4, 4, 3])
const paddedCols = tf.unstack(result, 2)
expect(paddedCols.length).toEqual(4)
expect(paddedCols[0].dataSync()).toEqual(ones(12))
expect(paddedCols[1].dataSync()).toEqual(ones(12))
expect(paddedCols[2].dataSync()).toEqual(zeros(12))
expect(paddedCols[3].dataSync()).toEqual(zeros(12))
}))
it('is padded to square and centered', () => tf.tidy(() => {
const imgTensor = tf.tensor4d(Array(24).fill(1), [1, 4, 2, 3])
const result = padToSquare(imgTensor, true)
expect(result.shape).toEqual([1, 4, 4, 3])
const paddedCols = tf.unstack(result, 2)
expect(paddedCols.length).toEqual(4)
expect(paddedCols[0].dataSync()).toEqual(zeros(12))
expect(paddedCols[1].dataSync()).toEqual(ones(12))
expect(paddedCols[2].dataSync()).toEqual(ones(12))
expect(paddedCols[3].dataSync()).toEqual(zeros(12))
}))
})
describe('uneven size', () => {
it('is padded to square', () => tf.tidy(() => {
const imgTensor = tf.tensor4d(Array(30).fill(1), [1, 5, 2, 3])
const result = padToSquare(imgTensor)
expect(result.shape).toEqual([1, 5, 5, 3])
const paddedCols = tf.unstack(result, 2)
expect(paddedCols.length).toEqual(5)
expect(paddedCols[0].dataSync()).toEqual(ones(15))
expect(paddedCols[1].dataSync()).toEqual(ones(15))
expect(paddedCols[2].dataSync()).toEqual(zeros(15))
expect(paddedCols[3].dataSync()).toEqual(zeros(15))
expect(paddedCols[4].dataSync()).toEqual(zeros(15))
}))
it('is padded to square and centered', () => tf.tidy(() => {
const imgTensor = tf.tensor4d(Array(30).fill(1), [1, 5, 2, 3])
const result = padToSquare(imgTensor, true)
expect(result.shape).toEqual([1, 5, 5, 3])
const data = result.dataSync()
const paddedCols = tf.unstack(result, 2)
expect(paddedCols.length).toEqual(5)
expect(paddedCols[0].dataSync()).toEqual(zeros(15))
expect(paddedCols[1].dataSync()).toEqual(zeros(15))
expect(paddedCols[2].dataSync()).toEqual(ones(15))
expect(paddedCols[3].dataSync()).toEqual(ones(15))
expect(paddedCols[4].dataSync()).toEqual(zeros(15))
}))
})
})
export function zeros(length: number): Float32Array {
return new Float32Array(length)
}
export function ones(length: number): Float32Array {
return new Float32Array(length).fill(1)
}
\ No newline at end of file
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs"
},
"include": [
"src",
"test"
]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment