April 28, 2022
“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:
Source: https://mvnrepository.com/artifact/org.tensorflow/tensorflow/1.8.0
b) Inception5h.
Source: https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip
c) Liferay 7:
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:
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…
Post by,
Vipul Dave