sexta-feira, 16 de junho de 2017

K-means and decision tree using Weka and JavaFX

Weka is one of the most known tools for Machine Learning in Java, which also has a great Java API including API for k-means clustering. Using JavaFX it is possible to visualize unclassified data, classify the data using Weka APIs and then visualize the result in a JavaFX chart, like the Scatter chart.

In this post we will show a simple application that allows you to load data, show it without series distinction using a JavaFX scatter chart,, then we use Weka to classify the data in a defined number of clusters and finally separated the clustered data by chart series. We will be using the Iris.2D.arff file that comes with Weka download.

K-means clustering using Weka is really simple and requires only a few lines of code as you can see in this post. In our application we will build 3 charts for the Iris dataset:

  1. Data without class distinction (no classes)
  2. The data with the ground truth classification
  3. Data clustered using weka

As you can see the clustered data is really close to the real one (the data with correct labels). The code to build the clustered data:

After creating these 3 charts I also modified the whole code to add a decision tree classifier using weka J48 algorithm implementation. Right after the chart you can see the tree that I built our of the Iris 2d data:

When you click in any chart you will see a new item is added and it will be classified on center chart using the decision tree and on clustered chart using the k-means classification.

We use our generated decision tree to classify data and also the cluster. In the image above as you can see the cluster classify some data differently from what is classified with the decision tree.

I think it is particularly interesting how it is easy to visualize data with JavaFX. The full code for this project can be found on my github, but here is the main class code:

segunda-feira, 5 de junho de 2017

Recognizing Handwritten digits from a JavaFX application using Deeplearning4j

We already talked about tensorflow and JavaFX on this blog, but tensorflow Java API is still incomplete. A mature and well documented API is DeepLearning4J.

In this example we load the trained model in our application, create a canvas for writing and enter is pressed, the canvas image is resized and sent to the deeplearning4j trained model for recognition:

The way it "guess" the digit is like the "hello world" for  deep neural networks. A neuron roughly mimics the human brain neuron and it has a weight, which controls when the neuron is activated. A neural network consists of many neurons linked to each other and organized in layers. What we do is provide to our neural network labeled data and adjust the weights of our neurons until it is able to correctly predict values for the given data, this is called training.

Once it is trained, we test the neural network against known labeled data to measure the neural network precision (in our case the precision is 97.5%!). In our case we use the famous MNIST database.

Because it has hidden layers between the input layer (where we input our data) and the output layer (where we get our predictions), we call it deep neural network. We have many other concepts and types of neural networks, I encourage you to watch some videos about the subject on youtube.

And if it is the first time you reading about this stuff, be aware that it won't be the last time!

If you try the code you may find that it is not so precise as this web application, for example. The reason is that I didn't handle the image precisely before sending it for prediction, we just resize it to 28x28 pixels as required by our trained model.

The code of the JavaFX application is below and the full project is on my github, including the training Java code, which was created using deeplearning4j examples.

sábado, 29 de abril de 2017

Control Arduino from a JavaFX application

That's exciting. I just finished a post about JArduino and Java and I was supposed to sleep, but I wanted to give it a try with JavaFX, so I did and in less than a minute I could make a first experiment!

Show a light sensor information in a label

In Control Arduino from Java using JArduino post we printed on console the current light intensity coming from a LDR connected to an arduino. In less than a minute I could add it to a label in a JavaFX application, see:

 I basically reused the same class from the other post and read the output in a JavaFX thread (see Platform.runLater on the code below).

My plan was to also turn on a LED from the JavaFX application. So let's continue on the original plan.

Controlling a LED and reading a LDR

In the second version I used a chart to display the LDR sensor data in real time and also used a button to control a LED, so when we turn the LED on we can see the values changing.

See our circuit now and notice a LED on digital pin D1:

Now the code on LightSensorApp (which extends JArduino) was modified to include also a LED command. The JavaFX application still look simple, we have a button a chart.

The following video shows it in action:

The code is below and we didn't modify the maven pom.xml, so please check the only two Java files we have in our project.

The code used in these projects are on my github.

What's next?

You tell me. This is so exciting and my head explode about the possibilities. Think about using API with the great hansolo libraries, such as TilesFX and Medusa. Or about integrating your business process with "things" using the JArduino ethernet integration and jBPM.... The list goes on and on.
NOTE: I know and understand that we have many other modern boards with modern APIs, but arduino is, easy, cheap and popular which makes it really exciting!

Control Arduino from Java using JArduino

In general when we want to integrate Java with Arduino we can use the serial port for interaction, where arduino will receive commands and also write sensors information. This is greatly explained on JavaFX 8: Introduction by Example book on Arduino chapter. The problem with this approach is that you will end having to maintain two distinct codes in two different languages.

Another approach is use JArduino, which allows you to write Java code to directly interact with arduino! What you should do is follow the procedure from README file in JArduino page to install the firmware in your arduino board, then build a maven project that has jarduino as a maven dependency to start building Java applications that uses arduino. Since it is Java we can use JavaFX, JavaEE and all the Java APIs!


I always start writing before having something working (that's the reason why I had more than 50 draft posts on this blog). So I faced some unwanted issue and I had to figure out a few things to get this working (in fact, it is still not working, but if someone is reading this, it worked).

The error that took me a few minutes was:

java.lang.UnsatisfiedLinkError: no rxtxSerial64 in java.library.path thrown while loading

This comes from the missing native rxtx library. Just download the one required for your system here and place in your JDK lib directory, in my case it is: /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64

That was the major issue, a simple one to be honest.

Reading a light sensor (LDR) from Java

The Hello World jArduino tutorial is the classic blink app. I wanted something a little more interactive, so I started creating a simple Java application that reads an analog input connected to a LDR, just like described in this arduino tutorial, but with Java. Things to note about the code:

  • The JArduino class mimics an Arduino sketch. You just have to extend JArduino and implement your stuff on loop and setup methods, just like I meant to this on Quick visual effects apps development with JavaFX but with Processing;
  • I am using Maven and I had to add some extra repositories to my pom.xml because some dependencies are not available in maven central;
  • The only thing I did to prepare my arduino to be used with jArduino was upload the jArduino sketch to it. But remember to provide the USB port where arduino is connected when creating the jArduino application
  •  The physical circuit is the same  from the Using an LDR Sensor with Arduino: A Tutorial for Beginners tutorial, the only difference is that I am using a brazilian arduino called "garagino", which I bought in a promotion years ago! See:

And here's the code:

The result is simple the light intensity on console. If I cover the sensor, the number decrease, see:

Now let' s add something visual to it, let's use JavaFX! Next post I will share a version with JavaFX.