一、 神经网络推理
1.1、eIQ Auto框架介绍
恩智浦eIQ™ Auto深度学习(DL)工具包让开发人员能够将DL算法引入到自己的应用中,并不断满足汽车标准。工具包中的eIQ Auto软件和随附工具可帮助开发人员从开发环境快速轻松地迁移到在汽车级嵌入式处理器中全面实施AI应用。如下图所示:
1.2、TensorFlow 模型训练
使用 TF-Slim 模块中的 cifarnet 模块定义:
之所以舍去 lrn ,是由于目前 eIQ 中并不支持这个 op。
先看一下相关的训练过程:
1)定路径
# Where the checkpoint and logs will be saved to.
TRAIN_DIR=/media/jcq/Soft/Tensorflow/tensorflow-models/research/slim/tmp/cifarnet-model
# Where the dataset is saved to.
DATASET_DIR=/media/jcq/Soft/Tensorflow/tensorflow-models/research/slim/tmp/cifar10
2)
# Download the dataset
python download_and_convert_data.py \
--dataset_name=cifar10 \
--dataset_dir=${DATASET_DIR}
3)
# Run training.
python train_image_classifier.py \
--train_dir=${TRAIN_DIR} \
--dataset_name=cifar10 \
--dataset_split_name=train \
--dataset_dir=${DATASET_DIR} \
--model_name=cifarnet \
--preprocessing_name=cifarnet \
--max_number_of_steps=5000 \
--batch_size=16 \
--save_interval_secs=120 \
--save_summaries_secs=120 \
--log_every_n_steps=100 \
--optimizer=sgd \
--learning_rate=0.1 \
--learning_rate_decay_factor=0.1 \
--num_epochs_per_decay=200 \
--weight_decay=0.004
4)
# Run evaluation.
python eval_image_classifier.py \
--checkpoint_path=${TRAIN_DIR} \
--eval_dir=${TRAIN_DIR} \
--dataset_name=cifar10 \
--dataset_split_name=test \
--dataset_dir=${DATASET_DIR} \
--model_name=cifarnet
5)
# Exporting the Inference Graph
/home/jcq/.conda/envs/tensorflow_gpu/bin/python export_inference_graph.py \
--alsologtostderr \
--dataset_dir=${DATASET_DIR} \
--dataset_name=cifar10 \
--model_name=cifarnet \
--image_size=32 \
--output_file=/media/jcq/Soft/Tensorflow/tensorflow-models/research/slim/tmp/cifarnet_cifar10_inf_graph.pb
6)
# Freezing the exported Graph
/home/jcq/.conda/envs/tensorflow_gpu/bin/python \
-u /home/jcq/.conda/envs/tensorflow_gpu/lib/python3.6/site-packages/tensorflow_core/python/tools/freeze_graph.py \
--input_graph=/media/jcq/Soft/Tensorflow/tensorflow-models/research/slim/tmp/cifarnet_cifar10_inf_graph.pb \
--input_checkpoint=/media/jcq/Soft/Tensorflow/tensorflow-models/research/slim/tmp/cifarnet-model/model.ckpt-5000 \
--output_graph=/media/jcq/Soft/Tensorflow/tensorflow-models/research/slim/tmp/cifarnet-model/frozen_cifarnet_cifar10_with_lrn.pb \
--input_binary=True \
--output_node_names=CifarNet/Predictions/Reshape_1
7)
# Freezing the exported Graph
bazel build tensorflow/examples/label_image:label_image
/media/jcq/Work/Tensorflow/tensorflow-1.14.0/bazel-bin/tensorflow/examples/label_image/label_image \
--image=${HOME}/Pictures/flowers.jpg \
--input_layer=input \
--output_layer=CifarNet/Predictions/Reshape_1 \
--graph=/media/jcq/Soft/Tensorflow/tensorflow-models/research/slim/tmp/cifarnet-model/frozen_cifarnet_cifar10_with_lrn.pb \
--labels=/tmp/imagenet_slim_labels.txt \
--input_mean=0 \
--input_std=255
最终模型结构如下:
二、Eagle eye 中运行 tensorflow 模型
2.1、TensorFlow 模型推理
以下说明了如何在 Eagle Eye 中利用 eIQ 中对模型进行推理使用: 载入 cifarnet 模型:
status = case_mobilenet_full("data/frozen_cifarnet_cifar10.pb",
"data/airunner/cat.1712.jpg",
"data/airunner/image_classification/imagenet_slim_labels.txt", false, false, flip);
对模型进行推理的过程:
int case_mobilenet_pre_split(const std::string& aMnetGraphP1,
const std::string& aMnetGraphP2,
const std::string& aImageFile,
const std::string& aSlimLabelsFile,
bool isFixed,
bool isExtSoftmax,
bool flipDisplay)
{
if (!isFixed)
{
auto results_cpu = case_network_target(aMnetGraphP1, aMnetGraphP2, aImageFile, aSlimLabelsFile, "CPU", isFixed, isExtSoftmax, flipDisplay);
}
else
{
auto results_cpu = case_network_target(aMnetGraphP1, aMnetGraphP2, aImageFile, aSlimLabelsFile, "CPU", isFixed, isExtSoftmax, flipDisplay);
}
return 0;
}
输出要处理的部分:
最终输出的结果处理:
lOutputTensors[lOutputTensor]->Invalidate();
float result = *(lOutputTensors[lOutputTensor]->DataPtr());
float sigmoid = static_cast(1. / (1. + exp((result) * -1.)));
std::cout << "Dog: " << sigmoid << " Cat: " << 1 - sigmoid << std::endl;
三、参考资料
【1】NXP 官网中关于 eIQ 的介绍
https://www.nxp.com.cn/design/software/development-software/eiq-auto-dl-toolkit:EIQ-AUTO
【2】 TF slim
https://www.cnblogs.com/hejunlin1992/p/8082535.html
评论