本文介紹了如何使用 TensorFlow 在智能機(jī)上(包括安卓和 iOS 設(shè)備)執(zhí)行實(shí)時(shí)單人姿態(tài)估計(jì)。 GitHub 地址:https://github.com/edvardHua/PoseEstimationForMobile 該 repo 使用 TensorFlow 實(shí)現(xiàn) CPM 和 Hourglass 模型。這里未使用常規(guī)的卷積,而是在模型內(nèi)部使用了反向卷積(又叫 Mobilenet V2),以便執(zhí)行實(shí)時(shí)推斷。 注:你可以修改網(wǎng)絡(luò)架構(gòu),來(lái)訓(xùn)練更高 PCKh 的模型。架構(gòu)地址:https://github.com/edvardHua/PoseEstimationForMobile/tree/master/training/src 該庫(kù)包含:
下面的 gif 是在 Mi Mix2s 上截取的(~60 FPS) 你可以下載以下 apk,在自己的設(shè)備上進(jìn)行測(cè)試。
訓(xùn)練 依賴項(xiàng)
數(shù)據(jù)集 訓(xùn)練數(shù)據(jù)集:https://drive.google.com/open?id=1zahjQWhuKIYWRRI2ZlHzn65Ug_jIiC4l 將其解壓縮,獲取以下文件結(jié)構(gòu): # root @ ubuntu in ~/hdd/ai_challenger$ tree -L 1 ..├── ai_challenger_train.json├── ai_challenger_valid.json├── train└── valid 該訓(xùn)練數(shù)據(jù)集僅包含單人圖像,數(shù)據(jù)來(lái)源是 AI Challenger 競(jìng)賽。共包含 22446 個(gè)訓(xùn)練樣本和 1500 個(gè)測(cè)試樣本。 該 repo 作者使用 tf-pose-estimation 庫(kù)中的數(shù)據(jù)增強(qiáng)代碼將標(biāo)注遷移為 COCO 格式。tf-pose-estimation 庫(kù):https://github.com/ildoonet/tf-pose-estimation 超參數(shù) 訓(xùn)練步驟中,使用 experiments 文件夾中的 cfg 文件傳輸超參數(shù)。 以下是 mv2_cpm.cfg 文件的內(nèi)容: [Train]model: 'mv2_cpm'checkpoint: Falsedatapath: '/root/hdd/ai_challenger'imgpath: '/root/hdd/'visible_devices: '0, 1, 2'multiprocessing_num: 8max_epoch: 1000lr: '0.001'batchsize: 5decay_rate: 0.95input_width: 192input_height: 192n_kpoints: 14scale: 2modelpath: '/root/hdd/trained/mv2_cpm/models'logpath: '/root/hdd/trained/mv2_cpm/log'num_train_samples: 20000per_update_tensorboard_step: 500per_saved_model_step: 2000pred_image_on_tensorboard: True 該 cfg 文件覆蓋模型的所有參數(shù),在 network_mv2_cpm.py 中仍有一些參數(shù)。 使用 nvidia-docker 訓(xùn)練 通過(guò)以下命令構(gòu)建 docker: cd training/dockerdocker build -t single-pose . 或者 docker pull edvardhua/single-pose 然后運(yùn)行以下命令,訓(xùn)練模型: nvidia-docker run -it -d \-v <dataset_path>:/data5 -v <training_code_path>/training:/workspace \-p 6006:6006 -e LOG_PATH=/root/hdd/trained/mv2_cpm/log \-e PARAMETERS_FILE=experiments/mv2_cpm.cfg edvardhua/single-pose 此外,它還在 port 6006 上創(chuàng)建了 tensorboard。確保安裝了 nvidia-docker。 按一般方法訓(xùn)練 1. 安裝依賴項(xiàng): cd trainingpip3 install -r requirements.txt 還需要安裝 cocoapi (https://github.com/cocodataset/cocoapi)。 2. 編輯 experiments 文件夾中的參數(shù)文件,它包含幾乎所有超參數(shù)和訓(xùn)練中需要定義的其他配置。之后,傳輸參數(shù)文件,開(kāi)始訓(xùn)練: cd trainingpython3 src/train.py experiments/mv2_cpm.cfg 在 3 張英偉達(dá) 1080Ti 顯卡上經(jīng)過(guò) 12 個(gè)小時(shí)的訓(xùn)練后,該模型幾乎收斂。以下是對(duì)應(yīng)的 tensorboard 圖。 基準(zhǔn)(PCKh) 運(yùn)行以下命令,評(píng)估 PCKh 值。 python3 src/benchmark.py --frozen_pb_path=hourglass/model-360000.pb \--anno_json_path=/root/hdd/ai_challenger/ai_challenger_valid.json \--img_path=/root/hdd \--output_node_name=hourglass_out_3 預(yù)訓(xùn)練模型
安卓 demo 由于 mace 框架,你可以使用 GPU 在安卓智能機(jī)上運(yùn)行該模型。 按照以下命令將模型轉(zhuǎn)換為 mace 格式: cd <your-mace-path># You transer hourglass or cpm model by changing `yml` file.python tools/converter.py convert --config=<PoseEstimationForMobilePath>/release/mace_ymls/cpm.yml 然后根據(jù) mace 文檔的說(shuō)明,將模型集成到安卓設(shè)備中。 至于如何調(diào)用模型、解析輸出,可以參見(jiàn)安卓源代碼:https://github.com/edvardHua/PoseEstimationForMobile/tree/master/android_demo。 一些芯片的平均推斷時(shí)間基準(zhǔn)如下所示: 以下是該 repo 作者構(gòu)建該 demo 的環(huán)境:
在構(gòu)建 mace-demo 時(shí),不同環(huán)境可能會(huì)遇到不同的錯(cuò)誤。為避免這種情況,作者建議使用 docker。 docker pull registry.cn-hangzhou.aliyuncs.com/xiaomimace/mace-dev-litedocker run -it --privileged -d --name mace-dev --net=host -v to/you/path/PoseEstimationForMobile/android_demo/demo_mace:/demo_mace registry.cn-hangzhou.aliyuncs.com/xiaomimace/mace-dev-litedocker run -it --privileged -d --name mace-dev --net=host \ -v to/you/path/PoseEstimationForMobile/android_demo/demo_mace:/demo_mace \ registry.cn-hangzhou.aliyuncs.com/xiaomimace/mace-dev-lite# Enter to dockerdocker exec -it mace-dev bash# Exec command inside the dockercd /demo_mace && ./gradlew build 或者將模型轉(zhuǎn)換為 tflite: # Convert to frozen pb.cd trainingpython3 src/gen_frozen_pb.py \--checkpoint=<you_training_model_path>/model-xxx --output_graph=<you_output_model_path>/model-xxx.pb \--size=192 --model=mv2_cpm_2# If you update tensorflow to 1.9, run following command.python3 src/gen_tflite_coreml.py \--frozen_pb=forzen_graph.pb \--input_node_name='image' \--output_node_name='Convolutional_Pose_Machine/stage_5_out' \--output_path='./' \--type=tflite # Convert to tflite.# See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/docs_src/mobile/tflite/devguide.md for more information.bazel-bin/tensorflow/contrib/lite/toco/toco \--input_file=<you_output_model_path>/model-xxx.pb \--output_file=<you_output_tflite_model_path>/mv2-cpm.tflite \--input_format=TENSORFLOW_GRAPHDEF --output_format=TFLITE \--inference_type=FLOAT \--input_shape='1,192,192,3' \--input_array='image' \--output_array='Convolutional_Pose_Machine/stage_5_out' 然后,將 tflite 文件放在 android_demo/app/src/main/assets 中,修改 ImageClassifierFloatInception.kt 中的參數(shù)。 ............// parameters need to modify in ImageClassifierFloatInception.kt/** * Create ImageClassifierFloatInception instance * * @param imageSizeX Get the image size along the x axis. * @param imageSizeY Get the image size along the y axis. * @param outputW The output width of model * @param outputH The output height of model * @param modelPath Get the name of the model file stored in Assets. * @param numBytesPerChannel Get the number of bytes that is used to store a single * color channel value. */ fun create( activity: Activity, imageSizeX: Int = 192, imageSizeY: Int = 192, outputW: Int = 96, outputH: Int = 96, modelPath: String = 'mv2-cpm.tflite', numBytesPerChannel: Int = 4 ): ImageClassifierFloatInception = ImageClassifierFloatInception( activity, imageSizeX, imageSizeY, outputW, outputH, modelPath, numBytesPerChannel)............ 最后,將該項(xiàng)目導(dǎo)入 Android Studio,在智能機(jī)設(shè)備上運(yùn)行。 iOS Demo 首先,將模型轉(zhuǎn)換為 CoreML 模型: # Convert to frozen pb.cd trainingpython3 src/gen_frozen_pb.py \--checkpoint=<you_training_model_path>/model-xxx --output_graph=<you_output_model_path>/model-xxx.pb \--size=192 --model=mv2_cpm_2# Run the following command to get mlmodelpython3 src/gen_tflite_coreml.py \--frozen_pb=forzen_graph.pb \--input_node_name='image' \--output_node_name='Convolutional_Pose_Machine/stage_5_out' \--output_path='./' \--type=coreml 然后,按照 PoseEstimation-CoreML 中的說(shuō)明來(lái)操作(https://github.com/tucan9389/PoseEstimation-CoreML)。 |
|
來(lái)自: 二十八畝田123 > 《待分類(lèi)》