test分支要进行大改,大改进行中

pull/2/head
Administrator 5 years ago
parent eb5a629d89
commit 87cdeed935

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: com.alibaba:fastjson:1.2.51" level="project" />
</component>
</module>

@ -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;//使用特征向量均值分类
}

@ -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) {
}
}

@ -3,11 +3,13 @@ package org.wlld.imageRecognition;
import org.wlld.MatrixTools.Matrix; import org.wlld.MatrixTools.Matrix;
import org.wlld.MatrixTools.MatrixOperation; import org.wlld.MatrixTools.MatrixOperation;
import org.wlld.config.Classifier;
import org.wlld.config.StudyPattern; import org.wlld.config.StudyPattern;
import org.wlld.i.OutBack; import org.wlld.i.OutBack;
import org.wlld.imageRecognition.border.*; import org.wlld.imageRecognition.border.*;
import org.wlld.nerveEntity.SensoryNerve; import org.wlld.nerveEntity.SensoryNerve;
import org.wlld.tools.ArithUtil; import org.wlld.tools.ArithUtil;
import org.wlld.tools.IdCreator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -55,7 +57,7 @@ public class Operation {//进行计算
public void study(Matrix matrix, Map<Integer, Double> tagging) throws Exception { public void study(Matrix matrix, Map<Integer, Double> tagging) throws Exception {
if (templeConfig.getStudyPattern() == StudyPattern.Speed_Pattern) { if (templeConfig.getStudyPattern() == StudyPattern.Speed_Pattern) {
List<Double> list = convolution(matrix, tagging); List<Double> list = convolution(matrix, tagging);
intoNerve(1, list, templeConfig.getSensoryNerves(), true, tagging, null); intoDnnNetwork(1, list, templeConfig.getSensoryNerves(), true, tagging, null);
} else { } else {
throw new Exception("pattern is wrong"); throw new Exception("pattern is wrong");
} }
@ -74,7 +76,7 @@ public class Operation {//进行计算
isKernelStudy = false; isKernelStudy = false;
} }
//进卷积网络 //进卷积网络
intoNerve2(1, matrix, templeConfig.getConvolutionNerveManager().getSensoryNerves(), intoConvolutionNetwork(1, matrix, templeConfig.getConvolutionNerveManager().getSensoryNerves(),
isKernelStudy, tagging, matrixBack); isKernelStudy, tagging, matrixBack);
if (isNerveStudy) { if (isNerveStudy) {
//卷积后的结果 //卷积后的结果
@ -82,7 +84,38 @@ public class Operation {//进行计算
if (templeConfig.isHavePosition() && tagging > 0) { if (templeConfig.isHavePosition() && tagging > 0) {
border.end(myMatrix, tagging); border.end(myMatrix, tagging);
} }
//进行聚类 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<Integer, Double> map = new HashMap<>();
map.put(tagging, 1.0);
List<Double> 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(); LVQ lvq = templeConfig.getLvq();
Matrix vector = MatrixOperation.matrixToVector(myMatrix, true); Matrix vector = MatrixOperation.matrixToVector(myMatrix, true);
MatrixBody matrixBody = new MatrixBody(); MatrixBody matrixBody = new MatrixBody();
@ -90,16 +123,22 @@ public class Operation {//进行计算
matrixBody.setId(tagging); matrixBody.setId(tagging);
lvq.insertMatrixBody(matrixBody); lvq.insertMatrixBody(matrixBody);
} }
} else {
throw new Exception("pattern is wrong"); private List<Double> getFeature(Matrix matrix) throws Exception {//将特征矩阵转化为集合并除10
List<Double> 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 模式 //图像视觉 speed 模式
public void look(Matrix matrix, long eventId) throws Exception { public void look(Matrix matrix, long eventId) throws Exception {
if (templeConfig.getStudyPattern() == StudyPattern.Speed_Pattern) { if (templeConfig.getStudyPattern() == StudyPattern.Speed_Pattern) {
List<Double> list = convolution(matrix, null); List<Double> 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) { } else if (templeConfig.getStudyPattern() == StudyPattern.Accuracy_Pattern) {
throw new Exception("studyPattern not right"); throw new Exception("studyPattern not right");
} }
@ -127,18 +166,18 @@ public class Operation {//进行计算
List<Double> list = sub(matrix1); List<Double> list = sub(matrix1);
imageBack.setFrameBody(frameBody); 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()); return toPosition(frameBodies, frame.getWidth(), frame.getHeight());
} else if (templeConfig.getStudyPattern() == StudyPattern.Accuracy_Pattern) { } else if (templeConfig.getStudyPattern() == StudyPattern.Accuracy_Pattern) {
for (FrameBody frameBody : frameBodies) { for (FrameBody frameBody : frameBodies) {
intoNerve2(eventId, frameBody.getMatrix(), templeConfig.getConvolutionNerveManager().getSensoryNerves(), intoConvolutionNetwork(eventId, frameBody.getMatrix(), templeConfig.getConvolutionNerveManager().getSensoryNerves(),
false, -1, matrixBack); false, -1, matrixBack);
Matrix myMatrix = matrixBack.getMatrix(); Matrix myMatrix = matrixBack.getMatrix();
//卷积层输出即边框回归的输入的特征向量 //卷积层输出即边框回归的输入的特征向量
frameBody.setEndMatrix(myMatrix); frameBody.setEndMatrix(myMatrix);
Matrix vector = MatrixOperation.matrixToVector(myMatrix, true); //Matrix vector = MatrixOperation.matrixToVector(myMatrix, true);
int id = getClassificationId2(vector); int id = getClassificationIdByLVQ(myMatrix);
frameBody.setId(id); frameBody.setId(id);
} }
return toPosition(frameBodies, frame.getWidth(), frame.getHeight()); return toPosition(frameBodies, frame.getWidth(), frame.getHeight());
@ -209,7 +248,7 @@ public class Operation {//进行计算
int id = frameBody.getId(); int id = frameBody.getId();
int x = frameBody.getX(); int x = frameBody.getX();
int y = frameBody.getY(); int y = frameBody.getY();
KClustering kClustering = templeConfig.getkClusteringMap().get(id); KClustering kClustering = templeConfig.getKClusteringMap().get(id);
Map<Integer, Box> boxMap = kClustering.getPositionMap(); Map<Integer, Box> boxMap = kClustering.getPositionMap();
//将矩阵化为向量 //将矩阵化为向量
matrix = MatrixOperation.matrixToVector(matrix, true); matrix = MatrixOperation.matrixToVector(matrix, true);
@ -285,17 +324,54 @@ public class Operation {//进行计算
//图像视觉 Accuracy 模式 //图像视觉 Accuracy 模式
public int toSee(Matrix matrix) throws Exception { public int toSee(Matrix matrix) throws Exception {
if (templeConfig.getStudyPattern() == StudyPattern.Accuracy_Pattern) { if (templeConfig.getStudyPattern() == StudyPattern.Accuracy_Pattern) {
intoNerve2(2, matrix, templeConfig.getConvolutionNerveManager().getSensoryNerves(), intoConvolutionNetwork(2, matrix, templeConfig.getConvolutionNerveManager().getSensoryNerves(),
false, -1, matrixBack); false, 0, matrixBack);
Matrix myMatrix = matrixBack.getMatrix(); Matrix myMatrix = matrixBack.getMatrix();
Matrix vector = MatrixOperation.matrixToVector(myMatrix, true); int classifier = templeConfig.getClassifier();
return getClassificationId2(vector); 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 { } else {
throw new Exception("pattern is wrong"); throw new Exception("pattern is wrong");
} }
} }
private int getClassificationId2(Matrix myVector) throws Exception { private int getClassificationIdByDnn(Matrix myMatrix) throws Exception {
List<Double> 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<Integer, Matrix> matrixK = templeConfig.getVectorK().getMatrixK();
double minDist = 0;
int id = 0;
for (Map.Entry<Integer, Matrix> 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; int id = 0;
double distEnd = 0; double distEnd = 0;
LVQ lvq = templeConfig.getLvq(); LVQ lvq = templeConfig.getLvq();
@ -328,18 +404,34 @@ public class Operation {//进行计算
return list; return list;
} }
private void intoNerve(long eventId, List<Double> featureList, List<SensoryNerve> sensoryNerveList /*
, boolean isStudy, Map<Integer, Double> map, OutBack imageBack) throws Exception { * @param eventId ID
* @param featureList
* @param sensoryNerveList
* @param isStudy
* @param map
* @param outBack
* */
private void intoDnnNetwork(long eventId, List<Double> featureList, List<SensoryNerve> sensoryNerveList
, boolean isStudy, Map<Integer, Double> map, OutBack outBack) throws Exception {//进入DNN 神经网络
for (int i = 0; i < sensoryNerveList.size(); i++) { for (int i = 0; i < sensoryNerveList.size(); i++) {
sensoryNerveList.get(i).postMessage(eventId, featureList.get(i), isStudy, map sensoryNerveList.get(i).postMessage(eventId, featureList.get(i), isStudy, map
, imageBack); , outBack);
} }
} }
private void intoNerve2(long eventId, Matrix featur, List<SensoryNerve> 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<SensoryNerve> sensoryNerveList
, boolean isKernelStudy, int E, OutBack outBack) throws Exception {//进入卷积神经网络
for (int i = 0; i < sensoryNerveList.size(); i++) { 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);
} }
} }
} }

@ -1,6 +1,7 @@
package org.wlld.imageRecognition; package org.wlld.imageRecognition;
import org.wlld.MatrixTools.Matrix; import org.wlld.MatrixTools.Matrix;
import org.wlld.config.Classifier;
import org.wlld.config.StudyPattern; import org.wlld.config.StudyPattern;
import org.wlld.function.ReLu; import org.wlld.function.ReLu;
import org.wlld.function.Sigmod; import org.wlld.function.Sigmod;
@ -23,7 +24,6 @@ import java.util.Map;
public class TempleConfig { public class TempleConfig {
private NerveManager nerveManager;//神经网络管理器 private NerveManager nerveManager;//神经网络管理器
private NerveManager convolutionNerveManager;//卷积神经网络管理器 private NerveManager convolutionNerveManager;//卷积神经网络管理器
private double cutThreshold = 10;//切割阈值默认值
private int row = 5;//行的最小比例 private int row = 5;//行的最小比例
private int column = 3;//列的最小比例 private int column = 3;//列的最小比例
private int deep = 2;//默认深度 private int deep = 2;//默认深度
@ -35,11 +35,44 @@ public class TempleConfig {
private double th = 0.6;//标准阈值 private double th = 0.6;//标准阈值
private boolean boxReady = false;//边框已经学习完毕 private boolean boxReady = false;//边框已经学习完毕
private double iouTh = 0.5;//IOU阈值 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<Integer, KClustering> entry : kClusteringMap.entrySet()) {
entry.getValue().start();
}
boxReady = true;
}
}
private Map<Integer, KClustering> kClusteringMap = new HashMap<>(); private Map<Integer, KClustering> kClusteringMap = new HashMap<>();
public Map<Integer, KClustering> getkClusteringMap() { public Map<Integer, KClustering> getKClusteringMap() {
return kClusteringMap; return kClusteringMap;
} }
@ -79,26 +112,10 @@ public class TempleConfig {
this.frame = frame; this.frame = frame;
} }
public void startLvq() throws Exception {//进行量化
if (studyPattern == StudyPattern.Accuracy_Pattern) {
lvq.start();
}
}
public LVQ getLvq() { public LVQ getLvq() {
return lvq; return lvq;
} }
public void boxStudy() throws Exception {//边框回归 学习结束之后最后进行调用
if (isHavePosition) {
for (Map.Entry<Integer, KClustering> entry : kClusteringMap.entrySet()) {
entry.getValue().start();
}
boxReady = true;
}
}
public NerveManager getNerveManager() { public NerveManager getNerveManager() {
return nerveManager; return nerveManager;
} }
@ -163,15 +180,25 @@ public class TempleConfig {
nerveManager.init(initPower, false); 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; int deep = 0;
lvq = new LVQ(classificationNub + 1, lvqNub);
Map<Integer, Matrix> matrixMap = new HashMap<>();//主键与期望矩阵的映射 Map<Integer, Matrix> matrixMap = new HashMap<>();//主键与期望矩阵的映射
while (width > 5 && height > 5) { while (width > 5 && height > 5) {
width = width / 3; width = width / 3;
height = height / 3; height = height / 3;
deep++; 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)); matrixMap.put(0, new Matrix(height, width));
double nub = 10;//每个分类期望参数的跨度 double nub = 10;//每个分类期望参数的跨度
@ -179,7 +206,7 @@ public class TempleConfig {
Matrix matrix = new Matrix(height, width);//初始化期望矩阵 Matrix matrix = new Matrix(height, width);//初始化期望矩阵
double t = (k + 1) * nub;//期望矩阵的分类参数数值 double t = (k + 1) * nub;//期望矩阵的分类参数数值
for (int i = 0; i < height; i++) {//给期望矩阵注入期望参数 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); matrix.setNub(i, j, t);
} }
} }
@ -242,8 +269,6 @@ public class TempleConfig {
lists.add(rowVectorToList(matrices[i])); lists.add(rowVectorToList(matrices[i]));
} }
} else {
throw new Exception("not ready");
} }
} }
@ -270,7 +295,7 @@ public class TempleConfig {
modelParameter.setDepthNerves(modelParameter1.getDepthNerves()); modelParameter.setDepthNerves(modelParameter1.getDepthNerves());
modelParameter.setOutNerves(modelParameter1.getOutNerves()); modelParameter.setOutNerves(modelParameter1.getOutNerves());
} }
if (isHavePosition) {//存在边框学习模型参数 if (isHavePosition && kClusteringMap != null && kClusteringMap.size() > 0) {//存在边框学习模型参数
Map<Integer, KBorder> kBorderMap = kToBody(); Map<Integer, KBorder> kBorderMap = kToBody();
modelParameter.setFrame(frame); modelParameter.setFrame(frame);
modelParameter.setBorderMap(kBorderMap); modelParameter.setBorderMap(kBorderMap);
@ -376,16 +401,11 @@ public class TempleConfig {
boxMap.put(boxEntry.getKey(), box); boxMap.put(boxEntry.getKey(), box);
} }
} }
} }
} }
} }
} }
public void setCutThreshold(double cutThreshold) {
this.cutThreshold = cutThreshold;
}
public int getDeep() { public int getDeep() {
return deep; return deep;
} }

