Commit 62751e06 authored by Elias Englmeier's avatar Elias Englmeier
Browse files

added a new service for data analytics

parent 0b8b658b
......@@ -5,5 +5,7 @@ Export-Package:\
analysisService.exception,\
analysisService.model,\
analysisService.service,\
analysisService.service.persistence
analysisService.service.persistence,\
analysisSerivce.data.machinelearning,\
org.tensorflow.*
-includeresource: META-INF/service.xml=../analysisService-service/service.xml
\ No newline at end of file
......@@ -3,6 +3,8 @@ dependencies {
compileOnly group: "com.liferay", name: "com.liferay.osgi.util", version: "3.0.0"
compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
compileOnly group: "org.osgi", name: "org.osgi.core", version: "6.0.0"
compile group: 'org.tensorflow', name: 'tensorflow', version: '1.4.0'
compile group: 'nz.ac.waikato.cms.weka', name: 'weka-stable', version: '3.8.0'
}
\ No newline at end of file
compile group: 'org.tensorflow', name: 'libtensorflow_jni', version: '1.4.0'
compile group: 'org.tensorflow', name: 'libtensorflow', version: '1.4.0'
}
......@@ -34,6 +34,7 @@ import com.liferay.portal.kernel.transaction.Propagation;
import com.liferay.portal.kernel.transaction.Transactional;
import com.liferay.portal.kernel.util.OrderByComparator;
import java.io.File;
import java.io.Serializable;
import java.util.List;
......@@ -147,6 +148,8 @@ public interface AnalysisLocalService extends BaseLocalService,
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public int getAnalysisesCount();
public int predictImage(File file, byte[] model);
/**
* Returns the OSGi service identifier.
*
......
......@@ -149,6 +149,10 @@ public class AnalysisLocalServiceUtil {
return getService().getAnalysisesCount();
}
public static int predictImage(java.io.File file, byte[] model) {
return getService().predictImage(file, model);
}
/**
* Returns the OSGi service identifier.
*
......
......@@ -153,6 +153,11 @@ public class AnalysisLocalServiceWrapper implements AnalysisLocalService,
return _analysisLocalService.getAnalysisesCount();
}
@Override
public int predictImage(java.io.File file, byte[] model) {
return _analysisLocalService.predictImage(file, model);
}
/**
* Returns the OSGi service identifier.
*
......
......@@ -56,4 +56,7 @@ public interface AnalysisService extends BaseService {
* @return the OSGi service identifier
*/
public java.lang.String getOSGiServiceIdentifier();
@JSONWebService(method = "GET")
public java.lang.String testCall();
}
\ No newline at end of file
......@@ -51,6 +51,10 @@ public class AnalysisServiceUtil {
return getService().getOSGiServiceIdentifier();
}
public static java.lang.String testCall() {
return getService().testCall();
}
public static AnalysisService getService() {
return _serviceTracker.getService();
}
......
......@@ -42,6 +42,11 @@ public class AnalysisServiceWrapper implements AnalysisService,
return _analysisService.getOSGiServiceIdentifier();
}
@Override
public java.lang.String testCall() {
return _analysisService.testCall();
}
@Override
public AnalysisService getWrappedService() {
return _analysisService;
......
//package analysisService.data.machinelearning;
//import java.io.File;
//import java.io.IOException;
//
//import org.datavec.image.loader.ImageLoader;
//import org.deeplearning4j.nn.graph.ComputationGraph;
//import org.deeplearning4j.nn.modelimport.keras.trainedmodels.TrainedModels;
//import org.deeplearning4j.zoo.PretrainedType;
//import org.deeplearning4j.zoo.ZooModel;
//import org.deeplearning4j.zoo.model.VGG16;
//import org.nd4j.linalg.api.ndarray.INDArray;
//import org.nd4j.linalg.dataset.api.preprocessor.DataNormalization;
//import org.nd4j.linalg.dataset.api.preprocessor.VGG16ImagePreProcessor;
//
//public class ImageLabelerDL4J {
//
// private ComputationGraph vgg16;
//
// public ImageLabelerDL4J(){
// ZooModel zooModel = new VGG16();
// try {
// ComputationGraph vgg16 = (ComputationGraph) zooModel.initPretrained(PretrainedType.IMAGENET);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
//
// public String predictFile(File file) throws IOException{
// // Convert file to INDArray
// ImageLoader loader = new ImageLoader(224, 224, 3);
// INDArray image = loader.asMatrix(file);
//
// // delete the physical file
// file.delete();
//
// // Mean subtraction pre-processing step for VGG
// DataNormalization scaler = new VGG16ImagePreProcessor();
// scaler.transform(image);
//
// //Inference returns array of INDArray, index[0] has the predictions
// INDArray[] output = vgg16.output(false,image);
//
// // convert 1000 length numeric index of probabilities per label
// // to sorted return top 5 convert to string using helper function VGG16.decodePredictions
// // "predictions" is string of our results
// String predictions = TrainedModels.VGG16.decodePredictions(output[0]);
//
// // return results along with form to run another inference
// return predictions;
// }
//
//
//}
......@@ -16,9 +16,10 @@ import org.tensorflow.types.UInt8;
/** THIS IS JUST A CODE EXAMPLE AND NOT WORKING IN ANY WAY!
* Sample use of the TensorFlow Java API to label images using a pre-trained model. */
public class ImageLabeler {
public class ImageLabelerTensorFlow {
private static Tensor<Float> constructAndExecuteGraphToNormalizeImage(byte[] imageBytes) {
public Tensor<Float> constructAndExecuteGraphToNormalizeImage(byte[] imageBytes) {
try (Graph g = new Graph()) {
GraphBuilder b = new GraphBuilder(g);
// Some constants specific to the pre-trained model at:
......@@ -52,8 +53,7 @@ public class ImageLabeler {
}
}
private static float[] executeInceptionGraph(byte[] graphDef, Tensor<Float> image) {
public float[] executeInceptionGraph(byte[] graphDef, Tensor<Float> image) {
try (Graph g = new Graph()) {
g.importGraphDef(graphDef);
try (Session s = new Session(g);
......@@ -62,8 +62,8 @@ public class ImageLabeler {
final long[] rshape = result.shape();
if (result.numDimensions() != 2 || rshape[0] != 1) {
throw new RuntimeException(
String.format(
"Expected model to produce a [1 N] shaped tensor where N is the number of labels, instead it produced one with shape %s",
String.format("Expected model to produce a [1 N] shaped tensor where N is the number of labels,"
+ " instead it produced one with shape %s",
Arrays.toString(rshape)));
}
int nlabels = (int) rshape[1];
......@@ -72,7 +72,7 @@ public class ImageLabeler {
}
}
private static int maxIndex(float[] probabilities) {
public int maxIndex(float[] probabilities) {
int best = 0;
for (int i = 1; i < probabilities.length; ++i) {
if (probabilities[i] > probabilities[best]) {
......@@ -82,7 +82,7 @@ public class ImageLabeler {
return best;
}
private static byte[] readAllBytesOrExit(Path path) {
private byte[] readAllBytesOrExit(Path path) {
try {
return Files.readAllBytes(path);
} catch (IOException e) {
......@@ -92,7 +92,7 @@ public class ImageLabeler {
return null;
}
private static List<String> readAllLinesOrExit(Path path) {
private List<String> readAllLinesOrExit(Path path) {
try {
return Files.readAllLines(path, Charset.forName("UTF-8"));
} catch (IOException e) {
......@@ -102,7 +102,7 @@ public class ImageLabeler {
return null;
}
private static class GraphBuilder {
private class GraphBuilder {
GraphBuilder(Graph g) {
this.g = g;
}
......
package analysisService.data.mining;
import ideaService.service.IdeasLocalServiceUtil;
import projectService.service.ProjectLocalServiceUtil;
import weka.core.Attribute;
public class Classifiers {
......
......@@ -16,12 +16,21 @@ package analysisService.service.http;
import aQute.bnd.annotation.ProviderType;
import analysisService.service.AnalysisServiceUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.security.auth.HttpPrincipal;
import com.liferay.portal.kernel.service.http.TunnelUtil;
import com.liferay.portal.kernel.util.MethodHandler;
import com.liferay.portal.kernel.util.MethodKey;
/**
* Provides the HTTP utility for the
* {@link analysisService.service.AnalysisServiceUtil} service utility. The
* {@link AnalysisServiceUtil} service utility. The
* static methods of this class calls the same methods of the service utility.
* However, the signatures are different because it requires an additional
* {@link com.liferay.portal.kernel.security.auth.HttpPrincipal} parameter.
* {@link HttpPrincipal} parameter.
*
* <p>
* The benefits of using the HTTP utility is that it is fast and allows for
......@@ -40,10 +49,37 @@ import aQute.bnd.annotation.ProviderType;
*
* @author Brian Wing Shun Chan
* @see AnalysisServiceSoap
* @see com.liferay.portal.kernel.security.auth.HttpPrincipal
* @see analysisService.service.AnalysisServiceUtil
* @see HttpPrincipal
* @see AnalysisServiceUtil
* @generated
*/
@ProviderType
public class AnalysisServiceHttp {
public static java.lang.String testCall(HttpPrincipal httpPrincipal) {
try {
MethodKey methodKey = new MethodKey(AnalysisServiceUtil.class,
"testCall", _testCallParameterTypes0);
MethodHandler methodHandler = new MethodHandler(methodKey);
Object returnObj = null;
try {
returnObj = TunnelUtil.invoke(httpPrincipal, methodHandler);
}
catch (Exception e) {
throw new com.liferay.portal.kernel.exception.SystemException(e);
}
return (java.lang.String)returnObj;
}
catch (com.liferay.portal.kernel.exception.SystemException se) {
_log.error(se, se);
throw se;
}
}
private static Log _log = LogFactoryUtil.getLog(AnalysisServiceHttp.class);
private static final Class<?>[] _testCallParameterTypes0 = new Class[] { };
}
\ No newline at end of file
......@@ -16,9 +16,16 @@ package analysisService.service.http;
import aQute.bnd.annotation.ProviderType;
import analysisService.service.AnalysisServiceUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import java.rmi.RemoteException;
/**
* Provides the SOAP utility for the
* {@link analysisService.service.AnalysisServiceUtil} service utility. The
* {@link AnalysisServiceUtil} service utility. The
* static methods of this class calls the same methods of the service utility.
* However, the signatures are different because it is difficult for SOAP to
* support certain types.
......@@ -53,9 +60,23 @@ import aQute.bnd.annotation.ProviderType;
* @author Brian Wing Shun Chan
* @see AnalysisServiceHttp
* @see analysisService.model.AnalysisSoap
* @see analysisService.service.AnalysisServiceUtil
* @see AnalysisServiceUtil
* @generated
*/
@ProviderType
public class AnalysisServiceSoap {
public static java.lang.String testCall() throws RemoteException {
try {
java.lang.String returnValue = AnalysisServiceUtil.testCall();
return returnValue;
}
catch (Exception e) {
_log.error(e, e);
throw new RemoteException(e.getMessage());
}
}
private static Log _log = LogFactoryUtil.getLog(AnalysisServiceSoap.class);
}
\ No newline at end of file
......@@ -14,6 +14,15 @@
package analysisService.service.impl;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.tensorflow.Tensor;
import analysisService.data.machinelearning.ImageLabelerTensorFlow;
import analysisService.service.base.AnalysisLocalServiceBaseImpl;
/**
* The implementation of the analysis local service.
......@@ -35,4 +44,20 @@ public class AnalysisLocalServiceImpl extends AnalysisLocalServiceBaseImpl {
*
* Never reference this class directly. Always use {@link analysisService.service.AnalysisLocalServiceUtil} to access the analysis local service.
*/
public int predictImage(File file, byte [] model){
Path path = Paths.get(file.getAbsolutePath());
try {
byte[] data = Files.readAllBytes(path);
ImageLabelerTensorFlow i = new ImageLabelerTensorFlow();
Tensor<Float> ten = i.constructAndExecuteGraphToNormalizeImage(data);
float[] result = i.executeInceptionGraph(model, ten);
return i.maxIndex(result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
}
\ No newline at end of file
......@@ -14,6 +14,9 @@
package analysisService.service.impl;
import com.liferay.portal.kernel.jsonwebservice.JSONWebService;
import analysisService.data.machinelearning.ImageLabelerTensorFlow;
import analysisService.service.base.AnalysisServiceBaseImpl;
/**
......@@ -36,4 +39,11 @@ public class AnalysisServiceImpl extends AnalysisServiceBaseImpl {
*
* Never reference this class directly. Always use {@link analysisService.service.AnalysisServiceUtil} to access the analysis remote service.
*/
@JSONWebService(method = "GET")
public String testCall(){
ImageLabelerTensorFlow i = new ImageLabelerTensorFlow();
return i.getClass().toString();
}
}
\ No newline at end of file
......@@ -13,6 +13,6 @@
##
build.namespace=ANALYSIS
build.number=1
build.date=1510754149528
build.number=12
build.date=1511193637994
build.auto.upgrade=true
\ No newline at end of file
......@@ -13,6 +13,6 @@
##
build.namespace=GB
build.number=22
build.date=1510754154157
build.number=33
build.date=1511193642506
build.auto.upgrade=true
\ No newline at end of file
......@@ -13,6 +13,6 @@
##
build.namespace=IDEA
build.number=158
build.date=1510754157471
build.number=169
build.date=1511193645483
build.auto.upgrade=true
\ No newline at end of file
......@@ -13,6 +13,6 @@
##
build.namespace=IDEA
build.number=158
build.date=1510754157471
build.number=169
build.date=1511193645483
build.auto.upgrade=true
\ No newline at end of file
......@@ -13,6 +13,6 @@
##
build.namespace=PROJECT
build.number=70
build.date=1510754160365
build.number=81
build.date=1511193648087
build.auto.upgrade=true
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment