diff --git a/easyAi.iml b/easyAi.iml new file mode 100644 index 0000000..389499f --- /dev/null +++ b/easyAi.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/wlld/config/Classifier.java b/src/main/java/org/wlld/config/Classifier.java new file mode 100644 index 0000000..0d7bea3 --- /dev/null +++ b/src/main/java/org/wlld/config/Classifier.java @@ -0,0 +1,7 @@ +package org.wlld.config; + +public class Classifier {//分类器 + public static final int LVQ = 1;//LVQ分类 + public static final int DNN = 2; //使用DNN分类 + public static final int VAvg = 3;//使用特征向量均值分类 +} diff --git a/src/main/java/org/wlld/imageRecognition/MaxPoint.java b/src/main/java/org/wlld/imageRecognition/MaxPoint.java new file mode 100644 index 0000000..3da9ab1 --- /dev/null +++ b/src/main/java/org/wlld/imageRecognition/MaxPoint.java @@ -0,0 +1,26 @@ +package org.wlld.imageRecognition; + +import org.wlld.MatrixTools.Matrix; +import org.wlld.i.OutBack; + +public class MaxPoint implements OutBack { + private int id; + private double point; + + public int getId() { + return id; + } + + @Override + public void getBack(double out, int id, long eventId) { + if (out > point) { + point = out; + this.id = id; + } + } + + @Override + public void getBackMatrix(Matrix matrix, long eventId) { + + } +} diff --git a/src/main/java/org/wlld/imageRecognition/Operation.java b/src/main/java/org/wlld/imageRecognition/Operation.java index cf4ee30..56b5fd6 100644 --- a/src/main/java/org/wlld/imageRecognition/Operation.java +++ b/src/main/java/org/wlld/imageRecognition/Operation.java @@ -3,11 +3,13 @@ package org.wlld.imageRecognition; import org.wlld.MatrixTools.Matrix; import org.wlld.MatrixTools.MatrixOperation; +import org.wlld.config.Classifier; import org.wlld.config.StudyPattern; import org.wlld.i.OutBack; import org.wlld.imageRecognition.border.*; import org.wlld.nerveEntity.SensoryNerve; import org.wlld.tools.ArithUtil; +import org.wlld.tools.IdCreator; import java.util.ArrayList; import java.util.HashMap; @@ -55,7 +57,7 @@ public class Operation {//进行计算 public void study(Matrix matrix, Map tagging) throws Exception { if (templeConfig.getStudyPattern() == StudyPattern.Speed_Pattern) { List list = convolution(matrix, tagging); - intoNerve(1, list, templeConfig.getSensoryNerves(), true, tagging, null); + intoDnnNetwork(1, list, templeConfig.getSensoryNerves(), true, tagging, null); } else { throw new Exception("pattern is wrong"); } @@ -74,7 +76,7 @@ public class Operation {//进行计算 isKernelStudy = false; } //进卷积网络 - intoNerve2(1, matrix, templeConfig.getConvolutionNerveManager().getSensoryNerves(), + intoConvolutionNetwork(1, matrix, templeConfig.getConvolutionNerveManager().getSensoryNerves(), isKernelStudy, tagging, matrixBack); if (isNerveStudy) { //卷积后的结果 @@ -82,24 +84,61 @@ public class Operation {//进行计算 if (templeConfig.isHavePosition() && tagging > 0) { border.end(myMatrix, tagging); } - //进行聚类 - LVQ lvq = templeConfig.getLvq(); - Matrix vector = MatrixOperation.matrixToVector(myMatrix, true); - MatrixBody matrixBody = new MatrixBody(); - matrixBody.setMatrix(vector); - matrixBody.setId(tagging); - lvq.insertMatrixBody(matrixBody); + int classifier = templeConfig.getClassifier(); + switch (classifier) { + case Classifier.DNN: + dnn(tagging, myMatrix); + break; + case Classifier.LVQ: + lvq(tagging, myMatrix); + break; + case Classifier.VAvg: + vectorAvg(tagging, myMatrix); + break; + } } } else { throw new Exception("pattern is wrong"); } } + private void dnn(int tagging, Matrix myMatrix) throws Exception {//DNN网络学习 + Map map = new HashMap<>(); + map.put(tagging, 1.0); + List feature = getFeature(myMatrix); + intoDnnNetwork(1, feature, templeConfig.getSensoryNerves(), true, map, null); + } + + private void vectorAvg(int tagging, Matrix myMatrix) throws Exception {//特征矩阵均值学习 + Matrix vector = MatrixOperation.matrixToVector(myMatrix, true); + VectorK vectorK = templeConfig.getVectorK(); + vectorK.insertMatrix(tagging, vector); + } + + private void lvq(int tagging, Matrix myMatrix) throws Exception {//LVQ学习 + LVQ lvq = templeConfig.getLvq(); + Matrix vector = MatrixOperation.matrixToVector(myMatrix, true); + MatrixBody matrixBody = new MatrixBody(); + matrixBody.setMatrix(vector); + matrixBody.setId(tagging); + lvq.insertMatrixBody(matrixBody); + } + + private List getFeature(Matrix matrix) throws Exception {//将特征矩阵转化为集合并除10 + List list = new ArrayList<>(); + for (int i = 0; i < matrix.getX(); i++) { + for (int j = 0; j < matrix.getY(); j++) { + list.add(ArithUtil.div(matrix.getNumber(i, j), 10)); + } + } + return list; + } + //图像视觉 speed 模式 public void look(Matrix matrix, long eventId) throws Exception { if (templeConfig.getStudyPattern() == StudyPattern.Speed_Pattern) { List list = convolution(matrix, null); - intoNerve(eventId, list, templeConfig.getSensoryNerves(), false, null, outBack); + intoDnnNetwork(eventId, list, templeConfig.getSensoryNerves(), false, null, outBack); } else if (templeConfig.getStudyPattern() == StudyPattern.Accuracy_Pattern) { throw new Exception("studyPattern not right"); } @@ -127,18 +166,18 @@ public class Operation {//进行计算 List list = sub(matrix1); imageBack.setFrameBody(frameBody); //进入神经网络判断 - intoNerve(eventId, list, templeConfig.getSensoryNerves(), false, null, imageBack); + intoDnnNetwork(eventId, list, templeConfig.getSensoryNerves(), false, null, imageBack); } return toPosition(frameBodies, frame.getWidth(), frame.getHeight()); } else if (templeConfig.getStudyPattern() == StudyPattern.Accuracy_Pattern) { for (FrameBody frameBody : frameBodies) { - intoNerve2(eventId, frameBody.getMatrix(), templeConfig.getConvolutionNerveManager().getSensoryNerves(), + intoConvolutionNetwork(eventId, frameBody.getMatrix(), templeConfig.getConvolutionNerveManager().getSensoryNerves(), false, -1, matrixBack); Matrix myMatrix = matrixBack.getMatrix(); //卷积层输出即边框回归的输入的特征向量 frameBody.setEndMatrix(myMatrix); - Matrix vector = MatrixOperation.matrixToVector(myMatrix, true); - int id = getClassificationId2(vector); + //Matrix vector = MatrixOperation.matrixToVector(myMatrix, true); + int id = getClassificationIdByLVQ(myMatrix); frameBody.setId(id); } return toPosition(frameBodies, frame.getWidth(), frame.getHeight()); @@ -209,7 +248,7 @@ public class Operation {//进行计算 int id = frameBody.getId(); int x = frameBody.getX(); int y = frameBody.getY(); - KClustering kClustering = templeConfig.getkClusteringMap().get(id); + KClustering kClustering = templeConfig.getKClusteringMap().get(id); Map boxMap = kClustering.getPositionMap(); //将矩阵化为向量 matrix = MatrixOperation.matrixToVector(matrix, true); @@ -285,17 +324,54 @@ public class Operation {//进行计算 //图像视觉 Accuracy 模式 public int toSee(Matrix matrix) throws Exception { if (templeConfig.getStudyPattern() == StudyPattern.Accuracy_Pattern) { - intoNerve2(2, matrix, templeConfig.getConvolutionNerveManager().getSensoryNerves(), - false, -1, matrixBack); + intoConvolutionNetwork(2, matrix, templeConfig.getConvolutionNerveManager().getSensoryNerves(), + false, 0, matrixBack); Matrix myMatrix = matrixBack.getMatrix(); - Matrix vector = MatrixOperation.matrixToVector(myMatrix, true); - return getClassificationId2(vector); + int classifier = templeConfig.getClassifier(); + int id = -1; + switch (classifier) { + case Classifier.LVQ: + id = getClassificationIdByLVQ(myMatrix); + break; + case Classifier.DNN: + id = getClassificationIdByDnn(myMatrix); + break; + case Classifier.VAvg: + id = getClassificationIdByVag(myMatrix); + break; + } + return id; } else { throw new Exception("pattern is wrong"); } } - private int getClassificationId2(Matrix myVector) throws Exception { + private int getClassificationIdByDnn(Matrix myMatrix) throws Exception { + List list = getFeature(myMatrix); + MaxPoint maxPoint = new MaxPoint(); + long id = IdCreator.get().nextId(); + intoDnnNetwork(id, list, templeConfig.getSensoryNerves(), false, null, maxPoint); + return maxPoint.getId(); + } + + private int getClassificationIdByVag(Matrix myMatrix) throws Exception {//VAG获取分类 + Matrix myVector = MatrixOperation.matrixToVector(myMatrix, true); + Map matrixK = templeConfig.getVectorK().getMatrixK(); + double minDist = 0; + int id = 0; + for (Map.Entry entry : matrixK.entrySet()) { + Matrix matrix = entry.getValue(); + double dist = MatrixOperation.getEDist(matrix, myVector); + if (minDist == 0 || dist < minDist) { + minDist = dist; + id = entry.getKey(); + } + } + return id; + } + + private int getClassificationIdByLVQ(Matrix myMatrix) throws Exception {//LVQ获取分类 + Matrix myVector = MatrixOperation.matrixToVector(myMatrix, true); int id = 0; double distEnd = 0; LVQ lvq = templeConfig.getLvq(); @@ -328,18 +404,34 @@ public class Operation {//进行计算 return list; } - private void intoNerve(long eventId, List featureList, List sensoryNerveList - , boolean isStudy, Map map, OutBack imageBack) throws Exception { + /* + * @param eventId 事件ID + * @param featureList 特征集合 + * @param sensoryNerveList 感知神经元集合 + * @param isStudy 是否学习 + * @param map 标注 + * @param outBack 输出结果回调类 + * */ + private void intoDnnNetwork(long eventId, List featureList, List sensoryNerveList + , boolean isStudy, Map map, OutBack outBack) throws Exception {//进入DNN 神经网络 for (int i = 0; i < sensoryNerveList.size(); i++) { sensoryNerveList.get(i).postMessage(eventId, featureList.get(i), isStudy, map - , imageBack); + , outBack); } } - private void intoNerve2(long eventId, Matrix featur, List sensoryNerveList - , boolean isKernelStudy, int E, OutBack outBack) throws Exception { + /* + * @param eventId 事件ID + * @param feature 特征矩阵 + * @param sensoryNerveList 感知神经元集合 + * @param isKernelStudy 是否进行核学习 + * @param E 期望矩阵 + * @param outBack 输出结果回调类 + * */ + private void intoConvolutionNetwork(long eventId, Matrix feature, List sensoryNerveList + , boolean isKernelStudy, int E, OutBack outBack) throws Exception {//进入卷积神经网络 for (int i = 0; i < sensoryNerveList.size(); i++) { - sensoryNerveList.get(i).postMatrixMessage(eventId, featur, isKernelStudy, E, outBack); + sensoryNerveList.get(i).postMatrixMessage(eventId, feature, isKernelStudy, E, outBack); } } } \ No newline at end of file diff --git a/src/main/java/org/wlld/imageRecognition/TempleConfig.java b/src/main/java/org/wlld/imageRecognition/TempleConfig.java index f7ebae4..4525ef7 100644 --- a/src/main/java/org/wlld/imageRecognition/TempleConfig.java +++ b/src/main/java/org/wlld/imageRecognition/TempleConfig.java @@ -1,6 +1,7 @@ package org.wlld.imageRecognition; import org.wlld.MatrixTools.Matrix; +import org.wlld.config.Classifier; import org.wlld.config.StudyPattern; import org.wlld.function.ReLu; import org.wlld.function.Sigmod; @@ -23,7 +24,6 @@ import java.util.Map; public class TempleConfig { private NerveManager nerveManager;//神经网络管理器 private NerveManager convolutionNerveManager;//卷积神经网络管理器 - private double cutThreshold = 10;//切割阈值默认值 private int row = 5;//行的最小比例 private int column = 3;//列的最小比例 private int deep = 2;//默认深度 @@ -35,11 +35,44 @@ public class TempleConfig { private double th = 0.6;//标准阈值 private boolean boxReady = false;//边框已经学习完毕 private double iouTh = 0.5;//IOU阈值 - private int lvqNub = 50;//lvq循环次数,默认50 + private int lvqNub = 30;//lvq循环次数,默认30 + private VectorK vectorK; + private int classifier = Classifier.VAvg;//默认分类类别使用的是向量均值分类 //边框聚类集合 模型需要返回 + + public int getClassifier() { + return classifier; + } + + public void setClassifier(int classifier) {//设置最后使用的分类器 + this.classifier = classifier; + } + + public VectorK getVectorK() { + return vectorK; + } + + public void finishStudy() throws Exception {//结束 + switch (classifier) { + case Classifier.LVQ: + lvq.start(); + break; + case Classifier.VAvg: + vectorK.study(); + break; + } + if (isHavePosition) { + for (Map.Entry entry : kClusteringMap.entrySet()) { + entry.getValue().start(); + } + boxReady = true; + } + + } + private Map kClusteringMap = new HashMap<>(); - public Map getkClusteringMap() { + public Map getKClusteringMap() { return kClusteringMap; } @@ -79,26 +112,10 @@ public class TempleConfig { this.frame = frame; } - public void startLvq() throws Exception {//进行量化 - if (studyPattern == StudyPattern.Accuracy_Pattern) { - lvq.start(); - } - } - public LVQ getLvq() { return lvq; } - public void boxStudy() throws Exception {//边框回归 学习结束之后最后进行调用 - if (isHavePosition) { - for (Map.Entry entry : kClusteringMap.entrySet()) { - entry.getValue().start(); - } - boxReady = true; - } - - } - public NerveManager getNerveManager() { return nerveManager; } @@ -163,15 +180,25 @@ public class TempleConfig { nerveManager.init(initPower, false); } - private void initConvolutionVision(boolean initPower, int width, int height) throws Exception { + private void initConvolutionVision(boolean initPower, int width, int height) throws Exception {//精准模式 int deep = 0; - lvq = new LVQ(classificationNub + 1, lvqNub); Map matrixMap = new HashMap<>();//主键与期望矩阵的映射 while (width > 5 && height > 5) { width = width / 3; height = height / 3; deep++; } + switch (classifier) { + case Classifier.DNN: + initNerveManager(true, height * width, this.deep); + break; + case Classifier.LVQ: + lvq = new LVQ(classificationNub, lvqNub); + break; + case Classifier.VAvg: + vectorK = new VectorK(height * width); + break; + } //加载各识别分类的期望矩阵 matrixMap.put(0, new Matrix(height, width)); double nub = 10;//每个分类期望参数的跨度 @@ -179,7 +206,7 @@ public class TempleConfig { Matrix matrix = new Matrix(height, width);//初始化期望矩阵 double t = (k + 1) * nub;//期望矩阵的分类参数数值 for (int i = 0; i < height; i++) {//给期望矩阵注入期望参数 - for (int j = 0; j < width - 1; j++) { + for (int j = 0; j < width; j++) { matrix.setNub(i, j, t); } } @@ -242,8 +269,6 @@ public class TempleConfig { lists.add(rowVectorToList(matrices[i])); } - } else { - throw new Exception("not ready"); } } @@ -270,7 +295,7 @@ public class TempleConfig { modelParameter.setDepthNerves(modelParameter1.getDepthNerves()); modelParameter.setOutNerves(modelParameter1.getOutNerves()); } - if (isHavePosition) {//存在边框学习模型参数 + if (isHavePosition && kClusteringMap != null && kClusteringMap.size() > 0) {//存在边框学习模型参数 Map kBorderMap = kToBody(); modelParameter.setFrame(frame); modelParameter.setBorderMap(kBorderMap); @@ -376,16 +401,11 @@ public class TempleConfig { boxMap.put(boxEntry.getKey(), box); } } - } } } } - public void setCutThreshold(double cutThreshold) { - this.cutThreshold = cutThreshold; - } - public int getDeep() { return deep; } diff --git a/src/main/java/org/wlld/imageRecognition/VectorK.java b/src/main/java/org/wlld/imageRecognition/VectorK.java new file mode 100644 index 0000000..a9333cd --- /dev/null +++ b/src/main/java/org/wlld/imageRecognition/VectorK.java @@ -0,0 +1,74 @@ +package org.wlld.imageRecognition; + +import org.wlld.MatrixTools.Matrix; +import org.wlld.MatrixTools.MatrixOperation; +import org.wlld.tools.ArithUtil; + +import java.util.*; + +public class VectorK { + private Map> matrixMap = new HashMap<>(); + private Map matrixK = new HashMap<>(); + private int length; + + public VectorK(int length) { + this.length = length; + } + + public Map getMatrixK() { + return matrixK; + } + + public void insertMatrix(int type, Matrix matrix) throws Exception { + if (matrix.isRowVector() && matrix.getY() == length) { + if (matrixMap.containsKey(type)) { + List matrixList = matrixMap.get(type); + matrixList.add(matrix); + } else { + List list = new ArrayList<>(); + list.add(matrix); + matrixMap.put(type, list); + } + } else { + throw new Exception("MATRIX IS NOT RIGHT"); + } + } + + private Matrix sigma(List matrixList) throws Exception { + Matrix matrix = new Matrix(1, length); + for (Matrix matrix1 : matrixList) { + matrix = MatrixOperation.add(matrix, matrix1); + } + MatrixOperation.mathMul(matrix, ArithUtil.div(1, matrixList.size())); + return matrix; + } + + private Matrix mind(List matrixList) throws Exception {//拿中位数 + Matrix matrix = new Matrix(1, length); + List> lists = new ArrayList<>(); + for (Matrix matrix1 : matrixList) { + for (int i = 0; i < matrix1.getY(); i++) { + if (lists.size() <= i) { + lists.add(new ArrayList<>()); + } + List list = lists.get(i); + list.add(matrix1.getNumber(0, i)); + Collections.sort(list); + } + } + for (int i = 0; i < length; i++) { + List list = lists.get(i); + int index = list.size() / 2; + matrix.setNub(0, i, list.get(index)); + } + return matrix; + } + + public void study() throws Exception { + for (Map.Entry> entry : matrixMap.entrySet()) { + List matrixList = entry.getValue(); + Matrix matrix = sigma(matrixList); + matrixK.put(entry.getKey(), matrix); + } + } +} diff --git a/src/main/java/org/wlld/imageRecognition/border/KClustering.java b/src/main/java/org/wlld/imageRecognition/border/KClustering.java index 789f92a..69ad9f8 100644 --- a/src/main/java/org/wlld/imageRecognition/border/KClustering.java +++ b/src/main/java/org/wlld/imageRecognition/border/KClustering.java @@ -111,12 +111,14 @@ public class KClustering { } private Matrix averagePosition(List boxes) throws Exception { - double nub = ArithUtil.div(1, boxes.size()); Matrix matrix = new Matrix(1, 4); - for (Box box : boxes) { - matrix = MatrixOperation.add(matrix, box.getMatrixPosition()); + if (boxes.size() > 0) { + double nub = ArithUtil.div(1, boxes.size()); + for (Box box : boxes) { + matrix = MatrixOperation.add(matrix, box.getMatrixPosition()); + } + MatrixOperation.mathMul(matrix, nub); } - MatrixOperation.mathMul(matrix, nub); return matrix; } @@ -140,12 +142,14 @@ public class KClustering { } private Matrix average(List matrixList) throws Exception {//进行矩阵均值计算 - double nub = ArithUtil.div(1, matrixList.size()); Matrix matrix = new Matrix(1, length); - for (Box matrixBody1 : matrixList) { - matrix = MatrixOperation.add(matrix, matrixBody1.getMatrix()); + if (matrixList.size() > 0) { + double nub = ArithUtil.div(1, matrixList.size()); + for (Box matrixBody1 : matrixList) { + matrix = MatrixOperation.add(matrix, matrixBody1.getMatrix()); + } + MatrixOperation.mathMul(matrix, nub); } - MatrixOperation.mathMul(matrix, nub); return matrix; } @@ -154,9 +158,9 @@ public class KClustering { if (matrixList.size() > 1) { Random random = new Random(); for (int i = 0; i < matrices.length; i++) {//初始化均值向量 - //int index = random.nextInt(matrixList.size()); + int index = random.nextInt(matrixList.size()); //要进行深度克隆 - matrices[i] = matrixList.get(i).getMatrix(); + matrices[i] = matrixList.get(index).getMatrix(); } //进行两者的比较 boolean isEqual = false; diff --git a/src/main/java/org/wlld/imageRecognition/border/LVQ.java b/src/main/java/org/wlld/imageRecognition/border/LVQ.java index a5540b7..d08b079 100644 --- a/src/main/java/org/wlld/imageRecognition/border/LVQ.java +++ b/src/main/java/org/wlld/imageRecognition/border/LVQ.java @@ -19,7 +19,7 @@ public class LVQ { private double studyPoint = 0.1;//量化学习率 private int length;//向量长度(需要返回) private boolean isReady = false; - private int lvqNub = 50; + private int lvqNub; public void setTypeNub(int typeNub) { this.typeNub = typeNub; diff --git a/src/main/java/org/wlld/nerveEntity/OutNerve.java b/src/main/java/org/wlld/nerveEntity/OutNerve.java index 6dad146..d2760dd 100644 --- a/src/main/java/org/wlld/nerveEntity/OutNerve.java +++ b/src/main/java/org/wlld/nerveEntity/OutNerve.java @@ -40,7 +40,11 @@ public class OutNerve extends Nerve { double out = activeFunction.function(sigma); if (isStudy) {//输出结果并进行BP调整权重及阈值 outNub = out; - this.E = E.get(getId()); + if (E.containsKey(getId())) { + this.E = E.get(getId()); + } else { + this.E = 0; + } gradient = outGradient();//当前梯度变化 //调整权重 修改阈值 并进行反向传播 updatePower(eventId); @@ -61,6 +65,8 @@ public class OutNerve extends Nerve { Matrix myMatrix = dynamicNerve(matrix, eventId, isKernelStudy); if (isKernelStudy) {//回传 Matrix matrix1 = matrixMapE.get(E); + //System.out.println("E================" + E); + //System.out.println(myMatrix.getString()); if (matrix1.getX() <= myMatrix.getX() && matrix1.getY() <= myMatrix.getY()) { double g = getGradient(myMatrix, matrix1); backMatrix(g, eventId); diff --git a/src/test/java/org/wlld/HelloWorld.java b/src/test/java/org/wlld/HelloWorld.java index 2348ad5..9df5e35 100644 --- a/src/test/java/org/wlld/HelloWorld.java +++ b/src/test/java/org/wlld/HelloWorld.java @@ -3,6 +3,7 @@ package org.wlld; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.wlld.MatrixTools.Matrix; +import org.wlld.config.Classifier; import org.wlld.config.StudyPattern; import org.wlld.imageRecognition.Operation; import org.wlld.imageRecognition.Picture; @@ -22,57 +23,84 @@ import java.util.Map; */ public class HelloWorld { public static void main(String[] args) throws Exception { - test(); + food(); //testPic(); //testModel(); } - - public static void test() throws Exception { + public static void food() throws Exception { Picture picture = new Picture(); TempleConfig templeConfig = new TempleConfig(); //templeConfig.setHavePosition(true); // Frame frame = new Frame(); -// frame.setWidth(3024); -// frame.setHeight(4032); -// frame.setLengthHeight(100); -// frame.setLengthWidth(100); +// frame.setWidth(640); +// frame.setHeight(640); +// frame.setLengthHeight(640); +// frame.setLengthWidth(640); // templeConfig.setFrame(frame); - ModelParameter modelParameter = JSONObject.parseObject(ModelData.DATA, ModelParameter.class); - templeConfig.init(StudyPattern.Accuracy_Pattern, true, 1076, 1436, 1); - templeConfig.insertModel(modelParameter); + templeConfig.setClassifier(Classifier.DNN); + templeConfig.init(StudyPattern.Accuracy_Pattern, true, 640, 640, 2); Operation operation = new Operation(templeConfig); - - for (int i = 1; i < 100; i++) {//faster rcnn神经网络学习 - System.out.println("study==" + i); + //a b c d 物品 e是背景 + //一阶段 + for (int i = 1; i < 290; i++) {//一阶段 + System.out.println("study1==" + i); //读取本地URL地址图片,并转化成矩阵 - Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + i + ".png"); - Matrix wrong = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/b/b" + i + ".png"); + Matrix a = picture.getImageMatrixByLocal("D:\\share\\picture/a" + i + ".png"); + Matrix b = picture.getImageMatrixByLocal("D:\\share\\picture/b" + i + ".png"); + //Matrix c = picture.getImageMatrixByLocal("D:\\share\\picture/c" + i + ".png"); + //Matrix d = picture.getImageMatrixByLocal("D:\\share\\picture/d" + i + ".png"); + //Matrix f = picture.getImageMatrixByLocal("D:\\share\\picture/f" + i + ".png"); //将图像矩阵和标注加入进行学习,Accuracy_Pattern 模式 进行第二次学习 //第二次学习的时候,第三个参数必须是 true - operation.learning(right, 1, true); - operation.learning(wrong, 0, true); + // operation.learning(f, 0, false); + operation.learning(a, 0, false); + operation.learning(b, 1, false); + //operation.learning(c, 2, false); + // operation.learning(d, 3, false); } -// //精准模式在全部学习结束的时候一定要使用此方法,速度模式不要调用此方法 - templeConfig.startLvq();//原型向量量化 -// templeConfig.boxStudy();//边框回归 -// for (int j = 1; j < 2; j++) { -// Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + j + ".png"); -// Map> map = operation.lookWithPosition(right, j); -// System.out.println("j===" + j); -// } - //测试集图片,进行识别测试 -// for (int j = 121; j < 140; j++) { -// Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + j + ".png"); -// Matrix wrong = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/b/b" + j + ".png"); -// int rightId = operation.toSee(right); -// int wrongId = operation.toSee(wrong); -// System.out.println("该图是菜单:" + rightId); -// System.out.println("该图是桌子:" + wrongId); -// } - + System.out.println("一阶段完毕================"); + //二阶段 + for (int i = 1; i < 290; i++) { + System.out.println("study2==" + i); + //读取本地URL地址图片,并转化成矩阵 + Matrix a = picture.getImageMatrixByLocal("D:\\share\\picture/a" + i + ".png"); + Matrix b = picture.getImageMatrixByLocal("D:\\share\\picture/b" + i + ".png"); + //Matrix c = picture.getImageMatrixByLocal("D:\\share\\picture/c" + i + ".png"); + // Matrix d = picture.getImageMatrixByLocal("D:\\share\\picture/d" + i + ".png"); + //Matrix f = picture.getImageMatrixByLocal("D:\\share\\picture/f" + i + ".png"); + //将图像矩阵和标注加入进行学习,Accuracy_Pattern 模式 进行第二次学习 + //第二次学习的时候,第三个参数必须是 true + // operation.learning(f, 0, true); + operation.learning(a, 0, true); + operation.learning(b, 1, true); + //operation.learning(c, 2, true); + // operation.learning(d, 3, true); + } + templeConfig.finishStudy();//结束学习 + for (int i = 290; i < 301; i++) { + //读取本地URL地址图片,并转化成矩阵 + Matrix a = picture.getImageMatrixByLocal("D:\\share\\picture/a" + i + ".png"); + Matrix b = picture.getImageMatrixByLocal("D:\\share\\picture/b" + i + ".png"); + //Matrix c = picture.getImageMatrixByLocal("D:\\share\\picture/c" + i + ".png"); + // Matrix d = picture.getImageMatrixByLocal("D:\\share\\picture/d" + i + ".png"); + //将图像矩阵和标注加入进行学习,Accuracy_Pattern 模式 进行第二次学习 + //第二次学习的时候,第三个参数必须是 true + int an = operation.toSee(a); + int bn = operation.toSee(b); + //int cn = operation.toSee(c); + //int dn = operation.toSee(d); + System.out.println("这是0==" + an); + System.out.println("这是1==" + bn); + //System.out.println("这是2==" + cn); + //System.out.println("这是3==" + dn); + } +// ModelParameter modelParameter2 = templeConfig.getModel(); +// String model1 = JSON.toJSONString(modelParameter2); +// System.out.println("完成阶段==" + model1); } + public static void testPic() throws Exception { //测试SPEED模式学习过程 //初始化图像转矩阵类:作用就是说将一个图片文件转化为矩阵类 diff --git a/src/test/java/org/wlld/ModelData.java b/src/test/java/org/wlld/ModelData.java index d2307a8..358128c 100644 --- a/src/test/java/org/wlld/ModelData.java +++ b/src/test/java/org/wlld/ModelData.java @@ -8,4 +8,5 @@ package org.wlld; public class ModelData { public static final String DATA = "{\"borderBodyMap\":{},\"dymNerveStudies\":[{\"list\":[0.17219079338704957,0.2763277109442507,0.9494048971996161,0.6102827011347881,0.5147286506308538,0.7986383832057466,0.07067447220703016,0.09597199886439445,0.592226095059381],\"threshold\":0.2313533959212353},{\"list\":[0.006345633475881796,0.9125707391532067,0.8247156297251186,0.7069092127630808,0.35693418734094406,0.8009079323334614,0.11939034586272823,0.7654188856037588,0.2928824973142261],\"threshold\":0.682409278380978},{\"list\":[0.03888564362061908,0.7561642625034624,0.4948713698741891,0.6801324357405192,0.2905823279809705,0.7145377814192968,0.7834921462660909,0.29037864226052856,0.9120919748762001],\"threshold\":-0.0135320723729325},{\"list\":[0.5444683957099418,0.6561427315930956,0.5969273341608405,0.18422830427903736,0.22460070534129006,0.6702807575773493,0.42782028259035876,0.4972072545345054,0.7993596860458512],\"threshold\":-0.2821005667362801},{\"list\":[0.9331788407720958,0.9502756008967073,0.2012586093867874,0.29443194397996486,0.8161755080391601,0.6284886837805234,0.1469260344801857,0.919777359973982,0.16901058803535995],\"threshold\":-0.47302239316895}],\"dymOutNerveStudy\":{\"list\":[0.4641878653734878,0.3409186955332846,0.8582614720215028,0.11834201951979306,0.1727875779799236,0.7555896417968606,0.5281598748423247,0.4227867096522142,0.7909028674750446],\"threshold\":-1.235831384076553}}"; public static final String DATA2 = "{\"borderMap\":{1:{\"length\":20,\"lists\":[[5.844403568065988,5.50778773956534,5.606414633733051,0.0,5.386920887403405,5.233412885479294,5.162840178567301,0.0,5.1255623267136405,5.141830665730661,5.1657816883884315,0.0,5.004452804593025,4.9798569063545655,5.016636247961047,0.0,5.749189888514196,5.70190948209303,5.801605198176744,0.0],[5.828724996533473,5.493283554607389,5.631974619349194,0.0,5.317736704655773,5.218094817311813,5.212689578814799,0.0,5.082924739086342,5.132779708136464,5.164620746418376,0.0,4.911496938930719,4.988858771682838,5.046763317425185,0.0,5.699177931446964,5.698029263370381,5.801805811009228,0.0],[5.860473429990608,5.45589287402917,5.570510436591154,0.0,5.518068147003296,5.192524064124404,5.139476802116294,0.0,5.291255554753069,5.054683165036734,5.097237514753458,0.0,5.10970902525052,4.920070729882904,4.93876825132474,0.0,5.781853445215731,5.691894972408872,5.804011696862248,0.0],[5.903759460472417,5.477826289715017,5.5611454606226545,0.0,5.694718254634795,5.161641926142435,5.109002098167241,0.0,5.497617617278256,4.98657986065054,5.0652895669803675,0.0,5.282389409342257,4.812592396284728,4.882106667356817,0.0,5.8566360867764535,5.685686228353719,5.797862243284943,0.0],[5.994468916865423,5.568337986048426,5.500639788937314,0.0,5.861039943193394,5.260253165540613,5.068570988314742,0.0,5.767041352159131,5.062201450068188,5.012072010383438,0.0,5.669517050960502,4.904950004714434,4.805664344910293,0.0,5.95990277418828,5.72475239614638,5.77466481550324,0.0],[6.015426903883286,5.8470514572899015,5.885381874181652,0.0,5.507452374788038,5.128588487781482,5.15060241651049,0.0,5.246789986410181,5.062232576109659,5.10186018477504,0.0,5.088615013578635,4.848978165449897,4.83972833413785,0.0,4.988274392693213,4.819633224446484,4.9986420367282935,0.0],[6.247511704207002,5.660231890395592,5.476163966208906,0.0,6.144336683624375,5.329570242640683,5.215598110014992,0.0,5.995249012064939,5.226389717498529,5.091125671269078,0.0,5.857181537483512,5.041241662182233,4.894810796367934,0.0,5.990115513074854,5.652444003499067,5.646581153997495,0.0],[6.291073776097935,5.852998776895548,5.800288118121379,0.0,6.228340775698824,5.30632808574176,5.198200168874879,0.0,6.115507285987589,5.197095838194831,5.22581281722505,0.0,5.941234881776568,5.069861591728098,5.002997254155649,0.0,5.845482073076225,5.149997054509098,5.208096994702694,0.0],[6.015333937535676,5.697840060888947,5.731947117389336,0.0,5.724243076613664,5.211112945683922,5.187132851530306,0.0,5.561896755619004,5.012273549882052,5.042722358474358,0.0,5.344199441967036,4.813943212098502,4.860831304693194,0.0,5.520890402105406,5.261313172882038,5.396896792524224,0.0],[6.134295025815421,5.866263113237292,5.848159090409577,0.0,5.880281177905433,5.260231535098279,5.056749218567724,0.0,5.780212042383378,5.109105625941937,5.051923827509647,0.0,5.691242958767389,4.9064556031645035,4.716943010959617,0.0,5.529219860779304,4.906170955094314,4.977027090238533,0.0]],\"positionMap\":{0:{\"list\":[5.844403568065988,5.50778773956534,5.606414633733051,0.0,5.386920887403405,5.233412885479294,5.162840178567301,0.0,5.1255623267136405,5.141830665730661,5.1657816883884315,0.0,5.004452804593025,4.9798569063545655,5.016636247961047,0.0,5.749189888514196,5.70190948209303,5.801605198176744,0.0],\"positionList\":[0.037617866,0.0514890801,-0.24683371333757,-0.2536925224250764]},1:{\"list\":[5.828724996533473,5.493283554607389,5.631974619349194,0.0,5.317736704655773,5.218094817311813,5.212689578814799,0.0,5.082924739086342,5.132779708136464,5.164620746418376,0.0,4.911496938930719,4.988858771682838,5.046763317425185,0.0,5.699177931446964,5.698029263370381,5.801805811009228,0.0],\"positionList\":[0.03711449842542006,0.03795972393995935,-0.24544295499236052,-0.2504543499333437]},2:{\"list\":[5.860473429990608,5.45589287402917,5.570510436591154,0.0,5.518068147003296,5.192524064124404,5.139476802116294,0.0,5.291255554753069,5.054683165036734,5.097237514753458,0.0,5.10970902525052,4.920070729882904,4.93876825132474,0.0,5.781853445215731,5.691894972408872,5.804011696862248,0.0],\"positionList\":[0.03647642678,0.0675049636,-0.24759640748608083,-0.2546522259388265]},3:{\"list\":[5.903759460472417,5.477826289715017,5.5611454606226545,0.0,5.694718254634795,5.161641926142435,5.109002098167241,0.0,5.497617617278256,4.98657986065054,5.0652895669803675,0.0,5.282389409342257,4.812592396284728,4.882106667356817,0.0,5.8566360867764535,5.685686228353719,5.797862243284943,0.0],\"positionList\":[0.03591811415,0.085125744525,-0.2476811083485065,-0.25449237438864614]},4:{\"list\":[5.994468916865423,5.568337986048426,5.500639788937314,0.0,5.861039943193394,5.260253165540613,5.068570988314742,0.0,5.767041352159131,5.062201450068188,5.012072010383438,0.0,5.669517050960502,4.904950004714434,4.805664344910293,0.0,5.95990277418828,5.72475239614638,5.77466481550324,0.0],\"positionList\":[0.03496277915,0.12174056916,-0.2479780599388255,-0.2503803531768641]},5:{\"list\":[6.015426903883286,5.8470514572899015,5.885381874181652,0.0,5.507452374788038,5.128588487781482,5.15060241651049,0.0,5.246789986410181,5.062232576109659,5.10186018477504,0.0,5.088615013578635,4.848978165449897,4.83972833413785,0.0,4.988274392693213,4.819633224446484,4.9986420367282935,0.0],\"positionList\":[0.09633108823289614,0.05315779520730831,-0.21653264461816674,-0.21712225970646373]},6:{\"list\":[6.247511704207002,5.660231890395592,5.476163966208906,0.0,6.144336683624375,5.329570242640683,5.215598110014992,0.0,5.995249012064939,5.226389717498529,5.091125671269078,0.0,5.857181537483512,5.041241662182233,4.894810796367934,0.0,5.990115513074854,5.652444003499067,5.646581153997495,0.0],\"positionList\":[0.04890593282617866,0.16048974203776248,-0.24497042918979597,-0.24030927868709873]},7:{\"list\":[6.291073776097935,5.852998776895548,5.800288118121379,0.0,6.228340775698824,5.30632808574176,5.198200168874879,0.0,6.115507285987589,5.197095838194831,5.22581281722505,0.0,5.941234881776568,5.069861591728098,5.002997254155649,0.0,5.845482073076225,5.149997054509098,5.208096994702694,0.0],\"positionList\":[0.08601398609965034,0.16643102116188557,-0.22904218735776002,-0.21603066902282053]},8:{\"list\":[6.015333937535676,5.697840060888947,5.731947117389336,0.0,5.724243076613664,5.211112945683922,5.187132851530306,0.0,5.561896755619004,5.012273549882052,5.042722358474358,0.0,5.344199441967036,4.813943212098502,4.860831304693194,0.0,5.520890402105406,5.261313172882038,5.396896792524224,0.0],\"positionList\":[0.07617865997880763,0.08175136710010798,-0.23273639679966462,-0.2403522604706781]},9:{\"list\":[6.134295025815421,5.866263113237292,5.848159090409577,0.0,5.880281177905433,5.260231535098279,5.056749218567724,0.0,5.780212042383378,5.109105625941937,5.051923827509647,0.0,5.691242958767389,4.9064556031645035,4.716943010959617,0.0,5.529219860779304,4.906170955094314,4.977027090238533,0.0],\"positionList\":[0.09309722534523573,0.11906624150627519,-0.22155152795041738,-0.2166732995703171]}},\"speciesQuantity\":10}},\"depthNerves\":[],\"dymNerveStudies\":[{\"list\":[0.17219079338704957,0.2763277109442507,0.9494048971996161,0.6102827011347881,0.5147286506308538,0.7986383832057466,0.07067447220703016,0.09597199886439445,0.592226095059381],\"threshold\":0.2313533959212353},{\"list\":[0.006345633475881796,0.9125707391532067,0.8247156297251186,0.7069092127630808,0.35693418734094406,0.8009079323334614,0.11939034586272823,0.7654188856037588,0.2928824973142261],\"threshold\":0.682409278380978},{\"list\":[0.03888564362061908,0.7561642625034624,0.4948713698741891,0.6801324357405192,0.2905823279809705,0.7145377814192968,0.7834921462660909,0.29037864226052856,0.9120919748762001],\"threshold\":-0.0135320723729325},{\"list\":[0.5444683957099418,0.6561427315930956,0.5969273341608405,0.18422830427903736,0.22460070534129006,0.6702807575773493,0.42782028259035876,0.4972072545345054,0.7993596860458512],\"threshold\":-0.2821005667362801},{\"list\":[0.9331788407720958,0.9502756008967073,0.2012586093867874,0.29443194397996486,0.8161755080391601,0.6284886837805234,0.1469260344801857,0.919777359973982,0.16901058803535995],\"threshold\":-0.47302239316895}],\"dymOutNerveStudy\":{\"list\":[0.4641878653734878,0.3409186955332846,0.8582614720215028,0.11834201951979306,0.1727875779799236,0.7555896417968606,0.5281598748423247,0.4227867096522142,0.7909028674750446],\"threshold\":-1.235831384076553},\"frame\":{\"height\":4032,\"lengthHeight\":100,\"lengthWidth\":100,\"ready\":true,\"width\":3024},\"lvqModel\":{\"length\":20,\"matrixModelList\":[{\"id\":0,\"rowVector\":[4.098096173380622,4.215497860369288,4.389667994539792,2.2130496389542597E-272,4.430074041164493,4.71630523945971,4.944475103579248,4.426099277908547E-272,4.4858637341423,4.828818752344372,5.141220791523923,0.0,4.4411855202371315,4.728053548555644,4.980377782992652,3.872836868169969E-272,4.25523669368397,4.540821453767527,4.799112191553137,1.6597872292156935E-272]},{\"id\":1,\"rowVector\":[5.991103611760031,5.670551319427767,5.7082362755851515,2.766312048692812E-272,5.671719685759619,5.202248842700966,5.179688402894632,5.532624097385643E-273,5.484494000861708,5.024549622198204,5.0422257425624135,5.532624097385643E-273,5.2837377653930675,4.825639850935169,4.852313231966156,2.2130496389542597E-272,5.409886336844953,5.152850162463683,5.292284878430469,4.97936168764709E-272]}],\"typeNub\":2},\"outNerves\":[]}"; + public static final String DATA3 = ""; }