Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
face
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Кубота
face
Commits
2b127e43
Commit
2b127e43
authored
Jun 10, 2018
by
vincent
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add centering option to padToSquare and center input face of face recognition net
parent
d7962a58
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
43 additions
and
25 deletions
+43
-25
extractFaceTensors.ts
src/extractFaceTensors.ts
+1
-1
index.ts
src/faceDetectionNet/index.ts
+3
-2
index.ts
src/faceRecognitionNet/index.ts
+3
-3
getImageTensor.ts
src/getImageTensor.ts
+0
-17
index.ts
src/index.ts
+5
-2
padToSquare.ts
src/padToSquare.ts
+31
-0
No files found.
src/extractFaceTensors.ts
View file @
2b127e43
...
@@ -2,7 +2,7 @@ import * as tf from '@tensorflow/tfjs-core';
...
@@ -2,7 +2,7 @@ import * as tf from '@tensorflow/tfjs-core';
import
{
FaceDetectionResult
}
from
'./faceDetectionNet/FaceDetectionResult'
;
import
{
FaceDetectionResult
}
from
'./faceDetectionNet/FaceDetectionResult'
;
import
{
NetInput
}
from
'./NetInput'
;
import
{
NetInput
}
from
'./NetInput'
;
import
{
getImageTensor
}
from
'./
transformInputs
'
;
import
{
getImageTensor
}
from
'./
getImageTensor
'
;
import
{
TNetInput
}
from
'./types'
;
import
{
TNetInput
}
from
'./types'
;
/**
/**
...
...
src/faceDetectionNet/index.ts
View file @
2b127e43
import
*
as
tf
from
'@tensorflow/tfjs-core'
;
import
*
as
tf
from
'@tensorflow/tfjs-core'
;
import
{
getImageTensor
}
from
'../getImageTensor'
;
import
{
NetInput
}
from
'../NetInput'
;
import
{
NetInput
}
from
'../NetInput'
;
import
{
getImageTensor
,
padToSquare
}
from
'../transformInputs
'
;
import
{
padToSquare
}
from
'../padToSquare
'
;
import
{
TNetInput
}
from
'../types'
;
import
{
TNetInput
}
from
'../types'
;
import
{
extractParams
}
from
'./extractParams'
;
import
{
extractParams
}
from
'./extractParams'
;
import
{
FaceDetectionResult
}
from
'./FaceDetectionResult'
;
import
{
FaceDetectionResult
}
from
'./FaceDetectionResult'
;
...
@@ -49,7 +50,7 @@ export function faceDetectionNet(weights: Float32Array) {
...
@@ -49,7 +50,7 @@ export function faceDetectionNet(weights: Float32Array) {
}
=
tf
.
tidy
(()
=>
{
}
=
tf
.
tidy
(()
=>
{
let
imgTensor
=
getImageTensor
(
input
)
let
imgTensor
=
getImageTensor
(
input
)
const
[
_
,
height
,
width
]
=
imgTensor
.
shape
const
[
height
,
width
]
=
imgTensor
.
shape
.
slice
(
1
)
imgTensor
=
padToSquare
(
imgTensor
)
imgTensor
=
padToSquare
(
imgTensor
)
paddedHeightRelative
=
imgTensor
.
shape
[
1
]
/
height
paddedHeightRelative
=
imgTensor
.
shape
[
1
]
/
height
...
...
src/faceRecognitionNet/index.ts
View file @
2b127e43
import
*
as
tf
from
'@tensorflow/tfjs-core'
;
import
*
as
tf
from
'@tensorflow/tfjs-core'
;
import
{
getImageTensor
}
from
'../getImageTensor'
;
import
{
NetInput
}
from
'../NetInput'
;
import
{
NetInput
}
from
'../NetInput'
;
import
{
getImageTensor
,
padToSquare
}
from
'../transformInputs
'
;
import
{
padToSquare
}
from
'../padToSquare
'
;
import
{
TNetInput
}
from
'../types'
;
import
{
TNetInput
}
from
'../types'
;
import
{
convDown
}
from
'./convLayer'
;
import
{
convDown
}
from
'./convLayer'
;
import
{
extractParams
}
from
'./extractParams'
;
import
{
extractParams
}
from
'./extractParams'
;
...
@@ -14,8 +15,7 @@ export function faceRecognitionNet(weights: Float32Array) {
...
@@ -14,8 +15,7 @@ export function faceRecognitionNet(weights: Float32Array) {
function
forward
(
input
:
tf
.
Tensor
|
NetInput
|
TNetInput
)
{
function
forward
(
input
:
tf
.
Tensor
|
NetInput
|
TNetInput
)
{
return
tf
.
tidy
(()
=>
{
return
tf
.
tidy
(()
=>
{
// TODO pad on both sides, to keep face centered
let
x
=
padToSquare
(
getImageTensor
(
input
),
true
)
let
x
=
padToSquare
(
getImageTensor
(
input
))
// work with 150 x 150 sized face images
// work with 150 x 150 sized face images
if
(
x
.
shape
[
1
]
!==
150
||
x
.
shape
[
2
]
!==
150
)
{
if
(
x
.
shape
[
1
]
!==
150
||
x
.
shape
[
2
]
!==
150
)
{
x
=
tf
.
image
.
resizeBilinear
(
x
,
[
150
,
150
])
x
=
tf
.
image
.
resizeBilinear
(
x
,
[
150
,
150
])
...
...
src/
transformInputs
.ts
→
src/
getImageTensor
.ts
View file @
2b127e43
...
@@ -3,23 +3,6 @@ import * as tf from '@tensorflow/tfjs-core';
...
@@ -3,23 +3,6 @@ import * as tf from '@tensorflow/tfjs-core';
import
{
NetInput
}
from
'./NetInput'
;
import
{
NetInput
}
from
'./NetInput'
;
import
{
TNetInput
}
from
'./types'
;
import
{
TNetInput
}
from
'./types'
;
export
function
padToSquare
(
imgTensor
:
tf
.
Tensor4D
):
tf
.
Tensor4D
{
return
tf
.
tidy
(()
=>
{
const
[
_
,
height
,
width
]
=
imgTensor
.
shape
if
(
height
===
width
)
{
return
imgTensor
}
if
(
height
>
width
)
{
const
pad
=
tf
.
fill
([
1
,
height
,
height
-
width
,
3
],
0
)
as
tf
.
Tensor4D
return
tf
.
concat
([
imgTensor
,
pad
],
2
)
}
const
pad
=
tf
.
fill
([
1
,
width
-
height
,
width
,
3
],
0
)
as
tf
.
Tensor4D
return
tf
.
concat
([
imgTensor
,
pad
],
1
)
})
}
export
function
getImageTensor
(
input
:
tf
.
Tensor
|
NetInput
|
TNetInput
):
tf
.
Tensor4D
{
export
function
getImageTensor
(
input
:
tf
.
Tensor
|
NetInput
|
TNetInput
):
tf
.
Tensor4D
{
return
tf
.
tidy
(()
=>
{
return
tf
.
tidy
(()
=>
{
if
(
input
instanceof
tf
.
Tensor
)
{
if
(
input
instanceof
tf
.
Tensor
)
{
...
...
src/index.ts
View file @
2b127e43
import
*
as
tf
from
'@tensorflow/tfjs-core'
;
import
{
euclideanDistance
}
from
'./euclideanDistance'
;
import
{
euclideanDistance
}
from
'./euclideanDistance'
;
import
{
faceDetectionNet
}
from
'./faceDetectionNet'
;
import
{
faceDetectionNet
}
from
'./faceDetectionNet'
;
import
{
faceRecognitionNet
}
from
'./faceRecognitionNet'
;
import
{
faceRecognitionNet
}
from
'./faceRecognitionNet'
;
import
{
NetInput
}
from
'./NetInput'
;
import
{
NetInput
}
from
'./NetInput'
;
import
*
as
tf
from
'@tensorflow/tfjs-co
re'
;
import
{
padToSquare
}
from
'./padToSqua
re'
;
export
{
export
{
euclideanDistance
,
euclideanDistance
,
faceDetectionNet
,
faceDetectionNet
,
faceRecognitionNet
,
faceRecognitionNet
,
NetInput
,
NetInput
,
tf
tf
,
padToSquare
}
}
export
*
from
'./extractFaces'
export
*
from
'./extractFaces'
...
...
src/padToSquare.ts
0 → 100644
View file @
2b127e43
import
*
as
tf
from
'@tensorflow/tfjs-core'
;
/**
* Pads the smaller dimension of an image tensor with zeros, such that width === height.
*
* @param imgTensor The image tensor.
* @param isCenterImage (optional, default: false) If true, add padding on both sides of the image, such that the image
* @returns The padded tensor with width === height.
*/
export
function
padToSquare
(
imgTensor
:
tf
.
Tensor4D
,
isCenterImage
:
boolean
=
false
):
tf
.
Tensor4D
{
return
tf
.
tidy
(()
=>
{
const
[
height
,
width
]
=
imgTensor
.
shape
.
slice
(
1
)
if
(
height
===
width
)
{
return
imgTensor
}
const
paddingAmount
=
Math
.
floor
(
Math
.
abs
(
height
-
width
)
*
(
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
[]
return
tf
.
concat
(
tensorsToStack
,
paddingAxis
)
})
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment