|
|
@@ -7,11 +7,7 @@ import com.example.controller.vo.ModelVO;
|
|
|
import com.example.dao.*;
|
|
|
import com.example.data.FitTestData;
|
|
|
import com.example.data.LibsvmAdapter;
|
|
|
-import com.example.entity.DLModel;
|
|
|
-import com.example.entity.Model;
|
|
|
-import com.example.entity.ModelType;
|
|
|
-import com.example.entity.PicFile;
|
|
|
-import com.example.entity.inner.ModelArgument;
|
|
|
+import com.example.entity.*;
|
|
|
import com.example.model.classification.*;
|
|
|
import com.example.model.cluster.KmeansCluster;
|
|
|
import com.example.model.helper.Utils;
|
|
|
@@ -19,13 +15,12 @@ import com.example.model.regression.RFRegression;
|
|
|
import com.example.services.pojo.DLGeneralModelPojo;
|
|
|
import com.example.services.pojo.DLModelPojo;
|
|
|
import com.example.services.pojo.ModelPojo;
|
|
|
-import com.example.services.pojo.ModelTypePojo;
|
|
|
import com.example.util.Json2Object;
|
|
|
-import com.example.util.Object2Json;
|
|
|
import lombok.Data;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import lombok.val;
|
|
|
import org.apache.spark.ml.PipelineModel;
|
|
|
+import org.apache.spark.ml.classification.LogisticRegressionModel;
|
|
|
+import org.apache.spark.ml.clustering.KMeansModel;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
@@ -92,7 +87,6 @@ public class ModelService {
|
|
|
*/
|
|
|
|
|
|
public int addModel(ModelVO modelVO){
|
|
|
-
|
|
|
Model modelInfo = new Model();
|
|
|
modelInfo.setFileInfoId(Integer.parseInt(modelVO.getFileInfoId()));
|
|
|
modelInfo.setConfigId(Integer.parseInt(modelVO.getConfigId()));
|
|
|
@@ -104,6 +98,7 @@ public class ModelService {
|
|
|
modelInfo.setArguments(JSON.toJSONString(modelVO.getArguments()));
|
|
|
return modelDao.save(modelInfo).getId();
|
|
|
}
|
|
|
+
|
|
|
public int addDlModel(DLModelVO dlModelVO) {
|
|
|
DLModel dlModel = new DLModel();
|
|
|
dlModel.setModelTypeId(Integer.parseInt(dlModelVO.getModelTypeId()));
|
|
|
@@ -121,20 +116,16 @@ public class ModelService {
|
|
|
* @return
|
|
|
*/
|
|
|
public List<ModelPojo> getAllModelByUserId(int userId){
|
|
|
-
|
|
|
- val rsl = configDao.findByUserId(userId).stream().map((config) -> {
|
|
|
+ List<ModelPojo> rsl = configDao.findByUserId(userId).stream().map((config) -> {
|
|
|
int configId = config.getId();
|
|
|
String configName = config.getConfName();
|
|
|
- //val modelAllConfig = new ArrayList<ModelPojo>();
|
|
|
- List modelPerConfig = modelDao.findByConfigId(configId).stream().map((model) -> {
|
|
|
+ List<ModelPojo> modelPerConfig = modelDao.findByConfigId(configId).stream().map((model) -> {
|
|
|
ModelType modelType = modelTypeDao.findById(model.getModelTypeId());
|
|
|
int flag = -1;
|
|
|
if(model.isTrained() && model.getResOfModel()!=null){
|
|
|
flag = 1;
|
|
|
}else if(model.isTrained() && model.getResOfModel()==null){
|
|
|
flag = 0;
|
|
|
- }else{
|
|
|
- flag = -1;
|
|
|
}
|
|
|
return new ModelPojo(String.valueOf(configId),
|
|
|
configName,
|
|
|
@@ -164,7 +155,7 @@ public class ModelService {
|
|
|
*/
|
|
|
public List<DLGeneralModelPojo> getAllGeneralModelByUserId(int userId){
|
|
|
List<PicFile> picFiles = pictureFileDao.findByUserId(userId);
|
|
|
- val result = picFiles.stream().map(picFile -> {
|
|
|
+ List<DLGeneralModelPojo> result = picFiles.stream().map(picFile -> {
|
|
|
List<DLModel> dlModels = dlModelDao.findByPicFileId(picFile.getId());
|
|
|
return dlModels.stream()
|
|
|
.map(dlModel -> new DLGeneralModelPojo(String.valueOf(dlModel.getId()), dlModel.getModelName(), String.valueOf(picFile.getId()), picFile.getFileName(),dlModel.isTrained()
|
|
|
@@ -201,16 +192,11 @@ public class ModelService {
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean changeModel(int modelId, String modelName, int modelTypeId){
|
|
|
-
|
|
|
Model model = modelDao.findById(modelId);
|
|
|
model.setModelName(modelName);
|
|
|
model.setModelTypeId(modelTypeId);
|
|
|
- val modelObject = modelDao.save(model);
|
|
|
- if(modelObject!=null){
|
|
|
- return true;
|
|
|
- }else {
|
|
|
- return false;
|
|
|
- }
|
|
|
+ modelDao.save(model);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -219,11 +205,10 @@ public class ModelService {
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean changeStatusOfTrained(int modelId, boolean trained){
|
|
|
-
|
|
|
Model model = modelDao.findById(modelId);
|
|
|
model.setTrained(trained);
|
|
|
- val modelObject = modelDao.save(model);
|
|
|
- return modelObject != null;
|
|
|
+ modelDao.save(model);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -233,7 +218,7 @@ public class ModelService {
|
|
|
* @param model
|
|
|
* @return
|
|
|
*/
|
|
|
- public boolean updataResOfModel(int modelId, HashMap<String, String> resOfModel, Object model){
|
|
|
+ public boolean updateResOfModel(int modelId, HashMap<String, String> resOfModel, Object model){
|
|
|
Model modelOfGet = modelDao.findById(modelId);
|
|
|
//log.info("\n\nmodel:"+JSON.toJSONString(model)+"\n\n");
|
|
|
//T.B.D.
|
|
|
@@ -241,38 +226,32 @@ public class ModelService {
|
|
|
modelOfGet.setResOfModel(JSON.toJSONString(resOfModel));
|
|
|
modelOfGet.setTrained(true);
|
|
|
log.info("\n\nstore model result: "+modelOfGet.getResOfModel());
|
|
|
- val modelObject = modelDao.save(modelOfGet);
|
|
|
- if(modelObject!=null){
|
|
|
- return true;
|
|
|
- }else {
|
|
|
- return false;
|
|
|
- }
|
|
|
+ modelDao.save(modelOfGet);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean trainTheModel(int modelId) throws Exception{
|
|
|
- //change training flag to true
|
|
|
-
|
|
|
|
|
|
+ //change training flag to true
|
|
|
changeStatusOfTrained(modelId, true);
|
|
|
|
|
|
- val trainingModel = modelDao.findById(modelId);
|
|
|
- val modelType1 = modelTypeDao.findById(trainingModel.getModelTypeId());
|
|
|
- val configInfo = configDao.findById(trainingModel.getConfigId());
|
|
|
- val features = JSONObject.parseArray(configInfo.getFieldIds(),String.class).stream().map(fieldId ->
|
|
|
- headerInfoDao.findById(Integer.parseInt(fieldId)).getFieldName()
|
|
|
- ).collect(Collectors.toList());
|
|
|
+ Model trainingModel = modelDao.findById(modelId);
|
|
|
+ ModelType modelType = modelTypeDao.findById(trainingModel.getModelTypeId());
|
|
|
+ Config configInfo = configDao.findById(trainingModel.getConfigId());
|
|
|
+ List<String> features = JSONObject.parseArray(configInfo.getFieldIds(),String.class).stream().map(fieldId ->
|
|
|
+ headerInfoDao.findById(Integer.parseInt(fieldId)).getFieldName()).collect(Collectors.toList());
|
|
|
|
|
|
//1. transfer file to libsvm
|
|
|
log.info("\n\nTransfer to LibSVM...\n\n");
|
|
|
String keyFeature = features.get(features.size()-1);
|
|
|
features.remove(features.size()-1);
|
|
|
- val featureNum = features.size();
|
|
|
- String libsvm = libsvmAdapter.parquet2Libsvm(configInfo.getFileInfoId(), keyFeature, features.toArray(new String[features.size()]));
|
|
|
+ int featureNum = features.size();
|
|
|
+ String libsvm = libsvmAdapter.parquet2Libsvm(configInfo.getFileInfoId(), keyFeature, features.toArray(new String[0]));
|
|
|
|
|
|
- String modelDetailName = modelType1.getModelDetailName();
|
|
|
- val modelArguments = new Json2Object().Json2HashMap(trainingModel.getArguments());
|
|
|
+ String modelDetailName = modelType.getModelDetailName();
|
|
|
+ HashMap<String,String> modelArguments = new Json2Object().Json2HashMap(trainingModel.getArguments());
|
|
|
boolean rsl = false;
|
|
|
try {
|
|
|
switch (modelDetailName) {
|
|
|
@@ -282,15 +261,15 @@ public class ModelService {
|
|
|
regParam: Float,
|
|
|
elasticNetParam: Float
|
|
|
* */
|
|
|
- val lrModel = lrClassification.lrTraining(libsvm,
|
|
|
+ LogisticRegressionModel lrModel = lrClassification.lrTraining(libsvm,
|
|
|
Integer.parseInt(modelArguments.get("MaxIter")),
|
|
|
Float.parseFloat(modelArguments.get("RegParam")),
|
|
|
Float.parseFloat(modelArguments.get("ElasticNetParam")));
|
|
|
log.info("\n\nstore Model result\n\n");
|
|
|
- val lrRst = lrClassification.lrResult(lrModel);
|
|
|
+ HashMap<String,String> lrRst = lrClassification.lrResult(lrModel);
|
|
|
|
|
|
log.info("\n\nupdate result of model\n\n");
|
|
|
- rsl = updataResOfModel(modelId, lrRst, lrModel);
|
|
|
+ rsl = updateResOfModel(modelId, lrRst, lrModel);
|
|
|
break;
|
|
|
case "DecisionTree":
|
|
|
/*
|
|
|
@@ -298,12 +277,12 @@ public class ModelService {
|
|
|
regParam: Float,
|
|
|
elasticNetParam: Float
|
|
|
* */
|
|
|
- val dtModel = dtClassification.dtTraining(libsvm,
|
|
|
+ PipelineModel dtModel = dtClassification.dtTraining(libsvm,
|
|
|
Integer.parseInt(modelArguments.get("Category")),
|
|
|
Float.parseFloat(modelArguments.get("TrainingSetOccupy")));
|
|
|
- val dtResult = dtClassification.dtResult(dtModel, modelId);
|
|
|
+ HashMap<String,String> dtResult = dtClassification.dtResult(dtModel, modelId);
|
|
|
|
|
|
- rsl = updataResOfModel(modelId, dtResult, dtModel);
|
|
|
+ rsl = updateResOfModel(modelId, dtResult, dtModel);
|
|
|
break;
|
|
|
case "RandomForest":
|
|
|
/*
|
|
|
@@ -312,12 +291,12 @@ public class ModelService {
|
|
|
elasticNetParam: Float
|
|
|
* */
|
|
|
|
|
|
- val rfModel = rfClassification.dtTraining(libsvm,
|
|
|
+ PipelineModel rfModel = rfClassification.dtTraining(libsvm,
|
|
|
Integer.parseInt(modelArguments.get("Category")),
|
|
|
Float.parseFloat(modelArguments.get("TrainingDataSetOccupy")));
|
|
|
- val rfResult = rfClassification.dtResult(rfModel, modelId);
|
|
|
+ HashMap<String,String> rfResult = rfClassification.dtResult(rfModel, modelId);
|
|
|
|
|
|
- rsl = updataResOfModel(modelId,rfResult,rfModel);
|
|
|
+ rsl = updateResOfModel(modelId,rfResult,rfModel);
|
|
|
break;
|
|
|
case "GBDT":
|
|
|
/*
|
|
|
@@ -326,50 +305,46 @@ public class ModelService {
|
|
|
elasticNetParam: Float
|
|
|
* */
|
|
|
|
|
|
- val gbtModel = gdbtClassification.dtTraining(libsvm,
|
|
|
+ PipelineModel gbtModel = gdbtClassification.dtTraining(libsvm,
|
|
|
Integer.parseInt(modelArguments.get("Category")),
|
|
|
Float.parseFloat(modelArguments.get("TrainingDataSetOccupy")));
|
|
|
- val gbtResult = gdbtClassification.dtResult(gbtModel, modelId);
|
|
|
+ HashMap<String,String> gbtResult = gdbtClassification.dtResult(gbtModel, modelId);
|
|
|
|
|
|
- rsl = updataResOfModel(modelId,gbtResult,gbtModel);
|
|
|
+ rsl = updateResOfModel(modelId,gbtResult,gbtModel);
|
|
|
break;
|
|
|
- case "K-Means":
|
|
|
|
|
|
- val kmeansModel = kmeansCluster.dtTraining(libsvm,
|
|
|
+ case "K-Means":
|
|
|
+ KMeansModel kmeansModel = kmeansCluster.dtTraining(libsvm,
|
|
|
Integer.parseInt(modelArguments.get("NumOfCluster")),
|
|
|
Integer.parseInt(modelArguments.get("Seed")));
|
|
|
- val kmeansResult = kmeansCluster.dtResult(kmeansModel);
|
|
|
- rsl = updataResOfModel(modelId,kmeansResult,kmeansModel);
|
|
|
+ HashMap<String,String> kmeansResult = kmeansCluster.dtResult(kmeansModel);
|
|
|
+ rsl = updateResOfModel(modelId,kmeansResult,kmeansModel);
|
|
|
break;
|
|
|
- case "MultilayerPerceptronClassifier":
|
|
|
|
|
|
- val mlpc = mulPerClassification.dtTraining(libsvm,
|
|
|
+ case "MultilayerPerceptronClassifier":
|
|
|
+ PipelineModel mlpc = mulPerClassification.dtTraining(libsvm,
|
|
|
Integer.parseInt(modelArguments.get("Category")),
|
|
|
Float.parseFloat(modelArguments.get("TrainingDataSetOccupy")), featureNum);
|
|
|
- val mlpcResult = mulPerClassification.dtResult(mlpc, modelId);
|
|
|
-
|
|
|
- rsl = updataResOfModel(modelId,mlpcResult,mlpc);
|
|
|
+ HashMap<String,String> mlpcResult = mulPerClassification.dtResult(mlpc, modelId);
|
|
|
+ rsl = updateResOfModel(modelId,mlpcResult,mlpc);
|
|
|
break;
|
|
|
- case "NaiveBayes":
|
|
|
|
|
|
- val nby = naiveBayesClassification.dtTraining(libsvm,
|
|
|
+ case "NaiveBayes":
|
|
|
+ PipelineModel nby = naiveBayesClassification.dtTraining(libsvm,
|
|
|
Integer.parseInt(modelArguments.get("Category")),
|
|
|
Float.parseFloat(modelArguments.get("TrainingDataSetOccupy")));
|
|
|
- val nbyResult = naiveBayesClassification.dtResult(nby, modelId);
|
|
|
-
|
|
|
- rsl = updataResOfModel(modelId,nbyResult,nby);
|
|
|
+ HashMap<String,String> nbyResult = naiveBayesClassification.dtResult(nby, modelId);
|
|
|
+ rsl = updateResOfModel(modelId,nbyResult,nby);
|
|
|
break;
|
|
|
|
|
|
case "RandomForestRegression":
|
|
|
-
|
|
|
- val rfReg = rfRegression.dtTraining(libsvm,
|
|
|
+ PipelineModel rfReg = rfRegression.dtTraining(libsvm,
|
|
|
Integer.parseInt(modelArguments.get("Category")),
|
|
|
Float.parseFloat(modelArguments.get("TrainingDataSetOccupy"))
|
|
|
);
|
|
|
- val rfRegResult = rfRegression.dtResult(rfReg);
|
|
|
-
|
|
|
- rsl = updataResOfModel(modelId,rfRegResult,rfReg);
|
|
|
+ HashMap<String,String> rfRegResult = rfRegression.dtResult(rfReg);
|
|
|
|
|
|
+ rsl = updateResOfModel(modelId,rfRegResult,rfReg);
|
|
|
break;
|
|
|
default:
|
|
|
rsl = false;
|
|
|
@@ -396,10 +371,10 @@ public class ModelService {
|
|
|
}
|
|
|
modelDao.delete(modelDao.findById(modelId));
|
|
|
return true;
|
|
|
-
|
|
|
}
|
|
|
+
|
|
|
public List<Map<String, String>> useModel(int fileId, int modelId) throws Exception{
|
|
|
- val res = fitTestData.transformData(fileId,modelId);
|
|
|
+ List<Map<String,String>> res = fitTestData.transformData(fileId,modelId);
|
|
|
fileService.deleteFile(fileId);
|
|
|
return res;
|
|
|
}
|