@ -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<Integer, List<Matrix>> matrixMap = new HashMap<>();
private Map<Integer, Matrix> matrixK = new HashMap<>();
private int length;
public VectorK(int length) {
this.length = length;
}
public Map<Integer, Matrix> getMatrixK() {
return matrixK;
}
public void insertMatrix(int type, Matrix matrix) throws Exception {
if (matrix.isRowVector() && matrix.getY() == length) {
if (matrixMap.containsKey(type)) {
List<Matrix> matrixList = matrixMap.get(type);
matrixList.add(matrix);
} else {
List<Matrix> list = new ArrayList<>();
list.add(matrix);
matrixMap.put(type, list);
}
} else {
throw new Exception("MATRIX IS NOT RIGHT");
}
}
private Matrix sigma(List<Matrix> 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<Matrix> matrixList) throws Exception {//拿中位数
Matrix matrix = new Matrix(1, length);
List<List<Double>> lists = new ArrayList<>();
for (Matrix matrix1 : matrixList) {
for (int i = 0; i < matrix1.getY(); i++) {
if (lists.size() <= i) {
lists.add(new ArrayList<>());
}
List<Double> list = lists.get(i);
list.add(matrix1.getNumber(0, i));
Collections.sort(list);
}
}
for (int i = 0; i < length; i++) {
List<Double> 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<Integer, List<Matrix>> entry : matrixMap.entrySet()) {
List<Matrix> matrixList = entry.getValue();
Matrix matrix = sigma(matrixList);
matrixK.put(entry.getKey(), matrix);
}
}
}

