Tuesday 22 January 2019

Implementing (How to run Keras model on Movidius neural compute stick)


This is an implementation report of the article ‘How to run Keras model on Movidius NCS’.  You can read the article at:

‘Source code for this post available on my GitHub repo - keras_mnist.’

Statement works OK:

The importance of this example is:  it trains a weight file and a json graph file using Keras.  Then it converts these to a tensor flow model.  It loads the graph file to the Movidius NCS stick.  At the end it runs the test example, a number picture(number 6) on the NCS.

Some details about implementation problems I had:

A through README is available.
  • Optionally, copy this folder into your NCSDK2 directory along with other TensorFlow examples. ncsdk/examples/tensorflow/keras_mnist
  • Plug NCS to a USB port on the host machine.
  • Run command - make all
  • Run command - make run
But the copying is not optional.  Because:
MAKEFILE does:
prereqs:
            (cd ../../data/ilsvrc12; make)

Make all produced the KERAS model with a long output but with the problem:
Training finished. If you want to retrain the model, delete 'weights.h5' and 'model.json' files.
(test -f weights.h5 && test -f model.json) || (echo "Please run \'make train\' first.")
test -f TF_Model/tf_model.meta || ./convert-mnist.py
from: can't read /var/mail/keras.models
from: can't read /var/mail/keras
./convert-mnist.py: 8: ./convert-mnist.py: model_file: not found
./convert-mnist.py: 9: ./convert-mnist.py: weights_file: not found
./convert-mnist.py: 11: ./convert-mnist.py: Syntax error: "(" unexpected
Makefile:45: recipe for target 'weights' failed
make: *** [weights] Error 2

The solution was to install KERAS with:
Pip3 install keras

It did another error which I corrected with putting at the top of convert-mnist.py
And at the top of makeFile:
#! /usr/bin/python3.5

After this I got the error:  ImportError: cannot import name '_validate_lengths'
Icorrected this by first:
Pip3 install numpy
And then because of its sideeffect I installed:
Pip3 scikit-image

The result is:
arsaral@ars:~/ncsdk/ncappzoo-ncsdk2/apps/keras_mnist-master$ make run
(test -f weights.h5 && test -f model.json) || python3 ./train-mnist.py
Training finished. If you want to retrain the model, delete 'weights.h5' and 'model.json' files.
(test -f weights.h5 && test -f model.json) || (echo "Please run \'make train\' first.")
test -f TF_Model/tf_model.meta || ./convert-mnist.py
test -f graph || mvNCCompile -s 12 TF_Model/tf_model.meta -in=conv2d_1_input -on=dense_2/Softmax
python3 ./predict-mnist-ncsdk2.py
/usr/local/lib/python3.5/dist-packages/mvnc/mvncapi.py:416: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead
  tensor = numpy.fromstring(tensor.raw, dtype=numpy.float32)
NCS
 [8.3982944e-05 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00
 2.1958351e-04 9.9804688e-01 0.0000000e+00 1.2531281e-03 0.0000000e+00]
Predicted: 6

The inferred picture is: