Skip to content

Commit 2bedc5f

Browse files
Merge pull request justadudewhohacks#16 from justadudewhohacks/quantize-weights
Quantize weights
2 parents 9db970d + 89aac16 commit 2bedc5f

File tree

126 files changed

+8430
-17135
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+8430
-17135
lines changed

README.md

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* **[Face Recognition](#about-face-recognition)**
99
* **[Face Landmark Detection](#about-face-landmark-detection)**
1010
* **[Usage](#usage)**
11+
* **[Loading the Models](#usage-load-models)**
1112
* **[Face Detection](#usage-face-detection)**
1213
* **[Face Recognition](#usage-face-recognition)**
1314
* **[Face Landmark Detection](#usage-face-landmark-detection)**
@@ -95,19 +96,46 @@ Or install the package:
9596
npm i face-api.js
9697
```
9798

98-
<a name="usage-face-detection"></a>
99+
<a name="usage-load-models"></a>
99100

100-
### Face Detection
101+
### Loading the Models
102+
103+
To load a model, you have provide the corresponding manifest.json file as well as the model weight files (shards) as assets. Simply copy them to your public or assets folder. The manifest.json and shard files of a model have to be located in the same directory / accessible under the same route.
104+
105+
Assuming the models reside in **public/model**:
106+
107+
``` javascript
108+
const net = new faceapi.FaceDetectionNet()
109+
// accordingly for the other models:
110+
// const net = new faceapi.FaceLandmarkNet()
111+
// const net = new faceapi.FaceRecognitionNet()
101112

102-
Download the weights file from your server and initialize the net (note, that your server has to host the *face_detection_model.weights* file).
113+
await net.load('/models/face_detection_model-weights_manifest.json')
114+
// await net.load('/models/face_landmark_68_model-weights_manifest.json')
115+
// await net.load('/models/face_recognition_model-weights_manifest.json')
116+
117+
// or simply
118+
await net.load('/models')
119+
```
120+
121+
Alternatively you can load the weights as a Float32Array (in case you want to use the uncompressed models):
103122

104123
``` javascript
105-
// initialize the face detector
106-
const res = await axios.get('face_detection_model.weights', { responseType: 'arraybuffer' })
124+
// using fetch
125+
const res = await fetch('/models/face_detection_model.weights')
126+
const weights = new Float32Array(await res.arrayBuffer())
127+
net.load(weights)
128+
129+
// using axios
130+
const res = await axios.get('/models/face_detection_model.weights', { responseType: 'arraybuffer' })
107131
const weights = new Float32Array(res.data)
108-
const detectionNet = faceapi.faceDetectionNet(weights)
132+
net.load(weights)
109133
```
110134

135+
<a name="usage-face-detection"></a>
136+
137+
### Face Detection
138+
111139
Detect faces and get the bounding boxes and scores:
112140

113141
``` javascript
@@ -141,15 +169,6 @@ const { boxes, scores } = detectionNet.forward('myImg')
141169

142170
### Face Recognition
143171

144-
Download the weights file from your server and initialize the net (note, that your server has to host the *face_recognition_model.weights* file).
145-
146-
``` javascript
147-
// initialize the face recognizer
148-
const res = await axios.get('face_recognition_model.weights', { responseType: 'arraybuffer' })
149-
const weights = new Float32Array(res.data)
150-
const recognitionNet = faceapi.faceRecognitionNet(weights)
151-
```
152-
153172
Compute and compare the descriptors of two face images:
154173

155174
``` javascript
@@ -180,15 +199,6 @@ const t = recognitionNet.forward('myImg')
180199

181200
### Face Landmark Detection
182201

183-
Download the weights file from your server and initialize the net (note, that your server has to host the *face_landmark_68_model.weights* file).
184-
185-
``` javascript
186-
// initialize the face recognizer
187-
const res = await axios.get('face_landmark_68_model.weights', { responseType: 'arraybuffer' })
188-
const weights = new Float32Array(res.data)
189-
const faceLandmarkNet = faceapi.faceLandmarkNet(weights)
190-
```
191-
192202
Detect face landmarks:
193203

194204
``` javascript

dist/commons/isTensor.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as tf from '@tensorflow/tfjs-core';
2+
export declare function isTensor(tensor: tf.Tensor, dim: number): boolean;
3+
export declare function isTensor1D(tensor: tf.Tensor): boolean;
4+
export declare function isTensor2D(tensor: tf.Tensor): boolean;
5+
export declare function isTensor3D(tensor: tf.Tensor): boolean;
6+
export declare function isTensor4D(tensor: tf.Tensor): boolean;

dist/commons/isTensor.js

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

dist/commons/isTensor.js.map

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

dist/commons/loadWeightMap.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export declare function getModelUris(uri: string | undefined, defaultModelName: string): {
2+
manifestUri: string;
3+
modelBaseUri: string;
4+
};
5+
export declare function loadWeightMap(uri: string | undefined, defaultModelName: string): Promise<any>;

dist/commons/loadWeightMap.js

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

dist/commons/loadWeightMap.js.map

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

0 commit comments

Comments
 (0)