[PYTHON] OpenPose on MacBook Pro Part 2

OpenPose on MacBook Pro I found that OpenPose works on MacBook Pro, but I was wondering if it could be run on GPU. Since there is something called PlaidML, I read the following and tried it because I thought I could manage it myself.

-PlaidML has become wonderful before I knew it -I tried Plaid ML on Mac, which is capable of deep learning on non-NVIDIA GPUs.

environment

Setup steps

Preparation

As a preparation, set up a Python virtual environment. It was okay to use the previous environment, but I recreated it in case there is some conflict.

Create a virtual environment
$ virtualenv mppose-plaidml

Enter the virtual environment
$ source mppose-plaidml/bin/activate

PlaidML setting, operation check

After entering the virtual environment, install the PlaidML package and configure the device to be used. My MacBook Pro included a Radeon Pro 555x, so I chose that.

Install PlaidML and benchmark packages
$ pip install plaidml-keras plaidbench

Set the device used by PlaidML
$ plaidml-setup
* Use y except for device settings
・ ・ ・
<Omission>
・ ・ ・
In the device settings section, select the device number displayed in the list and press Enter.
Multiple devices detected (You can override by setting PLAIDML_DEVICE_IDS).
Please choose a default device:

   1 : llvm_cpu.0
   2 : opencl_intel_uhd_graphics_630.0
   3 : opencl_cpu.0
   4 : opencl_amd_radeon_pro_555x_compute_engine.0
   5 : metal_intel(r)_uhd_graphics_630.0
   6 : metal_amd_radeon_pro_555x.0

Default device? (1,2,3,4,5,6)[1]:6
・ ・ ・
<Omission>
・ ・ ・

After installing the package and setting the device, check the operation.

Check the operation with the benchmark
$ plaidbench keras mobilenet
Running 1024 examples with mobilenet, batch size 1, on backend plaid
INFO:plaidml:Opening device "metal_amd_radeon_pro_555x.0"* If this display appears, it is operating on the selected device.
Compiling network... Warming up... Running...
Example finished, elapsed: 0.545s (compile), 14.425s (execution)

-----------------------------------------------------------------------------------------
Network Name         Inference Latency         Time / FPS          
-----------------------------------------------------------------------------------------
mobilenet            14.09 ms                  0.00 ms / 1000000000.00 fps
Correctness: PASS, max_error: 1.675534622336272e-05, max_abs_error: 7.674098014831543e-07, fail_ratio: 0.0

I knew it was working, but I didn't know "whether it's faster or slower than when using the CPU", so I wondered what to do, but before the command, PLAIDML_DEVICE_IDS = "device name" "There is information that if you write, it will work on that device, so I tried it.

Benchmark with CPU set as execution device
$ PLAIDML_DEVICE_IDS="opencl_cpu.0" plaidbench keras mobilenet
Running 1024 examples with mobilenet, batch size 1, on backend plaid
INFO:plaidml:Opening device "opencl_cpu.0"
Compiling network... Warming up... Running...
Example finished, elapsed: 3.034s (compile), 141.360s (execution)

-----------------------------------------------------------------------------------------
Network Name         Inference Latency         Time / FPS          
-----------------------------------------------------------------------------------------
mobilenet            138.05 ms                 134.30 ms / 7.45 fps
Correctness: FAIL, max_error: 0.005546755623072386, max_abs_error: 0.0003522038459777832, fail_ratio: 0.076

As a result, it seems that the performance of Radeon Pro 555x is about 10 times that of CPU, so I think that it is effective.

Also, if you select `` Window-> GPU history'' in the activity monitor, you can see the GPU device list and operating status in real time, so you can also use that to see the operating status.

OpenPose settings

Now, let's set up OpenPose. The procedure is mostly the same as last time, but some steps need to be changed in order to use PlaidML.

Install the package to run OpenPose.
$ pip install ipython configobj pillow opencv-python

Clone the Keras backend version of OpenPose from Github.
$ git clone https://github.com/michalfaber/keras_Realtime_Multi-Person_Pose_Estimation.git

Change to the directory.
$ cd keras_Realtime_Multi-Person_Pose_Estimation/

Go to the model directory in the repository and download the caffe and keras training models.
$ cd model
$ sh get_caffe_model.sh 
$ sh get_keras_model.sh 

Modify the model processing module to use PlaidML.
$ vi cmu_model.py

* Just add the following two lines to the beginning of the source text.
import plaidml.keras
plaidml.keras.install_backend()

Empty init.Create a py.(In my environment, if I do not create this, the python module under the model directory cannot be read when running the python script later, and an error occurs.)
$ touch __init.py__

Go back to the top of the repository and dump caffe's numpy training model layer.
$ cd ../
$ docker run -v `pwd`:/workspace -it bvlc/caffe:cpu python dump_caffe_layers.py

Convert the training model layer of caffe for keras.
$ python caffe_to_keras.py 

This completes the settings.

The operation was successful, but ...

So, I tried to check the operation with a sample photo and a camera as before ...

Try to detect the posture of the sample photo.
$ python demo_image.py --image sample_images/ski.jpg 

Start the camera and try to detect the posture.
$ python demo_camera.py

The attitude detection of the camera resulted in a processing time of about 0.21 sec, which was lower than the operation result of the CPU (processing time of about 0.07 sec). When I investigated "Why?", I found this kind of exchange in the PlaidML issue. Plaidml backend slower than TF backend #530

A possible possibility is that "the current PlaidML does not support many calculation models", and I think that it does not support the OpenPose calculation model (CMU?) Used this time. I will.

Normally, I think it is justified to give up here and "prepare an environment using GPU". This time, I was asking "How much can I do in the current environment at home?", So I've done so far, so the rest

--Expecting after the version upgrade of PlaidML (It seems that the update has stopped for over a year, so expectations are low?) --Try to make it work with the calculation model supported by PlaidML

In such a place, I think I'll do it while thinking about various things.

Recommended Posts

OpenPose on MacBook Pro Part 2
OpenPose on MacBook Pro
Notes on setting up tensorflow-gpu on MacBook Pro (Mid 2012)
Can't get PyAutoGUI coordinates correctly on MacBook Pro?
Implemented ESPnet on Macbook
Run Openpose on Python (Windows)
Install OpenPose on mac (Catalina)
Dual Boot (not VM) for Kali Linux ver2020.2 on MacBook pro 2017
Install Ubuntu 18.04 on MacBook Pro Touchbar model and connect to WIFI
Try using Pillow on iPython (Part 1)
Introduction to Python Hands On Part 1
Try using Pillow on iPython (Part 2)
WSL2 ~ Linux on Windows ~ (Part 1: Introduction)
Try using Pillow on iPython (Part 3)