Host machine with Ubuntu 14.04
UDOO Quad/Dual Board
uSD card with at least 8 GB
Download documentation and install latest Official Udoobuntu OS (at the moment of writing: UDOObuntu 2.1.2), https://www.udoo.org/downloads/
This document describes how to install and test Keras (Open source neural network library) and Theano (numerical computation library for python ) for deep learning library usage on i.MX6QD UDOO board.
$ sudo apt-get update && sudo apt-get upgrade
update your date system: e.g.
$ sudo date -s “07/08/2017 12:00”
First satisfy the run-time and build time dependencies:
$ sudo apt-get install python-software-properties software-properties-common make unzip zlib1g-dev git pkg-config autoconf automake libtool curl python-pip python-numpy libblas-dev liblapack-dev python-dev libatlas-base-dev gfortran libhdf5-serial-dev libhdf5-dev python-setuptools libyaml-dev libpython2.7-dev
$ sudo easy_install scipy
The last step is installing scipy through pip, and can take several hours.
First, we have a few more dependencies to get:
$sudo pip install scikit-learn
$sudo pip install pillow
$sudo pip install h5py
With these dependencies met, we can install a stable Theano release from the git source:
$ git clone https://github.com/Theano/Theano
$ cd Theano
Numpy 1.9 cause conflicts with armv7, so we need to change the setup.py configuration:
$ sudo nano setup.py
# install_requires=['numpy>=1.9.1', 'scipy>=0.14', 'six>=1.9.0'],
Then install it:
$ sudo python setup.py install
The installation can occur with the command: (this could take a lot of time!!!)
$ cd ..
$ git clone https://github.com/fchollet/keras.git
$ cd keras
$ sudo python setup.py install
$sudo pip install --upgrade keras
After Keras is installed, you will want to edit the Keras configuration file ~/.keras/keras.json to use Theano instead of the default TensorFlow backend. If it isn't there, you can create it. This requires changing two lines. The first change is:
"image_dim_ordering": "tf" --> "image_dim_ordering": "th"
and the second:
"backend": "tensorflow" --> "backend": "theano"
(The final file should look like the example below)
sudo nano ~/.keras/keras.json
You can also define the environment variable KERAS_BACKEND and this will override what is defined in your config file :
$ KERAS_BACKEND=theano python -c "from keras import backend"
Python 2.7.6 (default, Oct 26 2016, 20:46:32)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using Theano backend.
Be aware this test take some time (~1hr on udoo dual):
$ curl -sSL -k https://github.com/fchollet/keras/raw/master/examples/mnist_mlp.py | python
For demonstration, deep-learning-models repository provided by pyimagesearch and from fchollet git, and also have three Keras models (VGG16, VGG19, and ResNet50) online — these networks are , meaning that they can recognize
$ cd keras
$ git clone https://github.com/fchollet/deep-learning-models
$ Cd deep-learning-models
$ ls -l
Notice how we have four Python files. The , , and files correspond to their respective network architecture definitions. The file, as the name suggests, contains a couple helper functions that allow us to prepare images for classification as well as obtain the final class label predictions from the network
Classify ImageNet classes with ResNet50
ResNet50 model, with weights pre-trained on ImageNet. This model is available for both the Theano and TensorFlow backend, and can be built both with "channels_first" data format (channels, height, width) or "channels_last" data format (height, width, channels). The default input size for this model is 224x224.
We are now ready to write some Python code to classify image contents utilizing convolutional Neural Networks (CNNs) pre-trained on the ImageNet dataset. For udoo Quad/Dual use ResNet50 due to avoid space conflict. Also we are going to use ImageNet (http://image-net.org/) that is an image database organized according to the WordNet hierarchy, in which each node of the hierarchy is depicted by hundreds and thousands of images.
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
model = ResNet50(weights='imagenet')
#for this sample I download the image from: http://i.imgur.com/wpxMwsR.jpg
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print('Predicted:', decode_predictions(preds, top=3))
Save the file an run it.
Results for elephant image:
Top prediction was 0.8890 for African Elephant
Testing with this image: http://i.imgur.com/4FIOwAN.jpg
Top prediction was: 0.7799 for golden_retriever.
Now your Udoo is ready to use Keras and Theano as Deep Learning libraries, next time we are going to show some usage example for image classification models with OpenCV.