Skip to content

Commit 32cbd69

Browse files
mjkyungdmsuehir
authored andcommitted
Add FaceNet FP32 Inference (#259)
* Facenet update * Refactor model_init and place the model files in models directory * Update start.sh for FaceNet * Move Facenet to proper directory * Fix a typo * Add datafile * Add custum arg * model_init update * README.md update * Updatd licensing header * Add unit test for latency/accuracy * Add example log and unit test * Update dataset link in README.md * Fix indentation error * Update unit test command to reflect correct location * Use model-source-dir * add model-source-dir * Update dataset download instruction * Remove misplaced lines
1 parent 5f2437b commit 32cbd69

File tree

10 files changed

+559
-2
lines changed

10 files changed

+559
-2
lines changed

benchmarks/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencies to be installed:
1717
| -----------------------| --------------| ------------------- | --------- |------------------------------|
1818
| Adversarial Networks | TensorFlow | [DCGAN](https://arxiv.org/pdf/1511.06434.pdf) | Inference | [FP32](adversarial_networks/tensorflow/dcgan/README.md#fp32-inference-instructions) |
1919
| Content Creation | TensorFlow | [DRAW](https://arxiv.org/pdf/1502.04623.pdf) | Inference | [FP32](content_creation/tensorflow/draw/README.md#fp32-inference-instructions) |
20+
| Face Detection and Alignment | Tensorflow | [FaceNet](https://arxiv.org/pdf/1503.03832.pdf) | Inference | [FP32](face_detection_and_alignment/tensorflow/facenet/README.md#fp32-inference-instructions) |
2021
| Face Detection and Alignment | TensorFlow | [MTCC](https://arxiv.org/pdf/1604.02878.pdf) | Inference | [FP32](face_detection_and_alignment/tensorflow/mtcc/README.md#fp32-inference-instructions) |
2122
| Image Recognition | TensorFlow | [Inception ResNet V2](https://arxiv.org/pdf/1602.07261.pdf) | Inference | [Int8](image_recognition/tensorflow/inception_resnet_v2/README.md#int8-inference-instructions) [FP32](image_recognition/tensorflow/inception_resnet_v2/README.md#fp32-inference-instructions) |
2223
| Image Recognition | TensorFlow | [Inception V3](https://arxiv.org/pdf/1512.00567.pdf) | Inference | [Int8](image_recognition/tensorflow/inceptionv3/README.md#int8-inference-instructions) [FP32](image_recognition/tensorflow/inceptionv3/README.md#fp32-inference-instructions) |

benchmarks/common/tensorflow/start.sh

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,21 @@ function draw() {
288288
fi
289289
}
290290

291+
# FaceNet model
292+
function facenet() {
293+
if [ ${PRECISION} == "fp32" ]; then
294+
cp ${MOUNT_INTELAI_MODELS_SOURCE}/${PRECISION}/validate_on_lfw.py \
295+
${MOUNT_EXTERNAL_MODELS_SOURCE}/src/validate_on_lfw.py
296+
297+
CMD="${CMD} $(add_arg "--lfw_pairs" ${lfw_pairs})"
298+
PYTHONPATH=${PYTHONPATH}:${MOUNT_BENCHMARK}:${MOUNT_EXTERNAL_MODELS_SOURCE}
299+
PYTHONPATH=${PYTHONPATH} CMD=${CMD} run_model
300+
else
301+
echo "PRECISION=${PRECISION} is not supported for ${MODEL_NAME}"
302+
exit
303+
fi
304+
}
305+
291306
# Faster R-CNN (ResNet50) model
292307
function faster_rcnn() {
293308
export PYTHONPATH=$PYTHONPATH:${MOUNT_EXTERNAL_MODELS_SOURCE}/research:${MOUNT_EXTERNAL_MODELS_SOURCE}/research/slim
@@ -574,7 +589,7 @@ function ssd-resnet34() {
574589
if [ ${PRECISION} == "fp32" ]; then
575590
if [ ${NOINSTALL} != "True" ]; then
576591
for line in $(cat ${MOUNT_BENCHMARK}/object_detection/tensorflow/ssd-resnet34/requirements.txt)
577-
do
592+
do
578593
pip install $line
579594
done
580595
fi
@@ -669,7 +684,7 @@ function transformer_lt_official() {
669684
cp ${MOUNT_INTELAI_MODELS_SOURCE}/${MODE}/${PRECISION}/infer_ab.py \
670685
${MOUNT_EXTERNAL_MODELS_SOURCE}/official/transformer/infer_ab.py
671686

672-
CMD="${CMD}
687+
CMD="${CMD}
673688
--in_graph=${IN_GRAPH} \
674689
--vocab_file=${DATASET_LOCATION}/${vocab_file} \
675690
--file=${DATASET_LOCATION}/${file} \
@@ -776,6 +791,8 @@ if [ ${MODEL_NAME} == "dcgan" ]; then
776791
dcgan
777792
elif [ ${MODEL_NAME} == "draw" ]; then
778793
draw
794+
elif [ ${MODEL_NAME} == "facenet" ]; then
795+
facenet
779796
elif [ ${MODEL_NAME} == "faster_rcnn" ]; then
780797
faster_rcnn
781798
elif [ ${MODEL_NAME} == "gnmt" ]; then
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# FaceNet
2+
3+
This document has instructions for how to run FaceNet for the
4+
following modes/precisions:
5+
* [FP32 inference](#fp32-inference-instructions)
6+
7+
Benchmarking instructions and scripts for model training and inference
8+
other precisions are coming later.
9+
10+
## FP32 Inference Instructions
11+
12+
1. Clone the [davidsandberg/facenet](https://github.com/davidsandberg/facenet) repository:
13+
14+
```
15+
$ git clone https://github.com/davidsandberg/facenet.git
16+
```
17+
18+
2. Clone this [intelai/models](https://github.com/IntelAI/models)
19+
repository:
20+
21+
```
22+
$ git clone https://github.com/IntelAI/models.git
23+
```
24+
25+
3. Download and extract the pre-trained model.
26+
```
27+
$ wget https://storage.googleapis.com/intel-optimized-tensorflow/models/facenet_fp32_pretrained_model.tar.gz
28+
$ tar -zxvf facenet_fp32_pretrained_model.tar.gz
29+
$ ls checkpoint
30+
model-20181015-153825.ckpt-90.data-00000-of-00001 model-20181015-153825.ckpt-90.index model-20181015-153825.meta
31+
```
32+
33+
4. If you would like to run FaceNet FP32 inference, you will need the aligned LFW dataset.
34+
Instructions for downloading the dataset and converting it can be found in the documentation
35+
[here](https://github.com/davidsandberg/facenet/wiki/Validate-on-lfw), step 2 to step 4.
36+
37+
5. Navigate to the `benchmarks` directory in your local clone of
38+
the [intelai/models](https://github.com/IntelAI/models) repo from step 2.
39+
The `launch_benchmark.py` script in the `benchmarks` directory is
40+
used for starting a benchmarking run in a optimized TensorFlow docker
41+
container. It has arguments to specify which model, framework, mode,
42+
precision, and docker image.
43+
44+
Substitute in your own `--checkpoint` pretrained model file path (from step 3),
45+
and `--data-location` (from step 4).
46+
47+
FaceNet can be run for latency benchmarking, throughput
48+
benchmarking, or accuracy. Use one of the following examples below,
49+
depending on your use case.
50+
51+
* For latency (using `--batch-size 1`):
52+
53+
```
54+
python launch_benchmark.py \
55+
--model-name facenet \
56+
--precision fp32 \
57+
--mode inference \
58+
--framework tensorflow \
59+
--batch-size 1 \
60+
--socket-id 0 \
61+
--checkpoint /home/<user>/checkpoints \
62+
--data-location /home/<user>/dataset \
63+
--model-source-dir /home/<user>/facenet/ \
64+
--docker-image intelaipg/intel-optimized-tensorflow:latest-devel-mkl
65+
```
66+
Example log tail when benchmarking for latency:
67+
```
68+
Batch 979 elapsed Time 0.0297989845276
69+
Batch 989 elapsed Time 0.029657125473
70+
Batch 999 elapsed Time 0.0295519828796
71+
Batchsize: 1
72+
Time spent per BATCH: 30.1561 ms
73+
Total samples/sec: 33.1608 samples/s
74+
2019-03-28 21:00:02.725536: W tensorflow/core/kernels/queue_base.cc:277] _2_input_producer: Skipping cancelled enqueue attempt with queue not closed
75+
2019-03-28 21:00:02.725672: W tensorflow/core/kernels/queue_base.cc:277] _1_batch_join/fifo_queue: Skipping cancelled enqueue attempt with queue not closed
76+
2019-03-28 21:00:02.725683: W tensorflow/core/kernels/queue_base.cc:277] _1_batch_join/fifo_queue: Skipping cancelled enqueue attempt with queue not closed
77+
2019-03-28 21:00:02.725693: W tensorflow/core/kernels/queue_base.cc:277] _1_batch_join/fifo_queue: Skipping cancelled enqueue attempt with queue not closed
78+
2019-03-28 21:00:02.725713: W tensorflow/core/kernels/queue_base.cc:277] _1_batch_join/fifo_queue: Skipping cancelled enqueue attempt with queue not closed
79+
2019-03-28 21:00:02.725722: W tensorflow/core/kernels/queue_base.cc:277] _1_batch_join/fifo_queue: Skipping cancelled enqueue attempt with queue not closed
80+
2019-03-28 21:00:02.725746: W tensorflow/core/kernels/queue_base.cc:277] _1_batch_join/fifo_queue: Skipping cancelled enqueue attempt with queue not closed
81+
2019-03-28 21:00:02.725776: W tensorflow/core/kernels/queue_base.cc:277] _1_batch_join/fifo_queue: Skipping cancelled enqueue attempt with queue not closed
82+
lscpu_path_cmd = command -v lscpu
83+
lscpu located here: /usr/bin/lscpu
84+
Ran inference with batch size 1
85+
Log location outside container: {--output-dir value}/benchmark_facenet_inference_fp32_20190328_205911.log
86+
```
87+
88+
* For throughput (using `--batch-size 100`):
89+
90+
```
91+
python launch_benchmark.py \
92+
--model-name facenet \
93+
--precision fp32 \
94+
--mode inference \
95+
--framework tensorflow \
96+
--batch-size 100 \
97+
--socket-id 0 \
98+
--checkpoint /home/<user>/checkpoints \
99+
--data-location /home/<user>/dataset \
100+
--model-source-dir /home/<user>/facenet/ \
101+
--docker-image intelaipg/intel-optimized-tensorflow:latest-devel-mkl
102+
```
103+
Example log tail when benchmarking for throughput:
104+
```
105+
Batch 219 elapsed Time 0.446497917175
106+
Batch 229 elapsed Time 0.422048091888
107+
Batch 239 elapsed Time 0.433968067169
108+
Batchsize: 100
109+
Time spent per BATCH: 434.5414 ms
110+
Total samples/sec: 230.1277 samples/s
111+
Accuracy: 0.98833+-0.00489
112+
Validation rate: 0.96200+-0.01968 @ FAR=0.00100
113+
Area Under Curve (AUC): 0.999
114+
Equal Error Rate (EER): 0.011
115+
lscpu_path_cmd = command -v lscpu
116+
lscpu located here: /usr/bin/lscpu
117+
Ran inference with batch size 100
118+
Log location outside container: {--output-dir value}/benchmark_facenet_inference_fp32_20190329_002623.log
119+
```
120+
121+
* For accuracy (using `--accuracy-only`, and `--batch-size 100`):
122+
123+
```
124+
python launch_benchmark.py \
125+
--model-name facenet \
126+
--precision fp32 \
127+
--mode inference \
128+
--framework tensorflow \
129+
--accuracy-only \
130+
--batch-size 100 \
131+
--socket-id 0 \
132+
--checkpoint /home/<user>/checkpoints \
133+
--data-location /home/<user>/dataset \
134+
--model-source-dir /home/<user>/facenet/ \
135+
--docker-image intelaipg/intel-optimized-tensorflow:latest-devel-mkl
136+
```
137+
Example log tail when benchmarking for accuracy:
138+
```
139+
Batch 219 elapsed Time 0.398629188538
140+
Batch 229 elapsed Time 0.354953050613
141+
Batch 239 elapsed Time 0.366483926773
142+
Batchsize: 100
143+
Time spent per BATCH: 388.5419 ms
144+
Total samples/sec: 257.3725 samples/s
145+
Accuracy: 0.98833+-0.00489
146+
Validation rate: 0.96200+-0.01968 @ FAR=0.00100
147+
Area Under Curve (AUC): 0.999
148+
Equal Error Rate (EER): 0.011
149+
lscpu_path_cmd = command -v lscpu
150+
lscpu located here: /usr/bin/lscpu
151+
Ran inference with batch size 100
152+
Log location outside container: {--output-dir value}/benchmark_facenet_inference_fp32_20190328_214145.log
153+
```
154+
155+
Note that the `--verbose` or `--output-dir` flag can be added to any of the above commands
156+
to get additional debug output or change the default output location..
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2019 Intel Corporation
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: EPL-2.0
19+
#
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2019 Intel Corporation
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: EPL-2.0
19+
#
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2019 Intel Corporation
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: EPL-2.0
19+
#
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2019 Intel Corporation
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: EPL-2.0
19+
#
20+
21+
from common.base_model_init import BaseModelInitializer
22+
from common.base_model_init import set_env_var
23+
24+
import os
25+
from argparse import ArgumentParser
26+
27+
28+
class ModelInitializer(BaseModelInitializer):
29+
"""initialize mode and run benchmark for FaceNet model"""
30+
31+
def __init__(self, args, custom_args=[], platform_util=None):
32+
super(ModelInitializer, self).__init__(args, custom_args, platform_util)
33+
self.cmd = self.get_numactl_command(self.args.socket_id) + \
34+
self.python_exe + " "
35+
36+
# Set KMP env vars, if they haven't already been set
37+
self.set_kmp_vars()
38+
39+
pairs_file = os.path.join(self.args.model_source_dir,
40+
"data/pairs.txt")
41+
arg_parser = ArgumentParser(description='Parse custom args')
42+
arg_parser.add_argument(
43+
"--lfw_pairs", type=str,
44+
help="The file containing the pairs to use for validation.",
45+
dest="lfw_pairs", default=pairs_file)
46+
self.args = arg_parser.parse_args(
47+
self.custom_args, namespace=self.args)
48+
49+
# use default batch size if -1
50+
if self.args.batch_size == -1 or self.args.accuracy_only:
51+
self.args.batch_size = 100
52+
53+
# set num_inter_threads and num_intra_threads
54+
if self.args.batch_size > 32:
55+
self.set_num_inter_intra_threads(num_inter_threads=2)
56+
else:
57+
self.set_num_inter_intra_threads(num_inter_threads=1)
58+
59+
set_env_var("OMP_NUM_THREADS", self.args.num_intra_threads)
60+
61+
run_script = os.path.join(self.args.model_source_dir,
62+
"src/validate_on_lfw.py")
63+
64+
warmup_steps = 40
65+
max_steps = 1000
66+
if self.args.batch_size == 1:
67+
warmup_steps = 200
68+
69+
cmd_args = ' ' + self.args.data_location + \
70+
' ' + self.args.checkpoint + ' --distance_metric 1' + \
71+
' --use_flipped_images' + ' --subtract_mean' + \
72+
' --use_fixed_image_standardization' + \
73+
' --num_inter_threads=' + \
74+
str(self.args.num_inter_threads) + \
75+
' --num_intra_threads=' + \
76+
str(self.args.num_intra_threads) + \
77+
' --lfw_batch_size=' + str(self.args.batch_size) + \
78+
' --lfw_pairs=' + self.args.lfw_pairs + \
79+
' --warmup_steps=' + str(warmup_steps) + \
80+
' --max_steps=' + str(max_steps)
81+
82+
self.cmd = self.cmd + run_script + cmd_args
83+
84+
def run(self):
85+
"""run command to enable model benchmark or accuracy measurement"""
86+
87+
original_dir = os.getcwd()
88+
os.chdir(self.args.model_source_dir)
89+
if self.cmd:
90+
self.run_command(self.cmd)
91+
os.chdir(original_dir)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2019 Intel Corporation
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: EPL-2.0
19+
#
20+

0 commit comments

Comments
 (0)