Liferay and Machine learning with TensorFlow!

“The field of machine learning is concerned with the question of how to construct computer programs that automatically improve with experience” – Tom M. Mitchell.

As Artificial intelligence and Machine learning are in action now, there are various APIs and libraries available with Java too. Let’s look at TensorFlow – TensorFlow is an open source software library for machine learning, developed by Google and currently used in many of their projects. An easy, fast and fun way to get started with it is – Automation of tag assignments in Liferay on uploaded images based on a pre-trained model.

What is needed:

a) TensorFlow libraries:

    • libtensorflow_jni-1.8.0.jar
    • libtensorflow-1.8.0.jar
    • tensorflow-1.8.0.jar

Source: https://mvnrepository.com/artifact/org.tensorflow/tensorflow/1.8.0

b) Inception5h.

    • inception5h.zip (Extract it, this contains Graph file, Model directory and Label file)

Source: https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip

c) Liferay 7:

    Source: https://www.liferay.com/downloads

Lets look at the steps.
1) Portlet developed in Liferay that asks you to upload image.

2) Pass on the uploaded image path to LabelImage.class (TensorFlow-Inception5h), which return the metadata of the image.

Important snippet:

byte[] graphDef = readAllBytesOrExit(Paths.get(MODEL_DIR, GRAPH_FILE));
List labels = readAllLinesOrExit(Paths.get(MODEL_DIR, LABEL_FILE));
byte[] imageBytes = readAllBytesOrExit(Paths.get(imageFile));

try(Tensor image = constructAndExecuteGraphToNormalizeImage(imageBytes)) {
float[] labelProbabilities = executeInceptionGraph(graphDef, image);
int bestLabelIdx = maxIndex(labelProbabilities);
metadata = labels.get(bestLabelIdx);
String output = String.format(“BEST MATCH: %s (%.2f%% likely)”, metadata,
labelProbabilities[bestLabelIdx] * 100f);
}

3) Image is stored in Liferay in with relevant folder, along with metadata tags.

Note: inception5h contains trained model from google and it is test on around 2000+ images (I guess). Result won’t be 100% correct, but that’s all machine learning 🙂

Implementation reference:

  • https://www.tensorflow.org/install/install_java
  • https://github.com/tensorflow/tensorflow/tree/r1.8/tensorflow/java/src/main/java/org/tensorflow/examples

And that’s not all about it, this is just an example. Model used in this example is not trained in context-of/integration-with Liferay. There can be 100s of use case where this can be applied, like predict the user action, predict the prices, navigation suggestion, image recognition and metadata extraction and many more.

With Liferay (integration or in Liferay context), we can look forward to…

  • Training a model with updates.
  • Offline employ batch normalization to speed up training of the model.
  • Transfer learning by taking a pre-trained model and fine-tuning it for another task.

Post by,
Vipul Dave

Share it :

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz
  • Categories

  • Recent Comments

  • Recent Post

  • Tags