@ -111,12 +111,14 @@ public class KClustering {
} }
private Matrix averagePosition(List<Box> boxes) throws Exception { private Matrix averagePosition(List<Box> boxes) throws Exception {
double nub = ArithUtil.div(1, boxes.size());
Matrix matrix = new Matrix(1, 4); Matrix matrix = new Matrix(1, 4);
if (boxes.size() > 0) {
double nub = ArithUtil.div(1, boxes.size());
for (Box box : boxes) { for (Box box : boxes) {
matrix = MatrixOperation.add(matrix, box.getMatrixPosition()); matrix = MatrixOperation.add(matrix, box.getMatrixPosition());
} }
MatrixOperation.mathMul(matrix, nub); MatrixOperation.mathMul(matrix, nub);
}
return matrix; return matrix;
} }
@ -140,12 +142,14 @@ public class KClustering {
} }
private Matrix average(List<Box> matrixList) throws Exception {//进行矩阵均值计算 private Matrix average(List<Box> matrixList) throws Exception {//进行矩阵均值计算
double nub = ArithUtil.div(1, matrixList.size());
Matrix matrix = new Matrix(1, length); Matrix matrix = new Matrix(1, length);
if (matrixList.size() > 0) {
double nub = ArithUtil.div(1, matrixList.size());
for (Box matrixBody1 : matrixList) { for (Box matrixBody1 : matrixList) {
matrix = MatrixOperation.add(matrix, matrixBody1.getMatrix()); matrix = MatrixOperation.add(matrix, matrixBody1.getMatrix());
} }
MatrixOperation.mathMul(matrix, nub); MatrixOperation.mathMul(matrix, nub);
}
return matrix; return matrix;
} }
@ -154,9 +158,9 @@ public class KClustering {
if (matrixList.size() > 1) { if (matrixList.size() > 1) {
Random random = new Random(); Random random = new Random();
for (int i = 0; i < matrices.length; i++) {//初始化均值向量 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; boolean isEqual = false;

@ -19,7 +19,7 @@ public class LVQ {
private double studyPoint = 0.1;//量化学习率 private double studyPoint = 0.1;//量化学习率
private int length;//向量长度(需要返回) private int length;//向量长度(需要返回)
private boolean isReady = false; private boolean isReady = false;
private int lvqNub = 50; private int lvqNub;
public void setTypeNub(int typeNub) { public void setTypeNub(int typeNub) {
this.typeNub = typeNub; this.typeNub = typeNub;

@ -40,7 +40,11 @@ public class OutNerve extends Nerve {
double out = activeFunction.function(sigma); double out = activeFunction.function(sigma);
if (isStudy) {//输出结果并进行BP调整权重及阈值 if (isStudy) {//输出结果并进行BP调整权重及阈值
outNub = out; outNub = out;
if (E.containsKey(getId())) {
this.E = E.get(getId()); this.E = E.get(getId());
} else {
this.E = 0;
}
gradient = outGradient();//当前梯度变化 gradient = outGradient();//当前梯度变化
//调整权重 修改阈值 并进行反向传播 //调整权重 修改阈值 并进行反向传播
updatePower(eventId); updatePower(eventId);
@ -61,6 +65,8 @@ public class OutNerve extends Nerve {
Matrix myMatrix = dynamicNerve(matrix, eventId, isKernelStudy); Matrix myMatrix = dynamicNerve(matrix, eventId, isKernelStudy);
if (isKernelStudy) {//回传 if (isKernelStudy) {//回传
Matrix matrix1 = matrixMapE.get(E); Matrix matrix1 = matrixMapE.get(E);
//System.out.println("E================" + E);
//System.out.println(myMatrix.getString());
if (matrix1.getX() <= myMatrix.getX() && matrix1.getY() <= myMatrix.getY()) { if (matrix1.getX() <= myMatrix.getX() && matrix1.getY() <= myMatrix.getY()) {
double g = getGradient(myMatrix, matrix1); double g = getGradient(myMatrix, matrix1);
backMatrix(g, eventId); backMatrix(g, eventId);

@ -3,6 +3,7 @@ package org.wlld;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import org.wlld.MatrixTools.Matrix; import org.wlld.MatrixTools.Matrix;
import org.wlld.config.Classifier;
import org.wlld.config.StudyPattern; import org.wlld.config.StudyPattern;
import org.wlld.imageRecognition.Operation; import org.wlld.imageRecognition.Operation;
import org.wlld.imageRecognition.Picture; import org.wlld.imageRecognition.Picture;
@ -22,56 +23,83 @@ import java.util.Map;
*/ */
public class HelloWorld { public class HelloWorld {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
test(); food();
//testPic(); //testPic();
//testModel(); //testModel();
} }
public static void food() throws Exception {
public static void test() throws Exception {
Picture picture = new Picture(); Picture picture = new Picture();
TempleConfig templeConfig = new TempleConfig(); TempleConfig templeConfig = new TempleConfig();
//templeConfig.setHavePosition(true); //templeConfig.setHavePosition(true);
// Frame frame = new Frame(); // Frame frame = new Frame();
// frame.setWidth(3024); // frame.setWidth(640);
// frame.setHeight(4032); // frame.setHeight(640);
// frame.setLengthHeight(100); // frame.setLengthHeight(640);
// frame.setLengthWidth(100); // frame.setLengthWidth(640);
// templeConfig.setFrame(frame); // templeConfig.setFrame(frame);
ModelParameter modelParameter = JSONObject.parseObject(ModelData.DATA, ModelParameter.class); templeConfig.setClassifier(Classifier.DNN);
templeConfig.init(StudyPattern.Accuracy_Pattern, true, 1076, 1436, 1); templeConfig.init(StudyPattern.Accuracy_Pattern, true, 640, 640, 2);
templeConfig.insertModel(modelParameter);
Operation operation = new Operation(templeConfig); Operation operation = new Operation(templeConfig);
//a b c d 物品 e是背景
for (int i = 1; i < 100; i++) {//faster rcnn神经网络学习 //一阶段
System.out.println("study==" + i); for (int i = 1; i < 290; i++) {//一阶段
System.out.println("study1==" + i);
//读取本地URL地址图片,并转化成矩阵 //读取本地URL地址图片,并转化成矩阵
Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + i + ".png"); Matrix a = picture.getImageMatrixByLocal("D:\\share\\picture/a" + i + ".png");
Matrix wrong = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/b/b" + 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 模式 进行第二次学习 //将图像矩阵和标注加入进行学习Accuracy_Pattern 模式 进行第二次学习
//第二次学习的时候,第三个参数必须是 true //第二次学习的时候,第三个参数必须是 true
operation.learning(right, 1, true); // operation.learning(f, 0, false);
operation.learning(wrong, 0, true); operation.learning(a, 0, false);
operation.learning(b, 1, false);
//operation.learning(c, 2, false);
// operation.learning(d, 3, false);
} }
// //精准模式在全部学习结束的时候一定要使用此方法,速度模式不要调用此方法 System.out.println("一阶段完毕================");
templeConfig.startLvq();//原型向量量化 //二阶段
// templeConfig.boxStudy();//边框回归 for (int i = 1; i < 290; i++) {
// for (int j = 1; j < 2; j++) { System.out.println("study2==" + i);
// Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + j + ".png"); //读取本地URL地址图片,并转化成矩阵
// Map<Integer, List<FrameBody>> map = operation.lookWithPosition(right, j); Matrix a = picture.getImageMatrixByLocal("D:\\share\\picture/a" + i + ".png");
// System.out.println("j===" + j); 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");
// for (int j = 121; j < 140; j++) { //Matrix f = picture.getImageMatrixByLocal("D:\\share\\picture/f" + i + ".png");
// Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + j + ".png"); //将图像矩阵和标注加入进行学习Accuracy_Pattern 模式 进行第二次学习
// Matrix wrong = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/b/b" + j + ".png"); //第二次学习的时候,第三个参数必须是 true
// int rightId = operation.toSee(right); // operation.learning(f, 0, true);
// int wrongId = operation.toSee(wrong); operation.learning(a, 0, true);
// System.out.println("该图是菜单:" + rightId); operation.learning(b, 1, true);
// System.out.println("该图是桌子:" + wrongId); //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 { public static void testPic() throws Exception {
//测试SPEED模式学习过程 //测试SPEED模式学习过程

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save