Ir al contenido principal

Creando un detector de setas para el móvil (utilizando el framework de machine learning KerasJS)


About

I explain how to export weights from a Keras model and import those weights in Keras.js, a JavaScript framework for running pre-trained neural networks in the browser. I show you later how to include the final result into a Phonegap Cordova mobile application.

Introduction

We are going to create a mushroom detector application for our mobiles. For this task we need to use a machine learning approximation, and as a good approximation we'll use the KerasJS framework.

First Step: Build an image recognition system for several mushroom families.

  • We need to begin training a Keras model in Python. For this purpose we'll use a transfer learning/fine tuning process over a pre-trained InceptionV3 model. The pipeline will be:
  1. Transfer learning: take a ConvNet that has been pre-trained on ImageNet, remove the last fully-connected layer, then treat the rest of the ConvNet as a feature extractor for the new dataset. Once you extract the features for all images, train a classifier for the new dataset.
  2. Fine-tuning: replace and retrain the classifier on top of the ConvNet, and also fine-tune the weights of the pre-trained network via backpropagation.
  • But first we need the database for the customizable mushroom categories. We have to find a big labeled database with thousands of images of each mushroom family we want to detect. This is not an easy issue, so we can trick a bit by searching and downloading Google Images/Image Links:
We can use this Python script in order to download images from Google base in a set if query strings:
google-images-download.py
This script will download 200 images for each of the specified mushroom families:
search_keyword = [
'Paxinus involutus',
'sarcosphaera',
'Inonatus tamaricis',
...
...
];
After this process end we'll have a folder with 200 images named with the mushroom family. We have to cut this folders and move them to a new 'train_dir' folder. We need also create a 'val_dir' folder. In 'val_dir' folder we will have a copy of all downloaded mushroom family folders but just with some images for the validation process (this images cannot be anymore in the 'train_dir' folder).
Now we can start the fine tuning process using the Python Keras script:
fine-tune.py.py
This script will train the mushroom detection model and finally will create as output some weights files we'll use in the next step:
model_mushrooms.json
model_mushrooms_metadata.json
model_mushrooms_weights.buf
We can use the script: predict.py to check the accuracy of the training process.

Second Step: Running the Neural Network in the Browser

We will now create a tiny JavaScript application that loads the previously saved model and weights. Create frontent code and distribution folders:
mkdir -p frontend/dist
You’ll want to copy the extracted model data files into the frontend/dist directory:
cp model_mushrooms.json frontend/dist/
cp model_mushrooms_metadata.json frontend/dist/
cp model_mushrooms_weights.buf frontend/dist/
The Webpack Setup:
We will write the code in ES6 and prepare it for the browser using the webpack JavaScript code and asset bundler. So, install webpack and the webpack development server via npm by running
npm install webpack -g
npm install webpack-dev-server -g
You will also want to start a node project and install the required packages
npm init
npm install --save keras-js url-loader
  • The HTML:
I will use a very basic HTML file placed directly in the 'frontend/dist/index.html' distribution directory, which is generally not a good idea but works for this demonstration.
  • The JavaScript:
The actual model loading code will live inside 'frontend/entry.js'
  • With the sources in place, compile the bundle with:
cd frontend
webpack
To view the app, run:
webpack-dev-server
and open the indicated address (likely http://localhost:8080) in a web browser.
  • Testing the web:
Just type in the file input the name of an image file located in the folder 'frontend/dist/' and click the 'Predict' button. After some seconds you will have as output the prediction of the model with the top 3 confidence probabilities.


Third Step: Running the Neural Network in a Phonegap Cordova application.

  • This step is so simple as creating a new Phonegap project, and copying the former folders into the 'www' folder. Then we can just use a controller to redirect to a page containing our 'frontend/dist/index.html'.






Entradas populares de este blog

Evidencia a favor de la teoría de Jeremy England (usando computación evolutiva)

"You start with a random clump of atoms, and if you shine light on it for long enough, it should not be so surprising that you get a plant." Jeremy England (2014), interview commentary with Natalie Wolchover Hace ya un mes que terminé de estudiar a fondo el interesante trabajo que el físico  Jeremy England  está realizando en el  MIT (Massachusetts Institute of Technology) . En mi blog he divulgado todo lo referente a este trabajo con mucho nivel de detalle, siendo esta entrada un compendio de todo lo que el trabajo cuenta. La idea de esta línea de investigación viene a decir, a grosso modo , que la física de nuestro mundo mantiene una relación implícita entre complejidad y energía . Esta relación indica que, cuanto más complejo es un fenómeno, más energía debe disiparse de modo que crezca la probabilidad de que tal fenómeno finalmente acontezca. Esta teoría de Jeremy parte, y se deduce, de una base termodinámica y de mecánica estadística ya establecida, por lo que sus concl

Aprendizaje automático mediante Deep Q Ntework (DQN + TensorFlow)

"[Las neuronas son] células de formas delicadas y elegantes, las misteriosas mariposas del alma, cuyo batir de alas quién sabe si esclarecerá algún día el secreto de la vida mental."  (Ramón y Cajal) Introducción. Este artículo es una continuación de mi entrada anterior "Las matemáticas de la mente" [2]. Vimos en ese artículo cómo era posible que un simple algoritmo de computación pudiese imitar el modo en que nuestro cerebro aprende a realizar tareas con éxito, simplemente a partir del equivalente computacional de una red neuronal. Sin embargo, a pesar de que en dicha entrada os comentaba el caso de cómo se puede programar un algoritmo capaz de conseguir  literalmente,  aprender a jugar al Conecta4 (4 en raya) sin especificar ( pre-programar ) en ningún momento las reglas del juego; es posible que muchos notasen que aún así, todavía había que pre-procesar la entrada de la red neuronal para ofrecerle a las neuronas (nodos) de la capa de entrada ( inputs ) qué ficha

Aprendizaje autónomo por computación evolutiva (Conecta 4)

"[Las neuronas son] células de formas delicadas y elegantes, las misteriosas mariposas del alma, cuyo batir de alas quién sabe si esclarecerá algún día el secreto de la vida mental."  (Ramón y Cajal) Introducción. Dibujo de Ramón y Cajal de las células del cerebelo de un pollo,  mostrado en "Estructura de los centros nerviosos de las aves", Madrid, 1905. Dos noticias muy importantes que han tenido lugar estas últimas semanas en el campo de la neurociencia y la inteligencia artificial (de las cuales me hice eco en este mismo blog: aquí [1][2] y  aquí [3]), me hizo recordar un trabajo de computación que hice allá por el 2011 cuando inicié el doctorado en ingeniería (el cual por cierto aún no terminé, y que tengo absolutamente abandonado :( Ya me gustaría tener tiempo libre para poder retomarlo; porque además odio dejar las cosas a medias). Pues bien, el trabajo original[4] (que he mejorado) consistía en ser el desarrollo de un algoritmo capaz de aprender a jugar a