[PYTHON] Try running PlaidML image judgment on Mac

Try using PlaidML, which runs on the GPU of a Mac with OpenPose Part 2 on MacBookPro, what else works besides the benchmark? So when I looked it up, I found an article like this on qiita, so I tried to see if it works in the current situation (2020).

-I tried image classification on Mac GPU (AMD) using PlaidML

environment

Preparation

Set up a Python virtual environment.

Create a virtual environment
$ virtualenv plaidvison-plaidml

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

Setting

PlaidML setting, operation check

After entering the virtual environment, install the PlaidML package and configure the device to be used. This is the same as OpenPose on MacBookPro Part 2.

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

plaid vision settings

Settings, but end of error

It seems that the source was in the PlaidML repository at the time of posting the reference article, but when I tried it The link is broken, and when I looked it up, it was found at here.

So I will clone it from git.

$ git clone https://github.com/jbruestle/plaidvision.git

After cloning, enter the directory.

$ cd plaidvision

Install the required packages.

$ pip install -r requirements.txt 

After installing the package, I started it with the following command, but for some reason it ended with an error.

$ python plaidvision.py mobilenet
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Using PlaidML backend.
INFO:plaidml:Opening device "metal_amd_radeon_pro_555x.0"
Traceback (most recent call last):
  File "plaidvision.py", line 320, in <module>
    main()
  File "plaidvision.py", line 289, in main
    predictions = model.classify(frame)
  File "plaidvision.py", line 219, in classify
    img = scipy.misc.imresize(img, self.shape).astype(float)
AttributeError: module 'scipy.misc' has no attribute 'imresize'

Investigate and fix errors, but

It seems that it is an error because there is no attribute called imresize, so I checked it and tried to modify the source of plaidvision.py while referring to the following.

-Correspondence to Python error "Attribute Error: module'scipy.misc' has no attribute'imresize'" in deep learning -Where did scipy.misc.imresize go?

<Omission>
import pygame
import scipy.misc
from PIL import Image <-Add this line

<Omission>

The part that calls imresize here

    def classify(self, img, top_n=5):
        if img.shape != self.shape:
            img = scipy.misc.imresize(img, self.shape).astype(float)   <==Here
        data = np.expand_dims(img, axis=0)
        data = self.preprocess_input(data)
        predictions = self.model.predict(data)
        return self.decode_predictions(predictions, top=top_n)[0]

Change as below

    def classify(self, img, top_n=5):
        if img.shape != self.shape:
            img = np.array(Image.fromarray(img).resize((int(self.shape[1]),int(self.shape[0])), resample=0)).astype(float)  <==Change like this
        data = np.expand_dims(img, axis=0)
        data = self.preprocess_input(data)
        predictions = self.model.predict(data)
        return self.decode_predictions(predictions, top=top_n)[0]

After the correction, when I tried to execute it again, the error termination was not displayed, but the gray window was displayed and nothing was displayed.

graywindow.png

Fixed display issues

When I investigated why, I found such information.

-pygame doesn't work on macOS Mojave

Apparently there is a problem with the compatibility between the pygame installed by default and the Mac, and it seems that I had to install another version of pygame.

So uninstall the currently included pygame.

$ pip uninstall pygame

Then specify the version and install.

$ pip install pygame==2.0.0.dev6

Result is

After installing pygame, I ran it.

$ python plaidvision.py mobilenet

This time, it worked without problems and I was able to judge the image.

success.png

Recommended Posts

Try running PlaidML image judgment on Mac
Try running Jupyter Notebook on Mac
Try deepdream on Mac
Try running tensorflow on Docker + anaconda
Try running Kobuki's 3D simulator on ROS
GPU ~ Implementation of PlaidML on Mac ~ (as of May 2020)
Try running Distributed TensorFlow on Google Cloud Platform
python on mac
Try importing MLB data on Mac and Python
Try running Pyston 0.1
Install selenium on Mac and try it with python
Try using E-Cell 4 on Windows 7 or Mac OS X
Install pyenv on mac
Pyenv + virtualenv on Mac
Install Ansible on Mac
Install Python on Mac
Install Python 3 on Mac
numba installation on mac
Run OpenMVG on Mac
Try FEniCS on Windows!
Try Poerty on Windows
Try NeosVR on Linux
Install Python 3.4 on Mac
Install Caffe on Mac
Install mecab on mac
Install mecab-python on Mac
Try running Amazon Linux 2 on-premises (VM on your local PC).